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

#include "RocksDBCacheOptions"
#include "Tracker"
#include <osgEarth/Common>
#include <osgEarth/Cache>
#include <rocksdb/db.h>

namespace osgEarth { namespace RocksDBCache
{    
    /** 
     * Cache that stores data in a ROCKSDB database in the local filesystem.
     */
    class RocksDBCacheImpl : public osgEarth::Cache
    {
    public:
        META_Object( osgEarth, RocksDBCacheImpl );
        virtual ~RocksDBCacheImpl();
        RocksDBCacheImpl() { } // unused
        RocksDBCacheImpl( const RocksDBCacheImpl& rhs, const osg::CopyOp& op ) { } // unused

        /**
         * Constructs a new rocksdb cache object.
         * @param options Options structure that comes from a serialized description of 
         *        the object (see RocksDBCacheOptions)
         */
        RocksDBCacheImpl( const osgEarth::CacheOptions& options );

    public: // Cache interface

        osgEarth::CacheBin* addBin( const std::string& binID );

        osgEarth::CacheBin* getOrCreateDefaultBin();

        off_t getApproximateSize() const;

        // Compact the cache, reclaiming space fragmented by removing records
        bool compact();

        // Clear all records from the cache
        bool clear();

    protected:

        void init();
        void open();

        std::string  _rootPath;
        bool         _active;
        rocksdb::DB* _db;
        osg::ref_ptr<Tracker> _tracker;
        RocksDBCacheOptions _options;
    };


} } // namespace osgEarth::RocksDBCache

#endif // OSGEARTH_DRIVER_CACHE_ROCKSDB
