# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK
# 
# Path: sesql/typemap.py
# class TypeMap(object):
#     def __init__(self):
#     def all_subclasses(klass, done = None):
#     def all_tables(self):
#     def all_classes(self):
#     def all_class_names(self):
#     def get_class_names_for(self, table):
#     def get_classes_for(self, table):
#     def get_table_for(self, klass):
#     def get_class_by_name(self, klass):
# 
# Path: sesql/fieldmap.py
# class FieldMap(object):
#     def __init__(self):
#     def all_fields(self):
#     def get_field(self, field):
#     def get_primary(self):
# 
# Path: sesql/results.py
# class SeSQLResultSet(object):
#     """
#     A lazy SeSQL result set
#     It mimicks a bit the Django QuerySet, but doesn't work the same way,
#     and doesn't provide exactly the same methods
#     """
#     def __init__(self, objs, fields):
#         """
#         Constructor
#         Objs must be a list of (class, id) with optionally extra fields
#         """
#         self.objs = objs
#         self.fields = fields
# 
#     def brains(self):
#         """
#         Get the raw objects from SeSQL index, aka the "brains", as dictionnaries
#         """
#         for obj in self.objs:
#             yield dict(zip(self.fields, obj))
# 
#     def count(self):
#         """
#         Count results
#         """
#         return len(self.objs)
#     __len__ = count
# 
#     def iterator(self):
#         """
#         Iterate on self
#         """
#         for obj in self.objs:
#             try:
#                 yield self.load(obj)
#             except config.orm.not_found:
#                 log.warning("Object %r does not exist ! Broken index ?" % (obj,))
#     __iter__ = iterator
# 
#     def all(self):
#         """
#         Get all the results as a list
#         """
#         return list(self)
# 
#     def get(self, index):
#         """
#         Get the row at given index
#         """
#         return self.load(self.objs[index])
#     __getitem__ = get
# 
#     def __getslice__(self, i, j):
#         """
#         Get a slice
#         """
#         res = [ self.load(obj) for obj in self.objs[i:j] ]
#         return res
# 
#     @staticmethod
#     def load(obj):
#         """
#         Get a given object
#         """
#         objclass, objid = obj[:2]
#         objclass = typemap.get_class_by_name(objclass)
#         if not objclass:
#             return config.orm.not_found
#         entry = "%s:%s" % (objclass.__name__, objid)
#         log.debug("Fetching %s" % entry)
#         return config.orm.load_object(objclass, objid)
# 
#     def historize(self, query):
#         """save in the database the query for future processing"""
#         nb_results = self.count()
#         query_text = query.get_fulltext_query()[2][0]
#         config.orm.historize(query=query_text, nb_results=nb_results)


import logging
from sesql import config
from sesql.typemap import typemap
from sesql.fieldmap import fieldmap
from sesql.results import SeSQLResultSet
            ratio *= config.SMART_QUERY_RATIO
            sl = int(config.SMART_QUERY_INITIAL * ratio)
            cursor = self._attempt_short_query(sl, limit)
            if cursor.rowcount >= limit:
                log.debug("Found data with Query Plan B")
                return cursor
            log.debug("Found %d/%d rows with Query Plan B" % (cursor.rowcount,
                                                              limit))
        log.debug("Using Query Plan C")
        return self._do_longquery(limit)

    def shortquery(self, limit = 50):
        """
        Perform a long query and return a lazy Django result set
        """
        table = self.get_table_name()

        if table == config.MASTER_TABLE_NAME:
            # Multitable or unprecise query ? Falling back to longquery
            log.warning("Query on master table will not be optimized on %s" % self.query)
            return self.longquery(limit)

        if "sesql_relevance"  in self.order or "-sesql_relevance" in self.order:
            # Order on relevance ? Falling back to longquery
            log.info("Query sorting on relevance will not be optimized on %s" % self.query)
            return self.longquery(limit)

        log.debug("Trying short query for %s" % self.query)

        cursor = self._do_smart_query(limit)

# Path: sesql/config.py
# CONFIG_PATH_FALLBACK = 'sesql_config'
#     CONFIG_PATH = getattr(settings, 'SESQL_CONFIG_PATH', CONFIG_PATH_FALLBACK)
#     CONFIG_PATH = CONFIG_PATH_FALLBACK
# 
# Path: sesql/typemap.py
# class TypeMap(object):
#     def __init__(self):
#     def all_subclasses(klass, done = None):
#     def all_tables(self):
#     def all_classes(self):
#     def all_class_names(self):
#     def get_class_names_for(self, table):
#     def get_classes_for(self, table):
#     def get_table_for(self, klass):
#     def get_class_by_name(self, klass):
# 
# Path: sesql/fieldmap.py
# class FieldMap(object):
#     def __init__(self):
#     def all_fields(self):
#     def get_field(self, field):
#     def get_primary(self):
# 
# Path: sesql/results.py
# class SeSQLResultSet(object):
#     """
#     A lazy SeSQL result set
#     It mimicks a bit the Django QuerySet, but doesn't work the same way,
#     and doesn't provide exactly the same methods
#     """
#     def __init__(self, objs, fields):
#         """
#         Constructor
#         Objs must be a list of (class, id) with optionally extra fields
#         """
#         self.objs = objs
#         self.fields = fields
# 
#     def brains(self):
#         """
#         Get the raw objects from SeSQL index, aka the "brains", as dictionnaries
#         """
#         for obj in self.objs:
#             yield dict(zip(self.fields, obj))
# 
#     def count(self):
#         """
#         Count results
#         """
#         return len(self.objs)
#     __len__ = count
# 
#     def iterator(self):
#         """
#         Iterate on self
#         """
#         for obj in self.objs:
#             try:
#                 yield self.load(obj)
#             except config.orm.not_found:
#                 log.warning("Object %r does not exist ! Broken index ?" % (obj,))
#     __iter__ = iterator
# 
#     def all(self):
#         """
#         Get all the results as a list
#         """
#         return list(self)
# 
#     def get(self, index):
#         """
#         Get the row at given index
#         """
#         return self.load(self.objs[index])
#     __getitem__ = get
# 
#     def __getslice__(self, i, j):
#         """
#         Get a slice
#         """
#         res = [ self.load(obj) for obj in self.objs[i:j] ]
#         return res
# 
#     @staticmethod
#     def load(obj):
#         """
#         Get a given object
#         """
#         objclass, objid = obj[:2]
#         objclass = typemap.get_class_by_name(objclass)
#         if not objclass:
#             return config.orm.not_found
#         entry = "%s:%s" % (objclass.__name__, objid)
#         log.debug("Fetching %s" % entry)
#         return config.orm.load_object(objclass, objid)
# 
#     def historize(self, query):
#         """save in the database the query for future processing"""
#         nb_results = self.count()
#         query_text = query.get_fulltext_query()[2][0]
#         config.orm.historize(query=query_text, nb_results=nb_results)


# Path: sesql/query.py
import logging
from sesql import config
from sesql.typemap import typemap
from sesql.fieldmap import fieldmap
from sesql.results import SeSQLResultSet

            ratio *= config.SMART_QUERY_RATIO
            sl = int(config.SMART_QUERY_INITIAL * ratio)
            cursor = self._attempt_short_query(sl, limit)
            if cursor.rowcount >= limit:
                log.debug("Found data with Query Plan B")
                return cursor
            log.debug("Found %d/%d rows with Query Plan B" % (cursor.rowcount,
                                                              limit))
        log.debug("Using Query Plan C")
        return self._do_longquery(limit)

    def shortquery(self, limit = 50):
        """
        Perform a long query and return a lazy Django result set
        """
        table = self.get_table_name()

        if table == config.MASTER_TABLE_NAME:
            # Multitable or unprecise query ? Falling back to longquery
            log.warning("Query on master table will not be optimized on %s" % self.query)
            return self.longquery(limit)

        if "sesql_relevance"  in self.order or "-sesql_relevance" in self.order:
            # Order on relevance ? Falling back to longquery
            log.info("Query sorting on relevance will not be optimized on %s" % self.query)
            return self.longquery(limit)

        log.debug("Trying short query for %s" % self.query)

        cursor = self._do_smart_query(limit)

        return SeSQLResultSet(list(cursor), self.fields)