/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2016 Pelican Mapping
 * http://osgearth.org
 *
 * osgEarth is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 */
#ifndef OSGEARTH_DRIVER_CACHE_ROCKSDB_OPTIONS
#define OSGEARTH_DRIVER_CACHE_ROCKSDB_OPTIONS 1

#include <osgEarth/Common>
#include <osgEarth/Cache>
#include <string>

namespace osgEarth { namespace Drivers { namespace RocksDBCache
{
    using namespace osgEarth;
    
    /**
     * Serializable options for the RocksDBCache.
     */
    class RocksDBCacheOptions : public CacheOptions
    {
    public:
        RocksDBCacheOptions( const ConfigOptions& options =ConfigOptions() )
            : CacheOptions      ( options ),
              _maxSizeMB        ( 0 ),
              _sizeCheckPeriod  ( 100 ),
              _sizePurgePeriod  ( 75 ),
              _blockSize        ( 262144 ),// 256K
			  _blockCacheSize   ( 16777216 ), // 16MB
			  _writeBufferSize  ( 134217728 ), // 128MB
			  _maxFilesLevel0   ( 10 ),
			  _minBuffersToMerge( 1 )
        {
            setDriver( "RocksDB" );
            fromConfig( _conf ); 
        }

        /** dtor */
        virtual ~RocksDBCacheOptions() { }

    public:
        /** Folder containing the cache bins. */
        optional<std::string>& rootPath() { return _path; }
        const optional<std::string>& rootPath() const { return _path; }

		/** Folder to write RocksDB log files to. */
		optional<std::string>& logPath() { return _logPath; }
		const optional<std::string>& logPath() const { return _logPath; }

        /** Maximum size of the cache in megabytes. Note: this is not
         *  a guarantee - merely a guideline. */
        optional<unsigned>& maxSizeMB() { return _maxSizeMB; }
        const optional<unsigned>& maxSizeMB() const { return _maxSizeMB; }

        //--- Advanced options ---

        /** Number of writes between cap checks */
        optional<unsigned>& sizeCheckPeriod() { return _sizeCheckPeriod; }
        const optional<unsigned>& sizeCheckPeriod() const { return _sizeCheckPeriod; }

        /** Number of writer between cap purges */
        optional<unsigned>& sizePurgePeriod() { return _sizePurgePeriod; }
        const optional<unsigned>& sizePurgePeriod() const { return _sizePurgePeriod; }

        /** RocksDB block size */
        optional<unsigned>& blockSize() { return _blockSize; }
        const optional<unsigned>& blockSize() const { return _blockSize; }

		/** RocksDB LRU Cache size */
		optional<unsigned>& blockCacheSize() { return _blockCacheSize; }
		const optional<unsigned>& blockCacheSize() const { return _blockCacheSize; }

		/** RocksDB write_buffer_size: size of in-memory data block before being written to disk */
		optional<unsigned>& writeBufferSize() { return _writeBufferSize; }
		const optional<unsigned>& writeBufferSize() const { return _writeBufferSize; }

		/** RocksDB level0_file_num_compaction_trigger: number of files in level 0 before a compaction is triggered */
		optional<unsigned>& maxFilesLevel0() { return _maxFilesLevel0; }
		const optional<unsigned>& maxFilesLevel0() const { return _maxFilesLevel0; }

		/** RocksDB min_write_buffer_number_to_merge: number of full write buffers needed for level 0 merge */
		optional<unsigned>& minBuffersToMerge() { return _minBuffersToMerge; }
		const optional<unsigned>& minBuffersToMerge() const { return _minBuffersToMerge; }

        /** Obfuscation key string */
        optional<std::string>& key() { return _key; }
        const optional<std::string>& key() const { return _key; }

    public:
        virtual Config getConfig() const {
            Config conf = ConfigOptions::getConfig();
            conf.addIfSet( "path", _path );
			conf.addIfSet( "log_path", _logPath );
            conf.addIfSet( "max_size_mb", _maxSizeMB );
            conf.addIfSet( "size_check_period", _sizeCheckPeriod );
            conf.addIfSet( "size_purge_period", _sizePurgePeriod );
            conf.addIfSet( "block_size", _blockSize );
			conf.addIfSet( "block_cache_size", _blockCacheSize );
			conf.addIfSet( "write_buffer_size", _writeBufferSize );
			conf.addIfSet( "max_files_level0", _maxFilesLevel0 );
			conf.addIfSet( "min_buffers_to_merge", _minBuffersToMerge );
            conf.addIfSet( "key", _key );
            return conf;
        }
        virtual void mergeConfig( const Config& conf ) {
            ConfigOptions::mergeConfig( conf );
            fromConfig( conf );
        }

    private:
        void fromConfig( const Config& conf ) {
            conf.getIfSet( "path", _path );
			conf.getIfSet( "log_path", _logPath );
            conf.getIfSet( "max_size_mb", _maxSizeMB );
            conf.getIfSet( "size_check_period", _sizeCheckPeriod );
            conf.getIfSet( "size_purge_period", _sizePurgePeriod );
            conf.getIfSet( "block_size", _blockSize );
			conf.getIfSet( "block_cache_size", _blockCacheSize );
			conf.getIfSet( "write_buffer_size", _writeBufferSize );
			conf.getIfSet( "max_files_level0", _maxFilesLevel0 );
			conf.getIfSet( "min_buffers_to_merge", _minBuffersToMerge );
            conf.getIfSet( "key", _key );
        }

        optional<std::string> _path;
		optional<std::string> _logPath;
        optional<unsigned>    _maxSizeMB;
        optional<unsigned>    _sizeCheckPeriod;
        optional<unsigned>    _sizePurgePeriod;
        optional<unsigned>    _blockSize;
		optional<unsigned>    _blockCacheSize;
		optional<unsigned>    _writeBufferSize;
		optional<unsigned>    _maxFilesLevel0;
		optional<unsigned>    _minBuffersToMerge;
        optional<std::string> _key;
    };

} } } // namespace osgEarth::Drivers::RocksDBCache

#endif // OSGEARTH_DRIVER_CACHE_ROCKSDB_OPTIONS
