mirror of
https://github.com/chanzuckerberg/cellxgene.git
synced 2026-10-04 11:58:11 +08:00
Add a simple annotations cache to the server (#1220)
Reading the user annotations had a performance problem. The same file was read over and over. Now, the last set of user annotations are cached.
This commit is contained in:
@@ -92,6 +92,10 @@ class AnnotationsLocalFile(Annotations):
|
|||||||
# lock used to protect label file write ops
|
# lock used to protect label file write ops
|
||||||
self.label_lock = threading.RLock()
|
self.label_lock = threading.RLock()
|
||||||
|
|
||||||
|
# cache the most recent annotations
|
||||||
|
self.last_fname = None
|
||||||
|
self.last_labels = None
|
||||||
|
|
||||||
def is_safe_collection_name(self, name):
|
def is_safe_collection_name(self, name):
|
||||||
"""
|
"""
|
||||||
return true if this is a safe collection name
|
return true if this is a safe collection name
|
||||||
@@ -114,10 +118,20 @@ class AnnotationsLocalFile(Annotations):
|
|||||||
|
|
||||||
def read_labels(self, data_adaptor):
|
def read_labels(self, data_adaptor):
|
||||||
fname = self._get_filename(data_adaptor)
|
fname = self._get_filename(data_adaptor)
|
||||||
if fname is not None and os.path.exists(fname) and os.path.getsize(fname) > 0:
|
with self.label_lock:
|
||||||
return pd.read_csv(fname, dtype="category", index_col=0, header=0, comment="#", keep_default_na=False)
|
if fname is not None and os.path.exists(fname) and os.path.getsize(fname) > 0:
|
||||||
else:
|
# returned the cached labels if possible, otherwise read them from the file
|
||||||
return pd.DataFrame()
|
if fname == self.last_fname:
|
||||||
|
return self.last_labels
|
||||||
|
else:
|
||||||
|
labels = pd.read_csv(fname, dtype="category",
|
||||||
|
index_col=0, header=0, comment="#", keep_default_na=False)
|
||||||
|
# update the cache
|
||||||
|
self.last_fname = fname
|
||||||
|
self.last_labels = labels
|
||||||
|
return labels
|
||||||
|
else:
|
||||||
|
return pd.DataFrame()
|
||||||
|
|
||||||
def write_labels(self, df, data_adaptor):
|
def write_labels(self, df, data_adaptor):
|
||||||
# update our internal state and save it. Multi-threading often enabled,
|
# update our internal state and save it. Multi-threading often enabled,
|
||||||
@@ -142,6 +156,10 @@ class AnnotationsLocalFile(Annotations):
|
|||||||
else:
|
else:
|
||||||
open(fname, "w").close()
|
open(fname, "w").close()
|
||||||
|
|
||||||
|
# update the cache
|
||||||
|
self.last_fname = fname
|
||||||
|
self.last_labels = df
|
||||||
|
|
||||||
def _get_userid(self):
|
def _get_userid(self):
|
||||||
if self.CXGUID not in session:
|
if self.CXGUID not in session:
|
||||||
session[self.CXGUID] = uuid4().hex
|
session[self.CXGUID] = uuid4().hex
|
||||||
|
|||||||
Reference in New Issue
Block a user