Package com.sleepycat.je

Examples of com.sleepycat.je.SecondaryDatabase


    private void doJoin(int[][] dataSet, int[][] joinSet, int setNum)
        throws DatabaseException {

        String name = "Set#" + setNum;
        Database priDb = openPrimary("pri");
        SecondaryDatabase secDb0 = openSecondary(priDb, "sec0", true, 0);
        SecondaryDatabase secDb1 = openSecondary(priDb, "sec1", true, 1);
        SecondaryDatabase secDb2 = openSecondary(priDb, "sec2", true, 2);

        OperationStatus status;
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry data = new DatabaseEntry();
        Transaction txn;
        txn = txnBegin();

        for (int i = 0; i < dataSet.length; i += 2) {
            int[] vals = dataSet[i];
            setData(data, vals[0], vals[1], vals[2]);
            setKey(key, dataSet[i + 1][0]);
            status = priDb.put(txn, key, data);
            assertEquals(name, OperationStatus.SUCCESS, status);
        }

        txnCommit(txn);
        txn = txnBeginCursor();

        SecondaryCursor c0 = secDb0.openSecondaryCursor(txn, null);
        SecondaryCursor c1 = secDb1.openSecondaryCursor(txn, null);
        SecondaryCursor c2 = secDb2.openSecondaryCursor(txn, null);
        SecondaryCursor[] cursors = {c0, c1, c2};

        for (int i = 0; i < joinSet.length; i += 2) {
            int[] indexKeys = joinSet[i];
            int[] priKeys = joinSet[i + 1];
            String prefix = name + " row=" + i;
            for (int k = 0; k < 3; k += 1) {
                String msg = prefix + " k=" + k + " ikey=" + indexKeys[k];
                setKey(key, indexKeys[k]);
                status = cursors[k].getSearchKey(key, data,
                                                 LockMode.DEFAULT);
                assertEquals(msg, OperationStatus.SUCCESS, status);
            }
            for (int j = 0; j < 2; j += 1) {
                boolean withData = (j == 0);
                JoinConfig config = (j == 0) ? null : joinConfigNoSort;
                JoinCursor jc = priDb.join(cursors, config);
                assertSame(priDb, jc.getDatabase());
                for (int k = 0; k < priKeys.length; k += 1) {
                    String msg = prefix + " k=" + k + " pkey=" + priKeys[k];
                    if (withData) {
                        status = jc.getNext(key, data, LockMode.DEFAULT);
                    } else {
                        status = jc.getNext(key, LockMode.DEFAULT);
                    }
                    assertEquals(msg, OperationStatus.SUCCESS, status);
                    assertEquals(msg, priKeys[k], (int) key.getData()[0]);
                    if (withData) {
                        boolean dataFound = false;
                        for (int m = 0; m < dataSet.length; m += 2) {
                            int[] vals = dataSet[m];
                            int priKey = dataSet[m + 1][0];
                            if (priKey == priKeys[k]) {
                                for (int n = 0; n < 3; n += 1) {
                                    assertEquals(msg, vals[n],
                                                 (int) data.getData()[n]);
                                    dataFound = true;;
                                }
                            }
                        }
                        assertTrue(msg, dataFound);
                    }
                }
                String msg = prefix + " no more expected";
                if (withData) {
                    status = jc.getNext(key, data, LockMode.DEFAULT);
                } else {
                    status = jc.getNext(key, LockMode.DEFAULT);
                }
                assertEquals(msg, OperationStatus.NOTFOUND, status);

                Cursor[] sorted = DbInternal.getSortedCursors(jc);
                assertEquals(CURSOR_ORDER.length, sorted.length);
                if (config == joinConfigNoSort) {
                    Database db0 = sorted[0].getDatabase();
                    Database db1 = sorted[1].getDatabase();
                    Database db2 = sorted[2].getDatabase();
                    assertSame(db0, secDb0);
                    assertSame(db1, secDb1);
                    assertSame(db2, secDb2);
                } else if (setNum == CURSOR_ORDER_SET) {
                    Database db0 = sorted[CURSOR_ORDER[0]].getDatabase();
                    Database db1 = sorted[CURSOR_ORDER[1]].getDatabase();
                    Database db2 = sorted[CURSOR_ORDER[2]].getDatabase();
                    assertSame(db0, secDb0);
                    assertSame(db1, secDb1);
                    assertSame(db2, secDb2);
                }
                jc.close();
            }
        }

        c0.close();
        c1.close();
        c2.close();
        txnCommit(txn);

        secDb0.close();
        secDb1.close();
        secDb2.close();
        priDb.close();

        /* Remove dbs since we reuse them multiple times in a single case. */
        txn = txnBegin();
        env.removeDatabase(txn, "pri");
View Full Code Here


        dbConfig.setTransactional(isTransactional);
        dbConfig.setAllowCreate(true);
        dbConfig.setSortedDuplicates(allowDuplicates);
        dbConfig.setKeyCreator(new MyKeyCreator());
        Transaction txn = txnBegin();
        SecondaryDatabase secDb;
        try {
            secDb = env.openSecondaryDatabase(txn, dbName, priDb, dbConfig);
        } finally {
            txnCommit(txn);
        }
View Full Code Here

              String dbName,
              Database primaryDatabase,
              SecondaryConfig config)
  throws DatabaseException {

  SecondaryDatabase db;
  if (config.getExclusiveCreate()) {
      db = env.openSecondaryDatabase(null, dbName,
             primaryDatabase, config);
      databaseHandleCache.put(dbName, db);
  } else {
      db = (SecondaryDatabase) databaseHandleCache.get(dbName);
      if (db == null) {
    db = env.openSecondaryDatabase(null, dbName,
                 primaryDatabase, config);
    databaseHandleCache.put(dbName, db);
      } else {
    DbInternal.databaseConfigValidate(config, db.getConfig());
      }
  }
  return db;
    }
View Full Code Here

        throws DatabaseException {

        Database priDb1 = openPrimary("pri1");
        Database priDb2 = openPrimary("pri2");

        SecondaryDatabase secDb1 = openSecondary(priDb1, "sec1", null, null);
        SecondaryDatabase secDb2 = openSecondary(priDb2, "sec2", priDb1,
                                                 onDelete);

        OperationStatus status;
        DatabaseEntry data = new DatabaseEntry();
        DatabaseEntry key = new DatabaseEntry();
        DatabaseEntry pkey = new DatabaseEntry();
        Transaction txn = txnBegin();

        /*
         * pri1 has a record with primary key 1 and index key 3.
         * pri2 has a record with primary key 2 and foreign key 1,
         * which is the primary key of pri1.
         * pri2 has another record with primary key 3 and foreign key 1,
         * to enable testing cascade and nullify for secondary duplicates.
         */

        /* Add three records. */

        status = priDb1.put(txn, entry(1), entry(3));
        assertEquals(OperationStatus.SUCCESS, status);

        status = priDb2.put(txn, entry(2), entry(1));
        assertEquals(OperationStatus.SUCCESS, status);

        status = priDb2.put(txn, entry(3), entry(1));
        assertEquals(OperationStatus.SUCCESS, status);

        /* Verify record data. */

        status = priDb1.get(txn, entry(1), data, LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
        assertEquals(3, val(data));

        status = secDb1.get(txn, entry(3), data, LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
        assertEquals(3, val(data));

        status = priDb2.get(txn, entry(2), data, LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
        assertEquals(1, val(data));

        status = priDb2.get(txn, entry(3), data, LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
        assertEquals(1, val(data));

        SecondaryCursor cursor = secDb2.openSecondaryCursor(txn, null);
        status = cursor.getFirst(key, pkey, data, LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
        assertEquals(1, val(key));
        assertEquals(2, val(pkey));
        assertEquals(1, val(data));
        status = cursor.getNext(key, pkey, data, LockMode.DEFAULT);
        assertEquals(OperationStatus.SUCCESS, status);
        assertEquals(1, val(key));
        assertEquals(3, val(pkey));
        assertEquals(1, val(data));
        status = cursor.getNext(key, pkey, data, LockMode.DEFAULT);
        assertEquals(OperationStatus.NOTFOUND, status);
        cursor.close();

        txnCommit(txn);
        txn = txnBegin();

        /* Test delete action. */

        if (onDelete == ForeignKeyDeleteAction.ABORT) {

            /* Test that we abort trying to delete a referenced key. */

            try {
                status = priDb1.delete(txn, entry(1));
                fail();
            } catch (DatabaseException expected) {
                txnAbort(txn);
                txn = txnBegin();
            }

            /* Test that we can put a record into pri2 with a null foreign key
             * value. */

            status = priDb2.put(txn, entry(2), entry(0));
            assertEquals(OperationStatus.SUCCESS, status);

            status = priDb2.put(txn, entry(3), entry(0));
            assertEquals(OperationStatus.SUCCESS, status);

            /* The sec2 records should not be present since the key was set
             * to null above. */

            status = secDb2.get(txn, entry(1), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            /* Test that now we can delete the record in pri1, since it is no
             * longer referenced. */

            status = priDb1.delete(txn, entry(1));
            assertEquals(OperationStatus.SUCCESS, status);

            status = priDb1.get(txn, entry(1), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            status = secDb1.get(txn, entry(3), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

        } else if (onDelete == ForeignKeyDeleteAction.NULLIFY) {

            /* Delete the referenced key. */

            status = priDb1.delete(txn, entry(1));
            assertEquals(OperationStatus.SUCCESS, status);

            status = priDb1.get(txn, entry(1), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            status = secDb1.get(txn, entry(3), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            /* The pri2 records should still exist, but should have a zero/null
             * secondary key since it was nullified. */

            status = priDb2.get(txn, entry(2), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals(0, val(data));

            status = priDb2.get(txn, entry(3), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.SUCCESS, status);
            assertEquals(0, val(data));
           
            status = secDb2.get(txn, entry(1), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

        } else if (onDelete == ForeignKeyDeleteAction.CASCADE) {

            /* Delete the referenced key. */

            status = priDb1.delete(txn, entry(1));
            assertEquals(OperationStatus.SUCCESS, status);

            status = priDb1.get(txn, entry(1), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            status = secDb1.get(txn, entry(3), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            /* The pri2 records should have deleted also. */

            status = priDb2.get(txn, entry(2), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

            status = priDb2.get(txn, entry(3), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);
           
            status = secDb2.get(txn, entry(1), data, LockMode.DEFAULT);
            assertEquals(OperationStatus.NOTFOUND, status);

        } else {
            throw new IllegalStateException();
        }

        /*
         * Test that a foreign key value may not be used that is not present
         * in the foreign db. Key 2 is not in pri1 in this case.
         */
        try {
            status = priDb2.put(txn, entry(3), entry(2));
            fail();
        } catch (DatabaseException expected) { }

        txnCommit(txn);
        secDb1.close();
        secDb2.close();
        priDb1.close();
        priDb2.close();
    }
View Full Code Here

        SecondaryConfig secConfig = new SecondaryConfig();
        secConfig.setTransactional(true);
        secConfig.setAllowCreate(true);
        secConfig.setSortedDuplicates(true);
        secConfig.setKeyCreator(new MyKeyCreator(secKeyBinding, dataBinding));
        SecondaryDatabase exampleSecDb =
      exampleEnv.openSecondaryDatabase(txn, "bindingsSecDb",
               exampleDb, secConfig);
        txn.commit();

        /* DatabaseEntry represents the key and data of each record. */
        DatabaseEntry keyEntry = new DatabaseEntry();
        DatabaseEntry dataEntry = new DatabaseEntry();

        if (doInsert) {

            /*
             * Put some data in.  Note that the primary database is always used
             * to add data.  Adding or changing data in the secondary database
             * is not allowed; however, deleting through the secondary database
             * is allowed.
             */
            for (int i = offset; i < numRecords + offset; i++) {
                txn = exampleEnv.beginTransaction(null, null);
                StringBuffer stars = new StringBuffer();
                for (int j = 0; j < i; j++) {
                    stars.append('*');
                }
                MyData data = new MyData(i, stars.toString());

                IntegerBinding.intToEntry(i, keyEntry);
                dataBinding.objectToEntry(data, dataEntry);

                OperationStatus status =
                    exampleDb.put(txn, keyEntry, dataEntry);

                /*
                 * Note that put will throw a DatabaseException when error
                 * conditions are found such as deadlock.  However, the status
                 * return conveys a variety of information. For example, the
                 * put might succeed, or it might not succeed if the record
                 * exists and duplicates were not
                 */
                if (status != OperationStatus.SUCCESS) {
                    throw new DatabaseException
      ("Data insertion got status " + status);
                }
                txn.commit();
            }
        } else {
           
            /*
       * Retrieve the data by secondary key by opening a cursor on the
       * secondary database.  The key parameter for a secondary cursor is
       * always the secondary key, but the data parameter is always the
       * data of the primary database.  You can cast the cursor to a
       * SecondaryCursor and use additional method signatures for
       * retrieving the primary key also.  Or you can call
       * openSecondaryCursor() to avoid casting.
       */
            txn = exampleEnv.beginTransaction(null, null);
            Cursor cursor = exampleSecDb.openCursor(txn, null);

            while (cursor.getNext(keyEntry, dataEntry, LockMode.DEFAULT) ==
                   OperationStatus.SUCCESS) {

                String key = (String) secKeyBinding.entryToObject(keyEntry);
                MyData data = (MyData) dataBinding.entryToObject(dataEntry);

                System.out.println("key=" + key + " data=" + data);
            }
            cursor.close();
            txn.commit();
        }

        /*
         * Always close secondary databases before closing their associated
         * primary database.
         */
        catalogDb.close();
        exampleSecDb.close();
        exampleDb.close();
        exampleEnv.close();
    }
View Full Code Here

                                      String dbName,
                                      Database primaryDatabase,
                                      SecondaryConfig config)
        throws DatabaseException {

        SecondaryDatabase db;
        if (config.getExclusiveCreate()) {
            db = env.openSecondaryDatabase(null, dbName,
                                           primaryDatabase, config);
            databaseHandleCache.put(dbName, db);
        } else {
            db = (SecondaryDatabase) databaseHandleCache.get(dbName);
            if (db == null) {
                db = env.openSecondaryDatabase(null, dbName,
                                               primaryDatabase, config);
                databaseHandleCache.put(dbName, db);
            } else {
                DbInternal.validate(config, db.getConfig());
            }
        }
        return db;
    }
View Full Code Here

              String dbName,
              Database primaryDatabase,
              SecondaryConfig config)
  throws DatabaseException {

  SecondaryDatabase db;
  if (config.getExclusiveCreate()) {
      db = env.openSecondaryDatabase(null, dbName,
             primaryDatabase, config);
      databaseHandleCache.put(dbName, db);
  } else {
      db = (SecondaryDatabase) databaseHandleCache.get(dbName);
      if (db == null) {
    db = env.openSecondaryDatabase(null, dbName,
                 primaryDatabase, config);
    databaseHandleCache.put(dbName, db);
      } else {
    DbInternal.databaseConfigValidate(config, db.getConfig());
      }
  }
  return db;
    }
View Full Code Here

  try {
      Environment env = null;
      Transaction txn = null;
      Database db = null;
      SecondaryDatabase secDb = null;
      Cursor cursor = null;
      JEConnection dc = null;
      try {
    dc = getConnection(JE_ENV);
View Full Code Here

                 " Transactional, DeferredWrite, ReadOnly");
        }

        PersistKeyBinding keyBinding = getKeyBinding(keyClassName);

        SecondaryDatabase db = openSecondaryDatabase
            (txn, fileAndDbNames, priDb, config, doNotCreate);
        if (db == null) {
            assert doNotCreate;
            return null;
        }
View Full Code Here

            }
            /* First try creating a new database, populate if needed. */
            if (config.getAllowCreate()) {
                config.setExclusiveCreate(true);
                /* AllowPopulate is already set to true. */
                final SecondaryDatabase db = DbCompat.openSecondaryDatabase
                    (env, txn, fileAndDbNames[0], fileAndDbNames[1], priDb,
                     config);
                if (db != null) {
                    return db;
                }
            }
            /* Next try opening an existing database. */
            config.setAllowCreate(false);
            config.setAllowPopulate(false);
            config.setExclusiveCreate(false);
            final SecondaryDatabase db = DbCompat.openSecondaryDatabase
                (env, txn, fileAndDbNames[0], fileAndDbNames[1], priDb,
                 config);
            return db;
        } finally {
            config.setAllowPopulate(true);
View Full Code Here

TOP

Related Classes of com.sleepycat.je.SecondaryDatabase

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.