public boolean setup(boolean readOnly) {
      boolean open = false;
      try {
        EnvironmentConfig envConfig = new EnvironmentConfig();
        envConfig.setReadOnly(readOnly);
        envConfig.setAllowCreate(!readOnly);

        env = new Environment(envHome, envConfig);

        StoreConfig storeConfig = new StoreConfig();
        storeConfig.setReadOnly(readOnly);
        storeConfig.setAllowCreate(!readOnly);

        store = new EntityStore(env, "EntityStore", storeConfig);

        objectBySid = store.getPrimaryIndex(String.class, TestObject.class);

        open = true;
      } catch (EnvironmentLockedException e) {
      } catch (Exception e) {
        e.printStackTrace();
        System.exit(-1);
      }

      return open;
    }
Beispiel #2
0
  private void open() throws DatabaseException {

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(envConfig.getAllowCreate());
    config.setTransactional(envConfig.getTransactional());

    store = new EntityStore(env, "test", config);
  }
  boolean openStoreReadWrite() throws Exception {

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(true);
    final boolean retVal = openStore(config);
    if (retVal) {
      caseObj.newMetadataWritten = true;
    }
    return retVal;
  }
  void openNewStore() throws Exception {

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(true);
    config.setTransactional(true);

    EntityModel model = new AnnotationModel();
    config.setModel(model);
    caseObj.configure(model, config);

    newStore = new EntityStore(env, "new", config);
  }
Beispiel #5
0
  private void open(Class clsToRegister) throws DatabaseException {

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(envConfig.getAllowCreate());
    if (clsToRegister != null) {
      com.sleepycat.persist.model.EntityModel model =
          new com.sleepycat.persist.model.AnnotationModel();
      model.registerClass(clsToRegister);
      config.setModel(model);
    }
    open(config);
  }
  /** Create all primary and secondary indices. */
  private void init() throws DatabaseException {

    /* Open a transactional entity store. */
    System.out.println("-> Creating a BDB database");
    StoreConfig storeConfig = new StoreConfig();
    storeConfig.setAllowCreate(true);
    storeConfig.setTransactional(true);
    store = new EntityStore(env, "ExampleStore", storeConfig);

    eventByTime = store.getPrimaryIndex(Date.class, Event.class);
    eventByPrice = store.getSecondaryIndex(eventByTime, Integer.class, "price");
  }
  private void openGroup() throws IOException {

    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setTransactional(true);
    envConfig.setAllowCreate(true);

    ReplicationConfig repConfig = new ReplicationConfig();
    repConfig.setConfigParam(RepParams.VLSN_LOG_CACHE_SIZE.getName(), "2");

    repEnvInfo = RepTestUtils.setupEnvInfos(envRoot, nNodes, envConfig, repConfig);
    master = RepTestUtils.joinGroup(repEnvInfo);

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(true);
    config.setTransactional(true);
    store = new EntityStore(master, "test", config);
    primaryIndex = store.getPrimaryIndex(Integer.class, AppData.class);
  }
  /** Opens the store. */
  private void open() throws DatabaseException {

    StoreConfig config = new StoreConfig();
    config.setAllowCreate(envConfig.getAllowCreate());
    config.setTransactional(envConfig.getTransactional());

    store = new EntityStore(env, "test", config);

    primary = store.getPrimaryIndex(Integer.class, MyEntity.class);
    oneToOne = store.getSecondaryIndex(primary, Integer.class, "oneToOne");
    manyToOne = store.getSecondaryIndex(primary, Integer.class, "manyToOne");
    oneToMany = store.getSecondaryIndex(primary, Integer.class, "oneToMany");
    manyToMany = store.getSecondaryIndex(primary, Integer.class, "manyToMany");

    assertNotNull(primary);
    assertNotNull(oneToOne);
    assertNotNull(manyToOne);
    assertNotNull(oneToMany);
    assertNotNull(manyToMany);

    rawStore = new RawStore(env, "test", config);
    String clsName = MyEntity.class.getName();
    entityType = rawStore.getModel().getRawType(clsName);
    assertNotNull(entityType);

    primaryRaw = rawStore.getPrimaryIndex(clsName);
    oneToOneRaw = rawStore.getSecondaryIndex(clsName, "oneToOne");
    manyToOneRaw = rawStore.getSecondaryIndex(clsName, "manyToOne");
    oneToManyRaw = rawStore.getSecondaryIndex(clsName, "oneToMany");
    manyToManyRaw = rawStore.getSecondaryIndex(clsName, "manyToMany");

    assertNotNull(primaryRaw);
    assertNotNull(oneToOneRaw);
    assertNotNull(manyToOneRaw);
    assertNotNull(oneToManyRaw);
    assertNotNull(manyToManyRaw);
  }
  /**
   * Load the index data from the input file and transfer it to the persistent hash table
   *
   * @param dbDir
   */
  public void setup(File dbDir) {
    try {
      // Setup needed classes for interacting with Berkeley DNB Java Edition
      EnvironmentConfig envConfig = new EnvironmentConfig();
      StoreConfig storeConfig = new StoreConfig();
      envConfig.setAllowCreate(true);
      // Try to keep a high amount of data in the cache.
      envConfig.setCachePercent(80);
      storeConfig.setAllowCreate(true);
      this.myDbEnvironment = new Environment(dbDir, envConfig);
      this.dictStore = new EntityStore(myDbEnvironment, "EntityStore", storeConfig);
      this.pidx = dictStore.getPrimaryIndex(String.class, KeyValueEntry.class);

      if (this.pidx.count() == 0) {
        // prep the index
        try {
          BufferedReader reader = new BufferedReader(new FileReader(this.indexFile));
          String line = null;
          while ((line = reader.readLine()) != null) {
            // de-serialize object from input file
            KeyValueEntry kvp = KeyValueEntry.fromString(line);
            // store in persistent hash table
            this.pidx.put(kvp);
          }
          reader.close();
        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
      }

    } catch (DatabaseException dbe) {
      this.myDbEnvironment = null;
      this.dictStore = null;
      this.pidx = null;
    }
  }
Beispiel #10
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();
  }
Beispiel #11
0
  public void testSubclassIndex() throws DatabaseException {

    EnvironmentConfig envConfig = new EnvironmentConfig();
    envConfig.setAllowCreate(true);
    env = new Environment(envHome, envConfig);

    StoreConfig storeConfig = new StoreConfig();
    storeConfig.setAllowCreate(true);
    EntityStore store = new EntityStore(env, "foo", storeConfig);

    PrimaryIndex<String, Employee> employeesById =
        store.getPrimaryIndex(String.class, Employee.class);

    employeesById.put(new Employee("1"));
    employeesById.put(new Manager("2", "a"));
    employeesById.put(new Manager("3", "a"));
    employeesById.put(new Manager("4", "b"));

    Employee e;
    Manager m;

    e = employeesById.get("1");
    assertNotNull(e);
    assertTrue(!(e instanceof Manager));

    /* Ensure DB exists BEFORE calling getSubclassIndex. [#15247] */
    PersistTestUtils.assertDbExists(true, env, "foo", Employee.class.getName(), "dept");

    /* Normal use: Subclass index for a key in the subclass. */
    SecondaryIndex<String, String, Manager> managersByDept =
        store.getSubclassIndex(employeesById, Manager.class, String.class, "dept");

    m = managersByDept.get("a");
    assertNotNull(m);
    assertEquals("2", m.id);

    m = managersByDept.get("b");
    assertNotNull(m);
    assertEquals("4", m.id);

    EntityCursor<Manager> managers = managersByDept.entities();
    try {
      m = managers.next();
      assertNotNull(m);
      assertEquals("2", m.id);
      m = managers.next();
      assertNotNull(m);
      assertEquals("3", m.id);
      m = managers.next();
      assertNotNull(m);
      assertEquals("4", m.id);
      m = managers.next();
      assertNull(m);
    } finally {
      managers.close();
    }

    /* Getting a subclass index for the entity class is also allowed. */
    store.getSubclassIndex(employeesById, Employee.class, String.class, "other");

    /* Getting a subclass index for a base class key is not allowed. */
    try {
      store.getSubclassIndex(employeesById, Manager.class, String.class, "other");
      fail();
    } catch (IllegalArgumentException expected) {
    }

    store.close();
    env.close();
    env = null;
  }