/* osgEarth
 * Copyright 2025 Pelican Mapping
 * MIT License
 */
#ifndef OSGEARTH_DRIVER_CACHE_ROCKSDB_OPTIONS
#define OSGEARTH_DRIVER_CACHE_ROCKSDB_OPTIONS 1

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

namespace osgEarth { 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.set( "path", _path );
			conf.set( "log_path", _logPath );
            conf.set( "max_size_mb", _maxSizeMB );
            conf.set( "size_check_period", _sizeCheckPeriod );
            conf.set( "size_purge_period", _sizePurgePeriod );
            conf.set( "block_size", _blockSize );
			conf.set( "block_cache_size", _blockCacheSize );
			conf.set( "write_buffer_size", _writeBufferSize );
			conf.set( "max_files_level0", _maxFilesLevel0 );
			conf.set( "min_buffers_to_merge", _minBuffersToMerge );
            conf.set( "key", _key );
            return conf;
        }
        virtual void mergeConfig( const Config& conf ) {
            ConfigOptions::mergeConfig( conf );
            fromConfig( conf );
        }

    private:
        void fromConfig( const Config& conf ) {
            conf.get( "path", _path );
			conf.get( "log_path", _logPath );
            conf.get( "max_size_mb", _maxSizeMB );
            conf.get( "size_check_period", _sizeCheckPeriod );
            conf.get( "size_purge_period", _sizePurgePeriod );
            conf.get( "block_size", _blockSize );
			conf.get( "block_cache_size", _blockCacheSize );
			conf.get( "write_buffer_size", _writeBufferSize );
			conf.get( "max_files_level0", _maxFilesLevel0 );
			conf.get( "min_buffers_to_merge", _minBuffersToMerge );
            conf.get( "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::RocksDBCache

#endif // OSGEARTH_DRIVER_CACHE_ROCKSDB_OPTIONS
