Skip to content
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ public Long call()
private OfflineRoomDatabase m_db;
private StorageRecordDao m_srDao;
private StorageSettingDao m_settingDao;
private long m_pageSize = -1;
private volatile long m_pageSize = -1;
private static final long PAGE_SIZE_DEFAULT = 4096;
private static final Object PAGE_SIZE_LOCK = new Object();

public OfflineRoom(Context context, String name) {
RoomDatabase.Builder<OfflineRoomDatabase> builder;
Expand Down Expand Up @@ -290,22 +291,26 @@ public void deleteSetting(String name) {

private void initPageSize() {
if (m_pageSize == -1) {
try {
try (Cursor c = m_db.query("PRAGMA page_size", null)) {
if (c.getCount() == 1 && c.getColumnCount() == 1) {
c.moveToFirst();
m_pageSize = c.getLong(0);
} else {
synchronized (PAGE_SIZE_LOCK) {
if (m_pageSize == -1) {
try {
try (Cursor c = m_db.query("PRAGMA page_size", null)) {
if (c.getCount() == 1 && c.getColumnCount() == 1) {
c.moveToFirst();
m_pageSize = c.getLong(0);
} else {
m_pageSize = PAGE_SIZE_DEFAULT;
Log.e("MAE",
String.format("Unexpected result from PRAGMA page_size: %d rows, %d columns",
c.getCount(),
c.getColumnCount()));
}
}
} catch (Exception e) {
m_pageSize = PAGE_SIZE_DEFAULT;
Log.e("MAE",
String.format("Unexpected result from PRAGMA page_size: %d rows, %d columns",
c.getCount(),
c.getColumnCount()));
Log.e("MAE", "Failed to query PRAGMA page_size, using default page size.", e);
}
}
} catch (Exception e) {
m_pageSize = PAGE_SIZE_DEFAULT;
Log.e("MAE", "Failed to query PRAGMA page_size, using default page size.", e);
}
}
Comment on lines 291 to 315
Copy link

Copilot AI Dec 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thread-safety fix for concurrent initialization lacks test coverage. Consider adding a test that verifies thread-safe initialization by calling totalSize() or loadPageSize() concurrently from multiple threads to ensure the PRAGMA query is only executed once and that no race conditions occur.

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added ConcurrentPageSizeInitialization test that verifies thread-safe initialization by:

  • Creating 10 threads that simultaneously call loadPageSize() and totalSize()
  • Using CountDownLatch to ensure concurrent execution
  • Verifying all threads receive the same page size value (no race condition)
  • Validating no errors occur during concurrent access
  • Confirming page size is valid (positive and power of 2)

Changes in commit b140ab0.

}
Expand Down