use of org.apache.gora.persistency.impl.DirtyMapWrapper in project gora by apache.
the class AccumuloStore method populate.
public ByteSequence populate(Iterator<Entry<Key, Value>> iter, T persistent) throws IOException {
ByteSequence row = null;
Map<Utf8, Object> currentMap = null;
List currentArray = null;
Text currentFam = null;
int currentPos = 0;
Schema currentSchema = null;
Field currentField = null;
BinaryDecoder decoder = DecoderFactory.get().binaryDecoder(new byte[0], null);
while (iter.hasNext()) {
Entry<Key, Value> entry = iter.next();
if (row == null) {
row = entry.getKey().getRowData();
}
byte[] val = entry.getValue().get();
Field field = fieldMap.get(getFieldName(entry));
if (currentMap != null) {
if (currentFam.equals(entry.getKey().getColumnFamily())) {
currentMap.put(new Utf8(entry.getKey().getColumnQualifierData().toArray()), fromBytes(currentSchema, entry.getValue().get()));
continue;
} else {
persistent.put(currentPos, currentMap);
currentMap = null;
}
} else if (currentArray != null) {
if (currentFam.equals(entry.getKey().getColumnFamily())) {
currentArray.add(fromBytes(currentSchema, entry.getValue().get()));
continue;
} else {
persistent.put(currentPos, new GenericData.Array<T>(currentField.schema(), currentArray));
currentArray = null;
}
}
switch(field.schema().getType()) {
case // first entry only. Next are handled above on the next loop
MAP:
currentMap = new DirtyMapWrapper<>(new HashMap<Utf8, Object>());
currentPos = field.pos();
currentFam = entry.getKey().getColumnFamily();
currentSchema = field.schema().getValueType();
currentMap.put(new Utf8(entry.getKey().getColumnQualifierData().toArray()), fromBytes(currentSchema, entry.getValue().get()));
break;
case ARRAY:
currentArray = new DirtyListWrapper<>(new ArrayList<>());
currentPos = field.pos();
currentFam = entry.getKey().getColumnFamily();
currentSchema = field.schema().getElementType();
currentField = field;
currentArray.add(fromBytes(currentSchema, entry.getValue().get()));
break;
case // default value of null acts like union with null
UNION:
Schema effectiveSchema = field.schema().getTypes().get(firstNotNullSchemaTypeIndex(field.schema()));
// map and array were coded without union index so need to be read the same way
if (effectiveSchema.getType() == Type.ARRAY) {
currentArray = new DirtyListWrapper<>(new ArrayList<>());
currentPos = field.pos();
currentFam = entry.getKey().getColumnFamily();
currentSchema = field.schema().getElementType();
currentField = field;
currentArray.add(fromBytes(currentSchema, entry.getValue().get()));
break;
} else if (effectiveSchema.getType() == Type.MAP) {
currentMap = new DirtyMapWrapper<>(new HashMap<Utf8, Object>());
currentPos = field.pos();
currentFam = entry.getKey().getColumnFamily();
currentSchema = effectiveSchema.getValueType();
currentMap.put(new Utf8(entry.getKey().getColumnQualifierData().toArray()), fromBytes(currentSchema, entry.getValue().get()));
break;
}
// continue like a regular top-level union
case RECORD:
SpecificDatumReader<?> reader = new SpecificDatumReader<Schema>(field.schema());
persistent.put(field.pos(), reader.read(null, DecoderFactory.get().binaryDecoder(val, decoder)));
break;
default:
persistent.put(field.pos(), fromBytes(field.schema(), entry.getValue().get()));
}
}
if (currentMap != null) {
persistent.put(currentPos, currentMap);
} else if (currentArray != null) {
persistent.put(currentPos, new GenericData.Array<T>(currentField.schema(), currentArray));
}
persistent.clearDirty();
return row;
}
use of org.apache.gora.persistency.impl.DirtyMapWrapper in project gora by apache.
the class MongoStore method fromMongoMap.
/* pp */
Object fromMongoMap(final String docf, final Schema fieldSchema, final BSONDecorator easybson, final Field f) {
BasicDBObject map = easybson.getDBObject(docf);
Map<Utf8, Object> rmap = new HashMap<>();
if (map == null) {
return new DirtyMapWrapper(rmap);
}
for (Entry<String, Object> e : map.entrySet()) {
String mapKey = e.getKey();
String decodedMapKey = decodeFieldKey(mapKey);
DocumentFieldType storeType = mapping.getDocumentFieldType(docf);
Object o = fromDBObject(fieldSchema.getValueType(), storeType, f, mapKey, new BSONDecorator(map));
rmap.put(new Utf8(decodedMapKey), o);
}
return new DirtyMapWrapper<>(rmap);
}
use of org.apache.gora.persistency.impl.DirtyMapWrapper in project gora by apache.
the class CouchDBStore method fromCouchDBMap.
private Object fromCouchDBMap(final Schema fieldSchema, final Field field, final String docf, final Object value) {
final Map<String, Object> map = (Map<String, Object>) ((Map<String, Object>) value).get(docf);
final Map<Utf8, Object> rmap = new HashMap<>();
if (map == null) {
return new DirtyMapWrapper(rmap);
}
for (Map.Entry<String, Object> e : map.entrySet()) {
Schema innerSchema = fieldSchema.getValueType();
;
Object o = fromDBObject(innerSchema, field, e.getKey(), e.getValue());
rmap.put(new Utf8(e.getKey()), o);
}
return new DirtyMapWrapper<>(rmap);
}
use of org.apache.gora.persistency.impl.DirtyMapWrapper in project gora by apache.
the class AccumuloStore method putMap.
private int putMap(Mutation m, int count, Schema valueType, Object o, Pair<Text, Text> col, String fieldName) throws GoraException {
// First of all we delete map field on accumulo store
Text rowKey = new Text(m.getRow());
Query<K, T> query = newQuery();
query.setFields(fieldName);
query.setStartKey((K) rowKey.toString());
query.setEndKey((K) rowKey.toString());
deleteByQuery(query);
flush();
if (o == null) {
return 0;
}
Set<?> es = ((Map<?, ?>) o).entrySet();
for (Object entry : es) {
Object mapKey = ((Entry<?, ?>) entry).getKey();
Object mapVal = ((Entry<?, ?>) entry).getValue();
if ((o instanceof DirtyMapWrapper && ((DirtyMapWrapper<?, ?>) o).isDirty()) || !(o instanceof DirtyMapWrapper)) {
m.put(col.getFirst(), new Text(toBytes(mapKey)), new Value(toBytes(valueType, mapVal)));
count++;
}
// TODO map value deletion
}
return count;
}
Aggregations