示例#1
0
  private void doSecondaryBulkLoad(boolean closeAndOpenNormally) throws DatabaseException {

    PrimaryIndex<Integer, RelatedX> priX;
    PrimaryIndex<Integer, RelatedY> priY;
    SecondaryIndex<Integer, Integer, RelatedX> secX;

    /* Open priX with SecondaryBulkLoad=true. */
    StoreConfig config = new StoreConfig();
    config.setAllowCreate(true);
    config.setSecondaryBulkLoad(true);
    open(config);

    /* Getting priX should not create the secondary index. */
    priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
    PersistTestUtils.assertDbExists(false, env, STORE_NAME, RelatedX.class.getName(), "key2");

    /* We can put records that violate the secondary key constraint. */
    priX.put(new RelatedX());

    if (closeAndOpenNormally) {
      /* Open normally and attempt to populate the secondary. */
      close();
      open();
      if (isTransactional && DbCompat.POPULATE_ENFORCES_CONSTRAINTS) {
        /* Constraint enforcement requires transactions. */
        try {
          /* Before adding the foreign key, constraint is violated. */
          priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
          fail();
        } catch (DatabaseException e) {
          assertTrue(e.toString(), e.toString().contains("foreign key not allowed"));
        }
      }
      /* Open priX with SecondaryBulkLoad=true. */
      close();
      open(config);
      /* Add the foreign key to avoid the constraint error. */
      priY = store.getPrimaryIndex(Integer.class, RelatedY.class);
      priY.put(new RelatedY());
      /* Open normally and the secondary will be populated. */
      close();
      open();
      priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
      PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedX.class.getName(), "key2");
      secX = store.getSecondaryIndex(priX, Integer.class, "key2");
    } else {
      /* Get secondary index explicitly and it will be populated. */
      if (isTransactional && DbCompat.POPULATE_ENFORCES_CONSTRAINTS) {
        /* Constraint enforcement requires transactions. */
        try {
          /* Before adding the foreign key, constraint is violated. */
          secX = store.getSecondaryIndex(priX, Integer.class, "key2");
          fail();
        } catch (DatabaseException e) {
          assertTrue(e.toString(), e.toString().contains("foreign key not allowed"));
        }
      }
      /* Add the foreign key. */
      priY = store.getPrimaryIndex(Integer.class, RelatedY.class);
      priY.put(new RelatedY());
      secX = store.getSecondaryIndex(priX, Integer.class, "key2");
      PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedX.class.getName(), "key2");
    }

    RelatedX x = secX.get(88);
    assertNotNull(x);
    close();
  }