/* osgEarth
 * Copyright 2016 Pelican Mapping
 * MIT License
 */
#ifndef OSGEARTH_DRIVER_CACHE_LEVELDB
#define OSGEARTH_DRIVER_CACHE_LEVELDB 1

#include "LevelDBCacheOptions"
#include "Tracker"
#include <osgEarth/Common>
#include <osgEarth/Cache>
#include <leveldb/db.h>

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

        /**
         * Constructs a new leveldb cache object.
         * @param options Options structure that comes from a serialized description of 
         *        the object (see LevelDBCacheOptions)
         */
        LevelDBCacheImpl( 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;
        leveldb::DB* _db;
        osg::ref_ptr<Tracker> _tracker;
        LevelDBCacheOptions _options;
    };


} } } // namespace osgEarth::Drivers::LevelDBCache

#endif // OSGEARTH_DRIVER_CACHE_LEVELDB
