Java tutorial
/* * 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.collection; import com.mongodb.DBObject; import de.otto.mongodb.profiler.AverageMeasure; import org.joda.time.DateTime; import java.util.Deque; import java.util.Iterator; import java.util.concurrent.ConcurrentLinkedDeque; import static com.google.common.base.Preconditions.checkNotNull; import static de.otto.mongodb.profiler.util.MongoSupport.require; /** * Default implementation of a collection profile. * * @author Robert Gacki */ public class DefaultCollectionProfile implements CollectionProfile { public static final AverageMeasure DEFAULT_PADDING_FACTOR_MEASURE = new AverageMeasure(); public static final AverageMeasure DEFAULT_DOCUMENT_COUNT_MEASURE = new AverageMeasure(); private final String collectionName; private final Deque<Mark> marks; private final boolean cappedCollection; private volatile AverageMeasure paddingFactorMeasure; private volatile AverageMeasure documentCountMeasure; public DefaultCollectionProfile(String collectionName, boolean cappedCollection) { this.collectionName = checkNotNull(collectionName); this.cappedCollection = cappedCollection; this.marks = new ConcurrentLinkedDeque<>(); this.paddingFactorMeasure = DEFAULT_PADDING_FACTOR_MEASURE; this.documentCountMeasure = DEFAULT_DOCUMENT_COUNT_MEASURE; } public synchronized void reset() { this.marks.clear(); this.paddingFactorMeasure = DEFAULT_PADDING_FACTOR_MEASURE; this.documentCountMeasure = DEFAULT_DOCUMENT_COUNT_MEASURE; } public void removeMarks(DateTime before) { final long time = before.getMillis(); final Iterator<Mark> marks = this.marks.iterator(); while (marks.hasNext()) { if (marks.next().getTime() < time) { marks.remove(); } else { return; } } } public synchronized void add(DBObject dbo) { checkNotNull(dbo); final Number documentCount = require("count", Number.class, dbo); documentCountMeasure = documentCountMeasure.add(documentCount.doubleValue()); final Number paddingFactor = require("paddingFactor", Number.class, dbo); paddingFactorMeasure = paddingFactorMeasure.add(paddingFactor.doubleValue()); final Number dataSize = require("size", Number.class, dbo); final Number dataStorageSize = require("storageSize", Number.class, dbo); final Mark mark = new Mark(System.currentTimeMillis(), paddingFactor.doubleValue(), dataSize.longValue(), dataStorageSize.longValue(), documentCount.longValue()); marks.add(mark); } public String getCollectionName() { return collectionName; } public boolean isDataAvailable() { return !marks.isEmpty(); } public boolean isCappedCollection() { return cappedCollection; } public Deque<Mark> getMarks() { return marks; } public AverageMeasure getPaddingFactorMeasure() { return paddingFactorMeasure; } public AverageMeasure getDocumentCountMeasure() { return documentCountMeasure; } }