Пример #1
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();
  }
Пример #2
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();
  }
Пример #3
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();
  }
Пример #4
0
  private void run() throws DatabaseException {

    Random rand = new Random();

    /*
     * Create a set of events. Each insertion is a separate, auto-commit
     * transaction.
     */
    System.out.println("-> Inserting 4 events");
    eventByTime.put(new Event(makeDate(1), 100, "Company_A"));
    eventByTime.put(new Event(makeDate(2), 2, "Company_B"));
    eventByTime.put(new Event(makeDate(3), 20, "Company_C"));
    eventByTime.put(new Event(makeDate(4), 40, "CompanyD"));

    /* Load a whole set of events transactionally. */
    Transaction txn = env.beginTransaction(null, null);
    int maxPrice = 50;
    System.out.println("-> Inserting some randomly generated events");
    for (int i = 0; i < 25; i++) {
      Event e = new Event(makeDate(rand.nextInt(365)), rand.nextInt(maxPrice), "Company_X");
      if ((i % 2) == 0) {
        e.addRep("Bob");
        e.addRep("Nikunj");
      } else {
        e.addRep("Yongmin");
      }
      eventByTime.put(e);
    }
    txn.commitWriteNoSync();

    /*
     * Windows of events - display the events between June 1 and Aug 31
     */
    System.out.println("\n-> Display the events between June 1 and Aug 31");
    Date startDate = makeDate(Calendar.JUNE, 1);
    Date endDate = makeDate(Calendar.AUGUST, 31);

    EntityCursor<Event> eventWindow = eventByTime.entities(startDate, true, endDate, true);
    printEvents(eventWindow);

    /*
     * Display all events, ordered by a secondary index on price.
     */
    System.out.println("\n-> Display all events, ordered by price");
    EntityCursor<Event> byPriceEvents = eventByPrice.entities();
    printEvents(byPriceEvents);
  }
 /** 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.");
 }
Пример #6
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();
  }
Пример #7
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();
  }