Search in sources :

Example 1 with DirtyMapWrapper

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;
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) HashMap(java.util.HashMap) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Text(org.apache.hadoop.io.Text) BinaryDecoder(org.apache.avro.io.BinaryDecoder) Field(org.apache.avro.Schema.Field) Value(org.apache.accumulo.core.data.Value) Utf8(org.apache.avro.util.Utf8) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) SpecificDatumReader(org.apache.avro.specific.SpecificDatumReader) ByteSequence(org.apache.accumulo.core.data.ByteSequence) Key(org.apache.accumulo.core.data.Key)

Example 2 with DirtyMapWrapper

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);
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) DocumentFieldType(org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType) BSONDecorator(org.apache.gora.mongodb.utils.BSONDecorator) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Utf8(org.apache.avro.util.Utf8)

Example 3 with DirtyMapWrapper

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);
}
Also used : DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) Schema(org.apache.avro.Schema) Utf8(org.apache.avro.util.Utf8)

Example 4 with DirtyMapWrapper

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;
}
Also used : Entry(java.util.Map.Entry) DirtyMapWrapper(org.apache.gora.persistency.impl.DirtyMapWrapper) Value(org.apache.accumulo.core.data.Value) Text(org.apache.hadoop.io.Text) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

DirtyMapWrapper (org.apache.gora.persistency.impl.DirtyMapWrapper)4 Utf8 (org.apache.avro.util.Utf8)3 HashMap (java.util.HashMap)2 Value (org.apache.accumulo.core.data.Value)2 Schema (org.apache.avro.Schema)2 Text (org.apache.hadoop.io.Text)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ByteSequence (org.apache.accumulo.core.data.ByteSequence)1 Key (org.apache.accumulo.core.data.Key)1 Field (org.apache.avro.Schema.Field)1 BinaryDecoder (org.apache.avro.io.BinaryDecoder)1 SpecificDatumReader (org.apache.avro.specific.SpecificDatumReader)1 DocumentFieldType (org.apache.gora.mongodb.store.MongoMapping.DocumentFieldType)1 BSONDecorator (org.apache.gora.mongodb.utils.BSONDecorator)1 NodeList (org.w3c.dom.NodeList)1