private int getResultSize(QueryResult result) { int foundEntities = 0; for(Entity entity : result) { foundEntities++; log.debug("Iterating entity: {} found: {}", entity.getInternalId(), foundEntities); } return foundEntities; }
private void runIdGeneration(Entity entity) throws JasDBStorageException { if(entity.getInternalId() == null || entity.getInternalId().isEmpty()) { entity.setInternalId(generator.generateNewId()); } }
@Override public void removeEntity(Entity entity) throws JasDBStorageException { removeEntity(entity.getInternalId()); }
private List<WebEntity> loadEntities(QueryResult result) throws JasDBException { List<WebEntity> entities = new ArrayList<>(); for(Entity entity : result) { WebEntity webEntity = new WebEntity(); webEntity.setData(SimpleEntity.toJson(entity)); webEntity.setId(entity.getInternalId()); entities.add(webEntity); } return entities; } }
public static Key entityToKey(Entity entity) throws JasDBStorageException { CompositeKey compositeKey = new CompositeKey(); compositeKey.addKey(DEFAULT_DOC_ID_MAPPER, SimpleEntity.DOCUMENT_ID, new UUIDKey(entity.getInternalId())); return compositeKey; } }
@Override public void doDataOperation(String instanceId, String bag, Entity entity) throws JasDBStorageException { RecordWriter<UUIDKey> recordWriter = recordWriterFactoryLoader.loadRecordWriter(instanceId, bag); RecordResult recordResult = recordWriter.readRecord(new UUIDKey(entity.getInternalId())); if(recordResult.isRecordFound()) { Entity removeEntity = BagOperationUtil.toEntity(recordResult.getStream()); if(removeEntity != null) { recordWriter.removeRecord(new UUIDKey(entity.getInternalId())); removeFromIndex(instanceId, bag, removeEntity); } else { throw new JasDBStorageException("Unable to remove entity with id: " + entity.getInternalId() + ", cannot be deserialed from storage"); } } else { throw new JasDBStorageException("Unable to delete entity with id: " + entity.getInternalId() + ", could not be found"); } }
@Override public void doDataOperation(String instanceId, String bag, Entity entity) throws JasDBStorageException { UUIDKey documentKey = new UUIDKey(entity.getInternalId()); RecordWriter<UUIDKey> recordWriter = recordWriterFactoryLoader.loadRecordWriter(instanceId, bag); RecordResult result = recordWriter.readRecord(documentKey); if(result != null && result.isRecordFound()) { Entity oldEntity = BagOperationUtil.toEntity(result.getStream()); //let's update the actual record, so we can get the new record pointer and set this on the entity log.debug("Updating record: {}", entity.getInternalId()); recordWriter.updateRecord(documentKey, BagOperationUtil.toStream(entity)); doIndexModifications(instanceId, bag, oldEntity, entity); } else { throw new JasDBStorageException("Unable to update entity: " + entity.getInternalId() + " record does not exist"); } }
@Override public void doDataOperation(String instanceId, String bag, Entity entity) throws JasDBStorageException { UUIDKey documentKey = new UUIDKey(entity.getInternalId()); RecordWriter<UUIDKey> recordWriter = recordWriterFactory.loadRecordWriter(instanceId, bag); RecordResult result = recordWriter.readRecord(documentKey); if(result != null && result.isRecordFound()) { LOG.debug("Persist operation found existing entity, redirecting to update operation for entity: {}", entity); updateOperation.doDataOperation(instanceId, bag, entity); } else { LOG.debug("No existing entity was found, inserting data: {}", entity); insertOperation.doDataOperation(instanceId, bag, entity); } }
List<String> assertResult(int start, int amount, QueryResult result) { List<String> keysFoundInOrder = new ArrayList<>(); for(int i=start; i<(start + amount) && result.hasNext(); i++) { Entity entity = result.next(); String expectedId = longToId.get((long) i); assertNotNull("There should be a returned entity", entity); assertEquals("The id's should match", expectedId, entity.getInternalId()); Property property = entity.getProperty("field1"); assertNotNull("Property should be set", property); assertTrue("Property should be String", property.getFirstValueObject() instanceof String); assertEquals("Property value should match", "value" + i, property.getFirstValueObject()); keysFoundInOrder.add(entity.getInternalId()); } return keysFoundInOrder; }
@Test public void testEmptyCollections() throws Exception { DBSession session = sessionFactory.createSession(); EntityManager entityManager = session.getEntityManager(); try { String id = entityManager.persist(new TestEntity(null, "Piet", "de Klos", newArrayList(), new HashMap<>())).getInternalId(); TestEntity entity = entityManager.findEntity(TestEntity.class, id); assertThat(entity, notNullValue()); assertThat(entity.getFirstName(), is("Piet")); assertThat(entity.getLastName(), is("de Klos")); assertThat(entity.getHobbies().size(), is(0)); assertThat(entity.getProperties().size(), is(0)); } finally { session.closeSession(); 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 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()); }
@Override public void updateEntity(RequestContext context, Entity entity) throws JasDBStorageException { resourceLockManager.sharedLock(); try { if(StringUtils.stringNotEmpty(entity.getInternalId())) { bagUpdateOperation.doDataOperation(instanceId, bagName, entity); } else { throw new JasDBStorageException("Unable to update record, entity has no id specified"); } } finally { resourceLockManager.sharedUnlock(); } }
@Override public void removeEntity(RequestContext context, Entity entity) throws JasDBStorageException { resourceLockManager.sharedLock(); try { if(StringUtils.stringNotEmpty(entity.getInternalId())) { bagRemoveOperation.doDataOperation(instanceId, bagName, entity); } else { throw new JasDBStorageException("Unable to remove record, entity has no id specified"); } } finally { resourceLockManager.sharedUnlock(); } }
private RestEntity doModificationOperation(String instanceId, String bagName, String rawData, RequestContext context, OPERATION_TYPE type) throws RestException { if(stringNotEmpty(instanceId) && stringNotEmpty(bagName)) { LOG.debug("Raw entity data received: {}", rawData); StreamedEntity streamedEntity = ENTITY_HANDLER.deserialize(StreamedEntity.class, rawData); Entity storeEntity = streamedEntity.getEntity(); try { StorageService storageService = storageServiceFactory.getOrCreateStorageService(instanceId, bagName); if(type == OPERATION_TYPE.UPDATE) { LOG.debug("Updating entity with id: {}", storeEntity.getInternalId()); storageService.persistEntity(context, storeEntity); } else if(type == OPERATION_TYPE.INSERT) { LOG.debug("Inserting new entity into bag: {}", bagName); storageService.insertEntity(context, storeEntity); } return new StreamedEntity(storeEntity); } catch(JasDBStorageException e) { throw new RestException("Unable to store entity: " + e.getMessage()); } } else { throw new SyntaxException("Cannot store entity, no Bag specified"); } }
@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(); } }
@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")); }
@Override public Entity insertEntity(RemotingContext context, String instance, String bag, Entity entity) throws RemoteException { String connectionString = new RestConnectionBuilder().instance(instance).bag(bag).entities().getConnectionString(); try { ByteArrayOutputStream bos = new ByteArrayOutputStream(); new JsonRestResponseHandler().serialize(new StreamedEntity(entity), bos); ClientResponse clientResponse = doInternalRequest(context, connectionString, new HashMap<>(), bos.toByteArray(), REQUEST_MODE.POST); try { StreamedEntity returnedEntity = new JsonRestResponseHandler().deserialize(StreamedEntity.class, clientResponse.getEntityInputStream()); entity.setInternalId(returnedEntity.getEntity().getInternalId()); return returnedEntity.getEntity(); } finally { clientResponse.close(); } } catch(RestException e) { throw new RemoteException("Unable to insert entity on remote destination", e); } }
@Override public void doDataOperation(String instanceId, String bag, Entity entity) throws JasDBStorageException { ClonableDataStream entityStream = BagOperationUtil.toStream(entity); if(entityStream != null) { StatRecord bagWrite = StatisticsMonitor.createRecord("bag:writeRecord"); recordWriterFactory.loadRecordWriter(instanceId, bag).writeRecord(new UUIDKey(entity.getInternalId()), entityStream); bagWrite.stop(); StatRecord bagIndexUpdate = StatisticsMonitor.createRecord("bag:indexUpdate"); insertIntoIndexes(instanceId, bag, entity); bagIndexUpdate.stop(); } else { throw new JasDBStorageException("Invalid entity, can't insert empty entity"); } }
@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(); } }