From aa74730361d0591c100812f7293982257082d5f4 Mon Sep 17 00:00:00 2001 From: Debanjan Chatterjee Date: Tue, 6 Feb 2024 12:34:46 +0530 Subject: [PATCH] fix: sonarlint issues --- .../android/sdk/core/DBPersistentManager.java | 30 ++++++++++++------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java b/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java index f5be6cc8b..cd5d7b635 100644 --- a/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java +++ b/core/src/main/java/com/rudderstack/android/sdk/core/DBPersistentManager.java @@ -16,6 +16,7 @@ import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; +import com.rudderstack.android.ruddermetricsreporterandroid.RudderReporter; import com.rudderstack.android.sdk.core.persistence.DefaultPersistenceProviderFactory; import com.rudderstack.android.sdk.core.persistence.Persistence; import com.rudderstack.android.sdk.core.persistence.PersistenceProvider; @@ -244,6 +245,7 @@ void flushEvents() { try { if (persistence.isAccessible()) { + acquireSemaphore(); String deleteSQL = String.format(Locale.US, "DELETE FROM %s", EVENTS_TABLE_NAME); RudderLogger.logDebug(String.format(Locale.US, "DBPersistentManager: flushEvents: deleteSQL: %s", deleteSQL)); synchronized (DB_LOCK) { @@ -266,6 +268,7 @@ void clearEventsFromDB(List messageIds) { try { // get writable database if (persistence.isAccessible()) { + acquireSemaphore(); RudderLogger.logInfo(String.format(Locale.US, "DBPersistentManager: clearEventsFromDB: Clearing %d messages from DB", messageIds.size())); // format CSV string from messageIds list StringBuilder builder = new StringBuilder(); @@ -316,7 +319,7 @@ void getEventsFromDB(Map messageIdStatusMap,//(id (row_id), st } Cursor cursor; synchronized (DB_LOCK) { - semaphore.acquire(); + acquireSemaphore(); cursor = persistence.rawQuery(selectSQL, null); } if (!cursor.moveToFirst()) { @@ -342,8 +345,6 @@ void getEventsFromDB(Map messageIdStatusMap,//(id (row_id), st } catch (SQLiteDatabaseCorruptException ex) { RudderLogger.logError(ex); ReportManager.reportError(ex); - } catch (InterruptedException e) { - throw new RuntimeException(e); } } @@ -508,14 +509,10 @@ private boolean checkIfColumnExists(String newColumn) { return false; } - Semaphore semaphore = new Semaphore(1); + private final Semaphore migrationSemaphore = new Semaphore(1); void checkForMigrations() { - try { - semaphore.acquire(); - } catch (InterruptedException e) { - //no-op - } + acquireSemaphore(); Runnable runnable = () -> { try { boolean isNewColumnAdded = false; @@ -536,13 +533,22 @@ void checkForMigrations() { NullPointerException ex) { RudderLogger.logError(DBPERSISTENT_MANAGER_CHECK_FOR_MIGRATIONS_TAG + ex.getLocalizedMessage()); } finally { - semaphore.release(); + migrationSemaphore.release(); } }; // Need to perform db operations on a separate thread to support strict mode. executor.execute(runnable); } + private void acquireSemaphore() { + try { + migrationSemaphore.acquire(); + } catch (InterruptedException e) { + ReportManager.reportError(e); + Thread.currentThread().interrupt(); + } + } + private void performMigration(String columnName) { try { if (persistence.isAccessible()) { @@ -608,6 +614,7 @@ void markDeviceModeTransformationAndDMProcessedDone(List rowIds) { " WHERE " + MESSAGE_ID_COL + " IN " + rowIdsCSVString + ";"; synchronized (DB_LOCK) { + acquireSemaphore(); persistence.execSQL(sql); } } @@ -617,6 +624,7 @@ public void markDeviceModeProcessedDone(Integer rowId) { DBPersistentManager.DM_PROCESSED_COL + " = " + DBPersistentManager.DM_PROCESSED_DONE + " WHERE " + MESSAGE_ID_COL + " = " + rowId + ";"; synchronized (DB_LOCK) { + acquireSemaphore(); persistence.execSQL(sql); } } @@ -628,6 +636,7 @@ private void updateEventStatus(String rowIdsCSVString, int status) { ") WHERE " + MESSAGE_ID_COL + " IN " + rowIdsCSVString + ";"; synchronized (DB_LOCK) { + acquireSemaphore(); persistence.execSQL(sql); } } @@ -639,6 +648,7 @@ void runGcForEvents() { private void deleteDoneEvents() { synchronized (DB_LOCK) { + acquireSemaphore(); persistence.delete(EVENTS_TABLE_NAME, DBPersistentManager.STATUS_COL + " = " + DBPersistentManager.STATUS_ALL_DONE, null);