2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Berkeley DB Java Edition のサンプルコード

Posted at

参考: http://www.gokhanatil.com/2011/09/oracle-berkeley-db-java-edition.html

import java.io.File;
import com.sleepycat.bind.tuple.IntegerBinding;
import com.sleepycat.bind.tuple.StringBinding;
import com.sleepycat.bind.tuple.TupleBinding;
import com.sleepycat.bind.tuple.TupleInput;
import com.sleepycat.bind.tuple.TupleOutput;
import com.sleepycat.je.Database;
import com.sleepycat.je.DatabaseConfig;
import com.sleepycat.je.DatabaseEntry;
import com.sleepycat.je.DatabaseException;
import com.sleepycat.je.Environment;
import com.sleepycat.je.EnvironmentConfig;
import com.sleepycat.je.OperationStatus;
import com.sleepycat.je.Sequence;
import com.sleepycat.je.SequenceConfig;

public class DB {
	public static void main(String[] args) {

		try {
			///////////////////////////
			// EnvironmentConfig の作成
			///////////////////////////
			
			EnvironmentConfig envConf = new EnvironmentConfig();
			// 存在しなければ作成
			envConf.setAllowCreate(true);
			// 第一引数のディレクトリは予め作成しておく 
			Environment dbEnv = new Environment(
					new File("/home/akisute/tmp/db"), envConf);

			
			////////////////////////////////////////////////
			// DatabaseConfig の作成、データベースの open
			////////////////////////////////////////////////

			DatabaseConfig dbConf = new DatabaseConfig();
			// 存在しなければ作成
			dbConf.setAllowCreate(true);
			Database testDB = dbEnv.openDatabase(null, "testDB", dbConf);
			
			
			///////////////////////////////////////////////////////////////////////
			// DatabaseEntry の作成
			//     DatabaseEntry に key と value になるオブジェクトをセットして、
			//     db.put(null, key, value) とすることで key-value を DB に登録できる
			///////////////////////////////////////////////////////////////////////

			DatabaseEntry key = new DatabaseEntry();
			DatabaseEntry data = new DatabaseEntry();
			
			IntegerBinding.intToEntry(1, key);
			StringBinding.stringToEntry("Value String", data);
			
			// これで {1: "Value String} が登録される
			testDB.put(null, key, data);
			

			//////////////////////////////////////////
			// Sequence の作成
			//     item の id に連番などを使いたい時は
			//     Sequence を使う
			//////////////////////////////////////////

			SequenceConfig seqConf = new SequenceConfig();
			seqConf.setAllowCreate(true);

			// Sequence の名前を決定
			DatabaseEntry entMySeq = new DatabaseEntry();
			StringBinding.stringToEntry("MY_SEQ", entMySeq);

			Sequence mySeq = testDB.openSequence(null, entMySeq, seqConf);

			// {1: "No.1", 2: "No.2", ... , 10: "No.10"} を作成 
			for (int i = 0; i < 10; i++) {
				// Sequence の get を呼び出すことで、
				// 次の番号を得られる
				IntegerBinding.intToEntry((int) mySeq.get(null, 1), key);
				StringBinding.stringToEntry("No." + i, data);
				testDB.put(null, key, data);
			}
			

			///////////////////////////
			// データベースの read
			///////////////////////////
			
			// {3: "No.3"} を呼び出す
			IntegerBinding.intToEntry(3, key);

			if ((testDB.get(null, key, data, null) == OperationStatus.SUCCESS)) {
				System.out.println("Key  :" + IntegerBinding.entryToInt(key));
				System.out.println("Data :" + StringBinding.entryToString(data));
			} else {
				System.out.println("Couldn't find");
			}
			
			
			////////////////////////////////////////////////////
			// TupleInput/TupleOutput の使用
			//     value として複数のオブジェクトを登録したい時は
			//     TupleInput/TupleOutput を使用する
			////////////////////////////////////////////////////
			
			// put //
			
			TupleOutput output = new TupleOutput();
			DatabaseEntry entry = new DatabaseEntry();
			
			output.writeString("name");
			output.writeString("email");
			output.writeString("phone number");
			
			IntegerBinding.intToEntry(11, key);
			TupleBinding.outputToEntry(output, entry);
			
			testDB.put(null, key, entry);
			
			// get //
			
			IntegerBinding.intToEntry(11, key);

			if ((testDB.get(null, key, entry, null) == OperationStatus.SUCCESS)) {
				TupleInput input = TupleBinding.entryToInput(entry);
				System.out.println("Tuple1 :" + input.readString());
				System.out.println("Tuple2 :" + input.readString());
				System.out.println("Tuple3 :" + input.readString());
			} else {
				System.out.println("Couldn't find");
			}

			
			///////////////////////////
			// データベースの close
			///////////////////////////
			mySeq.close();
			testDB.close();
			dbEnv.close();
			
		} catch (DatabaseException dbe) {
			System.out.println("Error :" + dbe.getMessage());
		}
	}
}
2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?