public BDBCreator(String tableName, ArrayList<Integer> keyPositions, int secondaryIndex) { /* Creates and loads a BDB table with a secondary index on the column defined by*/ this.tableName = tableName.toLowerCase(); this.keyPositions = keyPositions; this.secIndexPos = secondaryIndex; this.tMap = Main.indexTypeMaps.get(this.tableName); setEnvironment(); dbConfig = new DatabaseConfig(); dbConfig.setAllowCreate(true); myDB = myDbEnvironment.openDatabase(null, this.tableName, dbConfig); SecondaryConfig secConfig = new SecondaryConfig(); secConfig.setAllowCreate(true); secConfig.setSortedDuplicates(true); createSecDB(secConfig); setConfig(dbConfig, secConfig); loadData(); if (secDB != null) { secDB.close(); } if (myDB != null) { myDB.close(); } closeEnvironment(); }
private void createSecDB(SecondaryConfig secConfig) { TupleBinding<DataRow> tBinder = new GenericTupleBinder(tMap); SecondaryKeyCreator secKeyCreator = new GenericSecKeyCreator(tBinder, secIndexPos, tMap); // Get a secondary object and set the key creator on it. secConfig.setKeyCreator(secKeyCreator); String secDbName = tableName + "-secDB"; secDB = myDbEnvironment.openSecondaryDatabase(null, secDbName, myDB, secConfig); }
public static void main(String[] args) { Environment myEnv; Database myDatabase; try { EnvironmentConfig envConfig = new EnvironmentConfig(); envConfig.setAllowCreate(true); myEnv = new Environment(new File("/Users/broso/testDB"), envConfig); // Environment open omitted for clarity DatabaseConfig myDbConfig = new DatabaseConfig(); SecondaryConfig mySecConfig = new SecondaryConfig(); myDbConfig.setAllowCreate(true); mySecConfig.setAllowCreate(true); // Duplicates are frequently required for secondary databases. mySecConfig.setSortedDuplicates(true); // Open the primary String dbName = "myPrimaryDatabase"; Database myDb = myEnv.openDatabase(null, dbName, myDbConfig); // Open the secondary. // Key creators are described in the next section. // AKeyCreator akc = new AKeyCreator(); // Get a secondary object and set the key creator on it. // mySecConfig.setKeyCreator(keyCreator); // Perform the actual open String secDbName = "mySecondaryDatabase"; SecondaryDatabase mySecDb = myEnv.openSecondaryDatabase(null, secDbName, myDb, mySecConfig); } catch (DatabaseException de) { // Exception handling goes here } }
@Test public void testConfigMatching() throws Throwable { try { /* DatabaseConfig matching. */ DatabaseConfig confA = new DatabaseConfig(); DatabaseConfig confB = new DatabaseConfig(); try { confA.validate(confB); } catch (Exception E) { fail("expected valid match"); } try { confB.validate(confA); } catch (Exception E) { fail("expected valid match"); } try { confA.validate(null); // uses the DEFAULT config } catch (Exception E) { fail("expected valid match"); } confA.setReadOnly(true); try { confA.validate(confB); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confA.setReadOnly(false); confA.setSortedDuplicates(true); try { confA.validate(confB); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confA.setSortedDuplicates(false); confA.setOverrideBtreeComparator(true); confA.setBtreeComparator(TestComparator.class); confB.setOverrideBtreeComparator(true); confB.setBtreeComparator(TestComparator2.class); try { confA.validate(confB); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confA.setBtreeComparator((Class) null); confA.setOverrideBtreeComparator(false); confB.setBtreeComparator((Class) null); confB.setOverrideBtreeComparator(false); confA.setOverrideDuplicateComparator(true); confA.setDuplicateComparator(TestComparator.class); confB.setOverrideDuplicateComparator(true); confB.setDuplicateComparator(TestComparator2.class); try { confA.validate(confB); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } /* Same tests as above but for serialized comparators. */ confA.setOverrideBtreeComparator(true); confA.setBtreeComparator(new TestSerialComparator()); confB.setOverrideBtreeComparator(true); confB.setBtreeComparator(new TestSerialComparator2()); try { confA.validate(confB); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confA.setBtreeComparator((Comparator) null); confA.setOverrideBtreeComparator(false); confB.setBtreeComparator((Comparator) null); confB.setOverrideBtreeComparator(false); confA.setOverrideDuplicateComparator(true); confA.setDuplicateComparator(new TestSerialComparator()); confB.setOverrideDuplicateComparator(true); confB.setDuplicateComparator(new TestSerialComparator2()); try { confA.validate(confB); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } /* SecondaryConfig matching. */ SecondaryConfig confC = new SecondaryConfig(); SecondaryConfig confD = new SecondaryConfig(); confC.setKeyCreator(new SecKeyCreator1()); confD.setKeyCreator(new SecKeyCreator1()); try { confC.validate(confD); } catch (Exception E) { E.printStackTrace(); fail("expected valid match"); } try { confD.validate(confC); } catch (Exception E) { fail("expected valid match"); } try { confC.validate(null); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confD.setKeyCreator(new SecKeyCreator2()); try { confC.validate(confD); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confD.setKeyCreator(new SecKeyCreator1()); confD.setMultiKeyCreator(new SecMultiKeyCreator1()); try { confC.validate(confD); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confD.setMultiKeyCreator(null); confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.NULLIFY); try { confC.validate(confD); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confC.setForeignKeyDeleteAction(ForeignKeyDeleteAction.ABORT); confC.setForeignKeyNullifier(new ForeignKeyNullifier1()); try { confC.validate(confD); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confC.setForeignKeyNullifier(null); confC.setForeignMultiKeyNullifier(new ForeignMultiKeyNullifier1()); try { confC.validate(confD); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confC.setForeignMultiKeyNullifier(null); confC.setImmutableSecondaryKey(true); try { confC.validate(confD); fail("expected exception"); } catch (IllegalArgumentException E) { // ok } confC.setImmutableSecondaryKey(false); } catch (Throwable t) { t.printStackTrace(); throw t; } }
private void setConfig(DatabaseConfig mydbConfig, SecondaryConfig mySecDBConfig) { mydbConfig.setTransactional(false); mySecDBConfig.setTransactional(false); }