Skip to content

Commit

Permalink
Clean up functions: Put into namespace / remove unused ones
Browse files Browse the repository at this point in the history
  • Loading branch information
teusbenschop committed May 31, 2024
1 parent 0423a5e commit 64e2121
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 146 deletions.
4 changes: 0 additions & 4 deletions assets/header.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,6 @@ std::string Assets_Header::run ()
embedded_css.push_back ("body { font-size: " + filter::strings::convert_to_string (fontsize) + "%; }");
}
fontsize = m_webserver_request.database_config_user ()->getMenuFontSize ();
std::string filename = menu_font_size_filebased_cache_filename (m_webserver_request.session_identifier);
if (fontsize != 100) {
embedded_css.push_back (".menu-advanced, .menu-basic { font-size: " + filter::strings::convert_to_string (fontsize) + "%; }");
}
Expand All @@ -270,7 +269,6 @@ std::string Assets_Header::run ()
embedded_css.push_back (".bibleeditor { font-size: " + filter::strings::convert_to_string (fontsize) + "% !important; }");
}
fontsize = m_webserver_request.database_config_user ()->getResourcesFontSize ();
filename = resource_font_size_filebased_cache_filename (m_webserver_request.session_identifier);
if (fontsize != 100) {
embedded_css.push_back (".resource { font-size: " + filter::strings::convert_to_string (fontsize) + "% !important; }");
}
Expand All @@ -279,7 +277,6 @@ std::string Assets_Header::run ()
embedded_css.push_back (".hebrew { font-size: " + filter::strings::convert_to_string (fontsize) + "%!important; }");
}
fontsize = m_webserver_request.database_config_user ()->getGreekFontSize ();
filename = greek_font_size_filebased_cache_filename (m_webserver_request.session_identifier);
if (fontsize != 100) {
embedded_css.push_back (".greek { font-size: " + filter::strings::convert_to_string (fontsize) + "%!important; }");
}
Expand All @@ -288,7 +285,6 @@ std::string Assets_Header::run ()
}

int current_theme_index = m_webserver_request.database_config_user ()->getCurrentTheme ();
filename = current_theme_filebased_cache_filename (m_webserver_request.session_identifier);
// Add the theme color css class selector name on the body element,..
m_view->set_variable ("body_theme_color", Filter_Css::theme_picker (current_theme_index, 0));
// ..workspacewrapper div element..
Expand Down
133 changes: 23 additions & 110 deletions database/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,13 @@ int size (const std::string& resource, const int book)
}


}
} // namespace.


namespace database::cache::file {


std::string database_filebased_cache_full_path (std::string file)
static std::string full_path (std::string file)
{
return filter_url_create_root_path ({database_logic_databases (), "cache", file});
}
Expand All @@ -275,7 +278,7 @@ std::string database_filebased_cache_full_path (std::string file)
// The purpose of splitting the file up into paths is
// to avoid that the cache folder would contain too many files
// and so would become slow.
std::string database_filebased_cache_split_file (std::string file)
static std::string split_file (std::string file)
{
if (file.size () > 9) file.insert (9, "/");
if (file.size () > 18) file.insert (18, "/");
Expand All @@ -285,136 +288,46 @@ std::string database_filebased_cache_split_file (std::string file)
}


bool database_filebased_cache_exists (std::string schema)
bool exists (std::string schema)
{
schema = filter_url_clean_filename (schema);
schema = database_filebased_cache_split_file (schema);
schema = database_filebased_cache_full_path (schema);
schema = database::cache::file::split_file (schema);
schema = database::cache::file::full_path (schema);
return file_or_dir_exists (schema);
}


void database_filebased_cache_put (std::string schema, std::string contents)
void put (std::string schema, const std::string& contents)
{
schema = filter_url_clean_filename (schema);
schema = database_filebased_cache_split_file (schema);
schema = database_filebased_cache_full_path (schema);
std::string path = filter_url_dirname (schema);
if (!file_or_dir_exists (path)) filter_url_mkdir (path);
schema = split_file (schema);
schema = full_path (schema);
const std::string path = filter_url_dirname (schema);
if (!file_or_dir_exists (path))
filter_url_mkdir (path);
filter_url_file_put_contents (schema, contents);
}


std::string database_filebased_cache_get (std::string schema)
std::string get (std::string schema)
{
schema = filter_url_clean_filename (schema);
schema = database_filebased_cache_split_file (schema);
schema = database_filebased_cache_full_path (schema);
schema = database::cache::file::split_file (schema);
schema = database::cache::file::full_path (schema);
return filter_url_file_get_contents (schema);
}


void database_filebased_cache_remove (std::string schema)
void remove (std::string schema)
{
schema = filter_url_clean_filename (schema);
schema = database_filebased_cache_split_file (schema);
schema = database_filebased_cache_full_path (schema);
schema = split_file (schema);
schema = full_path (schema);
filter_url_unlink (schema);
}


// Create a file name based on the client's IPv4 and a unique data identifier.
std::string database_filebased_cache_name_by_ip (std::string address, std::string id)
{
id = "_" + id;
std::string ipv4_sp = "::ffff:";
const unsigned long pos = address.find (ipv4_sp);
if (address.find (ipv4_sp) != std::string::npos) address.erase (pos, ipv4_sp.length ());
if (address.find (id) == std::string::npos) address.append (id);
return address;
}


// Create a file name based on the client's session id and a unique
// data identifier.
std::string database_filebased_cache_name_by_session_id (std::string sid, std::string id)
{
id = "_" + id;
if (sid.find (id) == std::string::npos) sid.append (id);
return sid;
}


// File name for focused book file based database cache by session id
// plus abbreviation.
std::string focused_book_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "focbo");
}


// File name for focused chapter file based database cache by session
// id plus abbreviation.
std::string focused_chapter_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "focch");
}


// File name for focused verse file based database cache by session id
// plus abbreviation.
std::string focused_verse_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "focve");
}


// File name for general font size file based database cache by
// session id plus abbreviation.
std::string general_font_size_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "genfs");
}


// File name for menu font size file based database cache by session
// id plus abbreviation.
std::string menu_font_size_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "menfs");
}


// File name for resource font size file based database cache by
// session id plus abbreviation.
std::string resource_font_size_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "resfs");
}


// File name for hebrew font size file based database cache by
// session id plus abbreviation.
std::string hebrew_font_size_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "hebfs");
}


// File name for greek font size file based database cache by session
// id plus abbreviation.
std::string greek_font_size_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "grefs");
}


// File name for current theme file based database cache by session
// id plus abbreviation.
std::string current_theme_filebased_cache_filename (std::string sid)
{
return database_filebased_cache_name_by_session_id (sid, "curth");
}
} // namespace.


// Deletes expired cached items.
Expand All @@ -425,7 +338,7 @@ void database_cache_trim (bool clear)
std::string output, error;

// The directory that contains the file-based cache files.
std::string path = database_filebased_cache_full_path ("");
std::string path = database::cache::file::full_path ("");

// Get the free space on the file system that contains the cache.
output.clear ();
Expand Down
25 changes: 5 additions & 20 deletions database/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

namespace database::cache::sql {


std::string fragment ();


void create (const std::string& resource, int book);
void remove (const std::string& resource);
void remove (const std::string&, int book);
Expand All @@ -41,28 +38,16 @@ void ready (const std::string& resource, const int book, const bool ready);
int size (const std::string& resource, const int book);
std::string path (const std::string& resource, int book);


}

namespace database::cache::file {

bool exists (std::string schema);
void put (std::string schema, const std::string& contents);
std::string get (std::string schema);

bool database_filebased_cache_exists (std::string schema);
void database_filebased_cache_put (std::string schema, std::string contents);
std::string database_filebased_cache_get (std::string schema);
void database_filebased_cache_remove (std::string schema);
std::string database_filebased_cache_name_by_ip (std::string address, std::string id);
std::string database_filebased_cache_name_by_session_id (std::string sid, std::string id);

}

std::string focused_book_filebased_cache_filename (std::string sid);
std::string focused_chapter_filebased_cache_filename (std::string sid);
std::string focused_verse_filebased_cache_filename (std::string sid);
std::string general_font_size_filebased_cache_filename (std::string sid);
std::string menu_font_size_filebased_cache_filename (std::string sid);
std::string resource_font_size_filebased_cache_filename (std::string sid);
std::string hebrew_font_size_filebased_cache_filename (std::string sid);
std::string greek_font_size_filebased_cache_filename (std::string sid);
std::string current_theme_filebased_cache_filename (std::string sid);


void database_cache_trim (bool clear);
Expand Down
2 changes: 0 additions & 2 deletions menu/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ std::string menu_logic_main_categories (Webserver_Request& webserver_request, st

std::string menutooltip;
int current_theme_index = webserver_request.database_config_user ()->getCurrentTheme ();
std::string filename = current_theme_filebased_cache_filename (webserver_request.session_identifier);
std::string color = Filter_Css::theme_picker (current_theme_index, 1);

if (!menu_logic_translate_category (webserver_request, &menutooltip).empty ()) {
Expand Down Expand Up @@ -291,7 +290,6 @@ std::string menu_logic_basic_categories (Webserver_Request& webserver_request)
std::vector <std::string> html;

int current_theme_index = webserver_request.database_config_user ()->getCurrentTheme ();
std::string filename = current_theme_filebased_cache_filename (webserver_request.session_identifier);
std::string color = Filter_Css::theme_picker (current_theme_index, 1);

if (read_index_acl (webserver_request)) {
Expand Down
1 change: 0 additions & 1 deletion personalize/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ std::string personalize_index (Webserver_Request& webserver_request)

// Set the chosen theme on the option HTML tag.
std::string theme_key = filter::strings::convert_to_string (webserver_request.database_config_user ()->getCurrentTheme ());
std::string filename = current_theme_filebased_cache_filename (webserver_request.session_identifier);
std::string theme_html;
theme_html = Options_To_Select::add_selection ("Basic", "0", theme_html);
theme_html = Options_To_Select::add_selection ("Light", "1", theme_html);
Expand Down
1 change: 0 additions & 1 deletion read/index.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ std::string read_index (Webserver_Request& webserver_request)
std::string cls = Filter_Css::getClass (bible);
std::string font = fonts::logic::get_text_font (bible);
int current_theme_index = webserver_request.database_config_user ()->getCurrentTheme ();
std::string filename = current_theme_filebased_cache_filename (webserver_request.session_identifier);
int direction = database::config::bible::get_text_direction (bible);
int lineheight = database::config::bible::get_line_height (bible);
int letterspacing = database::config::bible::get_letter_spacing (bible);
Expand Down
6 changes: 3 additions & 3 deletions resource/logic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,8 @@ std::string resource_logic_web_or_cache_get (std::string url, std::string& error
{
#ifndef HAVE_CLIENT
// On the Cloud, check if the URL is in the cache.
if (database_filebased_cache_exists (url)) {
return database_filebased_cache_get (url);
if (database::cache::file::exists (url)) {
return database::cache::file::get (url);
}
#endif

Expand All @@ -759,7 +759,7 @@ std::string resource_logic_web_or_cache_get (std::string url, std::string& error
// In the Cloud, cache the response based on certain criteria.
bool cache = database_cache_can_cache (error, html);
if (cache) {
database_filebased_cache_put (url, html);
database::cache::file::put (url, html);
}
#endif

Expand Down
10 changes: 5 additions & 5 deletions unittests/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ TEST (database, cache)
{
std::string url = "https://netbible.org/bible/1/2/3";
std::string contents = "Bible contents";
EXPECT_EQ (false, database_filebased_cache_exists (url));
EXPECT_EQ ("", database_filebased_cache_get (url));
database_filebased_cache_put (url, contents);
EXPECT_EQ (true, database_filebased_cache_exists (url));
EXPECT_EQ (contents, database_filebased_cache_get (url));
EXPECT_EQ (false, database::cache::file::exists (url));
EXPECT_EQ ("", database::cache::file::get (url));
database::cache::file::put (url, contents);
EXPECT_EQ (true, database::cache::file::exists (url));
EXPECT_EQ (contents, database::cache::file::get (url));
database_cache_trim (false);
}

Expand Down

0 comments on commit 64e2121

Please sign in to comment.