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 all 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,7 @@ public enum JsonType {
STRING,
NULL,
SUB_DOC,
ARRAY
ARRAY,
// DOCUMENT_ID represent the _id field type which is union of String, Number, Boolean and Null
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
@@ -1,9 +1,39 @@
package io.stargate.sgv3.docsapi.config.constants;

import io.stargate.sgv3.docsapi.api.model.command.clause.filter.JsonType;
import java.util.Map;

public interface DocumentConstants {
/** Names of "special" fields in Documents */
interface Fields {
/** Primary key for Documents stored; has special handling for many operations. */
String DOC_ID = "_id";
}

interface KeyTypeId {
/**
* Type id are used in key stored in database representing the datatype of the id field. These
* values should not be changed once data is stored in the DB.
*/
int TYPE_ID_STRING = 1;

int TYPE_ID_NUMBER = 2;
int TYPE_ID_BOOLEAN = 3;
int TYPE_ID_NULL = 4;

Map<Integer, JsonType> keyIDMap =
Map.of(
TYPE_ID_STRING,
JsonType.STRING,
TYPE_ID_NUMBER,
JsonType.NUMBER,
TYPE_ID_BOOLEAN,
JsonType.BOOLEAN,
TYPE_ID_NULL,
JsonType.NULL);

static JsonType getJsonType(int typeId) {
return keyIDMap.get(typeId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class DocsException extends RuntimeException implements Supplier<CommandR
private final ErrorCode errorCode;

public DocsException(ErrorCode errorCode) {
this(errorCode, null, null);
this(errorCode, errorCode.getMessage(), null);
}

public DocsException(ErrorCode errorCode, String message) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public enum ErrorCode {

SHRED_BAD_DOCID_TYPE("Bad type for '_id' property"),

SHRED_BAD_DOCID_EMPTY_STRING("Bad value for '_id' property: empty String not allowed"),

SHRED_INTERNAL_NO_PATH("Internal: path being built does not point to a property or element"),

SHRED_NO_MD5("MD5 Hash algorithm not available"),
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,9 +7,11 @@
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;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.util.List;
import java.util.function.Supplier;

Expand All @@ -23,7 +25,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<DocumentId>> ids =
docsToDelete
.onItem()
.transformToMulti(
Expand Down Expand Up @@ -62,7 +64,7 @@ private QueryOuterClass.Query buildDeleteQuery() {
* @param doc
* @return
*/
private static Uni<String> deleteDocument(
private static Uni<DocumentId> deleteDocument(
QueryExecutor queryExecutor, QueryOuterClass.Query query, ReadDocument doc) {
query = bindDeleteQuery(query, doc);
return queryExecutor
Expand All @@ -82,7 +84,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 @@ -2,6 +2,7 @@

import io.stargate.sgv3.docsapi.api.model.command.CommandResult;
import io.stargate.sgv3.docsapi.api.model.command.CommandStatus;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
Expand All @@ -11,7 +12,7 @@
*
* @param deletedIds - document ids deleted
*/
public record DeleteOperationPage(List<String> deletedIds) implements Supplier<CommandResult> {
public record DeleteOperationPage(List<DocumentId> 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 @@ -9,6 +9,7 @@
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.shredding.model.DocumentId;
import io.stargate.sgv3.docsapi.service.shredding.model.WritableShreddedDocument;
import java.util.List;
import java.util.function.Supplier;
Expand All @@ -26,20 +27,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<DocumentId>> 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<DocumentId> 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());
}

private QueryOuterClass.Query buildInsertQuery() {
Expand All @@ -58,7 +59,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 @@ -2,6 +2,7 @@

import io.stargate.sgv3.docsapi.api.model.command.CommandResult;
import io.stargate.sgv3.docsapi.api.model.command.CommandStatus;
import io.stargate.sgv3.docsapi.service.shredding.model.DocumentId;
import io.stargate.sgv3.docsapi.service.shredding.model.WritableShreddedDocument;
import java.util.List;
import java.util.Map;
Expand All @@ -11,8 +12,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<DocumentId> 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) {}
Loading