# Path: dockit/schema/common.py
# class UnSet(object):
# class DotPathTraverser(object):
# class DotPathList(list):
# class DotPathDict(dict):
# class DotPathSet(set):
# class GenericDotPathObject(object):
#     def __nonzero__(self):
#     def __init__(self, dotpath):
#     def resolve_for_schema(self, schema):
#     def resolve_for_instance(self, instance):
#     def resolve_for_raw_data(self, data, schema=None):
#     def _resolve_loop(self):
#     def resolve_next(self):
#     def next_part(self):
#     def current(self):
#     def current_value(self):
#     def current_field(self):
#     def end(self, field=None, value=None):
#     def next(self, field=None, value=None):
#     def set_value(self, value):
#     def traverse_dot_path(self, traverser):
#     def set_value(self, attr, value):
#     def traverse_dot_path(self, traverser):
#     def set_value(self, attr, value):
#     def traverse_dot_path(self, traverser):
#     def set_value(self, attr, value):
#     def __init__(self, value):
#     def traverse_dot_path(self, traverser):


from django.db import models
from django.utils import simplejson
from django.core.serializers.json import DjangoJSONEncoder
from django.core.exceptions import ObjectDoesNotExist
from dockit.schema.common import DotPathTraverser, DotPathNotFound
        from dockit.backends import get_index_router
        from dockit.backends import get_index_router
        from dockit.backends.djangodocument.models import RegisteredIndexDocument
        from dockit.backends.djangodocument.models import RegisteredIndexDocument
        from dockit.schema import Document
        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 on_delete(self, collection, doc_id):
        RegisteredIndexDocument.objects.filter(index__collection=collection, doc_id=doc_id).delete()
    
    def evaluate_query_index(self, registered_index, query_index, doc_id, data):
        
        schema = query_index.document
        
        #evaluate if document passes filters
        for inclusion in query_index.inclusions:
            dotpath = inclusion.dotpath()
            traverser = DotPathTraverser(dotpath)
            try:
                traverser.resolve_for_raw_data(data, schema=schema)
            except DotPathNotFound:
                return False
            except ObjectDoesNotExist:
                return False
            if traverser.current_value != inclusion.value:
                return False
        for exclusion in query_index.exclusions:
            dotpath = exclusion.dotpath()
            traverser = DotPathTraverser(dotpath)
            try:
                traverser.resolve_for_raw_data(data, schema=schema)

# Path: dockit/schema/common.py
# class UnSet(object):
# class DotPathTraverser(object):
# class DotPathList(list):
# class DotPathDict(dict):
# class DotPathSet(set):
# class GenericDotPathObject(object):
#     def __nonzero__(self):
#     def __init__(self, dotpath):
#     def resolve_for_schema(self, schema):
#     def resolve_for_instance(self, instance):
#     def resolve_for_raw_data(self, data, schema=None):
#     def _resolve_loop(self):
#     def resolve_next(self):
#     def next_part(self):
#     def current(self):
#     def current_value(self):
#     def current_field(self):
#     def end(self, field=None, value=None):
#     def next(self, field=None, value=None):
#     def set_value(self, value):
#     def traverse_dot_path(self, traverser):
#     def set_value(self, attr, value):
#     def traverse_dot_path(self, traverser):
#     def set_value(self, attr, value):
#     def traverse_dot_path(self, traverser):
#     def set_value(self, attr, value):
#     def __init__(self, value):
#     def traverse_dot_path(self, traverser):


# Path: dockit/backends/djangodocument/managers.py
from django.db import models
from django.utils import simplejson
from django.core.serializers.json import DjangoJSONEncoder
from django.core.exceptions import ObjectDoesNotExist
from dockit.schema.common import DotPathTraverser, DotPathNotFound
        from dockit.backends import get_index_router
        from dockit.backends import get_index_router
        from dockit.backends.djangodocument.models import RegisteredIndexDocument
        from dockit.backends.djangodocument.models import RegisteredIndexDocument
        from dockit.schema import Document

        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 on_delete(self, collection, doc_id):
        RegisteredIndexDocument.objects.filter(index__collection=collection, doc_id=doc_id).delete()
    
    def evaluate_query_index(self, registered_index, query_index, doc_id, data):
        
        schema = query_index.document
        
        #evaluate if document passes filters
        for inclusion in query_index.inclusions:
            dotpath = inclusion.dotpath()
            traverser = DotPathTraverser(dotpath)
            try:
                traverser.resolve_for_raw_data(data, schema=schema)
            except DotPathNotFound:
                return False
            except ObjectDoesNotExist:
                return False
            if traverser.current_value != inclusion.value:
                return False
        for exclusion in query_index.exclusions:
            dotpath = exclusion.dotpath()
            traverser = DotPathTraverser(dotpath)
            try:
                traverser.resolve_for_raw_data(data, schema=schema)

            except DotPathNotFound: