The SwappyItems Key-Values Store
SearchOnHibernateData.cpp

This is a example, how to use all the generated temp and hibernate data by just making the same instances (like in ReadPbfData.cpp). It is a example for each() and apply() too.

#include <cstdio>
#include <utility> // pair
#include <chrono>
#include <inttypes.h>
#include <vector>
#include "SwappyItems.hpp"
using namespace std;
#define FILE_ITEMS ( 7*1024) // 2*1024 // 64*1024
#define FILE_MULTI 6 // 4 // 16
#define RAM_MULTI 9 // 8
#define BBITS 5 // 5
#define BMASK ((BBITS+4)* FILE_ITEMS) // +4
typedef uint64_t Key; // for the key-value tuple, 8 byte
struct NodeData {
uint8_t _used;
double _lon;
double _lat;
};
struct WayData {
uint8_t _type;
char _name[256];
};
struct PlaceData {
uint8_t _type;
double _lon;
double _lat;
char _name[256];
};
// ---------------------------------------------------------------------
std::chrono::time_point<std::chrono::system_clock> start;
double mseconds;
int main(int argc, char** argv) {
Key query = 103535041; // 185769837 // node 280319902
if (argc == 2) query = stol(argv[1]);
start = std::chrono::high_resolution_clock::now();
ways = new SwappyItemsWAYS(23);
// get a way by key -------------------------------------------
// std::pair<WayData, vector<Key> > * val = ways->get(query);
std::pair<NodeData, vector<Key> > * val = nodes->get(query);
if (val == nullptr) {
printf("The '%lu' does not exist.\n", query);
} else {
//printf("Name of '%lu': %s\n", query, val->first._name);
//printf("Name of '%lu': %d\n", query, val->first._used);
nodes->apply(nn, query, [](SwappyItemsNODES::Data * dat) {
// we can do anything to "dat", which is the data to key "r/2".
// the data is also written as copy via reference to "nn"
dat->first._used = 255;
});
printf("Name of '%lu': %d\n", query, nn.first._used);
}
// ---------------------------------------------------------
Key k2;
bool here = places->each(p, [&k2](Key key, SwappyItemsPLACES::Data & val) {
printf("each %d gets '%lu' %s\n", val.first._type, key, val.first._name);
k2 = key; // an other kind of "returning" a value
//val.first._type++; // change data works!!
// stop on true
return val.first._name[1] == 'o'; // the second letter must be a "o"
});
if (here) printf("found: '%lu' %s\n", k2, p.first._name);
ways->each(w, [](Key key, SwappyItemsWAYS::Data & val) {
if (val.first._name[0] == 'A') {
printf("'%lu' with type %d has name %s\n", key, val.first._type, val.first._name);
for (auto k : val.second) {
printf(" ref '%lu'\n", k);
}
}
// all items, no stop
return false;
});
nodes->each(n, [&w](Key nkey, SwappyItemsNODES::Data & val) {
if (val.first._used > 2) {
printf("%lu\n", nkey);
// Test node-each in a way-each
ways->each(w, [nkey](Key eKey, SwappyItemsWAYS::Data & eVal) {
// search the node-key in refs of all ways
for(Key r : eVal.second) {
// stop if found
if (r == nkey) {
printf(" in way %s\n", eVal.first._name);
return true;
}
SwappyItemsNODES::Data nn;
// test a "special get", that does not confuse prio-queue and ram!
// we try to get a "random" key = r/2
bool rHalfExist = nodes->apply(nn, r/2, [](SwappyItemsNODES::Data * dat) {
// we can do anything to "dat", which is the data to key "r/2".
// the data is also written as copy via reference to "nn"
dat->first._lon = 99999;
});
if(rHalfExist) {
// we print a dot, if the half value of a ref key from a way exists in the nodes!
printf("'%lu' nn %lf\n", r, nn.first._lon);
}
}
return false;
});
}
// all items, no stop
return false;
});
delete places;
delete ways;
delete nodes;
auto now = std::chrono::high_resolution_clock::now();
mseconds = std::chrono::duration<double, std::milli>(now-start).count();
printf("# end: %.f ms\n", mseconds);
return 0;
}
uint64_t Key
Definition: SearchOnHibernateData.cpp:17
SwappyItemsPLACES * places
Definition: SearchOnHibernateData.cpp:42
int main(int argc, char **argv)
Definition: SearchOnHibernateData.cpp:54
double mseconds
Definition: SearchOnHibernateData.cpp:47
SwappyItemsNODES * nodes
Definition: SearchOnHibernateData.cpp:41
SwappyItems< Key, WayData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > SwappyItemsWAYS
Definition: SearchOnHibernateData.cpp:37
SwappyItems< Key, NodeData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > SwappyItemsNODES
Definition: SearchOnHibernateData.cpp:38
SwappyItems< Key, PlaceData, FILE_ITEMS, FILE_MULTI, RAM_MULTI, BBITS, BMASK > SwappyItemsPLACES
Definition: SearchOnHibernateData.cpp:39
std::chrono::time_point< std::chrono::system_clock > start
Definition: SearchOnHibernateData.cpp:46
SwappyItemsWAYS * ways
Definition: SearchOnHibernateData.cpp:40
Definition: SwappyItems.hpp:49
std::pair< TVALUE, std::vector< TKEY > > Data
Definition: SwappyItems.hpp:52
bool apply(Data &back, const TKEY &key, std::function< void(Data *)> foo)
Definition: SwappyItems.hpp:879
Data * get(const TKEY &key)
Definition: SwappyItems.hpp:188
bool each(Data &back, std::function< bool(TKEY, Data &)> foo)
Definition: SwappyItems.hpp:790
uint64_t Key
Definition: osm2graph.cpp:48
Definition: osm2graph.cpp:53
Definition: ReadPbfData.cpp:44
Definition: osm2graph.cpp:60