Ejemplo n.º 1
0
  public void testBadSequenceKeys() throws DatabaseException {

    open();
    try {
      PrimaryIndex<Boolean, BadSequenceKeyEntity1> index =
          store.getPrimaryIndex(Boolean.class, BadSequenceKeyEntity1.class);
      fail();
    } catch (IllegalArgumentException expected) {
      assertTrue(expected.getMessage().indexOf("Type not allowed for sequence") >= 0);
    }
    try {
      PrimaryIndex<BadSequenceKeyEntity2.Key, BadSequenceKeyEntity2> index =
          store.getPrimaryIndex(BadSequenceKeyEntity2.Key.class, BadSequenceKeyEntity2.class);
      fail();
    } catch (IllegalArgumentException expected) {
      assertTrue(expected.getMessage().indexOf("Type not allowed for sequence") >= 0);
    }
    try {
      PrimaryIndex<BadSequenceKeyEntity3.Key, BadSequenceKeyEntity3> index =
          store.getPrimaryIndex(BadSequenceKeyEntity3.Key.class, BadSequenceKeyEntity3.class);
      fail();
    } catch (IllegalArgumentException expected) {
      assertTrue(
          expected
                  .getMessage()
                  .indexOf(
                      "A composite key class used with a sequence may contain "
                          + "only a single integer key field")
              >= 0);
    }
    close();
  }
Ejemplo n.º 2
0
  public void testSeparateSequence() throws DatabaseException {

    open();

    PrimaryIndex<Integer, SeparateSequenceEntity1> priIndex1 =
        store.getPrimaryIndex(Integer.class, SeparateSequenceEntity1.class);

    PrimaryIndex<Integer, SeparateSequenceEntity2> priIndex2 =
        store.getPrimaryIndex(Integer.class, SeparateSequenceEntity2.class);

    Transaction txn = txnBegin();
    SeparateSequenceEntity1 e1 = new SeparateSequenceEntity1();
    SeparateSequenceEntity2 e2 = new SeparateSequenceEntity2();
    priIndex1.put(txn, e1);
    assertEquals(1, e1.key);
    priIndex2.putNoOverwrite(txn, e2);
    assertEquals(Integer.valueOf(1), e2.key);
    e1.key = 0;
    priIndex1.putNoOverwrite(txn, e1);
    assertEquals(2, e1.key);
    e2.key = null;
    priIndex2.put(txn, e2);
    assertEquals(Integer.valueOf(2), e2.key);
    txnCommit(txn);

    close();
  }
Ejemplo n.º 3
0
  /**
   * Same as testPutEntitySubclassWithGetSubclassIndex2 but store the first instance of the subclass
   * after closing and reopening the store, *without* calling getSubclassIndex. This ensures that a
   * single call to getSubclassIndex is sufficient and subsequent use of the store does not require
   * it. [#16399]
   */
  public void testPutEntitySubclassWithGetSubclassIndex2() throws DatabaseException {

    open();

    PrimaryIndex<Long, Statement> pri = store.getPrimaryIndex(Long.class, Statement.class);

    SecondaryIndex<String, Long, ExtendedStatement> sec =
        store.getSubclassIndex(pri, ExtendedStatement.class, String.class, "name");

    Transaction txn = txnBegin();
    pri.put(txn, new Statement(1));
    txnCommit(txn);

    close();
    open();

    pri = store.getPrimaryIndex(Long.class, Statement.class);

    txn = txnBegin();
    pri.put(txn, new ExtendedStatement(2, "abc"));
    txnCommit(txn);

    sec = store.getSubclassIndex(pri, ExtendedStatement.class, String.class, "name");

    ExtendedStatement o = sec.get("abc");
    assertNotNull(o);
    assertEquals(2, o.id);

    close();
  }
Ejemplo n.º 4
0
  public void testCursorCount() throws DatabaseException {

    open();

    PrimaryIndex<Integer, MyEntity> priIndex = store.getPrimaryIndex(Integer.class, MyEntity.class);

    SecondaryIndex<Integer, Integer, MyEntity> secIndex =
        store.getSecondaryIndex(priIndex, Integer.class, "secKey");

    Transaction txn = txnBeginCursor();

    MyEntity e = new MyEntity();
    e.priKey = 1;
    e.secKey = 1;
    priIndex.put(txn, e);

    EntityCursor<MyEntity> cursor = secIndex.entities(txn, null);
    cursor.next();
    assertEquals(1, cursor.count());
    cursor.close();

    e.priKey = 2;
    priIndex.put(txn, e);
    cursor = secIndex.entities(txn, null);
    cursor.next();
    assertEquals(2, cursor.count());
    cursor.close();

    txnCommit(txn);
    close();
  }
Ejemplo n.º 5
0
  public void testCustomCompare() throws DatabaseException {

    open();

    PrimaryIndex<ReverseIntKey, CustomCompareEntity> priIndex =
        store.getPrimaryIndex(ReverseIntKey.class, CustomCompareEntity.class);

    SecondaryIndex<ReverseIntKey, ReverseIntKey, CustomCompareEntity> secIndex1 =
        store.getSecondaryIndex(priIndex, ReverseIntKey.class, "secKey1");

    SecondaryIndex<ReverseIntKey, ReverseIntKey, CustomCompareEntity> secIndex2 =
        store.getSecondaryIndex(priIndex, ReverseIntKey.class, "secKey2");

    Transaction txn = txnBegin();
    for (int i = 1; i <= 5; i += 1) {
      assertTrue(priIndex.putNoOverwrite(txn, new CustomCompareEntity(i)));
    }
    txnCommit(txn);

    txn = txnBeginCursor();
    EntityCursor<CustomCompareEntity> c = priIndex.entities(txn, null);
    for (int i = 5; i >= 1; i -= 1) {
      CustomCompareEntity e = c.next();
      assertNotNull(e);
      assertEquals(new ReverseIntKey(i), e.key);
    }
    c.close();
    txnCommit(txn);

    txn = txnBeginCursor();
    c = secIndex1.entities(txn, null);
    for (int i = -1; i >= -5; i -= 1) {
      CustomCompareEntity e = c.next();
      assertNotNull(e);
      assertEquals(new ReverseIntKey(-i), e.key);
      assertEquals(new ReverseIntKey(i), e.secKey1);
    }
    c.close();
    txnCommit(txn);

    txn = txnBeginCursor();
    c = secIndex2.entities(txn, null);
    for (int i = -1; i >= -5; i -= 1) {
      CustomCompareEntity e = c.next();
      assertNotNull(e);
      assertEquals(new ReverseIntKey(-i), e.key);
      assertTrue(e.secKey2.contains(new ReverseIntKey(i)));
    }
    c.close();
    txnCommit(txn);

    close();
  }
Ejemplo n.º 6
0
  /** 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");
  }
Ejemplo n.º 7
0
  // Open the indices
  public SimpleDA(EntityStore store) throws DatabaseException {

    // Primary key for SimpleEntityClass classes
    pIdx = store.getPrimaryIndex(String.class, SimpleEntityClass.class);

    // Secondary key for SimpleEntityClass classes
    // Last field in the getSecondaryIndex() method must be
    // the name of a class member; in this case, an
    // SimpleEntityClass.class data member.
    sIdx = store.getSecondaryIndex(pIdx, String.class, "sKey");

    sec_pcursor = pIdx.entities();
    sec_scursor = sIdx.subIndex("skeyone").entities();
  }
Ejemplo n.º 8
0
  /**
   * When opening an X_TO_MANY secondary that has a persistent key class, the key class was not
   * recognized as being persistent if it was never before referenced when getSecondaryIndex was
   * called. This was a bug in JE 3.0.12, reported on OTN. [#15103]
   */
  public void testToManyKeyClass() throws DatabaseException {

    open();

    PrimaryIndex<Integer, ToManyKeyEntity> priIndex =
        store.getPrimaryIndex(Integer.class, ToManyKeyEntity.class);
    SecondaryIndex<ToManyKey, Integer, ToManyKeyEntity> secIndex =
        store.getSecondaryIndex(priIndex, ToManyKey.class, "key2");

    priIndex.put(new ToManyKeyEntity());
    secIndex.get(new ToManyKey());

    close();
  }
Ejemplo n.º 9
0
  /**
   * Checks that we get an appropriate exception when storing an entity subclass instance, which
   * contains a secondary key, without registering the subclass up front. [#16399]
   */
  public void testPutEntitySubclassWithoutRegisterClass() throws DatabaseException {

    open();

    final PrimaryIndex<Long, Statement> pri = store.getPrimaryIndex(Long.class, Statement.class);

    final Transaction txn = txnBegin();
    pri.put(txn, new Statement(1));
    try {
      pri.put(txn, new ExtendedStatement(2, null));
      fail();
    } catch (IllegalArgumentException expected) {
      assertTrue(
          expected.toString(),
          expected
              .getMessage()
              .contains(
                  "Entity subclasses defining a secondary key must be "
                      + "registered by calling EntityModel.registerClass or "
                      + "EntityStore.getSubclassIndex before storing an instance "
                      + "of the subclass: "
                      + ExtendedStatement.class.getName()));
    }
    txnAbort(txn);

    close();
  }
    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;
    }
Ejemplo n.º 11
0
  void closeStore() throws DatabaseException {

    if (store != null) {
      store.close();
      store = null;
    }
  }
Ejemplo n.º 12
0
  void closeNewStore() throws DatabaseException {

    if (newStore != null) {
      newStore.close();
      newStore = null;
    }
  }
Ejemplo n.º 13
0
  @Override
  public String saveDocument(
      DsSessionDto aSessionDto,
      DsDocumentDto aDocumentDto,
      String aKey,
      boolean aKeyIsNumeric,
      String aValue,
      String aSecurityPolicyName,
      String aContentFileKey) {

    if ("bug".equals(aSessionDto.getUsername())) {
      throw new IllegalStateException("this is a bug");
    }

    Transaction tx = entityStore.getEnvironment().beginTransaction(null, null);

    Document doc = new Document();
    doc.setFileKey(aContentFileKey);

    doc.setFileName(getFileName(aDocumentDto));
    documentPrimaryIndex.put(doc);
    tx.commit();

    return String.valueOf(doc.getId());
  }
Ejemplo n.º 14
0
  /** Closes the store. */
  private void close() throws DatabaseException {

    store.close();
    store = null;
    rawStore.close();
    rawStore = null;
  }
Ejemplo n.º 15
0
  public void testCompositeSequence() throws DatabaseException {

    open();

    PrimaryIndex<CompositeSequenceEntity1.Key, CompositeSequenceEntity1> priIndex1 =
        store.getPrimaryIndex(CompositeSequenceEntity1.Key.class, CompositeSequenceEntity1.class);

    PrimaryIndex<CompositeSequenceEntity2.Key, CompositeSequenceEntity2> priIndex2 =
        store.getPrimaryIndex(CompositeSequenceEntity2.Key.class, CompositeSequenceEntity2.class);

    Transaction txn = txnBegin();
    CompositeSequenceEntity1 e1 = new CompositeSequenceEntity1();
    CompositeSequenceEntity2 e2 = new CompositeSequenceEntity2();
    priIndex1.put(txn, e1);
    assertEquals(1, e1.key.key);
    priIndex2.putNoOverwrite(txn, e2);
    assertEquals(Integer.valueOf(1), e2.key.key);
    e1.key = null;
    priIndex1.putNoOverwrite(txn, e1);
    assertEquals(2, e1.key.key);
    e2.key = null;
    priIndex2.put(txn, e2);
    assertEquals(Integer.valueOf(2), e2.key.key);
    txnCommit(txn);

    EntityCursor<CompositeSequenceEntity1> c1 = priIndex1.entities();
    e1 = c1.next();
    assertEquals(2, e1.key.key);
    e1 = c1.next();
    assertEquals(1, e1.key.key);
    e1 = c1.next();
    assertNull(e1);
    c1.close();

    EntityCursor<CompositeSequenceEntity2> c2 = priIndex2.entities();
    e2 = c2.next();
    assertEquals(Integer.valueOf(2), e2.key.key);
    e2 = c2.next();
    assertEquals(Integer.valueOf(1), e2.key.key);
    e2 = c2.next();
    assertNull(e2);
    c2.close();

    close();
  }
Ejemplo n.º 16
0
  @Override
  public void deleteDocument(DsSessionDto aSessionDto, String aDocumentId) {

    if ("bug".equals(aSessionDto.getUsername())) {
      throw new IllegalStateException("this is a bug");
    }

    Transaction tx = entityStore.getEnvironment().beginTransaction(null, null);
    documentPrimaryIndex.delete(Long.valueOf(aDocumentId));
    tx.commit();
  }
Ejemplo n.º 17
0
  public void testBadKeyClass1() throws DatabaseException {

    open();
    try {
      PrimaryIndex<BadKeyClass1, UseBadKeyClass1> index =
          store.getPrimaryIndex(BadKeyClass1.class, UseBadKeyClass1.class);
      fail();
    } catch (IllegalArgumentException expected) {
      assertTrue(expected.getMessage().indexOf("@KeyField") >= 0);
    }
    close();
  }
Ejemplo n.º 18
0
  public void testReadOnly() throws DatabaseException {

    open();
    PrimaryIndex<Integer, SharedSequenceEntity1> priIndex =
        store.getPrimaryIndex(Integer.class, SharedSequenceEntity1.class);
    Transaction txn = txnBegin();
    SharedSequenceEntity1 e = new SharedSequenceEntity1();
    priIndex.put(txn, e);
    assertEquals(1, e.key);
    txnCommit(txn);
    close();

    /*
     * Check that we can open the store read-only and read the records
     * written above.
     */
    openReadOnly();
    priIndex = store.getPrimaryIndex(Integer.class, SharedSequenceEntity1.class);
    e = priIndex.get(1);
    assertNotNull(e);
    close();
  }
Ejemplo n.º 19
0
  @Override
  public void tearDown() throws Exception {

    if (store != null) {
      try {
        store.close();
      } catch (Throwable e) {
        System.out.println("tearDown: " + e);
      }
      store = null;
    }
    super.tearDown();
  }
  /** Closes all files and mediator classes related to the persistent hash table */
  public void close() {
    try {
      if (this.myDbEnvironment != null) {
        this.myDbEnvironment.cleanLog(); // Clean the log before closing
        dictStore.close();
        this.myDbEnvironment.close();
      }

      this.myDbEnvironment = null;
      this.dictStore = null;
      this.pidx = null;
    } catch (DatabaseException dbe) {
    }
  }
Ejemplo n.º 21
0
  /** 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);
  }
Ejemplo n.º 22
0
  public void testUninitializedCursor() throws DatabaseException {

    open();

    PrimaryIndex<Integer, MyEntity> priIndex = store.getPrimaryIndex(Integer.class, MyEntity.class);

    Transaction txn = txnBeginCursor();

    MyEntity e = new MyEntity();
    e.priKey = 1;
    e.secKey = 1;
    priIndex.put(txn, e);

    EntityCursor<MyEntity> entities = priIndex.entities(txn, null);
    try {
      entities.nextDup();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.prevDup();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.current();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.delete();
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.update(e);
      fail();
    } catch (IllegalStateException expected) {
    }
    try {
      entities.count();
      fail();
    } catch (IllegalStateException expected) {
    }

    entities.close();
    txnCommit(txn);
    close();
  }
Ejemplo n.º 23
0
  /**
   * When Y is opened and X has a key with relatedEntity=Y.class, X should be opened automatically.
   * If X is not opened, foreign key constraints will not be enforced. [#15358]
   */
  public void testAutoOpenRelatedEntity() throws DatabaseException {

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

    /* Opening X should create (and open) Y and enforce constraints. */
    open();
    priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
    PersistTestUtils.assertDbExists(true, env, STORE_NAME, RelatedY.class.getName(), null);
    if (isTransactional) {
      /* Constraint enforcement requires transactions. */
      try {
        priX.put(new RelatedX());
        fail();
      } catch (DatabaseException e) {
        assertTrue(
            "" + e.getMessage(),
            (e.getMessage().indexOf("foreign key not allowed: it is not present") >= 0)
                || (e.getMessage().indexOf("DB_FOREIGN_CONFLICT") >= 0));
      }
    }
    priY = store.getPrimaryIndex(Integer.class, RelatedY.class);
    priY.put(new RelatedY());
    priX.put(new RelatedX());
    close();

    /* Delete should cascade even when X is not opened explicitly. */
    open();
    priY = store.getPrimaryIndex(Integer.class, RelatedY.class);
    assertEquals(1, priY.count());
    priY.delete(88);
    assertEquals(0, priY.count());
    priX = store.getPrimaryIndex(Integer.class, RelatedX.class);
    assertEquals(0, priX.count()); /* Failed prior to [#15358] fix. */
    close();
  }
Ejemplo n.º 24
0
  public void testKeyName() throws DatabaseException {

    open();

    PrimaryIndex<Long, BookEntity> pri1 = store.getPrimaryIndex(Long.class, BookEntity.class);
    PrimaryIndex<Long, AuthorEntity> pri2 = store.getPrimaryIndex(Long.class, AuthorEntity.class);

    BookEntity book = new BookEntity();
    pri1.put(book);
    AuthorEntity author = new AuthorEntity();
    author.bookIds.add(book.bookId);
    pri2.put(author);

    close();

    open();
    pri1 = store.getPrimaryIndex(Long.class, BookEntity.class);
    pri2 = store.getPrimaryIndex(Long.class, AuthorEntity.class);
    book = pri1.get(1L);
    assertNotNull(book);
    author = pri2.get(1L);
    assertNotNull(author);
    close();
  }
Ejemplo n.º 25
0
  /**
   * When opening read-only, secondaries are not opened when the primary is opened, causing a
   * different code path to be used for opening secondaries. For a RawStore in particular, this
   * caused an unreported NullPointerException in JE 3.0.12. No SR was created because the use case
   * is very obscure and was discovered by code inspection.
   */
  public void testOpenRawStoreReadOnly() throws DatabaseException {

    open();
    store.getPrimaryIndex(Integer.class, MyEntity.class);
    close();

    StoreConfig config = new StoreConfig();
    config.setReadOnly(true);
    config.setTransactional(envConfig.getTransactional());
    RawStore rawStore = new RawStore(env, "test", config);

    String clsName = MyEntity.class.getName();
    rawStore.getSecondaryIndex(clsName, "secKey");

    rawStore.close();
  }
Ejemplo n.º 26
0
  /** A proxied object may not current contain a field that references the parent proxy. [#15815] */
  public void testProxyNestedRef() throws DatabaseException {

    open();
    PrimaryIndex<Integer, ProxyNestedRef> index =
        store.getPrimaryIndex(Integer.class, ProxyNestedRef.class);
    ProxyNestedRef entity = new ProxyNestedRef();
    entity.list.add(entity.list);
    try {
      index.put(entity);
      fail();
    } catch (IllegalArgumentException expected) {
      assertTrue(
          expected.getMessage().indexOf("Cannot embed a reference to a proxied object") >= 0);
    }
    close();
  }
Ejemplo n.º 27
0
  /** Disallow primary keys on entity subclasses. [#15757] */
  public void testEntitySubclassWithPrimaryKey() throws DatabaseException {

    open();
    PrimaryIndex<Integer, EntitySuperClass> index =
        store.getPrimaryIndex(Integer.class, EntitySuperClass.class);
    EntitySuperClass e1 = new EntitySuperClass(1, "one");
    index.put(e1);
    assertEquals(e1, index.get(1));
    EntitySubClass e2 = new EntitySubClass(2, "two", "foo", 9);
    try {
      index.put(e2);
    } catch (IllegalArgumentException e) {
      assertTrue(e.getMessage().contains("PrimaryKey may not appear on an Entity subclass"));
    }
    assertEquals(e1, index.get(1));
    close();
  }
Ejemplo n.º 28
0
  public void testPersistentFields() throws DatabaseException {

    open();
    PrimaryIndex<Integer, PersistentFields> pri =
        store.getPrimaryIndex(Integer.class, PersistentFields.class);
    PersistentFields o1 = new PersistentFields(-1, 1, 2, 3, 4, 5, 6);
    assertNull(pri.put(o1));
    PersistentFields o2 = pri.get(-1);
    assertNotNull(o2);
    assertEquals(0, o2.transient1);
    assertEquals(0, o2.transient2);
    assertEquals(0, o2.transient3);
    assertEquals(4, o2.persistent1);
    assertEquals(5, o2.persistent2);
    assertEquals(6, o2.persistent3);
    close();
  }
  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);
  }
    public void close() {
      if (store != null) {
        try {
          store.close();
          store = null;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }

      if (env != null) {
        try {
          env.close();
          env = null;
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }