/* -*-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
#define OSGEARTH_DRIVER_CACHE_ROCKSDB 1

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

namespace osgEarth { namespace Drivers { 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::Drivers::RocksDBCache

#endif // OSGEARTH_DRIVER_CACHE_ROCKSDB
