Getting started with JE in Berkeley DB

Getting started with JE in Berkeley DB

brief introduction

    Berkeley DB Java Edition (JE) is written entirely in JAVA, which is suitable for managing massive file databases. Je has the following advantages:
    1. It is the hardware system, not the JE itself, that restricts the JE database to process 1 to 1 million records efficiently.
    2. Multithreading support, JE uses timeout to deal with the trivial problems between threads.
    3. All databases take the form of simple key/value correspondence.
    4. Support things.
    5. Allows creation of secondary libraries. In this way, we can use the first level key and the second level key to access our data conveniently.
    6. Supports RAM buffering, which reduces frequent IO operations.
    7. Support logs.
    8. Data backup and recovery.
    9. Cursor support. ,

Get JE installation environment

    JE download address: http://download.oracle.com/otn/berkeley-db/je-7.5.11.zip
    After unpacking the package, add the jar file in JE_HOME/lib/je-.jar to your environment variable to use JE.
    For help documents, please refer to JE home / docs / index.html
    See JE_HOME/src / *. For relevant source code*

Code

JE Operation class:
    package je_test;
    import java.io.File;
    import java.io.UnsupportedEncodingException;
    import java.util.ArrayList;

    import com.sleepycat.je.Cursor;
    import com.sleepycat.je.CursorConfig;
    import com.sleepycat.je.Database;
    import com.sleepycat.je.DatabaseConfig;
    import com.sleepycat.je.DatabaseEntry;
    import com.sleepycat.je.Environment;
    import com.sleepycat.je.EnvironmentConfig;
    import com.sleepycat.je.LockConflictException;
    import com.sleepycat.je.LockMode;
    import com.sleepycat.je.OperationStatus;
    import com.sleepycat.je.Transaction;
    import com.sleepycat.je.TransactionConfig;

    public class BerkeleyDBUtil {

        // Database environment
        private Environment env = null;

        // data base
        private static Database frontierDatabase = null;

        // Database name
        private static String dbName = "frontier_database";

        public BerkeleyDBUtil(String homeDirectory) {

            // 1. Create EnvironmentConfig
            EnvironmentConfig envConfig = new EnvironmentConfig();
            envConfig.setTransactional(true);
            envConfig.setAllowCreate(true);

            // 2, Using EnvironmentConfig to configure the Environment
            env = new Environment(new File(homeDirectory), envConfig);

            // 3. Create DatabaseConfig
            DatabaseConfig dbConfig = new DatabaseConfig();
            dbConfig.setTransactional(true);
    dbConfig.setAllowCreate(true);

            // 4. Open Database with Environment and DatabaseConfig
            frontierDatabase = env.openDatabase(null, dbName, dbConfig);

        }

        /** Write records to the database and determine whether there can be duplicate data. Pass in key and value
            * If there can be duplicate data, use put() directly. If there can't be duplicate data, use putNoOverwrite().
        */
        public boolean writeToDatabase(String key, String value, boolean isOverwrite) {
            try {
                // Set key/value. Note that the bytes array is used in DatabaseEntry
                DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
                DatabaseEntry theData = new DatabaseEntry(value.getBytes("UTF-8"));
                OperationStatus status = null;
                Transaction txn = null;
                try {
                    // 1. Transaction configuration
                    TransactionConfig txConfig = new TransactionConfig();
                    txConfig.setSerializableIsolation(true);
                    txn = env.beginTransaction(null, txConfig);
                    // 2. Write data
                    if (isOverwrite) {
                        status = frontierDatabase.put(txn, theKey, theData);
                    } else {
                        status = frontierDatabase.putNoOverwrite(txn, theKey, theData);
                    }
                    txn.commit();
                    if (status == OperationStatus.SUCCESS) {
                        System.out.println("To database" + dbName + "Write in:" + key + "," + value);
                        return true;
                    } else if (status == OperationStatus.KEYEXIST) {
                        System.out.println("To database" + dbName + "Write in:" + key + "," + value + "fail,The value already exists");
                        return false;
                    } else {
                        System.out.println("To database" + dbName + "Write in:" + key + "," + value + "fail");
                        return false;
                    }
                } catch (LockConflictException lockConflict) {
                    txn.abort();
                    System.out.println("To database" + dbName + "Write in:" + key + "," + value + "Appear lock abnormal");
                    return false;
                }
            } catch (Exception e) {
                // error handling
                System.out.println("To database" + dbName + "Write in:" + key + "," + value + "An error occurred");
                return false;
            }
        }

        /*
         * Read data from the database, pass in key, and return value
         */
        public String readFromDatabase(String key) {
            try {
                DatabaseEntry theKey = new DatabaseEntry(key.getBytes("UTF-8"));
                DatabaseEntry theData = new DatabaseEntry();
                Transaction txn = null;
                try {
                    // 1. Configure Transaction related information
                    TransactionConfig txConfig = new TransactionConfig();
                    txConfig.setSerializableIsolation(true);
                    txn = env.beginTransaction(null, txConfig);
                    // 2. Read data
                    OperationStatus status = frontierDatabase.get(txn, theKey, theData, LockMode.DEFAULT);
                    txn.commit();
                    if (status == OperationStatus.SUCCESS) {
                        // 3. Convert byte to String
                        byte[] retData = theData.getData();
                        String value = new String(retData, "UTF-8");
                        System.out.println("From database" + dbName + "Read in:" + key + "," + value);
                        return value;
                    } else {
                        System.out.println("No record found for key '" + key + "'.");
                        return "";
                    }
                } catch (LockConflictException lockConflict) {
                    txn.abort();
                    System.out.println("From database" + dbName + "Read in:" + key + "Appear lock abnormal");
                    return "";
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return "";
            }
        }

        /*
         * Traverse all records in the database and return list
         */
        public ArrayList<String> getEveryItem() {
            // TODO Auto-generated method stub
            System.out.println("===========Traverse database" + dbName + "All data in==========");
            Cursor myCursor = null;
            ArrayList<String> resultList = new ArrayList<String>();
            Transaction txn = null;
            try {
                txn = this.env.beginTransaction(null, null);
                CursorConfig cc = new CursorConfig();
                cc.setReadCommitted(true);
                if (myCursor == null)
                myCursor = frontierDatabase.openCursor(txn, cc);
                DatabaseEntry foundKey = new DatabaseEntry();
                DatabaseEntry foundData = new DatabaseEntry();
                // Use cursor.getPrev method to traverse cursor to get data
                if (myCursor.getFirst(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                    String theKey = new String(foundKey.getData(), "UTF-8");
                    String theData = new String(foundData.getData(), "UTF-8");
                    resultList.add(theKey);
                    System.out.println("Key | Data : " + theKey + " | " + theData + "");
                    while (myCursor.getNext(foundKey, foundData, LockMode.DEFAULT) == OperationStatus.SUCCESS) {
                        theKey = new String(foundKey.getData(), "UTF-8");
                        theData = new String(foundData.getData(), "UTF-8");
                        resultList.add(theKey);
                        System.out.println("Key | Data : " + theKey + " | " + theData + "");
                    }
                }
                myCursor.close();
                txn.commit();
                return resultList;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return null;
            } catch (Exception e) {
                System.out.println("getEveryItem Exception in processing");
                txn.abort();
                if (myCursor != null) {
                    myCursor.close();
                }
                return null;
            }
        }

        /*
         * Delete a record in the database according to the key value
         */
        public boolean deleteFromDatabase(String key) {
            boolean success = false;
            long sleepMillis = 0;
            for (int i = 0; i < 3; i++) {
                if (sleepMillis != 0) {
                try {
                    Thread.sleep(sleepMillis);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                sleepMillis = 0;
            }
            Transaction txn = null;
            try {
                // 1. Use cursor.getPrev method to traverse cursor to get data
                TransactionConfig txConfig = new TransactionConfig();
                txConfig.setSerializableIsolation(true);
                txn = env.beginTransaction(null, txConfig);
                DatabaseEntry theKey;
                theKey = new DatabaseEntry(key.getBytes("UTF-8"));

                // 2. Delete data and submit
                OperationStatus res = frontierDatabase.delete(txn, theKey);
                txn.commit();
                if (res == OperationStatus.SUCCESS) {
                    System.out.println("From database" + dbName + "Delete from:" + key);
                    success = true;
                    return success;
                } else if (res == OperationStatus.KEYEMPTY) {
                    System.out.println("Not from database" + dbName + "Found in:" + key + ". Cannot delete");
                } else {
                    System.out.println("Delete operation failed due to" + res.toString());
                }
                return false;
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                return false;
            } catch (LockConflictException lockConflict) {
                System.out.println("Delete operation failed, appears lockConflict abnormal");
                sleepMillis = 1000;
                continue;
            } finally {
                if (!success) {
                    if (txn != null) {
                        txn.abort();
                    }
                }
            }
            return false;
        }

        public void closeDB() {
            if (frontierDatabase != null) {
                frontierDatabase.close();
            }
            if (env != null) {
                env.close();
            }
        }
    }

JE Test class:
    package je_test;

    public class BerkeleyDBUtilTest {  

        private BerkeleyDBUtil dbUtil = new BerkeleyDBUtil("/Users/liuzhixiong/Desktop/envhome/");  

        public static void main(String[] args) {
            new BerkeleyDBUtilTest().testWriteToDatabase();
            String key = "2";
            String value = new BerkeleyDBUtilTest().testReadFromDatabase(key);
            System.out.println("value=" + value);
        }

        public void testWriteToDatabase() {  
            for (int i = 0; i < 10; i++){  
                dbUtil.writeToDatabase(i+"", "Student"+i, true);  
            }  
        }  

        public String testReadFromDatabase(String key) {  
            String value = dbUtil.readFromDatabase(key);
            return value;
        }  

        public int testGetEveryItem() {  
            int size = dbUtil.getEveryItem().size();  
            return size;
        }  

        public void testDeleteFromDatabase() {  
            dbUtil.deleteFromDatabase("4");  
        }  

        public void cleanup() {  
            dbUtil.closeDB();  
        } 
    }  

Tags: Database Java Oracle

Posted on Tue, 05 May 2020 10:31:16 -0700 by depsipher