Download presentation
Presentation is loading. Please wait.
Published byKerry Holmes Modified over 9 years ago
1
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | BDB Java Sample Program Backing enterprise and embedded Java applications with BDB Berkeley DB 1
2
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 DB2
3
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 DB3
4
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 DB4
5
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 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); Berkeley DB5
6
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Base key-value API – Example cont. Writing and getting values with binding DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); TupleBinding keyBinding = TupleBinding.getPrimitiveBinding(Long.class); TupleBinding 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); Berkeley DB6
7
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Base key-value API – Example cont. Using cursors DatabaseEntry key = new DatabaseEntry(); DatabaseEntry value = new DatabaseEntry(); TupleBinding keyBinding = TupleBinding.getPrimitiveBinding(Long.class); TupleBinding 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 DB7
8
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 DB8
9
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 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); Berkeley DB9
10
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | 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; } } Berkeley DB10
11
Copyright © 2014 Oracle and/or its affiliates. All rights reserved. | Direct persistence layer API – Example cont. Writing and getting objects PrimaryIndex index = store.getPrimaryIndex(Long.class, Ticket.class); index.put(new Ticket(1L, “myTicket”)); Ticket ticket = index.get(1L); Using cursors EntityCursor cursor = index.entities(); for (Ticket t : cursor) String meterId = t.getMeterId(); Berkeley DB11
12
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:/ Berkeley DB12
13
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 DB13
14
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 DB14
15
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 DB15
16
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 DB16
17
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 DB17
18
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 DB18
19
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 DB19
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.