Values that can be stored in LMDB

笑着哭i 提交于 2019-12-04 21:50:35

It sounds like you're not a C programmer, but in the context of the C language, the data for keys and values in LMDB are both (void *) - that is, generic pointers to anything. That means any data type that can be expressed in the programming language can be used. In other languages, through whatever bindings are provided, your options may be more limited.

LMDB doesn't care what you store, all it sees are blobs. Obviously it is not an RDBMS. If you want to store structured data, you need to manage that structure yourself. You could take a complex data structure and serialize it into a blob, and store it under a single key. This is the approach used in OpenLDAP's slapd. Or, you could setup individual columns of your data as separate named DBs in LMDB and store individual values in their respective DBs. (For example, indices in OpenLDAP and SQLite/SQLightning are handled this way.) So yes, while LMDB does not provide any functions for managing relations itself, you can certainly use it as the backing store of an RDBMS if you want to. (Again, see SQLightning for example. Backends for MySQL/MariaDB or Postgres are doable as well, but involve a lot more glue code between their frontends and the LMDB API.)

Both keys and data can contain any combination of zero or more bytes.

So your data blobs could be comma-separated text strings, JSON or some form of packet binary data (like flatbuffers).

It also means you must invest own work if you want to add multiple keys to locate the same data or if you want to store multiple types of data similar to multiple tables in a RMDB.

One simple way to store data could be like:

born;1980-01-01;2 -> <null>
born;1983-05-17;1 -> <null>
born;1983-05-17;3 -> <null>
born;1992-09-11;4 -> <null>
db;nextuser -> 5
names;benny;2 -> <null>
names;jenny;3 -> <null>
names;john;1 -> <null>
names;sue;4 -> <null>
occupation;student;3 -> <null>
occupation;student;4 -> <null>
occupation;teacher;1 -> <null>
occupation;teacher;2 -> <null>
students;1;3 -> <null>
students;2;3 -> <null>
students;2;4 -> <null>
teachers;3;1 -> <null>
teachers;4;1 -> <null>
teachers;4;2 -> <null>
users;0001 -> {"name":"john","born":"1983-05-17","occupation":"teacher"}
users;0002 -> {"name":"benny","born":"1980-01-01","occupation":"teacher"}
users;0003 -> {"name":"jenny","born":"1983-05-17","occupation":"student"}
users;0004 -> {"name":"sue","born":"1992-09-11","occupation":"student"}

The above data would allow the users to be iterated on insertion order (id) by using the users;<id> keys. Or by age using the born;<date>;<id> keys.

Iterating through students;<teacher-id>;<student-id> with teacher-id = 2 would give the id of all students mapped to user "benny".

Iterating through teachers;<student-id>;<teacher-id> with student-id = 4 would give the id of all teachers for user "sue".

Instead of using key prefixes for different type of data, it's also possible to create multiple key/value databases in LMDB.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!