Open Replica read-only when possible (#3776)

* Open Replica read-only when possible

Specifically, when either
 - the command is read-only; or
 - the command requires GC (including recurrence updates) but GC is disabled by config

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Dustin J. Mitchell 2025-02-14 04:25:19 -05:00 committed by GitHub
parent a97deb0c84
commit a701f8fc7d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 121 additions and 17 deletions

View file

@ -598,9 +598,6 @@ int Context::initialize(int argc, const char** argv) {
createDefaultConfig();
bool create_if_missing = !config.getBoolean("exit.on.missing.db");
tdb2.open_replica(data_dir, create_if_missing);
////////////////////////////////////////////////////////////////////////////
//
// [3] Instantiate Command objects and capture command entities.
@ -674,6 +671,21 @@ int Context::initialize(int argc, const char** argv) {
if (foundAssumed) header("No command specified - assuming 'information'.");
}
////////////////////////////////////////////////////////////////////////////
//
// [7.5] Open the Replica.
//
////////////////////////////////////////////////////////////////////////////
bool create_if_missing = !config.getBoolean("exit.on.missing.db");
Command* c = commands[cli2.getCommand()];
// We must allow writes if either 'gc' is enabled and the command performs GC, or the command
// itself is read-write.
bool read_write =
(config.getBoolean("gc") && (c->needs_gc() || c->needs_recur_update())) || !c->read_only();
tdb2.open_replica(data_dir, create_if_missing, read_write);
////////////////////////////////////////////////////////////////////////////
//
// [8] Initialize hooks.

View file

@ -50,10 +50,13 @@ bool TDB2::debug_mode = false;
static void dependency_scan(std::vector<Task>&);
////////////////////////////////////////////////////////////////////////////////
void TDB2::open_replica(const std::string& location, bool create_if_missing) {
_replica = tc::new_replica_on_disk(location, create_if_missing);
void TDB2::open_replica(const std::string& location, bool create_if_missing, bool read_write) {
_replica = tc::new_replica_on_disk(location, create_if_missing, read_write);
}
////////////////////////////////////////////////////////////////////////////////
void TDB2::open_replica_in_memory() { _replica = tc::new_replica_in_memory(); }
////////////////////////////////////////////////////////////////////////////////
// Add the new task to the replica.
void TDB2::add(Task& task) {
@ -190,11 +193,8 @@ void TDB2::purge(Task& task) {
////////////////////////////////////////////////////////////////////////////////
rust::Box<tc::Replica>& TDB2::replica() {
// Create a replica in-memory if `open_replica` has not been called. This
// occurs in tests.
if (!_replica) {
_replica = tc::new_replica_in_memory();
}
// One of the open_replica_ methods must be called before this one.
assert(_replica);
return _replica.value();
}

View file

@ -46,7 +46,8 @@ class TDB2 {
TDB2() = default;
void open_replica(const std::string &, bool create_if_missing);
void open_replica(const std::string &, bool create_if_missing, bool read_write);
void open_replica_in_memory();
void add(Task &);
void modify(Task &);
void purge(Task &);

View file

@ -104,8 +104,11 @@ mod ffi {
fn new_replica_in_memory() -> Result<Box<Replica>>;
/// Create a new replica stored on-disk.
fn new_replica_on_disk(taskdb_dir: String, create_if_missing: bool)
-> Result<Box<Replica>>;
fn new_replica_on_disk(
taskdb_dir: String,
create_if_missing: bool,
read_write: bool,
) -> Result<Box<Replica>>;
/// Commit the given operations to the replica.
fn commit_operations(&mut self, ops: Vec<Operation>) -> Result<()>;
@ -490,11 +493,14 @@ impl From<tc::Replica> for Replica {
fn new_replica_on_disk(
taskdb_dir: String,
create_if_missing: bool,
read_write: bool,
) -> Result<Box<Replica>, CppError> {
use tc::storage::AccessMode::*;
let access_mode = if read_write { ReadWrite } else { ReadOnly };
let storage = tc::StorageConfig::OnDisk {
taskdb_dir: PathBuf::from(taskdb_dir),
create_if_missing,
access_mode: tc::storage::AccessMode::ReadWrite,
access_mode,
}
.into_storage()?;
Ok(Box::new(tc::Replica::new(storage).into()))