Skip to content

Commit

Permalink
SERXIONE-6515 : Integrity check
Browse files Browse the repository at this point in the history
Reason for change: Delete file if not a database, or corrupted.
Fix namespace limits when namespace has no items.
Test Procedure: None
Risks: None
Signed-off-by: Nikita Poltorapavlo <[email protected]>
  • Loading branch information
npoltorapavlo committed Nov 13, 2024
1 parent c90d0f6 commit 58fbb21
Show file tree
Hide file tree
Showing 2 changed files with 270 additions and 128 deletions.
36 changes: 33 additions & 3 deletions PersistentStore/sqlite/Store2.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ namespace Plugin {
, _maxValue(maxValue)
, _limit(limit)
{
IntegrityCheck();
Open();
}
~Store2() override
Expand All @@ -96,6 +97,32 @@ namespace Plugin {
}

private:
void IntegrityCheck()
{
Core::File file(_path);
Core::Directory(file.PathName().c_str()).CreatePath();
auto rc = sqlite3_open(_path.c_str(), &_data);
sqlite3_stmt* stmt;
sqlite3_prepare_v2(_data, "pragma integrity_check;",
-1, &stmt, nullptr);
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
auto text = (const char*)
sqlite3_column_text(stmt, 0);
if (strcmp(text, "ok") != 0) {
TRACE(Trace::Error,
(_T("%s sqlite error %s"), __FUNCTION__, text));
}
}
sqlite3_finalize(stmt);
sqlite3_close_v2(_data);
if (rc != SQLITE_DONE) {
OnError(__FUNCTION__, rc);
if ((rc == SQLITE_MISUSE) || (rc == SQLITE_CORRUPT)
|| (rc == SQLITE_NOTADB)) {
ASSERT(file.Destroy());
}
}
}
void Open()
{
Core::File file(_path);
Expand Down Expand Up @@ -150,12 +177,13 @@ namespace Plugin {
+ std::to_string(_maxSize) + " then raise (fail, 'max size') end; end;",
"create temporary trigger if not exists item_limit_default insert on item"
" begin select case when"
" (select length(new.key)+length(new.value)+sum(length(key)+length(value)) from item where ns = new.ns) > "
" (select sum(s) from (select sum(length(key)+length(value)) s from item where ns = new.ns"
" union all select length(new.key)+length(new.value) s)) > "
+ std::to_string(_limit) + " then raise (fail, 'limit') end; end;",
"create temporary trigger if not exists item_limit insert on item"
" begin select case when"
" (select size-length(new.key)-length(new.value)-sum(length(key)+length(value)) from limits"
" inner join item on limits.n = item.ns where n = new.ns) < 0"
" (select size-length(new.key)-length(new.value)-ifnull(sum(length(key)+length(value)), 0) from limits"
" left join item on limits.n = item.ns where n = new.ns) < 0"
" then raise (fail, 'limit') end; end;"
};
for (auto& sql : statements) {
Expand All @@ -172,6 +200,8 @@ namespace Plugin {
OnError(__FUNCTION__, rc);
}
}

private:
bool IsTimeSynced() const
{
#ifdef WITH_SYSMGR
Expand Down
Loading

0 comments on commit 58fbb21

Please sign in to comment.