Getting started with the BangDB for Java on Windows

Download

Please download the BangDB for Java for windows file and un-compress it using 7zip. If you don't have 7zip then you can get it from 7zip-org. Please ensure that you pick the right binary for 32/64 bit as you may need

The downloaded files are (once uncompressed)

  • bangdb-win-[32/64]-java-1.5.2.7z - downloaded from above
    • jar - folder, Contains bangdb.jar
    • native - various dlls and libs for bangdb native and jni
    • java-src - folder, contains bangdb class file for jni wrapper, scripts to create headers and build jar file.
    • Test-BangDB-Java - The test files for BangDB ready to run
    • bangdb.config - The configuration file for BangDB
    • README

Install

Store the un-compressed files and folder wherever you like. You can also create path variable to locate these files but it's not required. There is no separate install file for the binary. But ensure that you have JAVA SDK available on your machine and JAVA_PATH set properly. See more information on how to set JAVA_PATH on windows

The donload contains the bangdb.jar and native dlls to build and run java apps for BangDB

Running sample test file

To set run the test files for BangDB, go to the Test-BangDB-Java folder, and run the make-app.bat file to make the project and run the exapp.bat to run the test files. If every thing goes fine you have the test app running smoothly.

However, due to mismatch of JAVA/JNI version etc, you may see errors and in that case we will have to build the JNI wrapper dll on the machine and then build the app. The source code for the java wrapper (for jni) is provided in the java-src\bangdbjava\ folder. Below are the steps to build it using the VS solution provided in the bangdbjava dir

  • Go to the java-src\bangdbjava\ dir and open the VS solution. If you are unable to open it then you will need to start from scratch, please see next section here to do that
  • If you are able to open the project then edit the project properties and add the jni folder into the include dir [project->properties->configuration properties->C/C++->General->Additional include dir] The default loc set here is c:\Program Files\jdk1.7.0_21\include and c:\Program Files\jdk1.7.0_21\include\win32
  • Now build the project and it should work now. This is enough for almost all scenarios

However, if you were unable to build the bangdbjava.dll from the above project solution file provided in the java-src\bangdbjava then you probably want to start from scratch on your machine and build the bangdbjava.dll on your own. Here are the steps to do that;

  • Create a c++ project (dll) using the visual studio, Name it bangdbjava
  • copy and paste all the headers and soruce files available in the java-src in your project dir
  • Add these file into your project (right click->add existing) headers and soruce
  • Set the build configuration (32/64 bit)
  • "Character Set = not set" (project->properties->configuration properties->Genereal)
  • Additional include dir = java-src dir (project->properties->configuration properties->C/C++)
  • Similar as above, add JDK include and jdk include\win32 dir [for ex; on my system it's c:\program files\jdk1.7.0_21\include and c:\program files\jdk1.7.0_21\include\win32]
  • Precompile header = Not using PCH (project->properties->Configuration Properties->C/C++->precompiled Headers)
  • input = bangdbwin.dll (project->properties->configuration properties->Linker->input->Additional Dependencies)
  • Ensure that you have copied and pasted the bangdbwin.dll in the project dir

Build the project.

This should give the bangdbjava.dll. lib and exp files. Copy these files into the native folder (of the downloaded stuff)

Once bangdbjava.dll is built, you can simply go to the Test-bangDB-Java folder, run make-app.bat and then exapp.bat to run the test cases

		> cd Test-BangDB-Java/testbangdb
		
		> make-app.bat
		
		> exapp.bat
		
		# or with command lines args, put and get 500000 using 4 threads
		> exapp.bat 500000 4
	

To run the test file, simply the make-app.bat file to compile the files and then simply run the exeapp.bat

You can run the test app now as follows to get and put 100,000 items using 4 threads;

exeapp.bat 10000 4

Sample Code

Below is sample code to do some simple operations using BangDB. The bangdb.config file defines many configurable parameters, but most importantly edit the following values based on your liking. You can leave it as it is for now to run the test files

  • SERVER_DIR - This is basically the location where the db will keep its files
  • KEY_SIZE - This is to denote the maximum length of key that you will be using to store values. The min value is 8 bytes, and maximum is dependent upon the PAGE_SIZE defined. however keep it small for better performance
  • BUFF_POOL_SIZE_HINT - The size of the buffer pool

Please look at the document which describes meaning of various parameters in the config file at resources or go to the configuration section for detailed information and explanation

To compile and run, you will need to include the bangdb.jar and bangdbjava.dll. For example, if you code is under the package testbangdb, and you have kept bangdb.jar and bangdbjava.dll in the root folder then you can run following to compile stuff from the root dir


	javac -cp "bangdb.jar" testbangdb/*.java
	# and to run the app, use following
	java -cp .;bangdb.jar -Djava.library.path="." testbangdb.mainclass [command arguments]
	# change the mainclass with name of your class which contains main(), and provide command line arguments as required
		

	/* create db, table and get connection */
	System.loadLibrary("bangdbjava");
	Database db = new DatabaseImpl("mydb", null, TRANSACTIONTYPE .DB_MULTIOPS_TRANSACTION_NONE, dbPath, null);
	Table tbl = db.getTable("mytbl", DBAccess.OPENCREATE, null);
	if(tbl == null)
	{
		System.out.println("table null error");
		return;
	}
	Connection conn = tbl.getConnection();
	if(conn == null)
	{
		System.out.println("connection null error");
		return;
	}
	
	bool success = true;
	
	/* insert a value and get it */
	String k = "my key\0sjsj";
	String v = "The test value";
	byte[] key = k.getBytes();
	byte[] val = v.getBytes();

	if(conn.put(key, val, InsertOptions.INSERT_UNIQUE) < 0)
	{
		success = false;
		System.out.println("Put failed");
	}
		
	byte[] retval = conn.get(key);
	if(retval == null)
		System.out.println("Get Failed.");
	else
	{
		if(!Arrays.equals(retval, val))
	    	System.out.println("Get Mismatch.");
	}
	
	/* update */ 
	v = "This is the updated value";
	val = v.getBytes();

	if(conn.put(key, val, InsertOptions.UPDATE_EXISTING) < 0)
	{
		success = false;
		System.out.println("Update failed");
	}
		
	retval = conn.get(key);
	if(retval == null)
		System.out.println("Get Failed.");
	else
	{
		if(!Arrays.equals(retval, val))
	    	System.out.println("Get Mismatch.");
	}
	
	/* delete */
	if(conn.del(key) < 0)
	{
		success = false;
    	System.out.println("delete failed");
	}
		
	if(conn.get(key) != null)
	{
		success = false;
		System.out.println("got deleted value, failed");
	}
	
	/* for string types */
	k = "str key_1";
	v = "This is value 1 for key 1";
		
	if(conn.put(k, v, InsertOptions.INSERT_UPDATE) < 0)
	{
		success = false;
		System.out.println("str put, failed");
	}
		
	String outval;
	if((outval = conn.get(k)) == null)
	{
		success = false;
		System.out.println("str get, failed");
	}
	else if(outval.compareTo(v) != 0)
	{
		success = false;
	   	System.out.println("str mismatch get, failed");
	}
	
	db.closeDatabase(DBClose.DEFAULT);
	if(success)
		System.out.println("Test Passed");
	else
		System.out.println("Test Failed");	
		
	return;

	

Later in the section of developing using BangDB, more and advanced details are covered