Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
BDB Java Sample Program
Backing enterprise and embedded Java applications with BDB
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
Berkeley DB
• A high-performance key-value database
– Designed for high-throughput applications requiring in-process, bullet-proof
management of mission-critical data
– Scale gracefully from managing a few bytes to terabytes of data
• Full ACID transaction support
– Concurrent transactional operations with multiple isolation levels
– Full transactional recovery support
• Cursor and secondary index support
– Fast and flexible data access
• Cross platform support
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
2
Berkeley DB for Java
• Why Java?
– Cross platform, write once run everywhere
– Scale gracefully from tiny embedded devices to clustered enterprise applications
• How?
– Base key-value API
– Direct persistence layer (DPL) API
– JDBC API
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
3
Base key-value API
• Lower level API
– Full control over persisted data format
– Resemble closely to the C API
– Work with Java 4+
– More verbose
• Handles
– Environment
– Database
– Cursor
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
4
Base key-value API - Example
• Opening the environment and database
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setInitializeCache(true);
envConfig.setInitializeLocking(true);
envConfig.setInitializeLogging(true);
envConfig.setTransactional(true);
Environment env = new Environment(“envHome”, envConfig);
DatabaseConfig dbConfig = new DatabaseConfig();
dbConfig.setTransactional(true);
dbConfig.setType(DatabaseType.BTREE);
Database db = env.openDatabase(null, “myDatabase.db”, null, dbConfig);
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
5
Base key-value API – Example cont.
• Writing and getting values with binding
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
TupleBinding<Long> keyBinding = TupleBinding.getPrimitiveBinding(Long.class);
TupleBinding<String> valueBinding = TupleBinding.getPrimitiveBinding(String.class);
keyBinding.objectToEntry(1L, key);
valueBinding.objectToEntry(“value”, value);
db.put(null, key, value);
// get the same value back
DatabaseEntry dbValue = new DatabaseEntry();
db.get(null, key, dbValue, LockMode.DEFAULT);
String strValue = valueBinding.entryToObject(dbValue);
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
6
Base key-value API – Example cont.
• Using cursors
DatabaseEntry key = new DatabaseEntry();
DatabaseEntry value = new DatabaseEntry();
TupleBinding<Long> keyBinding = TupleBinding.getPrimitiveBinding(Long.class);
TupleBinding<String> valueBinding = TupleBinding.getPrimitiveBinding(String.class);
keyBinding.objectToEntry(1L, key);
Cursor cur = db.openCursor(null, null);
OperationStatus status = cur.getSearchKey(key, value, LockMode.DEFAULT);
if (status == OperationStatus.SUCCESS)
String strValue = valueBinding.entryToObject(dbValue);
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
7
Direct persistence layer API
• Higher level API
– Work with objects instead of key-value pairs
– Use annotation, less cluttered code
– Work better with relatively static schema
– Require Java 5+
• Core classes
– EntityStore
– PrimaryIndex/SecondaryIndex
– EntityCursor
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
8
Direct persistence layer API - Example
• Opening the environment and entity store
EnvironmentConfig envConfig = new EnvironmentConfig();
envConfig.setInitializeCache(true);
envConfig.setInitializeLocking(true);
envConfig.setInitializeLogging(true);
envConfig.setTransactional(true);
Environment env = new Environment(“envHome”, envConfig);
StoreConfig storeConfig = new StoreConfig();
storeConfig.setAllowCreate(true).setTransactional(true);
EntityStore store = new EntityStore(env, name, storeConfig);
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
9
Direct persistence layer API – Example cont.
• Annotate entity classes
@Entity
public class Ticket {
@PrimaryKey
private Long ticketId;
private String meterId;
public Ticket() {}
public Ticket(Long id, String mId) { ticketId = id; meterId = mId; }
public Long getTicketId() { return ticketId; }
public String getMeterId() { return meterId; }
}
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
10
Direct persistence layer API – Example cont.
• Writing and getting objects
PrimaryIndex<Long, Ticket> index = store.getPrimaryIndex(Long.class, Ticket.class);
index.put(new Ticket(1L, “myTicket”));
Ticket ticket = index.get(1L);
• Using cursors
EntityCursor<Ticket> cursor = index.entities();
for (Ticket t : cursor)
String meterId = t.getMeterId();
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
11
JDBC API
• Use SQLite dialect
• Support JDBC 4
• Work with Java 4 - Java 7
• JDBC URL
jdbc:sqlite:/<database file name>
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
12
The sample program
• The story
– Simulates a parking lot with one parking meter
• OLTP & OLAP
– Ticket transactions (CRUD) follow the OLTP paradigm
– Operational analysis (BI/Data mining) follow the OLAP paradigm
• Cross platform/IDE support
– Linux / Unix / Windows
– IntelliJ / Eclipse / JDeveloper
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
13
The sample program – cont.
• Support all three APIs
– Base key-value API
– DPL API
– JDBC API
• Cover many features
– Transaction
– Cursor
– Primary and secondary index
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
14
The sample program – Data model
TICKET (
TICKET_ID
METER_ID
ISSUE_TIME
TICKET_LOG(
LOG_TIME
METER_ID
TICKET_ID
ACTION
CHARGE_FEE
INTEGER PRIMARY KEY,
TEXT,
INTEGER)
INTEGER PRIMARY KEY,
TEXT,
INTEGER,
TEXT,
INTEGER)
SEQUENCE “TICKET_ID_SEQ”
INDEX METER_IDX ON TICKET_LOG(METER_ID, LOG_TIME)
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
15
The sample program – Data access layer
• DbManager
– Manages an environment or a JDBC connection
– Manages transactions
– Creates DAOs
• TicketDAO
– CRUD operations on Tickets
• TicketLogDAO
– Append TicketLogs and query TicketLogs given a meterId and a period
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
16
The sample program – Application components
• Meter
– Represent a parking meter
– Create Tickets and compute parking fees
• Reporting
– Represent a BI reporting module
– Create reports using TicketLog queries
• Driver
– A demo driver program
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
17
The sample program - Run
• General steps:
– Build the following Berkeley DB components on your platform
•
•
•
•
Core
SQL API
Java API
JDBC API
– Import the sample program into your IDE
– Configure the project’s build path to include the Java and JDBC jars
– Configure ‘java.library.path’ to point to native Berkeley DB libraries in your run
configuration
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
18
Resources
• We need to figure out where to put the following and add that here
– Code
– Word & PPT
– 5 videos
• Main (PPT + code walk through)
• BDB build
• 3 IDEs
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Berkeley DB
19