Search in sources :

Example 6 with JSONDocument

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

the class EntityQueryServiceImplTest method testExecute_success.

@Test
public void testExecute_success() throws Exception {
    Collection mockEntitiesCollection = mock(Collection.class);
    Entity entity1 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 1").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo1")).build()).build();
    Entity entity2 = Entity.newBuilder().setTenantId("tenant-1").setEntityType(TEST_ENTITY_TYPE).setEntityId(UUID.randomUUID().toString()).setEntityName("Test entity 2").putAttributes(EDS_COLUMN_NAME1, AttributeValue.newBuilder().setValue(org.hypertrace.entity.data.service.v1.Value.newBuilder().setString("foo2")).build()).build();
    List<Document> docs = List.of(new JSONDocument(JsonFormat.printer().print(entity1)), new JSONDocument(JsonFormat.printer().print(entity2)), // this doc will result in parsing error
    new JSONDocument("{\"entityId\": [1, 2]}"));
    when(mockEntitiesCollection.aggregate(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    when(mockEntitiesCollection.search(any())).thenReturn(convertToCloseableIterator(docs.iterator()));
    EntityQueryRequest request = EntityQueryRequest.newBuilder().setEntityType(TEST_ENTITY_TYPE).addOrderBy(OrderByExpression.newBuilder().setExpression(Expression.newBuilder().setColumnIdentifier(ColumnIdentifier.newBuilder().setColumnName(ATTRIBUTE_ID1).build()))).build();
    StreamObserver<ResultSetChunk> mockResponseObserver = mock(StreamObserver.class);
    Context.current().withValue(RequestContext.CURRENT, mockRequestContextWithTenantId()).call(() -> {
        EntityQueryServiceImpl eqs = new EntityQueryServiceImpl(mockEntitiesCollection, mockMappingForAttributes1And2(), 1, false);
        eqs.execute(request, mockResponseObserver);
        return null;
    });
    verify(mockEntitiesCollection, times(0)).aggregate(any());
    verify(mockEntitiesCollection, times(1)).search(any());
    verify(mockResponseObserver, times(3)).onNext(any());
    verify(mockResponseObserver, times(1)).onCompleted();
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) Collection(org.hypertrace.core.documentstore.Collection) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Document(org.hypertrace.core.documentstore.Document) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) EntityQueryRequest(org.hypertrace.entity.query.service.v1.EntityQueryRequest) ResultSetChunk(org.hypertrace.entity.query.service.v1.ResultSetChunk) Test(org.junit.jupiter.api.Test)

Example 7 with JSONDocument

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

the class EntityTypeServiceImpl method upsertEntityRelationshipType.

@Override
public void upsertEntityRelationshipType(EntityRelationshipType request, StreamObserver<EntityRelationshipType> responseObserver) {
    Optional<String> tenantId = RequestContext.CURRENT.get().getTenantId();
    if (tenantId.isEmpty()) {
        responseObserver.onError(new ServiceException("Tenant id is missing in the request."));
        return;
    }
    try {
        // TODO: Since we currently use the same object that's received in the request while
        // persisting in the doc store, we need to add tenantId explicitly here.
        // We need to split these objects later.
        EntityRelationshipType newRequest = EntityRelationshipType.newBuilder(request).setTenantId(tenantId.get()).build();
        entityTypeRelationsCol.upsert(new SingleValueKey(tenantId.get(), request.getName()), new JSONDocument(PROTO_PRINTER.print(newRequest)));
        responseObserver.onNext(newRequest);
        responseObserver.onCompleted();
    } catch (IOException ioe) {
        responseObserver.onError(new RuntimeException("Error upserting EntityType:" + request, ioe));
    }
}
Also used : SingleValueKey(org.hypertrace.core.documentstore.SingleValueKey) ServiceException(com.google.protobuf.ServiceException) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) IOException(java.io.IOException) EntityRelationshipType(org.hypertrace.entity.type.service.v1.EntityRelationshipType)

Example 8 with JSONDocument

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

the class DocStoreConverterTest method entityToJSONDocumentConversion64BitNumberFixup.

@Test
public void entityToJSONDocumentConversion64BitNumberFixup() throws IOException {
    // Verify that timestamp attribute which is a long number won't get
    // converted to string
    Entity testEntity = Entity.newBuilder().setTenantId("tenant1").setEntityName("myentity1").putIdentifyingAttributes("entity_id", AttributeValue.newBuilder().setValue(Value.newBuilder().setString("my-entity-id-1")).build()).putAttributes("timestamp", AttributeValue.newBuilder().setValue(Value.newBuilder().setLong(1584055141072L)).build()).build();
    JSONDocument jsonDocument = DocStoreConverter.transform(testEntity);
    Assertions.assertEquals("{\"tenantId\":\"tenant1\",\"entityName\":\"myentity1\",\"identifyingAttributes\":{\"entity_id\":{\"value\":{\"string\":\"my-entity-id-1\"}}},\"attributes\":{\"timestamp\":{\"value\":{\"long\":1584055141072}}}}", jsonDocument.toJson());
}
Also used : Entity(org.hypertrace.entity.data.service.v1.Entity) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) Test(org.junit.jupiter.api.Test)

Example 9 with JSONDocument

use of org.hypertrace.core.documentstore.JSONDocument 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 10 with JSONDocument

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

the class EntityQueryServiceImpl method convertToJsonDocument.

@SneakyThrows
private JSONDocument convertToJsonDocument(LiteralConstant literalConstant) {
    // Convert setAttribute LiteralConstant to AttributeValue. Need to be able to store an array
    // literal constant as an array
    AttributeValue attributeValue = EntityQueryConverter.convertToAttributeValue(literalConstant).build();
    String jsonValue = PRINTER.print(attributeValue);
    return new JSONDocument(jsonValue);
}
Also used : AttributeValue(org.hypertrace.entity.data.service.v1.AttributeValue) JSONDocument(org.hypertrace.core.documentstore.JSONDocument) SneakyThrows(lombok.SneakyThrows)

Aggregations

JSONDocument (org.hypertrace.core.documentstore.JSONDocument)17 Document (org.hypertrace.core.documentstore.Document)10 Test (org.junit.jupiter.api.Test)10 SingleValueKey (org.hypertrace.core.documentstore.SingleValueKey)9 Collection (org.hypertrace.core.documentstore.Collection)8 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 MongoCollection (com.mongodb.client.MongoCollection)4 Query (org.hypertrace.core.documentstore.Query)4 ResultSetChunk (org.hypertrace.entity.query.service.v1.ResultSetChunk)4 ServiceException (com.google.protobuf.ServiceException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 Entity (org.hypertrace.entity.data.service.v1.Entity)3 EntityQueryRequest (org.hypertrace.entity.query.service.v1.EntityQueryRequest)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 BulkUpdateRequest (org.hypertrace.core.documentstore.BulkUpdateRequest)2 BulkUpdateResult (org.hypertrace.core.documentstore.BulkUpdateResult)2 Filter (org.hypertrace.core.documentstore.Filter)2