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

Java tutorial

Introduction

Here is the source code for de.otto.mongodb.profiler.op.analyze.MapReduceCommandAnalyzer.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.MapReduceProfile;
import de.otto.mongodb.profiler.op.ProfileEntry;
import org.joda.time.DateTime;

import static com.google.common.base.Preconditions.checkNotNull;
import static de.otto.mongodb.profiler.util.MongoSupport.require;

/**
 * Analyzer for the map-reduce command.
 *
 * @author Robert Gacki
 */
public class MapReduceCommandAnalyzer extends CommandAnalyzer {

    public static final String COMMAND = "mapreduce";

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

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

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

        final String ns = require("ns", String.class, dbo) + "." + require(COMMAND, String.class, command);

        final String mapFunction = require("map", String.class, command);
        final String reduceFunction = require("reduce", String.class, command);

        final String hash = context.createMapReduceHash(ns, mapFunction, reduceFunction);

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

        if (profile == null) {
            profile = context.addProfile(hash, new DefaultMapReduceProfile(hash, ns,
                    context.getOutlierConfiguration(), mapFunction, reduceFunction));
        }

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

        return true;
    }

    private static class DefaultMapReduceProfile extends DefaultOpProfile implements MapReduceProfile {

        private final String mapFunction;
        private final String reduceFunction;

        public DefaultMapReduceProfile(final String id, final String ns,
                final OutlierConfiguration outlierConfiguration, final String mapFunction,
                final String reduceFunction) {
            super(id, ns, outlierConfiguration);
            this.mapFunction = checkNotNull(mapFunction);
            this.reduceFunction = checkNotNull(reduceFunction);
        }

        @Override
        public String getMapFunction() {
            return mapFunction;
        }

        @Override
        public String getReduceFunction() {
            return reduceFunction;
        }

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

}