@Override public Object mapFromProperty(PropertyMetadata propertyMetadata, Property property) { return property.getValueObjects(); } }
@Override public Object mapFromProperty(PropertyMetadata propertyMetadata, Property property) { return property.getFirstValueObject(); } }
@Override public String getInstanceId() throws JasDBStorageException { return instance != null ? instance.getInstanceId() : null; }
@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 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(); } }
@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()); }
@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); }
List<String> getEntityValue(List<Entity> entities, final String property) { return Lists.transform(entities, entity -> { assert entity != null; return entity.getProperty(property).getFirstValue().toString(); }); }
@Test public void testCreateAndGetInstanceBag() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); session.createOrGetBag(MY_INSTANCE, BAG_1); //default instance should be null assertNull(session.getBag(BAG_1)); assertNotNull(MY_INSTANCE, session.getBag(MY_INSTANCE, BAG_1)); }
@Override public Object mapFromProperty(PropertyMetadata propertyMetadata, Property property) { EntityValue entityValue = (EntityValue) property.getFirstValue(); Entity embeddedEntity = entityValue.getValue(); List<Property> embeddedProperties = embeddedEntity.getProperties(); Map<String, Object> properties = new HashMap<>(); embeddedProperties.forEach(p -> { properties.put(p.getPropertyName(), p.getFirstValue().getValue()); }); return properties; } }
@Test public void testSwitchInstance() throws JasDBException, IOException { DBSession session = sessionFactory.createSession(); session.addInstance(MY_INSTANCE); assertEquals("default", session.getInstanceId()); session.switchInstance(MY_INSTANCE); assertEquals(MY_INSTANCE, session.getInstanceId()); session.addAndSwitchInstance("anotherInstance"); assertEquals("anotherInstance", session.getInstanceId()); }
@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; }
@RequestMapping(value = "/{instanceId}/delete", method = RequestMethod.GET) public String deleteInstance(@PathVariable String instanceId) throws JasDBException { DBSession session = sessionFactory.createSession(); session.deleteInstance(instanceId); return "redirect:/data/"; }
@RequestMapping(value = "/{instanceId}/{bag}/delete", method = RequestMethod.GET) public String deleteBag(@PathVariable String instanceId, @PathVariable String bag) throws JasDBException { DBSession session = sessionFactory.createSession(instanceId); session.removeBag(instanceId, bag); return "redirect:/data/"; }
@Override public boolean isMultiValueKey(IndexableItem indexableItem) throws JasDBStorageException { return indexableItem.isMultiValue(field); }
@Override public void removeBag(String bagName) throws JasDBStorageException { instance.removeBag(bagName); }
private static boolean isIndexable(KeyInfo keyInfo, IndexableItem item) { for(String field : keyInfo.getKeyFields()) { if(!item.hasValue(field)) { return false; } } return true; }
@Override public Object mapFromProperty(PropertyMetadata propertyMetadata, Property property) { return property.getFirstValueObject(); } }
@Override public <T> List<T> getValues(String propertyName) { Property property = getProperty(propertyName); if(property != null) { return property.getValueObjects(); } return null; }