Oracle Berkeley DB
Total Page:16
File Type:pdf, Size:1020Kb
#68 CONTENTS INCLUDE: n Oracle Berkeley DB Family n Berkeley DB Java Edition Features Getting Started with n BDB JE Base API and Collections API n BDB JE Direct Persistent Layer n BDB JE Transaction Support and Performance Tuning Oracle Berkeley DB n BDB JE Backup and Recovery Visit refcardz.com n Hot Tips and more... By Masoud Kalali entirely different set of features and characteristics (See Table ABOUT ORACLE BERKELEY DB 4). The base feature sets are shown in Table 1. The Oracle Berkeley DB (BDB) family consists of three open Table 1: Family Feature Sets source data persistence products which provide developers Feature Set Description with fast, reliable, high performance, enterprise ready local Data Store (DS) Single writer, multiple reader databases implemented in the ANSI C and Java programming Concurrent Data Store (CDS) Multiple writers, multiple snapshot readers languages. The BDB family typically stores key-value pairs but Transactional Data Store (TDS) Full ACID support on top of CDS is also flexible enough to store complex data models. BDB and High Availability (HA) Replication for fault tolerance. Fail over recovery support BDB Java Edition share the same base API, making it possible to easily switch between the two. Table 2 shows how these features are distributed between the We will review the most important aspects of Oracle BDB different BDB family members. family briefly. Then we will dig deep into Oracle BDB Java Table 2: Different Editions’ Feature Sets DS CDS TS HA Edition and see what its exclusive features are. We discuss BDB/BDB XML Edition Based API and Data Persistence Layer API. We will see how we can manage transactions DPL and Base API in addition BDB Java Edition to persisting complex objects graph using DPL will form the overall development subjects. Backup recovery, tuning, and AddiTIOnaL FEATURES data migration utilities to migrate data between different editions and installations forms the administrations issues The BDB family of products has several special features and which we will discuss in this Refcard. offers a range of unique benefits which are listed in Table 3. THE BDB FAMILY Table 3: Family Features and Benefits Feature Benefit Oracle BDB Core Edition Locking High concurrency Berkeley DB is written in ANSI C and can be used as a library Data stored in application-native format Performance, no translation required www.dzone.com Get More Refcardz! to access the persisted information from within the parent Programmatic API, no SQL Performance, flexibility/control application address space. Oracle BDB provides multiple In process, not client-server Performance, no IPC required interfaces for different programming languages including Zero administration Low cost of ownership ANSI C, the Java API through JNI in addition to Perl, PHP, and ACID transactions and recovery Reliability, data integrity Python. Dual License Open/Closed source distributions Oracle BDB XML Edition Built on top of the BDB, the BDB XML edition allows us to easily store and retrieve indexed XML documents and to use XQuery to access stored XML documents. It also supports Get More Refcardz accessing data through the same channels that BDB supports. (They’re free!) BDB Java Edition BDB Java Edition is a pure Java, high performance, and flexible n Authoritative content embeddable database for storing data in a key-value format. It n Designed for developers supports transactions, direct persistence of Java objects using n Written by top experts EJB 3.0-style annotations, and provides a low level key-value n Latest tools & technologies retrieval API as well as an “access as collection” API. n Hot tips & examples n Bonus content online KEY FEATURES n New issue every 1-2 weeks Berkeley DB Each of the BDB family members supports different feature sets. BDB XML edition enjoys a similar set of base features Subscribe Now for FREE! as the Core BDB. BDB Java edition on the other hand is Refcardz.com racle implemented in a completely different environment with an O DZone, Inc. | www.dzone.com 2 Oracle Berkeley DB In memory or on disk operation Transacted caching/ persisted data store db.get(null, keyValue, searchEntry, LockMode.DEFAULT);//retrieving record Similar data access API Easy switch between JE and BDB String foundData = new String(searchEntry.getData(), “UTF-8”); dataValue = new DatabaseEntry(“updated data content”. Just a set of library Easy to deploy and use getBytes(“UTF-8”)); Very large databases Virtually no limit on database size db.put(null, keyValue, dataValue);//updating an entry db.delete(null, keyValue);//delete operation db.close(); Features unique to BDB Java Edition are listed in Table 4. dbEnv.close(); Table 4: BDB Java Edition Exclusive Features There are multiple overrides for the Database.put method to Feature Benefit prevent duplicate records from being inserted and to prevent record overwrites. Fast, indexed, BTree Ultra fast data retrieval Java EE JTA and JCA support Integration with Java EE application servers DPL Sample Efficient Direct Persistence Layer EJB 3.0 like annotation to store Java Objects graph DPL sample consists of two parts, the entity class and the Easy Java Collections API Transactional manipulation of Base API through entity management class which handle CRUD over the entity enhanced Java Collections class. Low Level Base API Work with dynamic data schema Entity Class JMX Support Monitor able from within parent application @Entity public class Employee { These features, along with a common set of features, make the @PrimaryKey Java edition a potential candidate for use cases that require public String empID; public String lastname; caching, application data repositories, POJO persistence, @SecondaryKey(relate = Relationship.MANY_TO_MANY, relatedEntity = Project.class,onRelatedEntityDelete = queuing/buffering, Web services, SOA, and Integration. DeleteAction.NULLIFY) public Set<Long> projects; public Employee() { } INTRODUCinG BERKELEY DB JAVA EdiTION public Employee(String empID, String lastname, Set<Long> projects) { this.empID = empID; this.lastname = lastname; Installation this.projects = projects; You can download BDB JE from http//bit.ly/APfJ5. After } }} extracting the archive you’ll see several directories with self- describing names. The only file which is required to be in the This is a simple POJO with few annotations to mark it as an class path to compile and run the included code snippet is je- entity with a String primary key. For now ignore the 3.3.75.jar (the exact file name may vary) which is placed inside @Secondarykey annotation, we will discuss it later. the lib directory. Notice that BDB JE requires J2SE JDK version The data management Class 1.5.0_10 or later. EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); All editions of Berkeley DB are freely available for download Environment dbEnv = new Environment(new File(“/home/masoud/dben- and can be used in open source products which are dpl”), envConfig); StoreConfig stConf = new StoreConfig(); Hot not distributed to third parties. A commercial license is stConf.setAllowCreate(true); Tip necessary for using any of the BDB editions in a closed source and packaged product. For more information about EntityStore store = new EntityStore(dbEnv, “DPLSample”, stConf); licensing visit: http://bit.ly/17pMwZ PrimaryIndex<String, Employee> userIndex; userIndex = store.getPrimaryIndex(String.class, Employee.class); userIndex.putNoReturn(new Employee(“u180”, “Doe”, null));//insert Access APIs Employee user = userIndex.get(“u180”);//retrieve userIndex.putNoReturn(new Employee(“u180”, “Locke”, null));// BDB JE provides three APIs for accessing persisted data. The Update Base API provides a simple key-value model for storing and userIndex.delete(“u180”);//delete retrieving data. The Direct Persistence Layer (DPL) API lets store.close(); you persist any Java class with a default constructor into the dbEnv.close(); database and retrieve it using a rich set of data retrieval APIs. These two code snippets show the simplest from of performing And finally the Collections API which extends the well known CRUD operation without using transaction or complex object Java Collections API with data persistence and transaction relationships. support over data access. Sample code description Base API sample An Environment provides a unit of encapsulation for one or The Base API is the simplest way to access data. It stores a key more databases. Environments correspond to a directory on and a value which can be any serializable Java object. disk. The Environment is also used to manage and configure resources such as transactions. EnvironmentConfig is used to EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); configure the Environment, with options such as transaction Environment dbEnv = new Environment(new File(“/home/masoud/dben”), envConfig); configuration, locking, caching, getting different types of DatabaseConfig dbconf = new DatabaseConfig(); statistics including database, locks and transaction statistics, etc. dbconf.setAllowCreate(true); dbconf.setSortedDuplicates(false);//allow update DatabaseConfig Database db = dbEnv.openDatabase(null, “SampleDB “, dbconf); One level closer to our application is and DatabaseEntry searchEntry = new DatabaseEntry(); Database object when we use Base API. When we use DPL DatabaseEntry dataValue = new DatabaseEntry(“ data content”. getBytes(“UTF-8”)); these objects are replaced by StoreConfig and EntityStore. DatabaseEntry