diff options
Diffstat (limited to 'server/modules')
-rw-r--r-- | server/modules/metadata/flat_files/flat_files.cbp | 1 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/main.cpp | 100 | ||||
-rw-r--r-- | server/modules/metadata/flat_files/main.h | 1 |
3 files changed, 97 insertions, 5 deletions
diff --git a/server/modules/metadata/flat_files/flat_files.cbp b/server/modules/metadata/flat_files/flat_files.cbp index 3081897..038dcf0 100644 --- a/server/modules/metadata/flat_files/flat_files.cbp +++ b/server/modules/metadata/flat_files/flat_files.cbp @@ -40,6 +40,7 @@ <Linker> <Add library="boost_filesystem" /> <Add library="boost_system" /> + <Add library="boost_serialization" /> </Linker> <Unit filename="main.cpp" /> <Unit filename="main.h" /> diff --git a/server/modules/metadata/flat_files/main.cpp b/server/modules/metadata/flat_files/main.cpp index baaa933..f1583c5 100644 --- a/server/modules/metadata/flat_files/main.cpp +++ b/server/modules/metadata/flat_files/main.cpp @@ -22,12 +22,52 @@ #include "main.h" #include <iostream> +#include <fstream> #include <boost/filesystem.hpp> #include <boost/bind.hpp> +#include <boost/archive/binary_oarchive.hpp> +#include <boost/archive/binary_iarchive.hpp> +#include <boost/serialization/vector.hpp> + +std::string replace_home_var(const std::string &path) +{ + std::string home = getenv("HOME"), tmp_path = path; + for(std::string::size_type p1 = tmp_path.find("~"); p1 != std::string::npos; p1 = tmp_path.find("~", p1)) + tmp_path.replace(p1, 1, home); + return tmp_path; + +} + + void storage_impl::on_modules_loaded() { - //settings = api->get_module_settings(this); //you can get module settings here, by default settings loaded automatically after this function call + //just checking if we working fine via core + //test set +/* { + std::string d = "some string\ndfjoghdfyigehygerityertoweuyrtghyuewitgweyuitgweiurtgeiutrerytiewgytyewuirtgewrgyutygergytiu"; + std::vector<char> data; + for(auto i = d.begin(), end = d.end(); i != end; ++i) + { + data.push_back(*i); + } + api->metadata_set(this, "test_setting", data); + } */ + //test get +/* { + std::vector<char> data; + api->metadata_get(this, "test_setting", data); + std::string s; + for(auto i = data.begin(), end = data.end(); i != end; ++i) + { + s.push_back(*i); + } + std::cout<<"we have read:\n"<<s<<std::endl; + } */ + //test remove +/* { + api->metadata_remove(this, "test_setting"); + } */ } @@ -39,8 +79,8 @@ void storage_impl::load(core_api *a) info.description = "this module provide metadata storage in flat files"; info.version = "0.0.0.1draft"; info.default_settings["data_path"] = "~/.local/share/udm/metadata"; - info.on_modules_loaded = boost::bind(&storage_impl::on_modules_loaded, this); - std::cout<<"flat_files metadata module succesfully loaded\n"; + info.on_modules_loaded = boost::bind(&storage_impl::on_modules_loaded, this); //optional definition of function which is called after all modules loaded + //std::cout<<"flat_files metadata module succesfully loaded\n"; //working fine } @@ -49,27 +89,77 @@ const module_info &storage_impl::get_module_info() return info; } + + void storage_impl::set_module_settings(const std::map<std::string, std::string> &settings) { this->settings = settings; + parsed_data_path = replace_home_var(this->settings["data_path"]); } + bool storage_impl::set(const std::string &module_name, const std::string &setting_name, const std::vector<char> &data) { - //TODO + //print data +/* std::cout<<"printing data in metadata_flat_files set api:\n"; + for(auto i = data.begin(), end = data.end(); i != end; ++i) + { + std::cout<<*i; + } + std::cout<<std::endl; */ + std::string out_file_path = parsed_data_path; + out_file_path += "/"; + out_file_path += module_name; + if(!boost::filesystem::exists(out_file_path)) + { + //TODO: validation + boost::filesystem::create_directories(out_file_path); + } + else if(!boost::filesystem::is_directory(out_file_path)) + { + //error + return false; + } + out_file_path += "/"; + out_file_path += setting_name; + std::ofstream os(out_file_path, std::ios::binary); + boost::archive::binary_oarchive ar(os); + ar<<data; return true; } bool storage_impl::get(const std::string &module_name, const std::string &setting_name, std::vector<char> &data) { - //TODO + std::string in_file_path = parsed_data_path; + in_file_path += "/"; + in_file_path += module_name; + in_file_path += "/"; + in_file_path += setting_name; + if(!boost::filesystem::exists(in_file_path) || !boost::filesystem::is_regular(in_file_path)) + { + //data does not exists + return false; + } + std::ifstream is(in_file_path, std::ios::binary); + boost::archive::binary_iarchive ar(is); + ar>>data; return true; } bool storage_impl::remove(const std::string &module_name, const std::string &setting_name) { //TODO + std::string file_path = parsed_data_path; + file_path += "/"; + file_path += module_name; + file_path += "/"; + file_path += setting_name; + if(!boost::filesystem::exists(file_path) || !boost::filesystem::is_regular(file_path)) + { + return false; + } + boost::filesystem::remove(file_path); return true; } diff --git a/server/modules/metadata/flat_files/main.h b/server/modules/metadata/flat_files/main.h index 97a2723..0798674 100644 --- a/server/modules/metadata/flat_files/main.h +++ b/server/modules/metadata/flat_files/main.h @@ -44,6 +44,7 @@ class storage_impl: public module_metadata_storage module_info info; core_api *api = nullptr; std::map<std::string, std::string> settings; + std::string parsed_data_path; }; |