Package org.chaidb.sample

Source Code of org.chaidb.sample.TestChaiDB

/*
* Copyright (c) 2006, chaidb.org. All Rights Reserved.
*/

package org.chaidb.sample;

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

public class TestChaiDB {
    public static void main(String[] args) {
        TestChaiDB instance = new TestChaiDB();
        System.out.println("========== Test Without Transaction ==========");
        instance.testWithoutTransaction();
        System.out.println("========== Test With Transaction ==========");
        instance.testWithTransaction();
    }

    public void testWithoutTransaction() {
        Database db = null;
        try {
            System.out.println("Initializing database...");
            db = new Database("D:\\temp");
            System.out.println("Initializing BTree...");
            BTree testTree = db.openBTree("notxn.btree", BTreeType.BTREE);
            IntKey testKey = new IntKey(1);
            //store
            System.out.println("Testing store...");
            testTree.store(testKey, "Test Value".getBytes());
            //lookup
            System.out.println("Testing lookup...");
            Object o = testTree.lookup(testKey);
            if (o == null) {
                System.out.println("There in no key 1 in the btree");
            } else {
                System.out.println("Value of key 1 is " + new String((byte[])o));
            }

            //delete
            System.out.println("Testing delete...");
            testTree.delete(testKey);
            //lookup again
            System.out.println("Testing lookup...");
            o = testTree.lookup(testKey);
            if (o == null) {
                System.out.println("There in no key 1 in the btree");
            } else {
                System.out.println("Value of key 1 is " + new String((byte[])o));
            }

            System.out.println("Closing BTree...");
            testTree.close();
        } catch (ChaiDBException e) {
            e.printStackTrace();
        } finally{
            try {
                System.out.println("Closing database...");
                if(db!=null) db.close();
            } catch (ChaiDBException e) {
                e.printStackTrace();
            }
        }
        System.out.println("All tests completed.");
    }

    public void testWithTransaction() {
        Database db = null;
        try {
            System.out.println("Initializing database...");
            db = new Database("D:\\temp");
            Transaction txn = db.beginTransaction();
            BTree testTree = null;
            IntKey testKey = null;
            Object o = null;
            try {
                System.out.println("Initializing BTree...");
                testTree = db.openBTree("txn.btree", BTreeType.BTREE);
                testKey = new IntKey(1);
                //store
                System.out.println("Testing store...");
                testTree.store(testKey, "Test Value".getBytes());
                //lookup
                System.out.println("Testing lookup...");
                o = testTree.lookup(testKey);
                if (o == null) {
                    System.out.println("There in no key 1 in the btree");
                } else {
                    System.out.println("Value of key 1 is " + new String((byte[])o));
                }
                txn.commit();
            } catch (Exception e) {
                e.printStackTrace();
                txn.rollback();
            }

            txn = db.beginTransaction();
            try {
                //delete
                System.out.println("Testing delete...");
                testTree.delete(testKey);
                //lookup again
                System.out.println("Testing lookup...");
                o = testTree.lookup(testKey);
                if (o == null) {
                    System.out.println("There in no key 1 in the btree");
                } else {
                    System.out.println("Value of key 1 is " + new String((byte[])o));
                }

                System.out.println("Closing BTree...");
                testTree.close();

                txn.commit();
            } catch (ChaiDBException e) {
                e.printStackTrace();
                txn.rollback();               
            }
        } catch (ChaiDBException e) {
            e.printStackTrace();
        } finally{
            try {
                System.out.println("Closing database...");
                if(db!=null) db.close();
            } catch (ChaiDBException e) {
                e.printStackTrace();
            }
        }
        System.out.println("All tests completed.");
    }
}
TOP

Related Classes of org.chaidb.sample.TestChaiDB

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.