提交 9abf5e1d authored 作者: Thomas Mueller's avatar Thomas Mueller

A concurrent list, to replace the array list of old roots.

上级 1f9fcba2
......@@ -17,122 +17,103 @@ import java.util.Iterator;
*/
public class ConcurrentArrayList<K> {
// /**
// * The list.
// */
// volatile List<K> list = new List<K>(null, 0, 0);
//
// /**
// * Get the first element, or null if none.
// *
// * @return the first element
// */
// public K peekFirst() {
// return list.peekFirst();
// }
//
// /**
// * Get the last element, or null if none.
// *
// * @return the last element
// */
// public K peekLast() {
// return list.peekLast();
// }
//
// /**
// * Add an element at the end.
// *
// * @param obj the element
// */
// public synchronized void add(K obj) {
// K[] array = new K[list.length + 1];
// if (list.length > 0) {
// System.arraycopy(list.list, list.offset, dest, destPos, length)
// Arrays.copyOf(, newLength)
// list = new List<K>(list.add(obj);
// }
//
// /**
// * Remove the first element, if it matches.
// *
// * @param obj the element to remove
// * @return true if the element matched and was removed
// */
// public synchronized boolean removeFirst(K obj) {
// if (head.obj != obj) {
// return false;
// }
// head = head.next;
// return true;
// }
//
// /**
// * Remove the last element, if it matches.
// *
// * @param obj the element to remove
// * @return true if the element matched and was removed
// */
// public synchronized boolean removeLast(K obj) {
// if (peekLast() != obj) {
// return false;
// }
// head = Entry.removeLast(head);
// return true;
// }
//
// /**
// * Get an iterator over all entries.
// *
// * @return the iterator
// */
// public Iterator<K> iterator() {
// return new Iterator<K>() {
//
// List<K> list = head;
//
// @Override
// public boolean hasNext() {
// return current != NULL;
// }
//
// @Override
// public K next() {
// K x = current.obj;
// current = current.next;
// return x;
// }
//
// @Override
// public void remove() {
// throw DataUtils.newUnsupportedOperationException("remove");
// }
//
// };
// }
//
// /**
// * An entry in the linked list.
// */
// private static class List<K> {
// final K[] list;
// final int offset;
// final int length;
//
// List(K[] list, int offset, int length) {
// this.list = list;
// this.offset = offset;
// this.length = length;
// }
//
// public K peekFirst() {
// return length == 0 ? null : list[offset];
// }
//
// public K peekLast() {
// return length == 0 ? null : list[offset + length - 1];
// }
//
// }
/**
* The array.
*/
@SuppressWarnings("unchecked")
K[] array = (K[]) new Object[0];
/**
* Get the first element, or null if none.
*
* @return the first element
*/
public K peekFirst() {
K[] a = array;
return a.length == 0 ? null : a[0];
}
/**
* Get the last element, or null if none.
*
* @return the last element
*/
public K peekLast() {
K[] a = array;
int len = a.length;
return len == 0 ? null : a[len - 1];
}
/**
* Add an element at the end.
*
* @param obj the element
*/
public synchronized void add(K obj) {
int len = array.length;
array = Arrays.copyOf(array, len + 1);
array[len] = obj;
}
/**
* Remove the first element, if it matches.
*
* @param obj the element to remove
* @return true if the element matched and was removed
*/
public synchronized boolean removeFirst(K obj) {
if (peekFirst() != obj) {
return false;
}
int len = array.length;
@SuppressWarnings("unchecked")
K[] a = (K[]) new Object[len - 1];
System.arraycopy(array, 1, a, 0, len - 1);
array = a;
return true;
}
/**
* Remove the last element, if it matches.
*
* @param obj the element to remove
* @return true if the element matched and was removed
*/
public synchronized boolean removeLast(K obj) {
if (peekLast() != obj) {
return false;
}
array = Arrays.copyOf(array, array.length - 1);
return true;
}
/**
* Get an iterator over all entries.
*
* @return the iterator
*/
public Iterator<K> iterator() {
return new Iterator<K>() {
K[] a = array;
int index;
@Override
public boolean hasNext() {
return index < a.length;
}
@Override
public K next() {
return a[index++];
}
@Override
public void remove() {
throw DataUtils.newUnsupportedOperationException("remove");
}
};
}
}
......@@ -53,8 +53,8 @@ public class MVMap<K, V> extends AbstractMap<K, V>
private final DataType keyType;
private final DataType valueType;
private ConcurrentLinkedList<Page> oldRoots =
new ConcurrentLinkedList<Page>();
private ConcurrentArrayList<Page> oldRoots =
new ConcurrentArrayList<Page>();
private boolean closed;
private boolean readOnly;
......
......@@ -10,7 +10,7 @@ import java.util.LinkedList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import org.h2.mvstore.ConcurrentLinkedList;
import org.h2.mvstore.ConcurrentArrayList;
import org.h2.test.TestBase;
import org.h2.util.Task;
......@@ -48,8 +48,10 @@ public class TestConcurrentLinkedList extends TestBase {
private void testPerformance(final boolean stock) {
System.out.print(stock ? "stock " : "custom ");
long start = System.currentTimeMillis();
final ConcurrentLinkedList<Integer> test = new ConcurrentLinkedList<Integer>();
// final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
// final ConcurrentLinkedList<Integer> test =
// new ConcurrentLinkedList<Integer>();
final ConcurrentArrayList<Integer> test =
new ConcurrentArrayList<Integer>();
final LinkedList<Integer> x = new LinkedList<Integer>();
final AtomicInteger counter = new AtomicInteger();
Task task = new Task() {
......@@ -107,7 +109,7 @@ public class TestConcurrentLinkedList extends TestBase {
}
private void testConcurrent() {
final ConcurrentLinkedList<Integer> test = new ConcurrentLinkedList<Integer>();
final ConcurrentArrayList<Integer> test = new ConcurrentArrayList<Integer>();
// final ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
final AtomicInteger counter = new AtomicInteger();
final AtomicInteger size = new AtomicInteger();
......@@ -140,7 +142,7 @@ public class TestConcurrentLinkedList extends TestBase {
private void testRandomized() {
Random r = new Random(0);
for (int i = 0; i < 100; i++) {
ConcurrentLinkedList<Integer> test = new ConcurrentLinkedList<Integer>();
ConcurrentArrayList<Integer> test = new ConcurrentArrayList<Integer>();
// ConcurrentRing<Integer> test = new ConcurrentRing<Integer>();
LinkedList<Integer> x = new LinkedList<Integer>();
StringBuilder buff = new StringBuilder();
......
......@@ -3,10 +3,12 @@
* and the EPL 1.0 (http://h2database.com/html/license.html).
* Initial Developer: H2 Group
*/
package org.h2.mvstore;
package org.h2.dev.util;
import java.util.Iterator;
import org.h2.mvstore.DataUtils;
/**
* A very simple linked list that supports concurrent access.
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论