/** --------------------------------------------------------------------------- @TAG #cache
* @brief Prepares a cache table for the specified identifier.
*
* This method initializes and prepares a table for caching data associated with the given `stringId`.
* If the `stringId` matches specific identifiers like "file", "file-count", or "file-linelist" etc
* it creates tables with predefined columns tailored for their respective purposes:
*
* The table is then added to the internal application cache.
*
* @param stringId A string view representing the identifier for the cache table.
*
* @details
* - The method first checks if a cache table with the given `stringId` already exists using `CACHE_Get`.
* - If the table does not exist, it creates a new `table` object with predefined columns.
* - The table is wrapped in a `std::unique_ptr` and added to the cache using `CACHE_Add`.
*
* @note This method assumes that the `CACHE_Add` function handles the ownership of the table.
*/
void CDocument::CACHE_Prepare(const std::string_view& stringId, std::unique_ptr<gd::table::dto::table>* ptable) // @TAG #data.cache
{ LOG_VERBOSE_RAW("== Prepare table:" << stringId << "CDocument::CACHE_Prepare");
using namespace gd::table::dto;
constexpr unsigned uTableStyle = ( table::eTableFlagNull32 | table::eTableFlagRowStatus );
if( ptable == nullptr )
{
auto ptableFind = CACHE_Get(stringId, false);
if( ptableFind != nullptr ) return; // table already exists, exit
}
std::unique_ptr<gd::table::dto::table> ptable_;
// ## prepare file list
// columns: "path, size, date, extension
if( stringId == "file" ) // file cache, used to store file information
{
auto p_ = CACHE_Get(stringId, false);
if( p_ == nullptr )
{
// file table: key | path | size | date | extension
ptable_ = std::make_unique<table>(table(uTableStyle, { {"uint64", 0, "key"}, {"rstring", 0, "folder"}, {"rstring", 0, "filename"}, {"uint64", 0, "size"}, {"double", 0, "date"}, {"string", 20, "extension"} }, gd::table::tag_prepare{}));
ptable_->property_set("id", stringId); // set id for table, used to identify table in cache
}
}
else if( stringId == "file-dir" ) // file cache, used to store file information
{
auto p_ = CACHE_Get(stringId, false);
if( p_ == nullptr )
{
// file table: key | path | size | date | extension
ptable_ = std::make_unique<table>(table(uTableStyle, { {"uint64", 0, "key"}, {"rstring", 0, "path"}, {"uint64", 0, "size"}, {"double", 0, "date"}, {"string", 20, "extension"} }, gd::table::tag_prepare{}));
ptable_->property_set("id", stringId); // set id for table, used to identify table in cache
}
}
else if( stringId == "file-count" ) // row counter table
{
auto p_ = CACHE_Get(stringId, false);
if( p_ == nullptr )
{
// file-count table: key | file-key | filename
// count | code | characters | comment | string
ptable_ = std::make_unique<table>(table(uTableStyle,
{ {"uint64", 0, "key"}, {"uint64", 0, "file-key"}, {"rstring", 0, "filename"},
{"uint64", 0, "count"}, {"uint64", 0, "code"}, {"uint64", 0, "characters"}, {"uint64", 0, "comment"}, {"uint64", 0, "string"} }, gd::table::tag_prepare{})
);
ptable_->property_set("id", stringId); // set id for table, used to identify table in cache
}
}
else if( stringId == "file-linelist" ) // lists line where pattern was found
{
auto p_ = CACHE_Get(stringId, false);
if( p_ == nullptr )
{
// file-linelist table: key | file-key | filename
// line | row | column | pattern, segment
// line = the row in text where pattern was found
ptable_ = std::make_unique<table>(table(uTableStyle,
{ {"uint64", 0, "key"}, {"uint64", 0, "file-key"}, {"rstring", 0, "filename"},
{"rstring", 0, "line"}, {"uint64", 0, "row"}, {"uint64", 0, "column"}, {"string", 32, "pattern"}, {"string", 10, "segment"} }, gd::table::tag_prepare{})
);
ptable_->property_set("id", stringId); // set id for table, used to identify table in cache
}
}
else if( stringId == "file-snippet" )
{
ptable_ = std::make_unique<table>(table(uTableStyle,
{ {"uint64", 0, "key"}, {"uint64", 0, "file-key"}, {"rstring", 0, "filename"},
{"string", 10, "format"}, {"uint64", 0, "row"}, {"rstring", 0, "snippet"} }, gd::table::tag_prepare{})
);
ptable_->property_set("id", stringId); // set id for table, used to identify table in cache
}
else if( stringId == "keyvalue" )
{
auto ptableKeys = std::make_unique<gd::table::arguments::table>( gd::table::tag_full_meta{} );
ptableKeys->column_prepare();
ptableKeys->column_add("uint64", 0, "key"); // add key column
ptableKeys->column_add("uint64", 0, "file-key"); // foreign key to file table
ptableKeys->column_add("uint64", 0, "file-linelist-key"); // foreign key to file-linelist table
ptableKeys->column_add("rstring", 0, "filename"); // name of file
ptableKeys->column_add("uint64", 0, "row"); // row number in file
ptableKeys->prepare(); // prepare table
ptableKeys->property_set("id", stringId); // set id for table, used to identify table in cache
CACHE_Add( std::move(ptableKeys) ); // add table to cache
return; // exit, table is already added to cache
}
else if( stringId == "history" )
{
// file table: index | date | command | line
ptable_ = std::make_unique<table>(table(uTableStyle, { {"uint64", 0, "key"}, {"int32", 0, "index"}, {"string", 30, "date"}, {"string", 20, "name"}, {"rstring", 0, "line"}, {"rstring", 0, "alias"} }, gd::table::tag_prepare{}));
ptable_->property_set("id", stringId); // set id for table, used to identify table in cache
}
else { assert(false); } // unknown cache table
if( ptable != nullptr )
{
*ptable = std::move(ptable_); // move table to caller
}
else
{
CACHE_Add(std::move(*ptable_)); // add it to internal application cache
}
}