de.otto.mongodb.profiler.op.analyze.FindAndModifyCommandAnalyzer.java Source code

Java tutorial

Introduction

Here is the source code for de.otto.mongodb.profiler.op.analyze.FindAndModifyCommandAnalyzer.java

Source

/*
 *  Copyright 2013 Robert Gacki <robert.gacki@cgi.com>
 *
 *  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 de.otto.mongodb.profiler.op.analyze;

import com.mongodb.DBObject;
import de.otto.mongodb.profiler.op.Explanation;
import de.otto.mongodb.profiler.op.FindAndModifyProfile;
import de.otto.mongodb.profiler.op.ProfileEntry;
import org.joda.time.DateTime;

import static de.otto.mongodb.profiler.util.MongoSupport.require;

/**
 * Analyzer for the findandmodify command.
 *
 * @author Robert Gacki
 */
public class FindAndModifyCommandAnalyzer extends CommandAnalyzer {

    private static final String FIND_AND_MODIFY_COMMAND = "findandmodify";

    @Override
    protected boolean analyzeCommand(DBObject dbo, AnalyzerContext context) {

        final DBObject command = require("command", DBObject.class, dbo);

        if (!command.containsField(FIND_AND_MODIFY_COMMAND)) {
            return false;
        }

        final String op = require("op", String.class, dbo);
        final String ns = require("ns", String.class, dbo) + "."
                + require(FIND_AND_MODIFY_COMMAND, String.class, command);
        final DBObject queryData = require("query", DBObject.class, command);
        final DefaultFindAndModifyProfile.Outcome outcome = DefaultFindAndModifyProfile.Outcome
                .fromCommand(command);

        if (outcome == null) {
            return false;
        }

        final String hash = context.createQueryHash(ns, op + FIND_AND_MODIFY_COMMAND + outcome, queryData);

        DefaultFindAndModifyProfile profile = context.findProfile(hash, DefaultFindAndModifyProfile.class);

        if (profile == null) {
            profile = context.addProfile(hash, new DefaultFindAndModifyProfile(hash, ns,
                    context.getOutlierConfiguration(), queryData, outcome));
        }

        if (context.shouldExplainQuery(profile)) {
            final Explanation explanation = context
                    .explainQuery(require(FIND_AND_MODIFY_COMMAND, String.class, command), profile);
            if (explanation != null) {
                profile.add(explanation);
            }
        }

        profile.add(new ProfileEntry(dbo, DateTime.now()));

        return true;
    }

    private static class DefaultFindAndModifyProfile extends DefaultUpdateProfile implements FindAndModifyProfile {

        private final Outcome outcome;

        public DefaultFindAndModifyProfile(final String id, final String ns,
                final OutlierConfiguration outlierConfiguration, final DBObject query, final Outcome outcome) {
            super(id, ns, outlierConfiguration, query);
            this.outcome = outcome;
        }

        @Override
        public String getType() {
            return FindAndModifyProfile.TYPE_VALUE;
        }

        @Override
        public Outcome getOutcome() {
            return outcome;
        }
    }
}