标签:
翻译自http://www.h2database.com/html/mvstore.html
MVStore是一个持久化的、日志结构是的kv存储。本计划用它作为H2的下一代存储子系统,但你也可以在一个不涉及JDBC或者SQL的应用中直接使用它。
import org.h2.mvstore.*; // open the store (in-memory if fileName is null) MVStore s = MVStore.open(fileName); // create/get the map named "data" MVMap<Integer, String> map = s.openMap("data"); // add and read some data map.put(1, "Hello World"); System.out.println(map.get(1)); // close the store (this will persist changes) s.close();
下面的代码展示了如何使用这些工具
MVStore.Builder提供了一个流畅优美的用可选配置项构造store的接口
示例用法:
MVStore s = new MVStore.Builder(). fileName(fileName). encryptionKey("007".toCharArray()). compress(). open();
可用选项的列表如下:
MVRTreeMap是一个用于快速的R-Tree实现,使用示例如下:
// create an in-memory store MVStore s = MVStore.open(null); // open an R-tree map MVRTreeMap<String> r = s.openMap("data", new MVRTreeMap.Builder<String>()); // add two key-value pairs // the first value is the key id (to make the key unique) // then the min x, max x, min y, max y r.add(new SpatialKey(0, -3f, -2f, 2f, 3f), "left"); r.add(new SpatialKey(1, 3f, 4f, 4f, 5f), "right"); // iterate over the intersecting keys Iterator<SpatialKey> it = r.findIntersectingKeys(new SpatialKey(0, 0f, 9f, 3f, 6f)); for (SpatialKey k; it.hasNext();) { k = it.next(); System.out.println(k + ": " + r.get(k)); } s.close();
默认维度是2,new MVRTreeMap.Builder<String>().dimensions(3)这样可以设置一个不同的维度数,维度的取值最大值是32,最小值是1.
每一个store含有一组命名map。每个map按key存储,支持通用查找操作,比如查找第一个,查找最后一个,迭代部分或者全部的key等等等。
也支持一些不太通用的操作:快速的按索引查找、高效的根据key算出其索引(位置、index)。也就是意味着取中间的两个key也是非常快的,也能快速统计某个范围内的key。The iterator supports fast skipping. This is possible because internally, each map is organized in the form of a counted B+-tree.
在数据库侧,一个map能被一张表一样使用,map的key就是表的主键,map的值就是表的行。map也能代笔索引,map的key相当于索引的key,map的值相当于表的主键(针对那种非联合索引,map的key需含有主键字段)
版本是指在 指定时间的所有map中所有数据的一个快照。创建快照的速度很快:仅仅复制上一个快照后发生改变的page。这种行为通常也叫作COW(copy on write)。旧版本变成只读的。支持回滚到一个旧版本。
下面的示例代码展示了如何创建一个store,打开一个map,增加一些数据和存取当前的以及旧版本的数据:
// create/get the map named "data" MVMap<Integer, String> map = s.openMap("data"); // add some data map.put(1, "Hello"); map.put(2, "World"); // get the current version, for later use long oldVersion = s.getCurrentVersion(); // from now on, the old version is read-only s.commit(); // more changes, in the new version // changes can be rolled back if required // changes always go into "head" (the newest version) map.put(1, "Hi"); map.remove(2); // access the old data (before the commit) MVMap<Integer, String> oldMap = map.openVersion(oldVersion); // print the old version (can be done // concurrently with further modifications) // this will print "Hello" and "World": System.out.println(oldMap.get(1)); System.out.println(oldMap.get(2)); // print the newest version ("Hi") System.out.println(map.get(1));
支持多并发的事务开启
标签:
原文地址:http://www.cnblogs.com/simoncook/p/5188105.html