From 93dfaec7437ab5a3b82a530a148952f067556d64 Mon Sep 17 00:00:00 2001 From: Mark Wolters Date: Tue, 11 Jun 2024 12:56:32 -0400 Subject: [PATCH] new DataApiFindById op type --- .../adapter/dataapi/DataApiOpMapper.java | 1 + .../DataApiFindByIdOpDispenser.java | 62 +++++++++++++++++++ .../dataapi/ops/DataApiFindByIdOp.java | 38 ++++++++++++ .../adapter/dataapi/ops/DataApiOpType.java | 1 + 4 files changed, 102 insertions(+) create mode 100644 nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiFindByIdOpDispenser.java create mode 100644 nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiFindByIdOp.java diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/DataApiOpMapper.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/DataApiOpMapper.java index d075f0838..cdad5e57d 100644 --- a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/DataApiOpMapper.java +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/DataApiOpMapper.java @@ -64,6 +64,7 @@ public class DataApiOpMapper implements OpMapper { new DataApiListCollectionNamesOpDispenser(adapter, op, typeAndTarget.targetFunction); case estimated_document_count -> new DataApiEstimatedDocumentCountOpDispenser(adapter, op, typeAndTarget.targetFunction); + case find_by_id -> new DataApiFindByIdOpDispenser(adapter, op, typeAndTarget.targetFunction); }; } } diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiFindByIdOpDispenser.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiFindByIdOpDispenser.java new file mode 100644 index 000000000..01469723f --- /dev/null +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/opdispensers/DataApiFindByIdOpDispenser.java @@ -0,0 +1,62 @@ +/* + * 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.opdispensers; + +import com.datastax.astra.client.Database; +import com.datastax.astra.client.model.Filter; +import com.datastax.astra.client.model.FindOneOptions; +import com.datastax.astra.client.model.Projection; +import com.datastax.astra.client.model.Sort; +import io.nosqlbench.adapter.dataapi.DataApiDriverAdapter; +import io.nosqlbench.adapter.dataapi.ops.DataApiBaseOp; +import io.nosqlbench.adapter.dataapi.ops.DataApiFindByIdOp; +import io.nosqlbench.adapter.dataapi.ops.DataApiFindOneOp; +import io.nosqlbench.adapters.api.templating.ParsedOp; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.util.function.LongFunction; + +public class DataApiFindByIdOpDispenser extends DataApiOpDispenser { + private static final Logger logger = LogManager.getLogger(DataApiFindByIdOpDispenser.class); + private final LongFunction opFunction; + public DataApiFindByIdOpDispenser(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(); + Object id = getIdFromOp(op, l); + return new DataApiFindByIdOp( + db, + db.getCollection(targetFunction.apply(l)), + id + ); + }; + } + + private Object getIdFromOp(ParsedOp op, long l) { + return op.getAsRequiredFunction("id", Object.class).apply(l); + } + + @Override + public DataApiBaseOp getOp(long value) { + return opFunction.apply(value); + } +} diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiFindByIdOp.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiFindByIdOp.java new file mode 100644 index 000000000..603854512 --- /dev/null +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiFindByIdOp.java @@ -0,0 +1,38 @@ +/* + * 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.Document; +import com.datastax.astra.client.model.Filter; + +public class DataApiFindByIdOp extends DataApiBaseOp { + private final Collection collection; + private final Object id; + + public DataApiFindByIdOp(Database db, Collection collection, Object id) { + super(db); + this.collection = collection; + this.id = id; + } + + @Override + public Object apply(long value) { + return collection.findById(id); + } +} diff --git a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiOpType.java b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiOpType.java index fb1ac4a84..99577c3c2 100644 --- a/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiOpType.java +++ b/nb-adapters/adapter-dataapi/src/main/java/io/nosqlbench/adapter/dataapi/ops/DataApiOpType.java @@ -35,4 +35,5 @@ public enum DataApiOpType { list_collections, list_collection_names, estimated_document_count, + find_by_id, }