Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implemented write transactions #50

Merged
merged 6 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions lib/src/bindings/bindings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class _ObjectBoxBindings {
int Function(Pointer<Void> txn) obx_txn_close;
int Function(Pointer<Void> txn) obx_txn_abort;
int Function(Pointer<Void> txn) obx_txn_success;
int Function(Pointer<Void> txn, int was_successful) obx_txn_mark_success;

// box management
Pointer<Void> Function(Pointer<Void> store, int entity_id) obx_box;
Expand Down Expand Up @@ -185,6 +186,7 @@ class _ObjectBoxBindings {
obx_txn_close = _fn<obx_txn_close_native_t>("obx_txn_close").asFunction();
obx_txn_abort = _fn<obx_txn_abort_native_t>("obx_txn_abort").asFunction();
obx_txn_success = _fn<obx_txn_success_native_t>("obx_txn_success").asFunction();
obx_txn_mark_success = _fn<obx_txn_mark_success_native_t>("obx_txn_mark_success").asFunction();

// box management
obx_box = _fn<obx_box_native_t>("obx_box").asFunction();
Expand Down
1 change: 1 addition & 0 deletions lib/src/bindings/signatures.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef obx_txn_read_native_t = Pointer<Void> Function(Pointer<Void> store);
typedef obx_txn_close_native_t = Int32 Function(Pointer<Void> txn);
typedef obx_txn_abort_native_t = Int32 Function(Pointer<Void> txn);
typedef obx_txn_success_native_t = Int32 Function(Pointer<Void> txn);
typedef obx_txn_mark_success_native_t = Int32 Function(Pointer<Void> txn, Uint8 wasSuccessful);

// box management
typedef obx_box_native_t = Pointer<Void> Function(Pointer<Void> store, Uint32 entity_id);
Expand Down
17 changes: 13 additions & 4 deletions lib/src/store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@ class Store {
}

/// Executes a given function inside a transaction
///
/// Returns type of [fn] if [return] is called in [fn]
R runInTransaction<R>(TxMode mode, R Function() fn) {
assert(mode == TxMode.Read, "write transactions are currently not supported"); // TODO implement

Pointer<Void> txn = bindings.obx_txn_read(_cStore);
checkObxPtr(txn, "failed to created transaction");
bool wanna_write = mode == TxMode.Write;
Pointer<Void> txn = wanna_write ? bindings.obx_txn_write(_cStore) : bindings.obx_txn_read(_cStore);
checkObxPtr(txn, "failed to create transaction");
try {
if (wanna_write) {
checkObx(bindings.obx_txn_mark_success(txn, 1));
}
return fn();
} catch (ex) {
if (wanna_write) {
checkObx(bindings.obx_txn_mark_success(txn, 0));
}
rethrow;
} finally {
checkObx(bindings.obx_txn_close(txn));
}
Expand Down
69 changes: 69 additions & 0 deletions test/box_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import 'test_env.dart';
void main() {
TestEnv env;
Box box;
Store store;

final List<TestEntity> simple_items =
["One", "Two", "Three", "Four", "Five", "Six"].map((s) => TestEntity.initText(s)).toList();

setUp(() {
env = TestEnv("box");
box = env.box;
store = env.store;
});

test(".put() returns a valid id", () {
Expand Down Expand Up @@ -168,6 +170,73 @@ void main() {
expect(removed, equals(3));
});

test("simple write in txn works", () {
int count;
write_func() {
box.putMany(simple_items);
}
store.runInTransaction(TxMode.Write, write_func);
count = box.count();
expect(count, equals(6));
});

vaind marked this conversation as resolved.
Show resolved Hide resolved
test("failing transactions", () {
try {
store.runInTransaction(TxMode.Write, () {
box.putMany(simple_items);
throw Exception("Test exception");
});
} on Exception {
; //otherwise test fails due to not handling exceptions
} finally {
expect(box.count(), equals(0));
}
});

test("recursive write in write transaction", () {
store.runInTransaction(TxMode.Write, () {
box.putMany(simple_items);
store.runInTransaction(TxMode.Write, () {
box.putMany(simple_items);
});
});
expect(box.count(), equals(12));
});

test("recursive read in write transaction", () {
int count = store.runInTransaction(TxMode.Write, () {
box.putMany(simple_items);
return store.runInTransaction(TxMode.Read, () {
return box.count();
});
});
expect(count, equals(6));
});

test("recursive write in read -> fails during creation", () {
List<int> ids;
try {
ids = store.runInTransaction(TxMode.Read, () {
box.count();
return store.runInTransaction(TxMode.Write, () {
return box.putMany(simple_items);
});
});
} on ObjectBoxException catch (ex) {
expect(ex.toString(), equals("ObjectBoxException: failed to create transaction: "));
}
});

test("failing in recursive txn", () {
store.runInTransaction(TxMode.Write, () {
//should throw code10001 -> valid until fix
List<int> ids = store.runInTransaction(TxMode.Read, () {
return box.putMany(simple_items);
});
expect(ids.length, equals(6));
});
});

tearDown(() {
env.close();
});
Expand Down