Kon*_*ong 5 c++ java bit-manipulation leveldb
我通过 JNI 从 Java使用 LevelDB 。
我想提供一个数字(整数)键,并且能够按照该键的顺序迭代数据库。我遇到困难的地方是理解 LevelDb 的默认比较器实际上是如何工作的,以及如何将 int 编码为 a byte[],以使默认比较器按该值正确排序int。
LevelDb 文档指出:
前面的示例使用了 key 的默认排序函数,该函数按字典顺序对字节进行排序。
我已经用谷歌搜索过,但对如何将 an 实际编码int为按字典顺序排列的字节感到困惑?
注意:如果我提供自己的比较器,迭代时间大约会增加一倍,因为现在所有比较都必须在 JNI 边界上来回跳转,所以我不想这样做。
这种编码的工作原理:
public synchronized static byte[] encode(int key) {
encoded[0] = (byte)(key >> 24);
encoded[1] = (byte)(key >> 16);
encoded[2] = (byte)(key >> 8);
encoded[3] = (byte)(key);
return encoded;
}
Run Code Online (Sandbox Code Playgroud)