Search in sources :

Example 26 with Document

use of org.hypertrace.core.documentstore.Document in project entity-service by hypertrace.

the class EntityQueryServiceImpl method doUpdate.

private void doUpdate(RequestContext requestContext, EntityUpdateRequest request) throws Exception {
    if (request.getOperation().hasSetAttribute()) {
        SetAttribute setAttribute = request.getOperation().getSetAttribute();
        String attributeId = setAttribute.getAttribute().getColumnName();
        String subDocPath = entityAttributeMapping.getDocStorePathByAttributeId(requestContext, attributeId).orElseThrow(() -> new IllegalArgumentException("Unknown attribute FQN " + attributeId));
        JSONDocument jsonDocument = convertToJsonDocument(setAttribute.getValue());
        Map<Key, Map<String, Document>> entitiesUpdateMap = new HashMap<>();
        for (String entityId : request.getEntityIdsList()) {
            SingleValueKey key = new SingleValueKey(requestContext.getTenantId().orElseThrow(), entityId);
            if (entitiesUpdateMap.containsKey(key)) {
                entitiesUpdateMap.get(key).put(subDocPath, jsonDocument);
            } else {
                Map<String, Document> subDocument = new HashMap<>();
                subDocument.put(subDocPath, jsonDocument);
                entitiesUpdateMap.put(key, subDocument);
            }
        }
        try {
            entitiesCollection.bulkUpdateSubDocs(entitiesUpdateMap);
        } catch (Exception e) {
            LOG.error("Failed to update entities {}, subDocPath {}, with new doc {}.", entitiesUpdateMap, subDocPath, jsonDocument, e);
            throw e;
        }
    }
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) HashMap(java.util.HashMap) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Map(java.util.Map) HashMap(java.util.HashMap) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) Key(org.hypertrace.core.documentstore.Key) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException)

Example 27 with Document

use of org.hypertrace.core.documentstore.Document in project entity-service by hypertrace.

the class EntityQueryServiceImpl method transformUpdateOperations.

private Map<String, Document> transformUpdateOperations(List<UpdateOperation> updateOperationList, RequestContext requestContext) throws Exception {
    Map<String, Document> documentMap = new HashMap<>();
    for (UpdateOperation updateOperation : updateOperationList) {
        if (!updateOperation.hasSetAttribute()) {
            continue;
        }
        SetAttribute setAttribute = updateOperation.getSetAttribute();
        String attributeId = setAttribute.getAttribute().getColumnName();
        String subDocPath = entityAttributeMapping.getDocStorePathByAttributeId(requestContext, attributeId).orElseThrow(() -> new IllegalArgumentException("Unknown attribute FQN " + attributeId));
        try {
            documentMap.put(subDocPath, convertToJsonDocument(setAttribute.getValue()));
        } catch (Exception e) {
            LOG.error("Failed to put update corresponding to {} in the documentMap", subDocPath, e);
            throw e;
        }
    }
    return Collections.unmodifiableMap(documentMap);
}
Also used : HashMap(java.util.HashMap) UpdateOperation(org.hypertrace.entity.query.service.v1.UpdateOperation) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) SetAttribute(org.hypertrace.entity.query.service.v1.SetAttribute) ServiceException(com.google.protobuf.ServiceException) ConversionException(org.hypertrace.entity.query.service.converter.ConversionException)

Example 28 with Document

use of org.hypertrace.core.documentstore.Document in project entity-service by hypertrace.

the class EntityDataServiceImpl method upsertEntity.

private Entity upsertEntity(String tenantId, Entity entity) throws IOException {
    Document document = convertEntityToDocument(entity);
    Document result = entitiesCollection.upsertAndReturn(this.entityNormalizer.getEntityDocKey(tenantId, entity), document);
    return this.entityFromDocument(result).orElseThrow();
}
Also used : Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument)

Example 29 with Document

use of org.hypertrace.core.documentstore.Document in project document-store by hypertrace.

the class MongoCollection method bulkOperationOnArrayValue.

@Override
public BulkUpdateResult bulkOperationOnArrayValue(BulkArrayValueUpdateRequest request) throws Exception {
    List<BasicDBObject> basicDBObjects = new ArrayList<>();
    try {
        for (Document subDocument : request.getSubDocuments()) {
            basicDBObjects.add(getSanitizedObject(subDocument));
        }
    } catch (Exception e) {
        LOGGER.error("Exception updating document. keys: {} operation {} subDocPath {} subDocuments :{}", request.getKeys(), request.getOperation(), request.getSubDocPath(), request.getSubDocuments());
        throw e;
    }
    BasicDBObject operationObject;
    switch(request.getOperation()) {
        case ADD:
            operationObject = getAddOperationObject(request.getSubDocPath(), basicDBObjects);
            break;
        case REMOVE:
            operationObject = getRemoveOperationObject(request.getSubDocPath(), basicDBObjects);
            break;
        case SET:
            operationObject = getSetOperationObject(request.getSubDocPath(), basicDBObjects);
            break;
        default:
            throw new UnsupportedOperationException("Unknown operation : " + request.getOperation());
    }
    List<UpdateManyModel<BasicDBObject>> bulkWriteUpdate = List.of(new UpdateManyModel(selectionCriteriaForKeys(request.getKeys()), operationObject, new UpdateOptions()));
    BulkWriteResult writeResult = collection.bulkWrite(bulkWriteUpdate);
    LOGGER.debug("Write result for bulkOperationOnArrayValue: {}", writeResult);
    return new BulkUpdateResult(writeResult.getModifiedCount());
}
Also used : BasicDBObject(com.mongodb.BasicDBObject) ArrayList(java.util.ArrayList) BulkUpdateResult(org.hypertrace.core.documentstore.BulkUpdateResult) UpdateManyModel(com.mongodb.client.model.UpdateManyModel) ReturnDocument(com.mongodb.client.model.ReturnDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) MongoBulkWriteException(com.mongodb.MongoBulkWriteException) MongoServerException(com.mongodb.MongoServerException) MongoCommandException(com.mongodb.MongoCommandException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions) BulkWriteResult(com.mongodb.bulk.BulkWriteResult)

Example 30 with Document

use of org.hypertrace.core.documentstore.Document in project document-store by hypertrace.

the class MongoCollection method bulkUpsertImpl.

private BulkWriteResult bulkUpsertImpl(Map<Key, Document> documents) throws JsonProcessingException {
    List<UpdateOneModel<BasicDBObject>> bulkCollection = new ArrayList<>();
    for (Entry<Key, Document> entry : documents.entrySet()) {
        Key key = entry.getKey();
        // insert or overwrite
        bulkCollection.add(new UpdateOneModel<>(this.selectionCriteriaForKey(key), prepareUpsert(key, entry.getValue()), new UpdateOptions().upsert(true)));
    }
    return Failsafe.with(bulkWriteRetryPolicy).get(() -> collection.bulkWrite(bulkCollection, new BulkWriteOptions().ordered(false)));
}
Also used : UpdateOneModel(com.mongodb.client.model.UpdateOneModel) BulkWriteOptions(com.mongodb.client.model.BulkWriteOptions) ArrayList(java.util.ArrayList) ReturnDocument(com.mongodb.client.model.ReturnDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Key(org.hypertrace.core.documentstore.Key) UpdateOptions(com.mongodb.client.model.UpdateOptions) FindOneAndUpdateOptions(com.mongodb.client.model.FindOneAndUpdateOptions)

Aggregations

Document (org.hypertrace.core.documentstore.Document)66 JSONDocument (org.hypertrace.core.documentstore.JSONDocument)41 Test (org.junit.jupiter.api.Test)37 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)22 Collection (org.hypertrace.core.documentstore.Collection)21 ArrayList (java.util.ArrayList)20 Key (org.hypertrace.core.documentstore.Key)18 Query (org.hypertrace.core.documentstore.query.Query)18 ServiceException (com.google.protobuf.ServiceException)14 HashMap (java.util.HashMap)14 Query (org.hypertrace.core.documentstore.Query)14 MongoCollection (com.mongodb.client.MongoCollection)12 Map (java.util.Map)12 IOException (java.io.IOException)10 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)10 JsonNode (com.fasterxml.jackson.databind.JsonNode)8 Filter (org.hypertrace.core.documentstore.Filter)8 Entity (org.hypertrace.entity.data.service.v1.Entity)7 BulkArrayValueUpdateRequest (org.hypertrace.core.documentstore.BulkArrayValueUpdateRequest)6 Filter (org.hypertrace.core.documentstore.query.Filter)6