Lunchbox  1.9.1
 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 ()
 Destruct the persistent map. More...
 
LUNCHBOX_API bool insert (const std::string &key, const std::string &value)
 Insert or update a value in the database. More...
 
LUNCHBOX_API std::string operator[] (const std::string &key) const
 Retrieve a value for a key. More...
 

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>
void setup( const std::string& uri )
{
PersistentMap map( uri );
TEST( map.insert( "foo", "bar" ));
TEST( map[ "foo" ] == "bar" );
TEST( map[ "bar" ].empty( ));
}
void read( const std::string& uri )
{
PersistentMap map( uri );
TEST( map[ "foo" ] == "bar" );
TEST( map[ "bar" ].empty( ));
}
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 leveldb::Status& status )
{
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 35 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)
Parameters
urithe storage backend and destination.
Exceptions
std::runtime_errorif no suitable implementation is found.
leveldb::Statusif opening the leveldb database failed.
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::insert ( const std::string &  key,
const std::string &  value 
)

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
Version
1.9.2
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: