diff options
author | Jesse Morgan <jesse@jesterpm.net> | 2016-12-17 21:28:53 -0800 |
---|---|---|
committer | Jesse Morgan <jesse@jesterpm.net> | 2016-12-17 21:28:53 -0800 |
commit | 54df2afaa61c6a03cbb4a33c9b90fa572b6d07b8 (patch) | |
tree | 18147b92b969d25ffbe61935fb63035cac820dd0 /db-4.8.30/examples_java/src/persist/gettingStarted |
Berkeley DB 4.8 with rust build script for linux.
Diffstat (limited to 'db-4.8.30/examples_java/src/persist/gettingStarted')
4 files changed, 321 insertions, 0 deletions
diff --git a/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleDA.java b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleDA.java new file mode 100644 index 0000000..ab4c6e9 --- /dev/null +++ b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleDA.java @@ -0,0 +1,51 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2008-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +package persist.gettingStarted; + +import java.io.File; + +import com.sleepycat.db.DatabaseException; +import com.sleepycat.persist.EntityStore; +import com.sleepycat.persist.PrimaryIndex; +import com.sleepycat.persist.SecondaryIndex; +import com.sleepycat.persist.EntityCursor; + +public class SimpleDA { + // Open the indices + public SimpleDA(EntityStore store) + throws DatabaseException { + + // Primary key for SimpleEntityClass classes + pIdx = store.getPrimaryIndex( + String.class, SimpleEntityClass.class); + + // Secondary key for SimpleEntityClass classes + // Last field in the getSecondaryIndex() method must be + // the name of a class member; in this case, an + // SimpleEntityClass.class data member. + sIdx = store.getSecondaryIndex( + pIdx, String.class, "sKey"); + + sec_pcursor = pIdx.entities(); + sec_scursor = sIdx.subIndex("skeyone").entities(); + } + + public void close() + throws DatabaseException { + sec_pcursor.close(); + sec_scursor.close(); + } + + // Index Accessors + PrimaryIndex<String,SimpleEntityClass> pIdx; + SecondaryIndex<String,String,SimpleEntityClass> sIdx; + + EntityCursor<SimpleEntityClass> sec_pcursor; + EntityCursor<SimpleEntityClass> sec_scursor; +} diff --git a/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleEntityClass.java b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleEntityClass.java new file mode 100644 index 0000000..c4c0d81 --- /dev/null +++ b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleEntityClass.java @@ -0,0 +1,42 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2008-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +package persist.gettingStarted; + +import com.sleepycat.persist.model.Entity; +import com.sleepycat.persist.model.PrimaryKey; +import static com.sleepycat.persist.model.Relationship.*; +import com.sleepycat.persist.model.SecondaryKey; + +@Entity +public class SimpleEntityClass { + + // Primary key is pKey + @PrimaryKey + private String pKey; + + // Secondary key is the sKey + @SecondaryKey(relate=MANY_TO_ONE) + private String sKey; + + public void setpKey(String data) { + pKey = data; + } + + public void setsKey(String data) { + sKey = data; + } + + public String getpKey() { + return pKey; + } + + public String getsKey() { + return sKey; + } +} diff --git a/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleStoreGet.java b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleStoreGet.java new file mode 100644 index 0000000..44ac2dc --- /dev/null +++ b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleStoreGet.java @@ -0,0 +1,112 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2008-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +package persist.gettingStarted; + +import java.io.File; + +import com.sleepycat.db.DatabaseException; +import com.sleepycat.db.Environment; +import com.sleepycat.db.EnvironmentConfig; + +import com.sleepycat.persist.EntityStore; +import com.sleepycat.persist.StoreConfig; + +import java.io.FileNotFoundException; + +public class SimpleStoreGet { + + private static File envHome = new File("./JEDB"); + + private Environment envmnt; + private EntityStore store; + private SimpleDA sda; + + // The setup() method opens the environment and store + // for us. + public void setup() + throws DatabaseException { + + try { + EnvironmentConfig envConfig = new EnvironmentConfig(); + StoreConfig storeConfig = new StoreConfig(); + + // Open the environment and entity store + envmnt = new Environment(envHome, envConfig); + store = new EntityStore(envmnt, "EntityStore", storeConfig); + } catch (FileNotFoundException fnfe) { + System.err.println("setup(): " + fnfe.toString()); + System.exit(-1); + } + } + + public void shutdown() + throws DatabaseException { + + store.close(); + envmnt.close(); + } + + + private void run() + throws DatabaseException { + + setup(); + + // Open the data accessor. This is used to store + // persistent objects. + sda = new SimpleDA(store); + + // Instantiate and store some entity classes + SimpleEntityClass sec1 = sda.pIdx.get("keyone"); + SimpleEntityClass sec2 = sda.pIdx.get("keytwo"); + + SimpleEntityClass sec4 = sda.sIdx.get("skeythree"); + + System.out.println("sec1: " + sec1.getpKey()); + System.out.println("sec2: " + sec2.getpKey()); + System.out.println("sec4: " + sec4.getpKey()); + + System.out.println("############ Doing pcursor ##########"); + for (SimpleEntityClass seci : sda.sec_pcursor ) { + System.out.println("sec from pcursor : " + seci.getpKey() ); + } + + sda.pIdx.delete("keyone"); + System.out.println("############ Doing pcursor ##########"); + System.out.println("sec from pcursor : " + sda.sec_pcursor.first().getpKey()); + for (SimpleEntityClass seci : sda.sec_pcursor ) { + System.out.println("sec from pcursor : " + seci.getpKey() ); + } + + System.out.println("############ Doing scursor ##########"); + for (SimpleEntityClass seci : sda.sec_scursor ) { + System.out.println("sec from scursor : " + seci.getpKey() ); + } + + + + sda.close(); + shutdown(); + } + + public static void main(String args[]) { + SimpleStoreGet ssg = new SimpleStoreGet(); + try { + ssg.run(); + } catch (DatabaseException dbe) { + System.err.println("SimpleStoreGet: " + dbe.toString()); + dbe.printStackTrace(); + } catch (Exception e) { + System.out.println("Exception: " + e.toString()); + e.printStackTrace(); + } + System.out.println("All done."); + } + +} diff --git a/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleStorePut.java b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleStorePut.java new file mode 100644 index 0000000..0e48a5b --- /dev/null +++ b/db-4.8.30/examples_java/src/persist/gettingStarted/SimpleStorePut.java @@ -0,0 +1,116 @@ +/*- + * See the file LICENSE for redistribution information. + * + * Copyright (c) 2008-2009 Oracle. All rights reserved. + * + * $Id$ + */ + +package persist.gettingStarted; + +import java.io.File; + +import com.sleepycat.db.DatabaseException; +import com.sleepycat.db.Environment; +import com.sleepycat.db.EnvironmentConfig; + +import com.sleepycat.persist.EntityStore; +import com.sleepycat.persist.StoreConfig; + +import java.io.FileNotFoundException; + +public class SimpleStorePut { + + private static File envHome = new File("./JEDB"); + + private Environment envmnt; + private EntityStore store; + private SimpleDA sda; + + // The setup() method opens the environment and store + // for us. + public void setup() + throws DatabaseException { + + EnvironmentConfig envConfig = new EnvironmentConfig(); + StoreConfig storeConfig = new StoreConfig(); + + envConfig.setAllowCreate(true); + storeConfig.setAllowCreate(true); + + try { + // Open the environment and entity store + envmnt = new Environment(envHome, envConfig); + store = new EntityStore(envmnt, "EntityStore", storeConfig); + } catch (FileNotFoundException fnfe) { + System.err.println("setup(): " + fnfe.toString()); + System.exit(-1); + } + } + + // Close our environment and store. + public void shutdown() + throws DatabaseException { + + store.close(); + envmnt.close(); + } + + + private void run() + throws DatabaseException { + + setup(); + + // Open the data accessor. This is used to store + // persistent objects. + sda = new SimpleDA(store); + + // Instantiate and store some entity classes + SimpleEntityClass sec1 = new SimpleEntityClass(); + SimpleEntityClass sec2 = new SimpleEntityClass(); + SimpleEntityClass sec3 = new SimpleEntityClass(); + SimpleEntityClass sec4 = new SimpleEntityClass(); + SimpleEntityClass sec5 = new SimpleEntityClass(); + + sec1.setpKey("keyone"); + sec1.setsKey("skeyone"); + + sec2.setpKey("keytwo"); + sec2.setsKey("skeyone"); + + sec3.setpKey("keythree"); + sec3.setsKey("skeytwo"); + + sec4.setpKey("keyfour"); + sec4.setsKey("skeythree"); + + sec5.setpKey("keyfive"); + sec5.setsKey("skeyfour"); + + sda.pIdx.put(sec1); + sda.pIdx.put(sec2); + sda.pIdx.put(sec3); + sda.pIdx.put(sec4); + sda.pIdx.put(sec5); + + sda.close(); + + shutdown(); + } + + public static void main(String args[]) { + SimpleStorePut ssp = new SimpleStorePut(); + try { + ssp.run(); + } catch (DatabaseException dbe) { + System.err.println("SimpleStorePut: " + dbe.toString()); + dbe.printStackTrace(); + } catch (Exception e) { + System.out.println("Exception: " + e.toString()); + e.printStackTrace(); + } + System.out.println("All done."); + } + +} |