Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

_id field to support String, Number, Boolean and Null Json type #66

Merged
merged 13 commits into from
Feb 2, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.stargate.sgv3.docsapi.exception.DocsException;
import io.stargate.sgv3.docsapi.exception.ErrorCode;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.math.BigDecimal;
import java.util.EnumSet;
import java.util.List;
Expand Down Expand Up @@ -58,6 +59,9 @@ private static JsonLiteral<?> getLiteral(Object value) {
if (value == null) {
return new JsonLiteral<>(null, JsonType.NULL);
}
if (value instanceof DocumentId) {
return new JsonLiteral<>((DocumentId) value, JsonType.DOCUMENT_ID);
}
if (value instanceof BigDecimal) {
return new JsonLiteral<>((BigDecimal) value, JsonType.NUMBER);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public enum JsonType {
STRING,
NULL,
SUB_DOC,
ARRAY
ARRAY,
DOCUMENT_ID
maheshrajamani marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.ComparisonExpression;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.FilterClause;
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.ValueComparisonOperator;
import io.stargate.sgv3.docsapi.config.constants.DocumentConstants;
import io.stargate.sgv3.docsapi.exception.DocsException;
import io.stargate.sgv3.docsapi.exception.ErrorCode;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
Expand Down Expand Up @@ -44,7 +46,8 @@ public FilterClause deserialize(
} else {
// @TODO: Need to add array value type to this condition
expressionList.add(
ComparisonExpression.eq(entry.getKey(), jsonNodeValue(entry.getValue())));
ComparisonExpression.eq(
entry.getKey(), jsonNodeValue(entry.getKey(), entry.getValue())));
}
}
return new FilterClause(expressionList);
Expand All @@ -70,12 +73,15 @@ private ComparisonExpression createComparisonExpression(Map.Entry<String, JsonNo
ValueComparisonOperator.getComparisonOperator(updateField.getKey());
JsonNode value = updateField.getValue();
// @TODO: Need to add array and sub-document value type to this condition
expression.add(operator, jsonNodeValue(value));
expression.add(operator, jsonNodeValue(entry.getKey(), value));
}
return expression;
}

private static Object jsonNodeValue(JsonNode node) {
private static Object jsonNodeValue(String path, JsonNode node) {
if (path.equals(DocumentConstants.Fields.DOC_ID)) {
return DocumentId.fromJson(node);
}
switch (node.getNodeType()) {
case BOOLEAN:
return node.booleanValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ public static Map<QueryOuterClass.Value, QueryOuterClass.Value> getDoubleMapValu
return to;
}

public static QueryOuterClass.Value getDocumentIdValue(DocumentId documentId) {
public static List<QueryOuterClass.Value> getDocumentIdValue(DocumentId documentId) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this because Tuples are expressed as Lists in protobuf/bridge/grpc?

// Temporary implementation until we convert it to Tuple in DB
return Values.of(documentId.toString());
List<QueryOuterClass.Value> tupleValues =
List.of(Values.of(documentId.typeId()), Values.of(documentId.toString()));
return tupleValues;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.stargate.sgv3.docsapi.exception.ErrorCode;
import io.stargate.sgv3.docsapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv3.docsapi.service.operation.model.impl.ReadDocument;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -56,7 +57,7 @@ default Uni<FindResponse> findDocument(
try {
document =
new ReadDocument(
Values.string(row.getValues(0)), // key
getDocumentId(row.getValues(0)), // key
Values.uuid(row.getValues(1)), // tx_id
readDocument
? objectMapper.readTree(Values.string(row.getValues(2)))
Expand All @@ -70,6 +71,19 @@ default Uni<FindResponse> findDocument(
});
}

/**
* Database key type is tuple<int, text>, first field is json value type and second field is text
*
* @param value
* @return
*/
default DocumentId getDocumentId(QueryOuterClass.Value value) {
QueryOuterClass.Collection coll = value.getCollection();
int typeId = Values.tinyint(coll.getElements(0));
String documentIdAsText = Values.string(coll.getElements(1));
return DocumentId.fromDatabase(typeId, documentIdAsText);
}

private String extractPagingStateFromResultSet(QueryOuterClass.ResultSet rSet) {
if (rSet.hasPagingState()) {
return BytesValues.toBase64(rSet.getPagingState());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
protected QueryOuterClass.Query getCreateTable(String keyspace, String table) {
String createTable =
"CREATE TABLE IF NOT EXISTS %s.%s ("
+ " key text,"
+ " key tuple<tinyint,text>,"
+ " tx_id timeuuid, "
+ " doc_json text,"
+ " doc_properties map<text, int>,"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import io.stargate.sgv3.docsapi.api.model.command.CommandContext;
import io.stargate.sgv3.docsapi.api.model.command.CommandResult;
import io.stargate.sgv3.docsapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv3.docsapi.service.bridge.serializer.CustomValueSerializers;
import io.stargate.sgv3.docsapi.service.operation.model.ModifyOperation;
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation;
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation.FindResponse;
Expand All @@ -23,7 +24,7 @@ public record DeleteOperation(CommandContext commandContext, ReadOperation readO
public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
Uni<FindResponse> docsToDelete = readOperation().getDocuments(queryExecutor);
final QueryOuterClass.Query delete = buildDeleteQuery();
final Uni<List<String>> ids =
final Uni<List<Object>> ids =
maheshrajamani marked this conversation as resolved.
Show resolved Hide resolved
docsToDelete
.onItem()
.transformToMulti(
Expand Down Expand Up @@ -62,7 +63,7 @@ private QueryOuterClass.Query buildDeleteQuery() {
* @param doc
* @return
*/
private static Uni<String> deleteDocument(
private static Uni<Object> deleteDocument(
QueryExecutor queryExecutor, QueryOuterClass.Query query, ReadDocument doc) {
query = bindDeleteQuery(query, doc);
return queryExecutor
Expand All @@ -71,7 +72,7 @@ private static Uni<String> deleteDocument(
.transformToUni(
result -> {
if (result.getRows(0).getValues(0).getBoolean()) {
return Uni.createFrom().item(doc.id());
return Uni.createFrom().item(doc.id().value());
} else {
return Uni.createFrom().nothing();
}
Expand All @@ -82,7 +83,7 @@ private static QueryOuterClass.Query bindDeleteQuery(
QueryOuterClass.Query builtQuery, ReadDocument doc) {
QueryOuterClass.Values.Builder values =
QueryOuterClass.Values.newBuilder()
.addValues(Values.of(doc.id()))
.addValues(Values.of(CustomValueSerializers.getDocumentIdValue(doc.id())))
.addValues(Values.of(doc.txnId()));
return QueryOuterClass.Query.newBuilder(builtQuery).setValues(values).build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*
* @param deletedIds - document ids deleted
*/
public record DeleteOperationPage(List<String> deletedIds) implements Supplier<CommandResult> {
public record DeleteOperationPage(List<Object> deletedIds) implements Supplier<CommandResult> {
@Override
public CommandResult get() {
return new CommandResult(Map.of(CommandStatus.DELETED_IDS, deletedIds));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import io.stargate.sgv3.docsapi.exception.DocsException;
import io.stargate.sgv3.docsapi.exception.ErrorCode;
import io.stargate.sgv3.docsapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv3.docsapi.service.bridge.serializer.CustomValueSerializers;
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -145,9 +147,9 @@ public enum Operator {
}

protected final Operator operator;
protected final String value;
protected final DocumentId value;

public IDFilter(Operator operator, String value) {
public IDFilter(Operator operator, DocumentId value) {
this.operator = operator;
this.value = value;
}
Expand All @@ -169,7 +171,8 @@ public int hashCode() {
public BuiltCondition get() {
switch (operator) {
case EQ:
return BuiltCondition.of(BuiltCondition.LHS.column("key"), Predicate.EQ, getValue(value));
return BuiltCondition.of(
BuiltCondition.LHS.column("key"), Predicate.EQ, getDocumentIdValue(value));
default:
throw new DocsException(
ErrorCode.UNSUPPORTED_FILTER_OPERATION,
Expand Down Expand Up @@ -245,4 +248,8 @@ private static QueryOuterClass.Value getValue(Object value) {
}
return Values.of((String) null);
}

private static QueryOuterClass.Value getDocumentIdValue(DocumentId value) {
return Values.of(CustomValueSerializers.getDocumentIdValue(value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,20 @@ public InsertOperation(CommandContext commandContext, WritableShreddedDocument d
@Override
public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
QueryOuterClass.Query query = buildInsertQuery();
final Uni<List<String>> ids =
final Uni<List<Object>> ids =
Multi.createFrom()
.items(documents.stream())
.onItem()
.transformToUniAndConcatenate(doc -> insertDocument(queryExecutor, query, doc))
.collect()
.asList();
return ids.onItem().transform(insertedIds -> new ModifyOperationPage(insertedIds, documents));
return ids.onItem().transform(insertedIds -> new InsertOperationPage(insertedIds, documents));
}

private static Uni<String> insertDocument(
private static Uni<Object> insertDocument(
QueryExecutor queryExecutor, QueryOuterClass.Query query, WritableShreddedDocument doc) {
query = bindInsertValues(query, doc);
return queryExecutor.executeWrite(query).onItem().transform(result -> doc.id().toString());
return queryExecutor.executeWrite(query).onItem().transform(result -> doc.id().value());
}

private QueryOuterClass.Query buildInsertQuery() {
Expand All @@ -58,7 +58,7 @@ private static QueryOuterClass.Query bindInsertValues(
// respect the order in the DocsApiConstants.ALL_COLUMNS_NAMES
QueryOuterClass.Values.Builder values =
QueryOuterClass.Values.newBuilder()
.addValues(CustomValueSerializers.getDocumentIdValue(doc.id()))
.addValues(Values.of(CustomValueSerializers.getDocumentIdValue(doc.id())))
.addValues(Values.of(doc.docJson()))
.addValues(Values.of(CustomValueSerializers.getIntegerMapValues(doc.docProperties())))
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.existKeys())))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
* The internal to modification operation results, what were the ID's of the docs we changed and
* what change.
*/
public record ModifyOperationPage(
List<String> insertedIds, List<WritableShreddedDocument> insertedDocs)
public record InsertOperationPage(
List<Object> insertedIds, List<WritableShreddedDocument> insertedDocs)
implements Supplier<CommandResult> {
@Override
public CommandResult get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.stargate.sgv3.docsapi.service.operation.model.ModifyOperation;
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation;
import io.stargate.sgv3.docsapi.service.shredding.Shredder;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import io.stargate.sgv3.docsapi.service.shredding.model.WritableShreddedDocument;
import io.stargate.sgv3.docsapi.service.updater.DocumentUpdater;
import java.util.List;
Expand Down Expand Up @@ -52,7 +53,7 @@ public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
.transform(updates -> new UpdateOperationPage(updates, returnDoc()));
}

private Uni<String> updatedDocument(
private Uni<DocumentId> updatedDocument(
QueryExecutor queryExecutor, WritableShreddedDocument writableShreddedDocument) {
final QueryOuterClass.Query updateQuery =
bindUpdateValues(buildUpdateQuery(), writableShreddedDocument);
Expand All @@ -62,7 +63,7 @@ private Uni<String> updatedDocument(
.transformToUni(
result -> {
if (result.getRows(0).getValues(0).getBoolean()) {
return Uni.createFrom().item(writableShreddedDocument.id().toString());
return Uni.createFrom().item(writableShreddedDocument.id());
} else {
return Uni.createFrom().nothing();
}
Expand Down Expand Up @@ -111,10 +112,10 @@ protected static QueryOuterClass.Query bindUpdateValues(
.addValues(Values.of(CustomValueSerializers.getStringMapValues(doc.queryTextValues())))
.addValues(Values.of(CustomValueSerializers.getSetValue(doc.queryNullValues())))
.addValues(Values.of(doc.docJson()))
.addValues(CustomValueSerializers.getDocumentIdValue(doc.id()))
.addValues(Values.of(CustomValueSerializers.getDocumentIdValue(doc.id())))
.addValues(Values.of(doc.txID()));
return QueryOuterClass.Query.newBuilder(builtQuery).setValues(values).build();
}

record UpdatedDocument(String id, JsonNode document) {}
record UpdatedDocument(DocumentId id, JsonNode document) {}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.stargate.sgv3.docsapi.service.operation.model.impl;

import com.fasterxml.jackson.databind.JsonNode;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.util.UUID;

/**
Expand All @@ -10,4 +11,4 @@
* @param txnId Unique UUID resenting point in time of a document, used for LWT transactions
* @param document JsonNode representation of the document
*/
public record ReadDocument(String id, UUID txnId, JsonNode document) {}
public record ReadDocument(DocumentId id, UUID txnId, JsonNode document) {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public record UpdateOperationPage(
implements Supplier<CommandResult> {
@Override
public CommandResult get() {
List<String> updatedIds = new ArrayList<>(updatedDocuments().size());
List<Object> updatedIds = new ArrayList<>(updatedDocuments().size());
List<JsonNode> updatedDocs = new ArrayList<>(updatedDocuments().size());
updatedDocuments.forEach(
update -> {
updatedIds.add(update.id());
updatedIds.add(update.id().value());
updatedDocs.add(update.document());
});
if (returnDocs) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.stargate.sgv3.docsapi.api.model.command.clause.filter.ValueComparisonOperator;
import io.stargate.sgv3.docsapi.service.operation.model.ReadOperation;
import io.stargate.sgv3.docsapi.service.operation.model.impl.FindOperation;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -54,7 +55,7 @@ public FilterableResolver(ObjectMapper objectMapper, boolean findOne, boolean re
.addMatchRule(this::findById, FilterMatcher.MatchStrategy.STRICT)
.matcher()
.capture(ID_GROUP)
.compareValues("_id", ValueComparisonOperator.EQ, JsonType.STRING);
.compareValues("_id", ValueComparisonOperator.EQ, JsonType.DOCUMENT_ID);

// NOTE - can only do eq ops on fields until SAI changes
matchRules
Expand Down Expand Up @@ -83,8 +84,8 @@ public record FilteringOptions(int limit, String pagingState, int pageSize) {}
private ReadOperation findById(CommandContext commandContext, CaptureGroups<T> captures) {
List<FindOperation.DBFilterBase> filters = new ArrayList<>();

final CaptureGroup<String> idGroup =
(CaptureGroup<String>) captures.getGroupIfPresent(ID_GROUP);
final CaptureGroup<DocumentId> idGroup =
(CaptureGroup<DocumentId>) captures.getGroupIfPresent(ID_GROUP);
if (idGroup != null) {
idGroup.consumeAllCaptures(
expression ->
Expand Down Expand Up @@ -118,8 +119,8 @@ private ReadOperation findNoFilter(CommandContext commandContext, CaptureGroups<
private ReadOperation findDynamic(CommandContext commandContext, CaptureGroups<T> captures) {
List<FindOperation.DBFilterBase> filters = new ArrayList<>();

final CaptureGroup<String> idGroup =
(CaptureGroup<String>) captures.getGroupIfPresent(ID_GROUP);
final CaptureGroup<DocumentId> idGroup =
(CaptureGroup<DocumentId>) captures.getGroupIfPresent(ID_GROUP);
if (idGroup != null) {
idGroup.consumeAllCaptures(
expression ->
Expand Down
Loading