Pacific-Design.com

    
Home Index

1. Java

2. BerkeleyDB

Java / BerkeleyDB /

 package com.pacific-design.cache;
 
 import com.sleepycat.je.*;
 import org.junit.Test;
 import org.junit.Assert;
 import org.junit.runner.RunWith;
 import org.springframework.test.context.ContextConfiguration;
 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
 
 import java.security.MessageDigest;
 import java.security.*;
 import java.io.File;
 
 /*---------------------------------------------------------------------------------------*/
 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = "classpath:/testApplicationContext.xml")
 /*---------------------------------------------------------------------------------------*/
 public class BerkeleyDbTest {
 
     //BerkeleyDB database values
     Database DB = null;
     Environment ENV = null;
     String PATH = "/tmp/bdb";
 
     //Test specific values
     private static final int MAXIMUM_LOOPS = 2000000;
     private static final int DISPLAY = 250000;
     private static final boolean useSHA1 = false;
     private static int recordCounter = 0;
 
 
     /**
      * SHA1 Summary
      *
      * @param data
      * @return
      */
     private static String convertToHex(byte[] data) {
         StringBuffer buf = new StringBuffer();
         for (byte aData : data) {
             int halfbyte = (aData >>> 4) & 0x0F;
             int two_halfs = 0;
 
             do {
                 if ((0 <= halfbyte) && (halfbyte <= 9))
                     buf.append((char) ('0' + halfbyte));
                 else
                     buf.append((char) ('a' + (halfbyte - 10)));
 
                 halfbyte = aData & 0x0F;
 
             } while (two_halfs++ < 1);
         }
         return buf.toString();
     }
 
     /**
      * SHA1 Summary
      *
      * @param str
      * @return
      */
     public static String sha1sum(String str) {
         byte[] defaultBytes = str.getBytes();
 
         try {
             MessageDigest algorithm = MessageDigest.getInstance("MD5");
             algorithm.reset();
             algorithm.update(defaultBytes);
             byte messageDigest[] = algorithm.digest();
 
             StringBuffer hexString = new StringBuffer();
 
             for (byte aMessageDigest : messageDigest) {
                 hexString.append(Integer.toHexString(0xFF & aMessageDigest));
             }
 
             str = hexString + "";
 
         }
         catch (NoSuchAlgorithmException nsae) {
             System.out.println(nsae);
         }
 
         return str;
     }
 
     /**
      * Delete Directory for BerkeleyDB if exist
      *
      * @param path
      * @return
      */
     public boolean deleteDirectory(File path) {
         if (path.exists()) {
             File[] files = path.listFiles();
             for (File file : files) {
                 if (file.isDirectory()) {
                     deleteDirectory(file);
                 } else {
                     file.delete();
                 }
             }
         }
         return (path.delete());
     }
 
     /**
      * Create Directory for BerkeleyDB if does not exist
      *
      * @throws Exception 
      */
     public void CreateDirectory() throws Exception {
         try {
             if ((new File(PATH)).exists()) {
                 System.out.println("Directory: " + PATH + " already exist.");
 
             } else {
                 if ((new File(PATH)).mkdir()) {
                     System.out.println("Directory: " + PATH + " has been created.");
                 }
             }
         }
         catch (Exception e) {
             // Print out the exception that occurred
             System.out.println("Unable to create " + PATH + "  " + e.getMessage());
         }
     }
 
     /**
      * Add Records into BerkeleyDB
      *
      * @throws Exception
      */
     @Test
     public void testAddSomeRecords() throws Exception {
         // make sure we deleting all previous installation
         File dir = new File(PATH);
         deleteDirectory(dir);
 
         System.out.println("Creating database ...");
         CreateDirectory();
         long t1 = System.nanoTime();
 
         try {
             System.out.println("Open or create Berkeley DB environment.");
             EnvironmentConfig envConfig = new EnvironmentConfig();
             envConfig.setAllowCreate(true);
 
             System.out.println("Specifying cache size in bytes 0.5 GB = 536870912");
             envConfig.setCacheSize(536870912);
 
             System.out.println("Physical location of BerkeleyDB = " + PATH);
             ENV = new Environment(new File(PATH), envConfig);
 
             System.out.println("Open the database. Create it if it does not already exist.");
             DatabaseConfig dbConfig = new DatabaseConfig();
 
             dbConfig.setAllowCreate(true);
             dbConfig.setDeferredWrite(true);
 
             DB = ENV.openDatabase(null, "CacheData", dbConfig);
 
             //first pass insert
             for (recordCounter = 0; recordCounter < MAXIMUM_LOOPS; recordCounter++) {
 
                 String key = "Key" + recordCounter;
                 String val = "First" + recordCounter;
 
                 if (useSHA1) key = sha1sum(key);
 
                 DatabaseEntry entryKey = new DatabaseEntry(key.getBytes());
                 DatabaseEntry entryVal = new DatabaseEntry(val.getBytes());
 
                 DB.put(null, entryKey, entryVal);
 
                 if ((recordCounter % DISPLAY) == 0) {
                     System.out.println("First Pass = " + recordCounter);
                     System.out.println("Key = " + key);
                     System.out.println("First Value = " + val);
 
                     long t2 = System.nanoTime();
                     //System.out.println("Execution time: " + ((t2 - t1) * 1e-6) + " milliseconds");
                     System.out.println("Execution time: " + ((t2 - t1) * 1e-9) + " seconds");
                     System.out.println("Request per second: " + (recordCounter / ((t2 - t1) * 1e-9)));
                     System.out.println();
 
 
                 }
             }
 
             //second pass insert overwrite second 1/2 of the values for each key
             for (recordCounter = MAXIMUM_LOOPS / 2; recordCounter < MAXIMUM_LOOPS; recordCounter++) {
 
                 String key = "Key" + recordCounter;
                 String val = "Second" + recordCounter;
 
                 if (useSHA1) key = sha1sum(key);
 
                 DatabaseEntry entryKey = new DatabaseEntry(key.getBytes());
                 DatabaseEntry entryVal = new DatabaseEntry(val.getBytes());
 
                 DB.put(null, entryKey, entryVal);
 
                 if ((recordCounter % DISPLAY) == 0) {
                     System.out.println("Second Pass = " + recordCounter);
                     System.out.println("Key = " + key);
                     System.out.println("Second Value = " + val);
 
                     long t2 = System.nanoTime();
                     //System.out.println("Execution time: " + ((t2 - t1) * 1e-6) + " milliseconds");
                     System.out.println("Execution time: " + ((t2 - t1) * 1e-9) + " seconds");
                     System.out.println("Request per second: " + (recordCounter / ((t2 - t1) * 1e-9)));
                     System.out.println();
 
 
                 }
             }
 
 
             // Database.sync() is only supported for deferred-write databases
             System.out.println("Sync data with hard disk");
             long sync1 = System.nanoTime();
             DB.sync();
             long sync2 = System.nanoTime();
             System.out.println("Sync Time =" + ((sync2 - sync1) * 1e-9));
 
             System.out.println("---------------------------------------------------------------");
             System.out.println("Total Record = " + recordCounter);
 
 
             DB.close();
             ENV.close();
 
             long t3 = System.nanoTime();
             //System.out.println("Execution time: " + ((t3 - t1) * 1e-6) + " milliseconds");
             System.out.println(MAXIMUM_LOOPS + " Records Added in " + ((t3 - t1) * 1e-9) + " seconds");
             System.out.println("Requests per second: " + (recordCounter / ((t3 - t1) * 1e-9)));
             System.out.println("---------------------------------------------------------------");
 
         }
         catch (DatabaseException dbe) {
             System.out.println("Failed to create Berkeley DB ...");
             System.out.println(dbe.toString());
         }
     }
 
     /**
      * Get Berkeley DB Test Count
      *
      * @throws Exception
      */
     @Test
     public void testGetCount() throws Exception {
         CreateDirectory();
 
         //File dir = new File ("..");
         //String bdbdir = dir.getCanonicalPath() + "/" + PATH;
 
         System.out.println("Open or create Berkeley DB environment.");
         EnvironmentConfig envConfig = new EnvironmentConfig();
         envConfig.setAllowCreate(true);
 
         System.out.println("Specifying cache size in bytes 0.5 GB = 536870912");
         envConfig.setCacheSize(536870912);
 
         System.out.println("Specifying physical location of BerkeleyDB");
         ENV = new Environment(new File(PATH), envConfig);
 
         System.out.println("Open or Create database ...");
         DatabaseConfig dbConfig = new DatabaseConfig();
         dbConfig.setAllowCreate(true);
         DB = ENV.openDatabase(null, "CacheData", dbConfig);
 
         long counter = DB.count();
 
         System.out.println("---------------------------------------------------------------");
         System.out.println(" Total Records = " + counter);
         System.out.println("---------------------------------------------------------------");
         System.out.println("Close BerkeleyDB");
         DB.close();
         System.out.println("Close BerkeleyDB Environment");
         ENV.close();
 
     }
 
     /**
      * testGetRecord will take key as parameter search Berkeley DB
      * for the key and retrieves value corespomnding to the key.
      *
      * @throws Exception
      */
     public String Search(String keyTerm) throws Exception {
         try {
             EnvironmentConfig envConfig = new EnvironmentConfig();
             envConfig.setAllowCreate(true);
             envConfig.setCacheSize(536870912);
 
             ENV = new Environment(new File(PATH), envConfig);
 
             DatabaseConfig dbConfig = new DatabaseConfig();
             dbConfig.setAllowCreate(true);
             DB = ENV.openDatabase(null, "CacheData", dbConfig);
 
             String testKey2;
             if (useSHA1) testKey2 = sha1sum(keyTerm);
             else testKey2 = keyTerm;
 
             // Create DatabaseEntry objects for key and value
             //DatabaseEntry entryKey = new DatabaseEntry(keyTerm.getBytes());
             DatabaseEntry entryVal = new DatabaseEntry();
 
             //DB.get(null, entryKey, entryVal, null);
             try {
 
                 System.out.println("\n-------- Search Test Result for: " + keyTerm + " --------");
                 long start = System.nanoTime();
                 DB.get(null, new DatabaseEntry(testKey2.getBytes()), entryVal, null);
                 long end = System.nanoTime();
 
                 String result = new String(entryVal.getData());
 
                 System.out.println("key=" + keyTerm);
                 System.out.println("val=" + result);
 
                 return result;
 
 
             } catch (Exception e) {
                 System.out.println("Record " + keyTerm + " not found ...");
             }
 
 
         }
         catch (DatabaseException dbe) {
             System.out.println("Failed to open Berkeley DB for search ...");
             System.out.println(dbe.toString());
         }
 
         return null;
     }
 
     /**
      * Executing couple of searches against Berkeley DB with
      * first pass and second pass key
      *
      * @throws Exception
      */
     @Test
     public void testSearch() throws Exception {
 
         // first pass search test
         String result = Search("Key700000");
         Assert.assertEquals(result, "First700000");
 
         // second pass search test
         result = Search("Key1750000");
         Assert.assertEquals(result, "Second1750000");
     }
 }