Package org.chaidb.db

Source Code of org.chaidb.db.TestTxn

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* 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.
*
*/

package org.chaidb.db;

import org.chaidb.db.api.*;
import org.chaidb.db.api.keys.IntKey;
import org.chaidb.db.exception.ChaiDBException;

/**
* @author Kurt Sung
*/
public class TestTxn {
    private static Database db;

    public static void main(String[] args) throws ChaiDBException {
        db = new Database("D:\\temp");
        try {
            testCommit();
            testLookup();
            testRollback();
            testLookup();
            testDelete();
        } finally {
            db.close();
        }
    }

    private static void testCommit() throws ChaiDBException {
        Transaction txn = db.beginTransaction();
        BTree btree = db.openBTree("testtxn.idb", BTreeType.HYPER_BTREE);
        try {
            btree.acquire(LockType.WRITE);
            for (int i = 0; i < 100; i++) {
                IntKey key = new IntKey(i);
                byte[] data = new byte[2];
                data[0] = (byte) (i * 2 + 1);
                data[1] = (byte) (i * 2 + 2);
                btree.store(key, data);
            }
            btree.release();
            txn.commit();

        } catch (ChaiDBException e) {
            e.printStackTrace();
            txn.rollback();
        } finally {
            btree.close();
        }
    }

    private static void testRollback() throws ChaiDBException {
        Transaction txn = db.beginTransaction();
        BTree btree = db.openBTree("testtxn.idb", BTreeType.HYPER_BTREE);
        try {
            btree.acquire(LockType.WRITE);
            for (int i = 101; i < 200; i++) {
                IntKey key = new IntKey(i);
                byte[] data = new byte[2];
                data[0] = (byte) (i * 2 + 1);
                data[1] = (byte) (i * 2 + 2);
                btree.store(key, data);
            }
            btree.release();
            txn.rollback();

        } catch (ChaiDBException e) {
            e.printStackTrace();
            txn.rollback();
        } finally {
            btree.close();
        }
    }

    private static void testLookup() throws ChaiDBException {
        Transaction txn = db.beginTransaction();
        BTree btree = db.openBTree("testtxn.idb", BTreeType.HYPER_BTREE);
        try {
            btree.acquire(LockType.READ);
            for (int i = 0; i < 100; i++) {
                IntKey key = new IntKey(i);
                System.out.print(key + ":");

                DuplicatedKeyIterator it = btree.lookupValues(key);
                while (it.hasNext()) {
                    byte[] bytes = (byte[]) it.next();
                    for (int j = 0; j < bytes.length; j++) {
                        System.out.print(bytes[j]);
                        System.out.print(" ");
                    }
                }
                System.out.println();
                btree.release();
            }
            txn.commit();
        } catch (ChaiDBException e) {
            e.printStackTrace();
            txn.rollback();
        } finally {
            btree.close();
        }
    }

    private static void testDelete() throws ChaiDBException {
        Transaction txn = db.beginTransaction();
        BTree btree = db.openBTree("testtxn.idb", BTreeType.HYPER_BTREE);
        try {
            btree.acquire(LockType.WRITE);
            for (int i = 0; i < 200; i++) {
                IntKey key = new IntKey(i);
                btree.delete(key);
            }
            btree.release();
            txn.commit();

        } catch (ChaiDBException e) {
            e.printStackTrace();
            txn.rollback();
        } finally {
            btree.close();
        }
    }
}
TOP

Related Classes of org.chaidb.db.TestTxn

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.