• GET IT
  • Hello World
  • K-V
  • With Spring
  • List
  • ZSet
  • Other

    GET IT

    Maven
    1. <dependency> <groupId>top.thinkin.kitdb</groupId> <artifactId>store</artifactId> <version>VERSION</version></dependency>

    Hello World

    K-V

    以下是一个简单的示例。创建一个KitDB实例,简单操作K-V,然后关闭这个KitDB实例
    1. DB db = DB.build("/data/kitdb/");RKv kv = db.getrKv();kv.set("hello","word".getBytes());byte[] bytes = kv.get("hello");kv.del("hello");db.close();

    一般情况下,应该长期保留这个KitDB实例,使用它进行多次操作

    With Spring

    创建Bean

    1. @Beanpublic DB db() throws KitDBException { return DB.buildTransactionDB("/data/dbx",true);}

    操作K-V

    1. @Autowiredprivate DB db;public String doSome(String key) throws KitDBException{ RKv kv = db.getrKv(); byte[] bytes = kv.get(key); if ( bytes == null ){ return null; } returnnew String(bytes, Charset.defaultCharset());}

    List

    操作名为 hello 的List

    1. //插入元素for (int i = 0; i < 10 * 10000; i++) { rList.add("hello",("world"+i).getBytes());}//获取index为100的元素byte[] bytes_100 = rList.get("hello",100);//遍历获取所有的元素try (RIterator<RList> iterator = rList.iterator("hello")){ while (iterator.hasNext()) { RList.Entry er = iterator.next(); long index = er.getIndex(); byte[] valueBytes = er.getValue(); }}// 删除这个ListrList.delete("hello");

    ZSet

    操作名为 hello 的ZSet

    1. ZSet zSet = db.getzSet();int i = 10*10000;List<ZSet.Entry> entryList = new ArrayList<>(i);for (int j = 0; j < i; j++) { entryList.add(new ZSet.Entry(j,("world"+j).getBytes()));}//批量插入元素zSet.add("hello",entryList);//范围查询元素List<ZSet.Entry> rangList = zSet.range("hello",1000L,1500L,Integer.MAX_VALUE);//范围查询并删除查询到的元素List<ZSet.Entry> rangeDelList = zSet.rangeDel("hello",1000L,1500L,Integer.MAX_VALUE);//获取Sizeint size = zSet.size("hello");//设置过期时间zSet.ttl("hello",10);Thread.sleep(10*1000);//判断是否存在boolean exist = zSet.isExist("hello");

    Other

    无法对同一个目录打开多个KitDB实例

    1. DB db = DB.build("/data/kitdb/");DB db2 = DB.build("/data/kitdb/");// 抛出错误

    但可以打开多个只读实例,只读实例只能读取,不能写入

    1. DB db = DB.build("/data/kitdb/");DB db3 = DB.readOnly("/data/kitdb/");// 正常执行DB db4 = DB.readOnly("/data/kitdb/");// 正常执行DB db5 = DB.readOnly("/data/kitdb/");// 正常执行RKv rKv = db4.getrKv();byte[] value = rKv.get("hello");// 正常执行rKv.set("hello","world".getBytes());// 抛出错误