private int getResultSize(QueryResult result) { int foundEntities = 0; for(Entity entity : result) { foundEntities++; log.debug("Iterating entity: {} found: {}", entity.getInternalId(), foundEntities); } return foundEntities; }
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 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")); }
private void runIdGeneration(Entity entity) throws JasDBStorageException { if(entity.getInternalId() == null || entity.getInternalId().isEmpty()) { entity.setInternalId(generator.generateNewId()); } }
public static BagMeta fromEntity(Entity entity) throws JasDBStorageException { String instance = entity.getValue(Constants.INSTANCE).toString(); String name = entity.getValue(Constants.NAME).toString(); List<IndexDefinition> indexDefinitionList = new ArrayList<>(); if(entity.hasProperty(Constants.INDEXES)) { for(Object indexDefinition : entity.getValues(Constants.INDEXES)) { indexDefinitionList.add(IndexDefinition.fromHeader(indexDefinition.toString())); } } return new BagMeta(instance, name, indexDefinitionList); }
public static GrantObject fromEntity(Entity entity) { String grantObject = entity.getValue(Constants.GRANT_OBJECT).toString(); Map<String, Grant> userGrants = new ConcurrentHashMap<>(); Property grantsProperty = entity.getProperty(Constants.GRANTS); for(Value grantValue : grantsProperty.getValues()) { EntityValue entityValue = (EntityValue) grantValue; String grantUser = entityValue.toEntity().getValue(Constants.GRANT_USER).toString(); String grantMode = entityValue.toEntity().getValue(Constants.GRANT_MODE).toString(); userGrants.put(grantUser, new GrantMeta(grantUser, AccessMode.fromMode(grantMode))); } return new GrantObjectMeta(grantObject, userGrants); }
@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(); } }
assertEquals("Internal Doc id's should be the same", documentId, foundEntity.getInternalId()); assertTrue(foundEntity.hasProperty("simpleProperty1")); assertTrue(foundEntity.hasProperty("multiValueProperty")); assertTrue(foundEntity.hasProperty("integerProperty")); assertEquals(new Long(100), foundEntity.getProperty("simpleProperty1").getFirstValueObject()); assertEquals(3, foundEntity.getProperty("multiValueProperty").getValues().size()); assertEquals("value1", foundEntity.getProperty("multiValueProperty").getValues().get(0).getValue()); assertEquals("value2", foundEntity.getProperty("multiValueProperty").getValues().get(1).getValue()); assertEquals("value3", foundEntity.getProperty("multiValueProperty").getValues().get(2).getValue()); assertEquals(new Long(200), foundEntity.getProperty("integerProperty").getFirstValueObject()); assertTrue("There should be an embedded entity", foundEntity.hasProperty("embedded")); Object embedded = foundEntity.getProperty("embedded").getFirstValueObject(); assertNotNull(embedded); assertEquals("Embedded entity should be of type SimpleEntity", EmbeddedEntity.class, embedded.getClass());
List<String> getEntityValue(List<Entity> entities, final String property) { return Lists.transform(entities, entity -> { assert entity != null; return entity.getProperty(property).getFirstValue().toString(); }); }
private void handleValues(JsonParser parser, String field, boolean isCollection, Entity entity) throws IOException, MetadataParseException { JsonToken token = parser.getCurrentToken(); while(token != JsonToken.END_ARRAY && token != JsonToken.FIELD_NAME && token != JsonToken.END_OBJECT) { if(token.isNumeric()) { long value = parser.getLongValue(); entity.addProperty(field, isCollection, value); } else if(token == JsonToken.VALUE_TRUE || token == JsonToken.VALUE_FALSE) { entity.addProperty(field, parser.getBooleanValue()); } else if(token == JsonToken.START_OBJECT) { EmbeddedEntity embeddedEntity = new EmbeddedEntity(); if(handleProperties(parser, embeddedEntity) != null) { entity.addEntity(field, embeddedEntity); } } else if(token == JsonToken.VALUE_STRING) { String value = parser.getText(); if(SimpleEntity.DOCUMENT_ID.equals(field)) { entity.setInternalId(value); } else { entity.addProperty(field, isCollection, value); } } else if(token != JsonToken.VALUE_NULL) { throw new MetadataParseException("Unexpected token in entity: " + token); } token = parser.nextToken(); } }
public static User fromEntity(Entity entity) { String userName = entity.getValue(Constants.USER_NAME).toString(); String host = entity.getValue(Constants.HOST).toString(); String contentKey = entity.getValue(Constants.USER_CONTENT_KEY).toString(); String salt = entity.getValue(Constants.SALT).toString(); String hash = entity.getValue(Constants.USER_PASSWORD_HASH).toString(); String engine = entity.getValue(Constants.USER_ENGINE).toString(); return new UserMeta(userName, host, contentKey, salt, hash, engine); }
@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(); } }
if(token != JsonToken.VALUE_NULL) { String id = parser.getText(); entity.setInternalId(id);
@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; } }
JasDBMain.shutdown(); String entity1Id = entity1.getInternalId(); String entity2Id = entity2.getInternalId(); entity1 = bag.getEntity(entity1.getInternalId()); assertNotNull(entity1); assertEquals(entity1Id, entity1.getInternalId()); entity1.addProperty("testProperty", "My value for entity 1"); bag.updateEntity(entity1); entity2 = bag.getEntity(entity2.getInternalId()); assertNotNull(entity2); assertEquals(entity2Id, entity2.getInternalId()); entity2.addProperty("someProp", "Value 1 smaller"); bag.updateEntity(entity2); assertEquals(entity1Id, entity1.getInternalId()); assertEquals("My value for entity 1", entity1.getProperty("testProperty").getFirstValueObject()); assertEquals(entity2Id, entity2.getInternalId()); assertEquals("Value 1 smaller", entity2.getProperty("someProp").getFirstValueObject()); } finally { JasDBMain.shutdown();
@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); } }
@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 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(); } }
generator.writeStartObject(); for(Property property : entity.getProperties()) { if(property.hasValues()) { if(property.isMultiValue()) {