agent-backend/contracts/RocksDBSample/RocksDBSample.yjs
2021-09-26 12:49:24 +08:00

38 lines
958 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

@Permission("RocksDB")
@Description("这是个使用RocksDB的参考代码")
contract RocksDBSample{
function onCreate(){
Global.rocksdb = RocksDBUtil.loadDB("./dbdir/","false");
}
@Description("示例参数: {\"key\":\"abc\",\"value\":\"def\"}")
export function put(arg){
arg = JSON.parse(arg);
Global.rocksdb.put(arg.key,arg.value);
return "success";
}
@Description("示例参数: \"abc\"}")
export function get(arg){
return Global.rocksdb.get(arg);
return "failed";
}
@Description("示例参数: \"abc\"")
export function deleteKey(arg){
return Global.rocksdb.delete(arg);
}
@Description("遍历KV库无需参数")
export function iter(arg){
var iter = Global.rocksdb.newIterator();
var obj = undefined;
var ret = {
};
for (iter.seekToFirst();(obj=Global.rocksdb.getNext(iter))!=undefined;){
ret[obj.key]=obj.value;
}
return JSON.stringify(ret)
}
}