Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

65 lines (64 sloc) 1.891 kB
#pragma once
#include <kcpolydb.h>
#include <iostream>
#include <string>
#include "yadfs_exception.h"
#include "std_metadata.h"
#include "sha256.h"
class DataStore {
public:
DataStore(const std::string& path) {
std::cerr << path << "\n";
if (!mDB.open(path)) {
std::string error_message("Can't open datastore ");
error_message += path + ": ";
error_message += mDB.error().name();
throw yadfs_exception(error_message);
}
}
~DataStore() {
mDB.close();
}
bool getContentForObject(const std::string& path, std::string& content) {
return get(keyForContent(path), content);
}
bool getMetadataForObject(const std::string& path, const std::string& key, std::string& value) {
return get(keyForMetadata(path, key), value);
}
bool setContentForObject(const std::string& path, const std::string& content) {
bool success=true;
if (!content.empty()) {
std::string sha256(compute_sha256((const uint8_t*) &content[0], content.size()));
success=setMetadataForObject(path, YADFS_META_SHA256, sha256);
}
return success && set(keyForContent(path), content);
}
bool setMetadataForObject(const std::string& path, const std::string& key, std::string& value) {
return set(keyForMetadata(path, key), value);
}
private:
std::string keyForContent(const std::string& path) {
std::string prefix("content:");
return prefix+path;
}
std::string keyForMetadata(const std::string& path, const std::string& metadata) {
std::string prefix("meta:");
return prefix + metadata + ":"+path;
}
bool get(const std::string& key, std::string& value) {
value.clear();
if (!mDB.get(key, &value)) {
value.clear();
return false;
}
return true;
}
bool set(const std::string& key, const std::string& value) {
return mDB.set(key, value);
}
private:
kyotocabinet::PolyDB mDB;
private:
DataStore(const DataStore&);
DataStore& operator=(const DataStore&);
};
Jump to Line
Something went wrong with that request. Please try again.