realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

After initiating Compact the db.realm.tmp_compaction_space temp file is not removed

Open GitMurf opened this issue 3 years ago • 5 comments
trafficstars

How frequently does the bug occur?

All the time

Description

When I run either a RealmDB.compact() OR do shouldCompactOnLaunch the Realm DB file compacts fine but it leaves behind the db.realm.tmp_compaction_space temporary file that was copied during the compaction process. The problem then is that the database size is always taking up twice as much space (as it is duplicated to the new realm db file).

Should this be being deleted automatically? Is there a setting or something I need to configure to have it deleted? Do I have to delete it myself?

image

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

As the description above says, simply initiate RealmDB.compact() OR shouldCompactOnLaunch. It leaves behind the copied temp database file.

Version

Current

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

Windows 11 and Mac

Build environment

No response

Cocoapods version

No response

GitMurf avatar Apr 27 '22 05:04 GitMurf

@GitMurf Thanks for reporting this.

Just to clarify, have you observed this behavior on both Win and Mac, or do they behave differently?

fronck avatar Apr 27 '22 12:04 fronck

@fronck sorry for the slow response. I tried to get my hands on a Mac to test with but wasn't able to. At this time I can only confirm this happens on Windows. Do you need me to try and find someone with a Mac to test this on or can you look into this with just the repro of Windows? Thanks much!

GitMurf avatar May 10 '22 23:05 GitMurf

@GitMurf Thanks for the update. We can try to reproduce on a Mac.

kneth avatar May 11 '22 08:05 kneth

@GitMurf I have not been able to reproduce it on a Mac.

First I have generated a Realm file with the following script:

const Realm = require("realm");

const KeyValueSchema = {
    name: "KeyValue",
    primaryKey: "key",
    properties: {
        key: "string",
        value: "int",
    },
};

const config = {
    schema: [KeyValueSchema],
};

let realm = new Realm(config);
for(let i = 0; i < 100; i++) {
    realm.write(() => {
        realm.create(KeyValueSchema.name, { key: `__ ${i} __`, value: i });
    });
}

for(let i = 0; i < 25; i++) {
    realm.write(() => {
        realm.delete(realm.objectForPrimaryKey(KeyValueSchema.name, `__ ${i} __`));
    });
}
realm.close();

The deletions are added to the script to generate some unused free space for the compaction to remove.

The compaction is done by the script:

const Realm = require("realm");

const KeyValueSchema = {
    name: "KeyValue",
    primaryKey: "key",
    properties: {
        key: "string",
        value: "int",
    },
};

const config = {
    schema: [KeyValueSchema],
    shouldCompactOnLaunch: () => true,
};

let realm = new Realm(config);
realm.close();

As the following screenshot demonstrates, the initial file is larger than the compacted line but no extra files are produced:

Screenshot 2022-05-19 at 16 53 51

During compaction, aux. files are created. On Windows, deleting a file is not atomic. Your observation can be explained if your app was terminated while cleaning up after compaction.

kneth avatar May 19 '22 14:05 kneth

@GitMurf See also https://github.com/realm/realm-core/issues/4111

kneth avatar May 19 '22 15:05 kneth

@GitMurf It will be solved in https://github.com/realm/realm-js/pull/5121

kneth avatar Nov 18 '22 15:11 kneth