/simpledbm-rss/code/simpledbm-rss/src/test/java/org/simpledbm/rss/impl/im/btree/TestBTreeManager.java
Java | 3999 lines | 3275 code | 283 blank | 441 comment | 249 complexity | 1a67f4013060108c5770fd6e0fe78476 MD5 | raw file
Possible License(s): GPL-2.0
Large files files are truncated, but you can click here to view the full file
- /***
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *
- * Linking this library statically or dynamically with other modules
- * is making a combined work based on this library. Thus, the terms and
- * conditions of the GNU General Public License cover the whole
- * combination.
- *
- * As a special exception, the copyright holders of this library give
- * you permission to link this library with independent modules to
- * produce an executable, regardless of the license terms of these
- * independent modules, and to copy and distribute the resulting
- * executable under terms of your choice, provided that you also meet,
- * for each linked independent module, the terms and conditions of the
- * license of that module. An independent module is a module which
- * is not derived from or based on this library. If you modify this
- * library, you may extend this exception to your version of the
- * library, but you are not obligated to do so. If you do not wish
- * to do so, delete this exception statement from your version.
- *
- * Project: www.simpledbm.org
- * Author : Dibyendu Majumdar
- * Email : d dot majumdar at gmail dot com ignore
- */
- package org.simpledbm.rss.impl.im.btree;
-
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.nio.ByteBuffer;
- import java.util.Arrays;
- import java.util.Properties;
- import java.util.StringTokenizer;
- import java.util.concurrent.TimeUnit;
- import java.util.concurrent.atomic.AtomicInteger;
- import java.util.concurrent.locks.Condition;
- import java.util.concurrent.locks.Lock;
- import java.util.concurrent.locks.ReentrantLock;
-
- import junit.framework.Test;
- import junit.framework.TestSuite;
-
- import org.simpledbm.common.api.exception.SimpleDBMException;
- import org.simpledbm.common.api.key.IndexKey;
- import org.simpledbm.common.api.key.IndexKeyFactory;
- import org.simpledbm.common.api.locking.LockMode;
- import org.simpledbm.common.api.platform.Platform;
- import org.simpledbm.common.api.platform.PlatformObjects;
- import org.simpledbm.common.api.registry.ObjectRegistry;
- import org.simpledbm.common.api.tx.IsolationMode;
- import org.simpledbm.common.impl.registry.ObjectRegistryImpl;
- import org.simpledbm.common.tools.diagnostics.TraceBuffer;
- import org.simpledbm.common.util.ByteString;
- import org.simpledbm.junit.BaseTestCase;
- import org.simpledbm.rss.api.bm.BufferAccessBlock;
- import org.simpledbm.rss.api.fsm.FreeSpaceManager;
- import org.simpledbm.rss.api.im.IndexContainer;
- import org.simpledbm.rss.api.im.IndexManager;
- import org.simpledbm.rss.api.im.IndexScan;
- import org.simpledbm.rss.api.im.UniqueConstraintViolationException;
- import org.simpledbm.rss.api.latch.LatchFactory;
- import org.simpledbm.rss.api.loc.Location;
- import org.simpledbm.rss.api.loc.LocationFactory;
- import org.simpledbm.rss.api.locking.LockDeadlockException;
- import org.simpledbm.rss.api.locking.LockDuration;
- import org.simpledbm.rss.api.locking.LockMgrFactory;
- import org.simpledbm.rss.api.locking.util.LockAdaptor;
- import org.simpledbm.rss.api.pm.Page;
- import org.simpledbm.rss.api.pm.PageId;
- import org.simpledbm.rss.api.pm.PageManager;
- import org.simpledbm.rss.api.sp.SlottedPage;
- import org.simpledbm.rss.api.sp.SlottedPageManager;
- import org.simpledbm.rss.api.st.StorageContainer;
- import org.simpledbm.rss.api.st.StorageContainerFactory;
- import org.simpledbm.rss.api.st.StorageManager;
- import org.simpledbm.rss.api.tx.BaseLockable;
- import org.simpledbm.rss.api.tx.LoggableFactory;
- import org.simpledbm.rss.api.tx.Savepoint;
- import org.simpledbm.rss.api.tx.Transaction;
- import org.simpledbm.rss.api.tx.TransactionalModuleRegistry;
- import org.simpledbm.rss.api.wal.Lsn;
- import org.simpledbm.rss.impl.bm.BufferManagerImpl;
- import org.simpledbm.rss.impl.fsm.FreeSpaceManagerImpl;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.BTreeContext;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.BTreeImpl;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.BTreeNode;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.IndexItem;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.IndexItemFactory;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.LoadPageOperation;
- import org.simpledbm.rss.impl.im.btree.BTreeIndexManagerImpl.SearchResult;
- import org.simpledbm.rss.impl.latch.LatchFactoryImpl;
- import org.simpledbm.rss.impl.locking.LockEventListener;
- import org.simpledbm.rss.impl.locking.LockManagerFactoryImpl;
- import org.simpledbm.rss.impl.locking.LockManagerImpl;
- import org.simpledbm.rss.impl.locking.util.DefaultLockAdaptor;
- import org.simpledbm.rss.impl.pm.PageManagerImpl;
- import org.simpledbm.rss.impl.sp.SlottedPageImpl;
- import org.simpledbm.rss.impl.sp.SlottedPageManagerImpl;
- import org.simpledbm.rss.impl.st.FileStorageContainerFactory;
- import org.simpledbm.rss.impl.st.StorageManagerImpl;
- import org.simpledbm.rss.impl.tx.LoggableFactoryImpl;
- import org.simpledbm.rss.impl.tx.TransactionManagerImpl;
- import org.simpledbm.rss.impl.tx.TransactionalModuleRegistryImpl;
- import org.simpledbm.rss.impl.wal.LogFactoryImpl;
- import org.simpledbm.rss.impl.wal.LogManagerImpl;
-
- public class TestBTreeManager extends BaseTestCase {
-
- static final short TYPE_STRINGKEYFACTORY = 25000;
- static final short TYPE_ROWLOCATIONFACTORY = 25001;
-
- boolean doCrashTesting = false;
-
- public TestBTreeManager() {
- super();
- }
-
- public TestBTreeManager(String arg0) {
- super(arg0);
- }
-
- public TestBTreeManager(String arg0, boolean crashTesting) {
- super(arg0);
- doCrashTesting = crashTesting;
- }
-
- static boolean compressKeys = false;
- static boolean largeBM = false;
-
- /**
- * A simple string key.
- */
- public static class StringKey implements IndexKey {
-
- static final String MAX_KEY = "<INFINITY>";
- static final String MIN_KEY = "<NEG_INFINITY>";
-
- ByteString string = new ByteString();
-
- public StringKey() {
- }
-
- public StringKey(StringKey key) {
- string = new ByteString(key.string);
- }
-
- public StringKey(ByteBuffer bb) {
- string = new ByteString(bb);
- }
-
- public IndexKey cloneIndexKey() {
- return new StringKey(this);
- }
-
- public StringKey(final String s) {
- setString(s);
- }
-
- public void setBytes(byte[] bytes) {
- string = new ByteString(bytes);
- }
-
- @Override
- public String toString() {
- if (isMaxKey()) {
- return MAX_KEY;
- } else if (isMinKey()) {
- return MIN_KEY;
- }
- return string.toString().trim();
- }
-
- public void setString(String string) {
- if (MAX_KEY.equals(string)) {
- this.string = new ByteString(new byte[1]);
- } else if (MIN_KEY.equals(string)) {
- byte[] data = { ' ' };
- this.string = new ByteString(data);
- } else {
- byte data[];
- if (!compressKeys) {
- data = new byte[1024];
- Arrays.fill(data, (byte) ' ');
- byte[] srcdata = string.getBytes();
- System.arraycopy(srcdata, 0, data, 0, srcdata.length);
- } else {
- data = string.getBytes();
- }
- this.string = new ByteString(data);
- }
- }
-
- public int getStoredLength() {
- return string.getStoredLength();
- }
-
- public void store(ByteBuffer bb) {
- string.store(bb);
- }
-
- public boolean isMaxKey() {
- return string.length() == 1 && string.get(0) == 0;
- }
-
- public boolean isMinKey() {
- return string.length() == 1 && string.get(0) == ' ';
- }
-
- public int compareTo(IndexKey o) {
- StringKey sk = (StringKey) o;
- if (isMaxKey() || sk.isMaxKey()) {
- if (isMaxKey() && sk.isMaxKey()) {
- return 0;
- } else if (isMaxKey()) {
- return 1;
- } else {
- return -1;
- }
- } else if (isMinKey() || sk.isMinKey()) {
- if (isMinKey() && sk.isMinKey()) {
- return 0;
- } else if (isMinKey()) {
- return -1;
- } else {
- return 1;
- }
- }
- return string.compareTo(sk.string);
- }
-
- @Override
- public boolean equals(Object arg0) {
- return compareTo((IndexKey) arg0) == 0;
- }
- }
-
- public static class StringKeyFactory implements IndexKeyFactory {
-
- public IndexKey newIndexKey(int id) {
- return new StringKey();
- }
-
- public IndexKey maxIndexKey(int id) {
- StringKey s = new StringKey();
- s.setBytes(new byte[1]);
- return s;
- }
-
- public IndexKey minIndexKey(int id) {
- StringKey s = new StringKey(StringKey.MIN_KEY);
- return s;
- }
-
- public IndexKey newIndexKey(int containerId, ByteBuffer bb) {
- return new StringKey(bb);
- }
-
- public IndexKey parseIndexKey(int containerId, String s) {
- return new StringKey(s);
- }
-
- }
-
- /**
- * A simple location.
- */
- public static class RowLocation extends BaseLockable implements Location {
-
- int loc;
-
- protected RowLocation() {
- super((byte) 'R');
- }
-
- RowLocation(RowLocation other) {
- super(other);
- this.loc = other.loc;
- }
-
- RowLocation(ByteBuffer bb) {
- super((byte) 'R');
- loc = bb.getInt();
- }
-
- RowLocation(String s) {
- super((byte) 'R');
- loc = Integer.parseInt(s);
- }
-
- public Location cloneLocation() {
- return new RowLocation(this);
- }
-
- public void store(ByteBuffer bb) {
- bb.putInt(loc);
- }
-
- public int getStoredLength() {
- return Integer.SIZE / Byte.SIZE;
- }
-
- public int compareTo(Location o) {
- RowLocation rl = (RowLocation) o;
- return loc - rl.loc;
- }
-
- @Override
- public boolean equals(Object o) {
- return compareTo((Location) o) == 0;
- }
-
- @Override
- public int hashCode() {
- return loc;
- }
-
- @Override
- public String toString() {
- return Integer.toString(loc);
- }
-
- public int getContainerId() {
- return loc;
- }
-
- public int getX() {
- return 0;
- }
-
- public int getY() {
- return 0;
- }
- }
-
- public static class RowLocationFactory implements LocationFactory {
-
- public Location newLocation() {
- return new RowLocation();
- }
-
- public Location newLocation(ByteBuffer bb) {
- return new RowLocation(bb);
- }
-
- public Location newLocation(String s) {
- return new RowLocation(s);
- }
-
- }
-
- private IndexItem generateKey(IndexItemFactory btree, String s,
- int location, int childpage, boolean isLeaf) {
- StringKey key = (StringKey) btree.getNewIndexKey(1, s);
- RowLocation loc = (RowLocation) btree.getNewLocation();
- loc.loc = location;
- IndexItem item = new IndexItem(key, loc, childpage, isLeaf, btree
- .isUnique());
- return item;
- }
-
- /**
- * Initialize the test harness. New log is created, and the test container
- * initialized. The container is allocated an extent of 64 pages which ought
- * to be large enough for all the test cases.
- */
- void doInitContainer() throws Exception {
-
- final BTreeDB db = new BTreeDB(platform, true);
-
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- db.spacemgr.createContainer(trx, "testctr.dat", 1, 1, 20, db.spmgr
- .getPageType());
- trx.commit();
- } finally {
- db.shutdown();
- }
- }
-
- void doInitContainer2() throws Exception {
- final BTreeDB db = new BTreeDB(platform, true);
-
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- db.btreeMgr.createIndex(trx, "testctr.dat", 1, 20,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, true);
- trx.commit();
- } finally {
- db.shutdown();
- }
-
- }
-
- void doLoadData(ScanResult[] results) throws Exception {
-
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- IndexContainer index = db.btreeMgr.getIndex(1);
-
- IndexKeyFactory keyFactory = (IndexKeyFactory) db.objectFactory
- .getSingleton(TYPE_STRINGKEYFACTORY);
- LocationFactory locationFactory = (LocationFactory) db.objectFactory
- .getSingleton(TYPE_ROWLOCATIONFACTORY);
-
- for (int i = 0; i < results.length; i++) {
- IndexKey key = keyFactory.parseIndexKey(1, results[i].getKey());
- Location location = locationFactory.newLocation(results[i]
- .getLocation());
-
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- index.insert(trx, key, location);
- trx.commit();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doLoadData(String filename) throws Exception {
-
- BufferedReader reader = new BufferedReader(new InputStreamReader(Thread
- .currentThread().getContextClassLoader().getResourceAsStream(
- filename)));
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- IndexContainer index = db.btreeMgr.getIndex(1);
-
- IndexKeyFactory keyFactory = (IndexKeyFactory) db.objectFactory
- .getSingleton(TYPE_STRINGKEYFACTORY);
- LocationFactory locationFactory = (LocationFactory) db.objectFactory
- .getSingleton(TYPE_ROWLOCATIONFACTORY);
-
- String line = reader.readLine();
- while (line != null) {
- StringTokenizer st = new StringTokenizer(line, " ");
- String k = st.nextToken();
- IndexKey key = keyFactory.parseIndexKey(1, k);
- String l = st.nextToken();
- Location location = locationFactory.newLocation(l);
-
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- // System.out.println("Inserting (" + key + ", " + location +
- // ")");
- index.insert(trx, key, location);
- trx.commit();
- line = reader.readLine();
- }
- } finally {
- reader.close();
- db.shutdown();
- }
- }
-
- /**
- * Initialize data pages by loading data from specified XML resource.
- */
- public void doLoadXml(boolean testUnique, String dataFile) throws Exception {
-
- final BTreeDB db = new BTreeDB(platform, false);
-
- try {
- BTreeIndexManagerImpl.XMLLoader loader = new BTreeIndexManagerImpl.XMLLoader(
- db.btreeMgr);
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- try {
- loader.parseResource(dataFile);
- for (LoadPageOperation loadPageOp : loader.getPageOperations()) {
- final PageId pageid = loadPageOp.getPageId();
- final BTreeIndexManagerImpl btreemgr = db.btreeMgr;
- BufferAccessBlock bab = btreemgr.bufmgr.fixExclusive(
- pageid, false, -1, 0);
- try {
- PageId spaceMapPageId = new PageId(pageid
- .getContainerId(), loadPageOp
- .getSpaceMapPageNumber());
- BufferAccessBlock smpBab = btreemgr.bufmgr
- .fixExclusive(spaceMapPageId, false, -1, 0);
- try {
- Lsn lsn = trx.logInsert(bab.getPage(), loadPageOp);
- btreemgr.redo(bab.getPage(), loadPageOp);
- bab.setDirty(lsn);
- btreemgr.redo(smpBab.getPage(), loadPageOp);
- smpBab.setDirty(lsn);
- } finally {
- smpBab.unfix();
- }
- } finally {
- bab.unfix();
- }
- }
- } finally {
- // Doesn't matter whether we commit or abort as the changes are
- // logged as redo-only.
- trx.abort();
- }
- } finally {
- db.shutdown();
- }
- }
-
- /**
- * Initialize data pages by loading data from specified XML resource.
- */
- public void doValidateTree(String dataFile) throws Exception {
-
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- doValidateTree(db, dataFile);
- } finally {
- db.shutdown();
- }
- }
-
- /**
- * Initialize data pages by loading data from specified XML resource.
- */
- public void doValidateTree(final BTreeDB db, String dataFile)
- throws Exception {
-
- BTreeIndexManagerImpl.XMLLoader loader = new BTreeIndexManagerImpl.XMLLoader(
- db.btreeMgr);
- loader.parseResource(dataFile);
- for (LoadPageOperation loadPageOp : loader.getPageOperations()) {
- final PageId pageid = loadPageOp.getPageId();
- final BTreeIndexManagerImpl btreemgr = db.btreeMgr;
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- BufferAccessBlock bab = btreemgr.bufmgr.fixExclusive(pageid, false,
- -1, 0);
- try {
- /*
- * Log record is being applied to BTree page.
- */
- SlottedPage r = (SlottedPage) bab.getPage();
- BTreeNode node = new BTreeNode(po, loadPageOp
- .getIndexItemFactory(), r);
- // System.out.println("Validating page ->");
- // node.dumpAsXml();
- // System.out.println("------------------");
- assertEquals(node.header.keyCount, loadPageOp.items.size());
- assertEquals(node.header.leftSibling, loadPageOp.leftSibling);
- assertEquals(node.header.rightSibling, loadPageOp.rightSibling);
- assertEquals(node.header.keyFactoryType, loadPageOp
- .getKeyFactoryType());
- assertEquals(node.header.locationFactoryType, loadPageOp
- .getLocationFactoryType());
- assertEquals(node.isLeaf(), loadPageOp.isLeaf());
- assertEquals(node.isUnique(), loadPageOp.isUnique());
- for (int k = 1, i = 0; k < r.getNumberOfSlots(); k++, i++) {
- if (r.isSlotDeleted(k)) {
- continue;
- }
- IndexItem item = loadPageOp.items.get(i);
- IndexItem item1 = node.getItem(k);
- assertEquals(item, item1);
- }
- } finally {
- bab.unfix();
- }
- }
- }
-
- /*
- * Splits page 2 into 2 and 3.
- */
- void doPageSplit(boolean testLeaf, boolean testUnique) throws Exception {
- /* Create the write ahead log */
- final BTreeDB db = new BTreeDB(platform, false);
-
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- int pageNumber = 2;
- Transaction trx;
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, pageNumber), 0));
- try {
- trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> SPLITTING PAGE");
- bcursor.searchKey = generateKey(btree.indexItemFactory,
- "da", 5, -1, testLeaf);
- btree.doSplit(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- }
- } finally {
- db.shutdown();
- }
- }
-
- /*
- * Merges previously split pages 2 and 3.
- */
- void doRestartAndMerge(boolean testLeaf, boolean testUnique, int l, int r)
- throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
-
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- // System.out.println("--> BEFORE MERGE");
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, l), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, r), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, l), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, r), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> MERGING PAGE");
- btree.doMerge(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- }
- } finally {
- db.shutdown();
- }
- }
-
- /*
- * Merges previously split pages 2 and 3.
- */
- void doRestart() throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- System.out.println("RESTART PROCESSING COMPLETED");
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartAndLink(boolean testLeaf, boolean testUnique)
- throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- // System.out.println("--> BEFORE LINKING CHILD TO PARENT");
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, 2), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, 3), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- // prepareParentPage(pageFactory, bufmgr, btree, 1, 4, testUnique,
- // 2);
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- bcursor.setP(db.bufmgr.fixForUpdate(new PageId(1, 4), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> LINKING CHILD TO PARENT");
- btree.doLink(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- bcursor.unfixR();
- bcursor.unfixP();
- }
-
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- bcursor.setP(db.bufmgr.fixForUpdate(new PageId(1, 4), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> DE-LINKING CHILD FROM PARENT");
- btree.doUnlink(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- bcursor.unfixR();
- }
-
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartLink(boolean testLeaf, boolean testUnique) throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- // System.out.println("--> BEFORE LINKING CHILD TO PARENT");
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, 2), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, 3), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- // prepareParentPage(pageFactory, bufmgr, btree, 1, 4, testUnique,
- // 2);
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- bcursor.setP(db.bufmgr.fixForUpdate(new PageId(1, 4), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> LINKING CHILD TO PARENT");
- btree.doLink(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- bcursor.unfixR();
- bcursor.unfixP();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartDelink(boolean testLeaf, boolean testUnique) throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- bcursor.setP(db.bufmgr.fixForUpdate(new PageId(1, 4), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> DE-LINKING CHILD FROM PARENT");
- btree.doUnlink(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- bcursor.unfixR();
- }
-
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartAndUnlink(boolean testLeaf, boolean testUnique, int p, int q,
- int r) throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, q), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, r), 0));
- bcursor.setP(db.bufmgr.fixForUpdate(new PageId(1, p), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> DE-LINKING CHILD FROM PARENT");
- btree.doUnlink(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- bcursor.unfixR();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartAndRedistribute(boolean testLeaf, boolean testUnique)
- throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- // System.out.println("--> BEFORE REDISTRIBUTING KEYS");
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, 2), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, 3), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- bcursor.searchKey = generateKey(btree.indexItemFactory,
- "da", 5, -1, testLeaf);
- // System.out.println("--> REDISTRIBUTING KEYS");
- btree.doRedistribute(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doRedistributeIssue28() throws Exception {
- boolean testUnique = false;
- boolean testLeaf = true;
-
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, 2), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, 3), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- bcursor.searchKey = generateKey(btree.indexItemFactory,
- "da", 5, -1, testLeaf);
- btree.doRedistribute(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartAndIncreaseTreeHeight(boolean testLeaf, boolean testUnique)
- throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- // System.out.println("--> BEFORE INCREASING TREE HEIGHT");
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, 2), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, 3), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- bcursor.searchKey = generateKey(btree.indexItemFactory,
- "da", 5, -1, testLeaf);
- // System.out.println("--> INCREASING TREE HEIGHT");
- btree.doIncreaseTreeHeight(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doRestartAndDecreaseTreeHeight(boolean testLeaf, boolean testUnique,
- int p, int q) throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- // System.out.println("--> BEFORE DECREASING TREE HEIGHT");
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, p), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, q), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setP(db.bufmgr.fixForUpdate(new PageId(1, p), 0));
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, q), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- bcursor.searchKey = generateKey(btree.indexItemFactory,
- "da", 5, -1, testLeaf);
- // System.out.println("--> DECREASING TREE HEIGHT");
- btree.doDecreaseTreeHeight(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixP();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doNewRedistribute(boolean testLeaf) throws Exception {
- boolean testUnique = false;
-
- final BTreeDB db = new BTreeDB(platform, false);
- final PlatformObjects po = db.platform
- .getPlatformObjects(IndexManager.LOGGER_NAME);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
- BufferAccessBlock bab = db.bufmgr.fixShared(new PageId(1, 2), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
- bab = db.bufmgr.fixShared(new PageId(1, 3), 0);
- try {
- BTreeNode node = new BTreeNode(po, btree.indexItemFactory, bab
- .getPage());
- node.dump();
- } finally {
- bab.unfix();
- }
-
- BTreeContext bcursor = new BTreeContext(db.tracer);
- bcursor.setQ(db.bufmgr.fixForUpdate(new PageId(1, 2), 0));
- bcursor.setR(db.bufmgr.fixForUpdate(new PageId(1, 3), 0));
- try {
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- bcursor.searchKey = generateKey(btree.indexItemFactory,
- "da", 5, -1, testLeaf);
- btree.doNewRedistribute(trx, bcursor);
- } finally {
- if (okay)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- bcursor.unfixQ();
- }
- } finally {
- db.shutdown();
- }
- }
-
- /**
- * Test case for Issue 64.
- */
- void doSearchIssue64() throws Exception {
- final boolean testUnique = false;
-
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- IndexKeyFactory keyFactory = (IndexKeyFactory) db.objectFactory
- .getSingleton(TYPE_STRINGKEYFACTORY);
- LocationFactory locationFactory = (LocationFactory) db.objectFactory
- .getSingleton(TYPE_ROWLOCATIONFACTORY);
- IndexKey key = keyFactory.parseIndexKey(1, "zzz");
- Location location = locationFactory.newLocation();
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- IndexScan scan = btree.openScan(trx, key, location, true);
- try {
- if (scan.fetchNext()) {
- fail("Unexpected result");
- }
- } catch (SimpleDBMException e) {
- fail("Unexpected exception " + e.getMessage());
- } finally {
- scan.close();
- trx.abort();
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doSingleInsert(boolean testUnique, boolean commit, String k, String loc)
- throws Exception {
- doSingleInsert(testUnique, commit, k, loc, null, null);
- }
-
- void doSingleInsert(boolean testUnique, boolean commit, String k,
- String loc, String commitresult, String abortresult)
- throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- IndexKeyFactory keyFactory = (IndexKeyFactory) db.objectFactory
- .getSingleton(TYPE_STRINGKEYFACTORY);
- LocationFactory locationFactory = (LocationFactory) db.objectFactory
- .getSingleton(TYPE_ROWLOCATIONFACTORY);
- IndexKey key = keyFactory.parseIndexKey(1, k);
- Location location = locationFactory.newLocation(loc);
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> INSERTING KEY");
- btree.insert(trx, key, location);
- if (commitresult != null) {
- doValidateTree(db, commitresult);
- }
- okay = true;
- } finally {
- if (okay && commit) {
- // System.out.println("--> COMMITTING INSERT");
- trx.commit();
- if (commitresult != null) {
- doValidateTree(db, commitresult);
- }
- } else {
- // System.out.println("--> ABORTING INSERT");
- trx.abort();
- if (abortresult != null) {
- doValidateTree(db, abortresult);
- }
- }
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doDoubleInsert(boolean testUnique, boolean commit1, String k1,
- String loc1, boolean commit2, String k2, String loc2,
- String result1, String result2, String result3) throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- IndexKeyFactory keyFactory = (IndexKeyFactory) db.objectFactory
- .getSingleton(TYPE_STRINGKEYFACTORY);
- LocationFactory locationFactory = (LocationFactory) db.objectFactory
- .getSingleton(TYPE_ROWLOCATIONFACTORY);
-
- IndexKey key1 = keyFactory.parseIndexKey(1, k1);
- Location location1 = locationFactory.newLocation(loc1);
- Transaction trx1 = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay1 = false;
- try {
- // System.out.println("--> INSERTING KEY 1");
- btree.insert(trx1, key1, location1);
- doValidateTree(db, result1);
- okay1 = true;
-
- IndexKey key2 = keyFactory.parseIndexKey(1, k2);
- Location location2 = locationFactory.newLocation(loc2);
- Transaction trx2 = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay2 = false;
-
- try {
- // System.out.println("--> INSERTING KEY 2");
- btree.insert(trx2, key2, location2);
- doValidateTree(db, result2);
- okay2 = true;
- } finally {
- if (okay2 && commit2) {
- // System.out.println("--> COMMITTING KEY 2");
- trx2.commit();
- } else {
- // System.out.println("--> ABORTING KEY 2");
- trx2.abort();
- }
- }
- } finally {
- if (okay1 && commit1) {
- // System.out.println("--> COMMITTING KEY 1");
- trx1.commit();
- } else {
- // System.out.println("--> ABORTING KEY 1");
- trx1.abort();
- doValidateTree(db, result3);
- }
- }
- } finally {
- db.shutdown();
- }
- }
-
- void doSingleDelete(boolean testUnique, boolean commit, String k, String loc)
- throws Exception {
- final BTreeDB db = new BTreeDB(platform, false);
- try {
- final BTreeImpl btree = db.btreeMgr.getBTreeImpl(1,
- TYPE_STRINGKEYFACTORY, TYPE_ROWLOCATIONFACTORY, testUnique);
-
- IndexKeyFactory keyFactory = (IndexKeyFactory) db.objectFactory
- .getSingleton(TYPE_STRINGKEYFACTORY);
- LocationFactory locationFactory = (LocationFactory) db.objectFactory
- .getSingleton(TYPE_ROWLOCATIONFACTORY);
- IndexKey key = keyFactory.parseIndexKey(1, k);
- Location location = locationFactory.newLocation(loc);
- Transaction trx = db.trxmgr.begin(IsolationMode.SERIALIZABLE);
- boolean okay = false;
- try {
- // System.out.println("--> DELETING KEY");
- btree.delete(trx, key, location);
- okay = true;
- } finally {
- if (okay && commit)
- trx.commit();
- else {
- trx.abort();
- }
- }
- } finally {
- db.shutdown();
- }
- }
-
- boolean t1Failed = false;
- boolean t2Failed = f…
Large files files are truncated, but you can click here to view the full file