Package com.sleepycat.je

Examples of com.sleepycat.je.SecondaryDatabase


  boolean retval = false;
  try
 
   //*-- first, get the environment
   if (env == null) { openEnv(DBDIR, false); if (env == null) return retval; }
   SecondaryDatabase  dbe = getSecDB(); //if (dbe == null) return retval;
   if (dbe != null) dbe.close();  
   env.removeDatabase(null, dbname);   
   retval = true;
  }
  catch (DatabaseException dbe) { }

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

                 " Transactional, DeferredWrite, ReadOnly");
        }

        PersistKeyBinding keyBinding = getKeyBinding(keyClassName);

        SecondaryDatabase db = openSecondaryDatabase
            (txn, fileAndDbNames, primaryIndex,
             secKeyMeta.getKeyName(), 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 true; comparators are set. */
                final SecondaryDatabase db = DbCompat.openSecondaryDatabase
                    (env, txn, fileAndDbNames[0], fileAndDbNames[1], priDb,
                     config);
                if (db != null) {
                    /* For unit testing. */
                    boolean doFlush = false;
                    /* Update dup ordering bug info. [#17252] */
                    if (config.getDuplicateComparator() != null &&
                        entityFormat.setSecKeyCorrectlyOrdered(keyName)) {
                        catalog.flush(txn);
                        doFlush = true;
                    }
                   
                    /*
                     * expectFlush is false except when set by
                     * SecondaryDupOrderTest.
                     */
                    assert !expectFlush || doFlush;
                   
                    return db;
                }
            }
            /* Next try opening an existing database. */
            config.setAllowCreate(false);
            config.setAllowPopulate(false);
            config.setExclusiveCreate(false);
           
            /* Account for dup ordering bug. [#17252] */
            if (config.getDuplicateComparator() != null &&
                entityFormat.isSecKeyIncorrectlyOrdered(keyName)) {
                /* <!-- begin JE only --> */
                config.setOverrideDuplicateComparator(false);
                /* <!-- end JE only --> */
                config.setDuplicateComparator((Comparator<byte[]>) null);
            }
            final SecondaryDatabase db = DbCompat.openSecondaryDatabase
                (env, txn, fileAndDbNames[0], fileAndDbNames[1], priDb,
                 config);
            return db;
        } finally {
            config.setAllowPopulate(true);
View Full Code Here

   */
  public void get(IMetaData[] keys, ICache cache) throws Exception {
    // 检查是否有还未初始化的单项键值索引表
    for (IMetaData o : keys) {
      String name = o.getName();
      SecondaryDatabase db = dbKeys.get(name);
      if (db == null) {
        synchronized (this) {
          db = dbKeys.get(name);
          if (db == null) {
            dbKeys.put(name, env.openSecondaryDatabase(null, name, dbMain,
View Full Code Here

   *          关键字名称
   * @throws Exception
   *           数据操作异常
   */
  public void clear(String name) throws Exception {
    SecondaryDatabase db = dbKeys.get(name);
    if (db != null) {
      Transaction tran = env.beginTransaction(null, null);
      DatabaseEntry key = new DatabaseEntry();
      DatabaseEntry pKey = new DatabaseEntry();
      DatabaseEntry data = new DatabaseEntry();
      try {
        SecondaryCursor cursor = db.openSecondaryCursor(tran, null);
        try {
          while (cursor.getNext(key, pKey, data, null) == OperationStatus.SUCCESS) {
            cursor.delete();
            dataBinding.entryToObject(data).remove();
          }
View Full Code Here

      DatabaseEntry data = new DatabaseEntry();
      List<DatabaseEntry> entries = new ArrayList<DatabaseEntry>();
      try {
        for (int i = 0; i < size; i++) {
          IMetaData o = keys[i];
          SecondaryDatabase db = dbKeys.get(o.getName());
          if (db == null) {
            return;
          }
          SecondaryCursor cursor = db.openSecondaryCursor(null, null);
          key.setData(o.getBytes());
          cursor.getSearchKey(key, data, null);
          cursors[i] = cursor;
        }
        JoinCursor joinCursor = dbMain.join(cursors, null);
View Full Code Here

   *          缓存关键字
   * @throws Exception
   *           数据操作异常
   */
  private void clear(IMetaData data) throws Exception {
    SecondaryDatabase db = dbKeys.get(data.getName());
    if (db != null) {
      Transaction tran = env.beginTransaction(null, null);
      DatabaseEntry key = new DatabaseEntry(data.getBytes());
      DatabaseEntry pKey = new DatabaseEntry();
      DatabaseEntry pData = new DatabaseEntry();
      try {
        SecondaryCursor cursor = db.openSecondaryCursor(tran, null);
        try {
          OperationStatus ret = cursor.getSearchKey(key, pKey, pData, null);
          while (ret == OperationStatus.SUCCESS) {
            cursor.delete();
            dataBinding.entryToObject(pData).remove();
View Full Code Here

    }

    public void testPutAndDelete()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();

        DatabaseEntry data = new DatabaseEntry();
        DatabaseEntry key = new DatabaseEntry();
        OperationStatus status;
        Transaction txn = txnBegin();
       
        /* Database.put() */
        status = priDb.put(txn, entry(1), entry(2));
        assertSame(OperationStatus.SUCCESS, status);
        status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(1), key);
        assertDataEquals(entry(2), data);

        /* Database.putNoOverwrite() */
        status = priDb.putNoOverwrite(txn, entry(1), entry(1));
        assertSame(OperationStatus.KEYEXIST, status);
        status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(1), key);
        assertDataEquals(entry(2), data);
       
        /* Database.put() overwrite */
        status = priDb.put(txn, entry(1), entry(3));
        assertSame(OperationStatus.SUCCESS, status);
        status = secDb.get(txn, entry(102), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.NOTFOUND, status);
        status = secDb.get(txn, entry(103), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.SUCCESS, status);
        assertDataEquals(entry(1), key);
        assertDataEquals(entry(3), data);
       
        /* Database.delete() */
        status = priDb.delete(txn, entry(1));
        assertSame(OperationStatus.SUCCESS, status);
        status = priDb.delete(txn, entry(1));
        assertSame(OperationStatus.NOTFOUND, status);
        status = secDb.get(txn, entry(103), key, data, LockMode.DEFAULT);
        assertSame(OperationStatus.NOTFOUND, status);

        /*
         * Database.putNoDupData() cannot be called since the primary cannot be
         * configured for duplicates.
         */

        /* Primary and secondary are empty now. */

        /* Get a txn for a cursor. */
        txnCommit(txn);
        txn = txnBeginCursor();

        Cursor priCursor = null;
        SecondaryCursor secCursor = null;
        try {
            priCursor = priDb.openCursor(txn, null);
            secCursor = secDb.openSecondaryCursor(txn, null);

            /* Cursor.putNoOverwrite() */
            status = priCursor.putNoOverwrite(entry(1), entry(2));
            assertSame(OperationStatus.SUCCESS, status);
            status = secCursor.getSearchKey(entry(102), key, data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(1), key);
            assertDataEquals(entry(2), data);

            /* Cursor.putCurrent() */
            status = priCursor.putCurrent(entry(3));
            assertSame(OperationStatus.SUCCESS, status);
            status = secCursor.getSearchKey(entry(102), key, data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);
            status = secCursor.getSearchKey(entry(103), key, data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(1), key);
            assertDataEquals(entry(3), data);

            /* Cursor.delete() */
            status = priCursor.delete();
            assertSame(OperationStatus.SUCCESS, status);
            status = priCursor.delete();
            assertSame(OperationStatus.KEYEMPTY, status);
            status = secCursor.getSearchKey(entry(103), key, data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);
            status = priCursor.getSearchKey(entry(1), data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);

            /* Cursor.put() */
            status = priCursor.put(entry(1), entry(4));
            assertSame(OperationStatus.SUCCESS, status);
            status = secCursor.getSearchKey(entry(104), key, data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(1), key);
            assertDataEquals(entry(4), data);

            /* SecondaryCursor.delete() */
            status = secCursor.delete();
            assertSame(OperationStatus.SUCCESS, status);
            status = secCursor.delete();
            assertSame(OperationStatus.KEYEMPTY, status);
            status = secCursor.getCurrent(new DatabaseEntry(), key, data,
                                          LockMode.DEFAULT);
            assertSame(OperationStatus.KEYEMPTY, status);
            status = secCursor.getSearchKey(entry(104), key, data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);
            status = priCursor.getSearchKey(entry(1), data,
                                            LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);

            /*
             * Cursor.putNoDupData() cannot be called since the primary cannot
             * be configured for duplicates.
             */

            /* Primary and secondary are empty now. */
        } finally {
            if (secCursor != null) {
                secCursor.close();
            }
            if (priCursor != null) {
                priCursor.close();
            }
        }

        txnCommit(txn);
        secDb.close();
        priDb.close();
    }
View Full Code Here

    }

    public void testGet()
        throws DatabaseException {

        SecondaryDatabase secDb = initDb();
        Database priDb = secDb.getPrimaryDatabase();

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

        /*
         * For parameters that do not require initialization with a non-null
         * data array, we set them to null to make sure this works. [#12121]
         */

        /* Add one record for each key with one data/duplicate. */
        for (int i = 0; i < NUM_RECS; i += 1) {
            status = priDb.put(txn, entry(i), entry(i));
            assertSame(OperationStatus.SUCCESS, status);
        }

        /* SecondaryDatabase.get() */
        for (int i = 0; i < NUM_RECS; i += 1) {

            data.setData(null);
            status = secDb.get(txn, entry(i + KEY_OFFSET), key,
                               data, LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(i), key);
            assertDataEquals(entry(i), data);
        }
        data.setData(null);
        status = secDb.get(txn, entry(NUM_RECS + KEY_OFFSET), key,
                           data, LockMode.DEFAULT);
        assertSame(OperationStatus.NOTFOUND, status);

        /* SecondaryDatabase.getSearchBoth() */
        for (int i = 0; i < NUM_RECS; i += 1) {
            data.setData(null);
            status = secDb.getSearchBoth(txn, entry(i + KEY_OFFSET), entry(i),
                                         data, LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(i), data);
        }
        data.setData(null);
        status = secDb.getSearchBoth(txn, entry(NUM_RECS + KEY_OFFSET),
                                     entry(NUM_RECS), data, LockMode.DEFAULT);
        assertSame(OperationStatus.NOTFOUND, status);

        /* Get a cursor txn. */
        txnCommit(txn);
        txn = txnBeginCursor();

        SecondaryCursor cursor = secDb.openSecondaryCursor(txn, null);
        try {
            /* SecondaryCursor.getFirst()/getNext() */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getFirst(secKey, key, data, LockMode.DEFAULT);
            for (int i = 0; i < NUM_RECS; i += 1) {
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getNext(secKey, key, data, LockMode.DEFAULT);
            }
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getCurrent() (last) */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getCurrent(secKey, key, data, LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(NUM_RECS - 1 + KEY_OFFSET), secKey);
            assertDataEquals(entry(NUM_RECS - 1), key);
            assertDataEquals(entry(NUM_RECS - 1), data);

            /* SecondaryCursor.getLast()/getPrev() */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getLast(secKey, key, data, LockMode.DEFAULT);
            for (int i = NUM_RECS - 1; i >= 0; i -= 1) {
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getPrev(secKey, key, data, LockMode.DEFAULT);
            }
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getCurrent() (first) */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getCurrent(secKey, key, data, LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(0 + KEY_OFFSET), secKey);
            assertDataEquals(entry(0), key);
            assertDataEquals(entry(0), data);

            /* SecondaryCursor.getSearchKey() */
            key.setData(null);
            data.setData(null);
            status = cursor.getSearchKey(entry(KEY_OFFSET - 1), key,
                                         data, LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);
            for (int i = 0; i < NUM_RECS; i += 1) {
                key.setData(null);
                data.setData(null);
                status = cursor.getSearchKey(entry(i + KEY_OFFSET), key,
                                             data, LockMode.DEFAULT);
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
            }
            key.setData(null);
            data.setData(null);
            status = cursor.getSearchKey(entry(NUM_RECS + KEY_OFFSET), key,
                                         data, LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getSearchBoth() */
            data.setData(null);
            status = cursor.getSearchKey(entry(KEY_OFFSET - 1), entry(0),
                                         data, LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);
            for (int i = 0; i < NUM_RECS; i += 1) {
                data.setData(null);
                status = cursor.getSearchBoth(entry(i + KEY_OFFSET), entry(i),
                                              data, LockMode.DEFAULT);
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i), data);
            }
            data.setData(null);
            status = cursor.getSearchBoth(entry(NUM_RECS + KEY_OFFSET),
                                          entry(NUM_RECS), data,
                                          LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getSearchKeyRange() */
            key.setData(null);
            data.setData(null);
            status = cursor.getSearchKeyRange(entry(KEY_OFFSET - 1), key,
                                              data, LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(0), key);
            assertDataEquals(entry(0), data);
            for (int i = 0; i < NUM_RECS; i += 1) {
                key.setData(null);
                data.setData(null);
                status = cursor.getSearchKeyRange(entry(i + KEY_OFFSET), key,
                                                  data, LockMode.DEFAULT);
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
            }
            key.setData(null);
            data.setData(null);
            status = cursor.getSearchKeyRange(entry(NUM_RECS + KEY_OFFSET),
                                              key, data, LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getSearchBothRange() */
            data.setData(null);
            status = cursor.getSearchBothRange(entry(1 + KEY_OFFSET), entry(1),
                                               data, LockMode.DEFAULT);
            assertSame(OperationStatus.SUCCESS, status);
            assertDataEquals(entry(1), data);
            for (int i = 0; i < NUM_RECS; i += 1) {
                data.setData(null);
                status = cursor.getSearchBothRange(entry(i + KEY_OFFSET),
                                                   entry(i), data,
                                                   LockMode.DEFAULT);
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i), data);
            }
            data.setData(null);
            status = cursor.getSearchBothRange(entry(NUM_RECS + KEY_OFFSET),
                                               entry(NUM_RECS), data,
                                               LockMode.DEFAULT);
            assertSame(OperationStatus.NOTFOUND, status);

            /* Add one duplicate for each key. */
            Cursor priCursor = priDb.openCursor(txn, null);
            try {
                for (int i = 0; i < NUM_RECS; i += 1) {
                    status = priCursor.put(entry(i + KEY_OFFSET), entry(i));
                    assertSame(OperationStatus.SUCCESS, status);
                }
            } finally {
                priCursor.close();
            }

            /* SecondaryCursor.getNextDup() */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getFirst(secKey, key, data, LockMode.DEFAULT);
            for (int i = 0; i < NUM_RECS; i += 1) {
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getNextDup(secKey, key, data,
                                           LockMode.DEFAULT);
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i + KEY_OFFSET), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getNextDup(secKey, key, data,
                                           LockMode.DEFAULT);
                assertSame(OperationStatus.NOTFOUND, status);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getNext(secKey, key, data, LockMode.DEFAULT);
            }
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getNextNoDup() */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getFirst(secKey, key, data, LockMode.DEFAULT);
            for (int i = 0; i < NUM_RECS; i += 1) {
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getNextNoDup(secKey, key, data,
                                             LockMode.DEFAULT);
            }
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getPrevDup() */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getLast(secKey, key, data, LockMode.DEFAULT);
            for (int i = NUM_RECS - 1; i >= 0; i -= 1) {
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i + KEY_OFFSET), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getPrevDup(secKey, key, data,
                                           LockMode.DEFAULT);
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getPrevDup(secKey, key, data,
                                           LockMode.DEFAULT);
                assertSame(OperationStatus.NOTFOUND, status);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getPrev(secKey, key, data, LockMode.DEFAULT);
            }
            assertSame(OperationStatus.NOTFOUND, status);

            /* SecondaryCursor.getPrevNoDup() */
            secKey.setData(null);
            key.setData(null);
            data.setData(null);
            status = cursor.getLast(secKey, key, data, LockMode.DEFAULT);
            for (int i = NUM_RECS - 1; i >= 0; i -= 1) {
                assertSame(OperationStatus.SUCCESS, status);
                assertDataEquals(entry(i + KEY_OFFSET), secKey);
                assertDataEquals(entry(i + KEY_OFFSET), key);
                assertDataEquals(entry(i), data);
                secKey.setData(null);
                key.setData(null);
                data.setData(null);
                status = cursor.getPrevNoDup(secKey, key, data,
                                             LockMode.DEFAULT);
            }
            assertSame(OperationStatus.NOTFOUND, status);
        } finally {
            cursor.close();
        }

        txnCommit(txn);
        secDb.close();
        priDb.close();
    }
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.