def get_key(self): ''' Get the current key at the iterator :returns: The current key at the iterator ''' size = new_native_size() pointer = LDB.leveldb_iter_key(self.iterator, get_reference_to(size)) return as_python_string(pointer, size)
def get(self, key, verify_checksum=False, fill_cache=True): ''' Delete the supplied value at the given key. :param key: The key to delete the value at :throws DatabaseException: If an error has occurred ''' result = None options = self.get_read_options(verify_checksum, fill_cache) try: error = new_native_string() size = new_native_size() value = LDB.leveldb_get(self.database, options, key, len(key), get_reference_to(size), get_reference_to(error)) self.handle_error(error) return as_python_string(value, size) finally: if LDB and options: LDB.leveldb_readoptions_destroy(options)
def assert_no_error(self): ''' Check if there was an error after the last error. If there was, then throw an exception. :throws DatabaseException: If there was a problem with the iterator ''' error = new_native_string() LDB.leveldb_iter_get_error(error, get_reference_to(error)) message = as_python_string(error) if message: raise DatabaseException(message)
def destroy(self): ''' Completely blows away the underlying database. This process is not reversable. :throws DatabaseException: If an error has occurred ''' options = self.get_open_options() try: error = new_native_string() LDB.leveldb_destroy_db(options, self.filename, get_reference_to(error)) self.handle_error(error) finally: if LDB and options: LDB.leveldb_options_destroy(options) self.close()
def repair(self): ''' Attempt to repair a database after setting paranoid checks still results in an error. :throws DatabaseException: If an error has occurred ''' options = self.get_open_options() try: error = new_native_string() LDB.leveldb_repair_db(options, self.filename, get_reference_to(error)) self.handle_error(error) finally: if LDB and options: LDB.leveldb_options_destroy(options) self.close()
def delete(self, key, synchronous=False): ''' Delete the supplied value at the given key. :param key: The key to delete the value at :param synchronous: True to make the put to disk synchronous, False to buffer :throws DatabaseException: If an error has occurred ''' options = LDB.leveldb_writeoptions_create() try: error = new_native_string() LDB.leveldb_writeoptions_set_sync(options, synchronous) LDB.leveldb_delete(self.database, options, key, len(key), get_reference_to(error)) self.handle_error(error) finally: if options: LDB.leveldb_writeoptions_destroy(options)
def write(self, write_batch, synchronous=False): ''' Apply the write batch to the datbase. :param write_batch: The write batch to apply to the database. :param synchronous: True to make the put to disk synchronous, False to buffer :throws DatabaseException: If an error has occurred ''' options = LDB.leveldb_writeoptions_create() try: error = new_native_string() LDB.leveldb_writeoptions_set_sync(options, synchronous) LDB.leveldb_write(self.database, options, write_batch, get_reference_to(error)) self.handle_error(error) finally: if options: LDB.leveldb_writeoptions_destroy(options)
def open(self): ''' Creates the neccessary data structures and opens the underlying database. To tune the underlying options for the database read the following: http://docs.basho.com/riak/1.0.0/tutorials/choosing-a-backend/LevelDB/ :throws DatabaseException: If an error has occurred ''' options = self.get_open_options() try: error = new_native_string() self.database = LDB.leveldb_open(options, self.filename, get_reference_to(error)) self.handle_error(error) finally: LDB.leveldb_options_destroy(options)