From 38584d28ca38bb16f3fff3a4f085685ff39c0ee3 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Fri, 5 Feb 2021 14:40:59 +0100 Subject: [PATCH 1/5] move (most of the) implementations depending on native code to native/ --- objectbox/lib/src/common.dart | 21 ------------------ .../src/{ => native}/bindings/bindings.dart | 0 .../{ => native}/bindings/data_visitor.dart | 0 .../{ => native}/bindings/flatbuffers.dart | 0 .../src/{ => native}/bindings/helpers.dart | 0 .../{ => native}/bindings/objectbox-c.dart | 0 .../lib/src/{ => native}/bindings/objectbox.h | 0 .../src/{ => native}/bindings/structs.dart | 0 objectbox/lib/src/{ => native}/box.dart | 22 +++++++++---------- objectbox/lib/src/{ => native}/model.dart | 0 .../lib/src/{ => native}/observable.dart | 0 .../lib/src/{ => native}/query/builder.dart | 0 .../lib/src/{ => native}/query/property.dart | 0 .../lib/src/{ => native}/query/query.dart | 0 objectbox/lib/src/{ => native}/store.dart | 0 objectbox/lib/src/{ => native}/sync.dart | 0 .../lib/src/{ => native}/transaction.dart | 0 objectbox/lib/src/native/version.dart | 22 +++++++++++++++++++ 18 files changed, 33 insertions(+), 32 deletions(-) rename objectbox/lib/src/{ => native}/bindings/bindings.dart (100%) rename objectbox/lib/src/{ => native}/bindings/data_visitor.dart (100%) rename objectbox/lib/src/{ => native}/bindings/flatbuffers.dart (100%) rename objectbox/lib/src/{ => native}/bindings/helpers.dart (100%) rename objectbox/lib/src/{ => native}/bindings/objectbox-c.dart (100%) rename objectbox/lib/src/{ => native}/bindings/objectbox.h (100%) rename objectbox/lib/src/{ => native}/bindings/structs.dart (100%) rename objectbox/lib/src/{ => native}/box.dart (96%) rename objectbox/lib/src/{ => native}/model.dart (100%) rename objectbox/lib/src/{ => native}/observable.dart (100%) rename objectbox/lib/src/{ => native}/query/builder.dart (100%) rename objectbox/lib/src/{ => native}/query/property.dart (100%) rename objectbox/lib/src/{ => native}/query/query.dart (100%) rename objectbox/lib/src/{ => native}/store.dart (100%) rename objectbox/lib/src/{ => native}/sync.dart (100%) rename objectbox/lib/src/{ => native}/transaction.dart (100%) create mode 100644 objectbox/lib/src/native/version.dart diff --git a/objectbox/lib/src/common.dart b/objectbox/lib/src/common.dart index 29d5825dd..36d4f5ac0 100644 --- a/objectbox/lib/src/common.dart +++ b/objectbox/lib/src/common.dart @@ -1,8 +1,3 @@ -import 'dart:ffi'; - -import 'package:ffi/ffi.dart' show allocate, free; - -import 'bindings/bindings.dart'; // TODO use pub_semver? /// Wrapper for a semantic version information. @@ -23,22 +18,6 @@ class Version { String toString() => '$major.$minor.$patch'; } -/// Returns the underlying ObjectBox-C library version. -Version nativeLibraryVersion() { - var majorPtr = allocate(), - minorPtr = allocate(), - patchPtr = allocate(); - - try { - C.version(majorPtr, minorPtr, patchPtr); - return Version(majorPtr.value, minorPtr.value, patchPtr.value); - } finally { - free(majorPtr); - free(minorPtr); - free(patchPtr); - } -} - /// ObjectBox native exception wrapper. class ObjectBoxException implements Exception { /// Dart message related to this native error. diff --git a/objectbox/lib/src/bindings/bindings.dart b/objectbox/lib/src/native/bindings/bindings.dart similarity index 100% rename from objectbox/lib/src/bindings/bindings.dart rename to objectbox/lib/src/native/bindings/bindings.dart diff --git a/objectbox/lib/src/bindings/data_visitor.dart b/objectbox/lib/src/native/bindings/data_visitor.dart similarity index 100% rename from objectbox/lib/src/bindings/data_visitor.dart rename to objectbox/lib/src/native/bindings/data_visitor.dart diff --git a/objectbox/lib/src/bindings/flatbuffers.dart b/objectbox/lib/src/native/bindings/flatbuffers.dart similarity index 100% rename from objectbox/lib/src/bindings/flatbuffers.dart rename to objectbox/lib/src/native/bindings/flatbuffers.dart diff --git a/objectbox/lib/src/bindings/helpers.dart b/objectbox/lib/src/native/bindings/helpers.dart similarity index 100% rename from objectbox/lib/src/bindings/helpers.dart rename to objectbox/lib/src/native/bindings/helpers.dart diff --git a/objectbox/lib/src/bindings/objectbox-c.dart b/objectbox/lib/src/native/bindings/objectbox-c.dart similarity index 100% rename from objectbox/lib/src/bindings/objectbox-c.dart rename to objectbox/lib/src/native/bindings/objectbox-c.dart diff --git a/objectbox/lib/src/bindings/objectbox.h b/objectbox/lib/src/native/bindings/objectbox.h similarity index 100% rename from objectbox/lib/src/bindings/objectbox.h rename to objectbox/lib/src/native/bindings/objectbox.h diff --git a/objectbox/lib/src/bindings/structs.dart b/objectbox/lib/src/native/bindings/structs.dart similarity index 100% rename from objectbox/lib/src/bindings/structs.dart rename to objectbox/lib/src/native/bindings/structs.dart diff --git a/objectbox/lib/src/box.dart b/objectbox/lib/src/native/box.dart similarity index 96% rename from objectbox/lib/src/box.dart rename to objectbox/lib/src/native/box.dart index 49072063e..013cc7be1 100644 --- a/objectbox/lib/src/box.dart +++ b/objectbox/lib/src/native/box.dart @@ -2,17 +2,17 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart' show allocate, free; -import 'bindings/bindings.dart'; -import 'bindings/flatbuffers.dart'; -import 'bindings/helpers.dart'; -import 'bindings/structs.dart'; -import 'modelinfo/index.dart'; -import 'query/query.dart'; -import 'relations/info.dart'; -import 'relations/to_many.dart'; -import 'relations/to_one.dart'; -import 'store.dart'; -import 'transaction.dart'; +import '../bindings/bindings.dart'; +import '../bindings/flatbuffers.dart'; +import '../bindings/helpers.dart'; +import '../bindings/structs.dart'; +import '../modelinfo/index.dart'; +import '../query/query.dart'; +import '../relations/info.dart'; +import '../relations/to_many.dart'; +import '../relations/to_one.dart'; +import '../store.dart'; +import '../transaction.dart'; /// Box put (write) mode. enum PutMode { diff --git a/objectbox/lib/src/model.dart b/objectbox/lib/src/native/model.dart similarity index 100% rename from objectbox/lib/src/model.dart rename to objectbox/lib/src/native/model.dart diff --git a/objectbox/lib/src/observable.dart b/objectbox/lib/src/native/observable.dart similarity index 100% rename from objectbox/lib/src/observable.dart rename to objectbox/lib/src/native/observable.dart diff --git a/objectbox/lib/src/query/builder.dart b/objectbox/lib/src/native/query/builder.dart similarity index 100% rename from objectbox/lib/src/query/builder.dart rename to objectbox/lib/src/native/query/builder.dart diff --git a/objectbox/lib/src/query/property.dart b/objectbox/lib/src/native/query/property.dart similarity index 100% rename from objectbox/lib/src/query/property.dart rename to objectbox/lib/src/native/query/property.dart diff --git a/objectbox/lib/src/query/query.dart b/objectbox/lib/src/native/query/query.dart similarity index 100% rename from objectbox/lib/src/query/query.dart rename to objectbox/lib/src/native/query/query.dart diff --git a/objectbox/lib/src/store.dart b/objectbox/lib/src/native/store.dart similarity index 100% rename from objectbox/lib/src/store.dart rename to objectbox/lib/src/native/store.dart diff --git a/objectbox/lib/src/sync.dart b/objectbox/lib/src/native/sync.dart similarity index 100% rename from objectbox/lib/src/sync.dart rename to objectbox/lib/src/native/sync.dart diff --git a/objectbox/lib/src/transaction.dart b/objectbox/lib/src/native/transaction.dart similarity index 100% rename from objectbox/lib/src/transaction.dart rename to objectbox/lib/src/native/transaction.dart diff --git a/objectbox/lib/src/native/version.dart b/objectbox/lib/src/native/version.dart new file mode 100644 index 000000000..1719e5826 --- /dev/null +++ b/objectbox/lib/src/native/version.dart @@ -0,0 +1,22 @@ +import 'dart:ffi'; + +import 'package:ffi/ffi.dart' show allocate, free; + +import 'bindings/bindings.dart'; +import '../common.dart'; + +/// Returns the underlying ObjectBox-C library version. +Version libraryVersion() { + var majorPtr = allocate(), + minorPtr = allocate(), + patchPtr = allocate(); + + try { + C.version(majorPtr, minorPtr, patchPtr); + return Version(majorPtr.value, minorPtr.value, patchPtr.value); + } finally { + free(majorPtr); + free(minorPtr); + free(patchPtr); + } +} From 7997eea5a538cf4491711e92c70393d56a952058 Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Fri, 5 Feb 2021 15:51:24 +0100 Subject: [PATCH 2/5] add native/web conditional exports & fix import paths --- generator/integration-tests/basics/1.dart | 2 +- generator/integration-tests/indexes/1.dart | 2 +- generator/lib/src/code_chunks.dart | 5 ++++- generator/lib/src/entity_resolver.dart | 6 ++++-- objectbox/lib/integration_test.dart | 4 ++-- objectbox/lib/internal.dart | 2 +- objectbox/lib/objectbox.dart | 5 ++--- objectbox/lib/src/box.dart | 2 ++ objectbox/lib/src/model.dart | 2 ++ objectbox/lib/src/modelinfo/modelentity.dart | 4 +++- .../lib/src/modelinfo/modelproperty.dart | 6 ++++-- .../lib/src/native/bindings/data_visitor.dart | 2 +- .../lib/src/native/bindings/flatbuffers.dart | 2 +- .../lib/src/native/bindings/helpers.dart | 6 +++--- .../lib/src/native/bindings/structs.dart | 2 +- objectbox/lib/src/native/box.dart | 12 ++++++------ objectbox/lib/src/native/model.dart | 4 ++-- objectbox/lib/src/native/observable.dart | 2 +- objectbox/lib/src/native/query/query.dart | 7 +++---- objectbox/lib/src/native/store.dart | 7 ++++--- objectbox/lib/src/native/sync.dart | 2 +- objectbox/lib/src/native/transaction.dart | 19 ++----------------- objectbox/lib/src/native/version.dart | 2 +- objectbox/lib/src/observable.dart | 2 ++ objectbox/lib/src/query.dart | 2 ++ objectbox/lib/src/relations/to_many.dart | 8 +++++--- objectbox/lib/src/store.dart | 17 +++++++++++++++++ objectbox/lib/src/sync.dart | 2 ++ objectbox/lib/src/web/box.dart | 0 objectbox/lib/src/web/model.dart | 0 objectbox/lib/src/web/observable.dart | 0 objectbox/lib/src/web/query.dart | 0 objectbox/lib/src/web/store.dart | 0 objectbox/lib/src/web/sync.dart | 0 objectbox/test/basics_test.dart | 4 ++-- objectbox/test/flatbuffers_test.dart | 2 +- objectbox/test/observer_test.dart | 2 +- objectbox/test/sync_test.dart | 2 +- 38 files changed, 85 insertions(+), 63 deletions(-) create mode 100644 objectbox/lib/src/box.dart create mode 100644 objectbox/lib/src/model.dart create mode 100644 objectbox/lib/src/observable.dart create mode 100644 objectbox/lib/src/query.dart create mode 100644 objectbox/lib/src/store.dart create mode 100644 objectbox/lib/src/sync.dart create mode 100644 objectbox/lib/src/web/box.dart create mode 100644 objectbox/lib/src/web/model.dart create mode 100644 objectbox/lib/src/web/observable.dart create mode 100644 objectbox/lib/src/web/query.dart create mode 100644 objectbox/lib/src/web/store.dart create mode 100644 objectbox/lib/src/web/sync.dart diff --git a/generator/integration-tests/basics/1.dart b/generator/integration-tests/basics/1.dart index 6e5f23d74..048a3e9eb 100644 --- a/generator/integration-tests/basics/1.dart +++ b/generator/integration-tests/basics/1.dart @@ -4,7 +4,7 @@ import 'lib/objectbox.g.dart'; import 'package:test/test.dart'; import '../test_env.dart'; import '../common.dart'; -import 'package:objectbox/src/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; void main() { TestEnv env; diff --git a/generator/integration-tests/indexes/1.dart b/generator/integration-tests/indexes/1.dart index fe84b5a31..980e0bb7e 100644 --- a/generator/integration-tests/indexes/1.dart +++ b/generator/integration-tests/indexes/1.dart @@ -6,7 +6,7 @@ import 'lib/objectbox.g.dart'; import 'package:test/test.dart'; import '../test_env.dart'; import '../common.dart'; -import 'package:objectbox/src/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; void main() { TestEnv env; diff --git a/generator/lib/src/code_chunks.dart b/generator/lib/src/code_chunks.dart index 2279880df..6608c550b 100644 --- a/generator/lib/src/code_chunks.dart +++ b/generator/lib/src/code_chunks.dart @@ -1,6 +1,9 @@ import 'dart:convert'; + +// TODO remove native imports + import 'package:objectbox/src/modelinfo/index.dart'; -import 'package:objectbox/src/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; import 'package:source_gen/source_gen.dart' show InvalidGenerationSourceError; class CodeChunks { diff --git a/generator/lib/src/entity_resolver.dart b/generator/lib/src/entity_resolver.dart index 1366cc773..ea4c88f15 100644 --- a/generator/lib/src/entity_resolver.dart +++ b/generator/lib/src/entity_resolver.dart @@ -1,14 +1,16 @@ import 'dart:async'; import 'dart:convert'; +// TODO remove native imports + import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:build/build.dart'; import 'package:objectbox/objectbox.dart'; import 'package:objectbox/internal.dart'; -import 'package:objectbox/src/bindings/bindings.dart'; -import 'package:objectbox/src/bindings/helpers.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/helpers.dart'; import 'package:objectbox/src/modelinfo/index.dart'; import 'package:source_gen/source_gen.dart'; diff --git a/objectbox/lib/integration_test.dart b/objectbox/lib/integration_test.dart index f54ced1fd..08971a025 100644 --- a/objectbox/lib/integration_test.dart +++ b/objectbox/lib/integration_test.dart @@ -1,7 +1,7 @@ library integration_test; -import './src/bindings/bindings.dart'; -import './src/bindings/helpers.dart'; +import './src/native/bindings/bindings.dart'; +import './src/native/bindings/helpers.dart'; import 'internal.dart'; // ignore_for_file: public_member_api_docs diff --git a/objectbox/lib/internal.dart b/objectbox/lib/internal.dart index 6542143aa..8fc0d8318 100644 --- a/objectbox/lib/internal.dart +++ b/objectbox/lib/internal.dart @@ -4,7 +4,7 @@ library objectbox_internal; export 'src/model.dart'; export 'src/modelinfo/index.dart'; -export 'src/query/query.dart' +export 'src/query.dart' // don't export the same things as objectbox.dart to avoid docs conflicts hide Query, diff --git a/objectbox/lib/objectbox.dart b/objectbox/lib/objectbox.dart index 75aeee343..4afc8402f 100644 --- a/objectbox/lib/objectbox.dart +++ b/objectbox/lib/objectbox.dart @@ -9,7 +9,7 @@ export 'src/annotations.dart'; export 'src/box.dart' show Box; export 'src/common.dart'; export 'src/observable.dart'; -export 'src/query/query.dart' +export 'src/query.dart' show Query, QueryBuilder, @@ -21,7 +21,6 @@ export 'src/query/query.dart' StringPropertyQuery; export 'src/relations/to_many.dart' show ToMany; export 'src/relations/to_one.dart' show ToOne; -export 'src/store.dart' show Store; +export 'src/store.dart' show Store, TxMode; export 'src/sync.dart' show Sync, SyncClient, SyncCredentials, SyncRequestUpdatesMode, SyncState; -export 'src/transaction.dart' show TxMode; diff --git a/objectbox/lib/src/box.dart b/objectbox/lib/src/box.dart new file mode 100644 index 000000000..a897ee98a --- /dev/null +++ b/objectbox/lib/src/box.dart @@ -0,0 +1,2 @@ +export 'native/box.dart' + if (dart.library.html) 'web/box.dart'; diff --git a/objectbox/lib/src/model.dart b/objectbox/lib/src/model.dart new file mode 100644 index 000000000..006d79dff --- /dev/null +++ b/objectbox/lib/src/model.dart @@ -0,0 +1,2 @@ +export 'native/model.dart' + if (dart.library.html) 'web/model.dart'; diff --git a/objectbox/lib/src/modelinfo/modelentity.dart b/objectbox/lib/src/modelinfo/modelentity.dart index 7c6ff8da1..084fe974b 100644 --- a/objectbox/lib/src/modelinfo/modelentity.dart +++ b/objectbox/lib/src/modelinfo/modelentity.dart @@ -1,4 +1,6 @@ -import '../bindings/bindings.dart'; +// TODO remove native imports + +import '../native/bindings/bindings.dart'; import 'iduid.dart'; import 'modelbacklink.dart'; import 'modelinfo.dart'; diff --git a/objectbox/lib/src/modelinfo/modelproperty.dart b/objectbox/lib/src/modelinfo/modelproperty.dart index 7cb52085a..f37cf88b9 100644 --- a/objectbox/lib/src/modelinfo/modelproperty.dart +++ b/objectbox/lib/src/modelinfo/modelproperty.dart @@ -1,5 +1,7 @@ -import '../bindings/bindings.dart'; -import '../bindings/helpers.dart'; +// TODO remove native imports + +import '../native/bindings/bindings.dart'; +import '../native/bindings/helpers.dart'; import 'iduid.dart'; import 'modelentity.dart'; diff --git a/objectbox/lib/src/native/bindings/data_visitor.dart b/objectbox/lib/src/native/bindings/data_visitor.dart index 79f39c4da..3866c9f47 100644 --- a/objectbox/lib/src/native/bindings/data_visitor.dart +++ b/objectbox/lib/src/native/bindings/data_visitor.dart @@ -2,7 +2,7 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart' show allocate, free; -import '../modelinfo/entity_definition.dart'; +import '../../modelinfo/entity_definition.dart'; import '../store.dart'; import 'bindings.dart'; diff --git a/objectbox/lib/src/native/bindings/flatbuffers.dart b/objectbox/lib/src/native/bindings/flatbuffers.dart index 658202aa3..3110abd11 100644 --- a/objectbox/lib/src/native/bindings/flatbuffers.dart +++ b/objectbox/lib/src/native/bindings/flatbuffers.dart @@ -4,7 +4,7 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart' as f; -import '../../flatbuffers/flat_buffers.dart' as fb; +import '../../../flatbuffers/flat_buffers.dart' as fb; // ignore_for_file: public_member_api_docs diff --git a/objectbox/lib/src/native/bindings/helpers.dart b/objectbox/lib/src/native/bindings/helpers.dart index e89ec2740..6be2a8818 100644 --- a/objectbox/lib/src/native/bindings/helpers.dart +++ b/objectbox/lib/src/native/bindings/helpers.dart @@ -3,9 +3,9 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart'; -import '../annotations.dart'; -import '../common.dart'; -import '../modelinfo/entity_definition.dart'; +import '../../annotations.dart'; +import '../../common.dart'; +import '../../modelinfo/entity_definition.dart'; import '../store.dart'; import 'bindings.dart'; diff --git a/objectbox/lib/src/native/bindings/structs.dart b/objectbox/lib/src/native/bindings/structs.dart index 653d63d6c..db18428b4 100644 --- a/objectbox/lib/src/native/bindings/structs.dart +++ b/objectbox/lib/src/native/bindings/structs.dart @@ -3,7 +3,7 @@ import 'dart:typed_data' show Uint8List; import 'package:ffi/ffi.dart' show allocate, free, Utf8; -import '../common.dart'; +import '../../common.dart'; import 'bindings.dart'; // ignore_for_file: public_member_api_docs diff --git a/objectbox/lib/src/native/box.dart b/objectbox/lib/src/native/box.dart index 013cc7be1..2f2215b9a 100644 --- a/objectbox/lib/src/native/box.dart +++ b/objectbox/lib/src/native/box.dart @@ -2,17 +2,17 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart' show allocate, free; -import '../bindings/bindings.dart'; -import '../bindings/flatbuffers.dart'; -import '../bindings/helpers.dart'; -import '../bindings/structs.dart'; import '../modelinfo/index.dart'; -import '../query/query.dart'; import '../relations/info.dart'; import '../relations/to_many.dart'; import '../relations/to_one.dart'; import '../store.dart'; -import '../transaction.dart'; +import 'bindings/bindings.dart'; +import 'bindings/flatbuffers.dart'; +import 'bindings/helpers.dart'; +import 'bindings/structs.dart'; +import 'query/query.dart'; +import 'transaction.dart'; /// Box put (write) mode. enum PutMode { diff --git a/objectbox/lib/src/native/model.dart b/objectbox/lib/src/native/model.dart index 1f73ef95d..f23b515c3 100644 --- a/objectbox/lib/src/native/model.dart +++ b/objectbox/lib/src/native/model.dart @@ -2,10 +2,10 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; +import '../common.dart'; +import '../modelinfo/index.dart'; import 'bindings/bindings.dart'; import 'bindings/helpers.dart'; -import 'common.dart'; -import 'modelinfo/index.dart'; // ignore_for_file: public_member_api_docs diff --git a/objectbox/lib/src/native/observable.dart b/objectbox/lib/src/native/observable.dart index 2f229f095..e927c8e63 100644 --- a/objectbox/lib/src/native/observable.dart +++ b/objectbox/lib/src/native/observable.dart @@ -1,10 +1,10 @@ import 'dart:async'; import 'dart:ffi'; +import '../util.dart'; import 'bindings/bindings.dart'; import 'query/query.dart'; import 'store.dart'; -import 'util.dart'; // ignore_for_file: non_constant_identifier_names diff --git a/objectbox/lib/src/native/query/query.dart b/objectbox/lib/src/native/query/query.dart index b8657d958..d439948de 100644 --- a/objectbox/lib/src/native/query/query.dart +++ b/objectbox/lib/src/native/query/query.dart @@ -5,14 +5,13 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart' show allocate, free, Utf8; +import '../../common.dart'; +import '../../modelinfo/entity_definition.dart'; +import '../../store.dart'; import '../bindings/bindings.dart'; import '../bindings/data_visitor.dart'; import '../bindings/helpers.dart'; import '../bindings/structs.dart'; -import '../common.dart'; -import '../modelinfo/entity_definition.dart'; -import '../store.dart'; -import '../transaction.dart'; part 'builder.dart'; part 'property.dart'; diff --git a/objectbox/lib/src/native/store.dart b/objectbox/lib/src/native/store.dart index 46601d834..b9a84e89c 100644 --- a/objectbox/lib/src/native/store.dart +++ b/objectbox/lib/src/native/store.dart @@ -2,15 +2,16 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart'; +import '../common.dart'; +import '../modelinfo/index.dart'; +import '../store.dart'; +import '../util.dart'; import 'bindings/bindings.dart'; import 'bindings/helpers.dart'; import 'box.dart'; -import 'common.dart'; import 'model.dart'; -import 'modelinfo/index.dart'; import 'sync.dart'; import 'transaction.dart'; -import 'util.dart'; /// Represents an ObjectBox database and works together with [Box] to allow /// getting and putting. diff --git a/objectbox/lib/src/native/sync.dart b/objectbox/lib/src/native/sync.dart index 0095fd8c8..12c9255d5 100644 --- a/objectbox/lib/src/native/sync.dart +++ b/objectbox/lib/src/native/sync.dart @@ -5,11 +5,11 @@ import 'dart:typed_data' show Uint8List; import 'package:ffi/ffi.dart'; import 'package:meta/meta.dart'; +import '../util.dart'; import 'bindings/bindings.dart'; import 'bindings/helpers.dart'; import 'bindings/structs.dart'; import 'store.dart'; -import 'util.dart'; /// Credentials used to authenticate a sync client against a server. class SyncCredentials { diff --git a/objectbox/lib/src/native/transaction.dart b/objectbox/lib/src/native/transaction.dart index d83eaa111..5dd03549d 100644 --- a/objectbox/lib/src/native/transaction.dart +++ b/objectbox/lib/src/native/transaction.dart @@ -1,27 +1,12 @@ import 'dart:ffi'; +import '../modelinfo/entity_definition.dart'; +import '../store.dart'; import 'bindings/bindings.dart'; import 'bindings/helpers.dart'; -import 'modelinfo/entity_definition.dart'; -import 'store.dart'; // ignore_for_file: public_member_api_docs -/// Configure transaction mode. Used with [Store.runInTransaction()]. -enum TxMode { - /// Read only transaction - trying to execute a write operation results in an - /// error. This is useful if you want to group many reads inside a single - /// transaction, e.g. to improve performance or to get a consistent view of - /// the data across multiple operations. - read, - - /// Read/Write transaction. There can be only a single write transaction at - /// any time - it holds a lock on the database. Compared to read transaction, - /// read/write transactions have much higher "cost", because they need to - /// write data to the disk at the end. - write, -} - // TODO enable annotation once meta:1.3.0 is out // @internal class Transaction { diff --git a/objectbox/lib/src/native/version.dart b/objectbox/lib/src/native/version.dart index 1719e5826..157322f1c 100644 --- a/objectbox/lib/src/native/version.dart +++ b/objectbox/lib/src/native/version.dart @@ -2,8 +2,8 @@ import 'dart:ffi'; import 'package:ffi/ffi.dart' show allocate, free; -import 'bindings/bindings.dart'; import '../common.dart'; +import 'bindings/bindings.dart'; /// Returns the underlying ObjectBox-C library version. Version libraryVersion() { diff --git a/objectbox/lib/src/observable.dart b/objectbox/lib/src/observable.dart new file mode 100644 index 000000000..98538b18e --- /dev/null +++ b/objectbox/lib/src/observable.dart @@ -0,0 +1,2 @@ +export 'native/observable.dart' + if (dart.library.html) 'web/observable.dart'; diff --git a/objectbox/lib/src/query.dart b/objectbox/lib/src/query.dart new file mode 100644 index 000000000..71bb40689 --- /dev/null +++ b/objectbox/lib/src/query.dart @@ -0,0 +1,2 @@ +export 'native/query/query.dart' + if (dart.library.html) 'web/query.dart'; diff --git a/objectbox/lib/src/relations/to_many.dart b/objectbox/lib/src/relations/to_many.dart index 6230b592d..d299d59c8 100644 --- a/objectbox/lib/src/relations/to_many.dart +++ b/objectbox/lib/src/relations/to_many.dart @@ -3,12 +3,14 @@ import 'dart:ffi'; import 'package:meta/meta.dart'; -import '../bindings/bindings.dart'; -import '../bindings/helpers.dart'; +// TODO remove native imports + import '../box.dart'; import '../modelinfo/entity_definition.dart'; +import '../native/bindings/bindings.dart'; +import '../native/bindings/helpers.dart'; +import '../native/transaction.dart'; import '../store.dart'; -import '../transaction.dart'; import 'info.dart'; /// Manages a to-many relation, an unidirectional link from a "source" entity to diff --git a/objectbox/lib/src/store.dart b/objectbox/lib/src/store.dart new file mode 100644 index 000000000..c8440a95d --- /dev/null +++ b/objectbox/lib/src/store.dart @@ -0,0 +1,17 @@ +export 'native/store.dart' + if (dart.library.html) 'web/store.dart'; + +/// Configure transaction mode. Used with [Store.runInTransaction()]. +enum TxMode { + /// Read only transaction - trying to execute a write operation results in an + /// error. This is useful if you want to group many reads inside a single + /// transaction, e.g. to improve performance or to get a consistent view of + /// the data across multiple operations. + read, + + /// Read/Write transaction. There can be only a single write transaction at + /// any time - it holds a lock on the database. Compared to read transaction, + /// read/write transactions have much higher "cost", because they need to + /// write data to the disk at the end. + write, +} diff --git a/objectbox/lib/src/sync.dart b/objectbox/lib/src/sync.dart new file mode 100644 index 000000000..9a768776f --- /dev/null +++ b/objectbox/lib/src/sync.dart @@ -0,0 +1,2 @@ +export 'native/sync.dart' + if (dart.library.html) 'web/sync.dart'; diff --git a/objectbox/lib/src/web/box.dart b/objectbox/lib/src/web/box.dart new file mode 100644 index 000000000..e69de29bb diff --git a/objectbox/lib/src/web/model.dart b/objectbox/lib/src/web/model.dart new file mode 100644 index 000000000..e69de29bb diff --git a/objectbox/lib/src/web/observable.dart b/objectbox/lib/src/web/observable.dart new file mode 100644 index 000000000..e69de29bb diff --git a/objectbox/lib/src/web/query.dart b/objectbox/lib/src/web/query.dart new file mode 100644 index 000000000..e69de29bb diff --git a/objectbox/lib/src/web/store.dart b/objectbox/lib/src/web/store.dart new file mode 100644 index 000000000..e69de29bb diff --git a/objectbox/lib/src/web/sync.dart b/objectbox/lib/src/web/sync.dart new file mode 100644 index 000000000..e69de29bb diff --git a/objectbox/test/basics_test.dart b/objectbox/test/basics_test.dart index b60f3b5c8..c882703d8 100644 --- a/objectbox/test/basics_test.dart +++ b/objectbox/test/basics_test.dart @@ -1,7 +1,7 @@ import 'dart:ffi' as ffi; import 'package:objectbox/internal.dart'; -import 'package:objectbox/src/bindings/bindings.dart'; -import 'package:objectbox/src/bindings/helpers.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/helpers.dart'; import 'package:test/test.dart'; void main() { diff --git a/objectbox/test/flatbuffers_test.dart b/objectbox/test/flatbuffers_test.dart index 2815a44e7..de46fd81e 100644 --- a/objectbox/test/flatbuffers_test.dart +++ b/objectbox/test/flatbuffers_test.dart @@ -3,7 +3,7 @@ import 'dart:typed_data'; import 'package:test/test.dart'; import 'package:flat_buffers/flat_buffers.dart' as fb_upstream; -import 'package:objectbox/src/bindings/flatbuffers.dart'; +import 'package:objectbox/src/native/bindings/flatbuffers.dart'; Uint8List addFbData(dynamic fbb) { fbb.startTable(); diff --git a/objectbox/test/observer_test.dart b/objectbox/test/observer_test.dart index 4d5eea4d0..d1a3c525f 100644 --- a/objectbox/test/observer_test.dart +++ b/objectbox/test/observer_test.dart @@ -1,6 +1,6 @@ import 'dart:ffi'; -import 'package:objectbox/src/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; import 'package:test/test.dart'; import 'entity.dart'; diff --git a/objectbox/test/sync_test.dart b/objectbox/test/sync_test.dart index f3fadf227..fb373634f 100644 --- a/objectbox/test/sync_test.dart +++ b/objectbox/test/sync_test.dart @@ -1,7 +1,7 @@ import 'dart:math'; import 'dart:typed_data'; -import 'package:objectbox/src/bindings/bindings.dart'; +import 'package:objectbox/src/native/bindings/bindings.dart'; import 'package:objectbox/objectbox.dart'; import 'package:objectbox/internal.dart'; import 'package:test/test.dart'; From b6192ee7235f8bcfb185541c57c116afc61fbc9f Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Fri, 5 Feb 2021 17:32:20 +0100 Subject: [PATCH 3/5] copy model definition enums from the generated code to avoid importing ffi; formatting --- generator/integration-tests/basics/1.dart | 1 - generator/integration-tests/indexes/1.dart | 1 - generator/lib/src/code_chunks.dart | 3 - generator/lib/src/entity_resolver.dart | 4 - objectbox/analysis_options.yaml | 3 +- objectbox/lib/integration_test.dart | 6 +- objectbox/lib/src/box.dart | 3 +- objectbox/lib/src/common.dart | 1 - objectbox/lib/src/model.dart | 3 +- objectbox/lib/src/modelinfo/enums.dart | 160 ++++++++++++++++++ objectbox/lib/src/modelinfo/index.dart | 1 + objectbox/lib/src/modelinfo/modelentity.dart | 4 +- .../lib/src/modelinfo/modelproperty.dart | 5 +- .../lib/src/native/bindings/helpers.dart | 58 ------- objectbox/lib/src/observable.dart | 3 +- objectbox/lib/src/query.dart | 3 +- objectbox/lib/src/sync.dart | 3 +- objectbox/lib/src/web/box.dart | 1 + objectbox/lib/src/web/model.dart | 1 + objectbox/lib/src/web/observable.dart | 1 + objectbox/lib/src/web/query.dart | 1 + objectbox/lib/src/web/store.dart | 1 + objectbox/lib/src/web/sync.dart | 1 + objectbox/test/sync_test.dart | 2 +- 24 files changed, 180 insertions(+), 90 deletions(-) create mode 100644 objectbox/lib/src/modelinfo/enums.dart diff --git a/generator/integration-tests/basics/1.dart b/generator/integration-tests/basics/1.dart index 048a3e9eb..34ffc3272 100644 --- a/generator/integration-tests/basics/1.dart +++ b/generator/integration-tests/basics/1.dart @@ -4,7 +4,6 @@ import 'lib/objectbox.g.dart'; import 'package:test/test.dart'; import '../test_env.dart'; import '../common.dart'; -import 'package:objectbox/src/native/bindings/bindings.dart'; void main() { TestEnv env; diff --git a/generator/integration-tests/indexes/1.dart b/generator/integration-tests/indexes/1.dart index 980e0bb7e..4047a5dbb 100644 --- a/generator/integration-tests/indexes/1.dart +++ b/generator/integration-tests/indexes/1.dart @@ -6,7 +6,6 @@ import 'lib/objectbox.g.dart'; import 'package:test/test.dart'; import '../test_env.dart'; import '../common.dart'; -import 'package:objectbox/src/native/bindings/bindings.dart'; void main() { TestEnv env; diff --git a/generator/lib/src/code_chunks.dart b/generator/lib/src/code_chunks.dart index 6608c550b..9ce1f601d 100644 --- a/generator/lib/src/code_chunks.dart +++ b/generator/lib/src/code_chunks.dart @@ -1,9 +1,6 @@ import 'dart:convert'; -// TODO remove native imports - import 'package:objectbox/src/modelinfo/index.dart'; -import 'package:objectbox/src/native/bindings/bindings.dart'; import 'package:source_gen/source_gen.dart' show InvalidGenerationSourceError; class CodeChunks { diff --git a/generator/lib/src/entity_resolver.dart b/generator/lib/src/entity_resolver.dart index ea4c88f15..938940064 100644 --- a/generator/lib/src/entity_resolver.dart +++ b/generator/lib/src/entity_resolver.dart @@ -1,16 +1,12 @@ import 'dart:async'; import 'dart:convert'; -// TODO remove native imports - import 'package:analyzer/dart/constant/value.dart'; import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/type.dart'; import 'package:build/build.dart'; import 'package:objectbox/objectbox.dart'; import 'package:objectbox/internal.dart'; -import 'package:objectbox/src/native/bindings/bindings.dart'; -import 'package:objectbox/src/native/bindings/helpers.dart'; import 'package:objectbox/src/modelinfo/index.dart'; import 'package:source_gen/source_gen.dart'; diff --git a/objectbox/analysis_options.yaml b/objectbox/analysis_options.yaml index 9a1bc5b36..a11934aa9 100644 --- a/objectbox/analysis_options.yaml +++ b/objectbox/analysis_options.yaml @@ -27,7 +27,8 @@ analyzer: - example/** - generator/** - benchmark/** - - lib/src/bindings/objectbox-c.dart + - lib/src/modelinfo/enums.dart + - lib/src/native/bindings/objectbox-c.dart - lib/flatbuffers/** # Not really our code - test/objectbox.g.dart # TODO remove exception after #168 is implemented strong-mode: diff --git a/objectbox/lib/integration_test.dart b/objectbox/lib/integration_test.dart index 08971a025..6361f8fbc 100644 --- a/objectbox/lib/integration_test.dart +++ b/objectbox/lib/integration_test.dart @@ -1,7 +1,7 @@ library integration_test; -import './src/native/bindings/bindings.dart'; -import './src/native/bindings/helpers.dart'; +import './src/native/bindings/bindings.dart' as native; +import './src/native/bindings/helpers.dart' as native; import 'internal.dart'; // ignore_for_file: public_member_api_docs @@ -31,6 +31,6 @@ class IntegrationTest { modelInfo.validate(); final model = Model(modelInfo); - checkObx(C.model_free(model.ptr)); + native.checkObx(native.C.model_free(model.ptr)); } } diff --git a/objectbox/lib/src/box.dart b/objectbox/lib/src/box.dart index a897ee98a..05ac67c16 100644 --- a/objectbox/lib/src/box.dart +++ b/objectbox/lib/src/box.dart @@ -1,2 +1 @@ -export 'native/box.dart' - if (dart.library.html) 'web/box.dart'; +export 'native/box.dart' if (dart.library.html) 'web/box.dart'; diff --git a/objectbox/lib/src/common.dart b/objectbox/lib/src/common.dart index 36d4f5ac0..fd3674034 100644 --- a/objectbox/lib/src/common.dart +++ b/objectbox/lib/src/common.dart @@ -1,4 +1,3 @@ - // TODO use pub_semver? /// Wrapper for a semantic version information. class Version { diff --git a/objectbox/lib/src/model.dart b/objectbox/lib/src/model.dart index 006d79dff..41d940590 100644 --- a/objectbox/lib/src/model.dart +++ b/objectbox/lib/src/model.dart @@ -1,2 +1 @@ -export 'native/model.dart' - if (dart.library.html) 'web/model.dart'; +export 'native/model.dart' if (dart.library.html) 'web/model.dart'; diff --git a/objectbox/lib/src/modelinfo/enums.dart b/objectbox/lib/src/modelinfo/enums.dart new file mode 100644 index 000000000..97510e0fa --- /dev/null +++ b/objectbox/lib/src/modelinfo/enums.dart @@ -0,0 +1,160 @@ +// Note: the enums in this file are copied from native/bindings/objectbox-c.dart +// to avoid package:ffi import which would break compatibility with web. + +import '../annotations.dart'; + +/// Maps [OBXPropertyType] to its string representation (name). +String obxPropertyTypeToString(int type) { + switch (type) { + case OBXPropertyType.Bool: + return 'bool'; + case OBXPropertyType.Byte: + return 'byte'; + case OBXPropertyType.Short: + return 'short'; + case OBXPropertyType.Char: + return 'char'; + case OBXPropertyType.Int: + return 'int'; + case OBXPropertyType.Long: + return 'long'; + case OBXPropertyType.Float: + return 'float'; + case OBXPropertyType.Double: + return 'double'; + case OBXPropertyType.String: + return 'string'; + case OBXPropertyType.Date: + return 'date'; + case OBXPropertyType.Relation: + return 'relation'; + case OBXPropertyType.DateNano: + return 'dateNano'; + case OBXPropertyType.ByteVector: + return 'byteVector'; + case OBXPropertyType.StringVector: + return 'stringVector'; + } + + throw Exception('Invalid OBXPropertyType: $type'); +} + +int propertyTypeToOBXPropertyType(PropertyType type) { + switch (type) { + case PropertyType.byte: + return OBXPropertyType.Byte; + case PropertyType.short: + return OBXPropertyType.Short; + case PropertyType.char: + return OBXPropertyType.Char; + case PropertyType.int: + return OBXPropertyType.Int; + case PropertyType.float: + return OBXPropertyType.Float; + case PropertyType.date: + return OBXPropertyType.Date; + case PropertyType.dateNano: + return OBXPropertyType.DateNano; + case PropertyType.byteVector: + return OBXPropertyType.ByteVector; + } + throw Exception('Invalid PropertyType: $type'); +} + +/// /// Bit-flags defining the behavior of entities. +/// /// Note: Numbers indicate the bit position +abstract class OBXEntityFlags { + /// /// Enable "data synchronization" for this entity type: objects will be synced with other stores over the network. + /// /// It's possible to have local-only (non-synced) types and synced types in the same store (schema/data model). + static const int SYNC_ENABLED = 2; +} + +/// /// Bit-flags defining the behavior of properties. +/// /// Note: Numbers indicate the bit position +abstract class OBXPropertyFlags { + /// /// 64 bit long property (internally unsigned) representing the ID of the entity. + /// /// May be combined with: NON_PRIMITIVE_TYPE, ID_MONOTONIC_SEQUENCE, ID_SELF_ASSIGNABLE. + static const int ID = 1; + + /// /// On languages like Java, a non-primitive type is used (aka wrapper types, allowing null) + static const int NON_PRIMITIVE_TYPE = 2; + + /// /// Unused yet + static const int NOT_NULL = 4; + static const int INDEXED = 8; + + /// /// Unused yet + static const int RESERVED = 16; + + /// /// Unique index + static const int UNIQUE = 32; + + /// /// Unused yet: Use a persisted sequence to enforce ID to rise monotonic (no ID reuse) + static const int ID_MONOTONIC_SEQUENCE = 64; + + /// /// Allow IDs to be assigned by the developer + static const int ID_SELF_ASSIGNABLE = 128; + + /// /// Unused yet + static const int INDEX_PARTIAL_SKIP_NULL = 256; + + /// /// Used by References for 1) back-references and 2) to clear references to deleted objects (required for ID reuse) + static const int INDEX_PARTIAL_SKIP_ZERO = 512; + + /// /// Virtual properties may not have a dedicated field in their entity class, e.g. target IDs of to-one relations + static const int VIRTUAL = 1024; + + /// /// Index uses a 32 bit hash instead of the value + /// /// 32 bits is shorter on disk, runs well on 32 bit systems, and should be OK even with a few collisions + static const int INDEX_HASH = 2048; + + /// /// Index uses a 64 bit hash instead of the value + /// /// recommended mostly for 64 bit machines with values longer >200 bytes; small values are faster with a 32 bit hash + static const int INDEX_HASH64 = 4096; + + /// /// The actual type of the variable is unsigned (used in combination with numeric OBXPropertyType_*). + /// /// While our default are signed ints, queries & indexes need do know signing info. + /// /// Note: Don't combine with ID (IDs are always unsigned internally). + static const int UNSIGNED = 8192; + + /// /// By defining an ID companion property, a special ID encoding scheme is activated involving this property. + /// /// + /// /// For Time Series IDs, a companion property of type Date or DateNano represents the exact timestamp. + static const int ID_COMPANION = 16384; +} + +abstract class OBXPropertyType { + /// ///< 1 byte + static const int Bool = 1; + + /// ///< 1 byte + static const int Byte = 2; + + /// ///< 2 bytes + static const int Short = 3; + + /// ///< 1 byte + static const int Char = 4; + + /// ///< 4 bytes + static const int Int = 5; + + /// ///< 8 bytes + static const int Long = 6; + + /// ///< 4 bytes + static const int Float = 7; + + /// ///< 8 bytes + static const int Double = 8; + static const int String = 9; + + /// ///< Unix timestamp (milliseconds since 1970) in 8 bytes + static const int Date = 10; + static const int Relation = 11; + + /// ///< Unix timestamp (nanoseconds since 1970) in 8 bytes + static const int DateNano = 12; + static const int ByteVector = 23; + static const int StringVector = 30; +} diff --git a/objectbox/lib/src/modelinfo/index.dart b/objectbox/lib/src/modelinfo/index.dart index a7a5897d3..8a18272b8 100644 --- a/objectbox/lib/src/modelinfo/index.dart +++ b/objectbox/lib/src/modelinfo/index.dart @@ -1,4 +1,5 @@ export 'entity_definition.dart'; +export 'enums.dart'; export 'iduid.dart'; export 'model_definition.dart'; export 'modelbacklink.dart'; diff --git a/objectbox/lib/src/modelinfo/modelentity.dart b/objectbox/lib/src/modelinfo/modelentity.dart index 084fe974b..f935ff1f5 100644 --- a/objectbox/lib/src/modelinfo/modelentity.dart +++ b/objectbox/lib/src/modelinfo/modelentity.dart @@ -1,6 +1,4 @@ -// TODO remove native imports - -import '../native/bindings/bindings.dart'; +import 'enums.dart'; import 'iduid.dart'; import 'modelbacklink.dart'; import 'modelinfo.dart'; diff --git a/objectbox/lib/src/modelinfo/modelproperty.dart b/objectbox/lib/src/modelinfo/modelproperty.dart index f37cf88b9..7f4905774 100644 --- a/objectbox/lib/src/modelinfo/modelproperty.dart +++ b/objectbox/lib/src/modelinfo/modelproperty.dart @@ -1,7 +1,4 @@ -// TODO remove native imports - -import '../native/bindings/bindings.dart'; -import '../native/bindings/helpers.dart'; +import 'enums.dart'; import 'iduid.dart'; import 'modelentity.dart'; diff --git a/objectbox/lib/src/native/bindings/helpers.dart b/objectbox/lib/src/native/bindings/helpers.dart index 6be2a8818..06cda99c9 100644 --- a/objectbox/lib/src/native/bindings/helpers.dart +++ b/objectbox/lib/src/native/bindings/helpers.dart @@ -3,7 +3,6 @@ import 'dart:typed_data'; import 'package:ffi/ffi.dart'; -import '../../annotations.dart'; import '../../common.dart'; import '../../modelinfo/entity_definition.dart'; import '../store.dart'; @@ -56,63 +55,6 @@ String cString(Pointer charPtr) { return Utf8.fromUtf8(charPtr.cast()); } -String obxPropertyTypeToString(int type) { - switch (type) { - case OBXPropertyType.Bool: - return 'bool'; - case OBXPropertyType.Byte: - return 'byte'; - case OBXPropertyType.Short: - return 'short'; - case OBXPropertyType.Char: - return 'char'; - case OBXPropertyType.Int: - return 'int'; - case OBXPropertyType.Long: - return 'long'; - case OBXPropertyType.Float: - return 'float'; - case OBXPropertyType.Double: - return 'double'; - case OBXPropertyType.String: - return 'string'; - case OBXPropertyType.Date: - return 'date'; - case OBXPropertyType.Relation: - return 'relation'; - case OBXPropertyType.DateNano: - return 'dateNano'; - case OBXPropertyType.ByteVector: - return 'byteVector'; - case OBXPropertyType.StringVector: - return 'stringVector'; - } - - throw Exception('Invalid OBXPropertyType: $type'); -} - -int propertyTypeToOBXPropertyType(PropertyType type) { - switch (type) { - case PropertyType.byte: - return OBXPropertyType.Byte; - case PropertyType.short: - return OBXPropertyType.Short; - case PropertyType.char: - return OBXPropertyType.Char; - case PropertyType.int: - return OBXPropertyType.Int; - case PropertyType.float: - return OBXPropertyType.Float; - case PropertyType.date: - return OBXPropertyType.Date; - case PropertyType.dateNano: - return OBXPropertyType.DateNano; - case PropertyType.byteVector: - return OBXPropertyType.ByteVector; - } - throw Exception('Invalid PropertyType: $type'); -} - class CursorHelper { final EntityDefinition _entity; final Store _store; diff --git a/objectbox/lib/src/observable.dart b/objectbox/lib/src/observable.dart index 98538b18e..0d69e5dd7 100644 --- a/objectbox/lib/src/observable.dart +++ b/objectbox/lib/src/observable.dart @@ -1,2 +1 @@ -export 'native/observable.dart' - if (dart.library.html) 'web/observable.dart'; +export 'native/observable.dart' if (dart.library.html) 'web/observable.dart'; diff --git a/objectbox/lib/src/query.dart b/objectbox/lib/src/query.dart index 71bb40689..9fcb2b5ca 100644 --- a/objectbox/lib/src/query.dart +++ b/objectbox/lib/src/query.dart @@ -1,2 +1 @@ -export 'native/query/query.dart' - if (dart.library.html) 'web/query.dart'; +export 'native/query/query.dart' if (dart.library.html) 'web/query.dart'; diff --git a/objectbox/lib/src/sync.dart b/objectbox/lib/src/sync.dart index 9a768776f..9f9d9ce3a 100644 --- a/objectbox/lib/src/sync.dart +++ b/objectbox/lib/src/sync.dart @@ -1,2 +1 @@ -export 'native/sync.dart' - if (dart.library.html) 'web/sync.dart'; +export 'native/sync.dart' if (dart.library.html) 'web/sync.dart'; diff --git a/objectbox/lib/src/web/box.dart b/objectbox/lib/src/web/box.dart index e69de29bb..8b1378917 100644 --- a/objectbox/lib/src/web/box.dart +++ b/objectbox/lib/src/web/box.dart @@ -0,0 +1 @@ + diff --git a/objectbox/lib/src/web/model.dart b/objectbox/lib/src/web/model.dart index e69de29bb..8b1378917 100644 --- a/objectbox/lib/src/web/model.dart +++ b/objectbox/lib/src/web/model.dart @@ -0,0 +1 @@ + diff --git a/objectbox/lib/src/web/observable.dart b/objectbox/lib/src/web/observable.dart index e69de29bb..8b1378917 100644 --- a/objectbox/lib/src/web/observable.dart +++ b/objectbox/lib/src/web/observable.dart @@ -0,0 +1 @@ + diff --git a/objectbox/lib/src/web/query.dart b/objectbox/lib/src/web/query.dart index e69de29bb..8b1378917 100644 --- a/objectbox/lib/src/web/query.dart +++ b/objectbox/lib/src/web/query.dart @@ -0,0 +1 @@ + diff --git a/objectbox/lib/src/web/store.dart b/objectbox/lib/src/web/store.dart index e69de29bb..8b1378917 100644 --- a/objectbox/lib/src/web/store.dart +++ b/objectbox/lib/src/web/store.dart @@ -0,0 +1 @@ + diff --git a/objectbox/lib/src/web/sync.dart b/objectbox/lib/src/web/sync.dart index e69de29bb..8b1378917 100644 --- a/objectbox/lib/src/web/sync.dart +++ b/objectbox/lib/src/web/sync.dart @@ -0,0 +1 @@ + diff --git a/objectbox/test/sync_test.dart b/objectbox/test/sync_test.dart index fb373634f..5ae98e1d5 100644 --- a/objectbox/test/sync_test.dart +++ b/objectbox/test/sync_test.dart @@ -1,7 +1,7 @@ import 'dart:math'; import 'dart:typed_data'; -import 'package:objectbox/src/native/bindings/bindings.dart'; +import 'package:objectbox/src/modelinfo/enums.dart'; import 'package:objectbox/objectbox.dart'; import 'package:objectbox/internal.dart'; import 'package:test/test.dart'; From d08b5180bffb62c03546a0bf4eadefe8fcd68b0b Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Fri, 5 Feb 2021 19:10:42 +0100 Subject: [PATCH 4/5] avoid native dependency in ToMany relation class --- objectbox/lib/objectbox.dart | 3 +- objectbox/lib/src/native/box.dart | 69 +++++++++++++++++++++++ objectbox/lib/src/native/query/query.dart | 1 + objectbox/lib/src/native/store.dart | 3 +- objectbox/lib/src/native/transaction.dart | 1 + objectbox/lib/src/relations/to_many.dart | 69 +++-------------------- objectbox/lib/src/store.dart | 18 +----- objectbox/lib/src/transaction.dart | 16 ++++++ objectbox/lib/src/web/transaction.dart | 1 + 9 files changed, 101 insertions(+), 80 deletions(-) create mode 100644 objectbox/lib/src/transaction.dart create mode 100644 objectbox/lib/src/web/transaction.dart diff --git a/objectbox/lib/objectbox.dart b/objectbox/lib/objectbox.dart index 4afc8402f..8801bcb61 100644 --- a/objectbox/lib/objectbox.dart +++ b/objectbox/lib/objectbox.dart @@ -21,6 +21,7 @@ export 'src/query.dart' StringPropertyQuery; export 'src/relations/to_many.dart' show ToMany; export 'src/relations/to_one.dart' show ToOne; -export 'src/store.dart' show Store, TxMode; +export 'src/store.dart' show Store; export 'src/sync.dart' show Sync, SyncClient, SyncCredentials, SyncRequestUpdatesMode, SyncState; +export 'src/transaction.dart' show TxMode; diff --git a/objectbox/lib/src/native/box.dart b/objectbox/lib/src/native/box.dart index 2f2215b9a..4d9f8a3b8 100644 --- a/objectbox/lib/src/native/box.dart +++ b/objectbox/lib/src/native/box.dart @@ -7,6 +7,7 @@ import '../relations/info.dart'; import '../relations/to_many.dart'; import '../relations/to_one.dart'; import '../store.dart'; +import '../transaction.dart'; import 'bindings/bindings.dart'; import 'bindings/flatbuffers.dart'; import 'bindings/helpers.dart'; @@ -322,4 +323,72 @@ class InternalBoxAccess { /// Close the box, freeing resources. static void close(Box box) => box._builder.clear(); + + /// Put the object in a given transaction. + static int put( + Box box, EntityT object, PutMode mode, Transaction tx) => + box._put(object, mode, tx); + + /// Put a standalone relation. + static void relPut( + Box box, + int relationId, + int sourceId, + int targetId, + ) => + checkObx(C.box_rel_put(box.ptr, relationId, sourceId, targetId)); + + /// Remove a standalone relation entry between two objects. + static void relRemove( + Box box, + int relationId, + int sourceId, + int targetId, + ) => + checkObx(C.box_rel_remove(box.ptr, relationId, sourceId, targetId)); + + /// Read all objects in this Box related to the given object. + /// Similar to box.getMany() but loads the OBX_id_array and reads objects + /// in a single Transaction, ensuring consistency. And it's a little more + /// efficient for not unpacking the id array to a dart list. + static List getRelated(Box box, RelInfo rel) { + final tx = Transaction(box._store, TxMode.read); + try { + Pointer cIdsPtr; + switch (rel.type) { + case RelType.toMany: + cIdsPtr = C.box_rel_get_ids(box.ptr, rel.id, rel.objectId); + break; + case RelType.toOneBacklink: + cIdsPtr = C.box_get_backlink_ids(box.ptr, rel.id, rel.objectId); + break; + case RelType.toManyBacklink: + cIdsPtr = C.box_rel_get_backlink_ids(box.ptr, rel.id, rel.objectId); + break; + default: + throw UnimplementedError(); + } + checkObxPtr(cIdsPtr); + final result = []; + try { + final cIds = cIdsPtr.ref; + if (cIds.count > 0) { + final cursor = tx.cursor(box._entity); + for (var i = 0; i < cIds.count; i++) { + final code = C.cursor_get( + cursor.ptr, cIds.ids[i], cursor.dataPtrPtr, cursor.sizePtr); + if (code != OBX_NOT_FOUND) { + checkObx(code); + result.add(box._entity.objectFromFB(box._store, cursor.readData)); + } + } + } + } finally { + C.id_array_free(cIdsPtr); + } + return result; + } finally { + tx.close(); + } + } } diff --git a/objectbox/lib/src/native/query/query.dart b/objectbox/lib/src/native/query/query.dart index d439948de..e722101a0 100644 --- a/objectbox/lib/src/native/query/query.dart +++ b/objectbox/lib/src/native/query/query.dart @@ -8,6 +8,7 @@ import 'package:ffi/ffi.dart' show allocate, free, Utf8; import '../../common.dart'; import '../../modelinfo/entity_definition.dart'; import '../../store.dart'; +import '../../transaction.dart'; import '../bindings/bindings.dart'; import '../bindings/data_visitor.dart'; import '../bindings/helpers.dart'; diff --git a/objectbox/lib/src/native/store.dart b/objectbox/lib/src/native/store.dart index b9a84e89c..956ce078c 100644 --- a/objectbox/lib/src/native/store.dart +++ b/objectbox/lib/src/native/store.dart @@ -4,14 +4,13 @@ import 'package:ffi/ffi.dart'; import '../common.dart'; import '../modelinfo/index.dart'; -import '../store.dart'; +import '../transaction.dart'; import '../util.dart'; import 'bindings/bindings.dart'; import 'bindings/helpers.dart'; import 'box.dart'; import 'model.dart'; import 'sync.dart'; -import 'transaction.dart'; /// Represents an ObjectBox database and works together with [Box] to allow /// getting and putting. diff --git a/objectbox/lib/src/native/transaction.dart b/objectbox/lib/src/native/transaction.dart index 5dd03549d..4732ffd30 100644 --- a/objectbox/lib/src/native/transaction.dart +++ b/objectbox/lib/src/native/transaction.dart @@ -2,6 +2,7 @@ import 'dart:ffi'; import '../modelinfo/entity_definition.dart'; import '../store.dart'; +import '../transaction.dart'; import 'bindings/bindings.dart'; import 'bindings/helpers.dart'; diff --git a/objectbox/lib/src/relations/to_many.dart b/objectbox/lib/src/relations/to_many.dart index d299d59c8..f2049f686 100644 --- a/objectbox/lib/src/relations/to_many.dart +++ b/objectbox/lib/src/relations/to_many.dart @@ -1,16 +1,11 @@ import 'dart:collection'; -import 'dart:ffi'; import 'package:meta/meta.dart'; -// TODO remove native imports - import '../box.dart'; import '../modelinfo/entity_definition.dart'; -import '../native/bindings/bindings.dart'; -import '../native/bindings/helpers.dart'; -import '../native/transaction.dart'; import '../store.dart'; +import '../transaction.dart'; import 'info.dart'; /// Manages a to-many relation, an unidirectional link from a "source" entity to @@ -179,13 +174,12 @@ class ToMany extends Object with ListMixin { switch (_rel.type) { case RelType.toMany: if (add) { - if (id == 0) id = _box.put(object, mode: mode); - checkObx( - C.box_rel_put(_otherBox.ptr, _rel.id, _rel.objectId, id)); + if (id == 0) id = InternalBoxAccess.put(_box, object, mode, tx); + InternalBoxAccess.relPut(_otherBox, _rel.id, _rel.objectId, id); } else { if (id == 0) return; - checkObx( - C.box_rel_remove(_otherBox.ptr, _rel.id, _rel.objectId, id)); + InternalBoxAccess.relRemove( + _otherBox, _rel.id, _rel.objectId, id); } break; case RelType.toOneBacklink: @@ -195,11 +189,11 @@ class ToMany extends Object with ListMixin { break; case RelType.toManyBacklink: if (add) { - if (id == 0) id = _box.put(object, mode: mode); - checkObx(C.box_rel_put(_box.ptr, _rel.id, id, _rel.objectId)); + if (id == 0) id = InternalBoxAccess.put(_box, object, mode, tx); + InternalBoxAccess.relPut(_box, _rel.id, id, _rel.objectId); } else { if (id == 0) return; - checkObx(C.box_rel_remove(_box.ptr, _rel.id, id, _rel.objectId)); + InternalBoxAccess.relRemove(_box, _rel.id, id, _rel.objectId); } break; default: @@ -231,22 +225,7 @@ class ToMany extends Object with ListMixin { __items = []; } else { _verifyAttached(); - switch (_rel.type) { - case RelType.toMany: - __items = _getMany( - () => C.box_rel_get_ids(_box.ptr, _rel.id, _rel.objectId)); - break; - case RelType.toOneBacklink: - __items = _getMany( - () => C.box_get_backlink_ids(_box.ptr, _rel.id, _rel.objectId)); - break; - case RelType.toManyBacklink: - __items = _getMany(() => - C.box_rel_get_backlink_ids(_box.ptr, _rel.id, _rel.objectId)); - break; - default: - throw UnimplementedError(); - } + __items = InternalBoxAccess.getRelated(_box, _rel); } if (_addedBeforeLoad.isNotEmpty) { __items.addAll(_addedBeforeLoad); @@ -261,36 +240,6 @@ class ToMany extends Object with ListMixin { "Don't call applyToDb() on new objects, use box.put() instead."); } } - - /// Similar to box.getMany() but loads the OBX_id_array and reads objects - /// in a single Transaction, ensuring consistency. And it's a little more - /// efficient for not unpacking the id array to a dart list. - List _getMany(Pointer Function() cIdsGetterFn) { - final tx = Transaction(_store, TxMode.read); - try { - final result = []; - final cIdsPtr = checkObxPtr(cIdsGetterFn()); - try { - final cIds = cIdsPtr.ref; - if (cIds.count > 0) { - final cursor = tx.cursor(_entity); - for (var i = 0; i < cIds.count; i++) { - final code = C.cursor_get( - cursor.ptr, cIds.ids[i], cursor.dataPtrPtr, cursor.sizePtr); - if (code != OBX_NOT_FOUND) { - checkObx(code); - result.add(_entity.objectFromFB(_store, cursor.readData)); - } - } - } - } finally { - C.id_array_free(cIdsPtr); - } - return result; - } finally { - tx.close(); - } - } } /// Internal only. diff --git a/objectbox/lib/src/store.dart b/objectbox/lib/src/store.dart index c8440a95d..7bab24e01 100644 --- a/objectbox/lib/src/store.dart +++ b/objectbox/lib/src/store.dart @@ -1,17 +1 @@ -export 'native/store.dart' - if (dart.library.html) 'web/store.dart'; - -/// Configure transaction mode. Used with [Store.runInTransaction()]. -enum TxMode { - /// Read only transaction - trying to execute a write operation results in an - /// error. This is useful if you want to group many reads inside a single - /// transaction, e.g. to improve performance or to get a consistent view of - /// the data across multiple operations. - read, - - /// Read/Write transaction. There can be only a single write transaction at - /// any time - it holds a lock on the database. Compared to read transaction, - /// read/write transactions have much higher "cost", because they need to - /// write data to the disk at the end. - write, -} +export 'native/store.dart' if (dart.library.html) 'web/store.dart'; diff --git a/objectbox/lib/src/transaction.dart b/objectbox/lib/src/transaction.dart new file mode 100644 index 000000000..07e8e5f55 --- /dev/null +++ b/objectbox/lib/src/transaction.dart @@ -0,0 +1,16 @@ +export 'native/transaction.dart' if (dart.library.html) 'web/transaction.dart'; + +/// Configure transaction mode. Used with [Store.runInTransaction()]. +enum TxMode { + /// Read only transaction - trying to execute a write operation results in an + /// error. This is useful if you want to group many reads inside a single + /// transaction, e.g. to improve performance or to get a consistent view of + /// the data across multiple operations. + read, + + /// Read/Write transaction. There can be only a single write transaction at + /// any time - it holds a lock on the database. Compared to read transaction, + /// read/write transactions have much higher "cost", because they need to + /// write data to the disk at the end. + write, +} diff --git a/objectbox/lib/src/web/transaction.dart b/objectbox/lib/src/web/transaction.dart new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/objectbox/lib/src/web/transaction.dart @@ -0,0 +1 @@ + From 2a523e2eb50ecd5c6b5fe6fa1c7999db24da1c3a Mon Sep 17 00:00:00 2001 From: Ivan Dlugos Date: Fri, 5 Feb 2021 19:13:57 +0100 Subject: [PATCH 5/5] CI linter - increase expected score, now that the package "supports" JS/web "supports" in quotes, because it's not actually supported yet, just won't import anything not available on JS/web platforms --- .github/workflows/lint.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index a2fe6ce61..eb991bed0 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -20,7 +20,7 @@ jobs: - name: Check scores shell: bash run: | - if [[ "90" -gt "${{ steps.analysis.outputs.total }}" ]]; then - echo "Score is less then 90, please check the analysis report and resolve the issues" + if [[ "100" -gt "${{ steps.analysis.outputs.total }}" ]]; then + echo "Score is less then 100, please check the analysis report and resolve the issues" exit 1 fi \ No newline at end of file