Skip to content

Commit

Permalink
refactor orma and use WAL mode
Browse files Browse the repository at this point in the history
  • Loading branch information
zoff99 committed Jan 22, 2025
1 parent b2763fb commit ed83df4
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
23 changes: 18 additions & 5 deletions src/main/java/com/zoffcc/applications/sorm/OrmaDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import java.util.List;
import java.util.concurrent.Semaphore;

import static com.zoffcc.applications.trifa.MainActivity.PREF__database_files_dir;

public class OrmaDatabase
{
private static final String TAG = "trifa.OrmaDatabase";
Expand All @@ -25,8 +23,15 @@ public class OrmaDatabase
static int current_db_version = 0;
static Semaphore orma_semaphore_lastrowid_on_insert = new Semaphore(1);

public OrmaDatabase()
private static String db_file_path = null;
private static String secrect_key = null;
private static boolean wal_mode = true;

public OrmaDatabase(final String db_file_path, final String secrect_key, boolean wal_mode)
{
OrmaDatabase.db_file_path = db_file_path;
OrmaDatabase.secrect_key = secrect_key;
OrmaDatabase.wal_mode = wal_mode;
}

public static Connection getSqldb()
Expand Down Expand Up @@ -616,21 +621,29 @@ public static void shutdown()
Log.i(TAG, "SHUTDOWN:finished");
}

public static void init()
public void init()
{
Log.i(TAG, "INIT:start");
// create a database connection
try
{
// Class.forName("org.sqlite.JDBC");
sqldb = DriverManager.getConnection("jdbc:sqlite:" + PREF__database_files_dir + "/main.db");
sqldb = DriverManager.getConnection("jdbc:sqlite:" + OrmaDatabase.db_file_path);
}
catch (Exception e)
{
e.printStackTrace();
Log.i(TAG, "INIT:R_Error:" + e.getMessage());
}

if (OrmaDatabase.wal_mode)
{
// set WAL mode
final String set_wal_mode = "PRAGMA journal_mode = WAL;";
run_multi_sql(set_wal_mode);
Log.i(TAG, "INIT:setting WAL mode");
}

Log.i(TAG, "loaded:sqlite:" + get_current_sqlite_version());

// --------------- CREATE THE DATABASE ---------------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ class MainActivity
var PREF__local_discovery_enabled = 1
var PREF__ipv6_enabled = 1
var PREF__force_udp_only = 0
@JvmStatic var PREF__DB_wal_mode = true
@JvmStatic var DB_PREF__open_files_directly = false
@JvmStatic var DB_PREF__notifications_active = true
@JvmStatic var DB_PREF__windows_audio_in_source = "Microphone"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.zoffcc.applications.trifa.HelperMessage.update_message_in_db_messagei
import com.zoffcc.applications.trifa.HelperMessage.update_message_in_db_no_read_recvedts
import com.zoffcc.applications.trifa.HelperMessage.update_message_in_db_resend_count
import com.zoffcc.applications.trifa.MainActivity.Companion.DB_PREF__send_push_notifications
import com.zoffcc.applications.trifa.MainActivity.Companion.PREF__DB_wal_mode
import com.zoffcc.applications.trifa.MainActivity.Companion.PREF__database_files_dir
import com.zoffcc.applications.trifa.MainActivity.Companion.add_tcp_relay_single_wrapper
import com.zoffcc.applications.trifa.MainActivity.Companion.audio_queue_play_trigger
import com.zoffcc.applications.trifa.MainActivity.Companion.bootstrap_single_wrapper
Expand Down Expand Up @@ -101,9 +103,9 @@ class TrifaToxService
{
}

com.zoffcc.applications.sorm.OrmaDatabase.init()
orma = OrmaDatabase(PREF__database_files_dir + "/main.db", "", PREF__DB_wal_mode);
orma!!.init()
// ------ correct startup order ------
orma = com.zoffcc.applications.sorm.OrmaDatabase()
globalstore.setOrmaRunning(true)
load_db_prefs()
try {
Expand Down Expand Up @@ -397,7 +399,7 @@ class TrifaToxService

globalstore.setOrmaRunning(false)
orma = null
com.zoffcc.applications.sorm.OrmaDatabase.shutdown()
OrmaDatabase.shutdown()
unlock_data_dir_input()
try
{
Expand Down

0 comments on commit ed83df4

Please sign in to comment.