def open(self, flags): fh = self.mongofs.open_file_cache.get(self.id, None) if fh is None: doc = self.mongo[self.database][self.collection].find_one(self.filter) if doc is None: return -errno.ENOENT id = doc["_id"] if self.mongofs.hide_id: del doc["_id"] if len(doc) == 0: json = "" else: json = ( dumps( doc, indent=self.mongofs.json_indent if self.mongofs.json_indent >= 0 else None, ensure_ascii=self.mongofs.json_escaping, ).encode(self.mongofs.json_encoding, errors="replace") + "\n" ) fh = MongoSharedFileHandle(BytesIO(json), id) self.mongofs.open_file_cache[self.id] = fh fh.refs += 1 return fh
def list_files_impl(self): if self.current_field is None: # TODO: Needs optimisation attrs = set() for doc in self.mongo[self.database][self.collection].find(self.filter, limit=50): for key, value in doc.iteritems(): if key not in self.filter and not isinstance(value, dict) and not isinstance(value, list): attrs.add(key) attrs -= set(self.filter.keys()) return list(attrs) else: # Count the distinct values of the field q = dict(self.filter) q[self.current_field] = {"$exists": True} values = self.mongo[self.database][self.collection].inline_map_reduce( map=Code("""function() { emit(this[fieldName], 1); }"""), reduce=Code("""function(key, values) { return Array.sum(values); }"""), query=q, scope={"fieldName": self.current_field}, ) return [ dumps(entry["_id"], ensure_ascii=False) + (".json" if entry["value"] == 1 else "") for entry in values ]