Skip to content

Commit

Permalink
Fix synchronization
Browse files Browse the repository at this point in the history
  • Loading branch information
Woohyeok Choi committed Feb 6, 2020
1 parent c9aba35 commit 0ad404f
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 98 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ android {
applicationId 'kaist.iclab.abclogger'
minSdkVersion 23
targetSdkVersion 29
versionCode 16
versionName "0.9.6-f"
versionCode 17
versionName "0.9.7"
setProperty('archivesBaseName', applicationId + "-v" + versionName)
buildConfigField("String", "DB_NAME", "\"abc-logger\"")
buildConfigField("String", "PREF_NAME", "\"abc-logger-pref\"")
Expand Down
40 changes: 20 additions & 20 deletions app/objectbox-models/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
},
{
"id": "7:3036374616977976360",
"indexId": "1:158062895926330245",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -83,7 +82,6 @@
},
{
"id": "7:9166426228699522215",
"indexId": "2:256201055211819376",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -144,7 +142,6 @@
},
{
"id": "7:5483891281740835030",
"indexId": "3:6415803526992157091",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -189,7 +186,6 @@
},
{
"id": "7:8554306890949689513",
"indexId": "4:6115229358585485744",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -254,7 +250,6 @@
},
{
"id": "7:3037623066580555738",
"indexId": "5:5988685338798823253",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -311,7 +306,6 @@
},
{
"id": "7:6227451213385940735",
"indexId": "6:3657864108008906612",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -348,7 +342,6 @@
},
{
"id": "7:859617273231228374",
"indexId": "7:2042024887155835266",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -405,7 +398,6 @@
},
{
"id": "7:950647487466205370",
"indexId": "8:8326708012163642480",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -458,7 +450,6 @@
},
{
"id": "7:8956753562347954950",
"indexId": "9:9155900179125864887",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -495,7 +486,6 @@
},
{
"id": "7:2244412886878113093",
"indexId": "10:539979283176053442",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -552,7 +542,6 @@
},
{
"id": "7:6938767616159991922",
"indexId": "11:6497556530959105106",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -629,7 +618,6 @@
},
{
"id": "7:4273578758557398982",
"indexId": "12:6039103646252155121",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -670,7 +658,6 @@
},
{
"id": "7:4541625880572395911",
"indexId": "13:5537872971623978248",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -711,7 +698,6 @@
},
{
"id": "7:6025001404903907859",
"indexId": "16:7250749398665699724",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -768,7 +754,6 @@
},
{
"id": "7:929324376506956077",
"indexId": "17:6738167932301395690",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -833,7 +818,6 @@
},
{
"id": "7:3315443384237767477",
"indexId": "19:5958550830631192722",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -882,7 +866,6 @@
},
{
"id": "7:8478344564654657002",
"indexId": "21:3208699785279838195",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -959,7 +942,6 @@
},
{
"id": "7:4235451395354468046",
"indexId": "22:7728963676690170246",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -1020,7 +1002,6 @@
},
{
"id": "6:6584685521572670325",
"indexId": "23:8572453880263911372",
"name": "isUploaded"
},
{
Expand Down Expand Up @@ -1059,7 +1040,26 @@
3186997738512980675,
6303027327249555250,
7327424447267581296,
3064297410742851827
3064297410742851827,
3208699785279838195,
8572453880263911372,
8326708012163642480,
5958550830631192722,
539979283176053442,
256201055211819376,
6039103646252155121,
6415803526992157091,
6497556530959105106,
7728963676690170246,
3657864108008906612,
2042024887155835266,
6115229358585485744,
158062895926330245,
5988685338798823253,
9155900179125864887,
5537872971623978248,
7250749398665699724,
6738167932301395690
],
"retiredPropertyUids": [
1683179073586285052,
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/kotlin/kaist/iclab/abclogger/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class App : Application(){
}
GlobalScope.launch {
ABC.bind(this@App)

// Debug.generateEntities(50000)
}
}

Expand Down
11 changes: 10 additions & 1 deletion app/src/main/kotlin/kaist/iclab/abclogger/Debug.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package kaist.iclab.abclogger

import android.util.Log
import kaist.iclab.abclogger.collector.activity.PhysicalActivityTransitionEntity
import kaist.iclab.abclogger.collector.fill
import kaist.iclab.abclogger.collector.survey.SurveyEntity
import java.util.concurrent.TimeUnit

Expand Down Expand Up @@ -163,6 +165,13 @@ object Debug {
}
})*/

suspend fun generateEntities(size: Int) {
(0..size).map {
PhysicalActivityTransitionEntity(type = "ASDFASDFASDFASDFASDFASDFASDF", isEntered = true).fill(System.currentTimeMillis())
}.let { ObjBox.put(it) }
Log.d("ZXCV", "${ObjBox.boxFor<PhysicalActivityTransitionEntity>()?.count()}")
}

suspend fun generateSurveyEntities (size: Int) {
val time = System.currentTimeMillis()
(0..size).map {
Expand All @@ -173,7 +182,7 @@ object Debug {
timeoutPolicy = "DISABLED",
deliveredTime = time + it * TimeUnit.MINUTES.toMillis(5),
json = testJson
)
).fill(System.currentTimeMillis())
}.let { ObjBox.put(it) }

val query = ObjBox.boxFor<SurveyEntity>()?.query()?.build() ?: return
Expand Down
20 changes: 14 additions & 6 deletions app/src/main/kotlin/kaist/iclab/abclogger/ObjBox.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import androidx.core.app.NotificationManagerCompat
import io.objectbox.BoxStore
import io.objectbox.kotlin.boxFor
import kaist.iclab.abclogger.collector.Base
import kaist.iclab.abclogger.collector.MyObjectBox
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand Down Expand Up @@ -90,41 +91,48 @@ object ObjBox {

fun maxSizeInBytes() = Prefs.maxDbSize * 1000L

inline fun <reified T : Any> boxFor() = try { boxStore.get()?.boxFor<T>() } catch (e: Exception) { null }
inline fun <reified T : Base> boxFor() = try { boxStore.get()?.boxFor<T>() } catch (e: Exception) { null }

fun <T : Any> boxFor(clazz: Class<T>) = try { boxStore.get()?.boxFor(clazz) } catch (e: Exception) { null }
fun <T : Base> boxFor(clazz: Class<T>) = try { boxStore.get()?.boxFor(clazz) } catch (e: Exception) { null }

inline fun <reified T : Any> putSync(entity: T?): Long {
inline fun <reified T : Base> putSync(entity: T?): Long {
entity ?: return -1L
if (boxStore.get()?.isClosed != false) return -1

if (BuildConfig.DEBUG) AppLog.d(T::class.java.name, entity)
return try { boxFor<T>()?.put(entity) } catch (e: Exception) { null } ?: -1
}

inline fun <reified T : Any> putSync(entities: Collection<T>?) {
inline fun <reified T : Base> putSync(entities: Collection<T>?) {
if (entities.isNullOrEmpty()) return
if (boxStore.get()?.isClosed != false) return

if (BuildConfig.DEBUG) AppLog.d(T::class.java.name, entities)
try { boxFor<T>()?.put(entities) } catch (e: Exception) { }
}

suspend inline fun <reified T : Any> put(entity: T?): Long = withContext<Long>(Dispatchers.IO) {
suspend inline fun <reified T : Base> put(entity: T?): Long = withContext<Long>(Dispatchers.IO) {
entity ?: return@withContext -1
if (boxStore.get()?.isClosed != false) return@withContext -1

if (BuildConfig.DEBUG) AppLog.d(T::class.java.name, entity)
return@withContext try { boxFor<T>()?.put(entity) } catch (e: Exception) { null } ?: -1
}

suspend inline fun <reified T : Any> put(entities: Collection<T>?) = withContext(Dispatchers.IO) {
suspend inline fun <reified T : Base> put(entities: Collection<T>?) = withContext(Dispatchers.IO) {
if (entities.isNullOrEmpty()) return@withContext
if (boxStore.get()?.isClosed != false) return@withContext

if (BuildConfig.DEBUG) AppLog.d(T::class.java.name, entities)
try { boxFor<T>()?.put(entities) } catch (e: Exception) { }
}

suspend inline fun <reified T : Base> removeByKeys(ids: Collection<Long>?) = withContext(Dispatchers.IO) {
if (ids.isNullOrEmpty()) return@withContext
if (boxStore.get()?.isClosed != false) return@withContext

try { boxFor<T>()?.removeByKeys(ids) } catch (e: Exception) { }
}
}


Expand Down
Loading

0 comments on commit 0ad404f

Please sign in to comment.