整数键/值可以存储在LevelDB中吗? [英] Can integer keys / values be stored in LevelDB?

查看:482
本文介绍了整数键/值可以存储在LevelDB中吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我搜索了支持整数键和整数值的键值存储。 LevelDB似乎是一个好的选择,虽然我找不到任何关于是否支持整数值/键的信息。

I have searched for key value stores that support integer keys and integer values. LevelDB seems a good option, though I can't find any information on whether integer values/keys are supported

推荐答案

几乎任何在LevelDB。您通过 Slice 结构。下面是一个例子:

You can store pretty much anything in LevelDB. You provide opaque slices of data into LevelDB via the Slice structure. Here is an example:

int intKey = 256;
int intValue = 256*256;

Slice key((char*)&intKey, sizeof(int));
Slice value((char*)&intValue, sizeof(int));

db->Put(leveldb::WriteOptions(), key, value);

这真是太棒了!

然而,有一点需要注意的是,虽然在LevelDB中存储整数通常是很好的(作为键和值) order by BytewiseComparator ,因此您的密钥必须支持字节比较。这也意味着如果你依赖于键的特定顺序,那么你必须注意系统上的字节顺序。

However, one thing to note is that while it's generally fine to store integers in LevelDB (as both keys and values), they will be order via the BytewiseComparator so your key has to support bytewise comparison. This also means that if you rely on specific ordering of the keys, then you have to be mindful of the endian-ness on the system.

您还可以通过 Comparator 这将允许您替换默认的 BytewiseComparator

这篇关于整数键/值可以存储在LevelDB中吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆