Ejemplo n.º 1
0
def get_connection(path, indexer=False, callback=None):
    """Get a connection to the database.

    This function reuses already existing connections.
    """
    global _index_connection, _search_connections

    try:
        _connection_attemts = _new = 0
        connection = None
        while _connection_attemts <= 3:
            try:
                if indexer:
                    if _index_connection is None:
                        _new = True
                        _index_connection = IndexerConnection(path)
                    connection = _index_connection
                else:
                    thread = get_current_thread()
                    if thread not in _search_connections:
                        _new = True
                        _search_connections[
                            thread] = connection = SearchConnection(path)
                    else:
                        connection = _search_connections[thread]
            except (xapian.DatabaseOpeningError, xapian.DatabaseLockError):
                time.sleep(0.5)
                _connection_attemts += 1
            else:
                break

        if callback:
            callback(connection)

        if not _new:
            connection.reopen()
        yield connection
    finally:
        if connection is not None:
            connection.close()
            _index_connection = None
Ejemplo n.º 2
0
    def __init__(self, dirname):
        self.dbPath = os.path.abspath(dirname)

        self.dbconn = IndexerConnection(self.dbPath)

        self.dbconn.add_field_action('title',
                                     FieldActions.INDEX_FREETEXT,
                                     weight=5,
                                     language='en')
        self.dbconn.add_field_action('text',
                                     FieldActions.INDEX_FREETEXT,
                                     language='en',
                                     spell=True,
                                     stop=STOPWORDS)
        #self.dbconn.add_field_action('citecnt', FieldActions.FACET, type='float')
        #self.dbconn.add_field_action('citecnt', FieldActions.WEIGHT)

        self.lock = threading.Lock()

        for k in FIELD_NUM.keys():
            self.dbconn.add_field_action(k, FieldActions.STORE_CONTENT)