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(); } }
@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")); }
@Override public <T> T findEntity(Class<T> type, String entityId) throws JasDBStorageException { EntityMetadata entityMetadata = ENTITY_MAPPER.getEntityMetadata(type); EntityBag bag = session.getBag(entityMetadata.getBagName()); if(bag != null) { Entity entity = bag.getEntity(entityId); return ENTITY_MAPPER.mapFrom(type, entity); } return null; }
@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 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 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(); } }
Entity entity = bag.getEntity(id); Assert.assertNotNull("Entity for id: " + id + " should be found", entity); assertEquals("Id should match expected id", id, entity.getInternalId());
entity1 = bag.getEntity(entity1.getInternalId()); assertNotNull(entity1); assertEquals(entity1Id, entity1.getInternalId()); bag.updateEntity(entity1); entity2 = bag.getEntity(entity2.getInternalId()); assertNotNull(entity2); assertEquals(entity2Id, entity2.getInternalId()); EntityBag bag = pojoDb.createOrGetBag("mybag"); entity1 = bag.getEntity(entity1Id); assertNotNull(entity1); assertEquals(entity1Id, entity1.getInternalId()); assertEquals("My value for entity 1", entity1.getProperty("testProperty").getFirstValueObject()); entity2 = bag.getEntity(entity2Id); assertNotNull(entity2); assertEquals(entity2Id, entity2.getInternalId());
bag = session.createOrGetBag("testbag"); Entity foundEntity = bag.getEntity(documentId); assertEquals("Internal Doc id's should be the same", documentId, foundEntity.getInternalId());