Skip to content

Commit

Permalink
fix: sonarlint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
itsdebs committed Feb 6, 2024
1 parent 7db228a commit aa74730
Showing 1 changed file with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -266,6 +268,7 @@ void clearEventsFromDB(List<Integer> 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();
Expand Down Expand Up @@ -316,7 +319,7 @@ void getEventsFromDB(Map<Integer, Integer> messageIdStatusMap,//(id (row_id), st
}
Cursor cursor;
synchronized (DB_LOCK) {
semaphore.acquire();
acquireSemaphore();
cursor = persistence.rawQuery(selectSQL, null);
}
if (!cursor.moveToFirst()) {
Expand All @@ -342,8 +345,6 @@ void getEventsFromDB(Map<Integer, Integer> messageIdStatusMap,//(id (row_id), st
} catch (SQLiteDatabaseCorruptException ex) {
RudderLogger.logError(ex);
ReportManager.reportError(ex);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}

Expand Down Expand Up @@ -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;
Expand All @@ -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()) {
Expand Down Expand Up @@ -608,6 +614,7 @@ void markDeviceModeTransformationAndDMProcessedDone(List<Integer> rowIds) {
" WHERE " + MESSAGE_ID_COL + " IN "
+ rowIdsCSVString + ";";
synchronized (DB_LOCK) {
acquireSemaphore();
persistence.execSQL(sql);
}
}
Expand All @@ -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);
}
}
Expand All @@ -628,6 +636,7 @@ private void updateEventStatus(String rowIdsCSVString, int status) {
") WHERE " + MESSAGE_ID_COL + " IN "
+ rowIdsCSVString + ";";
synchronized (DB_LOCK) {
acquireSemaphore();
persistence.execSQL(sql);
}
}
Expand All @@ -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);
Expand Down

0 comments on commit aa74730

Please sign in to comment.