This sample demonstrates a minimal program that uses the BDB API of Berkeley DB.
See samples/db/index.html for information on how to compile and run the samples.
In this example a single data store is created in a transactional
environment using one form of the DataStore and DataEnvironment constructors.
This is a good starting point, but as you need more control over
the environment and data store you will probably want to create the Berkeley
DB Db
and DbEnv
objects yourself and pass these to
the DataStore and DataEnvironment constructors.
A tuple format and binding are used for the record keys because tuples are very compact and are sorted deterministically. Numbers are sorted in ascending numerical order and strings are sorted in byte value (UTF byte) order. If you need a tuple key with more than one field, for example a key containing two integers or an integer and a string, then you will have to define the binding yourself by extending the TupleBinding class. Multi-field tuples are sorted by the first field, then the second field, and so on.
A serial format and binding are used for the record values because serial bindings are very easy to use and they support arbitrary Java objects. If you are concerned about data size and your record values will always be strings or some other simple data format, or if you intend to read the database from programs implemented in languages other than Java, then you should use a tuple format instead of a serial format for the record values.