Skip to content

Commit 8b59be3

Browse files
committed
Updates in performance and quality
#950
1 parent 03ff9f9 commit 8b59be3

File tree

4 files changed

+42
-47
lines changed

4 files changed

+42
-47
lines changed

database/notes.cpp

Lines changed: 33 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
5656
In older versions the notes were stored as a bundle of separate files.
5757
In newer versions each note is stored as one JSON file.
5858
This uses less space on disk.
59-
In older versions, on a Linux server, one notes took 32 kbytes.
59+
In older versions, on a Linux server, one note took 32 kbytes.
6060
A lot of that space is wasted.
6161
In newer versions one notes takes only 4 kbytes.
6262
That is a difference of 8 times.
@@ -184,21 +184,21 @@ bool Database_Notes::checkup_checksums ()
184184
void Database_Notes::trim ()
185185
{
186186
// Clean empty directories.
187-
std::string message = "Deleting empty notes folder ";
188-
std::string main_folder = main_folder_path ();
189-
std::vector <std::string> bits1 = filter_url_scandir (main_folder);
190-
for (auto bit1 : bits1) {
187+
const std::string message = "Deleting empty notes folder ";
188+
const std::string main_folder = main_folder_path ();
189+
const std::vector <std::string> bits1 = filter_url_scandir (main_folder);
190+
for (const auto& bit1 : bits1) {
191191
if (bit1.length () == 3) {
192-
std::string folder1 = filter_url_create_path ({main_folder, bit1});
193-
std::vector <std::string> bits2 = filter_url_scandir (folder1);
192+
const std::string folder1 = filter_url_create_path ({main_folder, bit1});
193+
const std::vector <std::string> bits2 = filter_url_scandir (folder1);
194194
if (bits2.empty ()) {
195195
Database_Logs::log (message + folder1);
196196
remove (folder1.c_str ());
197197
}
198-
for (auto bit2 : bits2) {
198+
for (const auto& bit2 : bits2) {
199199
if (bit2.length () == 3) {
200-
std::string folder2 = filter_url_create_path ({main_folder, bit1, bit2});
201-
std::vector <std::string> bits3 = filter_url_scandir (folder2);
200+
const std::string folder2 = filter_url_create_path ({main_folder, bit1, bit2});
201+
const std::vector <std::string> bits3 = filter_url_scandir (folder2);
202202
if (bits3.empty ()) {
203203
Database_Logs::log (message + folder2);
204204
remove (folder2.c_str());
@@ -214,11 +214,9 @@ void Database_Notes::trim_server ()
214214
{
215215
// Notes expiry.
216216
touch_marked_for_deletion ();
217-
/// Storage for notes to be deleted.
218-
std::vector <int> identifiers;
219-
// Deal with new notes storage in JSON.
220-
identifiers = get_due_for_deletion ();
221-
for (auto & identifier : identifiers) {
217+
// Storage for notes to be deleted.
218+
const std::vector <int> identifiers = get_due_for_deletion ();
219+
for (const auto identifier : identifiers) {
222220
trash_consultation_note (m_webserver_request, identifier);
223221
erase (identifier);
224222
}
@@ -233,35 +231,35 @@ void Database_Notes::optimize ()
233231
}
234232

235233

236-
void Database_Notes::sync ()
234+
void Database_Notes::sync () // Todo
237235
{
238-
std::string main_folder = main_folder_path ();
236+
const std::string main_folder = main_folder_path ();
239237

240238
// List of notes in the filesystem.
241239
std::vector <int> identifiers;
242240

243-
std::vector <std::string> bits1 = filter_url_scandir (main_folder);
244-
for (auto & bit1 : bits1) {
241+
const std::vector <std::string> bits1 = filter_url_scandir (main_folder);
242+
for (const auto& bit1 : bits1) {
245243
// Bit 1 / 2 / 3 may start with a 0, so conversion to int cannot be used, rather use a length of 3.
246244
// It used conversion to int before to determine it was a real note,
247245
// with the result that it missed 10% of the notes, which subsequently got deleted, oops!
248246
if (bit1.length () == 3) {
249-
std::vector <std::string> bits2 = filter_url_scandir (filter_url_create_path ({main_folder, bit1}));
250-
for (auto & bit2 : bits2) {
247+
const std::vector <std::string> bits2 = filter_url_scandir (filter_url_create_path ({main_folder, bit1}));
248+
for (const auto& bit2 : bits2) {
251249
// Old storage mechanism, e.g. folder "425".
252250
if (bit2.length () == 3) {
253-
std::vector <std::string> bits3 = filter_url_scandir (filter_url_create_path ({main_folder, bit1, bit2}));
254-
for (auto & bit3 : bits3) {
251+
const std::vector <std::string> bits3 = filter_url_scandir (filter_url_create_path ({main_folder, bit1, bit2}));
252+
for (const auto& bit3 : bits3) {
255253
if (bit3.length () == 3) {
256-
int identifier = filter::strings::convert_to_int (bit1 + bit2 + bit3);
254+
const int identifier = filter::strings::convert_to_int (bit1 + bit2 + bit3);
257255
identifiers.push_back (identifier);
258256
update_search_fields (identifier);
259257
}
260258
}
261259
}
262-
// New JSON storage mechanism, e.g. file "894093.json".
260+
// New JSON storage mechanism, e.g. file "894093.json". Todo
263261
if ((bit2.length () == 11) && bit2.find (".json") != std::string::npos) {
264-
int identifier = filter::strings::convert_to_int (bit1 + bit2.substr (0,6));
262+
const int identifier = filter::strings::convert_to_int (bit1 + bit2.substr (0,6));
265263
identifiers.push_back (identifier);
266264
update_database (identifier);
267265
update_search_fields (identifier);
@@ -404,9 +402,9 @@ std::string Database_Notes::note_file (int identifier)
404402
{
405403
// The maximum number of folders a folder may contain is constrained by the filesystem.
406404
// To overcome this, the notes will be stored in a folder structure.
407-
std::string sidentifier = std::to_string (identifier);
408-
std::string folder = sidentifier.substr (0, 3);
409-
std::string file = sidentifier.substr (3, 6) + ".json";
405+
const std::string id_path = std::to_string (identifier);
406+
const std::string folder = id_path.substr (0, 3);
407+
const std::string file = id_path.substr (3, 6) + ".json";
410408
return filter_url_create_path ({main_folder_path (), folder, file});
411409
}
412410

@@ -1882,15 +1880,15 @@ std::vector <std::string> Database_Notes::set_bulk (std::string json)
18821880

18831881

18841882
// Gets a field from a note in JSON format.
1885-
std::string Database_Notes::get_field (int identifier, std::string key)
1883+
std::string Database_Notes::get_field (int identifier, const std::string& key) // Todo
18861884
{
1887-
std::string file = note_file (identifier);
1888-
std::string json = filter_url_file_get_contents (file);
1885+
const std::string file = note_file (identifier);
1886+
const std::string json = filter_url_file_get_contents (file);
18891887
jsonxx::Object note;
18901888
note.parse (json);
1891-
std::string value;
1892-
if (note.has<jsonxx::String> (key)) value = note.get<jsonxx::String> (key);
1893-
return value;
1889+
if (note.has<jsonxx::String> (key))
1890+
return note.get<jsonxx::String> (key);
1891+
return std::string();
18941892
}
18951893

18961894

database/notes.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ class Database_Notes
213213
friend void test_database_notes ();
214214

215215
private:
216-
std::string get_field (int identifier, std::string key);
216+
std::string get_field (int identifier, const std::string& key);
217217
void set_field (int identifier, std::string key, std::string value);
218218

219219
};

search/renotes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
2929
#include <database/config/general.h>
3030

3131

32-
bool search_reindex_notes_running = false;
32+
static bool search_reindex_notes_running = false;
3333

3434

3535
void search_reindex_notes ()

unittests/notes.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -953,26 +953,23 @@ void test_database_notes ()
953953
checksum = database_notes.get_checksum (newidentifier);
954954
EXPECT_EQ (outdated_checksum, checksum);
955955
database_notes.sync ();
956-
// Sometimes something strange happens:
957-
// At times the checksum gets erased as the sync routine cannot find the original note.
958-
// The sync (2) call did not make any difference.
959956
checksum = database_notes.get_checksum (oldidentifier);
960-
if (!checksum.empty()) EXPECT_EQ (good_checksum_old, checksum);
957+
EXPECT_EQ (good_checksum_old, checksum);
961958
database_notes.set_checksum (oldidentifier, outdated_checksum);
962959
checksum = database_notes.get_checksum (newidentifier);
963-
if (!checksum.empty()) EXPECT_EQ (good_checksum_new, checksum);
960+
EXPECT_EQ (good_checksum_new, checksum);
964961
database_notes.set_checksum (newidentifier, outdated_checksum);
965962

966-
// Test that saving a note updates the checksum in most cases.
967-
database_notes.set_checksum (oldidentifier, "");
963+
// Test that saving a note updates the checksum.
964+
database_notes.set_checksum (oldidentifier, std::string());
968965
checksum = database_notes.get_checksum (oldidentifier);
969-
EXPECT_EQ ("", checksum);
966+
EXPECT_EQ (std::string(), checksum);
970967
database_notes.set_modified (oldidentifier, 1234567);
971968
checksum = database_notes.get_checksum (oldidentifier);
972969
EXPECT_EQ (false, checksum.empty());
973-
database_notes.set_checksum (newidentifier, "");
970+
database_notes.set_checksum (newidentifier, std::string());
974971
checksum = database_notes.get_checksum (newidentifier);
975-
EXPECT_EQ ("", checksum);
972+
EXPECT_EQ (std::string(), checksum);
976973
database_notes.set_modified (newidentifier, 1234567);
977974
checksum = database_notes.get_checksum (newidentifier);
978975
EXPECT_EQ (false, checksum.empty());

0 commit comments

Comments
 (0)