use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.
the class TestMVRTree method testExample.
private void testExample() {
// 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));
assertNotNull(k);
}
s.close();
}
use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.
the class TestMVRTree method testRandom.
private void testRandom(boolean quadraticSplit) {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s = openStore(fileName);
MVRTreeMap<String> m = s.openMap("data", new MVRTreeMap.Builder<String>());
m.setQuadraticSplit(quadraticSplit);
HashMap<SpatialKey, String> map = new HashMap<>();
Random rand = new Random(1);
int operationCount = 10000;
int maxValue = 300;
for (int i = 0; i < operationCount; i++) {
int key = rand.nextInt(maxValue);
Random rk = new Random(key);
float x = rk.nextFloat(), y = rk.nextFloat();
float p = (float) (rk.nextFloat() * 0.000001);
SpatialKey k = new SpatialKey(key, x - p, x + p, y - p, y + p);
String v = "" + rand.nextInt();
Iterator<SpatialKey> it;
switch(rand.nextInt(5)) {
case 0:
log(i + ": put " + k + " = " + v + " " + m.size());
m.put(k, v);
map.put(k, v);
break;
case 1:
log(i + ": remove " + k + " " + m.size());
m.remove(k);
map.remove(k);
break;
case 2:
{
p = (float) (rk.nextFloat() * 0.01);
k = new SpatialKey(key, x - p, x + p, y - p, y + p);
it = m.findIntersectingKeys(k);
while (it.hasNext()) {
SpatialKey n = it.next();
String a = map.get(n);
assertNotNull(a);
}
break;
}
case 3:
{
p = (float) (rk.nextFloat() * 0.01);
k = new SpatialKey(key, x - p, x + p, y - p, y + p);
it = m.findContainedKeys(k);
while (it.hasNext()) {
SpatialKey n = it.next();
String a = map.get(n);
assertNotNull(a);
}
break;
}
default:
String a = map.get(k);
String b = m.get(k);
if (a == null || b == null) {
assertTrue(a == b);
} else {
assertEquals(a, b);
}
break;
}
assertEquals(map.size(), m.size());
}
s.close();
}
use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.
the class TestMVRTree method testRandomInsert.
private void testRandomInsert() {
String fileName = getBaseDir() + "/" + getTestName();
FileUtils.delete(fileName);
MVStore s;
s = new MVStore.Builder().fileName(fileName).pageSplitSize(100).open();
MVRTreeMap<String> map = s.openMap("data", new MVRTreeMap.Builder<String>());
Random r = new Random(1);
for (int i = 0; i < 1000; i++) {
if (i % 100 == 0) {
r.setSeed(1);
}
float x = r.nextFloat() * 50, y = r.nextFloat() * 50;
SpatialKey k = new SpatialKey(i % 100, x, x + 2, y, y + 1);
map.put(k, "i:" + i);
if (i % 10 == 0) {
s.commit();
}
}
s.close();
}
use of org.h2.mvstore.rtree.MVRTreeMap in project h2database by h2database.
the class TestMVStoreTool method testCompact.
private void testCompact() {
String fileName = getBaseDir() + "/testCompact.h3";
FileUtils.createDirectories(getBaseDir());
FileUtils.delete(fileName);
// store with a very small page size, to make sure
// there are many leaf pages
MVStore s = new MVStore.Builder().pageSplitSize(1000).fileName(fileName).autoCommitDisabled().open();
MVMap<Integer, String> map = s.openMap("data");
for (int i = 0; i < 10; i++) {
map.put(i, "Hello World " + i * 10);
if (i % 3 == 0) {
s.commit();
}
}
for (int i = 0; i < 20; i++) {
map = s.openMap("data" + i);
for (int j = 0; j < i * i; j++) {
map.put(j, "Hello World " + j * 10);
}
s.commit();
}
MVRTreeMap<String> rTreeMap = s.openMap("rtree", new MVRTreeMap.Builder<String>());
Random r = new Random(1);
for (int i = 0; i < 10; i++) {
float x = r.nextFloat();
float y = r.nextFloat();
float width = r.nextFloat() / 10;
float height = r.nextFloat() / 10;
SpatialKey k = new SpatialKey(i, x, x + width, y, y + height);
rTreeMap.put(k, "Hello World " + i * 10);
if (i % 3 == 0) {
s.commit();
}
}
s.close();
MVStoreTool.compact(fileName, fileName + ".new", false);
MVStoreTool.compact(fileName, fileName + ".new.compress", true);
MVStore s1 = new MVStore.Builder().fileName(fileName).readOnly().open();
MVStore s2 = new MVStore.Builder().fileName(fileName + ".new").readOnly().open();
MVStore s3 = new MVStore.Builder().fileName(fileName + ".new.compress").readOnly().open();
assertEquals(s1, s2);
assertEquals(s1, s3);
s1.close();
s2.close();
s3.close();
long size1 = FileUtils.size(fileName);
long size2 = FileUtils.size(fileName + ".new");
long size3 = FileUtils.size(fileName + ".new.compress");
assertTrue("size1: " + size1 + " size2: " + size2 + " size3: " + size3, size2 < size1 && size3 < size2);
MVStoreTool.compact(fileName, false);
assertEquals(size2, FileUtils.size(fileName));
MVStoreTool.compact(fileName, true);
assertEquals(size3, FileUtils.size(fileName));
}
use of org.h2.mvstore.rtree.MVRTreeMap in project ignite by apache.
the class GridH2SpatialIndex method findFirstOrLast.
/**
* {@inheritDoc}
*/
@Override
public Cursor findFirstOrLast(Session ses, boolean first) {
Lock l = lock.readLock();
l.lock();
try {
checkClosed();
if (!first)
throw DbException.throwInternalError("Spatial Index can only be fetch by ascending order");
final int seg = threadLocalSegment();
final MVRTreeMap<Long> segment = segments[seg];
GridCursor<GridH2Row> iter = rowIterator(segment.keySet().iterator(), null);
return new SingleRowCursor(iter.next() ? iter.get() : null);
} catch (IgniteCheckedException e) {
throw DbException.convert(e);
} finally {
l.unlock();
}
}
Aggregations