2022/6/1

Read mender-store with python code

讀取 mender-store 的內容...
(lmdb) charles-chang@e431focal:~$ python
Python 3.10.4 (main, Mar 31 2022, 08:41:55) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import lmdb
>>> env = lmdb.open('./mender-store',subdir=False)
>>> txn = env.begin()
>>> myList = [ key for key, _ in txn.cursor()]
>>> print(myList)
[b'artifact-name', b'artifact-provides', b'authtoken']
>>> 
>>>print(txn.cursor().get(b'artifact-name'))
b'1.0.1.30rc1-deltafrom-1.0.0.28patch3'
>>> print(txn.cursor().get(b'artifact-providese'))
None
>>> print(txn.cursor().get(b'artifact-provides'))
b'{"rootfs-image.checksum":"457d85413d8dbdb2c278b4740493efb3712290f36668a919dd4297ae60ca6157","rootfs-image.version":"1.0.1.30rc1"}'

另外,lmdb 的 put/get :
>>> import lmdb
>>> env = lmdb.open('./testlmdb')
>>> txn = env.begin(write=True)
>>> txn.put(key=b'1',value=b'abc')
True
>>> txn.put(key=b'aaa',value=b'{a=1},{b=2}')
True
>>> txn.commit()

>>> txn = env.begin()
>>> for key,value in txn.cursor():
...     print(key,value)
... 
b'1' b'abc'
b'aaa' b'{a=1},{b=2}'


所以寫一個小程式,可以簡單create 這個 key-value 的 lmdb data file: 是用lmdb 的 code。

這樣,只要:
./mydb ./mender-store artifact-provides "{\"rootfs-image.checksum\":\"457d85413d8dbdb2c278b4740493efb3712290f36668a919dd4297ae60ca6157\",\"rootfs-image.version\":\"1.0.1.30rc1\"}"
就可以新增 value
然後 mylist 就是跟 python 一樣的function,列出 db 中所有 key-value record.

沒有留言:

張貼留言