CAssociativeDataStorage

Материал из CAMaaS preliminary wiki
Перейти к навигации Перейти к поиску
Пример
#include <camaas/idatastorage.h>
#include <chsvlib/chsvmath.h>
#include <string>
#include <iostream>
#include <iomanip>

using namespace CAMaaS;

int main(int, char**)
{
    auto map = CAssociativeDataStorage<InMemoryAssociativeDataStorageImplementation>();

    map.insert("Key1", 4, std::string("value1"));
    map.insert(reinterpret_cast<const std::uint8_t*>("Key2"), 4, "value2", 6);
    map.insert(std::string("Key3"), "value3", 6);
    map.insert(static_cast<const void*>("Key4"), 5, static_cast<const void*>("value4"), 7);
    auto v = map.find("Key3", 4);
    auto strFound = represent_as<InputByteStreamOwn>(v.input_stream()).read_all_as<std::string>();
    std::cout << strFound << "\n";
    map.erase(v);
    std::cout << represent_as<InputByteStreamOwn>(map.find("Key1", 4).input_stream()).read_all_as<std::string>() << "\n";
    std::cout << represent_as<InputByteStreamOwn>(map.find(reinterpret_cast<const std::uint8_t*>("Key2"), 4).input_stream()).read_all_as<std::string>() << "\n";
    if (bool(map.find("Key3", 4)))
        std::cout << "Found a deleted element\n";
    auto strg = represent_as<InMemoryDataStorageInputOwn>(map.find("Key4", 5).get_storage());
    std::cout << std::string(static_cast<const char*>(strg.data()), std::size_t(strg.byte_size())) << "\n";

    auto map2 = make_associative_data_storage();
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(map2.create_node("Some node 1")).write()).write("Value");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(map2.create_node("Some node 2")).write()).write("Value");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(map2.create_node("Some node 3")).write()).write("Value 3");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(map2.create_node("Some node 4")).write()).write("Value");
    map2.delete_node("Some node 2");
    std::cout << represent_as<InputByteStreamOwn>(map2.read("Some node 3")).read_all_as<std::string>() << "\n";
    
    auto ini_map = make_associative_data_storage<AssociativeDataStorageParserIni | AssociativeDataStorageReadAccess | AssociativeDataStorageWriteAccess>
        (make_file_based_data_storage("ini_test.ini", FileReadWrite, FileCreateAlways));
    auto node_storage = represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some node"));
    auto node_os = represent_as<OutputByteStreamOwn>(node_storage.write());
    node_os.write("Value");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some node 1")).write()).write("Value1");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some node 2")).write()).write("Value2");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some node 3")).write()).write("Value 3");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some node 4")).write()).write("Value 4");
    ini_map.delete_node("Some node 2");
    std::cout << represent_as<InputByteStreamOwn>(ini_map.read("Some node 3")).read_all_as<std::string>() << "\n";
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some section 1/Some node 1")).write()).write("Value");
    ini_map.create_node("Some section 1/empty node");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some section 2/Some node 1")).write()).write("Value");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some section 3/Some node 1")).write()).write("Value");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some section 3/Some node 2")).write()).write("Value");
    ini_map.create_node("Some section 4/Some node 1");
    represent_as<OutputByteStreamOwn>(represent_as<ContiguousDataStorageOwn>(ini_map.create_node("Some section with \\/ and \\\\/Some node with \\/ and \\\\")).write()).write("A value (\\, /)");
    ini_map.delete_node("Some section 3/Some node 1");
    ini_map.delete_node("Some section 3/Some node 2");
    ini_map.delete_node("Some section 4/Some node 1");
    std::cout << represent_as<InputByteStreamOwn>(ini_map.read("Some section with \\/ and \\\\/Some node with \\/ and \\\\")).read_all_as<std::string>() << "\n";
    if (bool(ini_map.find_node("Some section 4/Some node 1", std::nothrow)))
        std::cout << "Found a deleted node\n";
    return 0;
}