def delete(self): from dockit.backends import get_index_router pre_delete.send(sender=type(self), instance=self) backend = self._meta.get_document_backend_for_write() backend.delete(type(self), self._meta.collection, self.get_id()) get_index_router().on_delete(type(self), self._meta.collection, self.get_id()) post_delete.send(sender=type(self), instance=self)
def save(self): from dockit.backends import get_index_router created = not self.pk pre_save.send(sender=type(self), instance=self) backend = self._meta.get_document_backend_for_write() data = type(self).to_primitive(self) backend.save(type(self), self._meta.collection, data) get_index_router().on_save(type(self), self._meta.collection, self.get_id(), data) post_save.send(sender=type(self), instance=self, created=created)
def reindex(self, name, collection, query_hash): obj, created = self.get_or_create(name=name, collection=collection, defaults={'query_hash':query_hash}) from dockit.backends import get_index_router query_index = get_index_router().registered_querysets[collection][query_hash] documents = obj.get_document().objects.all() for doc in documents: self.evaluate_query_index(obj, query_index, doc.pk, doc.to_primitive(doc))
def delete(self): #CONSIDER we are taking from an index a list of doc ids from dockit.backends import get_index_router #TODO index_router should detect if there are any userspace indexes, if not skip notifying indexes #TODO if there are userspace indexes, they should be notified in a task index_router = get_index_router() for doc in self.values('pk'): index_router.on_delete(self.document, self.collection, doc['pk']) return self.queryset.delete()
def on_save(self, collection, doc_id, data): from dockit.backends import get_index_router index_router = get_index_router() if collection not in index_router.registered_querysets: return #no querysets have been registered registered_queries = self.filter(collection=collection) for query in registered_queries: if query.query_hash not in index_router.registered_querysets[collection]: continue #TODO stale index, perhaps we should remove query_index = index_router.registered_querysets[collection][query.query_hash] self.evaluate_query_index(query, query_index, doc_id, data)
def get_query(self, query_index): #lookup the appropriate query index self._register_pending_indexes() match = get_index_router().get_effective_queryset(query_index) query_index = match['queryset'] document = query_index.document queryset = RegisteredIndexDocument.objects.filter(index__collection=document._meta.collection, index__query_hash=query_index._index_hash()) for op in match['inclusions']: indexer = self._get_indexer_for_operation(document, op) queryset = queryset.filter(indexer.filter()) for op in match['exclusions']: indexer = self._get_indexer_for_operation(document, op) queryset = queryset.exclude(indexer.filter()) return IndexedDocumentQuery(query_index, queryset)
def _register_pending_indexes(self): if not self.pending_indexes: return if not db_table_exists(RegisteredIndex._meta.db_table): return router = get_index_router() while self.pending_indexes: queryset = self.pending_indexes.pop() document = queryset.document collection = queryset.document._meta.collection key = queryset.global_hash() # only register if the queryset is still active if collection in router.registered_querysets and key in router.registered_querysets[collection]: self.index_tasks.register_index(queryset)
def get_index_backend_for_read(self, queryset): return get_index_router().get_index_for_read(self._document, queryset)
def register_indexes_with_backend(self, indexes): from dockit.backends import get_index_router router = get_index_router() for index in indexes: router.register_queryset(index)