forked from exclipy/clang_indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClicDb.cpp
71 lines (63 loc) · 2.3 KB
/
ClicDb.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include "ClicDb.hpp"
#include "clic_printer.hpp"
#include "clic_parser.hpp"
#include <boost/foreach.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/algorithm/string.hpp>
#include <sstream>
#include <iterator>
ClicDb::ClicDb(const char* dbFilename) : db(NULL, 0)
{
try {
db.set_error_stream(&std::cerr);
db.open(NULL, dbFilename, NULL, DB_BTREE, DB_CREATE, 0);
} catch(DbException &e) {
std::cerr << "Exception thrown: " << e.what() << std::endl;
exit(1);
}
}
ClicDb::~ClicDb() {
db.close(0);
}
void ClicDb::clear() {
try {
db.truncate(0, 0, 0);
} catch(DbException &e) {
std::cerr << "Exception thrown: " << e.what() << std::endl;
exit(1);
}
}
void ClicDb::set(const std::string& usr, const std::set<std::string>& locations) {
std::stringstream ss;
printLocations(ss, locations);
std::string locationsString = ss.str();
Dbt key(const_cast<char*>(usr.c_str()), usr.size());
Dbt value(const_cast<char*>(locationsString.c_str()), locationsString.size());
db.put(NULL, &key, &value, 0);
}
std::set<std::string> ClicDb::get(const std::string& usr) {
Dbt key(const_cast<char*>(usr.c_str()), usr.size());
Dbt value;
if (db.get(NULL, &key, &value, 0) == DB_NOTFOUND)
return std::set<std::string>();
std::string str((char*)value.get_data(), value.get_size());
std::set<std::string> result;
boost::algorithm::split(result, str, boost::algorithm::is_any_of("\t"));
return result;
}
void ClicDb::addMultiple(const std::string& usr, const std::set<std::string>& locationsToAdd) {
std::set<std::string> storedLocations = get(usr);
const std::size_t originalCount = storedLocations.size();
// stuff that already exist is overwritten => ignored
boost::copy(locationsToAdd, std::inserter(storedLocations, storedLocations.begin()));
if (storedLocations.size() > originalCount)
set(usr, storedLocations);
}
void ClicDb::rmMultiple(const std::string& usr, const std::set<std::string>& locationsToRemove) {
std::set<std::string> storedLocations = get(usr);
int originalCount = storedLocations.size();
BOOST_FOREACH(std::string loc, locationsToRemove)
storedLocations.erase(loc);
if (storedLocations.size() < originalCount)
set(usr, storedLocations);
}