Line data Source code
1 :
2 : /* Copyright (c) 2014-2015, Stefan.Eilemann@epfl.ch
3 : *
4 : * This library is free software; you can redistribute it and/or modify it under
5 : * the terms of the GNU Lesser General Public License version 2.1 as published
6 : * by the Free Software Foundation.
7 : *
8 : * This library is distributed in the hope that it will be useful, but WITHOUT
9 : * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 : * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
11 : * details.
12 : *
13 : * You should have received a copy of the GNU Lesser General Public License
14 : * along with this library; if not, write to the Free Software Foundation, Inc.,
15 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 : */
17 :
18 : #include "persistentMap.h"
19 : #include <servus/uri.h>
20 :
21 : // #define HISTOGRAM
22 :
23 : namespace lunchbox
24 : {
25 : namespace detail
26 : {
27 : class PersistentMap
28 : {
29 : public:
30 7 : PersistentMap() : swap( false ) {}
31 7 : virtual ~PersistentMap() {}
32 0 : virtual size_t setQueueDepth( const size_t ) { return 0; }
33 : virtual bool insert( const std::string& key, const void* data,
34 : const size_t size ) = 0;
35 : virtual std::string operator [] ( const std::string& key ) const = 0;
36 6 : virtual bool fetch( const std::string&, const size_t ) const
37 6 : { return true; }
38 : virtual bool contains( const std::string& key ) const = 0;
39 : virtual bool flush() = 0;
40 :
41 : bool swap;
42 : #ifdef HISTOGRAM
43 : std::map< size_t, size_t > keys;
44 : std::map< size_t, size_t > values;
45 : #endif
46 : };
47 : }
48 : }
49 :
50 : // Impls - need detail::PersistentMap interface above
51 : #include "leveldb/persistentMap.h"
52 : #include "skv/persistentMap.h"
53 :
54 : namespace
55 : {
56 8 : lunchbox::detail::PersistentMap* _newImpl( const servus::URI& uri )
57 : {
58 : // Update handles() below on any change here!
59 : #ifdef LUNCHBOX_USE_LEVELDB
60 8 : if( lunchbox::leveldb::PersistentMap::handles( uri ))
61 6 : return new lunchbox::leveldb::PersistentMap( uri );
62 : #endif
63 : #ifdef LUNCHBOX_USE_SKV
64 : if( lunchbox::skv::PersistentMap::handles( uri ))
65 : return new lunchbox::skv::PersistentMap( uri );
66 : #endif
67 :
68 3 : if( !uri.getScheme().empty( ))
69 1 : LBTHROW( std::runtime_error(
70 : std::string( "No suitable implementation found for: " ) +
71 : boost::lexical_cast< std::string >( uri )));
72 :
73 : #ifdef LUNCHBOX_USE_LEVELDB
74 2 : return new lunchbox::leveldb::PersistentMap( uri );
75 : #endif
76 : LBTHROW( std::runtime_error(
77 : std::string( "No suitable implementation found for: " ) +
78 : boost::lexical_cast< std::string >( uri )));
79 : }
80 : }
81 :
82 : namespace lunchbox
83 : {
84 8 : PersistentMap::PersistentMap( const std::string& uri )
85 10 : : _impl( _newImpl( servus::URI( uri )))
86 6 : {}
87 :
88 0 : PersistentMap::PersistentMap( const servus::URI& uri )
89 0 : : _impl( _newImpl( uri ))
90 0 : {}
91 :
92 6 : PersistentMap::~PersistentMap()
93 : {
94 : #ifdef HISTOGRAM
95 : std::cout << std::endl << "keys" << std::endl;
96 : for( std::pair< size_t, size_t > i : _impl->keys )
97 : std::cout << i.first << ", " << i.second << std::endl;
98 : std::cout << std::endl << "values" << std::endl;
99 : for( std::pair< size_t, size_t > i : _impl->values )
100 : std::cout << i.first << ", " << i.second << std::endl;
101 : #endif
102 6 : delete _impl;
103 6 : }
104 :
105 0 : bool PersistentMap::handles( const servus::URI& uri )
106 : {
107 : #ifdef LUNCHBOX_USE_LEVELDB
108 0 : if( lunchbox::leveldb::PersistentMap::handles( uri ))
109 0 : return true;
110 : #endif
111 : #ifdef LUNCHBOX_USE_SKV
112 : if( lunchbox::skv::PersistentMap::handles( uri ))
113 : return true;
114 : #endif
115 :
116 0 : if( !uri.getScheme().empty( ))
117 0 : return false;
118 :
119 : #ifdef LUNCHBOX_USE_LEVELDB
120 0 : return true;
121 : #endif
122 : return false;
123 : }
124 :
125 0 : size_t PersistentMap::setQueueDepth( const size_t depth )
126 : {
127 0 : return _impl->setQueueDepth( depth );
128 : }
129 :
130 36 : bool PersistentMap::_insert( const std::string& key, const void* data,
131 : const size_t size )
132 : {
133 : #ifdef HISTOGRAM
134 : ++_impl->keys[ key.size() ];
135 : ++_impl->values[ size ];
136 : #endif
137 36 : return _impl->insert( key, data, size );
138 : }
139 :
140 84 : std::string PersistentMap::operator [] ( const std::string& key ) const
141 : {
142 84 : return (*_impl)[ key ];
143 : }
144 :
145 6 : bool PersistentMap::fetch( const std::string& key, const size_t sizeHint ) const
146 : {
147 6 : return _impl->fetch( key, sizeHint );
148 : }
149 :
150 3 : bool PersistentMap::contains( const std::string& key ) const
151 : {
152 3 : return _impl->contains( key );
153 : }
154 :
155 0 : bool PersistentMap::flush()
156 : {
157 0 : return _impl->flush();
158 : }
159 :
160 6 : void PersistentMap::setByteswap( const bool swap )
161 : {
162 6 : _impl->swap = swap;
163 6 : }
164 :
165 60 : bool PersistentMap::_swap() const
166 : {
167 60 : return _impl->swap;
168 : }
169 :
170 81 : }
|