From c5f74e688cf09093a1de0af296d3ee86f8454248 Mon Sep 17 00:00:00 2001 From: Mark Wolters Date: Wed, 12 Jun 2024 09:38:05 -0400 Subject: [PATCH] update many op impl --- .../DataApiUpdateManyOpDispenser.java | 40 ++++++++++++++++- .../DataApiUpdateOneOpDispenser.java | 5 +-- .../dataapi/ops/DataApiUpdateManyOp.java | 43 +++++++++++++++++++ 3 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiUpdateManyOp.java diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateManyOpDispenser.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateManyOpDispenser.java index 82269b156..253e22349 100644 --- a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateManyOpDispenser.java +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateManyOpDispenser.java @@ -16,19 +16,57 @@ package io.nosqlbench.adapter.dataapi.opdispensers; +import com.datastax.astra.client.Database; +import com.datastax.astra.client.model.*; import io.nosqlbench.adapter.dataapi.DataApiDriverAdapter; import io.nosqlbench.adapter.dataapi.ops.DataApiBaseOp; +import io.nosqlbench.adapter.dataapi.ops.DataApiUpdateManyOp; import io.nosqlbench.adapters.api.templating.ParsedOp; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import java.util.Map; +import java.util.Optional; import java.util.function.LongFunction; public class DataApiUpdateManyOpDispenser extends DataApiOpDispenser { + private static final Logger logger = LogManager.getLogger(DataApiUpdateManyOpDispenser.class); + private final LongFunction opFunction; + public DataApiUpdateManyOpDispenser(DataApiDriverAdapter adapter, ParsedOp op, LongFunction targetFunction) { super(adapter, op, targetFunction); + this.opFunction = createOpFunction(op); + } + + private LongFunction createOpFunction(ParsedOp op) { + return (l) -> { + Database db = spaceFunction.apply(l).getDatabase(); + Filter filter = getFilterFromOp(op, l); + UpdateManyOptions options = getUpdateManyOptions(op, l); + LongFunction> docMapFunc = op.getAsRequiredFunction("updates", Map.class); + + return new DataApiUpdateManyOp( + db, + db.getCollection(targetFunction.apply(l)), + filter, + new Update(docMapFunc.apply(l)), + options + ); + }; + } + + private UpdateManyOptions getUpdateManyOptions(ParsedOp op, long l) { + UpdateManyOptions options = new UpdateManyOptions(); + Optional> upsertFunction = op.getAsOptionalFunction("upsert", Boolean.class); + if (upsertFunction.isPresent()) { + options = options.upsert(upsertFunction.get().apply(l)); + } + + return options; } @Override public DataApiBaseOp getOp(long value) { - return null; + return opFunction.apply(value); } } diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateOneOpDispenser.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateOneOpDispenser.java index 168521f50..88c2ab6e7 100644 --- a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateOneOpDispenser.java +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiUpdateOneOpDispenser.java @@ -43,14 +43,13 @@ public class DataApiUpdateOneOpDispenser extends DataApiOpDispenser { Database db = spaceFunction.apply(l).getDatabase(); Filter filter = getFilterFromOp(op, l); UpdateOneOptions options = getUpdateOneOptions(op, l); - LongFunction docMapFunc = op.getAsRequiredFunction("update", Map.class); - LongFunction docFunc = (long m) -> new Document(docMapFunc.apply(m)); + LongFunction> docMapFunc = op.getAsRequiredFunction("update", Map.class); return new DataApiUpdateOneOp( db, db.getCollection(targetFunction.apply(l)), filter, - new Update(docFunc.apply(l)), + new Update(docMapFunc.apply(l)), options ); }; diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiUpdateManyOp.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiUpdateManyOp.java new file mode 100644 index 000000000..4eaa75427 --- /dev/null +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiUpdateManyOp.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 nosqlbench + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.nosqlbench.adapter.dataapi.ops; + +import com.datastax.astra.client.Collection; +import com.datastax.astra.client.Database; +import com.datastax.astra.client.model.Filter; +import com.datastax.astra.client.model.Update; +import com.datastax.astra.client.model.UpdateManyOptions; + +public class DataApiUpdateManyOp extends DataApiBaseOp { + private final Collection collection; + private final Filter filter; + private final Update update; + private final UpdateManyOptions options; + + public DataApiUpdateManyOp(Database db, Collection collection, Filter filter, Update update, UpdateManyOptions options) { + super(db); + this.collection = collection; + this.filter = filter; + this.update = update; + this.options = options; + } + + @Override + public Object apply(long value) { + return collection.updateMany(filter, update, options); + } +}