def begin_transaction(self): """PyLucene does not support transactions Thus this function just opens the database for write access. Call "cancel_transaction" or "commit_transaction" to close write access in order to remove the exclusive lock from the database directory. """ jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() self._writer_open()
def __del__(self): """remove lock and close writer after loosing the last reference""" jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() self._writer_close() if hasattr(self, "reader") and self.reader is not None: self.reader.close() self.reader = None if hasattr(self, "searcher") and self.searcher is not None: self.searcher.close() self.searcher = None
def begin_transaction(self): """PyLucene does not support transactions Thus this function just opens the database for write access. Call "cancel_transaction" or "commit_transaction" to close write access in order to remove the exclusive lock from the database directory. """ jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() self._writer_open()
def __del__(self): """remove lock and close writer after loosing the last reference""" jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() self._writer_close() if hasattr(self, "reader") and self.reader is not None: self.reader.close() self.reader = None if hasattr(self, "searcher") and self.searcher is not None: self.searcher.close() self.searcher = None
def __init__(self, basedir, analyzer=None, create_allowed=True): """Initialize or open an indexing database. Any derived class must override __init__. :raise ValueError: The given location exists, but the database type is incompatible (e.g. created by a different indexing engine) :raise OSError: the database failed to initialize :param basedir: The parent directory of the database :type basedir: str :param analyzer: Bitwise combination of possible analyzer flags to be used as the default analyzer for this database. Leave it empty to use the system default analyzer (self.ANALYZER_DEFAULT). See self.ANALYZER_TOKENIZE, self.ANALYZER_PARTIAL, ... :type analyzer: int :param create_allowed: create the database, if necessary; default: True :type create_allowed: bool """ jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() super(PyLuceneDatabase, self).__init__(basedir, analyzer=analyzer, create_allowed=create_allowed) self.pyl_analyzer = PyLucene.StandardAnalyzer() self.writer = None self.reader = None self.index_version = None try: # try to open an existing database tempreader = PyLucene.IndexReader.open(self.location) tempreader.close() except PyLucene.JavaError, err_msg: # Write an error out, in case this is a real problem instead of an absence of an index # TODO: turn the following two lines into debug output #errorstr = str(e).strip() + "\n" + self.errorhandler.traceback_str() #DEBUG_FOO("could not open index, so going to create: " + errorstr) # Create the index, so we can open cached readers on it if not create_allowed: raise OSError("Indexer: skipping database creation") try: # create the parent directory if it does not exist parent_path = os.path.dirname(self.location) if not os.path.isdir(parent_path): # recursively create all directories up to parent_path os.makedirs(parent_path) except IOError, err_msg: raise OSError("Indexer: failed to create the parent " \ + "directory (%s) of the indexing database: %s" \ % (parent_path, err_msg))
def __init__(self, basedir, analyzer=None, create_allowed=True): """Initialize or open an indexing database. Any derived class must override __init__. :raise ValueError: The given location exists, but the database type is incompatible (e.g. created by a different indexing engine) :raise OSError: the database failed to initialize :param basedir: The parent directory of the database :type basedir: str :param analyzer: Bitwise combination of possible analyzer flags to be used as the default analyzer for this database. Leave it empty to use the system default analyzer (self.ANALYZER_DEFAULT). See self.ANALYZER_TOKENIZE, self.ANALYZER_PARTIAL, ... :type analyzer: int :param create_allowed: create the database, if necessary; default: True :type create_allowed: bool """ jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() super(PyLuceneDatabase, self).__init__(basedir, analyzer=analyzer, create_allowed=create_allowed) self.pyl_analyzer = PyLucene.StandardAnalyzer() self.writer = None self.reader = None self.index_version = None try: # try to open an existing database tempreader = PyLucene.IndexReader.open(self.location) tempreader.close() except PyLucene.JavaError, err_msg: # Write an error out, in case this is a real problem instead of an absence of an index # TODO: turn the following two lines into debug output # errorstr = str(e).strip() + "\n" + self.errorhandler.traceback_str() # DEBUG_FOO("could not open index, so going to create: " + errorstr) # Create the index, so we can open cached readers on it if not create_allowed: raise OSError("Indexer: skipping database creation") try: # create the parent directory if it does not exist parent_path = os.path.dirname(self.location) if not os.path.isdir(parent_path): # recursively create all directories up to parent_path os.makedirs(parent_path) except IOError, err_msg: raise OSError( "Indexer: failed to create the parent " + "directory (%s) of the indexing database: %s" % (parent_path, err_msg) )
def make_query(self, *args, **kwargs): jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() return super(PyLuceneDatabase, self).make_query(*args, **kwargs)
def __init__(self, basedir, analyzer=None, create_allowed=True): """Initialize or open an indexing database. Any derived class must override __init__. :raise ValueError: The given location exists, but the database type is incompatible (e.g. created by a different indexing engine) :raise OSError: the database failed to initialize :param basedir: The parent directory of the database :type basedir: str :param analyzer: Bitwise combination of possible analyzer flags to be used as the default analyzer for this database. Leave it empty to use the system default analyzer (self.ANALYZER_DEFAULT). See self.ANALYZER_TOKENIZE, self.ANALYZER_PARTIAL, ... :type analyzer: int :param create_allowed: create the database, if necessary; default: True :type create_allowed: bool """ jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() super(PyLuceneDatabase, self).__init__( basedir, analyzer=analyzer, create_allowed=create_allowed) self.pyl_analyzer = PyLucene.StandardAnalyzer() self.writer = None self.reader = None self.index_version = None try: # try to open an existing database tempreader = PyLucene.IndexReader.open(self.location) tempreader.close() except PyLucene.JavaError as err_msg: # Write an error out, in case this is a real problem instead of an absence of an index # TODO: turn the following two lines into debug output #errorstr = str(e).strip() + "\n" + self.errorhandler.traceback_str() #DEBUG_FOO("could not open index, so going to create: " + errorstr) # Create the index, so we can open cached readers on it if not create_allowed: raise OSError("Indexer: skipping database creation") try: # create the parent directory if it does not exist parent_path = os.path.dirname(self.location) if not os.path.isdir(parent_path): # recursively create all directories up to parent_path os.makedirs(parent_path) except IOError as err_msg: raise OSError("Indexer: failed to create the parent " "directory (%s) of the indexing database: %s" % (parent_path, err_msg)) try: tempwriter = PyLucene.IndexWriter( self.location, self.pyl_analyzer, True) tempwriter.close() except PyLucene.JavaError as err_msg: raise OSError("Indexer: failed to open or create a Lucene" " database (%s): %s" % (self.location, err_msg)) # the indexer is initialized - now we prepare the searcher # windows file locking seems inconsistent, so we try 10 times numtries = 0 #self.dir_lock.acquire(blocking=True) # read "self.reader", "self.indexVersion" and "self.searcher" try: while numtries < 10: try: self.reader = PyLucene.IndexReader.open(self.location) self.indexVersion = self.reader.getCurrentVersion( self.location) self.searcher = PyLucene.IndexSearcher(self.reader) break except PyLucene.JavaError as e: # store error message for possible later re-raise (below) lock_error_msg = e time.sleep(0.01) numtries += 1 else: # locking failed for 10 times raise OSError("Indexer: failed to lock index database" " (%s)" % lock_error_msg) finally: pass # self.dir_lock.release() # initialize the searcher and the reader self._index_refresh()
def __init__(self, basedir, analyzer=None, create_allowed=True): """Initialize or open an indexing database. Any derived class must override __init__. :raise ValueError: The given location exists, but the database type is incompatible (e.g. created by a different indexing engine) :raise OSError: the database failed to initialize :param basedir: The parent directory of the database :type basedir: str :param analyzer: Bitwise combination of possible analyzer flags to be used as the default analyzer for this database. Leave it empty to use the system default analyzer (self.ANALYZER_DEFAULT). See self.ANALYZER_TOKENIZE, self.ANALYZER_PARTIAL, ... :type analyzer: int :param create_allowed: create the database, if necessary; default: True :type create_allowed: bool """ jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() super(PyLuceneDatabase, self).__init__(basedir, analyzer=analyzer, create_allowed=create_allowed) self.pyl_analyzer = PyLucene.StandardAnalyzer() self.writer = None self.reader = None self.index_version = None try: # try to open an existing database tempreader = PyLucene.IndexReader.open(self.location) tempreader.close() except PyLucene.JavaError as err_msg: # Write an error out, in case this is a real problem instead of an absence of an index # TODO: turn the following two lines into debug output #errorstr = str(e).strip() + "\n" + self.errorhandler.traceback_str() #DEBUG_FOO("could not open index, so going to create: " + errorstr) # Create the index, so we can open cached readers on it if not create_allowed: raise OSError("Indexer: skipping database creation") try: # create the parent directory if it does not exist parent_path = os.path.dirname(self.location) if not os.path.isdir(parent_path): # recursively create all directories up to parent_path os.makedirs(parent_path) except IOError as err_msg: raise OSError("Indexer: failed to create the parent " "directory (%s) of the indexing database: %s" % (parent_path, err_msg)) try: tempwriter = PyLucene.IndexWriter(self.location, self.pyl_analyzer, True) tempwriter.close() except PyLucene.JavaError as err_msg: raise OSError("Indexer: failed to open or create a Lucene" " database (%s): %s" % (self.location, err_msg)) # the indexer is initialized - now we prepare the searcher # windows file locking seems inconsistent, so we try 10 times numtries = 0 #self.dir_lock.acquire(blocking=True) # read "self.reader", "self.indexVersion" and "self.searcher" try: while numtries < 10: try: self.reader = PyLucene.IndexReader.open(self.location) self.indexVersion = self.reader.getCurrentVersion( self.location) self.searcher = PyLucene.IndexSearcher(self.reader) break except PyLucene.JavaError as e: # store error message for possible later re-raise (below) lock_error_msg = e time.sleep(0.01) numtries += 1 else: # locking failed for 10 times raise OSError("Indexer: failed to lock index database" " (%s)" % lock_error_msg) finally: pass # self.dir_lock.release() # initialize the searcher and the reader self._index_refresh()
def make_query(self, *args, **kwargs): jvm = PyLucene.getVMEnv() jvm.attachCurrentThread() return super(PyLuceneDatabase, self).make_query(*args, **kwargs)