18 #ifndef LUNCHBOX_PERSISTENTMAP_H
19 #define LUNCHBOX_PERSISTENTMAP_H
22 #include <lunchbox/bitOperation.h>
23 #include <lunchbox/debug.h>
26 #include <servus/uri.h>
28 #include <boost/foreach.hpp>
29 #include <boost/lexical_cast.hpp>
30 #include <boost/noncopyable.hpp>
31 #include <boost/type_traits.hpp>
41 namespace detail {
class PersistentMap; }
64 LUNCHBOX_API
explicit PersistentMap(
const std::string& uri =std::string());
70 LUNCHBOX_API
explicit PersistentMap(
const servus::URI& uri );
79 LUNCHBOX_API
static bool handles(
const servus::URI& uri );
105 template<
class V >
bool insert(
const std::string& key,
const V& value )
106 {
return _insert( key, value, boost::has_trivial_assign< V >( )); }
118 bool insert(
const std::string& key,
const std::vector< V >& values )
119 {
return _insert( key, values, boost::has_trivial_assign< V >( )); }
131 bool insert(
const std::string& key,
const std::set< V >& values )
132 {
return insert( key, std::vector<V>( values.begin(), values.end( ))); }
141 LUNCHBOX_API std::string
operator [] (
const std::string& key )
const;
150 template<
class V > V
get(
const std::string& key )
const
151 {
return _get< V >( key ); }
160 template<
class V > std::vector< V >
getVector(
const std::string& key )
170 template<
class V > std::set< V >
getSet(
const std::string& key )
const;
182 LUNCHBOX_API
bool fetch(
const std::string& key,
size_t sizeHint = 0 )
const;
185 LUNCHBOX_API
bool contains(
const std::string& key )
const;
188 LUNCHBOX_API
bool flush();
194 detail::PersistentMap*
const _impl;
196 LUNCHBOX_API
bool _insert(
const std::string& key,
const void* data,
198 LUNCHBOX_API
bool _swap()
const;
204 template<
size_t N >
bool
205 _insert(
const std::string& k,
char const (& v)[N],
const boost::true_type&)
207 return _insert( k, (
void*)v, N - 1 );
211 bool _insert(
const std::string& k,
const V& v,
const boost::true_type& )
213 if( boost::is_pointer< V >::value )
214 LBTHROW( std::runtime_error(
"Can't insert pointers" ));
215 return _insert( k, &v,
sizeof( V ));
219 bool _insert(
const std::string&,
const V& v,
const boost::false_type& )
220 {
LBTHROW( std::runtime_error(
"Can't insert non-POD " +
className( v ))); }
223 bool _insert(
const std::string& key,
const std::vector< V >& values,
224 const boost::true_type& )
225 {
return _insert( key, values.data(), values.size() *
sizeof( V )); }
227 template<
class V > V _get(
const std::string& k )
const
229 if( !boost::has_trivial_assign< V >( ))
230 LBTHROW( std::runtime_error(
"Can't retrieve non-POD " +
232 if( boost::is_pointer< V >::value )
233 LBTHROW( std::runtime_error(
"Can't retrieve pointers" ));
235 const std::string& value = (*this)[ k ];
236 if( value.size() !=
sizeof( V ))
237 LBTHROW( std::runtime_error( std::string(
"Wrong value size " ) +
238 boost::lexical_cast< std::string >( value.size( )) +
241 V v( *reinterpret_cast< const V* >( value.data( )));
249 bool PersistentMap::_insert(
const std::string& k,
const std::string& v,
250 const boost::false_type& )
252 return _insert( k, v.data(), v.length( ));
255 template<
class V >
inline
258 const std::string& value = (*this)[ key ];
259 std::vector< V > vector( reinterpret_cast< const V* >( value.data( )),
260 reinterpret_cast< const V* >( value.data() + value.size( )));
261 if( _swap() &&
sizeof( V ) != 1 )
263 BOOST_FOREACH( V& elem, vector )
269 template<
class V >
inline
272 std::string value = (*this)[ key ];
273 V*
const begin =
reinterpret_cast< V*
>(
274 const_cast< char *
>( value.data( )));
275 V*
const end = begin + value.size() /
sizeof( V );
277 if( _swap() &&
sizeof( V ) != 1 )
278 for( V* i = begin; i < end; ++i )
281 return std::set< V >( begin, end );
286 #endif //LUNCHBOX_PERSISTENTMAP_H
std::string operator[](const std::string &key) const
Retrieve a value for a key.
PersistentMap(const std::string &uri=std::string())
Construct a new persistent map.
Defines export visibility macros for library Lunchbox.
Basic type definitions not provided by the operating system.
void byteswap(T &value)
Swap the byte order of the given value.
std::string className(const T *object)
Print the RTTI name of the given class.
Unified interface to save key-value pairs in a persistent store.
bool insert(const std::string &key, const std::set< V > &values)
Insert or update a set of values in the database.
bool fetch(const std::string &key, size_t sizeHint=0) const
Asynchronously retrieve a value which to be read later.
bool insert(const std::string &key, const V &value)
Insert or update a value in the database.
~PersistentMap()
Destruct the persistent map.
bool contains(const std::string &key) const
std::set< V > getSet(const std::string &key) const
Retrieve a value as a set for a key.
size_t setQueueDepth(const size_t depth)
Set the maximum number of asynchronous outstanding write operations.
static bool handles(const servus::URI &uri)
std::vector< V > getVector(const std::string &key) const
Retrieve a value as a vector for a key.
bool flush()
Flush outstanding operations to the backend storage.
Abstraction layer and common utilities for multi-threaded programming.
bool insert(const std::string &key, const std::vector< V > &values)
Insert or update a vector of values in the database.
void setByteswap(const bool swap)
Enable or disable endianness conversion on reads.
This file contains logging classes.
#define LBTHROW(exc)
Log a std::exception if topic LOG_EXCEPTION is set before throwing exception.