@Test
  public void testLoadSave() throws IOException {
    Database db = new Database("test", "test.dbl");
    Table t1 = new Table("tname");

    t1.addColumn("A", CharField.class);
    t1.addColumn("B", DateField.class);
    Row r1 = t1.getRowSkeleton();

    Calendar calendar = Calendar.getInstance();
    calendar.set(70, 0, 0);
    Date date = calendar.getTime();

    r1.insert(1, Cell.makeCell(date));
    r1.insert(0, Cell.makeCell("text0"));

    t1.addRow(r1);
    db.addTable(t1);

    db.saveToStorage();
    db = new Database("test", "test.dbl");
    db.loadFromStorage();

    db.loadFromStorage();
    System.out.println(db);
    System.out.println(t1);

    assertTrue(db.getTable("tname").getHeader().equalsModel(t1.getHeader()));
    assertTrue(db.getTable("tname").row(0).at(0).getStringData().equals("text0"));
    assertEquals(
        db.getTable("tname").row(0).at(1).getStringData(), Cell.makeCell(date).getStringData());
    assertEquals(1, t1.size());
  }
Exemplo n.º 2
0
  @Test
  public void mustAllowDoubleCommitAndRollback() {
    SharedGroup db = new SharedGroup(testFile, SharedGroup.Durability.FULL, null);
    {
      WriteTransaction trans = db.beginWrite();
      Table tbl = trans.getTable("EmployeeTable");
      tbl.addColumn(RealmFieldType.STRING, "name");
      tbl.addColumn(RealmFieldType.INTEGER, "number");

      // allow commit before any changes
      assertEquals(0, tbl.size());
      tbl.add("Hello", 1);
      trans.commit();
    }
    {
      WriteTransaction trans = db.beginWrite();
      Table tbl = trans.getTable("EmployeeTable");
      // allow double rollback
      tbl.add("Hello", 2);
      assertEquals(2, tbl.size());
      trans.rollback();
      trans.rollback();
      trans.rollback();
      trans.rollback();
    }
    {
      ReadTransaction trans = db.beginRead();
      Table tbl = trans.getTable("EmployeeTable");
      assertEquals(1, tbl.size());
      trans.endRead();
    }
  }
Exemplo n.º 3
0
  public void testAddMissing() {
    Table<Integer> buf = new Table<Integer>(3, 10, 0);
    for (int i : Arrays.asList(1, 2, 4, 5, 6)) buf.add(i, i);
    System.out.println("buf = " + buf);
    assert buf.size() == 5 && buf.getNumMissing() == 1;

    Integer num = buf.remove();
    assert num == 1;
    num = buf.remove();
    assert num == 2;
    num = buf.remove();
    assert num == null;

    buf.add(3, 3);
    System.out.println("buf = " + buf);
    assert buf.size() == 4 && buf.getNumMissing() == 0;

    for (int i = 3; i <= 6; i++) {
      num = buf.remove();
      System.out.println("buf = " + buf);
      assert num == i;
    }

    num = buf.remove();
    assert num == null;
  }
Exemplo n.º 4
0
 public static void testComputeSize2() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   table.add(1, 1);
   System.out.println("table = " + table);
   assert table.computeSize() == table.size();
   assert table.computeSize() == 1;
   table.remove(false);
   System.out.println("table = " + table);
   assert table.computeSize() == table.size();
   assert table.computeSize() == 0;
 }
Exemplo n.º 5
0
  public void testCompact2() {
    Table<Integer> table = new Table<Integer>(3, 10, 0);
    for (int i = 1; i <= 80; i++) addAndGet(table, i);
    assert table.size() == 80;
    for (long i = 1; i <= 60; i++) table.remove();

    assert table.size() == 20;
    table.compact();
    assert table.size() == 20;
    assertCapacity(table.capacity(), table.getNumRows(), 10);
  }
Exemplo n.º 6
0
 public static void testAddition() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   assert !table.add(0, 0);
   addAndGet(table, 1, 5, 9, 10, 11, 19, 20, 29);
   System.out.println("table: " + table.dump());
   assert table.size() == 8;
   int size = table.computeSize();
   assert size == 8;
   assert table.size() == table.computeSize();
   assertCapacity(table.capacity(), 3, 10);
   assertIndices(table, 0, 0, 29);
 }
Exemplo n.º 7
0
 public static void testRemoveManyWithWrapping2() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   for (int seqno : Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16, 17, 18, 19, 20))
     table.add(seqno, seqno);
   System.out.println("table = " + table);
   assertIndices(table, 0, 0, 20);
   assert table.size() == 18 && table.getNumMissing() == 2;
   List<Integer> list = table.removeMany(false, 0);
   assert list.size() == 12;
   assertIndices(table, 0, 12, 20);
   assert table.size() == 6 && table.getNumMissing() == 2;
   table.purge(12);
   assertIndices(table, 12, 12, 20);
   assert table.size() == 6 && table.getNumMissing() == 2;
 }
Exemplo n.º 8
0
 @Test
 public void addEmptyRowWithPrimaryKeyString() {
   Table t = getTableWithStringPrimaryKey();
   long rowIndex = t.addEmptyRowWithPrimaryKey("Foo");
   assertEquals(1, t.size());
   assertEquals("Foo", t.getUncheckedRow(rowIndex).getString(0));
 }
Exemplo n.º 9
0
  public void testResizeWithPurge2() {
    Table<Integer> table = new Table<Integer>(3, 10, 0);
    for (int i = 1; i <= 50; i++) addAndGet(table, i);
    System.out.println("table = " + table);
    assert table.size() == 50;
    assertCapacity(table.capacity(), table.getNumRows(), 10);
    assertIndices(table, 0, 0, 50);
    table.removeMany(false, 43);
    System.out.println("table = " + table);
    assertIndices(table, 0, 43, 50);
    table.purge(43);
    System.out.println("table = " + table);
    assertIndices(table, 43, 43, 50);
    addAndGet(table, 52);
    assert table.get(43) == null;

    for (long i = 44; i <= 50; i++) {
      Integer num = table.get(i);
      assert num != null && num == i;
    }

    assert table.get(50) != null;
    assert table.get(51) == null;
    Integer num = table.get(52);
    assert num != null && num == 52;
    assert table.get(53) == null;
  }
Exemplo n.º 10
0
  // See https://github.com/realm/realm-java/issues/1775 .
  // Before 0.84.2, pk table added prefix "class_" to every class's name.
  // After 0.84.2, the pk table should be migrated automatically to remove the "class_".
  // In 0.84.2, the class names in pk table has been renamed to some incorrect names like "Thclass",
  // "Mclass",
  // "NClass", "Meclass" and etc..
  // The 0841_pk_migration.realm is made to produce the issue.
  @Test
  public void primaryKeyTableMigratedWithRightName() throws IOException {
    List<String> tableNames =
        Arrays.asList(
            "ChatList",
            "Drafts",
            "Member",
            "Message",
            "Notifs",
            "NotifyLink",
            "PopularPost",
            "Post",
            "Tags",
            "Threads",
            "User");

    configFactory.copyRealmFromAssets(context, "0841_pk_migration.realm", "default.realm");
    SharedGroup db =
        new SharedGroup(
            new File(configFactory.getRoot(), Realm.DEFAULT_REALM_NAME).getAbsolutePath(),
            SharedGroup.Durability.FULL,
            null);

    ImplicitTransaction tr = db.beginImplicitTransaction();
    // To trigger migratePrimaryKeyTableIfNeeded.
    tr.getTable("class_ChatList").getPrimaryKey();

    Table table = tr.getTable("pk");
    for (int i = 0; i < table.size(); i++) {
      UncheckedRow row = table.getUncheckedRow(i);
      // io_realm_internal_Table_PRIMARY_KEY_CLASS_COLUMN_INDEX 0LL
      assertTrue(tableNames.contains(row.getString(0)));
    }
    db.close();
  }
Exemplo n.º 11
0
 public static void testAdditionList() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   List<Tuple<Long, Integer>> msgs = createList(0);
   assert !table.add(msgs);
   long[] seqnos = {1, 5, 9, 10, 11, 19, 20, 29};
   msgs = createList(seqnos);
   assert table.add(msgs);
   System.out.println("table: " + table.dump());
   for (long seqno : seqnos) assert table.get(seqno) == seqno;
   assert table.size() == 8;
   int size = table.computeSize();
   assert size == 8;
   assert table.size() == table.computeSize();
   assertCapacity(table.capacity(), 3, 10);
   assertIndices(table, 0, 0, 29);
 }
Exemplo n.º 12
0
  public void testPurgeForce() {
    Table<Integer> table = new Table<Integer>(3, 10, 0);
    for (int i = 1; i <= 30; i++) table.add(i, i);
    System.out.println("table = " + table);
    table.purge(15, true);
    System.out.println("table = " + table);
    assertIndices(table, 15, 15, 30);
    for (int i = 1; i <= 15; i++) assert table._get(i) == null;
    for (int i = 16; i <= 30; i++) assert table._get(i) != null;
    assert table.get(5) == null && table.get(25) != null;

    table.purge(30, true);
    System.out.println("table = " + table);
    assertIndices(table, 30, 30, 30);
    assert table.isEmpty();
    for (int i = 1; i <= 30; i++) assert table._get(i) == null;

    for (int i = 31; i <= 40; i++) table.add(i, i);
    System.out.println("table = " + table);
    assert table.size() == 10;
    assertIndices(table, 30, 30, 40);

    table.purge(50, true);
    System.out.println("table = " + table);
    assert table.isEmpty();
    assertIndices(table, 40, 40, 40);
  }
Exemplo n.º 13
0
  public static void testPurge() {
    Table<Integer> table = new Table<Integer>(5, 10, 0);
    for (int seqno = 1; seqno <= 25; seqno++) table.add(seqno, seqno);

    int[] seqnos = {30, 31, 32, 37, 38, 39, 40, 41, 42, 47, 48, 49};
    for (int seqno : seqnos) table.add(seqno, seqno);

    System.out.println("table (before remove):\n" + table.dump());
    for (int seqno = 1; seqno <= 22; seqno++) table.remove(false);

    System.out.println("\ntable (after remove 22, before purge):\n" + table.dump());
    table.purge(22);
    System.out.println("\ntable: (after purge 22):\n" + table.dump());
    assert table.size() == 3 + seqnos.length;
    assert table.computeSize() == table.size();
  }
Exemplo n.º 14
0
  /**
   * Runs NUM adder threads, each adder adds 1 (unique) seqno. When all adders are done, we should
   * have NUM elements in the table.
   */
  public void testConcurrentAdd() {
    final int NUM = 100;
    final Table<Integer> buf = new Table<Integer>(3, 10, 0);

    CountDownLatch latch = new CountDownLatch(1);
    Adder[] adders = new Adder[NUM];
    for (int i = 0; i < adders.length; i++) {
      adders[i] = new Adder(latch, i + 1, buf);
      adders[i].start();
    }

    System.out.println("starting threads");
    latch.countDown();
    System.out.print("waiting for threads to be done: ");
    for (Adder adder : adders) {
      try {
        adder.join();
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }
    System.out.println("OK");
    System.out.println("buf = " + buf);
    assert buf.size() == NUM;
  }
Exemplo n.º 15
0
 public void testAdd() {
   Table<Integer> buf = new Table<Integer>(3, 10, 0);
   buf.add(1, 322649);
   buf.add(2, 100000);
   System.out.println("buf = " + buf);
   assert buf.size() == 2;
 }
Exemplo n.º 16
0
 @Test
 public void addEmptyRowWithPrimaryKeyLong() {
   Table t = getTableWithIntegerPrimaryKey();
   long rowIndex = t.addEmptyRowWithPrimaryKey(42);
   assertEquals(1, t.size());
   assertEquals(42, t.getUncheckedRow(rowIndex).getLong(0));
 }
Exemplo n.º 17
0
 public static void testDuplicateAddition() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   addAndGet(table, 1, 5, 9, 10);
   assert !table.add(5, 5);
   assert table.get(5) == 5;
   assert table.size() == 4;
   assertIndices(table, 0, 0, 10);
 }
Exemplo n.º 18
0
 public static void testCreation() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   System.out.println("table = " + table);
   int size = table.size();
   assert size == 0;
   assert table.get(15) == null;
   assertIndices(table, 0, 0, 0);
 }
Exemplo n.º 19
0
 protected void checkRead(SharedGroup db, int rows) {
   // Read transaction
   ReadTransaction trans = db.beginRead();
   Table tbl = trans.getTable("EmployeeTable");
   assertEquals(true, tbl.isValid());
   assertEquals(rows, tbl.size());
   trans.endRead();
 }
Exemplo n.º 20
0
 public void testCompact() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   for (int i = 1; i <= 80; i++) addAndGet(table, i);
   assert table.size() == 80;
   assertIndices(table, 0, 0, 80);
   List<Integer> list = table.removeMany(false, 60);
   assert list.size() == 60;
   assert list.get(0) == 1 && list.get(list.size() - 1) == 60;
   assertIndices(table, 0, 60, 80);
   table.purge(60);
   assertIndices(table, 60, 60, 80);
   assert table.size() == 20;
   table.compact();
   assertIndices(table, 60, 60, 80);
   assert table.size() == 20;
   assertCapacity(table.capacity(), table.getNumRows(), 10);
 }
Exemplo n.º 21
0
 public void testAddList() {
   Table<Integer> buf = new Table<Integer>(3, 10, 0);
   List<Tuple<Long, Integer>> msgs = createList(1, 2);
   boolean rc = buf.add(msgs);
   System.out.println("buf = " + buf);
   assert rc;
   assert buf.size() == 2;
 }
Exemplo n.º 22
0
 public static void testAdditionWithOffset() {
   Table<Integer> table = new Table<Integer>(3, 10, 100);
   addAndGet(table, 101, 105, 109, 110, 111, 119, 120, 129);
   System.out.println("table: " + table.dump());
   assert table.size() == 8;
   assertCapacity(table.capacity(), 3, 10);
   assertIndices(table, 100, 100, 129);
 }
Exemplo n.º 23
0
  protected void writeOneTransaction(SharedGroup db, long rows) {
    WriteTransaction trans = db.beginWrite();
    Table tbl = trans.getTable("EmployeeTable");
    tbl.addColumn(RealmFieldType.STRING, "name");
    tbl.addColumn(RealmFieldType.INTEGER, "number");

    for (long row = 0; row < rows; row++) tbl.add("Hi", 1);
    assertEquals(rows, tbl.size());
    trans.commit();

    // must throw exception as table is invalid now.
    try {
      assertEquals(1, tbl.size());
      fail();
    } catch (IllegalStateException e) {
      assertNotNull(e);
    }
  }
Exemplo n.º 24
0
 public static void testMove() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   for (int i = 1; i < 50; i++) addAndGet(table, i);
   table.removeMany(true, 49);
   assert table.isEmpty();
   addAndGet(table, 50);
   assert table.size() == 1;
   assertCapacity(table.capacity(), table.getNumRows(), 10);
 }
Exemplo n.º 25
0
 public static void testAdditionWithOffset2() {
   Table<Integer> table = new Table<Integer>(3, 10, 2);
   addAndGet(table, 1000, 1001);
   table.compact();
   addAndGet(table, 1005, 1009, 1010, 1011, 1019, 1020, 1029);
   System.out.println("table: " + table.dump());
   assert table.size() == 9;
   assertIndices(table, 2, 2, 1029);
 }
  @Test
  public void getTablesSubTest() throws Exception {
    Database db = new Database("test");
    Table t1 = new Table("tname1");

    t1.addColumn("A", CharField.class);
    t1.addColumn("B", IntegerField.class);

    Row r1 = t1.getRowSkeleton();
    r1.insert(0, Cell.makeCell("text1"));
    r1.insert(1, Cell.makeCell(11));
    t1.addRow(r1);

    r1.insert(0, Cell.makeCell("text2"));
    r1.insert(1, Cell.makeCell(12));
    t1.addRow(r1);

    r1.insert(0, Cell.makeCell("text3"));
    r1.insert(1, Cell.makeCell(13));
    t1.addRow(r1);

    Table t2 = new Table("tname2");
    t2.addColumn("A", CharField.class);
    t2.addColumn("B", IntegerField.class);
    Row r3 = t2.getRowSkeleton();
    r3.insert(0, Cell.makeCell("text2"));
    r3.insert(1, Cell.makeCell(12));
    t2.addRow(r3);

    Table resTable = db.getTablesSub(t1, t2);

    assertEquals(2, resTable.size());
    assertEquals(2, resTable.getHeader().size());
    assertEquals(CharField.class, resTable.getHeader().getTypeAt(0));
    assertEquals(IntegerField.class, resTable.getHeader().getTypeAt(1));
    assertEquals(2, resTable.size());

    assertEquals("text1", resTable.getStringRow(0).getCel().get(0));
    assertEquals("11", resTable.getStringRow(0).getCel().get(1));

    assertEquals("text3", resTable.getStringRow(1).getCel().get(0));
    assertEquals("13", resTable.getStringRow(1).getCel().get(1));
  }
Exemplo n.º 27
0
 public static void testMassAddition() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   final int NUM_ELEMENTS = 10005;
   for (int i = 1; i <= NUM_ELEMENTS; i++) table.add(i, i);
   System.out.println("table = " + table);
   assert table.size() == NUM_ELEMENTS;
   assertCapacity(table.capacity(), table.getNumRows(), 10);
   assertIndices(table, 0, 0, NUM_ELEMENTS);
   assert table.getNumMissing() == 0;
 }
Exemplo n.º 28
0
 public static void testAdditionListWithOffset() {
   Table<Integer> table = new Table<Integer>(3, 10, 100);
   long seqnos[] = {101, 105, 109, 110, 111, 119, 120, 129};
   List<Tuple<Long, Integer>> msgs = createList(seqnos);
   System.out.println("table: " + table.dump());
   assert table.add(msgs);
   assert table.size() == 8;
   for (long seqno : seqnos) assert table.get(seqno) == seqno;
   assertCapacity(table.capacity(), 3, 10);
   assertIndices(table, 100, 100, 129);
 }
Exemplo n.º 29
0
 public static void testRemoveMany() {
   Table<Integer> table = new Table<Integer>(3, 10, 0);
   for (int seqno : Arrays.asList(1, 2, 3, 4, 5, 7, 8, 9, 10)) table.add(seqno, seqno);
   System.out.println("table = " + table);
   assertIndices(table, 0, 0, 10);
   List<Integer> list = table.removeMany(true, 4);
   System.out.println("list=" + list + ", table=" + table);
   assert table.size() == 5 && table.getNumMissing() == 1;
   assert list != null && list.size() == 4;
   for (int num : Arrays.asList(1, 2, 3, 4)) assert list.contains(num);
   assertIndices(table, 4, 4, 10);
 }
Exemplo n.º 30
0
  public static void testGetNullMessages() {
    Table<Integer> table = new Table<Integer>(3, 10, 0);
    table.add(1, 1);
    table.add(100, 100);
    System.out.println("table = " + table);
    int num_null_elements = table.getNumMissing();
    assert num_null_elements == 98; // [2 .. 99]

    table.add(50, 50);
    System.out.println("table = " + table);
    assert table.size() == 3;
    assert table.getNumMissing() == 97;
  }