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; }
@RequestMapping(value = "/{instanceId}/createEntity", method = RequestMethod.POST) public String createData(@Valid WebEntity entity, @PathVariable String instanceId) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); EntityBag entityBag = session.createOrGetBag(entity.getBag()); entityBag.addEntity(SimpleEntity.fromJson(entity.getData())); return "redirect:/data/"; }
public static void main(String[] args) { try { LOG.info("Starting JasDB"); JasDBMain.start(); LOG.info("JasDB was started, awaiting termination signal"); DBSession session = new LocalDBSession(); EntityBag bag = session.createOrGetBag("test"); bag.addEntity(new SimpleEntity().addProperty("testProperty", "someValue")); JasDBMain.waitForShutdown(); LOG.info("JasDB was terminated"); } catch (JasDBException e) { LOG.error("Could not start JasDB", e); } } }
@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")); }
@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()); }
@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 testAndOperationMultiQueryBuilderTablescan() throws Exception { DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("thosha"); 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(BlockType.AND); builder.addQueryBlock(QueryBuilder.createBuilder().field("__ID").value(ID3)); builder.addQueryBlock(QueryBuilder.createBuilder().field("type").value("contribution")); QueryExecutor executor = bag.find(builder); List<Entity> entities = toList(executor.execute()); assertThat(entities.size(), is(1)); } finally { pojoDb.closeSession(); JasDBMain.shutdown(); } }
@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 testQueryNonExistingProperty() throws Exception { DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("thosha"); bag.addEntity(new SimpleEntity("00005442-4961-c49d-0000-013d73bba1f7").addProperty("type", "thing")); bag.addEntity(new SimpleEntity("00005442-4961-c49d-0000-013d73bba1f8").addProperty("type", "thing")); bag.addEntity(new SimpleEntity("00005442-4961-c49d-0000-013dad2eefd2").addProperty("type", "contribution")); bag.addEntity(new SimpleEntity("00005442-4961-c49d-0000-013dd66f0aed").addProperty("type", "contribution")); try { QueryBuilder builder = QueryBuilder.createBuilder(BlockType.AND); builder.addQueryBlock(QueryBuilder.createBuilder().field("NonExistingProperty").value("00005442-4961-c49d-0000-013dad2eefd2")); builder.addQueryBlock(QueryBuilder.createBuilder().field("type").value("contribution")); QueryExecutor executor = bag.find(builder); try (QueryResult result = executor.execute()) { assertThat(result.size(), is(0l)); } } finally { pojoDb.closeSession(); JasDBMain.shutdown(); } }
@Test public void testInvalidJsonInsert() throws Exception { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("MySpecialBag"); SimpleEntity entity = new SimpleEntity(); entity.addProperty("title", "Title of my content"); entity.addProperty("text", "Some big piece of text content"); bag.addEntity(entity); QueryExecutor executor = bag.find(QueryBuilder.createBuilder().field("text").value("Some big piece of text content")); QueryResult result = executor.execute(); assertThat(result.size(), is(1L)); for(Entity resultEntity : result) { String json = SimpleEntity.toJson(resultEntity); log.info("Output: {}", json); } }
@Test public void testSortDescendingInvalidType() throws Exception { DBSession session = sessionFactory.createSession(); try{ EntityBag bag = session.createOrGetBag("Bag"); SimpleEntity entity = new SimpleEntity(); entity.addProperty("name", "xxx"); entity.addProperty("v", "3"); bag.addEntity(entity); entity = new SimpleEntity(); entity.addProperty("name", 1); entity.addProperty("v", "1"); bag.addEntity(entity); entity = new SimpleEntity(); entity.addProperty("name", "xxx"); entity.addProperty("v", "2"); bag.addEntity(entity); QueryBuilder innerQuery = QueryBuilder.createBuilder(); innerQuery.field("name").value("xxx").sortBy("v", Order.DESCENDING); QueryExecutor executor = bag.find(innerQuery); QueryResult result = executor.execute(); assertThat(result.size(), is(2l)); assertThat(result.next().getValue("v"), is("3")); assertThat(result.next().getValue("v"), is("2")); } finally { JasDBMain.shutdown(); } }
@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(); } }
@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 testSortByNonExistingField() throws Exception { DBSession session = sessionFactory.createSession(); try{ EntityBag bag = session.createOrGetBag("Bag"); SimpleEntity entity = new SimpleEntity(); entity.addProperty("name", "xxx"); entity.addProperty("v", "1"); bag.addEntity(entity); entity = new SimpleEntity(); entity.addProperty("name", "xxx"); entity.addProperty("v", "2"); bag.addEntity(entity); QueryBuilder innerQuery = QueryBuilder.createBuilder(); innerQuery.field("name").value("xxx").sortBy("_id",Order.DESCENDING).sortBy("id", Order.DESCENDING); QueryExecutor executor = bag.find(innerQuery); QueryResult result = executor.execute(); assertThat(result.size(), is(2l)); } finally { JasDBMain.shutdown(); } }
@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(); } }
@Test public void testNotEqualsMultiIndexes() throws Exception { try { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("websites"); bag.ensureIndex(new SimpleIndexField("url", new StringKeyType()), false); bag.ensureIndex( new SimpleCompositeIndexField( new SimpleIndexField("stepid", new StringKeyType()), new SimpleIndexField("workflow", new LongKeyType()) ),false); bag.addEntity(new SimpleEntity().addProperty("url", "").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "http://someurl.nl/1").addProperty("stepid", 1L).addProperty("workflow", 1L)); bag.addEntity(new SimpleEntity().addProperty("url", "http://someurl.nl/2").addProperty("stepid", 1L).addProperty("workflow", 1L)); QueryResult r = bag.find(QueryBuilder.createBuilder() .field("stepid").value(1L) .field("workflow").value(1L) .field("url").notEquals("")).execute(); assertThat(r.size(), is(2L)); } finally { JasDBMain.shutdown(); } }
@Test public void testPersistMultiValue() throws Exception { DBSession session = sessionFactory.createSession(); EntityBag bag = session.createOrGetBag("testbag"); try { Entity entity = new SimpleEntity(); entity.addProperty("field1", "value1"); entity.addProperty("field1", "value2"); entity.addProperty("field1", "value3"); entity.addProperty("number", 100L); entity.addProperty("number", 500L); bag.addEntity(entity); String entityId = entity.getInternalId(); entity = bag.getEntity(entityId); Property property = entity.getProperty("field1"); Assert.assertNotNull(property); assertEquals("The object should be multivalue", true, property.isMultiValue()); assertEquals("There should be three properties", 3, property.getValues().size()); assertEquals("Unexpected value", "value1", property.getValues().get(0).getValue()); assertEquals("Unexpected value", "value2", property.getValues().get(1).getValue()); assertEquals("Unexpected value", "value3", property.getValues().get(2).getValue()); property = entity.getProperty("number"); assertEquals("The object should be multivalue", true, property.isMultiValue()); assertEquals("There should be three properties", 2, property.getValues().size()); assertEquals("Unexpected value", 100L, property.getValues().get(0).getValue()); assertEquals("Unexpected value", 500L, property.getValues().get(1).getValue()); } finally { JasDBMain.shutdown(); } }
@Test public void testKeySpecialCharacters() throws Exception { DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("inverted"); bag.addEntity(new SimpleEntity().addProperty("field1", "coëfficiënt van Poisson")); try { QueryExecutor executor = bag.find(QueryBuilder.createBuilder().field("field1").value("coëfficiënt van Poisson")); QueryResult result = executor.execute(); assertTrue(result.hasNext()); Entity entity = result.next(); assertEquals("coëfficiënt van Poisson", entity.getProperty("field1").getFirstValueObject()); assertFalse(result.hasNext()); } finally { pojoDb.closeSession(); JasDBMain.shutdown(); } }
@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()); } }