Lunchbox  1.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
lunchbox::PersistentMap Class Reference

Unified interface to save key-value pairs in a persistent store. More...

#include <persistentMap.h>

+ Inheritance diagram for lunchbox::PersistentMap:
+ Collaboration diagram for lunchbox::PersistentMap:

Public Member Functions

LUNCHBOX_API PersistentMap (const std::string &uri=std::string())
 Construct a new persistent map. More...
 
LUNCHBOX_API PersistentMap (const URI &uri)
 Construct a persistent map using an URI. More...
 
LUNCHBOX_API ~PersistentMap ()
 Destruct the persistent map. More...
 
template<class V >
bool insert (const std::string &key, const V &value)
 Insert or update a value in the database. More...
 
template<class V >
bool insert (const std::string &key, const std::vector< V > &values)
 Insert or update a vector of values in the database. More...
 
template<class V >
bool insert (const std::string &key, const std::set< V > &values)
 Insert or update a set of values in the database. More...
 
LUNCHBOX_API std::string operator[] (const std::string &key) const
 Retrieve a value for a key. More...
 
template<class V >
std::vector< V > getVector (const std::string &key)
 Retrieve a value as a vector for a key. More...
 
template<class V >
std::set< V > getSet (const std::string &key)
 Retrieve a value as a set for a key. More...
 
LUNCHBOX_API bool contains (const std::string &key) const
 
template<>
bool _insert (const std::string &k, const std::string &v, const boost::false_type &)
 

Static Public Member Functions

static LUNCHBOX_API bool handles (const URI &uri)
 

Detailed Description

Unified interface to save key-value pairs in a persistent store.

Example:

/* Copyright (c) 2014 Stefan.Eilemann@epfl.ch
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License version 2.1 as published
* by the Free Software Foundation.
*
* This library 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 library; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <test.h>
#include <lunchbox/persistentMap.h>
#ifdef LUNCHBOX_USE_LEVELDB
# include <leveldb/db.h>
#endif
#include <stdexcept>
const int ints[] = { 17, 53, 42, 65535, 32768 };
const size_t numInts = sizeof( ints ) / sizeof( int );
template< class T > void insertVector( PersistentMap& map )
{
std::vector< T > vector;
for( size_t i = 0; i < numInts; ++i )
vector.push_back( T( ints[ i ] ));
TEST( map.insert( typeid( vector ).name(), vector ));
}
template< class T > void readVector( PersistentMap& map )
{
const std::vector< T >& vector =
map.getVector< T >( typeid( vector ).name( ));
TEST( vector.size() == numInts );
for( size_t i = 0; i < numInts; ++i )
TEST( vector[ i ] == T( ints[i] ));
}
void setup( const std::string& uri )
{
PersistentMap map( uri );
TEST( map.insert( "the quick brown fox", "jumped over something" ));
TESTINFO( map[ "the quick brown fox" ] == "jumped over something",
map[ "the quick brown fox" ] );
TEST( map.insert( "foo", "bar" ));
TESTINFO( map[ "foo" ] == "bar",
map[ "foo" ] << " length " << map[ "foo" ].length( ));
TEST( map[ "bar" ].empty( ));
TEST( map.insert( "hans", std::string( "dampf" )));
TESTINFO( map[ "hans" ] == "dampf", map[ "hans" ] );
insertVector< int >( map );
insertVector< uint16_t >( map );
readVector< int >( map );
readVector< uint16_t >( map );
std::set< int > set( ints, ints + numInts );
TEST( map.insert( "std::set< int >", set ));
}
void read( const std::string& uri )
{
PersistentMap map( uri );
TEST( map[ "foo" ] == "bar" );
TEST( map[ "bar" ].empty( ));
readVector< int >( map );
readVector< uint16_t >( map );
const std::set< int >& set = map.getSet< int >( "std::set< int >" );
TESTINFO( set.size() == numInts, set.size() << " != " << numInts );
for( size_t i = 0; i < numInts; ++i )
TESTINFO( set.find( ints[i] ) != set.end(),
ints[i] << " not found in set" );
}
void testGenericFailures()
{
try
{
setup( "foobar://" );
}
catch( const std::runtime_error& )
{
return;
}
TESTINFO( false, "Missing exception" );
}
void testLevelDBFailures()
{
#ifdef LUNCHBOX_USE_LEVELDB
try
{
setup( "leveldb:///doesnotexist/deadbeef/coffee" );
}
catch( const std::runtime_error& )
{
return;
}
TESTINFO( false, "Missing exception" );
#endif
}
int main( int, char** )
{
try
{
#ifdef LUNCHBOX_USE_LEVELDB
setup( "" );
setup( "leveldb://" );
setup( "leveldb://persistentMap2.leveldb" );
read( "" );
read( "leveldb://" );
read( "leveldb://persistentMap2.leveldb" );
#endif
}
#ifdef LUNCHBOX_USE_LEVELDB
catch( const leveldb::Status& status )
{
TESTINFO( !"exception", status.ToString( ));
}
#endif
catch( const std::runtime_error& error )
{
TESTINFO( !"exception", error.what( ));
}
testGenericFailures();
testLevelDBFailures();
return EXIT_SUCCESS;
}

Definition at line 43 of file persistentMap.h.

Constructor & Destructor Documentation

LUNCHBOX_API lunchbox::PersistentMap::PersistentMap ( const std::string &  uri = std::string())

Construct a new persistent map.

Depending on the URI scheme an implementation backend is chosen. If no URI is given, a default one is selected. Available implementations are:

  • leveldb://path (if LUNCHBOX_USE_LEVELDB is defined)
  • skv://path_to_config#pdsname (if LUNCHBOX_USE_SKV is defined)
Parameters
urithe storage backend and destination.
Exceptions
std::runtime_errorif no suitable implementation is found.
std::runtime_errorif opening the leveldb failed.
Version
1.9.2
LUNCHBOX_API lunchbox::PersistentMap::PersistentMap ( const URI uri)
explicit

Construct a persistent map using an URI.

See other ctor for details.

Version
1.9.2
LUNCHBOX_API lunchbox::PersistentMap::~PersistentMap ( )

Destruct the persistent map.

Version
1.9.2

Member Function Documentation

LUNCHBOX_API bool lunchbox::PersistentMap::contains ( const std::string &  key) const
Returns
true if the key exists.
Version
1.9.2
template<class V >
std::set< V > lunchbox::PersistentMap::getSet ( const std::string &  key)
inline

Retrieve a value as a set for a key.

Parameters
keythe key to retreive.
Returns
the values, or an empty set if the key is not available.
Version
1.9.2

Definition at line 194 of file persistentMap.h.

template<class V >
std::vector< V > lunchbox::PersistentMap::getVector ( const std::string &  key)
inline

Retrieve a value as a vector for a key.

Parameters
keythe key to retreive.
Returns
the values, or an empty vector if the key is not available.
Version
1.9.2

Definition at line 186 of file persistentMap.h.

static LUNCHBOX_API bool lunchbox::PersistentMap::handles ( const URI uri)
static
Returns
true if an implementation for the given URI is available.
Version
1.9.2
template<class V >
bool lunchbox::PersistentMap::insert ( const std::string &  key,
const V &  value 
)
inline

Insert or update a value in the database.

Parameters
keythe key to store the value.
valuethe value stored at the key.
Returns
true on success, false otherwise
Exceptions
std::runtime_errorif the value is not copyable
Version
1.9.2

Definition at line 85 of file persistentMap.h.

Referenced by insert().

+ Here is the caller graph for this function:

template<class V >
bool lunchbox::PersistentMap::insert ( const std::string &  key,
const std::vector< V > &  values 
)
inline

Insert or update a vector of values in the database.

Parameters
keythe key to store the value.
valuethe values stored at the key.
Returns
true on success, false otherwise
Exceptions
std::runtime_errorif the vector values are not copyable
Version
1.9.2

Definition at line 98 of file persistentMap.h.

template<class V >
bool lunchbox::PersistentMap::insert ( const std::string &  key,
const std::set< V > &  values 
)
inline

Insert or update a set of values in the database.

Parameters
keythe key to store the value.
valuethe values stored at the key.
Returns
true on success, false otherwise
Exceptions
std::runtime_errorif the set values are not copyable
Version
1.9.2

Definition at line 111 of file persistentMap.h.

References insert().

+ Here is the call graph for this function:

LUNCHBOX_API std::string lunchbox::PersistentMap::operator[] ( const std::string &  key) const

Retrieve a value for a key.

Parameters
keythe key to retreive.
Returns
the value, or an empty string if the key is not available.
Version
1.9.2

The documentation for this class was generated from the following file: