private Object fromCouchDBEnum(final Schema fieldSchema, final String docf, final Object value) { final Object result; if (value instanceof Map) { result = AvroUtils.getEnumValue(fieldSchema, (String) ((Map) value).get(docf)); } else { result = AvroUtils.getEnumValue(fieldSchema, (String) value); } return result; }
/** * Return the field names from a persistent object. * * @param persistent the persistent object to get the fields names from. * @return the field names String array. */ public static String[] getPersistentFieldNames(PersistentBase persistent) { return getSchemaFieldNames(persistent.getSchema()); }
/** * Returns a clone with exactly the requested fields shallowly copied */ private static<T extends PersistentBase> T getPersistent(T obj, String[] fields) { List<Field> otherFields = obj.getSchema().getFields(); String[] otherFieldStrings = new String[otherFields.size()]; for(int i = 0; i<otherFields.size(); i++ ){ otherFieldStrings[i] = otherFields.get(i).name(); } if(Arrays.equals(fields, otherFieldStrings)) { return obj; } T newObj = AvroUtils.deepClonePersistent(obj); newObj.clear(); for (String field : fields) { Field otherField = obj.getSchema().getField(field); int index = otherField.pos(); newObj.put(index, obj.get(index)); } return newObj; }
@Override public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) throws GoraException { setKeyClass(keyClass); setPersistentClass(persistentClass); if (this.beanFactory == null) { this.beanFactory = new BeanFactoryImpl<>(keyClass, persistentClass); } schema = this.beanFactory.getCachedPersistent().getSchema(); fieldMap = AvroUtils.getFieldMap(schema); autoCreateSchema = DataStoreFactory.getAutoCreateSchema(properties, this); this.properties = properties; datumReader = new SpecificDatumReader<>(schema); datumWriter = new SpecificDatumWriter<>(schema); }
/** * Returns a clone with exactly the requested fields shallowly copied */ private static<T extends PersistentBase> T getPersistent(T obj, String[] fields) { List<Field> otherFields = obj.getSchema().getFields(); String[] otherFieldStrings = new String[otherFields.size()]; for(int i = 0; i<otherFields.size(); i++ ){ otherFieldStrings[i] = otherFields.get(i).name(); } if(Arrays.equals(fields, otherFieldStrings)) { return obj; } T newObj = AvroUtils.deepClonePersistent(obj); newObj.clear(); for (String field : fields) { Field otherField = obj.getSchema().getField(field); int index = otherField.pos(); newObj.put(index, obj.get(index)); } return newObj; }
@Override public void initialize(Class<K> keyClass, Class<T> persistentClass, Properties properties) { setKeyClass(keyClass); setPersistentClass(persistentClass); if (this.beanFactory == null) { this.beanFactory = new BeanFactoryImpl<>(keyClass, persistentClass); } schema = this.beanFactory.getCachedPersistent().getSchema(); fieldMap = AvroUtils.getFieldMap(schema); autoCreateSchema = DataStoreFactory.getAutoCreateSchema(properties, this); this.properties = properties; datumReader = new SpecificDatumReader<>(schema); datumWriter = new SpecificDatumWriter<>(schema); }
private Object fromCouchDBEnum(final Schema fieldSchema, final String docf, final Object value) { final Object result; if (value instanceof Map) { result = AvroUtils.getEnumValue(fieldSchema, (String) ((Map) value).get(docf)); } else { result = AvroUtils.getEnumValue(fieldSchema, (String) value); } return result; }
private static <T extends PersistentBase> T getPersistent(T persitent, String[] fields) { List<Schema.Field> otherFields = persitent.getSchema().getFields(); String[] otherFieldStrings = new String[otherFields.size()]; for (int i = 0; i < otherFields.size(); i++) { otherFieldStrings[i] = otherFields.get(i).name(); } if (Arrays.equals(fields, otherFieldStrings)) { return persitent; } T clonedPersistent = AvroUtils.deepClonePersistent(persitent); clonedPersistent.clear(); if (fields != null && fields.length > 0) { for (String field : fields) { Schema.Field otherField = persitent.getSchema().getField(field); int index = otherField.pos(); clonedPersistent.put(index, persitent.get(index)); } } else { for (String field : otherFieldStrings) { Schema.Field otherField = persitent.getSchema().getField(field); int index = otherField.pos(); clonedPersistent.put(index, persitent.get(index)); } } return clonedPersistent; }
/** * Return the field names from a persistent object. * * @param persistent the persistent object to get the fields names from. * @return the field names String array. */ public static String[] getPersistentFieldNames(PersistentBase persistent) { return getSchemaFieldNames(persistent.getSchema()); }
public static Object getEnumValue(Schema schema, int enumOrdinal) { String symbol = schema.getEnumSymbols().get(enumOrdinal); return getEnumValue(schema, symbol); }
private static <T extends PersistentBase> T getPersistent(T persitent, String[] fields) { List<Schema.Field> otherFields = persitent.getSchema().getFields(); String[] otherFieldStrings = new String[otherFields.size()]; for (int i = 0; i < otherFields.size(); i++) { otherFieldStrings[i] = otherFields.get(i).name(); } if (Arrays.equals(fields, otherFieldStrings)) { return persitent; } T clonedPersistent = AvroUtils.deepClonePersistent(persitent); clonedPersistent.clear(); if (fields != null && fields.length > 0) { for (String field : fields) { Schema.Field otherField = persitent.getSchema().getField(field); int index = otherField.pos(); clonedPersistent.put(index, persitent.get(index)); } } else { for (String field : otherFieldStrings) { Schema.Field otherField = persitent.getSchema().getField(field); int index = otherField.pos(); clonedPersistent.put(index, persitent.get(index)); } } return clonedPersistent; }
public static void testGetEmployee(DataStore<String, Employee> dataStore) throws Exception { dataStore.createSchema(); Employee employee = DataStoreTestUtil.createEmployee(); String ssn = employee.getSsn().toString(); dataStore.put(ssn, employee); dataStore.flush(); Employee after = dataStore.get(ssn, AvroUtils.getSchemaFieldNames(Employee.SCHEMA$)); assertEqualEmployeeObjects(employee, after); }
public static Object getEnumValue(Schema schema, int enumOrdinal) { String symbol = schema.getEnumSymbols().get(enumOrdinal); return getEnumValue(schema, symbol); }
@Test public void testDeepClonePersistent() throws Exception { CharSequence url = new Utf8("http://gora.apache.org/"); WebPage.Builder builder = WebPage.newBuilder() .setUrl(url) .setContent(ByteBuffer.wrap("Gora".getBytes("UTF-8"))); WebPage webPage = builder.build(); WebPage clonedWebPage = AvroUtils.deepClonePersistent(webPage); assertThat(clonedWebPage, is(notNullValue())); assertThat(clonedWebPage.getUrl(), is(equalTo(url))); assertThat(clonedWebPage.getContent(), is(notNullValue())); String clonedWebPageContent = new String(clonedWebPage.getContent().array(), "UTF-8"); assertThat(clonedWebPageContent, is(equalTo("Gora"))); }
public static void testGetEmployee3UnionField(DataStore<String, Employee> dataStore) throws Exception { Employee employee = DataStoreTestUtil.createEmployee(); employee.setBoss(new Utf8("Real boss")) ; String ssn = employee.getSsn().toString(); dataStore.put(ssn, employee); dataStore.flush(); Employee after = dataStore.get(ssn, AvroUtils.getSchemaFieldNames(Employee.SCHEMA$)); assertEqualEmployeeObjects(employee, after); assertEquals("Real boss", after.getBoss().toString()) ; }
result = AvroUtils.getEnumValue(schema, (String) binValue); break;
public static void testGetEmployeeRecursive(DataStore<String, Employee> dataStore) throws Exception { Employee employee = DataStoreTestUtil.createEmployee(); Employee boss = DataStoreTestUtil.createBoss(); employee.setBoss(boss); String ssn = employee.getSsn().toString(); dataStore.put(ssn, employee); dataStore.flush(); Employee after = dataStore.get(ssn, AvroUtils.getSchemaFieldNames(Employee.SCHEMA$)); assertEqualEmployeeObjects(employee, after); }
result = org.apache.gora.util.AvroUtils.getEnumValue(schema, (String) value); break;
public static void testGetEmployeeNested(DataStore<String, Employee> dataStore) throws Exception { Employee employee = DataStoreTestUtil.createEmployee(); WebPage webpage = new BeanFactoryImpl<>(String.class,WebPage.class).newPersistent() ; webpage.setUrl(new Utf8("url..")) ; webpage.setContent(ByteBuffer.wrap("test content".getBytes(Charset.defaultCharset()))) ; webpage.setParsedContent(new ArrayList<CharSequence>()); Metadata metadata = new BeanFactoryImpl<>(String.class,Metadata.class).newPersistent(); webpage.setMetadata(metadata) ; employee.setWebpage(webpage) ; String ssn = employee.getSsn().toString(); dataStore.put(ssn, employee); dataStore.flush(); Employee after = dataStore.get(ssn, AvroUtils.getSchemaFieldNames(Employee.SCHEMA$)); assertEqualEmployeeObjects(employee, after); assertEqualWebPageObjects(webpage, after.getWebpage()); }
result = org.apache.gora.util.AvroUtils.getEnumValue(schema, (String) value); break;