parent
f7a885b625
commit
d7e0ab1da3
9 changed files with 227 additions and 8 deletions
@ -0,0 +1,187 @@ |
|||||||
|
#include "fs_manager.h" |
||||||
|
|
||||||
|
namespace Bk::Json |
||||||
|
{ |
||||||
|
FsManager::FsManager(std::string path) |
||||||
|
: path(path)
|
||||||
|
{ |
||||||
|
auto dir = Directory(path); |
||||||
|
if (dir.exists()) |
||||||
|
{ |
||||||
|
File f_conf(path + "/collection.json"); |
||||||
|
if (f_conf.exists()) |
||||||
|
{ |
||||||
|
config = Parser(f_conf).parse();
|
||||||
|
write_config(); |
||||||
|
}
|
||||||
|
else
|
||||||
|
{ |
||||||
|
Pointer node(new Node()); |
||||||
|
node->set_null(); |
||||||
|
config = node; |
||||||
|
} |
||||||
|
}
|
||||||
|
else
|
||||||
|
{ |
||||||
|
Pointer node(new Node()); |
||||||
|
node->set_null(); |
||||||
|
config = node; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::config_init(int page_size) |
||||||
|
{ |
||||||
|
auto dir = Directory(path); |
||||||
|
std::string s_config = "\n" |
||||||
|
"{\n" |
||||||
|
" \"name\": \"" + dir.name() + "\",\n" |
||||||
|
" \"id_count\": 0,\n" |
||||||
|
" \"page_size\": " + std::to_string(page_size) + "\n"
|
||||||
|
"}"; |
||||||
|
config = Parser(s_config).parse();
|
||||||
|
write_config(); |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::init(bool force, int page_size) |
||||||
|
{ |
||||||
|
auto dir = Directory(path); |
||||||
|
if (dir.exists() && force) dir = dir.remove(); |
||||||
|
if (!dir.exists()) |
||||||
|
{ |
||||||
|
Directory::create(dir.path()); |
||||||
|
config_init(page_size); |
||||||
|
}
|
||||||
|
else
|
||||||
|
{ |
||||||
|
File f_conf(path + "/collection.json"); |
||||||
|
if (!f_conf.exists()) |
||||||
|
{ |
||||||
|
config_init(page_size); |
||||||
|
}
|
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::set_config(Object* object)
|
||||||
|
{
|
||||||
|
config->set_object(object); |
||||||
|
|
||||||
|
write_config(); |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::write(File file, Pointer& node) |
||||||
|
{ |
||||||
|
Type::DataStream data; |
||||||
|
auto raw = node->to_string(); |
||||||
|
data.push<char>(raw.c_str(), raw.length()); |
||||||
|
file.write(data); |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::write_config() |
||||||
|
{ |
||||||
|
if (!config->is_null()) |
||||||
|
{ |
||||||
|
File f_config = File(path + "/collection.json"); |
||||||
|
write(f_config, config); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::insert(Object* object) |
||||||
|
{ |
||||||
|
if (!config->is_null()) |
||||||
|
{ |
||||||
|
Object* conf_obj = config->get_p_object(); |
||||||
|
(*object)["id"] = Pointer(new Node()); |
||||||
|
(*object)["id"]->set_int((*conf_obj)["id_count"]->get_int()); |
||||||
|
auto f_list = File(Tools::string_format("%s/%d.json", path, get_page((*conf_obj)["id_count"]->get_int()))); |
||||||
|
if(f_list.exists())
|
||||||
|
{ |
||||||
|
auto ls = Parser(f_list).parse(); |
||||||
|
auto new_node = Pointer(new Node()); |
||||||
|
new_node->set_object(object); |
||||||
|
ls->get_p_list()->push_back(new_node); |
||||||
|
write(f_list, ls); |
||||||
|
}
|
||||||
|
else
|
||||||
|
{ |
||||||
|
auto ls = Pointer(new Node()); |
||||||
|
auto new_node = new Node(); |
||||||
|
new_node->set_object(object); |
||||||
|
auto new_ls = new List(); |
||||||
|
new_ls->push_back(Pointer(new_node)); |
||||||
|
ls->set_list(new_ls); |
||||||
|
write(f_list, ls); |
||||||
|
} |
||||||
|
(*conf_obj)["id_count"]->set_float((*conf_obj)["id_count"]->get_int() + 1); |
||||||
|
set_config(conf_obj); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::update(Object* object) |
||||||
|
{ |
||||||
|
if (object->find("id") != object->end()) |
||||||
|
{ |
||||||
|
int id = (*object)["id"]->get_int(); |
||||||
|
auto f_list = File(Tools::string_format("%s/%d.json", path, get_page(id))); |
||||||
|
if (f_list.exists()) |
||||||
|
{ |
||||||
|
auto ls = Parser(f_list).parse(); |
||||||
|
for (int i = 0; i < ls->get_list().size(); i++) |
||||||
|
{ |
||||||
|
auto node = ls->get_p_list()->at(i); |
||||||
|
if (node->get_object().find("id") != node->get_object().end()) |
||||||
|
{ |
||||||
|
if (node->get_object()["id"]->get_int() == id) |
||||||
|
{ |
||||||
|
ls->get_p_list()->at(i)->set_object(object); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
write(f_list, ls); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void FsManager::remove(int id) |
||||||
|
{ |
||||||
|
auto f_path = Tools::string_format("%s/%d.json", path, get_page(id)); |
||||||
|
auto f_list = File(f_path); |
||||||
|
if (f_list.exists()) |
||||||
|
{ |
||||||
|
auto ls = Parser(f_list).parse(); |
||||||
|
for (int i = 0; i < ls->get_list().size(); i++) |
||||||
|
{ |
||||||
|
auto node = ls->get_p_list()->at(i); |
||||||
|
if (node->get_object().find("id") != node->get_object().end()) |
||||||
|
{ |
||||||
|
if (node->get_object()["id"]->get_int() == id) |
||||||
|
{ |
||||||
|
ls->get_p_list()->erase(ls->get_p_list()->begin() + i); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
write(f_list, ls); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
Object FsManager::findby_id(int id) |
||||||
|
{ |
||||||
|
auto f_path = Tools::string_format("%s/%d.json", path, get_page(id)); |
||||||
|
auto f_list = File(f_path); |
||||||
|
if (f_list.exists()) |
||||||
|
{ |
||||||
|
auto ls = Parser(f_list).parse(); |
||||||
|
for (int i = 0; i < ls->get_list().size(); i++) |
||||||
|
{ |
||||||
|
auto node = ls->get_p_list()->at(i); |
||||||
|
if (node->get_object().find("id") != node->get_object().end()) |
||||||
|
{ |
||||||
|
if (node->get_object()["id"]->get_int() == id) |
||||||
|
{ |
||||||
|
return node->get_object(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
return Object(); |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,28 @@ |
|||||||
|
#include <bakatoolspch.h> |
||||||
|
#include "parser.h" |
||||||
|
|
||||||
|
namespace Bk::Json |
||||||
|
{ |
||||||
|
class FsManager
|
||||||
|
{ |
||||||
|
std::string path; |
||||||
|
Pointer config; |
||||||
|
public: |
||||||
|
FsManager(std::string path); |
||||||
|
|
||||||
|
void init(bool force = false, int page_size = 100); |
||||||
|
void config_init(int page_size); |
||||||
|
bool exists() { return File(path + "/collection.json").exists(); } |
||||||
|
|
||||||
|
void set_config(Object* object); |
||||||
|
void write(File file, Pointer& node); |
||||||
|
void write_config(); |
||||||
|
Object get_config() { return config->get_object(); } |
||||||
|
int get_page(float id) { return (int)(id/100.0f); } |
||||||
|
|
||||||
|
void insert(Object* object); |
||||||
|
void update(Object* object); |
||||||
|
void remove(int id); |
||||||
|
Object findby_id(int id); |
||||||
|
}; |
||||||
|
} |
@ -1,4 +1,4 @@ |
|||||||
#include "json_lexer.h" |
#include "lexer.h" |
||||||
|
|
||||||
namespace Bk::Json
|
namespace Bk::Json
|
||||||
{ |
{ |
@ -1,4 +1,4 @@ |
|||||||
#include "json_node.h" |
#include "node.h" |
||||||
namespace Bk::Json
|
namespace Bk::Json
|
||||||
{ |
{ |
||||||
std::string Node::to_string(int indent)
|
std::string Node::to_string(int indent)
|
@ -1,4 +1,4 @@ |
|||||||
#include "json_parser.h" |
#include "parser.h" |
||||||
|
|
||||||
namespace Bk::Json |
namespace Bk::Json |
||||||
{ |
{ |
@ -1,7 +1,7 @@ |
|||||||
#pragma once |
#pragma once |
||||||
|
|
||||||
#include <bakatoolspch.h> |
#include <bakatoolspch.h> |
||||||
#include "json_lexer.h" |
#include "lexer.h" |
||||||
|
|
||||||
namespace Bk::Json |
namespace Bk::Json |
||||||
{ |
{ |
Loading…
Reference in New Issue