/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.rya.mongodb.batch;

import org.apache.hadoop.conf.Configuration;

/**
 * Constants and utility methods related to batch writing statements in a MongoDB
 * Rya repository.
 */
public final class MongoDbBatchWriterUtils {
    /**
     * Config tag used to specify the number of statements to batch write at a
     * time.
     */
    public static final String BATCH_SIZE_TAG = "rya.mongodb.dao.batchwriter.size";

    /**
     * Config tag used to specify the time to wait in milliseconds to flush all
     * statements out that are queued for insertion if the queue has not filled
     * up to its capacity.
     */
    public static final String BATCH_FLUSH_TIME_MS_TAG = "rya.mongodb.dao.batchwriter.flushtime";

    /**
     * Private constructor to prevent instantiation.
     */
    private MongoDbBatchWriterUtils() {
    }

    /**
     * The number of statements to batch write at a time.
     * @param conf the {@link Configuration} to check.
     * @return the configured value or the default value.
     */
    public static int getConfigBatchSize(final Configuration conf) {
        return conf.getInt(BATCH_SIZE_TAG, MongoDbBatchWriterConfig.DEFAULT_BATCH_SIZE);
    }

    /**
     * The time to wait in milliseconds to flush all statements out that are
     * queued for insertion if the queue has not filled up to its capacity.
     * @param conf the {@link Configuration} to check.
     * @return the configured value or the default value.
     */
    public static long getConfigBatchFlushTimeMs(final Configuration conf) {
        return conf.getLong(BATCH_FLUSH_TIME_MS_TAG, MongoDbBatchWriterConfig.DEFAULT_BATCH_FLUSH_TIME_MS);
    }

    /**
     * Reads the specified configed to create and initialize a
     * {@link MongoDbBatchWriterConfig}. If no values are found then the default
     * values are used.
     * @param conf the {@link Configuration} to check.
     * @return  the {@link MongoDbBatchWriterConfig} populated with configured
     * values for the specified {@code conf}.
     */
    public static MongoDbBatchWriterConfig getMongoDbBatchWriterConfig(final Configuration conf) {
        final int batchSize = getConfigBatchSize(conf);
        final long batchFlushTimeMs = getConfigBatchFlushTimeMs(conf);
        final MongoDbBatchWriterConfig mongoDbBatchWriterConfig = new MongoDbBatchWriterConfig();
        mongoDbBatchWriterConfig.setBatchSize(batchSize);
        mongoDbBatchWriterConfig.setBatchFlushTimeMs(batchFlushTimeMs);
        return mongoDbBatchWriterConfig;
    }
}