1
0
Fork 0

Store intestinals into cpp file. Write to db ok.

This commit is contained in:
Kaveh Vahedipour 2016-03-09 12:29:01 +01:00
parent 3a239fe1d5
commit 3d066d2f03
4 changed files with 60 additions and 27 deletions

View File

@ -212,6 +212,8 @@ write_ret_t Agent::write (query_t const& query) {
if (_constituent.leading()) { // Leading
MUTEX_LOCKER(mutexLocker, _confirmedLock);
std::vector<bool> applied = _spear_head.apply(query); // Apply to spearhead
std::cout << _spear_head("/") <<std::endl;
std::vector<index_t> indices =
_state.log (query, applied, term(), id()); // Append to log w/ indicies
@ -230,8 +232,8 @@ write_ret_t Agent::write (query_t const& query) {
read_ret_t Agent::read (query_t const& query) const {
if (_constituent.leading()) { // We are leading
_read_db.read (query);
return read_ret_t(true,_constituent.leaderID());//(query); //TODO:
auto result = _read_db.read (query);
return read_ret_t(true,_constituent.leaderID(),result);//(query); //TODO:
} else { // We redirect
return read_ret_t(false,_constituent.leaderID());
}

View File

@ -27,22 +27,26 @@
#include <velocypack/Iterator.h>
#include <velocypack/velocypack-aliases.h>
#include <iostream>
using namespace arangodb::consensus;
struct NotEmpty {
bool operator()(const std::string& s) { return !s.empty(); }
};
static inline std::vector<std::string> split (
std::string str, const std::string& dlm) {
std::vector<std::string> sv;
size_t start = (str.find('/') == 0) ? 1:0, end = 0;
while (end != std::string::npos) {
end = str.find (dlm, start);
sv.push_back(str.substr(start, (end == std::string::npos) ?
std::string::npos : end - start));
start = ((end > (std::string::npos - dlm.size())) ?
std::string::npos : end + dlm.size());
}
return sv;
std::vector<std::string> split(const std::string& value, char separator) {
std::vector<std::string> result;
std::string::size_type p = (value.find(separator) == 0) ? 1:0;
std::string::size_type q;
while ((q = value.find(separator, p)) != std::string::npos) {
result.emplace_back(value, p, q - p);
p = q + 1;
}
result.emplace_back(value, p);
result.erase(std::find_if(result.rbegin(), result.rend(),
NotEmpty()).base(), result.end());
return result;
}
Node::Node (std::string const& name) : _parent(nullptr), _name(name) {}
@ -63,7 +67,7 @@ Node& Node::operator= (Node const& node) { // Assign node
return *this;
}
inline NodeType Node::type() const {return _children.size() ? NODE : LEAF;}
NodeType Node::type() const {return _children.size() ? NODE : LEAF;}
Node& Node::operator [](std::string name) {
return *_children[name];
@ -78,9 +82,9 @@ bool Node::append (std::string const name, std::shared_ptr<Node> const node) {
_children[name]->_parent = this;
return true;
}
#include <iostream>
Node& Node::operator ()(std::vector<std::string>& pv) {
std::cout << "const" << pv << pv.size() << std::endl;
if (pv.size()) {
auto found = _children.find(pv[0]);
if (found == _children.end()) {
@ -96,6 +100,7 @@ Node& Node::operator ()(std::vector<std::string>& pv) {
}
Node const& Node::operator ()(std::vector<std::string>& pv) const {
std::cout << "non const" << std::endl;
if (pv.size()) {
auto found = _children.find(pv[0]);
if (found == _children.end()) {
@ -109,22 +114,22 @@ Node const& Node::operator ()(std::vector<std::string>& pv) const {
}
Node const& Node::operator ()(std::string const& path) const {
PathType pv = split(path,"/");
PathType pv = split(path,'/');
return this->operator()(pv);
}
Node& Node::operator ()(std::string const& path) {
PathType pv = split(path,"/");
PathType pv = split(path,'/');
return this->operator()(pv);
}
Node const& Node::read (std::string const& path) const {
PathType pv = split(path,"/");
PathType pv = split(path,'/');
return this->operator()(pv);
}
Node& Node::write (std::string const& path) {
PathType pv = split(path,"/");
PathType pv = split(path,'/');
return this->operator()(pv);
}
@ -157,6 +162,7 @@ bool Node::apply (arangodb::velocypack::Slice const& slice) {
if (slice.type() == ValueType::Object) {
for (auto const& i : VPackObjectIterator(slice)) {
std::string key = i.key.toString();
key = key.substr(1,key.length()-2);
auto found = _children.find(key);
if (found == _children.end()) {
_children[key] = std::make_shared<Node>(key);
@ -171,15 +177,38 @@ bool Node::apply (arangodb::velocypack::Slice const& slice) {
return true;
}
bool Node::check (arangodb::velocypack::Slice const& slice) {
bool Node::check (arangodb::velocypack::Slice const& slice) const{
return true;
}
query_t Node::read (query_t const& query) const {
MUTEX_LOCKER(storeLocker, _storeLock);
query_t result = std::make_shared<arangodb::velocypack::Builder>();
result->add(VPackValue(VPackValueType::Object));
for (auto const& i : VPackArrayIterator(query->slice())) {
read (i, *result);
}
// TODO: Run through JSON and asseble result
query_t ret = std::make_shared<arangodb::velocypack::Builder>();
return ret;
result->close();
return result;
}
bool Node::read (arangodb::velocypack::Slice const& slice,
Builder& ret) const {
LOG(WARN)<<slice;
if (slice.type() == ValueType::Object) {
for (auto const& i : VPackObjectIterator(slice)) {
std::string key = i.key.toString();
auto found = _children.find(key);
if (found == _children.end()) {
return false;
}
found->second->read(i.value, ret);
}
} else {
ret.add(slice);
}
return true;
}

View File

@ -117,11 +117,13 @@ public:
protected:
Node* _parent;
bool apply (arangodb::velocypack::Slice const&);
private:
bool check (arangodb::velocypack::Slice const&);
bool apply (arangodb::velocypack::Slice const&);
bool read (arangodb::velocypack::Slice const&,
arangodb::velocypack::Builder&) const;
bool check (arangodb::velocypack::Slice const&) const;
typedef Buffer<uint8_t> value_t;

View File

@ -85,7 +85,7 @@ inline HttpHandler::status_t RestAgencyHandler::handleWrite () {
body.add(VPackValue(VPackValueType::Object));
_agent->waitFor (ret.indices.back()); // Wait for confirmation (last entry is enough)
for (size_t i = 0; i < ret.indices.size(); ++i) {
body.add(std::to_string(ret.applied[i]), Value(ret.indices[i]));
body.add(std::to_string(i), Value(ret.indices[i]));
}
body.close();
generateResult(body.slice());
@ -100,7 +100,7 @@ inline HttpHandler::status_t RestAgencyHandler::handleWrite () {
inline HttpHandler::status_t RestAgencyHandler::handleRead () {
arangodb::velocypack::Options options;
if (_request->requestType() != HttpRequest::HTTP_REQUEST_POST) {
if (_request->requestType() == HttpRequest::HTTP_REQUEST_POST) {
read_ret_t ret = _agent->read (_request->toVelocyPack(&options));
if (ret.accepted) {
generateResult(ret.result->slice());