如何获取 BerkeleyDB 中值的大小?
有没有办法只获取 BDB 中存储的值的长度(以字节为单位)?我不需要整个数据数组,只需要它的大小。
Is there a way to get only the length (in bytes) of a value stored in BDB? I don't need the entire data array, only its size.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不想检索整个条目并且不使用 DPL,我想说您应该在存储的字节数组的大小上添加辅助索引,并确保您的 DAO 在任何保存或更新。您可以添加一个
KeyCreator
,它根据记录在辅助数据库中创建辅助size
键。您想要执行什么类型的查询?您想搜索给定大小的所有记录吗?或者您想在检索某个记录之前知道它的大小?我认为后一个问题更难回答。
If you don't want to have to retrieve the entire entry and aren't using DPL, I'd say you should add a secondary index on the size of the stored byte array and make sure that your DAO properly updates this value on any save or updates. You could add a
KeyCreator
which creates a secondarysize
key in a secondary database based on the record.What type of query are you trying to perform? Are you wanted to search for all records of a given size? Or are you wanting to know the size of a certain record before you retrieve it? I think the latter question is harder to answer.
我假设您使用的是 JE 版本(或 BDB 的 Java 绑定),在这种情况下,一旦您获得 DatabaseEntry 所需的键, getSize() 应该给你你想要的。
如果您使用 C 绑定,请检查 DBT 句柄的大小字段。
I'm assuming you're using the JE version (or the Java binding of BDB) in which case, once you get the DatabaseEntry of the desired key, getSize() should give you what you want.
If you're using the C binding, check the DBT handle's size field.
如果将文档 ID 存储为重复数据项,而不是存储为一个 Blob 数据项值,则可以使用 DBC->count() 来检测匹配文档的数量,而无需实际检索一长串 ID。否则,Berkeley DB API 似乎不支持您所要求的内容(即使您认为他们添加它可能会很有效)。我也对此感到困惑,这就是我为自己的项目想出的解决方案。
If you store your document ids as duplicate data items, instead of as one blob data item value, then you can use DBC->count() to detect the number of matching documents without actually retrieving the long list of ids. Otherwise, the Berkeley DB API does not seem to support what you're asking for (even though you'd think it could be efficient for them to add it). I puzzled over this as well, and that was the solution I came up with for my own project.
对于您的问题,使用 DB_DBT_PARTIAL 标志并询问记录的开头将为您提供第一个 ID,并且 DBT.size 可用于计算 ID 的总数。
for your problem, using the DB_DBT_PARTIAL flag ang asking for the begining of the record will provide you your first IDs and the DBT.size can be used to compute the total number of IDs.