Uploaded image for project: 'Realm Kotlin'
  1. Realm Kotlin
  2. RKOTLIN-806

compactOnLaunch does not compact when it should

      How frequently does the bug occur?

      Sometimes

      Description

      I have a database with a very limited amount of used bytes, although the overall file size is well over 50 MB. I expected this database to compacted on launch by using RealmConfiguration.Builder(....).compactOnLaunch(), but that didn't happen.

      Checking the documentation of DEFAULT_COMPACT_ON_LAUNCH_CALLBACK, I see this comment:

      > The default implementation for determining if a file should be compacted or not. This implementation will only trigger if the file is above 50 MB and 50% or more of the space can be reclaimed.

      But it seems that the current implementation is checking if less than 50% can be reclaimed, so the other way around than described in the comment.

      Adding the last assertion to the unit test confirms this:

      @Test
      fun `doCompact test`() = runTest {
          assertEquals(false, LocalStorageInitializer.doCompact(50 * 1024 * 1024, 40 * 1024 *1024)) // not > 50MB and < 50% reclaimable -> should be false
          assertEquals(false, LocalStorageInitializer.doCompact(50 * 1024 * 1024 + 8, 25 * 1024 * 1024)) // > 50MB and > 50% reclaimable -> should be true
          assertEquals(false, LocalStorageInitializer.doCompact(50 * 1024 * 1024 + 8, 25 * 1024 * 1024 + 3)) // > 50MB and > 50% reclaimable -> should be true
          assertEquals(true, LocalStorageInitializer.doCompact(50 * 1024 * 1024 + 8, 25 * 1024 * 1024 + 4)) // > 50MB and >= 50% reclaimable -> should be true
          assertEquals(true, LocalStorageInitializer.doCompact(50 * 1024 * 1024 + 8, 25 * 1024 * 1024 + 5)) // > 50MB and < 50% reclaimable -> should be false
      
          // adding the following test, more than 50MB filesize and 25 bytes used, should be compacted
          assertEquals(true, LocalStorageInitializer.doCompact(50 * 1024 * 1024 + 8, 25)) // > 50MB and > 50% reclaimable -> should be true
      }
      

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      N/A
      

      Can you reproduce the bug?

      Always

      Reproduction Steps

      Use the unit test doCompact test, that currently exists in the codebase.

      Version

      all versions since https://github.com/realm/realm-kotlin/commit/10acd38d4c3a8dae6c3ed9ba27aeb709df4965bd

      What Atlas App Services are you using?

      Local Database only

      Are you using encryption?

      No

      Platform OS and version(s)

      Android 10+

      Build environment

      Android Studio version: 2022.2.1
      Android Build Tools version: 33.0.2
      Gradle version: 7.6.1

            Assignee:
            clemente.tort@mongodb.com Clemente Tort Barbero
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: