/* -*-c++-*- */
/* osgEarth - Dynamic map generation toolkit for OpenSceneGraph
 * Copyright 2008-2010 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_SQLITE3_CACHE_DRIVEROPTIONS
#define OSGEARTH_DRIVER_SQLITE3_CACHE_DRIVEROPTIONS 1

#include <osgEarth/Common>
#include <osgEarth/Caching>
#include <osgEarth/TileSource>

// there is leak using the cache if compressed. It's resolved in
// rev 11609 of osg svn

namespace osgEarth { namespace Drivers
{
    using namespace osgEarth;

    class Sqlite3CacheOptions : public CacheOptions // NO EXPORT; header only
    {
    public:
        /**
         * Pathname of the database file.
         */
        optional<std::string>& path() { return _path; }
        const optional<std::string>& path() const { return _path; }

        optional<bool>& asyncWrites() { return _useAsyncWrites; }
        const optional<bool>& asyncWrites() const { return _useAsyncWrites; }

        optional<bool>& serialized() { return _serialized; }
        const optional<bool>& serialized() const { return _serialized; }

        optional<unsigned int>& maxSize() { return _maxSize; }
        const optional<unsigned int>& maxSize() const { return _maxSize; }


    public:
        Sqlite3CacheOptions( const ConfigOptions& options =ConfigOptions() )
            : CacheOptions( options ),
              _useAsyncWrites( true ), 
              _serialized( false ),
              _maxSize(100)
        {
            setDriver( "sqlite3" );
            fromConfig( _conf );
        }

        Config getConfig() const {
            Config conf = CacheOptions::getConfig();
            conf.updateIfSet( "path", _path );
            conf.updateIfSet( "async_writes", _useAsyncWrites );
            conf.updateIfSet( "serialized", _serialized );
            conf.updateIfSet( "max_size", _maxSize );
            return conf;
        }

        void mergeConfig( const Config& conf ) {
            CacheOptions::mergeConfig( conf );
            fromConfig( conf );
        }

        void fromConfig( const Config& conf ) {
            conf.getIfSet( "path", _path );
            conf.getIfSet( "async_writes", _useAsyncWrites );
            conf.getIfSet( "serialized", _serialized );
            conf.getIfSet( "max_size", _maxSize );
        }

        optional<std::string> _path;
        optional<bool> _useAsyncWrites;
        optional<bool> _serialized;
        optional<unsigned int>_maxSize; // layer - MB
    };

} } // namespace osgEarth::Drivers

#endif // OSGEARTH_DRIVER_SQLITE3_CACHE_DRIVEROPTIONS

