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); }
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 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")); }
@Test public void testNotEqualsAge() throws Exception { DBSession pojoDb = sessionFactory.createSession(); EntityBag bag = pojoDb.createOrGetBag("inverted"); try { for(int age = 0; age < MAX_AGE; age++) { QueryExecutor executor = bag.find(QueryBuilder.createBuilder().field("age").notEquals(age)); long start = System.nanoTime(); long end = System.nanoTime(); long passed = end - start; LOG.info("Query execution took: {}", passed); try (QueryResult result = executor.execute()) { int expected = NUMBER_ENTITIES - ageAmounts.get((long) age); for (Entity entity : result) { assertThat(entity.getValue("age").toString(), not(equalTo(String.valueOf(age)))); } assertThat(result.size(), is((long) expected)); } } } finally { pojoDb.closeSession(); JasDBMain.shutdown(); } }
private void loadRecords() throws JasDBStorageException { for(RecordIteratorImpl recordIterator = writer.readAllRecords(); recordIterator.hasNext(); ) { RecordResultImpl recordResult = recordIterator.next(); Entity entity = SimpleEntity.fromStream(recordResult.getStream()); String type = entity.getValue(Constants.META_TYPE).toString(); if(type.equals(Constants.INSTANCE_TYPE)) { InstanceMeta instance = InstanceMeta.fromEntity(entity); instanceMetaMap.put(instance.getInstanceId(), new MetaWrapper<>(instance, recordResult.getRecordPointer())); } else if(type.equals(Constants.BAG_TYPE)) { BagMeta bagMeta = BagMeta.fromEntity(entity); String bagId = getBagKey(bagMeta.getInstanceId(), bagMeta.getName()); bagMetaMap.put(bagId, new MetaWrapper<>(bagMeta, recordResult.getRecordPointer())); } else if(metadataProviders.containsKey(type)) { metadataProviders.get(type).registerMetadataEntity(entity, recordResult.getRecordPointer()); } else { throw new JasDBStorageException("Unable to load metadata record: " + entity.toString() + " unknown type: " + type); } } }
@Test public void testSortDescendingInvalidType() throws Exception { DBSession session = sessionFactory.createSession(); try{ EntityBag bag = session.createOrGetBag("Bag"); SimpleEntity entity = new SimpleEntity(); entity.addProperty("name", "xxx"); entity.addProperty("v", "3"); bag.addEntity(entity); entity = new SimpleEntity(); entity.addProperty("name", 1); entity.addProperty("v", "1"); bag.addEntity(entity); entity = new SimpleEntity(); entity.addProperty("name", "xxx"); entity.addProperty("v", "2"); bag.addEntity(entity); QueryBuilder innerQuery = QueryBuilder.createBuilder(); innerQuery.field("name").value("xxx").sortBy("v", Order.DESCENDING); QueryExecutor executor = bag.find(innerQuery); QueryResult result = executor.execute(); assertThat(result.size(), is(2l)); assertThat(result.next().getValue("v"), is("3")); assertThat(result.next().getValue("v"), is("2")); } finally { JasDBMain.shutdown(); } }