示例#1
0
  @Test
  public void testCreateDeleteIndexed() throws Exception {
    idbm.wipeDatabase();

    Random r = new Random(1L);
    List<TestThrift> tts = new ArrayList<TestThrift>();
    for (int test = 0; test < 1000; test++) {
      TestThrift tt =
          new TestThrift(
              null,
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      idbm.create(tt);
      tts.add(tt);

      idbm.commit();
    }

    for (TestThrift tt : tts) {
      int ids = idbm.secondaryGet(tt.getClass(), "id", tt.id).size();
      int bs = idbm.secondaryGet(tt.getClass(), "b", tt.b).size();
      ComparableAssert.assertGreater(0, ids);
      ComparableAssert.assertGreater(0, bs);

      TestThrift ttModified =
          new TestThrift(
              tt.id,
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      idbm.delete(ttModified);
      idbm.commit();

      int ids2 = idbm.secondaryGet(tt.getClass(), "id", tt.id).size();
      int bs2 = idbm.secondaryGet(tt.getClass(), "b", tt.b).size();

      Assert.assertEquals(ids - 1, ids2);
      Assert.assertEquals(bs - 1, bs2);
    }

    idbm.wipeDatabase();
  }
示例#2
0
  @Test
  public void testSerializeIndexed() throws Exception {
    idbm.wipeDatabase();
    Random r = new Random(1L);
    Set<TestThrift> tts = new HashSet<TestThrift>();
    for (int test = 0; test < 10000; test++) {
      TestThrift tt =
          new TestThrift(
              null,
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      tts.add(tt);
      idbm.create(tt);
      idbm.commit();
    }

    idbm.dump(TestThrift.class, new File("."));
    idbm.wipeDatabase();

    idbm.load(TestThrift.class, new File("."));
    for (TestThrift tt : tts) {
      TestThrift tt2 = idbm.get(TestThrift.class, tt.id);
      Assert.assertEquals(tt, tt2);
    }

    new File("TestThrift.zombiedb.bz2").delete();
  }
示例#3
0
  @Test
  public void testCreateIndexedSpeed() throws Exception {
    idbm.wipeDatabase();

    Random r = new Random(1L);
    for (int test = 0; test < 1000; test++) {
      TestThrift tt =
          new TestThrift(
              null,
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      idbm.create(tt);

      idbm.commit();
    }

    for (int test = 0; test < 1000; test++) {
      // NOTE(jgauci): We never have collisions with the ids above
      TestThrift tt =
          new TestThrift(
              String.valueOf(100000 + r.nextInt()),
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      idbm.createWithId(tt);

      idbm.commit();
    }

    idbm.wipeDatabase();
  }
示例#4
0
 // All connections fight to update the same indexed object
 @Test
 public void testAtomicUpdate() throws Exception {
   idbm.wipeDatabase();
   final Random r = new Random(1L);
   final TestThrift tt =
       new TestThrift(
           "abcd",
           r.nextInt(),
           r.nextLong(),
           r.nextBoolean(),
           (byte) r.nextInt(),
           (short) r.nextInt(),
           r.nextDouble(),
           generateString(r, 16),
           "abc");
   ExecutorService threadPool = Executors.newFixedThreadPool(concurrentConnections.size());
   List<Future<?>> futures = new ArrayList<Future<?>>();
   for (int a = 0; a < concurrentConnections.size(); a++) {
     final IndexedDatabaseEngineManager idbmLocal = concurrentConnections.get(a).idbm;
     Future<?> future =
         threadPool.submit(
             new Runnable() {
               @Override
               public void run() {
                 try {
                   idbmLocal.register(TestThrift.class);
                 } catch (IOException e1) {
                   throw new RuntimeException(e1);
                 }
                 for (int b = 0; b < 1000; b++) {
                   TestThrift ttInner = null;
                   synchronized (tt) {
                     ttInner = new TestThrift(tt);
                     ttInner.b = r.nextBoolean();
                     ttInner.i = r.nextInt();
                     ttInner.st = generateString(r, 16);
                   }
                   try {
                     idbmLocal.upsert(ttInner);
                   } catch (IOException e) {
                     throw new RuntimeException(e);
                   }
                 }
               }
             });
     futures.add(future);
   }
   threadPool.shutdown();
   boolean timedOut = true;
   while (timedOut) {
     timedOut = false;
     for (Future<?> future : futures) {
       try {
         if (future.get(1, TimeUnit.SECONDS) != null) {
           Assert.fail("FUTURE FAILED");
         }
       } catch (TimeoutException e) {
         timedOut = true;
       }
     }
   }
   TestThrift tt2 = idbm.get(tt.getClass(), tt.id);
   Assert.assertEquals(1, idbm.secondaryGet(tt2.getClass(), "id", tt2.id).size());
   Assert.assertEquals(1, idbm.secondaryGet(tt2.getClass(), "b", tt2.b).size());
   Assert.assertEquals(1, idbm.secondaryGet(tt2.getClass(), "i", tt2.i).size());
   Assert.assertEquals(1, idbm.secondaryGet(tt2.getClass(), "st", tt2.st).size());
 }
示例#5
0
  @Test
  public void testCreateIndexed() throws Exception {
    idbm.wipeDatabase();

    Random r = new Random(1L);
    for (int test = 0; test < 100; test++) {
      TestThrift tt =
          new TestThrift(
              null,
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      idbm.create(tt);
      idbm.commit();

      // System.out.println(tt);
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "id", tt.id)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "i", tt.i)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "l", tt.l)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "b", tt.b)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "by", tt.by)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "s", tt.s)));
      try {
        idbm.secondaryGet(tt.getClass(), "d", tt.d);
        Assert.fail("Expected an exception");
      } catch (IOException e) {
      }
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "st", tt.st)));
      try {
        idbm.secondaryGet(tt.getClass(), "notIndexedString", tt.notIndexedString);
        Assert.fail("Expected an exception");
      } catch (IOException e) {
      }
    }

    for (int test = 0; test < 100; test++) {
      // NOTE(jgauci): We never have collisions with the ids above
      TestThrift tt =
          new TestThrift(
              String.valueOf(100000 + r.nextInt(100000)),
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              generateString(r, 16),
              "abc");
      idbm.createWithId(tt);
      idbm.commit();

      // System.out.println(tt);
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "id", tt.id)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "i", tt.i)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "l", tt.l)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "b", tt.b)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "by", tt.by)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "s", tt.s)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "st", tt.st)));
      try {
        idbm.secondaryGet(tt.getClass(), "d", tt.d);
        Assert.fail("Expected an exception");
      } catch (IOException e) {
      }
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "st", tt.st)));
      try {
        idbm.secondaryGet(tt.getClass(), "notIndexedString", tt.notIndexedString);
        Assert.fail("Expected an exception");
      } catch (IOException e) {
      }
    }

    for (int test = 0; test < 100; test++) {
      // NOTE(jgauci): We never have collisions with the ids above
      TestThrift tt =
          new TestThrift(
              String.valueOf(200000 + r.nextInt(100000)),
              r.nextInt(),
              r.nextLong(),
              r.nextBoolean(),
              (byte) r.nextInt(),
              (short) r.nextInt(),
              r.nextDouble(),
              null,
              "abc");
      idbm.createWithId(tt);
      idbm.commit();

      // System.out.println(tt);
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "id", tt.id)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "i", tt.i)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "l", tt.l)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "b", tt.b)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "by", tt.by)));
      Assert.assertEquals(tt, getLast(idbm.secondaryGet(tt.getClass(), "s", tt.s)));
      try {
        idbm.secondaryGet(tt.getClass(), "d", tt.d);
        Assert.fail("Expected an exception");
      } catch (IOException e) {
      }
      Assert.assertEquals(0, idbm.secondaryGet(tt.getClass(), "st", tt.st).size());
      try {
        idbm.secondaryGet(tt.getClass(), "notIndexedString", tt.notIndexedString);
        Assert.fail("Expected an exception");
      } catch (IOException e) {
      }
    }

    idbm.wipeDatabase();
  }