@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 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(); } }
@Test public void testRandomPersistUpdateBigRecordUpdate() throws Exception { String htmlData = ResourceUtil.getContent("datasets/htmlpage.data", "UTF-8"); 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"); entity.setProperty("bigdatafield", htmlData); bag.updateEntity(entity); changeCityCount(city, cityCounts, false); changeCityCount("unknown", cityCounts, true); } } assertCityIndexes(bag, cities, cityCounts); } finally { JasDBMain.shutdown(); } }
assertEquals(entity1Id, entity1.getInternalId()); entity1.addProperty("testProperty", "My value for entity 1"); bag.updateEntity(entity1); assertEquals(entity2Id, entity2.getInternalId()); entity2.addProperty("someProp", "Value 1 smaller"); bag.updateEntity(entity2);
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"));