@Test public void shouldPersistToFile() throws IOException { /** * WRITE DB 1 ** */ Db db = Ohm.db(DB_FILE); Table<Person> table = db.table(Person.class); assert 0 == table.insert(person("a", 100)); assert 1 == table.insert(person("b", 200)); for (int i = 0; i < 3; i++) { table.set(0, "name", "x" + i); table.set(0, "age", i); } table.print(); db.shutdown(); /** * READ DB 2 ** */ Db db2 = Ohm.db(DB_FILE); Table<Person> table2 = db2.table(Person.class); eq(table2.size(), 2); table2.print(); /** * WRITE DB 2 ** */ long id2 = table2.insert(person("c", 300)); eq(id2, 2L); eq(table2.size(), 3); table2.set(0, "name", "x"); table2.set(1, "name", "y"); table2.delete(1); eq(table2.size(), 2); table2.set(2, "name", "z"); table2.print(); db2.shutdown(); /** * READ DB 3 ** */ Db db3 = Ohm.db(DB_FILE); Table<Person> table3 = db3.table(Person.class); eq(table3.size(), 2); table3.print(); db3.shutdown(); }
@Test public void shouldPersistToFileMini() throws IOException, TransactionException { Db db = Ohm.db(DB_FILE); Table<Person> table = db.table(Person.class); assert 0 == table.insert(person("a", 100)); assert 1 == table.insert(person("b", 200)); db.shutdown(); /** * READ DB 2 ** */ Db db2 = Ohm.db(DB_FILE); Table<Person> table2 = db2.table(Person.class); Transaction txx = db2.startTransaction(); eq(table2.size(), 2); System.out.println("now delete..."); table2.delete(1); eq(table2.size(), 1); txx.commit(); Transaction txx2 = db2.startTransaction(); System.out.println("now set name"); table2.set(0, "name", "z"); txx2.commit(); db2.shutdown(); /** * READ DB 3 ** */ U.sleep(1000); System.out.println("============================="); Db db3 = Ohm.db(DB_FILE); Table<Person> table3 = db3.table(Person.class); eq(table3.size(), 1); }
@SuppressWarnings("unchecked") @Override public long insert(Object entity) { Table<Object> tbl = (Table<Object>) table(entity.getClass()); return tbl.insert(entity); }