Commit c6178620 authored by Stanislav Bohm's avatar Stanislav Bohm

RF: shared data operations are const

parent a6cf61f7
......@@ -7,17 +7,17 @@ Data::~Data() {
}
size_t Data::get_length()
size_t Data::get_length() const
{
return 0;
}
std::shared_ptr<Data> Data::get_at_index(size_t index)
std::shared_ptr<Data> Data::get_at_index(size_t index) const
{
assert(0);
}
std::shared_ptr<Data> Data::get_slice(size_t from, size_t to)
std::shared_ptr<Data> Data::get_slice(size_t from, size_t to) const
{
assert(0);
}
......
......@@ -26,22 +26,22 @@ public:
virtual std::string get_type_name() const = 0;
/** Get size of data */
virtual size_t get_size() = 0;
virtual size_t get_size() const = 0;
/** Get length of data (when object is not indexable then returns 0) */
virtual size_t get_length();
virtual size_t get_length() const;
/** Get debugging info string */
virtual std::string get_info() = 0;
virtual std::string get_info() const = 0;
/** Get subobject at given index (0 ... get_length()) */
virtual std::shared_ptr<Data> get_at_index(size_t index);
virtual std::shared_ptr<Data> get_at_index(size_t index) const;
/** Get subobject slice at given indices (0 ... get_length()) */
virtual std::shared_ptr<Data> get_slice(size_t from, size_t to);
virtual std::shared_ptr<Data> get_slice(size_t from, size_t to) const;
/** Serialize object into send buffer */
virtual size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) = 0;
virtual size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const = 0;
/** Get pointer to raw data, returns nullptr when it is not possible */
virtual const char *get_raw_data() const;
......
......@@ -18,21 +18,25 @@ Array::~Array()
logger->debug("Disposing array");
}
size_t Array::get_size()
size_t Array::get_length() const {
return length;
}
size_t Array::get_size() const
{
size_t size = 0;
for (size_t i = 0; i < length; i++) {
size_t size = 0;
for (size_t i = 0; i < length; i++) {
size += items[i]->get_size();
}
return size;
}
std::string Array::get_info()
std::string Array::get_info() const
{
return "Array";
}
std::shared_ptr<Data> Array::get_slice(size_t from, size_t to)
std::shared_ptr<Data> Array::get_slice(size_t from, size_t to) const
{
if (from > length) {
from = length;
......@@ -69,13 +73,13 @@ std::string Array::get_type_name() const
return "loom/array";
}
std::shared_ptr<Data> Array::get_at_index(size_t index)
std::shared_ptr<Data> Array::get_at_index(size_t index) const
{
assert(index < length);
return items[index];
}
size_t Array::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr)
size_t Array::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const
{
auto types = std::make_unique<base::MemItemWithSz>(sizeof(loom::base::Id) * length);
loom::base::Id *ts = reinterpret_cast<loom::base::Id*>(types->get_ptr());
......
......@@ -12,16 +12,13 @@ public:
Array(const DataVector &items);
~Array();
size_t get_length() override {
return length;
}
size_t get_size() override;
std::string get_info() override;
std::shared_ptr<Data> get_at_index(size_t index) override;
std::shared_ptr<Data> get_slice(size_t from, size_t to) override;
size_t get_length() const override;
size_t get_size() const override;
std::string get_info() const override;
std::shared_ptr<Data> get_at_index(size_t index) const override;
std::shared_ptr<Data> get_slice(size_t from, size_t to) const override;
std::string get_type_name() const override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const override;
std::shared_ptr<Data>& get_ref_at_index(size_t index);
......
......@@ -30,12 +30,25 @@ ExternFile::~ExternFile()
}
}
std::string loom::ExternFile::get_info()
size_t ExternFile::get_size() const {
return size;
}
const char *ExternFile::get_raw_data() const
{
return data;
}
bool ExternFile::has_raw_data() const {
return true;
}
std::string loom::ExternFile::get_info() const
{
return "<ExternFile '" + filename + "'>";
}
size_t ExternFile::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr)
size_t ExternFile::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const
{
assert(0); // TODO
}
......
......@@ -13,35 +13,21 @@ public:
ExternFile(const std::string &filename);
~ExternFile();
size_t get_size() override {
return size;
}
const char *get_raw_data() const override
{
return data;
}
bool has_raw_data() const override {
return true;
}
std::string get_info() override;
size_t get_size() const override;
const char *get_raw_data() const override;
bool has_raw_data() const override;
std::string get_info() const override;
std::string get_filename() const override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const override;
protected:
void open();
void map(int fd, bool write);
char *data;
size_t size;
std::string filename;
static size_t file_id_counter;
};
}
......
......@@ -24,22 +24,22 @@ std::string Index::get_type_name() const
return "loom/index";
}
size_t Index::get_length()
size_t Index::get_length() const
{
return length;
}
size_t Index::get_size()
size_t Index::get_size() const
{
return data->get_size() + sizeof(size_t) * (length + 1);
}
std::string Index::get_info()
std::string Index::get_info() const
{
return "Index";
}
std::shared_ptr<Data> Index::get_at_index(size_t index)
std::shared_ptr<Data> Index::get_at_index(size_t index) const
{
assert (index < length);
size_t addr = indices[index];
......@@ -53,7 +53,7 @@ std::shared_ptr<Data> Index::get_at_index(size_t index)
return data;
}
std::shared_ptr<Data> Index::get_slice(size_t from, size_t to)
std::shared_ptr<Data> Index::get_slice(size_t from, size_t to) const
{
if (from > length) {
from = length;
......@@ -81,7 +81,7 @@ std::shared_ptr<Data> Index::get_slice(size_t from, size_t to)
return data;
}
size_t Index::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr)
size_t Index::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const
{
logger->critical("Index::serialize_data");
abort();
......
......@@ -22,12 +22,12 @@ public:
~Index();
std::string get_type_name() const override;
size_t get_length() override;
size_t get_size() override;
std::string get_info() override;
std::shared_ptr<Data> get_at_index(size_t index) override;
std::shared_ptr<Data> get_slice(size_t from, size_t to) override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) override;
size_t get_length() const override;
size_t get_size() const override;
std::string get_info() const override;
std::shared_ptr<Data> get_at_index(size_t index) const override;
std::shared_ptr<Data> get_slice(size_t from, size_t to) const override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const override;
protected:
......
......@@ -17,7 +17,7 @@
using namespace loom;
using namespace loom::base;
size_t RawData::file_id_counter = 1;
std::atomic<size_t> RawData::file_id_counter(1);
RawData::RawData()
: data(nullptr), size(0)
......@@ -45,6 +45,22 @@ std::string RawData::get_type_name() const
return "loom/data";
}
size_t RawData::get_size() const {
return size;
}
const char *RawData::get_raw_data() const
{
if (data == nullptr) {
open();
}
return data;
}
bool RawData::has_raw_data() const {
return true;
}
char* RawData::init_empty(const std::string &work_dir, size_t size)
{
assert(data == nullptr);
......@@ -135,7 +151,7 @@ void RawData::map(int fd, bool write) const
}
}
std::string RawData::get_info()
std::string RawData::get_info() const
{
return "RawData file=" + filename;
}
......@@ -153,7 +169,7 @@ void RawData::init_from_mem(const std::string &work_dir, const void *ptr, size_t
memcpy(mem, ptr, size);
}
size_t RawData::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr)
size_t RawData::serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const
{
buffer.add(std::make_unique<base::SizeBufferItem>(size));
buffer.add(std::make_unique<DataBufferItem>(data_ptr, get_raw_data(), size));
......
......@@ -14,26 +14,14 @@ public:
std::string get_type_name() const override;
size_t get_size() override {
return size;
}
size_t get_size() const override;
const char *get_raw_data() const override
{
if (data == nullptr) {
open();
}
return data;
}
bool has_raw_data() const override {
return true;
}
std::string get_info() override;
const char *get_raw_data() const override;
bool has_raw_data() const override;
std::string get_info() const override;
std::string get_filename() const override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) override;
size_t serialize(Worker &worker, loom::base::SendBuffer &buffer, std::shared_ptr<Data> &data_ptr) const override;
char* init_empty(const std::string &work_dir, size_t size);
void init_from_string(const std::string &work_dir, const std::string &str);
......@@ -51,7 +39,7 @@ protected:
size_t size;
std::string filename;
static size_t file_id_counter;
static std::atomic<size_t> file_id_counter;
};
......
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