码迷,mamicode.com
首页 > 其他好文 > 详细

H2的MVStore

时间:2016-02-13 18:12:40      阅读:1022      评论:0      收藏:0      [点我收藏+]

标签:

翻译自http://www.h2database.com/html/mvstore.html

概述

MVStore是一个持久化的、日志结构是的kv存储。本计划用它作为H2的下一代存储子系统,但你也可以在一个不涉及JDBC或者SQL的应用中直接使用它。

  • MVStore代表多版本存储。
  • 每一个store包含大量的map,这些map可以用java.util.Map接口存取。
  • 支持基于文件存储和基于内存的操作。
  • 它希望更快,更简单的使用,更小。
  • 支持并发读写操作。
  • 支持事务(包括并发事务与两阶段提交(2-phase commit))
  • 模块化的工具,支持插拔式的数据类型定义、序列化实现,支持插拔式的存储载体(存到文件里、存到堆外内存),插拔式的映射实现(B-tree,R-tree,当前用的concurrent B-tree),BLOB存储,文件系统层的抽象以使其支持文件的加密与压缩

示例代码

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();

下面的代码展示了如何使用这些工具

Store Builder

MVStore.Builder提供了一个流畅优美的用可选配置项构造store的接口

示例用法:

MVStore s = new MVStore.Builder().
    fileName(fileName).
    encryptionKey("007".toCharArray()).
    compress().
    open();

可用选项的列表如下:

  • autoCommitBufferSize: 写buffer的大小.
  • autoCommitDisabled: 禁用自动commit.
  • backgroundExceptionHandler: 用于处理后台写入时产生的异常的处理器.
  • cacheSize: 缓存大小,以MB为单位.
  • compress: 是否采用LZF算法进行快速压缩.
  • compressHigh: 是否采用Deflate算法进慢速速压缩.
  • encryptionKey: 文件加密的key.
  • fileName: 基于文件存储时,用于存储的文件名.
  • fileStore: 存储实现.
  • pageSplitSize: pages的分割点.
  • readOnly: 是否以只读形式打开存储文件.

R-Tree

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.

特性

Maps

每一个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));

事务

支持多并发的事务开启

H2的MVStore

标签:

原文地址:http://www.cnblogs.com/simoncook/p/5188105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!