Java Code Examples for org.bson.BsonDocument#keySet()

The following examples show how to use org.bson.BsonDocument#keySet() . 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: RdbmsHandler.java    From mongo-kafka with Apache License 2.0 6 votes vote down vote up
static BsonDocument generateUpsertOrReplaceDoc(
    final BsonDocument keyDoc, final BsonDocument valueDoc, final BsonDocument filterDoc) {

  if (!valueDoc.containsKey(JSON_DOC_AFTER_FIELD)
      || valueDoc.get(JSON_DOC_AFTER_FIELD).isNull()
      || !valueDoc.get(JSON_DOC_AFTER_FIELD).isDocument()
      || valueDoc.getDocument(JSON_DOC_AFTER_FIELD).isEmpty()) {
    throw new DataException(
        "Error: valueDoc must contain non-empty 'after' field"
            + " of type document for insert/update operation");
  }

  BsonDocument upsertDoc = new BsonDocument();
  if (filterDoc.containsKey(ID_FIELD)) {
    upsertDoc.put(ID_FIELD, filterDoc.get(ID_FIELD));
  }

  BsonDocument afterDoc = valueDoc.getDocument(JSON_DOC_AFTER_FIELD);
  for (String f : afterDoc.keySet()) {
    if (!keyDoc.containsKey(f)) {
      upsertDoc.put(f, afterDoc.get(f));
    }
  }
  return upsertDoc;
}
 
Example 2
Source File: RdbmsHandler.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
protected static BsonDocument generateFilterDoc(BsonDocument keyDoc, BsonDocument valueDoc, OperationType opType) {
    if (keyDoc.keySet().isEmpty()) {
        if (opType.equals(OperationType.CREATE) || opType.equals(OperationType.READ)) {
            //create: no PK info in keyDoc -> generate ObjectId
            return new BsonDocument(DBCollection.ID_FIELD_NAME,new BsonObjectId());
        }
        //update or delete: no PK info in keyDoc -> take everything in 'before' field
        try {
            BsonDocument filter = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD);
            if (filter.isEmpty())
                throw new BsonInvalidOperationException("value doc before field is empty");
            return filter;
        } catch(BsonInvalidOperationException exc) {
            throw new DataException("error: value doc 'before' field is empty or has invalid type" +
                    " for update/delete operation which seems severely wrong -> defensive actions taken!",exc);
        }
    }
    //build filter document composed of all PK columns
    BsonDocument pk = new BsonDocument();
    for (String f : keyDoc.keySet()) {
        pk.put(f,keyDoc.get(f));
    }
    return new BsonDocument(DBCollection.ID_FIELD_NAME,pk);
}
 
Example 3
Source File: RdbmsHandler.java    From kafka-connect-mongodb with Apache License 2.0 6 votes vote down vote up
protected static BsonDocument generateUpsertOrReplaceDoc(BsonDocument keyDoc, BsonDocument valueDoc, BsonDocument filterDoc) {

        if (!valueDoc.containsKey(JSON_DOC_AFTER_FIELD)
                || valueDoc.get(JSON_DOC_AFTER_FIELD).isNull()
                || !valueDoc.get(JSON_DOC_AFTER_FIELD).isDocument()
                || valueDoc.getDocument(JSON_DOC_AFTER_FIELD).isEmpty()) {
            throw new DataException("error: valueDoc must contain non-empty 'after' field" +
                    " of type document for insert/update operation");
        }

        BsonDocument upsertDoc = new BsonDocument();
        if(filterDoc.containsKey(DBCollection.ID_FIELD_NAME)) {
            upsertDoc.put(DBCollection.ID_FIELD_NAME,filterDoc.get(DBCollection.ID_FIELD_NAME));
        }

        BsonDocument afterDoc = valueDoc.getDocument(JSON_DOC_AFTER_FIELD);
        for (String f : afterDoc.keySet()) {
            if (!keyDoc.containsKey(f)) {
                upsertDoc.put(f,afterDoc.get(f));
            }
        }
        return upsertDoc;
    }
 
Example 4
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
private BsonDocument convertToExtensionDocument(Map<String, String> namespaces, BsonDocument extension) {
	BsonDocument ext = new BsonDocument();
	for (String key : extension.keySet()) {
		String[] namespaceAndKey = key.split("#");
		if (namespaceAndKey.length != 2)
			continue;
		String namespace = namespaceAndKey[0];
		if (!namespaces.containsKey(namespace))
			continue;
		ext.put("@" + encodeMongoObjectKey(namespace), new BsonString(namespaces.get(namespace)));
		BsonValue extValue = extension.get(key);
		if (extValue instanceof BsonDocument) {
			ext.put(encodeMongoObjectKey(key), convertToExtensionDocument(namespaces, extValue.asDocument()));
		} else {
			ext.put(encodeMongoObjectKey(key), extValue);
		}
	}
	return ext;
}
 
Example 5
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
private BsonDocument convertToExtensionDocument(Map<String, String> namespaces, BsonDocument extension) {
	BsonDocument ext = new BsonDocument();
	for (String key : extension.keySet()) {
		String[] namespaceAndKey = key.split("#");
		if (namespaceAndKey.length != 2)
			continue;
		String namespace = namespaceAndKey[0];
		if (!namespaces.containsKey(namespace))
			continue;
		ext.put("@" + encodeMongoObjectKey(namespace), new BsonString(namespaces.get(namespace)));
		BsonValue extValue = extension.get(key);
		if (extValue instanceof BsonDocument) {
			ext.put(encodeMongoObjectKey(key), convertToExtensionDocument(namespaces, extValue.asDocument()));
		} else {
			ext.put(encodeMongoObjectKey(key), extValue);
		}
	}
	return ext;
}
 
Example 6
Source File: CaptureUtil.java    From epcis with Apache License 2.0 6 votes vote down vote up
private BsonDocument convertToExtensionDocument(Map<String, String> namespaces, BsonDocument extension) {
	BsonDocument ext = new BsonDocument();
	for (String key : extension.keySet()) {
		String[] namespaceAndKey = key.split("#");
		if (namespaceAndKey.length != 2)
			continue;
		String namespace = namespaceAndKey[0];
		if (!namespaces.containsKey(namespace))
			continue;
		ext.put("@" + encodeMongoObjectKey(namespace), new BsonString(namespaces.get(namespace)));
		BsonValue extValue = extension.get(key);
		if (extValue instanceof BsonDocument) {
			ext.put(encodeMongoObjectKey(key), convertToExtensionDocument(namespaces, extValue.asDocument()));
		} else {
			ext.put(encodeMongoObjectKey(key), extValue);
		}
	}
	return ext;
}
 
Example 7
Source File: RdbmsHandler.java    From mongo-kafka with Apache License 2.0 5 votes vote down vote up
static BsonDocument generateFilterDoc(
    final BsonDocument keyDoc, final BsonDocument valueDoc, final OperationType opType) {
  if (keyDoc.keySet().isEmpty()) {
    if (opType.equals(OperationType.CREATE) || opType.equals(OperationType.READ)) {
      // create: no PK info in keyDoc -> generate ObjectId
      return new BsonDocument(ID_FIELD, new BsonObjectId());
    }
    // update or delete: no PK info in keyDoc -> take everything in 'before' field
    try {
      BsonDocument filter = valueDoc.getDocument(JSON_DOC_BEFORE_FIELD);
      if (filter.isEmpty()) {
        throw new BsonInvalidOperationException("value doc before field is empty");
      }
      return filter;
    } catch (BsonInvalidOperationException exc) {
      throw new DataException(
          "Error: value doc 'before' field is empty or has invalid type"
              + " for update/delete operation which seems severely wrong -> defensive actions taken!",
          exc);
    }
  }
  // build filter document composed of all PK columns
  BsonDocument pk = new BsonDocument();
  for (String f : keyDoc.keySet()) {
    pk.put(f, keyDoc.get(f));
  }
  return new BsonDocument(ID_FIELD, pk);
}
 
Example 8
Source File: MongoV4MasterConnector.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
static Map getUpdatedFields(Document fullDocument, BsonDocument updatedFields, boolean bsonConversion) {
  if (bsonConversion) {
    if (fullDocument == null) {
      return (Map) MongoTypeUtil.convertBson(updatedFields);
    }
    HashMap<String, Object> res = new HashMap<>();
    for (String key : updatedFields.keySet()) {
      res.put(key, fullDocument.get(key));
    }
    return res;
  }
  return updatedFields;
}
 
Example 9
Source File: ChronoElement.java    From epcis with Apache License 2.0 5 votes vote down vote up
/**
 * Return all the keys associated with the element.
 * 
 * @return the set of all string keys associated with the element
 */
@Override
public Set<String> getPropertyKeys() {
	BsonDocument doc = getProperties();
	if (doc != null) {
		return doc.keySet();
	}
	return null;
}