光照内存映射数据库(lmdb)的异步包装器
aiolmdb的Python项目详细描述
aiolmdb
一个围绕lmdb的异步包装器。
aiolmdb是alpha质量的软件,期望api或体系结构发生变化 随着时间的推移。
用法
打开aiolmdb环境
importaiolmdb# Open a aiolmdb enviroment## Takes the same arguments that lmdb.open does.enviroment=aiolmdb.open("/tmp/path/to/enviorment",...)
打开aiolmdb数据库
与pylmdb不同,aiolmdb在open_db
上不返回数据库句柄,但是
而是一个完整的python对象。
# Open a databaserecords=enviroment.open_db("records")# Get the default ("" named database) within an enviromentdefault=enviroment.get_default_database()
查询aiolmdb数据库
对数据库的所有查询都返回协同路由并异步运行。
# Get a value(s) from the databaseresult=awaitdb.get(b'key')# Normal fetch, returned b'value'result=await.db.get(b'key',default=b'')# Defaults to b'' if no key is foundresult=awaitdb.get_multi([b'0',b'1'])# Gets multiple keys at once# Write a value into the databaseawaitdb.put(b'key',b'value')awaitdb.put_multi([(b'k1',b'v1'),(b'k2',b'v2')])# Puts multiple key-valuesatonce,atomically.# Delete a key from the databaseawaitdb.delete(b'key')awaitdb.delete_multi([b'k1',b'k2',b'k3'])# Drop the databaseawaitdb.drop()# Run any arbitrary transactionsdeftransaction_action(txn):returntxn.id()awaitdb.run(transaction_action)
使用编码器
应用程序不直接在bytearray上操作,需要转换
与序列化的bytearray之间的运行时对象。避免额外开支
在主循环运行此转换代码时,aiolmdb支持添加
运行此序列化/反序列化逻辑的数据库级编码器
执行器,而不是在主循环中。默认情况下,每个aiolmdb数据库都使用
支持直接写入类似字节的对象的IdentityCoder
。其他
编码器可以用于键和值来更改对象的类型
被API接受。
# Opening a database with specific codersdb=env.open_db("records",key_coder=UInt16Coder(),value_coder=JSONCoder())awaitdb.put(65535,{"key":"value"})# Takes the approriate maching keysawaitdb.get(65535)# Returns {"key": "value"}# Alter the coder for an existing database, useful for altering the enviroment# default database.db.key_coder=StringCoder()db.value_coder=JSONCoder()# Supported CodersIdentityCoder()# Raw bytes coderStringCoder()# String coderUInt16Coder()# 16-bit unsigned integer coderUInt32Coder()# 32-bit unsigned integer coderUInt64Coder()# 64-bit unsigned integer coderJSONCoder()# JSON coder, works with any JSON serializable objectPicleCoder()# Pickle coder, works with any picklable object compression# Create a new JSONCoder, gzipped with compression level 9# Runs the encoded JSON through zlib before writing to database, anddecompresseszlib_json_coder=JSONCoder().compressed(level=9)compressed_db=env.open_db("records",value_coder=zlib_json_coder)# Write your own custom coderfromaiolmdb.codersimportCoderclassCustomCoder(Coder):defserialize(self,obj):# Custom serialization logic## These objects need to have locally immutable state: the objects must not# change how it represents its state for the duration of all concurrent# transactions dealing with the object.## must return a bytes-like objectreturnbufferdefdeserialize(self,buffer):# Custom deserialization logic## aiolmdb uses LMDB transactions with `buffers=True`. this returns a# direct reference to the memory region. This buffer must NOT be modified in# any way. The lifetime of the buffer is also only valid during the scope of# the transaction that fetched it. To use the buffer outside of the context# of the serializer, it must be copied, and references to the buffer must# not be used elsewhere.## Returns the deserialized objectreturndeserialized_object
注意事项和问题
- 写入事务(放置、删除、弹出、替换)在中执行时仍然阻止 遗嘱执行人。因此,同时运行多个写事务将 逐一阻止所有其他事务,直到它们完成。长跑 强烈禁止写事务。
- 由于设计限制,跨多个数据库的原子事务是 目前做起来并不容易,代码也不是很Python。
待办事项
- 支持游标和范围查询