public synchronized void closeOpenedCursors() {
   try {
     for (Map.Entry<EntityCursor<?>, List<StackTraceElement>> e : openedCursors.entrySet()) {
       EntityCursor<?> cur = e.getKey();
       logger.warn("Opened cursor:" + cur);
       if (e.getValue() != null) {
         for (StackTraceElement ste : e.getValue())
           logger.debug(
               "\tat "
                   + ste.getClassName()
                   + "."
                   + ste.getMethodName()
                   + "("
                   + ste.getFileName()
                   + ":"
                   + ste.getLineNumber()
                   + ")");
       }
       cur.close();
     }
     openedCursors.clear();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
  // Shows all the inventory items that exist for a given
  // inventory name.
  private void showItem() throws DatabaseException {

    // Use the inventory name secondary key to retrieve
    // these objects.
    EntityCursor<Inventory> items = da.inventoryByName.subIndex(locateItem).entities();
    try {
      for (Inventory item : items) {
        displayInventoryRecord(item);
      }
    } finally {
      items.close();
    }
  }
  // Displays all the inventory items in the store
  private void showAllInventory() throws DatabaseException {

    // Get a cursor that will walk every
    // inventory object in the store.
    EntityCursor<Inventory> items = da.inventoryBySku.entities();

    try {
      for (Inventory item : items) {
        displayInventoryRecord(item);
      }
    } finally {
      items.close();
    }
  }
 @Override
 public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
   for (Enumeration<String> enumeration = httpSessionEvent.getSession().getAttributeNames();
       enumeration.hasMoreElements(); ) {
     String key = enumeration.nextElement();
     if (key.startsWith("cursor.")) {
       EntityCursor cursor = (EntityCursor) httpSessionEvent.getSession().getAttribute(key);
       try {
         cursor.close();
       } catch (Exception e) {
         e.printStackTrace();
       }
     }
   }
 }
 public synchronized <V> long countEstimate(EntityCursor<V> cursor) {
   try {
     return cursor.countEstimate();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
 public synchronized <V> V prevNoDup(EntityCursor<V> cursor) {
   try {
     return cursor.prevNoDup();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
 public synchronized <V> boolean update(EntityCursor<V> cursor, V value) {
   try {
     return cursor.update(value);
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
 public synchronized <V> int count(EntityCursor<V> cursor) {
   try {
     logger.trace("Warning: slow iterative cursor.count()");
     return cursor.count();
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
Beispiel #9
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();
  }
Beispiel #10
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();
  }
 public synchronized <V> EntityCursor<V> dup(EntityCursor<V> cursor) {
   try {
     EntityCursor<V> other = cursor.dup();
     addOpenedCursor(other);
     return other;
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
 public synchronized void close(EntityCursor<?> cursor) {
   try {
     if (openedCursors.containsKey(cursor)) {
       openedCursors.remove(cursor);
       cursor.close();
     }
   } catch (DatabaseException e) {
     throw getPersistenceManager().convertDatabaseException(e);
   }
 }
 /** Print all events covered by this cursor. */
 private void printEvents(EntityCursor<Event> eCursor) throws DatabaseException {
   try {
     for (Event e : eCursor) {
       System.out.println(e);
     }
   } finally {
     /* Be sure to close the cursor. */
     eCursor.close();
   }
 }
  private <K, V> void checkEmpty(EntityIndex<K, V> index) throws DatabaseException {

    Transaction txn = txnBeginCursor();
    EntityCursor<K> keys = index.keys(txn, null);
    assertNull(keys.next());
    assertTrue(!keys.iterator().hasNext());
    keys.close();
    EntityCursor<V> entities = index.entities(txn, null);
    assertNull(entities.next());
    assertTrue(!entities.iterator().hasNext());
    entities.close();
    txnCommit(txn);
  }
Beispiel #15
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();
  }
Beispiel #16
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();
  }
 /** Default constructor for the DNS resolver thread */
 public DnsResolver() {
   dnsCache = new ConcurrentHashMap<String, InetAddress>();
   urlsToResolve = new LinkedBlockingQueue<String>();
   CrawlerDrone.addLog("DNS Resolver starting......");
   resolutionIndex =
       CrawlerDrone.bdb.getPrimaryIndex(
           storage.BDBWrapper.DB_TO_RESOLVE_STORE, String.class, toResolveStore.class);
   if (resolutionIndex != null) {
     EntityCursor<toResolveStore> previousUnresolved = resolutionIndex.entities();
     for (toResolveStore item : previousUnresolved) {
       urlsToResolve.add(item.getPKey());
     }
     previousUnresolved.close();
     try {
       CrawlerDrone.bdb
           .getEnvironment()
           .truncateDatabase(null, storage.BDBWrapper.DB_TO_RESOLVE_STORE, false);
     } catch (DatabaseNotFoundException e) {
       // Nothing to restore
     }
   }
   CrawlerDrone.addLog("DNS Resolver started.");
 }
  private <T> void checkCursor(
      EntityCursor<T> cursor,
      Collection<T> collection,
      boolean collectionIsKeySet,
      List<List<Integer>> expected,
      Getter<T> getter)
      throws DatabaseException {

    boolean first;
    boolean firstDup;
    Iterator<T> iterator = collection.iterator();

    for (List<Integer> dups : expected) {
      for (int i : dups) {
        T o = cursor.next();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        /* Value iterator over duplicates. */
        if (!collectionIsKeySet) {
          assertTrue(iterator.hasNext());
          o = iterator.next();
          assertNotNull(o);
          assertEquals(i, getter.getKey(o));
        }
      }
    }

    first = true;
    for (List<Integer> dups : expected) {
      firstDup = true;
      for (int i : dups) {
        T o = first ? cursor.first() : (firstDup ? cursor.next() : cursor.nextDup());
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
        firstDup = false;
      }
    }

    first = true;
    for (List<Integer> dups : expected) {
      if (!dups.isEmpty()) {
        int i = dups.get(0);
        T o = first ? cursor.first() : cursor.nextNoDup();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        /* Key iterator over non-duplicates. */
        if (collectionIsKeySet) {
          assertTrue(iterator.hasNext());
          o = iterator.next();
          assertNotNull(o);
          assertEquals(i, getter.getKey(o));
        }
        first = false;
      }
    }

    List<List<Integer>> reversed = new ArrayList<List<Integer>>();
    for (List<Integer> dups : expected) {
      ArrayList<Integer> reversedDups = new ArrayList<Integer>(dups);
      Collections.reverse(reversedDups);
      reversed.add(reversedDups);
    }
    Collections.reverse(reversed);

    first = true;
    for (List<Integer> dups : reversed) {
      for (int i : dups) {
        T o = first ? cursor.last() : cursor.prev();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
      }
    }

    first = true;
    for (List<Integer> dups : reversed) {
      firstDup = true;
      for (int i : dups) {
        T o = first ? cursor.last() : (firstDup ? cursor.prev() : cursor.prevDup());
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
        firstDup = false;
      }
    }

    first = true;
    for (List<Integer> dups : reversed) {
      if (!dups.isEmpty()) {
        int i = dups.get(0);
        T o = first ? cursor.last() : cursor.prevNoDup();
        assertNotNull(o);
        assertEquals(i, getter.getKey(o));
        first = false;
      }
    }

    cursor.close();
  }
Beispiel #19
0
  /**
   * Ensures that custom comparators are persisted and work correctly during recovery. JE recovery
   * uses comparators, so they are serialized and stored in the DatabaseImpl. They are deserialized
   * during recovery prior to opening the EntityStore and its format catalog. But the formats are
   * needed by the comparator, so they are specially created when needed.
   *
   * <p>In particular we need to ensure that enum key fields work correctly, since their formats are
   * not static (like simple type formats are). [#17140]
   *
   * <p>Note that we don't need to actually cause a recovery in order to test the deserialization
   * and subsequent use of comparators. The JE DatabaseConfig.setBtreeComparator method serializes
   * and deserializes the comparator. The comparator is initialized on its first use, just as if
   * recovery were run.
   */
  public void testStoredComparators() throws DatabaseException {

    open();

    PrimaryIndex<StoredComparatorEntity.Key, StoredComparatorEntity> priIndex =
        store.getPrimaryIndex(StoredComparatorEntity.Key.class, StoredComparatorEntity.class);

    SecondaryIndex<
            StoredComparatorEntity.MyEnum, StoredComparatorEntity.Key, StoredComparatorEntity>
        secIndex = store.getSecondaryIndex(priIndex, StoredComparatorEntity.MyEnum.class, "secKey");

    final StoredComparatorEntity.Key[] priKeys =
        new StoredComparatorEntity.Key[] {
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.A, 1, StoredComparatorEntity.MyEnum.A),
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.A, 1, StoredComparatorEntity.MyEnum.B),
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.A, 2, StoredComparatorEntity.MyEnum.A),
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.A, 2, StoredComparatorEntity.MyEnum.B),
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.B, 1, StoredComparatorEntity.MyEnum.A),
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.B, 1, StoredComparatorEntity.MyEnum.B),
          new StoredComparatorEntity.Key(
              StoredComparatorEntity.MyEnum.C, 0, StoredComparatorEntity.MyEnum.C),
        };

    final StoredComparatorEntity.MyEnum[] secKeys =
        new StoredComparatorEntity.MyEnum[] {
          StoredComparatorEntity.MyEnum.C,
          StoredComparatorEntity.MyEnum.B,
          StoredComparatorEntity.MyEnum.A,
          null,
          StoredComparatorEntity.MyEnum.A,
          StoredComparatorEntity.MyEnum.B,
          StoredComparatorEntity.MyEnum.C,
        };

    assertEquals(priKeys.length, secKeys.length);
    final int nEntities = priKeys.length;

    Transaction txn = txnBegin();
    for (int i = 0; i < nEntities; i += 1) {
      priIndex.put(txn, new StoredComparatorEntity(priKeys[i], secKeys[i]));
    }
    txnCommit(txn);

    txn = txnBeginCursor();
    EntityCursor<StoredComparatorEntity> entities = priIndex.entities(txn, null);
    for (int i = nEntities - 1; i >= 0; i -= 1) {
      StoredComparatorEntity e = entities.next();
      assertNotNull(e);
      assertEquals(priKeys[i], e.key);
      assertEquals(secKeys[i], e.secKey);
    }
    assertNull(entities.next());
    entities.close();
    txnCommit(txn);

    txn = txnBeginCursor();
    entities = secIndex.entities(txn, null);
    for (StoredComparatorEntity.MyEnum myEnum :
        EnumSet.allOf(StoredComparatorEntity.MyEnum.class)) {
      for (int i = 0; i < nEntities; i += 1) {
        if (secKeys[i] == myEnum) {
          StoredComparatorEntity e = entities.next();
          assertNotNull(e);
          assertEquals(priKeys[i], e.key);
          assertEquals(secKeys[i], e.secKey);
        }
      }
    }
    assertNull(entities.next());
    entities.close();
    txnCommit(txn);

    close();
  }
Beispiel #20
0
  public void testCursorUpdate() 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();

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

    /* update() with primary entity cursor. */
    EntityCursor<MyEntity> entities = priIndex.entities(txn, null);
    e = entities.next();
    assertNotNull(e);
    assertEquals(1, e.priKey);
    assertEquals(Integer.valueOf(2), e.secKey);
    e.secKey = null;
    assertTrue(entities.update(e));
    e = entities.current();
    assertNotNull(e);
    assertEquals(1, e.priKey);
    assertEquals(null, e.secKey);
    e.secKey = 3;
    assertTrue(entities.update(e));
    e = entities.current();
    assertNotNull(e);
    assertEquals(1, e.priKey);
    assertEquals(Integer.valueOf(3), e.secKey);
    entities.close();

    /* update() with primary keys cursor. */
    EntityCursor<Integer> keys = priIndex.keys(txn, null);
    k = keys.next();
    assertNotNull(k);
    assertEquals(Integer.valueOf(1), k);
    try {
      keys.update(2);
      fail();
    } catch (UnsupportedOperationException expected) {
    }
    keys.close();

    /* update() with secondary entity cursor. */
    entities = secIndex.entities(txn, null);
    e = entities.next();
    assertNotNull(e);
    assertEquals(1, e.priKey);
    assertEquals(Integer.valueOf(3), e.secKey);
    try {
      entities.update(e);
      fail();
    } catch (UnsupportedOperationException expected) {
    } catch (IllegalArgumentException expectedForDbCore) {
    }
    entities.close();

    /* update() with secondary keys cursor. */
    keys = secIndex.keys(txn, null);
    k = keys.next();
    assertNotNull(k);
    assertEquals(Integer.valueOf(3), k);
    try {
      keys.update(k);
      fail();
    } catch (UnsupportedOperationException expected) {
    }
    keys.close();

    txnCommit(txn);
    close();
  }
Beispiel #21
0
  public void testCursorDelete() 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();

    /* delete() with primary and secondary entities cursor. */

    for (EntityIndex index : new EntityIndex[] {priIndex, secIndex}) {

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

      EntityCursor<MyEntity> cursor = index.entities(txn, null);

      e = cursor.next();
      assertNotNull(e);
      assertEquals(1, e.priKey);
      e = cursor.current();
      assertNotNull(e);
      assertEquals(1, e.priKey);
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      e = cursor.next();
      assertNotNull(e);
      assertEquals(2, e.priKey);
      e = cursor.current();
      assertNotNull(e);
      assertEquals(2, e.priKey);
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      e = cursor.next();
      assertNull(e);

      if (index == priIndex) {
        e = new MyEntity();
        e.priKey = 2;
        e.secKey = 1;
        assertTrue(!cursor.update(e));
      }

      cursor.close();
    }

    /* delete() with primary and secondary keys cursor. */

    for (EntityIndex index : new EntityIndex[] {priIndex, secIndex}) {

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

      EntityCursor<Integer> cursor = index.keys(txn, null);

      Integer k = cursor.next();
      assertNotNull(k);
      assertEquals(1, k.intValue());
      k = cursor.current();
      assertNotNull(k);
      assertEquals(1, k.intValue());
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      int expectKey = (index == priIndex) ? 2 : 1;
      k = cursor.next();
      assertNotNull(k);
      assertEquals(expectKey, k.intValue());
      k = cursor.current();
      assertNotNull(k);
      assertEquals(expectKey, k.intValue());
      assertTrue(cursor.delete());
      assertTrue(!cursor.delete());
      assertNull(cursor.current());

      k = cursor.next();
      assertNull(k);

      cursor.close();
    }

    txnCommit(txn);
    close();
  }
Beispiel #22
0
  public void testDeleteFromSubIndex() 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 = txnBegin();
    MyEntity e = new MyEntity();
    e.secKey = 1;
    e.priKey = 1;
    priIndex.put(txn, e);
    e.priKey = 2;
    priIndex.put(txn, e);
    e.priKey = 3;
    priIndex.put(txn, e);
    e.priKey = 4;
    priIndex.put(txn, e);
    txnCommit(txn);

    EntityIndex<Integer, MyEntity> subIndex = secIndex.subIndex(1);
    txn = txnBeginCursor();
    e = subIndex.get(txn, 1, null);
    assertEquals(1, e.priKey);
    assertEquals(Integer.valueOf(1), e.secKey);
    e = subIndex.get(txn, 2, null);
    assertEquals(2, e.priKey);
    assertEquals(Integer.valueOf(1), e.secKey);
    e = subIndex.get(txn, 3, null);
    assertEquals(3, e.priKey);
    assertEquals(Integer.valueOf(1), e.secKey);
    e = subIndex.get(txn, 5, null);
    assertNull(e);

    boolean deleted = subIndex.delete(txn, 1);
    assertTrue(deleted);
    assertNull(subIndex.get(txn, 1, null));
    assertNotNull(subIndex.get(txn, 2, null));

    EntityCursor<MyEntity> cursor = subIndex.entities(txn, null);
    boolean saw4 = false;
    for (MyEntity e2 = cursor.first(); e2 != null; e2 = cursor.next()) {
      if (e2.priKey == 3) {
        cursor.delete();
      }
      if (e2.priKey == 4) {
        saw4 = true;
      }
    }
    cursor.close();
    assertTrue(saw4);
    assertNull(subIndex.get(txn, 1, null));
    assertNull(subIndex.get(txn, 3, null));
    assertNotNull(subIndex.get(txn, 2, null));
    assertNotNull(subIndex.get(txn, 4, null));

    txnCommit(txn);
    close();
  }
Beispiel #23
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;
  }