Commit 18f93fd1 authored by Matthieu Dorier's avatar Matthieu Dorier

simplified conversion of numbers to string keys

parent c691fddc
......@@ -179,7 +179,7 @@ Run DataSet::createRun(const RunNumber& runNumber) {
throw Exception("Trying to create a Run with InvalidRunNumber");
}
std::string parent = fullname();
std::string runStr = Run::Impl::makeKeyStringFromRunNumber(runNumber);
std::string runStr = makeKeyStringFromNumber(runNumber);
m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, runStr);
return Run(m_impl->m_datastore, m_impl->m_level+1,
std::make_shared<std::string>(parent), runNumber);
......@@ -190,7 +190,7 @@ Run DataSet::createRun(WriteBatch& batch, const RunNumber& runNumber) {
throw Exception("Trying to create a Run with InvalidRunNumber");
}
std::string parent = fullname();
std::string runStr = Run::Impl::makeKeyStringFromRunNumber(runNumber);
std::string runStr = makeKeyStringFromNumber(runNumber);
batch.m_impl->store(m_impl->m_level+1, parent, runStr);
return Run(m_impl->m_datastore, m_impl->m_level+1,
std::make_shared<std::string>(parent), runNumber);
......
......@@ -51,7 +51,7 @@ Event Event::next() const {
if(s == 0) return Event();
size_t i = m_impl->m_container->size()+1;
if(keys[0].size() <= i) return Event();
EventNumber n = Impl::parseEventNumberFromKeyString(&keys[0][i]);
EventNumber n = parseNumberFromKeyString<EventNumber>(&keys[0][i]);
if(n == InvalidEventNumber) return Event();
return Event(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, n);
}
......
......@@ -11,6 +11,7 @@
#include <iomanip>
#include <memory>
#include "hepnos/Event.hpp"
#include "NumberUtil.hpp"
namespace hepnos {
......@@ -29,52 +30,8 @@ class Event::Impl {
, m_container(container)
, m_event_nr(n) {}
static std::string makeKeyStringFromEventNumber(const EventNumber& n) {
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#ifndef HEPNOS_READABLE_NUMBERS
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#else
std::stringstream strstr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << n;
return strstr.str();
#endif
}
static EventNumber parseEventNumberFromKeyString(const char* str) {
if(str[0] != '%') return InvalidEventNumber;
EventNumber n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strEventNumber;
strEventNumber << std::hex << std::string(str+1, 16);
strEventNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
std::string makeKeyStringFromEventNumber() const {
return makeKeyStringFromEventNumber(m_event_nr);
return makeKeyStringFromNumber(m_event_nr);
}
std::string fullpath() const {
......
/*
* (C) 2018 The University of Chicago
*
* See COPYRIGHT in top-level directory.
*/
#ifndef __HEPNOS_PRIVATE_NUMBER_UTIL_H
#define __HEPNOS_PRIVATE_NUMBER_UTIL_H
#include <sstream>
#include <memory>
#include <iomanip>
namespace hepnos {
template<typename T>
static std::string makeReadableKeyStringFromNumber(const T& n) {
constexpr int s = sizeof(T)*2;
std::stringstream strstr;
strstr << '%' << std::setfill('0') << std::setw(s) << std::hex << n;
return strstr.str();
}
template<typename T>
static std::string makeKeyStringFromNumber(const T& n) {
#ifdef HEPNOS_READABLE_NUMBERS
return makeReadableKeyStringFromNumber(n);
#else
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#endif
}
template<typename T>
static T parseNumberFromKeyString(const char* str) {
// string is assumed to start with a '%'
T n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strNumber;
strNumber << std::hex << std::string(str+1, sizeof(T)*2);
strNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
}
#endif
......@@ -11,6 +11,8 @@
namespace hepnos {
Run::iterator Run::Impl::m_end;
Run::Run()
: m_impl(std::make_unique<Run::Impl>(nullptr, 0, std::make_shared<std::string>(""), InvalidRunNumber)) {}
......@@ -48,14 +50,15 @@ Run Run::next() const {
std::vector<std::string> keys;
size_t s = m_impl->m_datastore->m_impl->nextKeys(
m_impl->m_level, *m_impl->m_container,
m_impl->m_level, *m_impl->m_dataset_name,
m_impl->makeKeyStringFromRunNumber(), keys, 1);
if(s == 0) return Run();
size_t i = m_impl->m_container->size()+1;
size_t i = m_impl->m_dataset_name->size()+1;
if(keys[0].size() <= i) return Run();
RunNumber rn = Impl::parseRunNumberFromKeyString(&keys[0][i]);
RunNumber rn = parseNumberFromKeyString<RunNumber>(&keys[0][i]);
if(rn == InvalidRunNumber) return Run();
return Run(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, rn);
return Run(m_impl->m_datastore, m_impl->m_level,
m_impl->m_dataset_name, rn);
}
bool Run::valid() const {
......@@ -101,10 +104,11 @@ bool Run::operator==(const Run& other) const {
if(!v1 && !v2) return true;
if(v1 && !v2) return false;
if(!v1 && v2) return false;
return m_impl->m_datastore == other.m_impl->m_datastore
&& m_impl->m_level == other.m_impl->m_level
&& *m_impl->m_container == *other.m_impl->m_container
&& m_impl->m_run_nr == other.m_impl->m_run_nr;
return m_impl->m_datastore == other.m_impl->m_datastore
&& m_impl->m_level == other.m_impl->m_level
&& (m_impl->m_dataset_name == other.m_impl->m_dataset_name
|| *m_impl->m_dataset_name == *other.m_impl->m_dataset_name)
&& m_impl->m_run_number == other.m_impl->m_run_number;
}
bool Run::operator!=(const Run& other) const {
......@@ -115,14 +119,14 @@ const RunNumber& Run::number() const {
if(!valid()) {
throw Exception("Calling Run member function on an invalid Run object");
}
return m_impl->m_run_nr;
return m_impl->m_run_number;
}
const std::string& Run::container() const {
if(!valid()) {
throw Exception("Calling Run member function on an invalid Run object");
}
return *m_impl->m_container;
return *m_impl->m_dataset_name;
}
SubRun Run::createSubRun(const SubRunNumber& subRunNumber) {
......@@ -130,7 +134,7 @@ SubRun Run::createSubRun(const SubRunNumber& subRunNumber) {
throw Exception("Calling Run member function on an invalid Run object");
}
std::string parent = m_impl->fullpath();
std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
std::string subRunStr = makeKeyStringFromNumber(subRunNumber);
m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, subRunStr);
return SubRun(m_impl->m_datastore, m_impl->m_level+1,
std::make_shared<std::string>(parent), subRunNumber);
......@@ -141,7 +145,7 @@ SubRun Run::createSubRun(WriteBatch& batch, const SubRunNumber& subRunNumber) {
throw Exception("Calling Run member function on an invalid Run object");
}
std::string parent = m_impl->fullpath();
std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
std::string subRunStr = makeKeyStringFromNumber(subRunNumber);
batch.m_impl->store(m_impl->m_level+1, parent, subRunStr);
return SubRun(m_impl->m_datastore, m_impl->m_level+1,
std::make_shared<std::string>(parent), subRunNumber);
......@@ -160,7 +164,7 @@ Run::iterator Run::find(const SubRunNumber& subRunNumber) {
}
int ret;
std::string parent = m_impl->fullpath();
std::string subRunStr = SubRun::Impl::makeKeyStringFromSubRunNumber(subRunNumber);
std::string subRunStr = makeKeyStringFromNumber(subRunNumber);
bool b = m_impl->m_datastore->m_impl->exists(m_impl->m_level+1, parent, subRunStr);
if(!b) {
return m_impl->m_end;
......
......@@ -10,6 +10,7 @@
#include <iomanip>
#include <memory>
#include "hepnos/Run.hpp"
#include "NumberUtil.hpp"
namespace hepnos {
......@@ -17,68 +18,27 @@ class Run::Impl {
public:
DataStore* m_datastore;
uint8_t m_level;
std::shared_ptr<std::string> m_container;
RunNumber m_run_nr;
iterator m_end;
DataStore* m_datastore;
uint8_t m_level;
std::shared_ptr<std::string> m_dataset_name;
RunNumber m_run_number;
Impl(DataStore* ds, uint8_t level, const std::shared_ptr<std::string>& container, const RunNumber& rn)
static iterator m_end;
Impl(DataStore* ds, uint8_t level,
const std::shared_ptr<std::string>& dataset,
const RunNumber& rn)
: m_datastore(ds)
, m_level(level)
, m_container(container)
, m_run_nr(rn) {}
static std::string makeKeyStringFromRunNumber(const RunNumber& n) {
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#ifndef HEPNOS_READABLE_NUMBERS
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#else
std::stringstream strstr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << n;
return strstr.str();
#endif
}
static RunNumber parseRunNumberFromKeyString(const char* str) {
if(str[0] != '%') return InvalidRunNumber;
RunNumber n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strRunNumber;
strRunNumber << std::hex << std::string(str+1, 16);
strRunNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
, m_dataset_name(dataset)
, m_run_number(rn) {}
std::string makeKeyStringFromRunNumber() const {
return makeKeyStringFromRunNumber(m_run_nr);
return makeKeyStringFromNumber(m_run_number);
}
std::string fullpath() const {
return *m_container + std::string("/") + makeKeyStringFromRunNumber(m_run_nr);
return *m_dataset_name + std::string("/") + makeKeyStringFromRunNumber();
}
};
......
......@@ -35,7 +35,7 @@ Run RunSet::operator[](const RunNumber& runNumber) {
RunSet::iterator RunSet::find(const RunNumber& runNumber) {
int ret;
std::string parent = m_impl->m_dataset->fullname();
std::string strNum = Run::Impl::makeKeyStringFromRunNumber(runNumber);
std::string strNum = makeKeyStringFromNumber(runNumber);
auto datastore = m_impl->m_dataset->m_impl->m_datastore;
auto level = m_impl->m_dataset->m_impl->m_level;
bool b = datastore->m_impl->exists(level+1, parent, strNum);
......
......@@ -56,7 +56,7 @@ SubRun SubRun::next() const {
if(s == 0) return SubRun();
size_t i = m_impl->m_container->size()+1;
if(keys[0].size() <= i) return SubRun();
SubRunNumber rn = Impl::parseSubRunNumberFromKeyString(&keys[0][i]);
SubRunNumber rn = parseNumberFromKeyString<SubRunNumber>(&keys[0][i]);
if(rn == InvalidSubRunNumber) return SubRun();
return SubRun(m_impl->m_datastore, m_impl->m_level, m_impl->m_container, rn);
}
......@@ -122,7 +122,7 @@ Event SubRun::createEvent(const EventNumber& eventNumber) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
std::string parent = m_impl->fullpath();
std::string eventStr = Event::Impl::makeKeyStringFromEventNumber(eventNumber);
std::string eventStr = makeKeyStringFromNumber(eventNumber);
m_impl->m_datastore->m_impl->store(m_impl->m_level+1, parent, eventStr);
return Event(m_impl->m_datastore, m_impl->m_level+1,
std::make_shared<std::string>(parent), eventNumber);
......@@ -133,7 +133,7 @@ Event SubRun::createEvent(WriteBatch& batch, const EventNumber& eventNumber) {
throw Exception("Calling SubRun member function on invalid SubRun object");
}
std::string parent = m_impl->fullpath();
std::string eventStr = Event::Impl::makeKeyStringFromEventNumber(eventNumber);
std::string eventStr = makeKeyStringFromNumber(eventNumber);
batch.m_impl->store(m_impl->m_level+1, parent, eventStr);
return Event(m_impl->m_datastore, m_impl->m_level+1,
std::make_shared<std::string>(parent), eventNumber);
......@@ -152,7 +152,7 @@ SubRun::iterator SubRun::find(const EventNumber& eventNumber) {
}
int ret;
std::string parent = m_impl->fullpath();
std::string eventStr = Event::Impl::makeKeyStringFromEventNumber(eventNumber);
std::string eventStr = makeKeyStringFromNumber(eventNumber);
bool b = m_impl->m_datastore->m_impl->exists(m_impl->m_level+1, parent, eventStr);
if(!b) {
return m_impl->m_end;
......
......@@ -10,6 +10,7 @@
#include <memory>
#include <iomanip>
#include "hepnos/SubRun.hpp"
#include "NumberUtil.hpp"
namespace hepnos {
......@@ -29,52 +30,8 @@ class SubRun::Impl {
, m_container(container)
, m_subrun_nr(rn) {}
static std::string makeKeyStringFromSubRunNumber(const SubRunNumber& n) {
std::string str(1+sizeof(n),'\0');
str[0] = '%';
#ifndef HEPNOS_READABLE_NUMBERS
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&str[1], &n, sizeof(n));
return str;
#else
unsigned i = sizeof(n);
auto n2 = n;
while(n2 != 0) {
str[i] = n2 & 0xff;
n2 = n2 >> 8;
i -= 1;
}
return str;
#endif
#else
std::stringstream strstr;
strstr << "%" << std::setfill('0') << std::setw(16) << std::hex << n;
return strstr.str();
#endif
}
static SubRunNumber parseSubRunNumberFromKeyString(const char* str) {
if(str[0] != '%') return InvalidSubRunNumber;
SubRunNumber n;
#ifdef HEPNOS_READABLE_NUMBERS
std::stringstream strSubRunNumber;
strSubRunNumber << std::hex << std::string(str+1, 16);
strSubRunNumber >> n;
#else
#if BOOST_ENDIAN_BIG_BYTE
std::memcpy(&n, &str[1], sizeof(n));
#else
n = 0;
for(unsigned i=0; i < sizeof(n); i++) {
n = 256*n + str[i+1];
}
#endif
#endif
return n;
}
std::string makeKeyStringFromSubRunNumber() const {
return makeKeyStringFromSubRunNumber(m_subrun_nr);
return makeKeyStringFromNumber(m_subrun_nr);
}
std::string fullpath() const {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment