@bergus
2017-02-22T15:07:02.000000Z
字数 2569
阅读 1608
python
leveldb
import leveldb
db = leveldb.LevelDB('./db')
db.Put('hello', 'world')
print db.Get('hello')
db.Delete('hello')
print db.Get('hello')
batch = leveldb.WriteBatch()
batch.Put('hello', 'world')
batch.Put('hello again', 'world')
batch.Delete('hello')
db.Write(batch, sync = True)
class LevelDB(builtin.object) | LevelDB(filename, **kwargs) -> leveldb object |
| Open a LevelDB database, from the given directory. |
| Only the parameter filename is mandatory. |
| filename the database directory | create_if_missing (default: True) if True, creates a new database if none exists | error_if_exists (default: False) if True, raises and error if the database already exists | paranoid_checks (default: False) if True, raises an error as soon as an internal corruption is detected | block_cache_size (default: 8 * (2 << 20)) maximum allowed size for the block cache in bytes | write_buffer_size (default 2 * (2 << 20))
| block_size (default: 4096) unit of transfer for the block cache in bytes | max_open_files: (default: 1000) | block_restart_interval
|
| Snappy compression is used, if available. |
| Some methods support the following parameters, having these semantics: |
| verify_checksum: iff True, the operation will check for checksum mismatches | fill_cache: iff True, the operation will fill the cache with the data read | sync: iff True, the operation will be guaranteed to sync the operation to disk |
| Methods supported are: |
| Get(key, verify_checksums = False, fill_cache = True): get value, raises KeyError if key not found |
| key: the query key |
| Put(key, value, sync = False): put key/value pair |
| key: the key | value: the value |
| Delete(key, sync = False): delete key/value pair, raises no error kf key not found |
| key: the key |
| Write(write_batch, sync = False): apply multiple put/delete operations atomatically |
| write_batch: the WriteBatch object holding the operations |
| RangeIter(key_from = None, key_to = None, include_value = True, verify_checksums = False, fill_cache = True): return iterator |
| key_from: if not None: defines lower bound (inclusive) for iterator | key_to: if not None: defined upper bound (inclusive) for iterator | include_value: if True, iterator returns key/value 2-tuples, otherwise, just keys |
| GetStats(): get a string of runtime information
class WriteBatch(builtin.object) | WriteBatch() -> write batch object |
| Create an object, which can hold a list of database operations, which | can be applied atomically. |
| Methods supported are: |
| Put(key, value): add put operation to batch |
| key: the key | value: the value |
| Delete(key): add delete operation to batch |
| key: the key |
'CompactRange',
'CreateSnapshot',
'Delete',
'Get',
'GetStats',
'Put',
'RangeIter',//通过Key进行切片处理,因为所有的存储都是有序的
'Write',
'__class__',
'__delattr__',
'__doc__',
'__format__',
'__getattribute__',
'__hash__',
'__init__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__']