From f7230697756355ba10637a5e2c5454f60ff6b4c0 Mon Sep 17 00:00:00 2001 From: jubb Date: Fri, 9 Apr 2021 14:19:48 +1000 Subject: [PATCH] fix: check actual collisions instead of not --- .../securesms/loki/database/SessionJobDatabase.kt | 8 -------- .../session/libsession/messaging/jobs/JobQueue.kt | 13 ++++++++++++- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt b/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt index bc0f896a54..dda3b7d7eb 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/loki/database/SessionJobDatabase.kt @@ -23,14 +23,6 @@ class SessionJobDatabase(context: Context, helper: SQLCipherOpenHelper) : Databa fun persistJob(job: Job) { val database = databaseHelper.writableDatabase val contentValues = ContentValues(4) - val existing = database.get(sessionJobTable, "$jobID = ?", arrayOf(job.id!!)) { cursor -> - cursor.count - } ?: 0 - // When adding multiple jobs in rapid succession, timestamps might not be good enough as a unique ID. To - // deal with this we keep track of the number of jobs with a given timestamp and that to the end of the - // timestamp to make it a unique ID. We can't use a random number because we do still want to keep track - // of the order in which the jobs were added. - job.id += existing contentValues.put(jobID, job.id) contentValues.put(jobType, job.getFactoryKey()) contentValues.put(failureCount, job.failureCount) diff --git a/libsession/src/main/java/org/session/libsession/messaging/jobs/JobQueue.kt b/libsession/src/main/java/org/session/libsession/messaging/jobs/JobQueue.kt index 77eff071b0..43cefbd13c 100644 --- a/libsession/src/main/java/org/session/libsession/messaging/jobs/JobQueue.kt +++ b/libsession/src/main/java/org/session/libsession/messaging/jobs/JobQueue.kt @@ -6,7 +6,9 @@ import kotlinx.coroutines.channels.Channel.Factory.UNLIMITED import org.session.libsession.messaging.MessagingConfiguration import org.session.libsignal.utilities.logging.Log import java.util.* +import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.Executors +import java.util.concurrent.atomic.AtomicInteger import kotlin.concurrent.schedule import kotlin.math.min import kotlin.math.pow @@ -16,6 +18,8 @@ import kotlin.math.roundToLong class JobQueue : JobDelegate { private var hasResumedPendingJobs = false // Just for debugging + private val jobTimestampMap = ConcurrentHashMap() + private val dispatcher = Executors.newSingleThreadExecutor().asCoroutineDispatcher() private val scope = GlobalScope + SupervisorJob() private val queue = Channel(UNLIMITED) @@ -44,7 +48,14 @@ class JobQueue : JobDelegate { } private fun addWithoutExecuting(job: Job) { - job.id = System.currentTimeMillis().toString() + // When adding multiple jobs in rapid succession, timestamps might not be good enough as a unique ID. To + // deal with this we keep track of the number of jobs with a given timestamp and that to the end of the + // timestamp to make it a unique ID. We can't use a random number because we do still want to keep track + // of the order in which the jobs were added. + val currentTime = System.currentTimeMillis() + jobTimestampMap.putIfAbsent(currentTime, AtomicInteger()) + job.id = jobTimestampMap[currentTime]!!.getAndIncrement().toString() + MessagingConfiguration.shared.storage.persistJob(job) }