Example #1
0
 /**
  * This method trims doc (1st arg) according to projected (2nd arg), which may have been modified
  * by projection. This method works in-place, meaning that the doc may be mutated.
  */
 private static void trim(DataMap doc, DataMap projected) {
   DataMap toAddDoc = new DataMap();
   Set<String> fields = doc.keySet();
   List<String> toRemoveDoc = new ArrayList<String>(fields.size());
   for (String f : fields) {
     Object v = doc.get(f);
     if (f.equals(PatchConstants.DELETE_COMMAND)) {
       DataList deletedFields = (DataList) v;
       DataList filteredDeleteFields = new DataList();
       for (Object patchDeleteField : deletedFields) {
         if (projected.containsKey(patchDeleteField)) {
           filteredDeleteFields.add(patchDeleteField);
         }
       }
       toRemoveDoc.add(f);
       if (!filteredDeleteFields.isEmpty()) {
         toAddDoc.put(PatchConstants.DELETE_COMMAND, filteredDeleteFields);
       }
     } else if (f.equals(PatchConstants.SET_COMMAND)) {
       DataMap setFields = (DataMap) v;
       Set<String> setFieldNames = setFields.keySet();
       List<String> toRemove = new LinkedList<String>();
       DataMap filteredSetFields = new DataMap();
       for (String setFieldName : setFieldNames) {
         if (projected.containsKey(setFieldName)) {
           filteredSetFields.put(setFieldName, projected.get(setFieldName));
         }
         toRemove.add(setFieldName);
       }
       for (String fieldToRemove : toRemove) {
         setFields.remove(fieldToRemove);
         if (filteredSetFields.containsKey(fieldToRemove)) {
           setFields.put(fieldToRemove, filteredSetFields.get(fieldToRemove));
         }
       }
       if (setFields.isEmpty()) {
         toRemoveDoc.add(f);
       }
     } else if (v instanceof DataMap) {
       if (projected.containsKey(f)) {
         trim((DataMap) v, (DataMap) projected.get(f));
       } else {
         toRemoveDoc.add(f);
       }
     }
   }
   // apply changes to doc
   for (String f : toRemoveDoc) {
     doc.remove(f);
   }
   for (String f : toAddDoc.keySet()) {
     doc.put(f, toAddDoc.get(f));
   }
 }
 private static List<Map.Entry<String, Object>> orderMapEntries(
     RecordDataSchema schema, DataMap map) {
   List<Map.Entry<String, Object>> output = new ArrayList<Map.Entry<String, Object>>(map.size());
   List<RecordDataSchema.Field> fields = schema.getFields();
   // collect fields in the record schema in the order the fields are declared
   for (RecordDataSchema.Field field : fields) {
     String fieldName = field.getName();
     Object found = map.get(fieldName);
     if (found != null) {
       output.add(new AbstractMap.SimpleImmutableEntry<String, Object>(fieldName, found));
     }
   }
   // collect fields that are in the DataMap that is not in the record schema.
   List<Map.Entry<String, Object>> uncollected =
       new ArrayList<Map.Entry<String, Object>>(map.size() - output.size());
   for (Map.Entry<String, Object> e : map.entrySet()) {
     if (schema.contains(e.getKey()) == false) {
       uncollected.add(e);
     }
   }
   Collections.sort(
       uncollected,
       new Comparator<Map.Entry<String, Object>>() {
         @Override
         public int compare(Map.Entry<String, Object> o1, Map.Entry<String, Object> o2) {
           return o1.getKey().compareTo(o2.getKey());
         }
       });
   output.addAll(uncollected);
   return output;
 }
Example #3
0
 /**
  * This method 'exposes' changes conveyed in the patch's meta commands to the main document.
  * Contents of $set commands are moved to the node which contains $set command. Names of removed
  * fields from $delete commands are moved to the nod which contains $delete command. The effect is
  * that patch will resemble structurally document which it is supposed to modify. This allows
  * application of projection to such patch to discover which changes relate to fields specified by
  * that projection. Examples: $delete: ['x', 'y', 'z'] => x: true, y: true, z: true $set: {x: 10,
  * y: {z: 'yeey'}, t: [10]} => x: 10, y: {z: 'yeey'}, t: [10]
  *
  * <p>This method works in-place, meaning that the doc may be mutated.
  */
 private static void expose(DataMap doc) {
   Set<String> fields = doc.keySet();
   DataMap toAdd = new DataMap();
   for (String f : fields) {
     Object v = doc.get(f);
     if (f.equals(PatchConstants.DELETE_COMMAND)) {
       for (Object removedFields : (DataList) v) {
         toAdd.put((String) removedFields, true);
       }
     } else if (f.equals(PatchConstants.SET_COMMAND)) {
       toAdd.putAll((DataMap) v);
     } else if (v instanceof DataMap) {
       expose((DataMap) v);
     }
   }
   doc.putAll(toAdd);
 }
Example #4
0
    private Object translate(Object value, DataSchema dataSchema, Schema avroSchema) {
      AvroOverride avroOverride = getAvroOverride(dataSchema);
      if (avroOverride != null) {
        return avroOverride
            .getCustomDataTranslator()
            .dataToAvroGeneric(this, value, dataSchema, avroSchema);
      }

      DataSchema dereferencedDataSchema = dataSchema.getDereferencedDataSchema();
      DataSchema.Type type = dereferencedDataSchema.getType();
      Object result;
      switch (type) {
        case NULL:
          if (value != Data.NULL) {
            appendMessage("value must be null for null schema");
            result = BAD_RESULT;
            break;
          }
          result = null;
          break;
        case BOOLEAN:
          result = ((Boolean) value).booleanValue();
          break;
        case INT:
          result = ((Number) value).intValue();
          break;
        case LONG:
          result = ((Number) value).longValue();
          break;
        case FLOAT:
          result = ((Number) value).floatValue();
          break;
        case DOUBLE:
          result = ((Number) value).doubleValue();
          break;
        case STRING:
          result = new Utf8((String) value);
          break;
        case BYTES:
          result = ByteBuffer.wrap(translateBytes(value));
          break;
        case ENUM:
          String enumValue = value.toString();
          EnumDataSchema enumDataSchema = (EnumDataSchema) dereferencedDataSchema;
          if (enumDataSchema.getSymbols().contains(enumValue) == false) {
            appendMessage(
                "enum value %1$s not one of %2$s", enumValue, enumDataSchema.getSymbols());
            result = BAD_RESULT;
            break;
          }
          result = _avroAdapter.createEnumSymbol(avroSchema, enumValue);
          break;
        case FIXED:
          byte[] bytes = translateBytes(value);
          FixedDataSchema fixedDataSchema = (FixedDataSchema) dereferencedDataSchema;
          if (fixedDataSchema.getSize() != bytes.length) {
            appendMessage(
                "ByteString size %1$d != FixedDataSchema size %2$d",
                bytes.length, fixedDataSchema.getSize());
            result = null;
            break;
          }
          GenericData.Fixed fixed = new GenericData.Fixed(avroSchema);
          fixed.bytes(bytes);
          result = fixed;
          break;
        case MAP:
          DataMap map = (DataMap) value;
          DataSchema valueDataSchema = ((MapDataSchema) dereferencedDataSchema).getValues();
          Schema valueAvroSchema = avroSchema.getValueType();
          Map<String, Object> avroMap = new HashMap<String, Object>(map.size());
          for (Map.Entry<String, Object> entry : map.entrySet()) {
            String key = entry.getKey();
            _path.addLast(key);
            Object entryAvroValue = translate(entry.getValue(), valueDataSchema, valueAvroSchema);
            _path.removeLast();
            avroMap.put(key, entryAvroValue);
          }
          result = avroMap;
          break;
        case ARRAY:
          DataList list = (DataList) value;
          DataSchema elementDataSchema = ((ArrayDataSchema) dereferencedDataSchema).getItems();
          Schema elementAvroSchema = avroSchema.getElementType();
          GenericData.Array<Object> avroList =
              new GenericData.Array<Object>(list.size(), avroSchema);
          for (int i = 0; i < list.size(); i++) {
            _path.addLast(i);
            Object entryAvroValue = translate(list.get(i), elementDataSchema, elementAvroSchema);
            _path.removeLast();
            avroList.add(entryAvroValue);
          }
          result = avroList;
          break;
        case RECORD:
          map = (DataMap) value;
          RecordDataSchema recordDataSchema = (RecordDataSchema) dereferencedDataSchema;
          GenericData.Record avroRecord = new GenericData.Record(avroSchema);
          for (RecordDataSchema.Field field : recordDataSchema.getFields()) {
            String fieldName = field.getName();
            DataSchema fieldDataSchema = field.getType();
            Schema.Field avroField = avroSchema.getField(fieldName);
            if (avroField == null) {
              // field present in input but there is no field for it in Avro schema.
              // TODO: Whether and how to indicate this condition to clients.
              continue;
            }
            _path.addLast(fieldName);
            Schema fieldAvroSchema = avroField.schema();
            Object fieldValue = map.get(fieldName);
            boolean isOptional = field.getOptional();
            if (isOptional) {
              if (fieldDataSchema.getDereferencedType() != DataSchema.Type.UNION) {
                if (fieldValue == null) {
                  fieldValue = Data.NULL;
                  fieldDataSchema = DataSchemaConstants.NULL_DATA_SCHEMA;
                }
                Map.Entry<String, Schema> fieldAvroEntry =
                    findUnionMember(fieldDataSchema, fieldAvroSchema);
                if (fieldAvroEntry == null) {
                  _path.removeLast();
                  continue;
                }
                fieldAvroSchema = fieldAvroEntry.getValue();
              } else {
                // already a union
                if (fieldValue == null) {
                  // field is not present
                  fieldValue = Data.NULL;
                  fieldDataSchema = DataSchemaConstants.NULL_DATA_SCHEMA;
                }
              }
            } else {
              if (fieldValue == null) {
                appendMessage("required field is absent");
                _path.removeLast();
                continue;
              }
            }
            Object fieldAvroValue = translate(fieldValue, fieldDataSchema, fieldAvroSchema);
            avroRecord.put(fieldName, fieldAvroValue);
            _path.removeLast();
          }
          result = avroRecord;
          break;
        case UNION:
          UnionDataSchema unionDataSchema = (UnionDataSchema) dereferencedDataSchema;
          String key;
          Object memberValue;
          if (value == Data.NULL) {
            key = DataSchemaConstants.NULL_TYPE;
            memberValue = Data.NULL;
          } else {
            map = (DataMap) value;
            Map.Entry<String, Object> entry = map.entrySet().iterator().next();
            key = entry.getKey();
            memberValue = entry.getValue();
          }
          DataSchema memberDataSchema = unionDataSchema.getType(key);
          Map.Entry<String, Schema> memberAvroEntry = findUnionMember(memberDataSchema, avroSchema);
          if (memberAvroEntry == null) {
            result = BAD_RESULT;
            break;
          }
          Schema memberAvroSchema = memberAvroEntry.getValue();
          _path.addLast(memberAvroEntry.getKey());
          Object memberAvroValue = translate(memberValue, memberDataSchema, memberAvroSchema);
          _path.removeLast();
          result = memberAvroValue;
          break;
        default:
          appendMessage("schema type unknown %1$s", dereferencedDataSchema.getType());
          result = BAD_RESULT;
          break;
      }
      return result;
    }