예제 #1
0
파일: Store.java 프로젝트: nologic/nabs
 private SecondaryConfig getSecondaryConfig(
     String secName,
     EntityMetadata entityMeta,
     String keyClassName,
     SecondaryKeyMetadata secKeyMeta) {
   SecondaryConfig config = secConfigMap.get(secName);
   if (config == null) {
     /* Set common properties to match the primary DB. */
     DatabaseConfig priConfig = getPrimaryConfig(entityMeta);
     config = new SecondaryConfig();
     config.setTransactional(priConfig.getTransactional());
     config.setAllowCreate(!priConfig.getReadOnly());
     config.setReadOnly(priConfig.getReadOnly());
     DbCompat.setDeferredWrite(config, DbCompat.getDeferredWrite(priConfig));
     /* Set secondary properties based on metadata. */
     config.setAllowPopulate(true);
     Relationship rel = secKeyMeta.getRelationship();
     config.setSortedDuplicates(
         rel == Relationship.MANY_TO_ONE || rel == Relationship.MANY_TO_MANY);
     setBtreeComparator(config, secKeyMeta.getClassName());
     PersistKeyCreator keyCreator =
         new PersistKeyCreator(catalog, entityMeta, keyClassName, secKeyMeta);
     if (rel == Relationship.ONE_TO_MANY || rel == Relationship.MANY_TO_MANY) {
       config.setMultiKeyCreator(keyCreator);
     } else {
       config.setKeyCreator(keyCreator);
     }
     DeleteAction deleteAction = secKeyMeta.getDeleteAction();
     if (deleteAction != null) {
       ForeignKeyDeleteAction baseDeleteAction;
       switch (deleteAction) {
         case ABORT:
           baseDeleteAction = ForeignKeyDeleteAction.ABORT;
           break;
         case CASCADE:
           baseDeleteAction = ForeignKeyDeleteAction.CASCADE;
           break;
         case NULLIFY:
           baseDeleteAction = ForeignKeyDeleteAction.NULLIFY;
           break;
         default:
           throw new IllegalStateException(deleteAction.toString());
       }
       config.setForeignKeyDeleteAction(baseDeleteAction);
       if (deleteAction == DeleteAction.NULLIFY) {
         config.setForeignMultiKeyNullifier(keyCreator);
       }
     }
     secConfigMap.put(secName, config);
   }
   return config;
 }
예제 #2
0
  private SecondaryDatabase openSecondary(
      Database priDb,
      boolean allowDuplicates,
      String dbName,
      boolean allowPopulate,
      boolean readOnly)
      throws DatabaseException {

    List secListBefore = priDb.getSecondaryDatabases();
    SecondaryConfig dbConfig = new SecondaryConfig();
    dbConfig.setTransactional(isTransactional);
    dbConfig.setAllowCreate(true);
    dbConfig.setSortedDuplicates(allowDuplicates);
    dbConfig.setReadOnly(readOnly);
    dbConfig.setAllowPopulate(allowPopulate);
    if (!readOnly) {
      if (useMultiKey) {
        dbConfig.setMultiKeyCreator(new SimpleMultiKeyCreator(new MyKeyCreator()));
      } else {
        dbConfig.setKeyCreator(new MyKeyCreator());
      }
    }
    Transaction txn = txnBegin();
    SecondaryDatabase secDb;
    try {
      secDb = env.openSecondaryDatabase(txn, dbName, priDb, dbConfig);
    } finally {
      txnCommit(txn);
    }
    assertNotNull(secDb);

    /* Check configuration. */
    assertSame(priDb, secDb.getPrimaryDatabase());
    SecondaryConfig config2 = secDb.getSecondaryConfig();
    assertEquals(allowPopulate, config2.getAllowPopulate());
    assertEquals(dbConfig.getKeyCreator(), config2.getKeyCreator());

    /* Make sure the new secondary is added to the primary's list. */
    List secListAfter = priDb.getSecondaryDatabases();
    assertTrue(secListAfter.remove(secDb));
    assertEquals(secListBefore, secListAfter);

    return secDb;
  }
예제 #3
0
  /** @deprecated use of Database.truncate */
  public void testOperationsNotAllowed() throws DatabaseException {

    SecondaryDatabase secDb = initDb();
    Database priDb = secDb.getPrimaryDatabase();
    Transaction txn = txnBegin();

    /* Open secondary without a key creator. */
    try {
      env.openSecondaryDatabase(txn, "xxx", priDb, null);
      fail();
    } catch (NullPointerException expected) {
    }
    try {
      env.openSecondaryDatabase(txn, "xxx", priDb, new SecondaryConfig());
      fail();
    } catch (NullPointerException expected) {
    }

    /* Open secondary with both single and multi key creators. */
    SecondaryConfig config = new SecondaryConfig();
    config.setKeyCreator(new MyKeyCreator());
    config.setMultiKeyCreator(new SimpleMultiKeyCreator(new MyKeyCreator()));
    try {
      env.openSecondaryDatabase(txn, "xxx", priDb, config);
      fail();
    } catch (IllegalArgumentException expected) {
    }

    /* Database operations. */

    DatabaseEntry key = entry(1);
    DatabaseEntry data = entry(2);

    try {
      secDb.getSearchBoth(txn, key, data, LockMode.DEFAULT);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.put(txn, key, data);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.putNoOverwrite(txn, key, data);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.putNoDupData(txn, key, data);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.truncate(txn, true);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    try {
      secDb.join(new Cursor[0], null);
      fail();
    } catch (UnsupportedOperationException expected) {
    }

    /* Cursor operations. */

    txnCommit(txn);
    txn = txnBeginCursor();

    SecondaryCursor cursor = null;
    try {
      cursor = secDb.openSecondaryCursor(txn, null);

      try {
        cursor.getSearchBoth(key, data, LockMode.DEFAULT);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.getSearchBothRange(key, data, LockMode.DEFAULT);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.putCurrent(data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.put(key, data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.putNoOverwrite(key, data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }

      try {
        cursor.putNoDupData(key, data);
        fail();
      } catch (UnsupportedOperationException expected) {
      }
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }

    txnCommit(txn);
    secDb.close();
    priDb.close();

    /* Primary with duplicates. */
    priDb = openDatabase(true, "testDBWithDups", false);
    try {
      openSecondary(priDb, true, "testSecDB", false, false);
      fail();
    } catch (IllegalArgumentException expected) {
    }

    priDb.close();

    /* Single secondary with two primaries.*/
    Database pri1 = openDatabase(false, "pri1", false);
    Database pri2 = openDatabase(false, "pri2", false);
    Database sec1 = openSecondary(pri1, false, "sec", false, false);
    try {
      openSecondary(pri2, false, "sec", false, false);
      fail();
    } catch (IllegalArgumentException expected) {
    }
    sec1.close();
    pri1.close();
    pri2.close();
  }