提交 510a0427 authored 作者: noelgrandin's avatar noelgrandin

use Collections.binarySearch for searching through oldRoots

上级 4fd45327
...@@ -9,6 +9,8 @@ package org.h2.dev.store.btree; ...@@ -9,6 +9,8 @@ package org.h2.dev.store.btree;
import java.util.AbstractMap; import java.util.AbstractMap;
import java.util.AbstractSet; import java.util.AbstractSet;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
...@@ -564,15 +566,8 @@ public class MVMap<K, V> extends AbstractMap<K, V> { ...@@ -564,15 +566,8 @@ public class MVMap<K, V> extends AbstractMap<K, V> {
return; return;
} }
ArrayList<Page> list = oldRoots; ArrayList<Page> list = oldRoots;
int i = 0; int i = Collections.binarySearch(list, oldest, PAGE_VERSION_COMPARATOR);
// TODO iterate over the list is inefficient if (i < 0) {
for (; i < list.size(); i++) {
Page p = list.get(i);
if (p.getVersion() > oldest) {
break;
}
}
if (i == 0) {
return; return;
} }
// create a new instance // create a new instance
...@@ -582,6 +577,21 @@ public class MVMap<K, V> extends AbstractMap<K, V> { ...@@ -582,6 +577,21 @@ public class MVMap<K, V> extends AbstractMap<K, V> {
oldRoots = list; oldRoots = list;
} }
private static final Comparator<Object> PAGE_VERSION_COMPARATOR = new Comparator<Object>() {
@Override
public int compare(Object o1, Object o2) {
Page p = (Page) o1;
Long key = (Long) o2;
if (p.getVersion() == key)
return 0;
else if (p.getVersion() < key)
return -1;
else
return 1;
}
};
public void setReadOnly(boolean readOnly) { public void setReadOnly(boolean readOnly) {
this.readOnly = readOnly; this.readOnly = readOnly;
} }
...@@ -674,14 +684,15 @@ public class MVMap<K, V> extends AbstractMap<K, V> { ...@@ -674,14 +684,15 @@ public class MVMap<K, V> extends AbstractMap<K, V> {
if (r.getVersion() == version) { if (r.getVersion() == version) {
newest = r; newest = r;
} else { } else {
// TODO could do a binary search
ArrayList<Page> list = oldRoots; ArrayList<Page> list = oldRoots;
for (int i = 0; i < list.size(); i++) { // find the newest page that has a getVersion() <= version
Page p = list.get(i); int i = Collections.binarySearch(list, version, PAGE_VERSION_COMPARATOR);
if (p.getVersion() <= version) { if (i > 0) {
newest = p; newest = list.get(i);
} else { } else {
break; i = -(i+1); // calculate insertion point
if (i > 0) {
newest = list.get(i - 1);
} }
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论