new DataApiFindById op type

This commit is contained in:
Mark Wolters 2024-06-11 12:56:32 -04:00
parent 37cd2f7bb4
commit 93dfaec743
4 changed files with 102 additions and 0 deletions

View File

@ -64,6 +64,7 @@ public class DataApiOpMapper implements OpMapper<DataApiBaseOp> {
new DataApiListCollectionNamesOpDispenser(adapter, op, typeAndTarget.targetFunction); new DataApiListCollectionNamesOpDispenser(adapter, op, typeAndTarget.targetFunction);
case estimated_document_count -> case estimated_document_count ->
new DataApiEstimatedDocumentCountOpDispenser(adapter, op, typeAndTarget.targetFunction); new DataApiEstimatedDocumentCountOpDispenser(adapter, op, typeAndTarget.targetFunction);
case find_by_id -> new DataApiFindByIdOpDispenser(adapter, op, typeAndTarget.targetFunction);
}; };
} }
} }

View File

@ -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<DataApiFindByIdOp> opFunction;
public DataApiFindByIdOpDispenser(DataApiDriverAdapter adapter, ParsedOp op, LongFunction<String> targetFunction) {
super(adapter, op, targetFunction);
this.opFunction = createOpFunction(op);
}
private LongFunction<DataApiFindByIdOp> 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);
}
}

View File

@ -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<Document> collection;
private final Object id;
public DataApiFindByIdOp(Database db, Collection<Document> collection, Object id) {
super(db);
this.collection = collection;
this.id = id;
}
@Override
public Object apply(long value) {
return collection.findById(id);
}
}

View File

@ -35,4 +35,5 @@ public enum DataApiOpType {
list_collections, list_collections,
list_collection_names, list_collection_names,
estimated_document_count, estimated_document_count,
find_by_id,
} }