Upload
kerry-holmes
View
220
Download
0
Embed Size (px)
Citation preview
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
BDB Java Sample ProgramBacking enterprise and embedded Java applications with BDB
Berkeley DB
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 2
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 3
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 4
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Base key-value API - Example• Opening the environment and databaseEnvironmentConfig 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);
Berkeley DB 5
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Base key-value API – Example cont.• Writing and getting values with bindingDatabaseEntry 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 backDatabaseEntry dbValue = new DatabaseEntry();
db.get(null, key, dbValue, LockMode.DEFAULT); String strValue = valueBinding.entryToObject(dbValue);
Berkeley DB 6
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Base key-value API – Example cont.• Using cursorsDatabaseEntry 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);
Berkeley DB 7
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 8
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Direct persistence layer API - Example• Opening the environment and entity storeEnvironmentConfig 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);
Berkeley DB 9
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Direct persistence layer API – Example cont.• Annotate entity classes@Entitypublic 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; } }
Berkeley DB 10
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
Direct persistence layer API – Example cont.• Writing and getting objectsPrimaryIndex<Long, Ticket> index = store.getPrimaryIndex(Long.class, Ticket.class);
index.put(new Ticket(1L, “myTicket”));
Ticket ticket = index.get(1L);
• Using cursorsEntityCursor<Ticket> cursor = index.entities();
for (Ticket t : cursor) String meterId = t.getMeterId();
Berkeley DB 11
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
JDBC API• Use SQLite dialect• Support JDBC 4• Work with Java 4 - Java 7• JDBC URL jdbc:sqlite:/<database file name>
Berkeley DB 12
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 13
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 14
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
The sample program – Data model
TICKET ( TICKET_ID INTEGER PRIMARY KEY, METER_ID TEXT, ISSUE_TIME INTEGER)TICKET_LOG( LOG_TIME INTEGER PRIMARY KEY, METER_ID TEXT, TICKET_ID INTEGER, ACTION TEXT, CHARGE_FEE INTEGER)
SEQUENCE “TICKET_ID_SEQ”INDEX METER_IDX ON TICKET_LOG(METER_ID, LOG_TIME)
Berkeley DB 15
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 16
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 17
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 18
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. |
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
Berkeley DB 19