Java Code Examples for org.bson.BsonArray#getValues()

The following examples show how to use org.bson.BsonArray#getValues() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 6 votes vote down vote up
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
 
Example 2
Source File: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 6 votes vote down vote up
private List<BsonDocument> processFiles(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            BsonDocument document = rawDocument.asDocument();
            if (document.get("length").isInt32()) {
                document.put("length", new BsonInt64(document.getInt32("length").getValue()));
            }
            if (document.containsKey("metadata") && document.getDocument("metadata").isEmpty()) {
                document.remove("metadata");
            }
            if (document.containsKey("aliases") && document.getArray("aliases").getValues().size() == 0) {
                document.remove("aliases");
            }
            if (document.containsKey("contentType") && document.getString("contentType").getValue().length() == 0) {
                document.remove("contentType");
            }
            documents.add(document);
        }
    }
    return documents;
}
 
Example 3
Source File: GridFSTest.java    From mongo-java-driver-rx with Apache License 2.0 5 votes vote down vote up
private List<BsonDocument> processChunks(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            documents.add(parseHexDocument(rawDocument.asDocument()));
        }
    }
    return documents;
}
 
Example 4
Source File: SharedFongoResource.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected boolean doInitialize(
    ResourceSpecifier aSpecifier, Map<String, Object> aAdditionalParams)
    throws ResourceInitializationException {
  // Work whether it's a list of DB Objects or a single
  if ("{}".equals(fongoData) || "[]".equals(fongoData) || Strings.isNullOrEmpty(fongoData)) {
    return true;
  }

  if (fongoData.trim().startsWith("[")) {
    CodecRegistry codecRegistry =
        CodecRegistries.fromProviders(
            Arrays.asList(
                new ValueCodecProvider(),
                new BsonValueCodecProvider(),
                new DocumentCodecProvider()));
    JsonReader reader = new JsonReader(fongoData);
    BsonArrayCodec arrayReader = new BsonArrayCodec(codecRegistry);

    BsonArray docArray = arrayReader.decode(reader, DecoderContext.builder().build());

    for (BsonValue doc : docArray.getValues()) {
      fongo
          .getDatabase(BALEEN)
          .getCollection(fongoCollection)
          .insertOne(Document.parse(doc.asDocument().toJson()));
    }
  } else if (fongoData.trim().startsWith("{")) {
    Document data = Document.parse(fongoData);
    fongo.getDatabase(BALEEN).getCollection(fongoCollection).insertOne(data);
  } else {
    getMonitor().error("Unsupported type");
    throw new ResourceInitializationException();
  }

  return true;
}
 
Example 5
Source File: GridFSTest.java    From mongo-java-driver-reactivestreams with Apache License 2.0 5 votes vote down vote up
private List<BsonDocument> processChunks(final BsonArray bsonArray, final List<BsonDocument> documents) {
    for (BsonValue rawDocument : bsonArray.getValues()) {
        if (rawDocument.isDocument()) {
            documents.add(parseHexDocument(rawDocument.asDocument()));
        }
    }
    return documents;
}
 
Example 6
Source File: BlockListProjector.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@Override
protected void doProjection(final String field, final BsonDocument doc) {
  if (!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {
    if (field.equals(FieldProjector.SINGLE_WILDCARD)
        || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
      handleWildcard(field, "", doc);
      return;
    }

    // NOTE: never try to remove the _id field
    if (!field.equals(ID_FIELD)) {
      doc.remove(field);
    }
    return;
  }

  int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
  String firstPart = field.substring(0, dotIdx);
  String otherParts = field.length() >= dotIdx ? field.substring(dotIdx + 1) : "";

  if (firstPart.equals(FieldProjector.SINGLE_WILDCARD)
      || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
    handleWildcard(firstPart, otherParts, doc);
    return;
  }

  BsonValue value = doc.get(firstPart);
  if (value != null) {
    if (value.isDocument()) {
      doProjection(otherParts, value.asDocument());
    }
    if (value.isArray()) {
      BsonArray values = value.asArray();
      for (BsonValue v : values.getValues()) {
        if (v != null && v.isDocument()) {
          doProjection(otherParts, v.asDocument());
        }
      }
    }
  }
}
 
Example 7
Source File: AllowListProjector.java    From mongo-kafka with Apache License 2.0 4 votes vote down vote up
@Override
protected void doProjection(final String field, final BsonDocument doc) {
  // special case short circuit check for '**' pattern
  // this is essentially the same as not using
  // whitelisting at all but instead take the full record
  if (getFields().contains(FieldProjector.DOUBLE_WILDCARD)) {
    return;
  }

  Iterator<Map.Entry<String, BsonValue>> iter = doc.entrySet().iterator();
  while (iter.hasNext()) {
    Map.Entry<String, BsonValue> entry = iter.next();
    String key =
        field.isEmpty()
            ? entry.getKey()
            : field + FieldProjector.SUB_FIELD_DOT_SEPARATOR + entry.getKey();
    BsonValue value = entry.getValue();

    // NOTE: always keep the _id field
    if ((!getFields().contains(key) && !key.equals(ID_FIELD)) && !checkForWildcardMatch(key)) {
      iter.remove();
    }

    if (value != null) {
      if (value.isDocument()) {
        // short circuit check to avoid recursion
        // if 'key.**' pattern exists
        String matchDoubleWildCard =
            key + FieldProjector.SUB_FIELD_DOT_SEPARATOR + FieldProjector.DOUBLE_WILDCARD;
        if (!getFields().contains(matchDoubleWildCard)) {
          doProjection(key, (BsonDocument) value);
        }
      }
      if (value.isArray()) {
        BsonArray values = (BsonArray) value;
        for (BsonValue v : values.getValues()) {
          if (v != null && v.isDocument()) {
            doProjection(key, (BsonDocument) v);
          }
        }
      }
    }
  }
}
 
Example 8
Source File: BlacklistProjector.java    From kafka-connect-mongodb with Apache License 2.0 4 votes vote down vote up
@Override
protected void doProjection(String field, BsonDocument doc) {

    if(!field.contains(FieldProjector.SUB_FIELD_DOT_SEPARATOR)) {

        if(field.equals(FieldProjector.SINGLE_WILDCARD)
                || field.equals(FieldProjector.DOUBLE_WILDCARD)) {
            handleWildcard(field,"",doc);
            return;
        }

        //NOTE: never try to remove the _id field
        if(!field.equals(DBCollection.ID_FIELD_NAME))
            doc.remove(field);

        return;
    }

    int dotIdx = field.indexOf(FieldProjector.SUB_FIELD_DOT_SEPARATOR);
    String firstPart = field.substring(0,dotIdx);
    String otherParts = field.length() >= dotIdx
                            ? field.substring(dotIdx+1) : "";

    if(firstPart.equals(FieldProjector.SINGLE_WILDCARD)
        || firstPart.equals(FieldProjector.DOUBLE_WILDCARD)) {
        handleWildcard(firstPart,otherParts,doc);
        return;
    }

    BsonValue value = doc.get(firstPart);
    if(value != null) {
        if(value.isDocument()) {
            doProjection(otherParts, (BsonDocument)value);
        }
        if(value.isArray()) {
            BsonArray values = (BsonArray)value;
            for(BsonValue v : values.getValues()) {
                if(v != null && v.isDocument()) {
                    doProjection(otherParts,(BsonDocument)v);
                }
            }
        }
    }

}
 
Example 9
Source File: WhitelistProjector.java    From kafka-connect-mongodb with Apache License 2.0 4 votes vote down vote up
@Override
protected void doProjection(String field, BsonDocument doc) {

    //special case short circuit check for '**' pattern
    //this is essentially the same as not using
    //whitelisting at all but instead take the full record
    if(fields.contains(FieldProjector.DOUBLE_WILDCARD)) {
        return;
    }

    Iterator<Map.Entry<String, BsonValue>> iter = doc.entrySet().iterator();
    while(iter.hasNext()) {
        Map.Entry<String, BsonValue> entry = iter.next();

        String key = field.isEmpty() ? entry.getKey()
                : field + FieldProjector.SUB_FIELD_DOT_SEPARATOR + entry.getKey();
        BsonValue value = entry.getValue();

        if(!fields.contains(key)
                //NOTE: always keep the _id field
                && !key.equals(DBCollection.ID_FIELD_NAME)) {

            if(!checkForWildcardMatch(key))
                iter.remove();

        }

        if(value != null) {
            if(value.isDocument()) {
                //short circuit check to avoid recursion
                //if 'key.**' pattern exists
                String matchDoubleWildCard = key
                        + FieldProjector.SUB_FIELD_DOT_SEPARATOR
                        + FieldProjector.DOUBLE_WILDCARD;
                if(!fields.contains(matchDoubleWildCard)) {
                    doProjection(key, (BsonDocument)value);
                }
            }
            if(value.isArray()) {
                BsonArray values = (BsonArray)value;
                for(BsonValue v : values.getValues()) {
                    if(v != null && v.isDocument()) {
                        doProjection(key,(BsonDocument)v);
                    }
                }
            }
        }

    }
}