@Test public void testNoConditionsFullScan() throws Exception { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("smallbag"); bag.addEntity(new SimpleEntity(ID1).addProperty("type", "thing")); bag.addEntity(new SimpleEntity(ID2).addProperty("type", "thing")); bag.addEntity(new SimpleEntity(ID3).addProperty("type", "contribution")); bag.addEntity(new SimpleEntity(ID4).addProperty("type", "contribution")); try { QueryBuilder builder = QueryBuilder.createBuilder(); QueryExecutor executor = bag.find(builder); List<Entity> entities = toList(executor.execute()); List<String> entityIds = entities.stream().map(Entity::getInternalId).collect(Collectors.toList()); assertThat(entityIds, hasItems(ID1, ID2, ID3, ID4)); } finally { session.closeSession(); JasDBMain.shutdown(); } } }
@Test public void testCreateAndInsertEntities() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); EntityBag bag = session.createOrGetBag(MY_INSTANCE, BAG_1); bag.addEntity(new SimpleEntity().addProperty("test", "value")); QueryResult result = bag.getEntities(); assertThat(result.size(), is(1l)); Entity entity = result.next(); assertThat(entity, notNullValue()); assertThat(entity.getProperty("test").getFirstValue().toString(), is("value")); }
@Override public Entity persist(Object persistableObject) throws JasDBStorageException { MapResult mappedResult = ENTITY_MAPPER.mapTo(persistableObject); String bagName = mappedResult.getBagName(); EntityBag bag = session.createOrGetBag(bagName); Entity persistedEntity; try { Entity entity = mappedResult.getJasDBEntity(); if(StringUtils.stringNotEmpty(entity.getInternalId()) && bag.getEntity(entity.getInternalId()) != null) { //update persistedEntity = bag.updateEntity(mappedResult.getJasDBEntity()); LOG.debug("Updated entity: {} in bag: {}", persistedEntity, bagName); } else { persistedEntity = bag.addEntity(mappedResult.getJasDBEntity()); LOG.debug("Created entity: {} in bag: {}", persistedEntity, bagName); } } catch(RuntimeJasDBException e) { //we do this in case we have exactly two threads at same time trying to persist persistedEntity = bag.updateEntity(mappedResult.getJasDBEntity()); LOG.debug("Updated entity: {} in bag: {}", persistedEntity, bagName); } //update the ID of the passed object ENTITY_MAPPER.updateId(persistedEntity.getInternalId(), persistableObject); return persistedEntity; }
@Test public void testBagFlush() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); long sizeBefore = bag.getDiskSize(); int TEST_SIZE = 1000; for(int i=0; i<TEST_SIZE; i++) { bag.addEntity(new SimpleEntity()); } bag.flush(); assertTrue(bag.getDiskSize() > sizeBefore); }
@Test public void testInsertOrUpdatePersist() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("insertOrUpdateBag"); SimpleEntity entity = new SimpleEntity(); entity.addProperty("Test", "value1"); String id = bag.persist(entity).getInternalId(); assertThat(bag.getEntities().size(), is(1L)); assertThat(bag.getEntity(id).getProperty("Test").getFirstValueObject(), is("value1")); SimpleEntity updatedEntity = new SimpleEntity(id); updatedEntity.addProperty("AnotherProperty", "AnotherValue"); bag.persist(updatedEntity); assertThat(bag.getEntities().size(), is(1L)); assertThat(bag.getEntity(id).getProperty("AnotherProperty").getFirstValueObject(), is("AnotherValue")); }
@Test public void testDeleteEmptyIndexValue() throws Exception { try { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("somebag"); bag.ensureIndex(new SimpleIndexField("field", new StringKeyType()), false); String id = bag.addEntity(new SimpleEntity().addProperty("anotherfield", "somevalue")).getInternalId(); bag.removeEntity(id); } finally { JasDBMain.shutdown(); } }
private List<Entity> getEntities(EntityBag bag, QueryBuilder query, int start, int limit) throws JasDBStorageException { QueryExecutor executor = bag.find(query); if(start > 0 && limit > 0) { executor.paging(start, limit); } else if(limit > 0) { executor.limit(limit); } final List<Entity> entities = new ArrayList<>(); try (QueryResult result = executor.execute()) { for (Entity entity : result) { entities.add(entity); } } return entities; }
@Test public void testPersisterRemove() throws Exception { int testSize = 1000; DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType(100)), false); bag.ensureIndex(new SimpleIndexField("testField", new LongKeyType()), true); Random rnd = new Random(); for(int i=0; i<testSize; i++) { int cityIdx = rnd.nextInt(SimpleBaseTest.possibleCities.length); String city = SimpleBaseTest.possibleCities[cityIdx]; SimpleEntity entity = new SimpleEntity(); entity.addProperty("city", city); entity.addProperty("testField", (long)i); bag.addEntity(entity); } for(String city : SimpleBaseTest.possibleCities) { QueryResult result = bag.find(QueryBuilder.createBuilder().field("city").value(city)).execute(); for(Entity foundEntity : result) { Long testFieldValue = foundEntity.getProperty("testField").getFirstValueObject(); bag.removeEntity(foundEntity); assertFalse("There should no longer be a result", bag.find(QueryBuilder.createBuilder().field("testField").value(testFieldValue)).execute().hasNext()); } result = bag.find(QueryBuilder.createBuilder().field("city").value(city)).execute(); assertEquals("There should no longer be any entity", (long) 0, result.size()); } }
@Test public void testPersistIndexNonUniqueQuery() throws JasDBException, InterruptedException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.addEntity(new SimpleEntity().addProperty("city", "Amsterdam")); bag.addEntity(new SimpleEntity().addProperty("city", "Amsterdam")); bag.addEntity(new SimpleEntity().addProperty("city", "Rotterdam")); bag.addEntity(new SimpleEntity().addProperty("city", "Utrecht")); bag.addEntity(new SimpleEntity().addProperty("city", "Utrecht")); QueryResult result = bag.find(QueryBuilder.createBuilder().field("city").value("Amsterdam")).execute(); assertThat(result.size(), is(2L)); result.close(); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType()), false); //let's give the index some time to build Thread.sleep(5000); result = bag.find(QueryBuilder.createBuilder().field("city").value("Amsterdam")).execute(); assertThat(result.size(), is(2L)); result.close(); } }
@Test public void testPersistFindPerformance() throws Exception { DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("mybag"); List<String> entityIds = new ArrayList<>(); for(int i=0; i<NUMBER_ENTITIES; i++) { SimpleEntity entity = new SimpleEntity(UUID.randomUUID().toString()); entity.addProperty("someProperty" + i, i); entity.addProperty("doubleId", entity.getInternalId()); bag.addEntity(entity); entityIds.add(entity.getInternalId()); } try { for(String id : entityIds) { Entity entity = bag.getEntity(id); Assert.assertNotNull("Entity for id: " + id + " should be found", entity); assertEquals("Id should match expected id", id, entity.getInternalId()); Assert.assertNotNull("There should be a property doubleId", entity.getProperty("doubleId")); assertEquals("Property doubleId should match expected id", id, entity.getProperty("doubleId").getFirstValueObject()); } } finally { JasDBMain.shutdown(); } }
try { for(int i=0; i<INITIAL_SIZE; i++) { Entity entity = bag.addEntity(new SimpleEntity().addProperty("testfield", "test" + i)); entities.add(entity.getInternalId()); try { for(int i=0; i<NUMBER_ENTITIES; i++) { Entity entity = bag.addEntity(new SimpleEntity().addProperty("testfield", "test" + (i + INITIAL_SIZE))); entities.add(entity.getInternalId()); Entity entity = bag.getEntity(id); Assert.assertNotNull("Entity for id: " + id + " should be found", entity); assertEquals("Id should match expected id", id, entity.getInternalId()); for(Entity en : bag.getEntities()) { log.debug("Loaded entity: {}", en.getInternalId()); recordsFound++;
@Test public void testRandomPersistUpdate() throws Exception { String[] cities = new String[] {"Amsterdam", "Rotterdam", "Utrecht", "Groningen", "Haarlem", "Den Haag", "Maastricht", "Eindhoven"}; int testSize = 1000; DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("city", new StringKeyType()), false); bag.ensureIndex(new SimpleIndexField("itemId", new LongKeyType()), true); try { Map<String, Integer> cityCounts = generateCities(testSize, bag); assertCityIndexes(bag, cities, cityCounts); for(int i=200; i<400; i++) { QueryExecutor executor = bag.find(QueryBuilder.createBuilder().field("itemId").value((long)i)); QueryResult result = executor.execute(); for(Entity entity : result) { String city = entity.getProperty("city").getFirstValueObject().toString(); entity.setProperty("city", "unknown"); bag.updateEntity(entity); changeCityCount(city, cityCounts, false); changeCityCount("unknown", cityCounts, true); } } assertCityIndexes(bag, cities, cityCounts); } finally { JasDBMain.shutdown(); } }
EntityBag bag = pojoDb.createOrGetBag("homeautotest"); bag.ensureIndex( new SimpleCompositeIndexField( new SimpleIndexField("controllerId", new StringKeyType()), String controllerEntityId = bag.addEntity(new SimpleEntity().addProperty("controllerId", "Renzes-MacBook-Pro-2.local") .addProperty("plugins", "7158f3ec-681f-4d9b-9ab1-6ab6c60288e3") .addProperty("type", "controller")).getInternalId(); String pluginEntityId = bag.addEntity(new SimpleEntity() .addProperty("controllerId", "Renzes-MacBook-Pro-2.local").addProperty("pluginId", "zwave") .addProperty("name", "ZWave provider") .addProperty("type", "plugin")).getInternalId(); String deviceEntityId = bag.addEntity(new SimpleEntity() .addProperty("controllerId", "Renzes-MacBook-Pro-2.local").addProperty("pluginId", "zwave") .addProperty("name", "ZWave provider") bag.updateEntity(new SimpleEntity(controllerEntityId).addProperty("controllerId", "Renzes-MacBook-Pro-2.local") .addProperty("plugins", "7158f3ec-681f-4d9b-9ab1-6ab6c60288e3").addProperty("type", "controller")); bag.updateEntity(new SimpleEntity(pluginEntityId) .addProperty("controllerId", "Renzes-MacBook-Pro-2.local").addProperty("pluginId", "zwave") .addProperty("name", "ZWave provider").addProperty("type", "plugin"));
@Test public void testEnsureAndRemoveIndex() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("field1", new StringKeyType()), true); bag.ensureIndex(new SimpleIndexField("field2", new StringKeyType()), false); bag.addEntity(new SimpleEntity().addProperty("field1", "value1").addProperty("field2", "testkey2value")); String jasdbHome = storageLocation + "/.jasdb"; File field1Index = new File(jasdbHome, "testbag_field1.idx"); File field2Index = new File(jasdbHome, "testbag_field2ID.idx"); assertTrue("Index 1 file should exist", field1Index.exists()); assertTrue("Index 2 file should exist", field2Index.exists()); bag.removeIndex("field1"); assertFalse("Index 1 file should no longer exist", field1Index.exists()); assertTrue("Index 2 file should exist", field2Index.exists()); bag.removeIndex("field2ID"); assertFalse("Index 1 file should no longer exist", field1Index.exists()); assertFalse("Index 2 file should no longer exist", field2Index.exists()); }
DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("testbag"); bag.ensureIndex(new SimpleIndexField("title", new StringKeyType()), true); bag.addEntity(entity); searchEntity.addProperty("MySearchProperty", "MySpecialSearchProperty"); searchEntity.setInternalId(searchTestId); bag.addEntity(searchEntity); } finally { JasDBMain.shutdown();
private Map<String, Integer> generateCities(int testSize, EntityBag bag) throws JasDBStorageException { Map<String, Integer> cityCounts = new HashMap<>(); Random rnd = new Random(); for(int i=0; i<testSize; i++) { int cityIdx = rnd.nextInt(cities.length); String city = cities[cityIdx]; SimpleEntity entity = new SimpleEntity(); entity.addProperty("city", city); entity.addProperty("itemId", (long)i); changeCityCount(city, cityCounts, true); bag.addEntity(entity); } return cityCounts; }
@Test public void testEntityManagerUpdate() throws JasDBException { DBSession session = sessionFactory.createSession(); EntityManager entityManager = session.getEntityManager(); String id = UUID.randomUUID().toString(); TestEntity entity = new TestEntity(id, "Renze", "de Vries", newArrayList("programming", "model building", "biking"), new ImmutableMap.Builder<String, String>() .put("city", "Amsterdam") .put("street", "Secret passageway 10") .put("zipcode", "0000TT").build()); assertThat(entityManager.persist(entity).getInternalId(), is(id)); EntityBag testBag = session.createOrGetBag("TEST_BAG"); assertThat(testBag.getSize(), is(1l)); Entity mappedEntity = testBag.getEntity(id); assertThat(mappedEntity.getValue("firstName"), is("Renze")); assertThat(mappedEntity.getValue("lastName"), is("de Vries")); entity.setFirstName("Updated"); entityManager.persist(entity); mappedEntity = testBag.getEntity(id); assertThat(mappedEntity.getValue("firstName"), is("Updated")); assertThat(mappedEntity.getValue("lastName"), is("de Vries")); }
private void assertFind(String someRandomId) throws Exception { JasDBMain.start(); log.info("START INDEX READ TEST"); DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("testbag"); try { log.info("Starting search for: {}", searchTestId); long startSearch = System.nanoTime(); Entity entity = bag.getEntity(searchTestId); long endSearch = System.nanoTime(); log.info("Search finished in: {}", (endSearch - startSearch)); Assert.assertNotNull("An entity should have been found for id: " + searchTestId, entity); assertEquals(searchTestId, entity.getInternalId()); log.info("Starting search for random: {}", someRandomId); startSearch = System.nanoTime(); entity = bag.getEntity(someRandomId); endSearch = System.nanoTime(); log.info("Search finished for random: {}", (endSearch - startSearch)); Assert.assertNotNull("An entity should have been found", entity); assertEquals(someRandomId, entity.getInternalId()); } finally { JasDBMain.shutdown(); } }
@RequestMapping(value = "/{instanceId}/{bag}") public String findAll(@PathVariable String instanceId, @PathVariable String bag, Model model) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); EntityBag entityBag = session.getBag(bag); PageResult result = new PageResult(); model.addAttribute("page", result); if(entityBag != null) { QueryResult queryResult = entityBag.getEntities(DEFAULT_PAGE_SIZE); result.setEntities(loadEntities(queryResult)); } else { result.setMessage(String.format("Unable to load Bag: %s on instance: %s as it does not exist", bag, instanceId)); } return "data/query"; }
@Override public void remove(Object persistableObject) throws JasDBStorageException { MapResult mappedResult = ENTITY_MAPPER.mapTo(persistableObject); String bagName = mappedResult.getBagName(); EntityBag bag = session.createOrGetBag(bagName); bag.removeEntity(mappedResult.getJasDBEntity().getInternalId()); }