Skip to content

Commit a53961f

Browse files
author
Krzysztof Borowy
committed
force checkpoint on pie and up
1 parent 0702af7 commit a53961f

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncLocalStorageUtil.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99

1010
import javax.annotation.Nullable;
1111

12+
import java.io.File;
1213
import java.util.Arrays;
1314
import java.util.Iterator;
1415

1516
import android.content.ContentValues;
17+
import android.content.Context;
1618
import android.database.Cursor;
1719
import android.database.sqlite.SQLiteDatabase;
20+
import android.os.Build;
1821
import android.text.TextUtils;
22+
import android.util.Log;
1923

2024
import com.facebook.react.bridge.ReadableArray;
2125

@@ -142,4 +146,36 @@ private static void deepMergeInto(JSONObject oldJSON, JSONObject newJSON)
142146
}
143147
}
144148
}
149+
/**
150+
* From Pie and up, Android started to use Write-ahead logging (WAL), instead of journal rollback
151+
* for atomic commits and rollbacks.
152+
* Basically, WAL does not write directly to the database file, rather to the supporting WAL file.
153+
* Because of that, migration to the next storage might not be successful, because the content of
154+
* RKStorage might be still in WAL file instead. Committing all data from WAL to db file is called
155+
* a "checkpoint" and is done automatically (by default) when the WAL file reaches a threshold
156+
* size of 1000 pages.
157+
* More here: https://sqlite.org/wal.html
158+
* <p>
159+
* This helper will force checkpoint on RKStorage, if Next storage file does not exists yet.
160+
*/
161+
public static void verifyAndForceSqliteCheckpoint(Context ctx) {
162+
File nextStorageFile = ctx.getDatabasePath("AsyncStorage");
163+
File currentStorageFile = ctx.getDatabasePath(ReactDatabaseSupplier.DATABASE_NAME);
164+
boolean isCheckpointRequired = Build.VERSION.SDK_INT >= Build.VERSION_CODES.P
165+
&& !nextStorageFile.exists()
166+
&& currentStorageFile.exists();
167+
if (!isCheckpointRequired) {
168+
Log.i("AsyncStorage_Next", "SQLite checkpoint not required.");
169+
return;
170+
}
171+
172+
try {
173+
ReactDatabaseSupplier supplier = ReactDatabaseSupplier.getInstance(ctx);
174+
supplier.get().execSQL("PRAGMA wal_checkpoint");
175+
supplier.closeDatabase();
176+
Log.i("AsyncStorage_Next", "Forcing SQLite checkpoint successful.");
177+
} catch (Exception e) {
178+
Log.w("AsyncStorage_Next", "Could not force checkpoint on RKStorage: " + e.getMessage());
179+
}
180+
}
145181
}

android/src/main/java/com/reactnativecommunity/asyncstorage/AsyncStoragePackage.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
/**
22
* Copyright (c) Facebook, Inc. and its affiliates.
3-
*
3+
* <p>
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*/
77

88
package com.reactnativecommunity.asyncstorage;
99

1010
import android.util.Log;
11+
1112
import com.facebook.react.ReactPackage;
1213
import com.facebook.react.bridge.JavaScriptModule;
1314
import com.facebook.react.bridge.NativeModule;
1415
import com.facebook.react.bridge.ReactApplicationContext;
1516
import com.facebook.react.bridge.ReactContext;
1617
import com.facebook.react.uimanager.ViewManager;
18+
1719
import java.util.ArrayList;
1820
import java.util.Collections;
1921
import java.util.List;
@@ -29,6 +31,7 @@ public List<NativeModule> createNativeModules(ReactApplicationContext reactConte
2931
Class storageClass = Class.forName("com.reactnativecommunity.asyncstorage.next.StorageModule");
3032
NativeModule inst = (NativeModule) storageClass.getDeclaredConstructor(new Class[]{ReactContext.class}).newInstance(reactContext);
3133
moduleList.add(inst);
34+
AsyncLocalStorageUtil.verifyAndForceSqliteCheckpoint(reactContext);
3235
} catch (Exception e) {
3336
String message = "Something went wrong when initializing module:"
3437
+ "\n"

0 commit comments

Comments
 (0)