diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp index ce1dc96722d33c0eca88d6ee369263b3b7e8625c..508733a08d59bac1473085d781ee5cf592265124 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.cpp @@ -466,7 +466,7 @@ int SQLiteRelationalStore::CreateDistributedTable(const std::string &tableName, if (errCode != E_OK) { LOGE("Create distributed table failed. %d", errCode); } else { - CleanDirtyLogIfNeed(tableName); + CleanDirtyLogIfNeed(tableName, nullptr); } if (schemaChanged) { LOGD("Notify schema changed."); @@ -1366,22 +1366,7 @@ int SQLiteRelationalStore::SetTrackerTable(const TrackerSchema &trackerSchema) if (errCode != -E_IGNORE_DATA) { return errCode; } - if (!IsDailyTrackerIntegrityRepair(trackerSchema.tableName)) { - return E_OK; - } - auto *handle = GetHandle(true, errCode); - if (handle != nullptr) { - handle->CheckAndCreateTrigger(tableInfo); - // Try clear historical mismatched log, which usually do not occur and apply to tracker table only. - if (isNoTableInSchema) { - handle->ClearLogOfMismatchedData(trackerSchema.tableName); - } - handle->RecoverNullExtendLog(trackerSchema, tableInfo.GetTrackerTable()); - ReleaseHandle(handle); - } - CleanDirtyLogIfNeed(trackerSchema.tableName); - LOGI("[RelationalStore] tracker check finish [%s length[%u]]", - DBCommon::StringMiddleMasking(trackerSchema.tableName).c_str(), trackerSchema.tableName.length()); + TrackerIntegrityRepair(trackerSchema, tableInfo, isNoTableInSchema); return E_OK; } errCode = sqliteStorageEngine_->UpdateExtendField(trackerSchema); @@ -1393,7 +1378,7 @@ int SQLiteRelationalStore::SetTrackerTable(const TrackerSchema &trackerSchema) if (isNoTableInSchema) { errCode = sqliteStorageEngine_->SetTrackerTable(trackerSchema, tableInfo, isFirstCreate); if (errCode == E_OK) { - CleanDirtyLogIfNeed(trackerSchema.tableName); + CleanDirtyLogIfNeed(trackerSchema.tableName, nullptr); } return errCode; } @@ -1958,13 +1943,16 @@ int32_t SQLiteRelationalStore::GetDeviceSyncTaskCount() const return syncAbleEngine_->GetDeviceSyncTaskCount(); } -void SQLiteRelationalStore::CleanDirtyLogIfNeed(const std::string &tableName) const +void SQLiteRelationalStore::CleanDirtyLogIfNeed(const std::string &tableName, + SQLiteSingleVerRelationalStorageExecutor *handle) const { int errCode = E_OK; - SQLiteSingleVerRelationalStorageExecutor *handle = GetHandle(true, errCode); if (handle == nullptr) { - LOGW("[RDBStore][ClearDirtyLog] Get handle failed %d", errCode); - return; + handle = GetHandle(true, errCode); + if (handle == nullptr) { + LOGW("[RDBStore][ClearDirtyLog] Get handle failed %d", errCode); + return; + } } ResFinalizer finalizer([this, handle]() { SQLiteSingleVerRelationalStorageExecutor *releaseHandle = handle; @@ -2054,5 +2042,34 @@ bool SQLiteRelationalStore::IsDailyTrackerIntegrityRepair(const std::string &tab } return true; } + +void SQLiteRelationalStore::TrackerIntegrityRepair(const TrackerSchema &trackerSchema, + const TableInfo &tableInfo, bool isNoTableInSchema) +{ + int errCode = E_OK; + auto *handle = GetHandle(true, errCode); + if (handle == nullptr) { + LOGW("[TrackerIntegrityRepair] get handle failed:%d", errCode); + return; + } + handle->CheckAndCreateTrigger(tableInfo); + ReleaseHandle(handle); + if (!IsDailyTrackerIntegrityRepair(trackerSchema.tableName)) { + return; + } + handle = GetHandle(true, errCode); + if (handle == nullptr) { + LOGW("[TrackerIntegrityRepair] get handle err:%d", errCode); + return; + } + // Try clear historical mismatched log, which usually do not occur and apply to tracker table only. + if (isNoTableInSchema) { + handle->ClearLogOfMismatchedData(trackerSchema.tableName); + } + handle->RecoverNullExtendLog(trackerSchema, tableInfo.GetTrackerTable()); + CleanDirtyLogIfNeed(trackerSchema.tableName, handle); + LOGI("[TrackerIntegrityRepair] check finish [%s length[%u]]", + DBCommon::StringMiddleMasking(trackerSchema.tableName).c_str(), trackerSchema.tableName.length()); +} } // namespace DistributedDB #endif diff --git a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h index ec6d5bc8e6e0449b597d22b32aabd2df933de08e..f5fd3527f2be3184ac89b798f06406e05fa74281 100644 --- a/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h +++ b/frameworks/libs/distributeddb/storage/src/sqlite/relational/sqlite_relational_store.h @@ -211,7 +211,10 @@ protected: int OperateDataStatusInner(const std::vector &tables, uint64_t virtualTime) const; - void CleanDirtyLogIfNeed(const std::string &tableName) const; + void CleanDirtyLogIfNeed(const std::string &tableName, SQLiteSingleVerRelationalStorageExecutor *handle) const; + + void TrackerIntegrityRepair(const TrackerSchema &trackerSchema, const TableInfo &tableInfo, + bool isNoTableInSchema); bool IsDailyTrackerIntegrityRepair(const std::string &tableName); diff --git a/frameworks/libs/distributeddb/test/unittest/common/storage/virtual_sqlite_relational_store.cpp b/frameworks/libs/distributeddb/test/unittest/common/storage/virtual_sqlite_relational_store.cpp index f187b6e030310e635bbdd2d6bc29080001d80ff3..024b3261a8f487e620691f088b995ef015dfc637 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/storage/virtual_sqlite_relational_store.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/storage/virtual_sqlite_relational_store.cpp @@ -28,7 +28,7 @@ int VirtualSqliteRelationalStore::CallCheckTrackerTable(const TrackerSchema &tra void VirtualSqliteRelationalStore::CallCleanDirtyLogIfNeed(const std::string &tableName) const { - CleanDirtyLogIfNeed(tableName); + CleanDirtyLogIfNeed(tableName, nullptr); } RelationalSchemaObject VirtualSqliteRelationalStore::CallGetSchemaObj() const diff --git a/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_syncer_lock_test.cpp b/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_syncer_lock_test.cpp index af9b119bfee70d0ca6cbe8157ec3ead67825b9a3..a8169850f7c73dbf544ae25dcb6f846cf9864e86 100644 --- a/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_syncer_lock_test.cpp +++ b/frameworks/libs/distributeddb/test/unittest/common/syncer/cloud/distributeddb_cloud_syncer_lock_test.cpp @@ -479,7 +479,7 @@ void DistributedDBCloudSyncerLockTest::CheckRecordNotFound(bool isLocalWin) sql = "UPDATE " + ASSETS_TABLE_NAME + " SET name = id"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); } else { - UpdateCloudDBData(0, cloudCount, 0, 0, ASSETS_TABLE_NAME); + UpdateCloudDBData(0, cloudCount, 0, 0, ASSETS_TABLE_NAME); CallSync(option); } sql = "UPDATE " + DBCommon::GetLogTableName(ASSETS_TABLE_NAME) + " SET status = 1"; @@ -1656,7 +1656,7 @@ HWTEST_F(DistributedDBCloudSyncerLockTest, RecordNotFoundTest003, TestSize.Level */ std::string sql = "UPDATE " + DBCommon::GetLogTableName(ASSETS_TABLE_NAME) + " SET status = 2"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); - UpdateCloudDBData(0, cloudCount, 0, 0, ASSETS_TABLE_NAME); + UpdateCloudDBData(0, cloudCount, 0, 0, ASSETS_TABLE_NAME); CallSync(option); sql = "UPDATE " + DBCommon::GetLogTableName(ASSETS_TABLE_NAME) + " SET status = 1"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); @@ -1711,7 +1711,7 @@ HWTEST_F(DistributedDBCloudSyncerLockTest, RecordNotFoundTest004, TestSize.Level */ std::string sql = "UPDATE " + DBCommon::GetLogTableName(ASSETS_TABLE_NAME) + " SET status = 2"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK); - UpdateCloudDBData(0, cloudCount, 0, 0, ASSETS_TABLE_NAME); + UpdateCloudDBData(0, cloudCount, 0, 0, ASSETS_TABLE_NAME); CallSync(option); sql = "UPDATE " + DBCommon::GetLogTableName(ASSETS_TABLE_NAME) + " SET status = 1"; EXPECT_EQ(RelationalTestUtils::ExecSql(db, sql), SQLITE_OK);