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

Java tutorial

Introduction

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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

/**
 * Analyzer of the profile of an 'aggregate' command.
 *
 * @author Robert Gacki
 */
public class AggregateCommandAnalyzer extends CommandAnalyzer {

    public static final String COMMAND = "aggregate";

    @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 List<DBObject> pipeline = require("pipeline", List.class, command);

        if (pipeline == null || pipeline.isEmpty()) {
            return false;
        }

        final String hash = context.createAggregateHash(ns, pipeline);

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

        if (profile == null) {
            profile = context.addProfile(hash,
                    new DefaultAggregateProfile(hash, ns, context.getOutlierConfiguration(), pipeline));
        }

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

        return true;
    }

    private static class DefaultAggregateProfile extends DefaultOpProfile implements AggregateProfile {

        private final List<DBObject> pipeline;

        public DefaultAggregateProfile(final String id, final String ns,
                final OutlierConfiguration outlierConfiguration, final List<DBObject> pipeline) {
            super(id, ns, outlierConfiguration);
            this.pipeline = Collections.unmodifiableList(new ArrayList<>(pipeline));
        }

        @Override
        public List<DBObject> getPipeline() {
            return pipeline;
        }

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