All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.nosqlbench.adapter.mongodb.core.MongoOpMapper Maven / Gradle / Ivy

/*
 * Copyright (c) 2022 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.mongodb.core;

import io.nosqlbench.adapter.mongodb.dispensers.MongoCommandOpDispenser;
import io.nosqlbench.engine.api.activityimpl.OpDispenser;
import io.nosqlbench.engine.api.activityimpl.OpMapper;
import io.nosqlbench.engine.api.activityimpl.uniform.flowtypes.Op;
import io.nosqlbench.engine.api.templating.ParsedOp;
import io.nosqlbench.engine.api.templating.TypeAndTarget;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.Optional;
import java.util.function.LongFunction;

public class MongoOpMapper implements OpMapper {
    private final static Logger logger = LogManager.getLogger(MongoOpMapper.class);

    private final MongodbDriverAdapter adapter;

    public MongoOpMapper(MongodbDriverAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public OpDispenser apply(ParsedOp op) {
        LongFunction ctxNamer = op.getAsFunctionOr("space", "default");
        LongFunction spaceF = l -> adapter.getSpaceCache().get(ctxNamer.apply(l));
        Optional> oDatabaseF = op.getAsOptionalFunction("database");
        if (oDatabaseF.isEmpty()) {
            logger.warn("op field 'database' was not defined");
        }

        Optional> target = op.getOptionalTypeAndTargetEnum(MongoDBOpTypes.class, String.class);
        // For any of the named operations which are called out directly AND supported via the fluent API,
        // use specialized dispensers
        if (target.isPresent()) {
            TypeAndTarget targetdata = target.get();
            return switch (targetdata.enumId) {
                case command -> new MongoCommandOpDispenser(adapter, spaceF, op);
//                case update -> new MongoDbUpdateOpDispenser(adapter, op, targetdata.targetFunction);
//            case insert -> new MongoDbInsertOpDispenser(adapter, op, opTypeAndTarget.targetFunction);
//            case delete -> new MongoDbDeleteOpDispenser(adapter, op, opTypeAndTarget.targetFunction);
//            case find -> new mongoDbFindOpDispenser(adapter, op, opTypeAndTarget.targetFunction);
//            case findAndModify -> new MongoDbFindAndModifyOpDispenser(adapter, op, opTypeAndTarget.targetFunction);
//            case getMore -> new MongoDbGetMoreOpDispenser(adapter, op, opTypeAndTarget.targetFunction);
            };
        }
        // For everything else use the command API
        else {
            return new MongoCommandOpDispenser(adapter, spaceF, op);
        }





    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy