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

Refactor FilterableResolver to address #201 #283

Merged
merged 1 commit into from
Mar 22, 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 @@ -6,12 +6,9 @@
import io.stargate.sgv2.api.common.cql.builder.QueryBuilder;
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.CommandResult;
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.CountOperationPage;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.ReadDocument;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Supplier;
Expand Down Expand Up @@ -44,15 +41,4 @@ private QueryOuterClass.Query buildSelectQuery() {
.where(conditions)
.build();
}

@Override
public Uni<FindResponse> getDocuments(
QueryExecutor queryExecutor, String pagingState, DBFilterBase.IDFilter additionalIdFilter) {
return Uni.createFrom().failure(new JsonApiException(ErrorCode.UNSUPPORTED_OPERATION));
}

@Override
public ReadDocument getNewDocument() {
throw new JsonApiException(ErrorCode.UNSUPPORTED_OPERATION);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.stargate.sgv2.jsonapi.exception.ErrorCode;
import io.stargate.sgv2.jsonapi.exception.JsonApiException;
import io.stargate.sgv2.jsonapi.service.bridge.executor.QueryExecutor;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.ReadDocument;
import io.stargate.sgv2.jsonapi.service.shredding.model.DocumentId;
import java.util.ArrayList;
Expand Down Expand Up @@ -112,27 +111,6 @@ default Uni<CountResponse> countDocuments(
});
}

/**
* A operation method which can return FindResponse instead of CommandResult. This method will be
* used by other commands which needs a document to be read.
*
* @param queryExecutor
* @param pagingState
* @param additionalIdFilter Used if a additional id filter need to be added to already available
* filters
* @return
*/
Uni<FindResponse> getDocuments(
QueryExecutor queryExecutor, String pagingState, DBFilterBase.IDFilter additionalIdFilter);

/**
* A operation method which can return ReadDocument with an empty document, if the filter
* condition has _id filter it will return document with this field added
*
* @return
*/
ReadDocument getNewDocument();

record FindResponse(List<ReadDocument> docs, String pagingState) {}

record CountResponse(int count) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,5 @@ public enum ReadType {
* Return only document id and transaction id of documents which satisfies the filter conditions
* as part of response
*/
KEY,
/** Return only count of documents which satisfies the filter condition */
COUNT
KEY
Copy link
Contributor Author

@maheshrajamani maheshrajamani Mar 22, 2023

Choose a reason for hiding this comment

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

Holding on to this Enum, will be adding SORT_DOCUMENT type in the next PR for in memory sort.

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
*/
public record DeleteOperation(
CommandContext commandContext,
ReadOperation readOperation,
FindOperation findOperation,
/**
* Added parameter to pass number of document to be deleted, this is needed because read
* documents limit changed to deleteLimit + 1
Expand All @@ -46,7 +46,7 @@ public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
() -> new AtomicReference<String>(null),
stateRef -> {
Uni<ReadOperation.FindResponse> docsToDelete =
readOperation().getDocuments(queryExecutor, stateRef.get(), null);
findOperation().getDocuments(queryExecutor, stateRef.get(), null);
return docsToDelete
.onItem()
.invoke(findResponse -> stateRef.set(findResponse.pagingState()));
Expand Down Expand Up @@ -173,7 +173,7 @@ private Uni<Boolean> deleteDocument(
private Uni<ReadDocument> readDocumentAgain(
QueryExecutor queryExecutor, ReadDocument prevReadDoc) {
// Read again if retry flag is `true`
return readOperation()
return findOperation()
.getDocuments(
queryExecutor,
null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,16 @@ public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
.map(docs -> new ReadOperationPage(docs.docs(), docs.pagingState()));
}

@Override
/**
* A operation method which can return FindResponse instead of CommandResult. This method will be
* used by other commands which needs a document to be read.
*
* @param queryExecutor
* @param pagingState
* @param additionalIdFilter Used if a additional id filter need to be added to already available
* filters
* @return
*/
public Uni<FindResponse> getDocuments(
QueryExecutor queryExecutor, String pagingState, DBFilterBase.IDFilter additionalIdFilter) {

Expand All @@ -67,8 +76,12 @@ public Uni<FindResponse> getDocuments(
}
}

/** {@inheritDoc} */
@Override
/**
* A operation method which can return ReadDocument with an empty document, if the filter
* condition has _id filter it will return document with this field added
*
* @return
*/
public ReadDocument getNewDocument() {
ObjectNode rootNode = objectMapper().createObjectNode();
DocumentId documentId = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* This operation method is used for 3 commands findOneAndUpdate, updateOne and updateMany
*
* @param commandContext
* @param readOperation
* @param findOperation
* @param documentUpdater
* @param returnDocumentInResponse - if `true` return document
* @param returnUpdatedDocument - if `true` return after update document, else before document
Expand All @@ -38,7 +38,7 @@
*/
public record ReadAndUpdateOperation(
CommandContext commandContext,
ReadOperation readOperation,
FindOperation findOperation,
DocumentUpdater documentUpdater,
boolean returnDocumentInResponse,
boolean returnUpdatedDocument,
Expand All @@ -59,7 +59,7 @@ public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
() -> new AtomicReference<String>(null),
stateRef -> {
Uni<ReadOperation.FindResponse> docsToUpdate =
readOperation().getDocuments(queryExecutor, stateRef.get(), null);
findOperation().getDocuments(queryExecutor, stateRef.get(), null);
return docsToUpdate
.onItem()
.invoke(findResponse -> stateRef.set(findResponse.pagingState()));
Expand All @@ -72,7 +72,7 @@ public Uni<Supplier<CommandResult>> execute(QueryExecutor queryExecutor) {
findResponse -> {
final List<ReadDocument> docs = findResponse.docs();
if (upsert() && docs.size() == 0 && matchedCount.get() == 0) {
return Multi.createFrom().item(readOperation().getNewDocument());
return Multi.createFrom().item(findOperation().getNewDocument());
} else {
// Below conditionality is because we read up to updateLimit +1 record.
if (matchedCount.get() + docs.size() <= updateLimit) {
Expand Down Expand Up @@ -259,7 +259,7 @@ protected static QueryOuterClass.Query bindUpdateValues(
*/
private Uni<ReadDocument> readDocumentAgain(
QueryExecutor queryExecutor, ReadDocument prevReadDoc) {
return readOperation()
return findOperation()
.getDocuments(
queryExecutor,
null,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package io.stargate.sgv2.jsonapi.service.resolver.model.impl;

import com.fasterxml.jackson.databind.ObjectMapper;
import io.stargate.sgv2.jsonapi.api.model.command.CommandContext;
import io.stargate.sgv2.jsonapi.api.model.command.impl.CountDocumentsCommands;
import io.stargate.sgv2.jsonapi.service.operation.model.CountOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadType;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand All @@ -15,8 +16,8 @@
public class CountDocumentsCommandResolver extends FilterableResolver<CountDocumentsCommands>
implements CommandResolver<CountDocumentsCommands> {
@Inject
public CountDocumentsCommandResolver(ObjectMapper objectMapper) {
super(objectMapper);
public CountDocumentsCommandResolver() {
super();
}

@Override
Expand All @@ -26,11 +27,7 @@ public Class<CountDocumentsCommands> getCommandClass() {

@Override
public Operation resolveCommand(CommandContext ctx, CountDocumentsCommands command) {
return resolve(ctx, command);
}

@Override
protected FilteringOptions getFilteringOption(CountDocumentsCommands command) {
return new FilteringOptions(Integer.MAX_VALUE, null, 1, ReadType.COUNT);
List<DBFilterBase> filters = resolve(ctx, command);
return new CountOperation(ctx, filters);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import io.stargate.sgv2.jsonapi.api.model.command.impl.DeleteManyCommand;
import io.stargate.sgv2.jsonapi.service.bridge.config.DocumentConfig;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadType;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DeleteOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.FindOperation;
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand All @@ -22,19 +24,21 @@ public class DeleteManyCommandResolver extends FilterableResolver<DeleteManyComm
implements CommandResolver<DeleteManyCommand> {

private final DocumentConfig documentConfig;
private final ObjectMapper objectMapper;

@Inject
public DeleteManyCommandResolver(DocumentConfig documentConfig, ObjectMapper objectMapper) {
super(objectMapper);
super();
this.documentConfig = documentConfig;
this.objectMapper = objectMapper;
}

@Override
public Operation resolveCommand(CommandContext commandContext, DeleteManyCommand command) {
ReadOperation readOperation = resolve(commandContext, command);
final FindOperation findOperation = getFindOperation(commandContext, command);
return new DeleteOperation(
commandContext,
readOperation,
findOperation,
documentConfig.maxDocumentDeleteCount(),
documentConfig.lwt().retries());
}
Expand All @@ -44,12 +48,16 @@ public Class<DeleteManyCommand> getCommandClass() {
return DeleteManyCommand.class;
}

@Override
protected FilteringOptions getFilteringOption(DeleteManyCommand command) {
return new FilteringOptions(
documentConfig.maxDocumentDeleteCount() + 1,
private FindOperation getFindOperation(CommandContext commandContext, DeleteManyCommand command) {
List<DBFilterBase> filters = resolve(commandContext, command);
// Read One extra document than delete limit so return moreData flag
return new FindOperation(
commandContext,
filters,
null,
documentConfig.maxDocumentDeleteCount() + 1,
documentConfig.defaultPageSize(),
ReadType.KEY);
ReadType.KEY,
objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import io.stargate.sgv2.jsonapi.api.model.command.impl.DeleteOneCommand;
import io.stargate.sgv2.jsonapi.service.bridge.config.DocumentConfig;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadType;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DeleteOperation;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.FindOperation;
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand All @@ -22,26 +24,28 @@ public class DeleteOneCommandResolver extends FilterableResolver<DeleteOneComman
implements CommandResolver<DeleteOneCommand> {

private final DocumentConfig documentConfig;
private final ObjectMapper objectMapper;

@Inject
public DeleteOneCommandResolver(DocumentConfig documentConfig, ObjectMapper objectMapper) {
super(objectMapper);
super();
this.documentConfig = documentConfig;
this.objectMapper = objectMapper;
}

@Override
public Operation resolveCommand(CommandContext commandContext, DeleteOneCommand command) {
ReadOperation readOperation = resolve(commandContext, command);
return new DeleteOperation(commandContext, readOperation, 1, documentConfig.lwt().retries());
FindOperation findOperation = getFindOperation(commandContext, command);
return new DeleteOperation(commandContext, findOperation, 1, documentConfig.lwt().retries());
}

@Override
public Class<DeleteOneCommand> getCommandClass() {
return DeleteOneCommand.class;
}

@Override
protected FilteringOptions getFilteringOption(DeleteOneCommand command) {
return new FilteringOptions(1, null, 1, ReadType.KEY);
private FindOperation getFindOperation(CommandContext commandContext, DeleteOneCommand command) {
List<DBFilterBase> filters = resolve(commandContext, command);
return new FindOperation(commandContext, filters, null, 1, 1, ReadType.KEY, objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import io.stargate.sgv2.jsonapi.service.bridge.config.DocumentConfig;
import io.stargate.sgv2.jsonapi.service.operation.model.Operation;
import io.stargate.sgv2.jsonapi.service.operation.model.ReadType;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.DBFilterBase;
import io.stargate.sgv2.jsonapi.service.operation.model.impl.FindOperation;
import io.stargate.sgv2.jsonapi.service.resolver.model.CommandResolver;
import io.stargate.sgv2.jsonapi.service.resolver.model.impl.matcher.FilterableResolver;
import java.util.List;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;

Expand All @@ -18,10 +21,12 @@ public class FindCommandResolver extends FilterableResolver<FindCommand>
implements CommandResolver<FindCommand> {

private final DocumentConfig documentConfig;
private final ObjectMapper objectMapper;

@Inject
public FindCommandResolver(DocumentConfig documentConfig, ObjectMapper objectMapper) {
super(objectMapper);
super();
this.objectMapper = objectMapper;
this.documentConfig = documentConfig;
}

Expand All @@ -31,18 +36,15 @@ public Class<FindCommand> getCommandClass() {
}

@Override
public Operation resolveCommand(CommandContext ctx, FindCommand command) {
return resolve(ctx, command);
}

@Override
protected FilteringOptions getFilteringOption(FindCommand command) {
public Operation resolveCommand(CommandContext commandContext, FindCommand command) {
List<DBFilterBase> filters = resolve(commandContext, command);
int limit =
command.options() != null && command.options().limit() != null
? command.options().limit()
: documentConfig.maxLimit();
int pageSize = documentConfig.defaultPageSize();
String pagingState = command.options() != null ? command.options().pagingState() : null;
return new FilteringOptions(limit, pagingState, pageSize, ReadType.DOCUMENT);
return new FindOperation(
commandContext, filters, pagingState, limit, pageSize, ReadType.DOCUMENT, objectMapper);
}
}
Loading