From 767b93ecb81c3260a89d5d478d0945d84f17c3c3 Mon Sep 17 00:00:00 2001 From: Simon Choi <13610195+simc@users.noreply.github.com> Date: Tue, 22 Aug 2023 00:09:00 +0200 Subject: [PATCH] Hive v4 --- .gitattributes | 4 - .github/ISSUE_TEMPLATE/bug_report.md | 21 - .github/ISSUE_TEMPLATE/feature_request.md | 22 - .github/ISSUE_TEMPLATE/question.md | 21 - .github/benchmark_read.png | Bin 14532 -> 0 bytes .github/benchmark_write.png | Bin 15190 -> 0 bytes .github/no-response.yml | 8 - .github/workflows/coverage.yml | 26 - .github/workflows/test.yml | 88 +- .gitignore | 4 + hive/CHANGELOG.md => CHANGELOG.md | 0 LICENSE | 19 +- README.md | 402 +++- analysis_options.yaml | 11 + hive/.pubignore | 1 - hive/LICENSE | 190 -- hive/README.md | 227 -- hive/analysis_options.yaml | 44 - hive/example/lib/main.dart | 43 - hive/example/lib/main.g.dart | 47 - hive/example/pubspec.yaml | 17 - hive/lib/hive.dart | 66 - hive/lib/src/adapters/big_int_adapter.dart | 21 - hive/lib/src/adapters/date_time_adapter.dart | 42 - .../src/adapters/ignored_type_adapter.dart | 15 - hive/lib/src/annotations/hive_field.dart | 26 - hive/lib/src/annotations/hive_type.dart | 22 - hive/lib/src/backend/js/backend_manager.dart | 33 - .../backend/js/native/backend_manager.dart | 135 -- .../backend/js/native/storage_backend_js.dart | 227 -- .../js/web_worker/backend_manager.dart | 98 - .../storage_backend_web_worker.dart | 180 -- .../src/backend/js/web_worker/web_worker.dart | 363 ---- .../js/web_worker/web_worker_interface.dart | 80 - .../js/web_worker/web_worker_operation.dart | 40 - .../js/web_worker/web_worker_stub.dart | 23 - hive/lib/src/backend/storage_backend.dart | 59 - .../lib/src/backend/stub/backend_manager.dart | 37 - .../backend/stub/backend_manager_memory.dart | 40 - .../backend/stub/random_access_buffer.dart | 100 - .../backend/stub/storage_backend_memory.dart | 79 - hive/lib/src/backend/vm/backend_manager.dart | 144 -- hive/lib/src/backend/vm/read_write_sync.dart | 43 - .../src/backend/vm/storage_backend_vm.dart | 243 --- hive/lib/src/binary/binary_reader.dart | 101 - hive/lib/src/binary/binary_reader_impl.dart | 344 --- hive/lib/src/binary/binary_writer.dart | 66 - hive/lib/src/binary/binary_writer_impl.dart | 429 ---- hive/lib/src/binary/frame.dart | 156 -- hive/lib/src/binary/frame_helper.dart | 33 - hive/lib/src/box/box.dart | 41 - hive/lib/src/box/box_base.dart | 165 -- hive/lib/src/box/box_base_impl.dart | 331 --- hive/lib/src/box/box_impl.dart | 117 - hive/lib/src/box/change_notifier.dart | 38 - .../src/box/default_compaction_strategy.dart | 9 - hive/lib/src/box/default_key_comparator.dart | 20 - hive/lib/src/box/keystore.dart | 284 --- hive/lib/src/box/lazy_box.dart | 15 - hive/lib/src/box/lazy_box_impl.dart | 113 - .../src/box_collection/box_collection.dart | 296 --- .../box_collection/box_collection_stub.dart | 57 - hive/lib/src/crypto/aes_cbc_pkcs7.dart | 87 - hive/lib/src/crypto/aes_engine.dart | 324 --- hive/lib/src/crypto/aes_tables.dart | 537 ----- hive/lib/src/crypto/crc32.dart | 70 - hive/lib/src/crypto/hive_aes_cipher.dart | 53 - hive/lib/src/crypto/hive_cipher.dart | 37 - hive/lib/src/hive.dart | 106 - hive/lib/src/hive_error.dart | 15 - hive/lib/src/hive_impl.dart | 292 --- hive/lib/src/io/buffered_file_reader.dart | 88 - hive/lib/src/io/buffered_file_writer.dart | 36 - hive/lib/src/io/frame_io_helper.dart | 95 - hive/lib/src/object/hive_collection.dart | 25 - .../lib/src/object/hive_collection_mixin.dart | 41 - hive/lib/src/object/hive_list.dart | 17 - hive/lib/src/object/hive_list_impl.dart | 173 -- hive/lib/src/object/hive_object.dart | 61 - hive/lib/src/object/hive_object_internal.dart | 57 - .../hive_storage_backend_preference.dart | 90 - hive/lib/src/registry/type_adapter.dart | 14 - hive/lib/src/registry/type_registry.dart | 25 - hive/lib/src/registry/type_registry_impl.dart | 143 -- .../src/util/delegating_list_view_mixin.dart | 140 -- hive/lib/src/util/extensions.dart | 58 - hive/lib/src/util/indexable_skip_list.dart | 277 --- hive/test/assets/.gitignore | 1 - .../hive_file/after/testBox.hive | 1 - .../hive_file/before/testBox.hive | 1 - .../after/testBox.hive | 1 - .../before/testBox.hive | 1 - .../before/testBox.hivec | 1 - .../no_hive_file/after/somethingElse.hive | 1 - .../no_hive_file/after/testBox.hive | 0 .../no_hive_file/before/somethingElse.hive | 1 - .../only_compact_file/after/testBox.hive | 1 - .../only_compact_file/before/testBox.hivec | 1 - hive/test/generated/frame_values.g.dart | 733 ------- .../generated/frame_values_encrypted.g.dart | 1506 ------------- hive/test/generated/frames.g.dart | 1308 ------------ hive/test/generated/frames_encrypted.g.dart | 1878 ----------------- .../date_time_adapter_upgrade.dart | 39 - .../delete_many_single_batch_test.dart | 40 - hive/test/integration/delete_many_test.dart | 46 - .../different_frame_types_test.dart | 38 - hive/test/integration/hive_list_test.dart | 73 - hive/test/integration/hive_object_test.dart | 71 - .../test/integration/ignore_type_id_test.dart | 51 - hive/test/integration/integration.dart | 67 - .../integration/put_large_lists_test.dart | 40 - .../integration/put_large_strings_test.dart | 27 - .../integration/put_many_same_key_test.dart | 51 - .../put_many_simultaneously_test.dart | 37 - .../put_many_single_batch_test.dart | 33 - .../integration/put_many_strings_test.dart | 32 - hive/test/integration/recovery_test.dart | 68 - hive/test/integration/watch_box_updates.dart | 87 - .../tests/adapters/big_int_adapter_test.dart | 36 - .../adapters/date_time_adapter_test.dart | 86 - .../adapters/ignored_type_adapter_test.dart | 22 - .../backend/js/backend_manager_test.dart | 35 - .../backend/js/storage_backend_js_test.dart | 262 --- .../memory/random_access_buffer_test.dart | 73 - .../memory/storage_backend_memory_test.dart | 111 - .../backend/vm/backend_manager_test.dart | 39 - .../backend/vm/read_write_sync_test.dart | 202 -- .../backend/vm/storage_backend_vm_test.dart | 449 ---- .../test/tests/binary/binary_reader_test.dart | 625 ------ .../test/tests/binary/binary_writer_test.dart | 568 ----- hive/test/tests/binary/frame_test.dart | 63 - hive/test/tests/box/box_base_test.dart | 442 ---- hive/test/tests/box/box_impl_test.dart | 216 -- hive/test/tests/box/change_notifier_test.dart | 55 - hive/test/tests/box/keystore_test.dart | 620 ------ hive/test/tests/box/lazy_box_impl_test.dart | 168 -- hive/test/tests/common.dart | 126 -- .../test/tests/crypto/aes_cbc_pkcs7_test.dart | 50 - hive/test/tests/crypto/aes_engine_test.dart | 49 - hive/test/tests/crypto/crc32_test.dart | 22 - hive/test/tests/crypto/message.dart | 45 - hive/test/tests/frames.dart | 198 -- hive/test/tests/hive_impl_test.dart | 352 --- .../tests/io/buffered_file_reader_test.dart | 117 - .../tests/io/buffered_file_writer_test.dart | 47 - hive/test/tests/io/frame_io_helper_test.dart | 104 - hive/test/tests/mocks.dart | 51 - .../object/hive_collection_mixin_test.dart | 81 - .../tests/object/hive_list_impl_test.dart | 196 -- hive/test/tests/object/hive_object_test.dart | 173 -- .../registry/type_registry_impl_test.dart | 211 -- .../util/delegating_list_view_mixin_test.dart | 283 --- .../tests/util/indexable_skip_list_test.dart | 55 - hive/test/util/is_browser.dart | 1 - hive/test/util/is_browser_js.dart | 1 - hive/test/util/is_browser_vm.dart | 1 - hive_flutter/.gitignore | 32 - hive_flutter/.metadata | 10 - hive_flutter/CHANGELOG.md | 46 - hive_flutter/LICENSE | 13 - hive_flutter/README.md | 1 - hive_flutter/analysis_options.yaml | 80 - hive_flutter/lib/adapters.dart | 9 - hive_flutter/lib/hive_flutter.dart | 19 - .../lib/src/adapters/color_adapter.dart | 12 - .../lib/src/adapters/time_adapter.dart | 17 - hive_flutter/lib/src/box_extensions.dart | 71 - hive_flutter/lib/src/crypto/extensions.dart | 11 - .../crypto/hive_aes_native_cbc_cipher.dart | 104 - .../crypto/hive_aes_native_gcm_cipher.dart | 104 - hive_flutter/lib/src/hive_extensions.dart | 25 - hive_flutter/lib/src/stub/path.dart | 14 - hive_flutter/lib/src/stub/path_provider.dart | 9 - hive_flutter/lib/src/watch_box_builder.dart | 84 - hive_flutter/pubspec.yaml | 23 - .../test/adapters/color_adapter_test.dart | 29 - .../test/adapters/time_adapter_test.dart | 34 - hive_flutter/test/crypto/directory.dart | 19 - .../crypto/hive_aes_native_cipher_test.dart | 37 - .../hive_aes_native_gcm_cipher_test.dart | 37 - hive_flutter/test/mocks.dart | 13 - hive_flutter/test/mocks.mocks.dart | 211 -- hive_generator/CHANGELOG.md | 119 -- hive_generator/LICENSE | 13 - hive_generator/README.md | 1 - hive_generator/analysis_options.yaml | 80 - hive_generator/build.yaml | 8 - hive_generator/example/README.md | 3 - hive_generator/example/lib/types.dart | 63 - hive_generator/example/lib/types.g.dart | 165 -- hive_generator/example/pubspec.yaml | 14 - hive_generator/lib/hive_generator.dart | 6 - hive_generator/lib/src/builder.dart | 25 - hive_generator/lib/src/class_builder.dart | 223 -- hive_generator/lib/src/enum_builder.dart | 50 - hive_generator/lib/src/helper.dart | 40 - .../lib/src/type_adapter_generator.dart | 170 -- hive_generator/lib/src/type_helper.dart | 133 -- hive_generator/pubspec.yaml | 21 - .../test/type_adapter_generator_test.dart | 12 - lcov_isar_test.info | 245 +++ lib/hive.dart | 11 + lib/src/box.dart | 134 ++ lib/src/hive.dart | 108 + lib/src/impl/box_impl.dart | 362 ++++ lib/src/impl/frame.dart | 28 + lib/src/impl/frame.g.dart | 808 +++++++ lib/src/impl/type_registry.dart | 93 + hive/pubspec.yaml => pubspec.yaml | 18 +- test/box_test.dart | 489 +++++ test/common.dart | 55 + 211 files changed, 2805 insertions(+), 23013 deletions(-) delete mode 100644 .gitattributes delete mode 100644 .github/ISSUE_TEMPLATE/bug_report.md delete mode 100644 .github/ISSUE_TEMPLATE/feature_request.md delete mode 100644 .github/ISSUE_TEMPLATE/question.md delete mode 100644 .github/benchmark_read.png delete mode 100644 .github/benchmark_write.png delete mode 100644 .github/no-response.yml delete mode 100644 .github/workflows/coverage.yml rename hive/CHANGELOG.md => CHANGELOG.md (100%) mode change 120000 => 100644 README.md create mode 100644 analysis_options.yaml delete mode 100644 hive/.pubignore delete mode 100644 hive/LICENSE delete mode 100644 hive/README.md delete mode 100644 hive/analysis_options.yaml delete mode 100644 hive/example/lib/main.dart delete mode 100644 hive/example/lib/main.g.dart delete mode 100644 hive/example/pubspec.yaml delete mode 100644 hive/lib/hive.dart delete mode 100644 hive/lib/src/adapters/big_int_adapter.dart delete mode 100644 hive/lib/src/adapters/date_time_adapter.dart delete mode 100644 hive/lib/src/adapters/ignored_type_adapter.dart delete mode 100644 hive/lib/src/annotations/hive_field.dart delete mode 100644 hive/lib/src/annotations/hive_type.dart delete mode 100644 hive/lib/src/backend/js/backend_manager.dart delete mode 100644 hive/lib/src/backend/js/native/backend_manager.dart delete mode 100644 hive/lib/src/backend/js/native/storage_backend_js.dart delete mode 100644 hive/lib/src/backend/js/web_worker/backend_manager.dart delete mode 100644 hive/lib/src/backend/js/web_worker/storage_backend_web_worker.dart delete mode 100644 hive/lib/src/backend/js/web_worker/web_worker.dart delete mode 100644 hive/lib/src/backend/js/web_worker/web_worker_interface.dart delete mode 100644 hive/lib/src/backend/js/web_worker/web_worker_operation.dart delete mode 100644 hive/lib/src/backend/js/web_worker/web_worker_stub.dart delete mode 100644 hive/lib/src/backend/storage_backend.dart delete mode 100644 hive/lib/src/backend/stub/backend_manager.dart delete mode 100644 hive/lib/src/backend/stub/backend_manager_memory.dart delete mode 100644 hive/lib/src/backend/stub/random_access_buffer.dart delete mode 100644 hive/lib/src/backend/stub/storage_backend_memory.dart delete mode 100644 hive/lib/src/backend/vm/backend_manager.dart delete mode 100644 hive/lib/src/backend/vm/read_write_sync.dart delete mode 100644 hive/lib/src/backend/vm/storage_backend_vm.dart delete mode 100644 hive/lib/src/binary/binary_reader.dart delete mode 100644 hive/lib/src/binary/binary_reader_impl.dart delete mode 100644 hive/lib/src/binary/binary_writer.dart delete mode 100644 hive/lib/src/binary/binary_writer_impl.dart delete mode 100644 hive/lib/src/binary/frame.dart delete mode 100644 hive/lib/src/binary/frame_helper.dart delete mode 100644 hive/lib/src/box/box.dart delete mode 100644 hive/lib/src/box/box_base.dart delete mode 100644 hive/lib/src/box/box_base_impl.dart delete mode 100644 hive/lib/src/box/box_impl.dart delete mode 100644 hive/lib/src/box/change_notifier.dart delete mode 100644 hive/lib/src/box/default_compaction_strategy.dart delete mode 100644 hive/lib/src/box/default_key_comparator.dart delete mode 100644 hive/lib/src/box/keystore.dart delete mode 100644 hive/lib/src/box/lazy_box.dart delete mode 100644 hive/lib/src/box/lazy_box_impl.dart delete mode 100644 hive/lib/src/box_collection/box_collection.dart delete mode 100644 hive/lib/src/box_collection/box_collection_stub.dart delete mode 100644 hive/lib/src/crypto/aes_cbc_pkcs7.dart delete mode 100644 hive/lib/src/crypto/aes_engine.dart delete mode 100644 hive/lib/src/crypto/aes_tables.dart delete mode 100644 hive/lib/src/crypto/crc32.dart delete mode 100644 hive/lib/src/crypto/hive_aes_cipher.dart delete mode 100644 hive/lib/src/crypto/hive_cipher.dart delete mode 100644 hive/lib/src/hive.dart delete mode 100644 hive/lib/src/hive_error.dart delete mode 100644 hive/lib/src/hive_impl.dart delete mode 100644 hive/lib/src/io/buffered_file_reader.dart delete mode 100644 hive/lib/src/io/buffered_file_writer.dart delete mode 100644 hive/lib/src/io/frame_io_helper.dart delete mode 100644 hive/lib/src/object/hive_collection.dart delete mode 100644 hive/lib/src/object/hive_collection_mixin.dart delete mode 100644 hive/lib/src/object/hive_list.dart delete mode 100644 hive/lib/src/object/hive_list_impl.dart delete mode 100644 hive/lib/src/object/hive_object.dart delete mode 100644 hive/lib/src/object/hive_object_internal.dart delete mode 100644 hive/lib/src/object/hive_storage_backend_preference.dart delete mode 100644 hive/lib/src/registry/type_adapter.dart delete mode 100644 hive/lib/src/registry/type_registry.dart delete mode 100644 hive/lib/src/registry/type_registry_impl.dart delete mode 100644 hive/lib/src/util/delegating_list_view_mixin.dart delete mode 100644 hive/lib/src/util/extensions.dart delete mode 100644 hive/lib/src/util/indexable_skip_list.dart delete mode 100644 hive/test/assets/.gitignore delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/hive_file/after/testBox.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/hive_file/before/testBox.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/after/testBox.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hivec delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/somethingElse.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/testBox.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/no_hive_file/before/somethingElse.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/only_compact_file/after/testBox.hive delete mode 100644 hive/test/assets/findHiveFileAndCleanUp/only_compact_file/before/testBox.hivec delete mode 100644 hive/test/generated/frame_values.g.dart delete mode 100644 hive/test/generated/frame_values_encrypted.g.dart delete mode 100644 hive/test/generated/frames.g.dart delete mode 100644 hive/test/generated/frames_encrypted.g.dart delete mode 100644 hive/test/integration/date_time_adapter_upgrade.dart delete mode 100644 hive/test/integration/delete_many_single_batch_test.dart delete mode 100644 hive/test/integration/delete_many_test.dart delete mode 100644 hive/test/integration/different_frame_types_test.dart delete mode 100644 hive/test/integration/hive_list_test.dart delete mode 100644 hive/test/integration/hive_object_test.dart delete mode 100644 hive/test/integration/ignore_type_id_test.dart delete mode 100644 hive/test/integration/integration.dart delete mode 100644 hive/test/integration/put_large_lists_test.dart delete mode 100644 hive/test/integration/put_large_strings_test.dart delete mode 100644 hive/test/integration/put_many_same_key_test.dart delete mode 100644 hive/test/integration/put_many_simultaneously_test.dart delete mode 100644 hive/test/integration/put_many_single_batch_test.dart delete mode 100644 hive/test/integration/put_many_strings_test.dart delete mode 100644 hive/test/integration/recovery_test.dart delete mode 100644 hive/test/integration/watch_box_updates.dart delete mode 100644 hive/test/tests/adapters/big_int_adapter_test.dart delete mode 100644 hive/test/tests/adapters/date_time_adapter_test.dart delete mode 100644 hive/test/tests/adapters/ignored_type_adapter_test.dart delete mode 100644 hive/test/tests/backend/js/backend_manager_test.dart delete mode 100644 hive/test/tests/backend/js/storage_backend_js_test.dart delete mode 100644 hive/test/tests/backend/memory/random_access_buffer_test.dart delete mode 100644 hive/test/tests/backend/memory/storage_backend_memory_test.dart delete mode 100644 hive/test/tests/backend/vm/backend_manager_test.dart delete mode 100644 hive/test/tests/backend/vm/read_write_sync_test.dart delete mode 100644 hive/test/tests/backend/vm/storage_backend_vm_test.dart delete mode 100644 hive/test/tests/binary/binary_reader_test.dart delete mode 100644 hive/test/tests/binary/binary_writer_test.dart delete mode 100644 hive/test/tests/binary/frame_test.dart delete mode 100644 hive/test/tests/box/box_base_test.dart delete mode 100644 hive/test/tests/box/box_impl_test.dart delete mode 100644 hive/test/tests/box/change_notifier_test.dart delete mode 100644 hive/test/tests/box/keystore_test.dart delete mode 100644 hive/test/tests/box/lazy_box_impl_test.dart delete mode 100644 hive/test/tests/common.dart delete mode 100644 hive/test/tests/crypto/aes_cbc_pkcs7_test.dart delete mode 100644 hive/test/tests/crypto/aes_engine_test.dart delete mode 100644 hive/test/tests/crypto/crc32_test.dart delete mode 100644 hive/test/tests/crypto/message.dart delete mode 100644 hive/test/tests/frames.dart delete mode 100644 hive/test/tests/hive_impl_test.dart delete mode 100644 hive/test/tests/io/buffered_file_reader_test.dart delete mode 100644 hive/test/tests/io/buffered_file_writer_test.dart delete mode 100644 hive/test/tests/io/frame_io_helper_test.dart delete mode 100644 hive/test/tests/mocks.dart delete mode 100644 hive/test/tests/object/hive_collection_mixin_test.dart delete mode 100644 hive/test/tests/object/hive_list_impl_test.dart delete mode 100644 hive/test/tests/object/hive_object_test.dart delete mode 100644 hive/test/tests/registry/type_registry_impl_test.dart delete mode 100644 hive/test/tests/util/delegating_list_view_mixin_test.dart delete mode 100644 hive/test/tests/util/indexable_skip_list_test.dart delete mode 100644 hive/test/util/is_browser.dart delete mode 100644 hive/test/util/is_browser_js.dart delete mode 100644 hive/test/util/is_browser_vm.dart delete mode 100644 hive_flutter/.gitignore delete mode 100644 hive_flutter/.metadata delete mode 100644 hive_flutter/CHANGELOG.md delete mode 100644 hive_flutter/LICENSE delete mode 100644 hive_flutter/README.md delete mode 100644 hive_flutter/analysis_options.yaml delete mode 100644 hive_flutter/lib/adapters.dart delete mode 100644 hive_flutter/lib/hive_flutter.dart delete mode 100644 hive_flutter/lib/src/adapters/color_adapter.dart delete mode 100644 hive_flutter/lib/src/adapters/time_adapter.dart delete mode 100644 hive_flutter/lib/src/box_extensions.dart delete mode 100644 hive_flutter/lib/src/crypto/extensions.dart delete mode 100644 hive_flutter/lib/src/crypto/hive_aes_native_cbc_cipher.dart delete mode 100644 hive_flutter/lib/src/crypto/hive_aes_native_gcm_cipher.dart delete mode 100644 hive_flutter/lib/src/hive_extensions.dart delete mode 100644 hive_flutter/lib/src/stub/path.dart delete mode 100644 hive_flutter/lib/src/stub/path_provider.dart delete mode 100644 hive_flutter/lib/src/watch_box_builder.dart delete mode 100644 hive_flutter/pubspec.yaml delete mode 100644 hive_flutter/test/adapters/color_adapter_test.dart delete mode 100644 hive_flutter/test/adapters/time_adapter_test.dart delete mode 100644 hive_flutter/test/crypto/directory.dart delete mode 100644 hive_flutter/test/crypto/hive_aes_native_cipher_test.dart delete mode 100644 hive_flutter/test/crypto/hive_aes_native_gcm_cipher_test.dart delete mode 100644 hive_flutter/test/mocks.dart delete mode 100644 hive_flutter/test/mocks.mocks.dart delete mode 100644 hive_generator/CHANGELOG.md delete mode 100644 hive_generator/LICENSE delete mode 100644 hive_generator/README.md delete mode 100644 hive_generator/analysis_options.yaml delete mode 100644 hive_generator/build.yaml delete mode 100644 hive_generator/example/README.md delete mode 100644 hive_generator/example/lib/types.dart delete mode 100644 hive_generator/example/lib/types.g.dart delete mode 100644 hive_generator/example/pubspec.yaml delete mode 100644 hive_generator/lib/hive_generator.dart delete mode 100644 hive_generator/lib/src/builder.dart delete mode 100644 hive_generator/lib/src/class_builder.dart delete mode 100644 hive_generator/lib/src/enum_builder.dart delete mode 100644 hive_generator/lib/src/helper.dart delete mode 100644 hive_generator/lib/src/type_adapter_generator.dart delete mode 100644 hive_generator/lib/src/type_helper.dart delete mode 100644 hive_generator/pubspec.yaml delete mode 100644 hive_generator/test/type_adapter_generator_test.dart create mode 100644 lcov_isar_test.info create mode 100644 lib/hive.dart create mode 100644 lib/src/box.dart create mode 100644 lib/src/hive.dart create mode 100644 lib/src/impl/box_impl.dart create mode 100644 lib/src/impl/frame.dart create mode 100644 lib/src/impl/frame.g.dart create mode 100644 lib/src/impl/type_registry.dart rename hive/pubspec.yaml => pubspec.yaml (57%) create mode 100644 test/box_test.dart create mode 100644 test/common.dart diff --git a/.gitattributes b/.gitattributes deleted file mode 100644 index bb563603b..000000000 --- a/.gitattributes +++ /dev/null @@ -1,4 +0,0 @@ -# Auto detect text files and perform LF normalization -* text=auto -* text eol=lf -*.png binary \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/bug_report.md b/.github/ISSUE_TEMPLATE/bug_report.md deleted file mode 100644 index 7451db6d3..000000000 --- a/.github/ISSUE_TEMPLATE/bug_report.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -name: Bug report -about: Create a report to help us improve -title: '' -labels: problem -assignees: '' - ---- - -**Steps to Reproduce** -Please tell me exactly how to reproduce the problem you are running into. - -**Code sample** -``` -Provide a few simple lines of code to show your problem. -``` - -**Version** - - Platform: iOS, Android, Mac, Windows, Linux, Web - - Flutter version: [e.g. 1.5.4] - - Hive version: [e.g. 0.5.0] diff --git a/.github/ISSUE_TEMPLATE/feature_request.md b/.github/ISSUE_TEMPLATE/feature_request.md deleted file mode 100644 index b0d2dbb74..000000000 --- a/.github/ISSUE_TEMPLATE/feature_request.md +++ /dev/null @@ -1,22 +0,0 @@ ---- -name: Feature request -about: Suggest an idea for this project -title: '' -labels: enhancement -assignees: '' - ---- - -**Is your feature request related to a problem? Please describe.** -A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] - -**Describe the solution you'd like** -A clear and concise description of what you want to happen. - -**Describe alternatives you've considered** -A clear and concise description of any alternative solutions or features you've considered. - -**Version** - - Platform: iOS, Android, Mac, Windows, Linux, Web - - Flutter version: [e.g. 1.5.4] - - Hive version: [e.g. 0.5.0] diff --git a/.github/ISSUE_TEMPLATE/question.md b/.github/ISSUE_TEMPLATE/question.md deleted file mode 100644 index 072de68fd..000000000 --- a/.github/ISSUE_TEMPLATE/question.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -name: Question -about: Describe this issue template's purpose here. -title: '' -labels: question -assignees: '' - ---- - -**Question** -Please explain the problem you are running into. - -**Code sample** -```dart -Provide a few simple lines of code to show your problem. -``` - -**Version** - - Platform: iOS, Android, Mac, Windows, Linux, Web - - Flutter version: [e.g. 1.5.4] - - Hive version: [e.g. 0.5.0] diff --git a/.github/benchmark_read.png b/.github/benchmark_read.png deleted file mode 100644 index fde145777372297670e64482550a4371fede60e0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 14532 zcmeHtWmFwax8}huSPl~00tbQx3l?0199#n-xI=Ia?(TkY4G>&|1$TFMcX#IGz3bjt z^UZvB-I?{>wca07t5?_Vs;=r?PgU2m_wEjs2TP!%5~2bC0Q66iqKW_jJO}_lc!vxJ zE!l!SeuQS%@-oU|_xJZoPcttHJ`Taz4ILvsam7DsyKZmqItHelLvt@KuPd7S6Y}bZ zCKtE&j{Fl#zeg4B9h`W^6s>IR9-p2k7c^d9-z=(HP$^moBy!;l-IJka~PXl zJ36zH^|Lj#sOi}{adv6*+vAek)9SNJM&-l7V@3Dg!_C^m^~2ur!`g1*<5u|N#$)5q zQ+(A^Sjm^CxnB=QFIpj84=0bElT#1p4-?DJzWD)P1aGM|d@i(vnV|1=q`fK#XZvH-6#u9Jua88UmfyB%~i z>-Jt=EU7JmKTp*z>ba%MIW?_f8J~lb02F>$!pQ%2Qn|f0YE{J6;>cs>cV@-^){b-PFrR!kO_3T~8{mOEQl(f2V z9NZg?L0;E2d97EnR6?djH1`H?APVKJCM|=Pb%H8Zpe)kc0v0_Uwi!fXmNEaueMXA| zmV@ivW&e-a(~wvz$lp7aLF| zy``{sR(bt$aNBzpn^K^iXHc@BZ#xg|jXaGMh=(F!+0;CKQRM{Oq~GWG)3F_0fo0W& z#<4%x4AinvI2s3LU@-&;VyZ}gu>`r&%5ZJoM|{%n*ECI|=m&vPq6jmk9Q6oSgZ&TT6Ckr2MRjJpXCz92$Y8BKKla9P6xbYB6km7~WUG@n;8=q4rI}jc1UL<|}Q`?}MiI;-jj6-w2Qe6$(R-%-5 z3<6uvCR56E1K>$A69){x_J?~bgL5ekznUrq>Za|q!)(J;7_9{N-gW+#SVKlhhb$J? z0wI~bDcp|M$49;3U4~`t1HYQ}Ra0**wX63W-vDWxb10``kSeY;bP=Fhz7u=vil5e?$u7)M3&04=N?IOcz zeK#~(YY+awcQiJ+YO>fm0rE}38Hu9*9@+0vtCW`|Psju zMOrm7-YXW#C0dMf%HHa@>o+7>*$)_fB$=Hb9*sZ-RZP5R2@(awxxPNfg8L5|AYn|u zh*MkZ-Iy6w1<(iDL8fgml0Q`;TdqRdg6{>mL_s~n{dfRw6CMW<66s#nWG8C6xp?g7 zSq8*FrNBX>0uJ-SFi7`>EPnz;xy!>2!TF0J%4mjLHL4OINauo?XBe( z^Iwy&r3cw{b{1F}BRbugpAs*56*OJv7w0mW%8Vve4?1BT$}~Z3LcdpzBxIe0dA4a( zo3551g-Gf9koWRI5PN9>hbYgnJurj9jUrjLgI-{x?{aWfFvkos;jy2ArCW;@lA|Zc zV)Nb=#DHY}V{wKKKd?>?}6XU@XNib0sC?CP=w2|OrLM_QlJYffDWqo<%p}n-#QjM=hK9RtoCd{ z_q~vGl?gNPr!gKIVXM0msW>=g-}ZDioNigi_0!Y^5yKGUob8qjtda`sZrV43y&Ox& zq6$MOpIW9zd2|QrqBmhuzMcfDvCpBvaU+>A*_LHoxk(f{2#2H_ZH@V9P3?hSy|G1z zE6K6F6&>*EmBJG=C2X1q?%n>fW)Ye;VF3CP9qQ})D*esG69g+#{j?S+&EH1t)l{7d zL&0fZ0&Ja(4O!_;7U2qYd{zMC9)^O{91tnV8s5J%p0wZ}<%Ow;2|#XeiG@h&ZF)!~ zKA4o%RCv?_SAXz+-qbssA~M9L^#tGh_$-cBfP>iml#m&`Y#dPZVWWt(L^-3BLI_aQ z7r6Uhy>YI6$Ka)#LUGJc12l6>7u2YDLU@qN{G}s-74EtfbXnoi1Pstxv2p)hRnDY< zVY^p|;-@uJ1Kw13QN@d5o1iDc3Z6jtt*_3?q?*+;B|NTm(n=n+T;|P0Qc>WBam~kirb_CC(1Sw+DZ;P*xO@NtM-+$LtPmUBk z@-r}CrFJ>Ty+KdQGPo{nB#(XIp}6UJg_Z}DuUAJi6|lDbyu$-CHhz}OxkpX@HgICl zcaB?u$}^E4f0h7h5+%FQY#LC{})QK!U`t zsC2&gk@HD`Hp`uQ^8|bA%w<6qWvF5y=arOuLOcqqFn*Rb%KlN-GYR0ZH-xeyeS;Y$BDium9)(* z+3sYs`(qmZ@9C4O>8Pz`LFKXXo*>@}NrUXP=Q`sr?+cK?K)lj9;F}l?ZiK@^+^D); z?hj4FwQm;M{OJ;M04<>TAWxN)lx|y7>+yovG{=sc%O!l;1 z=*2FI7d`uq4181M!aobvFY3q%FI#;jD`)x2FtO8 zzHHXBAXRgWC6Xbywi;)>F|BBI+d^>GN3>rX(MDbEXP=y5Lf1S<0YCd0Lv7Y|vxM61 zrdaaM4MI{oX*RE&zeL6rHPfclE)4inPrj)mUPz0VEMlHET5r6&yt(2DsjsZ4hsE*36|1yPs|tXekt z{#n|Eu&-dtj-kb7@T!K{6%WOFsF4_^Ew3`W)E87Xz!1>TLfFQ$Dsktk$=~xpJiYlT zjJrq_@GXmmVR}Bni)xT{(@agp6~Nd!VSHFkvONjn{drOvdtYI%AsE^JYgmhsdBjk ze{&4y4^56gM42Znp89z^=9D@W_xOoV=z^xlRa(oj&0A-rJAFq7-dNR6ebKF2zmn@s zld>{N*u?o99|a+!>bv?QuUxXSZtOAvP7km_zqR_UCm_$h!Lv zn$S#eij5xb%CV}O2l;UfpY9W*r=A>bF$-dy?nq?#nHrG=-*DFoSYYat+PDNw2`%dq z<1vER4~E6KM>@WWM%;ztz4BeGw&~*N_4rkWeV99tZSaQp3}S8PfQf4V>os0q8L{K(5MJX%-e+paqwg;Goi zp90xJg>f}Q}FTWfn zV+{|`nJUYD`YerQDQkj53ubF{@X{GzoS^8g!Gpe>hoACp`J+&8v*j#Kv{K$0NBk~#t@}O)JB_Ja$998-6YZ>9yr;}|t0gAJn_)mptRYB4Mjgt1T|Mc+C0VNm$ znx8U(1GW6jWj|)N8m!D-NTY-Hpr47Z z2kQ~AjmgKIn!kCxzU1>(sFh=8CeDE^aPNrxjf{9JR0&8=dj=iXHOA}!rO-SlI})av zbuM^IWz76fLH0W$LHep+BB0fiF%&@%00Q8jKp`LdR~^Bghm-#njUe|tz{aBu`qxhV zY|XDFWpCP79rPM9d+H26lOX~Nt=X!vs^K@DmJ7Ix?`0r-d~Y}1hHov#t2p1U`ho#o zm^?55{#|&$Z6*-#Un;QIu1{qaC|q;6FYWdfxKNUG!WJlZI0o#BCce_aVdLasaT#|{ z)EPs$C9%)0iXk^1+wEl7Nm}Mt;i-aLxjEe%c`NMxGy~!2Z%aU&*A-n~ zv}FzWde9m4b}+oa_8kFaj_cA@vml&n+)(^`-Cn(;MVQfGt8ez%Un`LJw)F=&eX-|; zE1BJfNdS;t#68hQ9>x-IC&N!8cCf*x>Oq7jYfSZ~@l7l`2$PRY_YEbPNzI88Bd^w^ zV!_=$&T9bA7;9>MeSoM!6%dFQA~n;4Nh)`P$mN&=Jn4Z+oaDVyb;mbUMlDErQOJPz z0#{25w;y-nhXzS$I2iVKSALP?y7B5FQX5aUsX+b|#$+5MfpJ7MW_c zKH8taEFfAq(4W30+JaIt>rAkf=mF%H>u%rY(Wik#Q8187rstF88+@DiR4J4BZocQ2jqt z@d_c;iW$sSaJ?ArSg8ULv){kMB-SS`=LRoxF{)!co*~(7iuiL9t_!v}pZS0XG^f_z5KX zvEs5E^7>a&qFlp|zBp)02A#2iS8BWf2y(1_`xy8-bfs079m0$m??y%FaB~75T44Q_ zVORr(2m9@w-**29bp>|b7InXYyweM~{G6*~kK(tEEDQu$K?gM4i7{+TUdtl9@We|o z4~Jm|$AkSd9o!_*xh-De^bsfTRsZoyuw#3=b+bzXBJ_zPFOYh%PJSM;=YC|vn-kIF zmQ7I$Jt!Qfg^)MQa=!74q)G6Yt?bwn2Q*Q#Pk0}QklVSfN%sO0# zM6D2BFUzFHB{8A{c(Bu-!8l4zWx?FBjSo(g3$bk@7=obosfA#T<6+J%Qf#OeC{-nW z4n4s#f*_yCiQwHBx{EGkOXz5c)DoWGqO(GAOqvAGYD27A^gBesCF(?3C(n?t7ea(b z7L`9DxbzXAZ}wxk5|S{KA7A-F8K>OM+ou=k1@amJ3ZB8se}uu219bfpQNI8K{tvOj z-+hiO7Zig3m$GRmm<0Jx=g8-Eq`KZdyMIpt{P+Di2*QK)M_a5-Yrb|^1nj@5ZSikb z&p%ig>wj}-n|T*xi34EYyXT4!wkrx}TA&;1puVnFS$Ubu?}k|Dtq*GZJ)ist>P&1s zoTUS3L64@{ds80uF+{j~p!%gAAY!NkvfgpeI;U99VfaN`F3h3n}Uwq@<+zRu5w z7|^Y&Q4*Vk`xTj+1LB#?U# z9pa_$m5ZUqg)O`v3mj`&c(U5rcoh1YkGl?E1#%yq2!N0tJb=Qaosc11~+PDFEqA#qN^>k&p4 z_c2GE4&>u6%H}MRG$caw4f)Yv?Kq{COrXQ&uIMC=8mM!`Z+&077D)6FRedl5@r>uWIj}O&+?g>oH3pq&D96ASUP0cpdX$Fc_?r4+(t0YU8 zn?BJCg7%9Ibl8`RSoRVf^fFv9SSp8R)sFm{C`eHR}C$67|27^@-&K|39?<5uW&ewSn|T zmK|~YvS?qO7${Y+vIg}kh}p1{zLp3+d9i@Ad=q6-k2mc!gZ{Y278}(7ek27!`>0pX%*^$S}Eb=>Vd^7U0H)`cIx`DLM|qO~;)<9GnJXolqy~av6f_ zBv7Lt`!1hwWYRX?OklV-Z9hi+Pt{VAH0#-3pUjYw*<3NV3f~<{!|LsyiqsQZijN8d zzS4GPcd8dT1mQ%uubAd=GFtuYl{#{S%Oi>DB8Fz2ceE^Wi7d|wPd4KL=OCmhk`?ZvibVotaf;R3(cjOyVl2G5lgQofEw^N&Vl244w@b}ql!lBf|bi0}# z78fTgEc@wo))$E@uwM;Wt4H)Vq$b|oZq%6fJjtxKVl;~u>Sj87ig=ugji(SLe5#R} zpx#LGu1jVC>@5B+$g@;r2kK|B#knY#;j#D40bF|b*p}9Qjl9(wSHk@9@|^q_l8YeJ+H5$`DO-0W5AQ3V!DEIRN2vPDF|xppih)hM#~L3U*mYV zKW2`OT8r#b9IXZ7tIfx-8k>Uu0B83&Pu&lcKFD3PN?)kS;(yyAYrO6|@2ICJklR*O z4h=bAsMYCij9Ls&t%k!(pCy=Ve*mF?l`j=lOEeC^vlO0a%x|w=NMNC{jJ82NF+ZR9 z_pH}m4+&qXYr+Q-Q)mkvK$1?9i=4|e)6DqL)n_k6kK4yJnR6%yaib?Z zLSUNPz*Zp|sFE>d+E&5ukI4F>4y(2_)lFC2xvo`863-r`zfZRSHAh2BYfdLDA~{sO zzyJVrkND#;l0ow_Xh7%`_5UE8CH{9hLs+~H>ubR=x+|~^>H8GFUU3rx2EefU6^8}@avw8A(2#eh>FUDp;)Yw2P z&D!|$AZMvsZS&;M^>GYvJVw_+K9cfLm~UTd z&Lec>j0TA3n7HB)As-BZB1PmRQ@NWX2AWtXJ%d?(+HMCe;tK29-tkdbZ(qLic*E~O zJ}NiSBj?w)dG02>I=F|cj2ciTjL^aql3%utz@DV#As3X!E&DTu6LypS)Fzq@Zj-&$ zl_qfo$Z8CZrGMj%daA${&ip3a8o$A#Lk%vztC{!iR&VtSTbKsF5i}D!$Brw6KM0K}VoT?Hngt%If}C@4R*? zGE1XBI&w_SIMzb@wr>0ulP3($15}^8?j7Dvmv7M|F8tNblt7cw**lO zu(eP)Zl)X1aJqcHk#iBHVtt8&s_kkNchu>IX&WR#?|Tmb?_iAgbw19h0H*6MmMk1^ zHdwWiV^4xDSewtzKIbxvY^I0xZ_1Fu^z>h&z!4ZO5M6gRIwUDaG`~t1o{SYi!!V~W z@{o2H^6TR>)wVd_ufg7J48b@1&L-a)aK?49r>R`4!lk&c;5Z936GMkEQcsRjAgZ>~ zml232E^^lO$}Tr^z%DwjC<{fmG{$P4c4xg1Ng|{Y%@u!awRVKyooGPDj&>effD7G%nR8s zE)!-&>DC0|^g7Hc*utfYgAOuPvE~Wpw^TCEo%;#bOI*7Kr{i<_$AiWnbD?{J2KJ4a*pkN_83B0ZR z8F1Yqey@v!ExXlF*aA8%Ap6;6B5*{W-5O6?H6pbFEXUiPl4tFIC<)pvJ8D_1g9!Jg zf3qNKUQmgcWC)`5h-i-d$ky+OR}0cA2pn6fmlgFKSW?>a`1#R5E^T;@U-zr@T&joJ+bzzQq$EQ5O4L;-cys zV(ss8uTD5(b{^0k=!X**$g{cT3HTBP?Bz;G3n`WzvAX7Z-8@*4mw>&8@5=0sd*6o8 zAH2OfZ1!GrgRSEIboJRRgF^^MKh^gJ3$!g)>pjZ1rkMK=gqCN1#u8pV~K5unGw9-!BU#x?$VeLy}Vd|Mv4}4 zM3SnC#pM|RS$o?d?}#ylWYhjR&KY`L>c_5zzQIT(&Y2Gd?VJMbvsBji)@Xc|%VC>r zg!U?dUl0T+kA98r!zsjHDJW=8-%xZPD zmS1}bQKOksC6mCfCt!t9HtBUHCvP;(W7sR>J-&(m$Ck=n@2aTT5se zm8vPPM@GBM9c!Zm_Uk3~3jP?tENj-cp0*0cH!=2l2jag@MW7Aj$}n^}OjMBdVYBv}_z*(Wz3Xq zarzf^{DE6X!AyaQ<(HibpqhxN}=;p22ZccjgV)0STqjo&C%xpc4p z-t#(v=W#aX@^A!OoPK$?1Ap)nr$}%%WXL8qt6h0ky6zTVTa4CnF}~zgku!ku`6{9< zPjZNG%a5arSGzD%%T@(^YW;$Y;HFx;Dnq*Im%0W?^12qEmmFfWIxBY>mUKC#4m+15 zuh!K#;X-sPcQXQ?nwE`$ce-+dtdij#y4%vC0k1-47}J@WjYN)6PsCgeK>;k=lpVS{ zY$XB=>RNtf}}Fcs@7&df>-BT+J!HjGny+SO{tJ3C?il{MX; zsepQ3ez7>ZL@iE``WxaC+jf>gK0ido3^=d1V*TXfcNj5S%(v?m)JNfymZbPaUe;M| z^+)Y&YFvpk->tww57+*W0v~}z%1Kn!138iTD;9-ccXB8J<;*>z7PB zm2;NAsSrsnt}fj{CFVT7+!jJez2Y!RLMZmZF2;$N#zo9lT2vkbY*+F~fit_Ym$KS0 zicNfSA{=ypR_W76J&>eZ!*)(@Y~(=Bp_$UE^N{x37MsOibm#liB0c#cB=HN6G1QnD zPPm#Y|SALf)C z^f7ME792&Ikaq29H}LaC{At^?0&*Eg2JS&Z92r8f4)W|nVHGhm6WY^?Qx996buUz# zjyZ62hDZ?(hO$wB9jg*b813Ho^nDLu((_isD6@HJ{`EXX> ze1oW<6&d=bnb7U{x%Nfcwb#(D=BB+-*@55qiOw)&FW(gJI@AFoJ)-(N)Tu*?F1lI7 z9bH-DeQqlv(Ka+xm0Gz%_kH_A}R-yCacfGDEtuv zg6sjPA=GN6RWWwLX|i*0(e&GXUSXO-e*SnS*12CUPSYV^zah5(jB3c^J=Q|BeNohyBgUKX{!`qLhxk$ zvBQ?GXTNw(Gq(S>21PuI>}=NqHIV*%GmV_aE%n`2Cny2!Hg~?L_r6cQubkNI;ds>> zb8t40@U=&@uexJmndHk`d2-`i3zH~dg3F6xFUq^Z#6@<{>f_Nyvjlg=u7^gsoOVeJ zbV<{)W6HF0W;uC&?zF7+um!728j8NN_+4idZB1GAFeksWV!efj5sfO`+c1%w_pMAF zA2lbt#os-H@W7`bM-!auRJ2#A>C^#KR$?Xq_k7{l;2Z6{w68fDXgj~qN0)=7E}X~Pz0Wld2H$IVeqa`5rAuU# zar%vg#<%v)>{qp4yF!X1JJCCv^7r`DLHLwZit2qhtKq?%zjaP>QsZyN;%8T&#)>u1 zA#Ojr`q}^%6xENt8OFhy;5O{!wJ5_A!Tw+a^^ek=Jy1qKpC?iIM5u~>n*IOcYHpP1M@fJ#_k+*$m9v zCg4-oVc*x?V+}CgfoGs(;CgyfbS7Y@M^)ogl@&lhFZ4eBR6fl`%A9bknECDmeKR{; z7#y{v-RM`;@!$$KH7Si@>9`cC2$m;GjbuS5%fMe+P)9|~Y{t#ohS?bVP9nZkg`M1C z=Y(+jnC4}}W7op(M!&|Hb1MN>kDgo}3UGC~Y%%zTd>HmqaoGpesUnn(jn(KP9O35i z@dgXilEMBHB<(A|V?gp8J|=4487oQ3++`(-khgH(@h}UGhi@9-d@KT;jn&8ZW#$T) zD`&k(O3-|a%6^omH7(UD+taC)dBR?5%AB~4zk{Fd){3Da@0D*_7SGjHy@7A5|5!QM zbH{O1Q*!jWi1AT;8iS==aH1OjJ_yV6=A=Xhbgv3dxz_w{aGz!t9;Md*%iPQbl^qvSI=POQ_&u=7vkJRp* Tv}JPlpL;)vfkl4`fA#qvw2QaZ diff --git a/.github/benchmark_write.png b/.github/benchmark_write.png deleted file mode 100644 index e5549050b7c6e087d0a265e11560c7f29ee16070..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 15190 zcmdtJ1yox>w=W)`K|+BPf)@y-P(mqQv=Tfx1c%ZF*HVg0vEWkNU4j>fQrx9@fl^9w zDDLk3Y43XLzwUkaeeZp5-TQ3TIXh?e*?Z5-Z)URhoO32fSy7q@L<0f<07Nnf2_yh; z=k^-NhL3a0d6Q|90svq^mF3kW&(F_qRM4tdKOBOx>e`2$LNLo~Ti!8+zOkPM#-@sD zx@uYn=YOx2H}rZ$7KEgf&n&FEgyuwMRGplh#^=`lnp@sKJaP}u>*ya3ORsF}{l2q% zkeFBJA78w+vwwPaw!XQuv9+7_sqyQ#$&rbuL=&a8}<1>pZ8><`J6VppM z6>VkpU$0#=0uoCWE{-lnXRd7$K3vVcyP7V)*r>bQy4cvcm|qRPT)3b(|NIze7WL%HE(kD>$2+WwO`)%i-VzyJ;$qA^{a`?jxq3P zmMZ`tv?n7Ws^&bo+3J0ZAHXZ)s(`Wp`_JWnGnvd<$B*900^ChM_EP4Cq8FsmVW5C+`TJEJ%Ycs43}7B|xlq^&@OIqM#k`01pZF z3om)0UdkC~$PhLnDdpf5LFyU+sUph7W8PneNW7)vUH9?q*XgFR#A0MVB!-zmRlP}F zEJ3ccQ`vsfpQ>{g266FWOtP)-Fc~MlLlg7&EK^+qQ{AALP*z}bB$|2I?0{+9Rh`f) z!uGZbFM#Bs3Lixt?Kj(V9cztOlZwGuS%DxP3o?VSkkHq+jQv4a{)0uhQtgtu&;uNRLQT@2fg$%@jmwbf}0VYpY;?cyQn^7g0|LLNrJN^fhoaYKL@ijPmdUQMU0Emala zN4BApx}JxtXB46JRdsGiUVe3e63JCsn6M9-(;-P%pUO-@_rh#VnS_7?WJb$-R(ebV z>gT|`x%-K_b9_c^?Xa>R91rN_TgzO$3F?Vb3{0Jvg^?f&3X{PVIDX7C;7Y>YHc4(v zjdBHI#j!EcF-CQ_U0g;}wiP~=C!ox46RgnsgeMgr>Cy7(02VQ0vH}OisrG;H?jSNz z)5SomRBVsQ@nJ3?BOh^?nO+8zxP&k!2r_0hMV1Q#j{|h4G@tMTUvho6YE8p|foW<@jy@#x1)tu}&v=)c zfnZiA&=ieVx^?*eY~c?sM;<(U+#YuQZb_-9%K(Sl777OS^<$aoe89&*%vgknB}~Y} z7nyl#{Bq$bh56ePo_p3o2=77#fdHfpfACzPC6gWYf|%(&z#a8Sv>8o&M*OSP;dc}S zn#+(c_X59Z>@Su_49;C>JbPs4<#wj^W7bGSy3U@Fu7h_J40E0o2Dp1M@`$58_uLIu zz~Xz_|BCpcS+TY7D}@6Tm_!oFD4W~2bNx`xJG@_r%g#t1ikX5@_0I0iE_X2nJC*4| z^{B&7yCm;$Gs%g%?|{FA_LVP}mQ-rq^LdJ8qG!qm%mZ8kLFeqQ+`tENXAt?35=_XF zq#91(RS$Alyvx?BfR|yrIRkSDLU>c{TUb9^zr<%H^2XwtryW9O>^?c8e=y6{5!y+= zzH|2I_c_QeBqt_Hm8n2T2++ysHX zJm@gDoyj3c(HrbXMn*GH@=-^*WcS?lee49{>vuzVq#oG8Y`98O zm9S22d8cINVc<~ZStBE>B|tpRzQ%{?Ip{BkplW`|V^zm4Og)V0rHDTd-! zf;IeS5HHw(2dz+159E{l5`FfjVpmOVIejYjYM%(>*a$@MREf&lUUWJ)w8_ zjaYCp!|#%WsOnXwV|Q~RbOrkNm>Bmcd8h+6?VLK{J!u>vpzUjGxX!hkf3ci$YQqGI z_UoIoa{6>^BEy|h04GU8E?GoJ^LNs5Nd$q$+q7V08{{sn zEvRMum3+S8g!%NmqF!IW{Y|QV8xQeiLjIR5it2G8`WSxc%*of-f#qpY*yS~uWC)lS zoGnW_T%GYIh6@OGpi5o*_!IP5Bjo6n>3p}Vl@Dr+rsQWjwX(D|c93T0Mt%QQe{R6e zV>1Wo%Yy;&B*Hyx4MCdNOT^2>$tpt?&QoZwaz$#_#8;|met3(fJU?n(>r2+;cT z_mnULMeC$og3DRJwk?vmEvTM_R9u~xqesEonvv+Z;8SHRuiq1riaDvWJ^GXh*lzWU z=G~R@q2-!AbzzYHh-8tlFkM6FQiPm*E99g~e@GpJA~zVv0ox8U=K|_j4ap{)$YEZ& zX-}}+pF>ynuc(wWa(xOebXmv44O@hWOz)Ci(L$F%(`;cD{+ga717#xT_ydLNgmLiy z5>Wh&D*g*O>!ZJq|2J|uYvxB6{-e}3)A%%p$XEZqCoiYD;8$uO1R@cEMNN#Rh@8w6 z5KiOx@@?lvw3mh1N*;U7zK3h=pq`Z4w#Ks}S>a!YM)*nAhU3A}KKBV#NJ}PKg=E-r zJl@AF^F|pBU2OT%3{G_r0FHGo1G@U(S8VLQ!9YC+aD{-;Jf(LZ-vL|{(#TfK#h_)| z+(6dJ{l}0FEz+(atQL=T@{UtSh1Q6g+>CNI*(f@svH>B{3J`E%mSQp!ffm+9aA8tWOah7+VQXox0iW4#U%M&qQX<&6G+JOL+>69v1 zGf{h^HV4WQz7HeJ(F!5DN_%#?7?%^^pNOrEW@aYsvLa^$l>Kx7DTZ|IR_4H8-$z6> zm?apg?rBU`Uw%Z;{JJLud?JKZoc%rThs(VFBhpbVe8WaSi^&QMpSm8f3A53;pRlLt zPRbJpxkP1FDz>TzuUhr^hdQe)9=W!;>L^o}4?_WElP;iKb9daGIP@q_$O)cNPx-*r zF$7Y1p4rCkOJWWyDv#jFBU4}duz&`%7@WX*sI$kP0Pj%{uZBO|Xv?V-{m@l6c?|JI z2t;?g3|wA^%Q^|FA+h|#Kb~jm9p1ZXs!+8-&R0OyIeLUM@GQ3_AF-gA1>VAk2 zxX#Pi`r`XT`Da!Wae$TDUJBYOQXzo>4tJpJr3m40nfC;-a10UW=ea06ce<8=JdwP` zGnT*4f8rf}E3f9~q-e_Pyt(>k!Nz-!U&EGvOIclqW!Ua zq*sIF4nXvz#Og>90JhQV#KHE;5WiiE!vFvb01$xAg_Y|O=O|~2kX!OzUcKh2@y@%l>EJU1rv-kKY2n=Le}ekbY!& zYR-Fg{ig7KRkEHKt?($}&$QWCuTafPKV#8AUkn^SG6I6nNO~g*(x}`y5dNK?exb^9 z=tgq=?65SKo9;>EVp3Sv#s#PMMh*SOLtnu)$k?0b0Egb-R9vruZ=sXGu(s}eIOrIs z+JY<^yr8DaB^!Qn0z0iTW75x^Grv1faib5i7(TT%fOeSY&Xv^P>B~h4{`76RfLNi- zN`rtBhVZml08ZGtW# z4G&%diOMumS3eOg6TylKw$y+JERWZBB3xM*C4Q<7V;#Iyd0^qS1+(<&kUUR40%}E1 zJedgd1wTr}-Z2^Jl@k?wt;P$>_ZB+u6pQ6~JypYWO*?^;BZVT86-1eiIZdW*RHr16 z4p=bRG?vfAO}Ol?Y=3@V$q)sm5Ke{Mb)40Z=yQVMZ=WbhRLg_9&QH!!PkQswjo@UO z3xNombTyE02mtSOz>DdH{OLTO`RtH4mjnd*>CrN+fuusPz<`bl7?qb~cMc#(DDgbg z@qrBvKd;z`8te<2G;~r~=U(|LmT-V?+2B`WN} z2y*x+(`TOoF|mokyCRIsZ2ER`$84a2cBA4|RFW5N`5B+}QsqPlV zXw8#Y#s10gEoel2JKq^Jwq7#h_xzqiYultLX`9aHTomr-%x2lv+B7un(<LE{K9C zTT{~&+Gj4vw@hAJ6?NIyjd~;V)B8K(tNVR(Jw9Nswz>&Dp1)o3#D zMC@6FZ8_vwdZnrCb9yEwU*p~+G)sbK{KYyc-lm9o7T{BMa~%c+5XtZqTsdJGD%RPoqy$zyT5vs-7>W^A@?7XKC|3`uSSC7U~fSs@4;5y{hv!{gohjY zBSFDY!YQDO5KA4HPs;q}mnFJJxnx~hxm!4TTmAYLDYq=&6>*hfS;F1SqCdp4MTSYO?@e`a(6Sz0t23GX&t;Z#baERVm| z1$n(iV1Z$P2S7Lm5C{Q8VndMt36R(SM&`e9vj1`Z)N7(T`XSI8>Xnt~;`hSKjUvq( z-ip<2gHOJD-EHRh@s4tdYvj=^k!|G5EZhu95TFWywcG5WLyCp;`qE!9F5gr+!4UuG z`{@3cH-UB-z+TVz$)!QDoW~ne(lVFV)r;p-fuY{mwrM-Y= zfcL%((_Os3ps3UHTg`Y_>Ln}!I|3U9`1gOl$Y3EvnSfB>M~D}M4+%gHBmNCzH>X1J zSB2H64#w*fCPV&&rg`0+4GEY1sjBSHt=N@`z#?E)YOU*LxL^Y=MT-E%KYEUdf)J1F zK=ir|<uC%&@@q1h`NgemwUOL8^~LP- zC~O{?Tb3{d3>y6j#wu-p|95b`C`fU6HL3IqEoROrq-5(a<9K!QFJmdNWbrSn0 zqUuJ^!84oAe(*Ms{>W@E&YnqYKrjuKTlLox6X+nVV~iUnD)+Sx{J-c{>4%02`ly3F z`_r>a27%d{`LDBA!pXTmcYatqS0udYy5i%gDe|t_L+vO!lHs*M0XInfP2UdGU425_ zXg^Ub2?|6Hf6|F(K>^pCb$CxtQgZ!;2xDBkXMUSwO=}rqLiprmAVNM9je}>4R!DFybX?H?EX)+eqyqpbG4YlOG*C z+@5-#^b*VL6bku)wGOJr0&!HWyy-0W{u){4uw5&=8*uYG0Q?;wOu3Fz;+c_Vo@Bgq(ac-0a(IAm%{^9`V^e?tunx8PA7%uRwZuGz1Y2c<#|z0UM(3` z>nGG&IAem*0*`co?4%&xa*KzMY}rt-N)5~n;Rk7g98+^=zO1{*~;$A zv$6U(W@o}BC4KCJ7q(MlhD86z+s;q|6b5rhm@v7mVJFSh<97ni&e*7m*tGa0y27Cr z{1xJ$9cDKrNmgwf`^Spr3A6-qD;{{O$S_UOiTM6}zkdF>)BsOSQBDdP;JF_zLk2Fx z_3-wGi7#6+#)ek6^-p%eY`iN(B^3@%h*i>27NCPSKdcL-8_nM20rY~DM@|>Et=R7w zGU=~P`QY0>a$zJk(PNbL?;(gq0TixL8xH~Cfcbl5M(yAxP|0``HsWur9dMb<--z3+9P`3 z1xZIEh8s`vVFzD%yKRs4j8}L6pfC$KR1FhRx5SS>C%^We)S9C!K&zLtWjNCH4i_`T z1ZPH;*>9@=_PcCffrdFViCJj~zR^IQ9O#Cw5@)EivSs|1A*3Ff?onegk-8oHrGTU! zCo!+nxd)yPl<~|Wwm3h4k)6s7AlBOR_jp;mk|`rxythMV8fR6U=V~pU_vZmtFiAX~ zX$2*q>iohHb0_2NH#X(MJnNPBYYEi1m^T6QIeOKY6S@G!ekK7uMJtUv)<_O5;JcMK zGl#LENrE~0Mz?5RkY?1fl2@C0(PQc0KgDv5N_F#cT?ZMIjdt ze{8V8&>qtKBuN5hqt(Fy8M_L)pH@RuX;}WW zY^PG-xj>;Dbat9BPIw0ZmG=)MSb?-62KK;cbfp~z6b+$@S0@ZCHiq9kB z137BNU)w)s+plY4gG?J}e*rDf^K4&c*xwDk#r_?jF1`Unthb_ZTt{yoyXYBIL5C=3 z6C4o;*t zE`KI84Sc|m`TddhN#mf6ZM5v*&w~en51MPwB2vDmH!HnWM`kXl!*{ARxDMLRucqE| zvQZyAm|u4Es8s6y`wG47vY60_cNJf5755(={@P*!{iJpJx89-B*E)gOzWvs5m=H{J z-8zo{?k&dSHLbs^>V#$oZ!287f&W=U|8vFvXK4kx3sd}Q7I^uc9yD20`kx2pKdWZ{ zWdHo@Y8a%40sl11_e__v$7J~1Khe?u+R*>#@c3V&HBnIW-PfBH)K2SIjDyxUvn&W! zs76XcI>mDWPCSb_l6ZrW*C^8%ULKES&|N8Jk@hFFsG^o~-7adYB?4E}{_jC#F9*{WyzKXB(n)jiX$O zd5cJtuG(CU5aJuRcb#TVa=FTS<(56u(RRy3r5clN?N?n9zMq*R*SE_l4YXY6jdHen zOP>J=IqeP4=zf+aQikbq_7lmN>5>L*U+eE|yOQE%aRJhowkGx&e`mAHeWQq!3_^f` zqIVW=Zd8AbzBkRaa7^A*|K(D`1vz@vz!{_Xix;yUF46b%_OVEcd-Cjr#X|G$%|#awV`{~f*Bd`iIKxb< zfjTgk;x1$PlL`wXa_K}Sm9MpK0aADb-vpZY4m0mM)iHiJ2P~_I7mn?Nq;b3u5OL`<^E`*7>ZdnKISBO5EEkaFXOKc`e-`6Yq ztW#|-X=NGzh+F~2r1HI1ogCj(Au2qEGVI3y8Inv^(3RmOvES74tooety3z87teKk) z)Q^rV{z_AR3dCQPWaWkzInT=Kdz_0Xm>$qLMC`Lray)$LMBdCXAhJoGDD z22v`k&@S8uI^l+JnC#$3?Q<9FH6E{0zNjD41Fo2mPv!dni-<6N0qL4X1-M1c(P9VoDRzD%wZD7E89dz7UBY`K*BQN@>2!) z`uJf}^Jy+f!==dU{6&?V+hvAw@BN_OL3biYxge1;J(Im}L72*ed8seaG=8Y7O;V<- zbwMZc;R*`)y<>^o2QY_+g~Co`o3BenJqGMM;_g~xiz=0Jz9LiYjHm}`*}J>p6;%Jh zmo8|%zp+ujv7Z{(O?He|)Lp}yU%^4tNJ%=l<=CYNE>iR>w%bqj&p6(eE8~|TZ1l*; zrDutYpDo6EX7~M3z>X4fP#YLv=g1|OC#s!&dW^S`QmT}+UbgEPGg4j`0Or~ zG$mtSlKfgno4ih`1i3zWsDro2Cc~)hr}DQtsd(nwj!*^Gr{~U6R(Fue2v6Poz{D5l z&}#k&50yc-4BQ0gyOzxyOXBsz`EXf^Kk%h6^@Sjk2#n;{EXnOHFwdZi9*OlJS8-Oy zn+%)YKO)l%Elc6wc}E9NPF{RdxB2<#t$?@w%d9DN_%}NFaK}dd$QTaN*wl;OFDjE6 z`nhE;rD;^hYjWhygGuso(zC2vZxwDk?MjgPM%?UOE|T?kh=jvR6X=l>PWhhF56fO* z>wprovl>;NtK~ieUP`KK=we|2M6aP5&SD=6QyH z-*xFxDAx&d_@5OREBU;Ao>AlyIGD`?2X^TG_K3T z|McfFmxB#-8@vibu2u)Tepd}~ZufV73!lsnH8=#-IC@Uh7%utiQw3%bri3msnhV8k zMVhE>T-P9qSRt)3Jqy#QkR9x*HU@b~lHNP*DNE1+8^5OnBT)Po6I#`VTT=wjW(l>TQPXs|R+H89M)4evcJM#Rlx-8^6gsM>7_If% zc>gii%3q-0Mp8-j;qph|-@L|{a*}}fPQ3W|LoFw~Q3VBo`yYb7cAor;a&CRnwxDy+ z8W8WVZF!}q&)@If8^t63TXI5S(t7EMkLRv`OHzk`o73lLE{N~@%MI#?E~3nQ6>zAW zJlSt48&R_C8LFeUCstT9H{4b*gKsh4`bHCHX)mLM2QfsaN^?KI|2u6409(1)uu8k+T^qs52i`shr3qH2-n)1Jr~>n|Ry#rHKB zBgW!`-uWNpPOq)umNr$%konn>EFi8r5cS3m2OP(&%b2_77!T)XT%WKEw&}*7vqP(s zon(1s9S}_RLZrg}-&#XS0`{;TBes?|RWXU#J&c%pb%?Xs>E{98e8@45cKZ-E4je*e z&~J+fspVDD2FHZxMaz*G)B3#Ua$6>!RC}i3Eq$u_KyxEvK0~E&lGAx1a!PUA`W4+Q zWfqUhvOu^WHiu1h#rOWY977U#ql4?lMUAxOsm1``tCzp}8LF+C62L;se8c>9Rn;PI zr|q^>%%}~GLF9P>Xt6DJxkv-@AMt$eHN-KFX*rzRSewl6PhIA=zdm%WYv&Aif21-S z{DY;Ueq}NKU~E=c`N1Jqdll3mb`AHOMzM z9Tf{pD~zvP*;A?zs8~*yWg*4U%}wZXnQ%W%oc!VFX2VdP-@8XY-JMoAa8q0E+#jae zGC^VMJk40Pm=+ytZ?hg0 z#WYz*!wYXyAqq=Z$QZBdeWSx6G!A_sIavWU#oq7{rSI9Pn$pJ3#IbD%c1l?3uewqr z%?;uhWA4Rp^neOv6A~C_yD1dIV3&RETcA=2ePdd#Kw-C~d_>u(co?e~&T={$+Ava8 zK{EZl4oB^gv}jaS&5bE3gVxcb%`(=A3BvY6%v0>GvT1(47c z@~Nn#(>C?_MrYN51AoCtA8PIr?HDb><(xIrE1TeLnZcx={ z&@KqdkI^z>UY-$S@hTHEP&vZ$c@GJ^93SLx8+l0p__U-O7Gqle?mn>Be&jr{8(>YS zg|Q+9e$@V!k;%Z0yaUikGQG#Sp&L&n)f#nu_^vX68BjKQKV+(5a+tkIp9=W1>J^6e zVHCT@Z9S_x)9m%$+F3w=h41eb6|*#X>~nB`!*9_cKR$cS6Na`5Bd;O>u&=x%HTL`% zn(eh;|EWm7iP5W5RyEsa#6$fnAE=|dovK;uwZjePK($(BK zVi)@$m-4Tx`m?6~|5J{#t@`{`jgIkWc)A?rzI|d}a^W~)vEw5V z9vyC|7bg=OG!8|veWPiEoh|%CqiH0jkRJG#%;zk=z|J&bn-~_zk>gX%EY(`2iTRq4 z4v{1@Ddrw?nx<4n(j!S;OH{Y^mzy}Wu&*g;LyB@1)%ge1Q=opIlduAD6R6zxRy^3R z+BKTABv~?$n5`Ray3X~0L zOJd;Phw33a5DDT>vDl%AH)LXw6XClqAdgTH!jbNJxe@^{dl|7&^w^+Fj!LVq(MyS3 zIQgwO$)yZvgm?Z)=TYd3X~#omh|8q2+_cUh=1S0dBypAG!!zI#1ta7#j?e5!pSmZ4ub%3%L}Z+& zCion@5V@rHOR1~F;CTUUVww3257v z$ZfI9Fgpoi4mJS^gXN=<7R-j`3PiR!E!QxLqo+4CCEp&G$wWbrMy!(C<(1M!XDI3BP_B zly0ILo23)Au}4Oj^XY)MX+L&_2EKq&#}RyG{U)G-H?AZ~-6kyo%AMd+XrE=Owcta7 z`@3{Z1)5@A8>!$HV7VxEVu@VTeRj1yG*J+iZ?aRCY%V^d!rtQNa%0YY2!bI^rV@Ra z6^XuL!goRVQLDTI=qs*}KQFTex3j-N; z8GF@XpW&=(zZ(N*AFeeR-mHhp-J`zUX^e@R2!Z&C@7RqPXgl^fs~)XoS14bq{@fzW z#!?%h$m@(+C)(?zQ}|AFZRvS!a|>b#B4KYb`_720ck|h*nqxDY;}x9(;ZaH>Pp1m= zN{fK@m-Rev0+>mJMqb`rN!ZhF<~0L1PCZj9t#{S>iu^p6SS$C?dDgPz#K>V^lqQ0e zpaCkRU3J@W&lyZ`$6ugV5JCJpTD+`j!rFK*`YsM&tfJ0$-`Xs#-aW(}mv7w?->oSn zg+&%QnYTyQ_#rc%X~tzQajM55y479-iX)*4%Cw=*Tn` z#u@OgMk+hRMpR#RFGaW~pzrgithX)t;1OV)xKzr@HD)}d^*%R;iNAks`dR0)%YVx0Eu&}*I+&K!_VDcdd_9TxPs$4<++5a-*>ePOREAms4@SW~F0S{C=LdAiW}4 zTN|g*xlN$#3VG3EDvmIJ^6Yku;j}#~UXO_5CHk<5S}2EL7^_AL(~__yYvL?~CJ^`J zqR5bCEt5NLzrU;cM`UUhiQ3e;;a*#SUWX3QIVd4L zdZs#-%TYISpnWjQLV!LP&jBWS9kyWl+0!LW8sBg3;qPvd$$WOksx?BGGk2S(ys>1` z?Y!1hh4(e?3`yspUTM)rkHNamzVKWO!7^sSF@AhGyTH8$nK*;|yP4D3AEU7JKRB=NlE~W7`(lG&|+zNGr0$e$H?bih`gecdun?897P@z`xGpof`O;N=(Ritvu zxYuW$h-H@tYKZ89(5WodIa-5q?*ek{Y!-){34&>wMxyBf+@5fsCyzNEUBHeV>@0 zCD(gmThL6uyG8M6v@d1+dtZvbwj0d3bI`BBxSea`SMIZjv`V5B{w3cM4-kgXihN*e zXsz9^TPfLo*?$yGz!;YtxEYS|Vn^-pCBxcS-oJ9*jw>&nxHw!`p*PP%E$73W%kS|& z(p#Z%2CZFnK!(ZNtW7LHSq!BQZf5bn+9E+|jMc1MPcyg-U8tE{&)^BwS#+g=5riX3 zSkD8RifVn=3+3u=fyW&^2(cGQcE|=1O#d4cTACFK1VI|pi7UW*l%Yj_`_R! zR?KHDov^*CR~-jzdAf~zEhV&J+D-IL%37wvW$B@L{QMpcM@Z;L+&J#rU5{1V$%F5L zyOvHqoNm<*G2v|!1uwfx3jhnZEywwZx-CA6q?bHiHR#97i-V+V!lmKv(vb+2p=W*PRn5-szaMm5jndyI{S)%wpWjT9 g^ZyNg{yk!im7Rs_V*BFr-)#~yl8O=~Vkq?g2WDh!3;+NC diff --git a/.github/no-response.yml b/.github/no-response.yml deleted file mode 100644 index 07d25d766..000000000 --- a/.github/no-response.yml +++ /dev/null @@ -1,8 +0,0 @@ -daysUntilClose: 14 -responseRequiredLabel: more-information-needed -closeComment: > - This issue has been automatically closed because there has been no response - to our request for more information from the original author. With only the - information that is currently in the issue, we don't have enough information - to take action. Please reach out if you have or find the answers we need so - that we can investigate further. diff --git a/.github/workflows/coverage.yml b/.github/workflows/coverage.yml deleted file mode 100644 index a32586d85..000000000 --- a/.github/workflows/coverage.yml +++ /dev/null @@ -1,26 +0,0 @@ -name: Coverage - -on: - push: - branches: - - master - -jobs: - coverage-hive: - runs-on: ubuntu-latest - - container: - image: google/dart:latest - - steps: - - uses: actions/checkout@v1 - - name: Collect coverage - run: | - pub get - pub global activate test_coverage - pub global run test_coverage --exclude "**/js/**" - working-directory: hive - - uses: codecov/codecov-action@v1.0.0 - with: - token: ${{ secrets.CODECOV_TOKEN }} - file: hive/coverage/lcov.info diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 04293a172..131fbbb41 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -3,74 +3,54 @@ name: Dart CI on: [push, pull_request] jobs: - test-hive: + format: + name: Check formatting runs-on: ubuntu-latest - strategy: - matrix: - test-platform: [vm, chrome] - dart-channel: ["2.16.0", "2.18.2"] steps: - - uses: actions/checkout@v1 - - uses: dart-lang/setup-dart@v1 - with: - sdk: ${{ matrix.dart-channel }} + - uses: actions/checkout@v3 + - name: Setup Dart + uses: dart-lang/setup-dart@v1 - name: Install dependencies run: dart pub get - working-directory: hive - - name: Run tests - run: dart pub run test -p ${{ matrix.test-platform }} - working-directory: hive + - name: Check formatting + run: dart format -o none . --set-exit-if-changed - test-hive-flutter: + lint: + name: Check lints runs-on: ubuntu-latest - strategy: - matrix: - flutter-channel: [dev, beta, stable] steps: - - uses: actions/checkout@v1 - - uses: actions/setup-java@v1 - with: - java-version: "12.x" - - uses: subosito/flutter-action@v1 - with: - channel: ${{ matrix.flutter-channel }} - - name: Override dependency version - run: | - echo -e "\ndependency_overrides:\n win32: 2.6.1\n hive:\n path: ../hive" >> pubspec.yaml - working-directory: hive_flutter + - uses: actions/checkout@v3 + - name: Setup Dart + uses: dart-lang/setup-dart@v1 - name: Install dependencies - run: flutter pub get - working-directory: hive_flutter - - name: Run tests - run: flutter test - working-directory: hive_flutter + run: dart pub get + - name: Check Lints + run: dart analyze - test-hive_generator: + test: + name: Test runs-on: ubuntu-latest - strategy: - matrix: - dart-channel: ["2.18.2"] steps: - - uses: actions/checkout@v1 - - uses: dart-lang/setup-dart@v1 - with: - sdk: ${{ matrix.dart-channel }} + - uses: actions/checkout@v3 + - name: Setup Dart + uses: dart-lang/setup-dart@v1 - name: Install dependencies run: dart pub get - working-directory: hive_generator/example - - name: Generate build_runner output - run: dart run build_runner build --delete-conflicting-outputs - working-directory: hive_generator/example + - name: Run tests + run: dart test - check-score: + coverage: + name: Code Coverage runs-on: ubuntu-latest - strategy: - matrix: - package: [hive, hive_generator, hive_flutter] steps: - - uses: actions/checkout@v1 - - uses: axel-op/dart-package-analyzer@v3 + - uses: actions/checkout@v3 + - name: Setup Flutter + uses: subosito/flutter-action@v2 + - name: Install dependencies + run: flutter pub get + - name: Code Coverage + run: flutter test --coverage --coverage-path lcov.info + - name: Upload Coverage + uses: codecov/codecov-action@v3 with: - githubToken: ${{ secrets.GITHUB_TOKEN }} - relativePath: ${{ matrix.package }} - minAnnotationLevel: warning + files: lcov.info diff --git a/.gitignore b/.gitignore index 03552a4f4..62a2881fb 100644 --- a/.gitignore +++ b/.gitignore @@ -103,3 +103,7 @@ web_worker.dart.js* !**/ios/**/default.pbxuser !**/ios/**/default.perspectivev3 !/packages/flutter_tools/test/data/dart_dependencies_test/**/.packages + +*.dylib +*.so +*.dll \ No newline at end of file diff --git a/hive/CHANGELOG.md b/CHANGELOG.md similarity index 100% rename from hive/CHANGELOG.md rename to CHANGELOG.md diff --git a/LICENSE b/LICENSE index e93f5543e..f49a4e16e 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ Apache License Version 2.0, January 2004 - https://www.apache.org/licenses/ + http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION @@ -175,16 +175,27 @@ END OF TERMS AND CONDITIONS - Copyright 2019 Simon Leier + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - https://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and - limitations under the License. + limitations under the License. \ No newline at end of file diff --git a/README.md b/README.md deleted file mode 120000 index aace88ba4..000000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -hive/README.md \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 000000000..3761fe832 --- /dev/null +++ b/README.md @@ -0,0 +1,401 @@ +

+ +

+

Fast, Enjoyable & Secure NoSQL Database

+ +

+ + + + + + + + + + + + + + + +

+ +Hive is a lightweight and blazing-fast key-value database made for Flutter and Dart. + +## Features 🌟 + +- 🌍 Cross-platform: mobile, desktop, browser +- 🚀 Need for Speed? Hive's got it in spades. +- 💡 Simple, powerful, & intuitive API +- 🔐 Tighter than Fort Knox: Encryption is built right in. +- 🧠 Think multi-tasking: Hive supports multiple isolates. +- 🔋 No need to pack extras: Hive comes with batteries included. + +> 🐝 A single bee can visit 5,000 flowers in a day! + +## Buzz into Action 🍯 + +Feeling the excitement? Great! Let's help you take your first flight with Hive. + +#### 🔗 Add dependencies + +To kickstart the journey add `hive`, `isar_flutter_libs` and `path_provider` to your `pubspec.yaml`. + +```yaml +dependencies: + hive: ^4.0.0 + isar_flutter_libs: ^4.0.0-dev.13 + path_provider: ^2.1.0 +``` + +Pssst! 🤫 `path_provider` will help you to find the optimal directory for each platform. + +#### 🏡 Designate a Home + +Hive needs a place to call home. Using `path_provider` we can find a valid directory. + +```dart +void main() async { + WidgetsFlutterBinding.ensureInitialized(); + final dir = await getApplicationDocumentsDirectory(); + Hive.defaultDirectory = dir.path; + + // ... +} +``` + +#### 🏁 And... Action! + +🎉 Woohoo! You're all set. Dive right in and let's get buzzing with Hive. + +```dart +import 'package:hive/hive.dart'; + +final box = Hive.box(); +box.put('name', 'David'); + +final name = box.get('name'); +print('Name: $name'); +``` + +> 🐝 Honeybees can fly at a speed of up to 30 kilometers per hour! + +# 📚 Hive Handbook + +In Hive, data is neatly organized into containers known as boxes. Think of boxes as tables you'd find in SQL, but far more flexible — they don't stick to a set structure and can contain a variety of data. Boxes can be encrypted to store sensitive data. + +## Table of Contents + +Want to jump to a specific section? Here's a handy table of contents: + +- [Opening Boxes](#-opening-boxes) +- [Closing Boxes](#-bidding-adieu-closing-boxes) +- [Inserting](#%EF%B8%8F-filling-the-honeycomb-inserting-data) +- [Reading](#-extracting-honey-i-mean-data) +- [Deleting](#-deleting-data) +- [Using Boxes like Lists](#-using-boxes-like-lists) +- [Type safety](#%EF%B8%8F-type-safety) +- [Non-primitive Objects](#-bee-yond-the-basics-non-primitive-objects) +- [Transactions](#-transactions) +- [FAQ](#-buzzworthy-questions-faq) + +> 🐝 Bees have five eyes – three simple eyes on top of the head, and two compound eyes, with numerous hexagonal facets. + +### 📦 Opening Boxes + +Your journey with Hive begins with opening your first box. Trust me, it's unbee-lievably easy: + +```dart +final box = Hive.box(name: 'myBox'); +``` + +When you call `Hive.box(name: 'myBox')` for the first time with a given name, Hive will craft a new box for you. If you call it again with the same name, Hive will return the already existing box. + +You can use `Hive.box()` without providing a name. In this case, Hive will return the default box. + +There are optional parameters you can pass to `Hive.box()`: + +| Parameter | Description | +| --------------- | ------------------------------------------------------------------------- | +| `name` | Label your box with a distinct name | +| `directory` | Select a home for your box. If omitted, Hive uses the `defaultDirectory`. | +| `encryptionKey` | Hand over this key, and Hive will encrypt your box. Keep it safe! | +| `maxSizeMiB` | The maximum size of the box in MiB. Go for a modest number. | + +> 🐝 Beeswax, which is secreted from the abdomen of worker bees, is used to construct the honeycomb. + +### 🌂 Bidding Adieu: Closing Boxes + +It's not advised to close boxes that might be accessed again. This prevents unnecessary overhead of reopening the box and ensures smooth data retrieval. + +To close a box just call `box.close()`. Wipe the box from the face of the earth with `box.deleteFromDisk()`. + +> 🐝 When a bee finds a good source of nectar, it flies back to the hive and shows its friends where the nectar source is by doing a dance. + +### ✍️ Filling the Honeycomb: Inserting Data + +Once we have a box, it's time to fill it with sweet data! At its core, a box is just a key-value store. String keys are mapped to arbitrary primitive values. You can think of a box as a persisted `Map`. + +```dart +final box = Hive.box(); +box.put('danceMoves', 'Waggle Dance'); +box.put('wingSpeed', 200); +``` + +Updating values? If a particular key already exists, Hive simply updates its corresponding value. And complex types like lists and maps? They're in too! + +```dart +box.put('friends', ['Buzzy', 'Stinger', 'Honey']); +box.put('memories', {'firstFlight': 'Sunny Day', 'bestNectar': 'Rose'}); +``` + +Instead of `.put()` you prefer the syntax of maps? Hive gets you: + +```dart +box['danceMoves'] = 'Round Dance'; +box['wingSpeed'] = 220; +``` + +Got a bucket of honey facts? Drop them all at once with `box.putAll()`: + +```dart +box.putAll({'favoriteFlower': 'Lavender', 'wingSpeed': 210}); +``` + +> 🐝 A single bee colony can produce anywhere from 30 to 100 pounds of honey in a year, depending on the availability of nectar sources. + +### 👀 Extracting Honey... I mean, Data! + +Need a snippet of info from your Hive? No need to don the beekeeper suit; just scoop it out using `box.get()` or `box.getAll()`. If a key doesn't exist, `box.get()` simply return a `null`. But fret not; you can tell it to have a backup plan: + +```dart +final box = Hive.box(name: 'beeees'); +final fav = box.get('favoriteFlower'); +final moves = box.get('danceMoves', defaultValue: 'waggle'); +``` + +Oh, and if you're feeling fancy, use the `[]` operator for a more stylish approach: + +```dart +final fav = box['favoriteFlower']; +final moves = box['danceMoves'] ?? 'waggle'; +``` + +> 🐝 Worker bees are the only bees most people ever see flying around outside the hive. They're female, and their roles are to forage for food, build and protect the hive, and more. + +### 🧹 Deleting Data + +Time for some spring cleaning in the hive! To remove a single entry from your box, use `box.delete()`: + +```dart +final deleted = box.delete('lavenderHoney'); +print('Honey eaten: $deleted'); // Honey eaten: true +``` + +Perhaps it's time for a complete reset, making space for a fresh batch of honey. If you're looking to remove all key-value pairs from a box, use `box.clear()`: + +```dart +box.clear(); +``` + +> 🐝 Bees have been around for more than 30 million years! Their long history predates the existence of humans and even dinosaurs. + +### ✨ Using Boxes like Lists + +In the bee world, honeycombs aren't just random compartments; they're methodically organized. Similarly, while we've been viewing Hive boxes as maps so far, they can be used just like lists: + +```dart +final box = Hive.box(); + +box.add('Rose'); +box.add('Tulip'); + +print(box.getAt(0)); // Rose +print(box.getAt(1)); // Tulip +``` + +But remember, bees can't retrieve honey from a comb that's empty or doesn't exist. Likewise, index-based operations will throw an error if you try an index out of bounds: + +```dart +final box = Hive.box(); +box.add('Daisy'); +print(box.getAt(1)); // This will make the bees buzz in confusion +``` + +Even if we insert a key-value pair we can still access the values by index. + +```dart +final box = Hive.box(); + +box.add('Lily'); +box.put('key', 'Orchid'); + +print(box.getAt(0)); // Lily +print(box.getAt(1)); // Orchid +``` + +Of course, we can also use the `[]` operator in combination with indexes : + +```dart +final box = Hive.box(); + +box.add('Marigold'); +print(box[0]); // Marigold + +box[0] = 'Daffodil'; +box[1] = 'Bluebell'; // This will get the bees in a whirl +``` + +> 🐝 To produce one pound of honey, a hive's bees must visit 2 million flowers and fly over 55,000 miles. + +### 🛡️ Type safety + +Safety is the bee's priority! To keep your data sweet and pure boxes have an optional generic type parameter. It allows you to store only values of a specific type in a box: + +```dart +final box = Hive.box(name: 'BeeTreasures'); +box.put('DaisyDance', 'SweetNectarShake'); +box.put('RoseRumba', 'GoldenPollenParty'); +box.put('TulipTango', 777); // Error - You can't fool the bees! +``` + +> 🐝 Bees have two stomachs. One is for eating, and the other is for storing nectar collected from flowers or water so they can carry it back to their hive. Talk about a sweet backpack! + +### 🧩 Bee-yond the Basics: Non-primitive Objects + +Hive goes beyond storing just basic data types! Along with primitives, lists, and maps, Hive can store any Dart object of your liking. The only buzz you need to know? Your object should come equipped with a `.fromJson()` and `.toJson()` method: + +```dart +class Bee { + Bee({required this.name, required this.role}); + + factory Bee.fromJson(Map json) => Bee( + name: json['name'] as String, + role: json['role'] as String, + ); + + final String name; + final String role; + + Map toJson() => { + 'name': name, + 'role': role, + }; +} +``` + +Before our bee-friends can buzz around in Hive, you need to do the beekeeper's job and register the `Bee` class: + +```dart +Hive.registerAdapter('Bee', Bee.fromJson); +``` + +Now, you're all set to let your bees fly: + +```dart +final box = Hive.box(); + +var bumble = Bee() + ..name = 'Bumble' + ..role = 'Worker'; +box.put('BumbleID', bumble); + +print(box.get('BumbleID')); // Bumble - Worker +``` + +> 🐝 Bees are responsible for pollinating about one-third of the world's food crops. + +### 🪢 Transactions + +Transactions are an efficient way to update multiple values at once. They are also useful if you want to make sure that a Box is not changed by other code while you are working with it. + +```dart +final box = Hive.box(); + +box.write(() { + box.store('nectar1', 'GoldenNectar'); + box.store('nectar2', 'WildflowerBrew'); + box.store('nectar3', 'CloverDew'); +}); + +box.read(() { + box.get('nectar1'); +}); +``` + +Changes made in a transaction are always atomic. Either all changes are applied or none of them. So if an error occurs during a transaction, the box will not be changed. + +```dart +final box = Hive.box(); +box.put('honeyLevel', 5); + +box.write(() { + box.put('honeyLevel', 6); + throw Exception('Oh no!!!'); +}); + +print(box.get('honeyLevel')); // 5 +``` + +> 🐝 Bees can recognize human faces, and they can even be trained to associate a picture of a face with sweet treats! + +### 🌼 Buzzworthy Questions: FAQ + +#### To bee or not to bee: Hive or Isar? + +> It's not always black and yellow! Both Hive and Isar have their sweet spots. Hive is a lightweight wrapper around Isar so if you are looking for a simple key-value store, Hive is the way to go. Isar is the way to go if you need queries, relations, and more advanced features. + +#### Will using Hive make my app as fast as a bee? + +> While we can't promise your app will gain wings, Hive sure will give it the speed it deserves. Hive is very resource efficient and optimized for mobile devices. Flutter like a butterfly, sting like a bee! + +#### Where in the beehive does Hive hide my honey... I mean, data? + +> Remember the `defaultDirectory` we set at the beginning? That's where Hive stores your data in a file named `yourBoxName.isar` or `yourBoxName.sqlite`. + +#### I've got some bee-autiful images! Can I store them directly in Hive? + +> While you might be tempted to put those pics right into Hive, it's best to store your images and other binary data as files outside the Hive. You can then store the file path in Hive. Think of it like leaving honey out in the open; it's better to keep it neatly stored in the appropriate place. + +#### Yikes! What if my app meets an untimely demise (gets killed)? What becomes of my Hive? + +> No need for a bee-mergency! If your app buzzes off unexpectedly, Hive ensures that your data remains safe and sound. Transactions are atomic, so either all changes are applied or none of them. If an error occurs during a transaction, the box will not be changed. + +#### How does Hive keep our data safe from sticky fingers? + +> We've got the queen's guard on duty! If you encrypt your box Hive uses 256-bit AES in CBC mode. Every database page is safeguarded separately, ensuring your sweet stuff remains secure and only accessible to those with the right key. Buzz-worthy protection, right? + +#### When should I rally the troops and use transactions? + +> Just like a hive making big decisions together, you'll want to use transactions when you have several operations that should be executed together. If one fails, they all fail. It ensures your data stays consistent, safe, and buzzing in harmony! + +#### What if I'm allergic to bees? + +> No worries! Hive is 100% sting-free, although we're pretty sure you'll get a buzz out of its performance. + +#### Hive operations are synchronous. Doesn't that make the bee waltz a bit slow? + +> Hive is incredibly fast an efficient. It's built on top of Isar, a high-performance database engine. If you want to keep database operations away from your UI isolate, you can use `compute()` or `Isolate.run()` to run them in a separate isolate. + +#### How many boxes should a wise beekeeper have? + +> While the sky's the limit in the world of bees, in Hive, every box becomes a separate file. So, even if you're buzzing with excitement, it's wise not to overdo it. + +### 📜 License + +``` +Copyright 2023 Simon Choi + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +``` diff --git a/analysis_options.yaml b/analysis_options.yaml new file mode 100644 index 000000000..85ad35931 --- /dev/null +++ b/analysis_options.yaml @@ -0,0 +1,11 @@ +include: package:very_good_analysis/analysis_options.yaml +analyzer: + exclude: + - "**/*.g.dart" + +linter: + rules: + cascade_invocations: false + use_string_in_part_of_directives: false + always_put_required_named_parameters_first: false + use_setters_to_change_properties: false diff --git a/hive/.pubignore b/hive/.pubignore deleted file mode 100644 index b1c1a4ab6..000000000 --- a/hive/.pubignore +++ /dev/null @@ -1 +0,0 @@ -!lib/src/backend/js/web_worker/web_worker.dart.js* diff --git a/hive/LICENSE b/hive/LICENSE deleted file mode 100644 index e93f5543e..000000000 --- a/hive/LICENSE +++ /dev/null @@ -1,190 +0,0 @@ - Apache License - Version 2.0, January 2004 - https://www.apache.org/licenses/ - - TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - - 2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - - 3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. - - 4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: - - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and - - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and - - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and - - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. - - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. - - 5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. - - 6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. - - 7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. - - 8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. - - 9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. - - END OF TERMS AND CONDITIONS - - Copyright 2019 Simon Leier - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - https://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. diff --git a/hive/README.md b/hive/README.md deleted file mode 100644 index a4053f95f..000000000 --- a/hive/README.md +++ /dev/null @@ -1,227 +0,0 @@ -

- -

-

Fast, Enjoyable & Secure NoSQL Database

- -[![GitHub Workflow Status (branch)](https://img.shields.io/github/actions/workflow/status/hivedb/hive/test.yml?label=tests&labelColor=333940&logo=github)](https://github.com/hivedb/hive/actions) -[![Codecov branch](https://img.shields.io/codecov/c/github/hivedb/hive/nndb?labelColor=333940&logo=codecov&logoColor=white)](https://codecov.io/gh/hivedb/hive) -[![Pub Version](https://img.shields.io/pub/v/hive?label=pub.dev&labelColor=333940&logo=dart)](https://pub.dev/packages/hive) -[![GitHub](https://img.shields.io/github/license/hivedb/hive?color=%23007A88&labelColor=333940&logo=apache)](https://github.com/hivedb/hive/blob/master/LICENSE) - -Hive is a lightweight and blazing fast key-value database written in pure Dart. Inspired by [Bitcask](https://en.wikipedia.org/wiki/Bitcask). - -[Documentation & Samples](https://docs.hivedb.dev/) 📖 - -### Before you start -Consider using [Isar](https://github.com/isar/isar), a Flutter database by the author of Hive that is superior in every way! - -## Features - -- 🚀 Cross platform: mobile, desktop, browser -- ⚡ Great performance (see [benchmark](#benchmark)) -- ❤️ Simple, powerful, & intuitive API -- 🔒 Strong encryption built in -- 🎈 **NO** native dependencies -- 🔋 Batteries included - -## Getting Started - -Check out the [Quick Start](https://docs.hivedb.dev) documentation to get started. - -## Usage - -You can use Hive just like a map. It is not necessary to await `Futures`. - -```dart -var box = Hive.box('myBox'); - -box.put('name', 'David'); - -var name = box.get('name'); - -print('Name: $name'); -``` - -## BoxCollections - -`BoxCollections` are a set of boxes which can be similarly used as normal boxes, except of that -they dramatically improve speed on web. They support opening and closing all boxes of a collection -at once and more efficiently store data in indexed DB on web. - -Aside, they also expose Transactions which can be used to speed up tremendous numbers of database -transactions on web. - -On `dart:io` platforms, there is no performance gain by BoxCollections or Transactions. Only -BoxCollections might be useful for some box hierarchy and development experience. - -```dart -// Create a box collection - final collection = await BoxCollection.open( - 'MyFirstFluffyBox', // Name of your database - {'cats', 'dogs'}, // Names of your boxes - path: './', // Path where to store your boxes (Only used in Flutter / Dart IO) - key: HiveCipher(), // Key to encrypt your boxes (Only used in Flutter / Dart IO) - ); - - // Open your boxes. Optional: Give it a type. - final catsBox = collection.openBox('cats'); - - // Put something in - await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4}); - await catsBox.put('loki', {'name': 'Loki', 'age': 2}); - - // Get values of type (immutable) Map? - final loki = await catsBox.get('loki'); - print('Loki is ${loki?['age']} years old.'); - - // Returns a List of values - final cats = await catsBox.getAll(['loki', 'fluffy']); - print(cats); - - // Returns a List of all keys - final allCatKeys = await catsBox.getAllKeys(); - print(allCatKeys); - - // Returns a Map with all keys and entries - final catMap = await catsBox.getAllValues(); - print(catMap); - - // delete one or more entries - await catsBox.delete('loki'); - await catsBox.deleteAll(['loki', 'fluffy']); - - // ...or clear the whole box at once - await catsBox.clear(); - - // Speed up write actions with transactions - await collection.transaction( - () async { - await catsBox.put('fluffy', {'name': 'Fluffy', 'age': 4}); - await catsBox.put('loki', {'name': 'Loki', 'age': 2}); - // ... - }, - boxNames: ['cats'], // By default all boxes become blocked. - readOnly: false, - ); -``` - - -## Store objects - -Hive not only supports primitives, lists and maps but also any Dart object you like. You need to generate a type adapter before you can store objects. - -```dart -@HiveType(typeId: 0) -class Person extends HiveObject { - - @HiveField(0) - String name; - - @HiveField(1) - int age; -} -``` - -Extending `HiveObject` is optional but it provides handy methods like `save()` and `delete()`. - -```dart -var box = await Hive.openBox('myBox'); - -var person = Person() - ..name = 'Dave' - ..age = 22; -box.add(person); - -print(box.getAt(0)); // Dave - 22 - -person.age = 30; -person.save(); - -print(box.getAt(0)) // Dave - 30 -``` - -## Hive ❤️ Flutter - -Hive was written with Flutter in mind. It is a perfect fit if you need a lightweight datastore for your app. After adding the required dependencies and initializing Hive, you can use Hive in your project: - -```dart -import 'package:hive/hive.dart'; -import 'package:hive_flutter/hive_flutter.dart'; - -class SettingsPage extends StatelessWidget { - @override - Widget build(BuildContext context) { - return ValueListenableBuilder( - valueListenable: Hive.box('settings').listenable(), - builder: (context, box, widget) { - return Switch( - value: box.get('darkMode'), - onChanged: (val) { - box.put('darkMode', val); - } - ); - }, - ); - } -} -``` - -Boxes are cached and therefore fast enough to be used directly in the `build()` method of Flutter widgets. - -### Native AES crypto implementation - -When using Flutter, Hive supports native encryption using [package:cryptography](https://pub.dev/packages/cryptography) -and [package:cryptography_flutter](https://pub.dev/packages/cryptography_flutter). - -Native AES implementations tremendously speed up operations on encrypted Boxes. - -Please follow these steps: - -1. add dependency to pubspec.yaml - -```yaml -dependencies: - cryptography_flutter: ^2.0.2 -``` - -2. enable native implementations - -```dart -import 'package:cryptography_flutter/cryptography_flutter.dart'; - -void main() { - // Enable Flutter cryptography - FlutterCryptography.enable(); - - // .... -} -``` - -## Benchmark - -| 1000 read iterations | 1000 write iterations | -| :--------------------------------------------------------------------------------------------------: | :-----------------------------------------------------------------------------------------: | -| ![](https://raw.githubusercontent.com/hivedb/hive/master/.github/benchmark_read.png) | ![](https://raw.githubusercontent.com/hivedb/hive/master/.github/benchmark_write.png) | -| SharedPreferences is on par with Hive when it comes to read performance. SQLite performs much worse. | Hive greatly outperforms SQLite and SharedPreferences when it comes to writing or deleting. | - -The benchmark was performed on a Oneplus 6T with Android Q. You can [run the benchmark yourself](https://github.com/hivedb/hive_benchmark). - -\*Take this benchmark with a grain of salt. It is very hard to compare databases objectively since they were made for different purposes. - -### Licence - -``` -Copyright 2019 Simon Leier - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. -``` diff --git a/hive/analysis_options.yaml b/hive/analysis_options.yaml deleted file mode 100644 index 8331402a3..000000000 --- a/hive/analysis_options.yaml +++ /dev/null @@ -1,44 +0,0 @@ -include: package:lints/recommended.yaml -analyzer: - exclude: - - "**/*.g.dart" - - "example/**" - errors: - invalid_use_of_protected_member: error - always_declare_return_types: error -linter: - rules: - - avoid_function_literals_in_foreach_calls - - avoid_renaming_method_parameters - - avoid_unused_constructor_parameters - - await_only_futures - - camel_case_types - - cancel_subscriptions - - comment_references - - constant_identifier_names - - control_flow_in_finally - - directives_ordering - - empty_statements - - implementation_imports - - invariant_booleans - - iterable_contains_unrelated_type - - list_remove_unrelated_type - - lines_longer_than_80_chars - - no_adjacent_strings_in_list - - non_constant_identifier_names - - only_throw_errors - - overridden_fields - - package_api_docs - - package_names - - package_prefixed_library_names - - prefer_initializing_formals - - prefer_interpolation_to_compose_strings - - prefer_typing_uninitialized_variables - - test_types_in_equals - - throw_in_finally - - type_init_formals - - unnecessary_brace_in_string_interps - - unnecessary_getters_setters - - unnecessary_lambdas - - unnecessary_null_aware_assignments - - unnecessary_statements diff --git a/hive/example/lib/main.dart b/hive/example/lib/main.dart deleted file mode 100644 index 25c8564cb..000000000 --- a/hive/example/lib/main.dart +++ /dev/null @@ -1,43 +0,0 @@ -import 'dart:io'; - -import 'package:hive/hive.dart'; - -part 'main.g.dart'; - -@HiveType(typeId: 1) -class Person { - Person({required this.name, required this.age, required this.friends}); - - @HiveField(0) - String name; - - @HiveField(1) - int age; - - @HiveField(2) - List friends; - - @override - String toString() { - return '$name: $age'; - } -} - -void main() async { - var path = Directory.current.path; - Hive - ..init(path) - ..registerAdapter(PersonAdapter()); - - var box = await Hive.openBox('testBox'); - - var person = Person( - name: 'Dave', - age: 22, - friends: ['Linda', 'Marc', 'Anne'], - ); - - await box.put('dave', person); - - print(box.get('dave')); // Dave: 22 -} diff --git a/hive/example/lib/main.g.dart b/hive/example/lib/main.g.dart deleted file mode 100644 index 5c486bddf..000000000 --- a/hive/example/lib/main.g.dart +++ /dev/null @@ -1,47 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'main.dart'; - -// ************************************************************************** -// TypeAdapterGenerator -// ************************************************************************** - -class PersonAdapter extends TypeAdapter { - @override - final int typeId = 1; - - @override - Person read(BinaryReader reader) { - final numOfFields = reader.readByte(); - final fields = { - for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), - }; - return Person( - name: fields[0] as String, - age: fields[1] as int, - friends: (fields[2] as List).cast(), - ); - } - - @override - void write(BinaryWriter writer, Person obj) { - writer - ..writeByte(3) - ..writeByte(0) - ..write(obj.name) - ..writeByte(1) - ..write(obj.age) - ..writeByte(2) - ..write(obj.friends); - } - - @override - int get hashCode => typeId.hashCode; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is PersonAdapter && - runtimeType == other.runtimeType && - typeId == other.typeId; -} diff --git a/hive/example/pubspec.yaml b/hive/example/pubspec.yaml deleted file mode 100644 index 497bea585..000000000 --- a/hive/example/pubspec.yaml +++ /dev/null @@ -1,17 +0,0 @@ -name: example - -dependencies: - hive: any - -dev_dependencies: - hive_generator: any - build_runner: any - -environment: - sdk: '>=2.12.0-0 <3.0.0' - -dependency_overrides: - hive: - path: ../ - hive_generator: - path: ../../hive_generator \ No newline at end of file diff --git a/hive/lib/hive.dart b/hive/lib/hive.dart deleted file mode 100644 index e26ac8c9a..000000000 --- a/hive/lib/hive.dart +++ /dev/null @@ -1,66 +0,0 @@ -/// Hive is a lightweight and blazing fast key-value store written in pure Dart. -/// It is strongly encrypted using AES-256. -library hive; - -import 'dart:async'; -import 'dart:convert'; -import 'dart:math'; -import 'dart:typed_data'; - -import 'package:crypto/crypto.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/box/default_compaction_strategy.dart'; -import 'package:hive/src/box/default_key_comparator.dart'; -import 'package:hive/src/crypto/aes_cbc_pkcs7.dart'; -import 'package:hive/src/crypto/crc32.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:hive/src/object/hive_object.dart'; -import 'package:hive/src/util/extensions.dart'; -import 'package:meta/meta.dart'; - -import 'src/backend/js/web_worker/web_worker_stub.dart' - if (dart.library.html) 'src/backend/js/web_worker/web_worker.dart'; - -export 'package:hive/src/box_collection/box_collection.dart'; - -export 'src/backend/js/web_worker/web_worker_stub.dart' - if (dart.library.html) 'src/backend/js/web_worker/web_worker.dart'; -export 'src/backend/stub/storage_backend_memory.dart'; -export 'src/object/hive_object.dart' show HiveObject, HiveObjectMixin; - -part 'src/annotations/hive_field.dart'; - -part 'src/annotations/hive_type.dart'; - -part 'src/binary/binary_reader.dart'; - -part 'src/binary/binary_writer.dart'; - -part 'src/box/box.dart'; - -part 'src/box/box_base.dart'; - -part 'src/box/lazy_box.dart'; - -part 'src/crypto/hive_aes_cipher.dart'; - -part 'src/crypto/hive_cipher.dart'; - -part 'src/hive.dart'; - -part 'src/hive_error.dart'; - -part 'src/object/hive_collection.dart'; - -part 'src/object/hive_list.dart'; - -part 'src/object/hive_storage_backend_preference.dart'; - -part 'src/registry/type_adapter.dart'; - -part 'src/registry/type_registry.dart'; - -/// Global constant to access Hive. -// ignore: non_constant_identifier_names -final HiveInterface Hive = HiveImpl(); diff --git a/hive/lib/src/adapters/big_int_adapter.dart b/hive/lib/src/adapters/big_int_adapter.dart deleted file mode 100644 index 6375248d3..000000000 --- a/hive/lib/src/adapters/big_int_adapter.dart +++ /dev/null @@ -1,21 +0,0 @@ -import 'package:hive/hive.dart'; - -/// Adapter for BigInt -class BigIntAdapter extends TypeAdapter { - @override - final typeId = 17; - - @override - BigInt read(BinaryReader reader) { - var len = reader.readByte(); - var intStr = reader.readString(len); - return BigInt.parse(intStr); - } - - @override - void write(BinaryWriter writer, BigInt obj) { - var intStr = obj.toString(); - writer.writeByte(intStr.length); - writer.writeString(intStr, writeByteCount: false); - } -} diff --git a/hive/lib/src/adapters/date_time_adapter.dart b/hive/lib/src/adapters/date_time_adapter.dart deleted file mode 100644 index 0b27ec2d8..000000000 --- a/hive/lib/src/adapters/date_time_adapter.dart +++ /dev/null @@ -1,42 +0,0 @@ -import 'package:hive/hive.dart'; - -/// Adapter for DateTime -class DateTimeAdapter extends TypeAdapter { - @override - final typeId = 16; - - @override - T read(BinaryReader reader) { - var millis = reader.readInt(); - return DateTimeWithoutTZ.fromMillisecondsSinceEpoch(millis) as T; - } - - @override - void write(BinaryWriter writer, DateTime obj) { - writer.writeInt(obj.millisecondsSinceEpoch); - } -} - -class DateTimeWithoutTZ extends DateTime { - DateTimeWithoutTZ.fromMillisecondsSinceEpoch(int millisecondsSinceEpoch) - : super.fromMillisecondsSinceEpoch(millisecondsSinceEpoch); -} - -/// Alternative adapter for DateTime with time zone info -class DateTimeWithTimezoneAdapter extends TypeAdapter { - @override - final typeId = 18; - - @override - DateTime read(BinaryReader reader) { - var millis = reader.readInt(); - var isUtc = reader.readBool(); - return DateTime.fromMillisecondsSinceEpoch(millis, isUtc: isUtc); - } - - @override - void write(BinaryWriter writer, DateTime obj) { - writer.writeInt(obj.millisecondsSinceEpoch); - writer.writeBool(obj.isUtc); - } -} diff --git a/hive/lib/src/adapters/ignored_type_adapter.dart b/hive/lib/src/adapters/ignored_type_adapter.dart deleted file mode 100644 index c4cabfaf6..000000000 --- a/hive/lib/src/adapters/ignored_type_adapter.dart +++ /dev/null @@ -1,15 +0,0 @@ -import 'package:hive/hive.dart'; - -/// Not part of public API -class IgnoredTypeAdapter implements TypeAdapter { - const IgnoredTypeAdapter([this.typeId = 0]); - - @override - final int typeId; - - @override - T? read(BinaryReader reader) => null; - - @override - void write(BinaryWriter writer, obj) {} -} diff --git a/hive/lib/src/annotations/hive_field.dart b/hive/lib/src/annotations/hive_field.dart deleted file mode 100644 index 9eba2119c..000000000 --- a/hive/lib/src/annotations/hive_field.dart +++ /dev/null @@ -1,26 +0,0 @@ -part of hive; - -/// Annotate all fields you want to persist with [HiveField]. -class HiveField { - /// The index of this field. - final int index; - - /// The default value of this field for class hive types. - /// - /// In enum hive types set `true` to use this enum value as default value - /// instead of null in null-safety. - /// - /// ```dart - /// @HiveType(typeId: 1) - /// enum MyEnum { - /// @HiveField(0) - /// apple, - /// - /// @HiveField(1, defaultValue: true) - /// pear - /// } - /// ``` - final dynamic defaultValue; - - const HiveField(this.index, {this.defaultValue}); -} diff --git a/hive/lib/src/annotations/hive_type.dart b/hive/lib/src/annotations/hive_type.dart deleted file mode 100644 index 6b4c9c6d4..000000000 --- a/hive/lib/src/annotations/hive_type.dart +++ /dev/null @@ -1,22 +0,0 @@ -part of hive; - -/// Annotate classes with [HiveType] to generate a `TypeAdapter`. -class HiveType { - /// The typeId of the annotated class. - final int typeId; - - /// The name of the generated adapter. - final String? adapterName; - - /// This parameter can be used to keep track of old fieldIds which must not - /// be reused. The generator will throw an error if a legacy fieldId is - /// used again. - // final List legacyFieldIds; - - /// If [adapterName] is not set, it'll be `"YourClass" + "Adapter"`. - const HiveType({ - required this.typeId, - this.adapterName, - //this.legacyFieldIds, - }); -} diff --git a/hive/lib/src/backend/js/backend_manager.dart b/hive/lib/src/backend/js/backend_manager.dart deleted file mode 100644 index 869e8dd0b..000000000 --- a/hive/lib/src/backend/js/backend_manager.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/backend/stub/backend_manager_memory.dart'; - -import 'native/backend_manager.dart' as native; -import 'web_worker/backend_manager.dart' as web_worker; - -/// Opens IndexedDB databases -abstract class BackendManager { - // caching the backend manager as it makes no sense to have different backends - // within the same application - static BackendManagerInterface? _manager; - - const BackendManager._(); - - static BackendManagerInterface select( - [HiveStorageBackendPreference? backendPreference]) { - if (_manager == null) { - if (backendPreference is HiveStorageBackendPreferenceWebWorker) { - _manager = web_worker.BackendManagerWebWorker(backendPreference); - } else if (backendPreference == HiveStorageBackendPreference.native || - backendPreference == null) { - _manager = native.BackendManager(); - } else if (backendPreference == HiveStorageBackendPreference.memory) { - _manager = BackendManagerMemory(); - } else { - throw UnimplementedError( - '$backendPreference is not a known HiveStorageBackendPreference'); - } - } - return _manager!; - } -} diff --git a/hive/lib/src/backend/js/native/backend_manager.dart b/hive/lib/src/backend/js/native/backend_manager.dart deleted file mode 100644 index f5a9bd3aa..000000000 --- a/hive/lib/src/backend/js/native/backend_manager.dart +++ /dev/null @@ -1,135 +0,0 @@ -import 'dart:async'; -import 'dart:html'; -import 'dart:indexed_db'; -import 'dart:js' as js; -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/js/native/storage_backend_js.dart'; -import 'package:hive/src/backend/storage_backend.dart'; - -/// Opens IndexedDB databases -class BackendManager implements BackendManagerInterface { - IdbFactory? get indexedDB => js.context.hasProperty('window') - ? window.indexedDB - : WorkerGlobalScope.instance.indexedDB; - - @override - Future open(String name, String? path, bool crashRecovery, - HiveCipher? cipher, String? collection) async { - // compatibility for old store format - final databaseName = collection ?? name; - final objectStoreName = collection == null ? 'box' : name; - - var db = - await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if (!(db.objectStoreNames ?? []).contains(objectStoreName)) { - db.createObjectStore(objectStoreName); - } - }); - - // in case the objectStore is not contained, re-open the db and - // update version - if (!(db.objectStoreNames ?? []).contains(objectStoreName)) { - db = await indexedDB!.open( - databaseName, - version: (db.version ?? 1) + 1, - onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if (!(db.objectStoreNames ?? []).contains(objectStoreName)) { - db.createObjectStore(objectStoreName); - } - }, - ); - } - - return StorageBackendJs(db, cipher, objectStoreName); - } - - @override - Future> openCollection( - Set names, - String? path, - bool crashRecovery, - HiveCipher? cipher, - String collection) async { - var db = - await indexedDB!.open(collection, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - for (var objectStoreName in names) { - if (!(db.objectStoreNames ?? []).contains(objectStoreName)) { - db.createObjectStore(objectStoreName); - } - } - }); - - // in case the objectStore is not contained, re-open the db and - // update version - if (!(names.every((objectStoreName) => - (db.objectStoreNames ?? []).contains(objectStoreName)))) { - db = await indexedDB!.open( - collection, - version: (db.version ?? 1) + 1, - onUpgradeNeeded: (e) { - var db = e.target.result as Database; - for (var objectStoreName in names) { - if (!(db.objectStoreNames ?? []).contains(objectStoreName)) { - db.createObjectStore(objectStoreName); - } - } - }, - ); - } - return Map.fromEntries( - names.map((e) => MapEntry(e, StorageBackendJs(db, cipher, e)))); - } - - @override - Future deleteBox(String name, String? path, String? collection) async { - // compatibility for old store format - final databaseName = collection ?? name; - final objectStoreName = collection == null ? 'box' : name; - - // directly deleting the entire DB if a non-collection Box - if (collection == null) { - await indexedDB!.deleteDatabase(databaseName); - } else { - final db = - await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if ((db.objectStoreNames ?? []).contains(objectStoreName)) { - db.deleteObjectStore(objectStoreName); - } - }); - if ((db.objectStoreNames ?? []).isEmpty) { - indexedDB!.deleteDatabase(databaseName); - } - } - } - - @override - Future boxExists(String name, String? path, String? collection) async { - // compatibility for old store format - final databaseName = collection ?? name; - final objectStoreName = collection == null ? 'box' : name; - // https://stackoverflow.com/a/17473952 - try { - var _exists = true; - if (collection == null) { - await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { - e.target.transaction!.abort(); - _exists = false; - }); - } else { - final db = - await indexedDB!.open(collection, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - _exists = (db.objectStoreNames ?? []).contains(objectStoreName); - }); - _exists = (db.objectStoreNames ?? []).contains(objectStoreName); - } - return _exists; - } catch (error) { - return false; - } - } -} diff --git a/hive/lib/src/backend/js/native/storage_backend_js.dart b/hive/lib/src/backend/js/native/storage_backend_js.dart deleted file mode 100644 index edf30b409..000000000 --- a/hive/lib/src/backend/js/native/storage_backend_js.dart +++ /dev/null @@ -1,227 +0,0 @@ -import 'dart:async'; -import 'dart:html'; -import 'dart:indexed_db'; -import 'dart:js' as js; -import 'dart:js_util'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:meta/meta.dart'; - -/// Handles all IndexedDB related tasks -class StorageBackendJs extends StorageBackend { - static const _bytePrefix = [0x90, 0xA9]; - final Database _db; - final HiveCipher? _cipher; - final String objectStoreName; - - TypeRegistry _registry; - - /// Not part of public API - StorageBackendJs(this._db, this._cipher, this.objectStoreName, - [this._registry = TypeRegistryImpl.nullImpl]); - - @override - String? get path => null; - - @override - bool supportsCompaction = false; - - bool _isEncoded(Uint8List bytes) { - return bytes.length >= _bytePrefix.length && - bytes[0] == _bytePrefix[0] && - bytes[1] == _bytePrefix[1]; - } - - /// Not part of public API - @visibleForTesting - Future encodeValue(Frame frame) async { - var value = frame.value; - if (_cipher == null) { - if (value == null) { - return value; - } else if (value is Uint8List) { - if (!_isEncoded(value)) { - return value.buffer; - } - } else if (value is num || - value is bool || - value is String || - value is List || - value is List || - value is List) { - return value; - } - } - - var frameWriter = BinaryWriterImpl(_registry); - frameWriter.writeByteList(_bytePrefix, writeLength: false); - - if (_cipher == null) { - frameWriter.write(value); - } else { - await frameWriter.writeEncrypted(value, _cipher!); - } - - var bytes = frameWriter.toBytes(); - var sublist = bytes.sublist(0, bytes.length); - return sublist.buffer; - } - - /// Not part of public API - @visibleForTesting - Future decodeValue(dynamic value) async { - if (value is ByteBuffer) { - var bytes = Uint8List.view(value); - if (_isEncoded(bytes)) { - var reader = BinaryReaderImpl(bytes, _registry); - reader.skip(2); - if (_cipher == null) { - return reader.read(); - } else { - return reader.readEncrypted(_cipher!); - } - } else { - return bytes; - } - } else { - return value; - } - } - - /// Not part of public API - @visibleForTesting - ObjectStore getStore(bool write) { - return _db - .transaction(objectStoreName, write ? 'readwrite' : 'readonly') - .objectStore(objectStoreName); - } - - /// Not part of public API - @visibleForTesting - Future> getKeys({bool cursor = false}) { - var store = getStore(false); - - if (hasProperty(store, 'getAllKeys') && !cursor) { - var completer = Completer>(); - var request = getStore(false).getAllKeys(null); - request.onSuccess.listen((_) { - completer.complete(request.result as List?); - }); - request.onError.listen((_) { - completer.completeError(request.error!); - }); - return completer.future; - } else { - return store.openCursor(autoAdvance: true).map((e) => e.key).toList(); - } - } - - /// Not part of public API - @visibleForTesting - Future> getValues({bool cursor = false}) { - var store = getStore(false); - - if (hasProperty(store, 'getAll') && !cursor) { - var completer = Completer>(); - var request = store.getAll(null); - request.onSuccess.listen((_) async { - var futures = (request.result as List).map(decodeValue); - completer.complete(await Future.wait(futures)); - }); - request.onError.listen((_) { - completer.completeError(request.error!); - }); - return completer.future; - } else { - return store.openCursor(autoAdvance: true).map((e) => e.value).toList(); - } - } - - @override - Future initialize( - TypeRegistry registry, Keystore keystore, bool lazy) async { - _registry = registry; - var keys = await getKeys(); - if (!lazy) { - var i = 0; - var values = await getValues(); - for (var value in values) { - var key = keys[i++]; - keystore.insert(Frame(key, value), notify: false); - } - } else { - for (var key in keys) { - keystore.insert(Frame.lazy(key), notify: false); - } - } - - return 0; - } - - @override - Future readValue(Frame frame) async { - var value = await getStore(false).getObject(frame.key); - return decodeValue(value); - } - - @override - Future writeFrames(List frames) async { - var store = getStore(true); - for (var frame in frames) { - if (frame.deleted) { - await store.delete(frame.key); - } else { - await store.put(await encodeValue(frame), frame.key); - } - } - } - - @override - Future> compact(Iterable frames) { - throw UnsupportedError('Not supported'); - } - - @override - Future clear() { - return getStore(true).clear(); - } - - @override - Future close() { - _db.close(); - return Future.value(); - } - - @override - Future deleteFromDisk() async { - final indexDB = js.context.hasProperty('window') - ? window.indexedDB - : WorkerGlobalScope.instance.indexedDB; - - // directly deleting the entire DB if a non-collection Box - if (_db.objectStoreNames?.length == 1) { - await indexDB!.deleteDatabase(_db.name!); - } else { - final db = - await indexDB!.open(_db.name!, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if ((db.objectStoreNames ?? []).contains(objectStoreName)) { - db.deleteObjectStore(objectStoreName); - } - }); - if ((db.objectStoreNames ?? []).isEmpty) { - await indexDB.deleteDatabase(_db.name!); - } - } - } - - @override - Future flush() => Future.value(); -} diff --git a/hive/lib/src/backend/js/web_worker/backend_manager.dart b/hive/lib/src/backend/js/web_worker/backend_manager.dart deleted file mode 100644 index ed59aae38..000000000 --- a/hive/lib/src/backend/js/web_worker/backend_manager.dart +++ /dev/null @@ -1,98 +0,0 @@ -import 'dart:async'; -import 'dart:html'; -import 'dart:indexed_db'; -import 'dart:js' as js; -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/js/web_worker/web_worker_interface.dart'; - -import '../../storage_backend.dart'; -import 'storage_backend_web_worker.dart'; - -/// Opens IndexedDB databases -class BackendManagerWebWorker implements BackendManagerInterface { - final HiveStorageBackendPreferenceWebWorker backendPreference; - - WebWorkerInterface? _interface; - - WebWorkerInterface get interface { - _interface ??= WebWorkerInterface( - backendPreference.path, backendPreference.onStackTrace); - return _interface!; - } - - BackendManagerWebWorker(this.backendPreference); - - IdbFactory? get indexedDB => js.context.hasProperty('window') - ? window.indexedDB - : WorkerGlobalScope.instance.indexedDB; - - @override - Future open(String name, String? path, - bool crashRecovery, HiveCipher? cipher, String? collection) async { - // compatibility for old store format - final databaseName = collection ?? name; - final objectStoreName = collection == null ? 'box' : name; - - await interface.query('open', databaseName, objectStoreName); - return StorageBackendWebWorker( - interface, databaseName, cipher, objectStoreName); - } - - @override - Future> openCollection( - Set names, - String? path, - bool crashRecovery, - HiveCipher? cipher, - String collection) async { - // for collections, create an additional interface - final interface = WebWorkerInterface( - backendPreference.path, backendPreference.onStackTrace); - await interface.query('open', collection, null, null, names); - return Map.fromEntries(names.map( - (e) => MapEntry( - e, - StorageBackendWebWorker(interface, collection, cipher, e), - ), - )); - } - - @override - Future deleteBox(String name, String? path, String? collection) async { - // compatibility for old store format - final databaseName = collection ?? name; - final objectStoreName = collection == null ? 'box' : name; - - // cipher doesn't matter on delete operation - await interface.query('delete', databaseName, objectStoreName); - } - - @override - Future boxExists(String name, String? path, String? collection) async { - // TODO(TheOneWithTheBraid): migrate into web worker - - // compatibility for old store format - final databaseName = collection ?? name; - final objectStoreName = collection == null ? 'box' : name; - // https://stackoverflow.com/a/17473952 - try { - var _exists = true; - if (collection == null) { - await indexedDB!.open(databaseName, version: 1, onUpgradeNeeded: (e) { - e.target.transaction!.abort(); - _exists = false; - }); - } else { - final db = - await indexedDB!.open(collection, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - _exists = (db.objectStoreNames ?? []).contains(objectStoreName); - }); - _exists = (db.objectStoreNames ?? []).contains(objectStoreName); - } - return _exists; - } catch (error) { - return false; - } - } -} diff --git a/hive/lib/src/backend/js/web_worker/storage_backend_web_worker.dart b/hive/lib/src/backend/js/web_worker/storage_backend_web_worker.dart deleted file mode 100644 index 116b3f64b..000000000 --- a/hive/lib/src/backend/js/web_worker/storage_backend_web_worker.dart +++ /dev/null @@ -1,180 +0,0 @@ -import 'dart:async'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/js/web_worker/web_worker_interface.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:meta/meta.dart'; - -/// Handles all IndexedDB related tasks -class StorageBackendWebWorker extends StorageBackend { - static const _bytePrefix = [0x90, 0xA9]; - final WebWorkerInterface _interface; - final String _db; - final HiveCipher? _cipher; - final String objectStoreName; - - TypeRegistry _registry; - - /// Not part of public API - StorageBackendWebWorker( - this._interface, this._db, this._cipher, this.objectStoreName, - [this._registry = TypeRegistryImpl.nullImpl]); - - @override - String? get path => null; - - @override - bool supportsCompaction = false; - - bool _isEncoded(Uint8List bytes) { - return bytes.length >= _bytePrefix.length && - bytes[0] == _bytePrefix[0] && - bytes[1] == _bytePrefix[1]; - } - - /// Not part of public API - @visibleForTesting - dynamic encodeValue(Frame frame) { - var value = frame.value; - if (_cipher == null) { - if (value == null) { - return value; - } else if (value is Uint8List) { - if (!_isEncoded(value)) { - return value.buffer; - } - } else if (value is num || - value is bool || - value is String || - value is List || - value is List || - value is List) { - return value; - } - } - - var frameWriter = BinaryWriterImpl(_registry); - frameWriter.writeByteList(_bytePrefix, writeLength: false); - - if (_cipher == null) { - frameWriter.write(value); - } else { - frameWriter.writeEncrypted(value, _cipher!); - } - - var bytes = frameWriter.toBytes(); - var sublist = bytes.sublist(0, bytes.length); - return sublist.buffer; - } - - /// Not part of public API - @visibleForTesting - dynamic decodeValue(dynamic value) { - if (value is ByteBuffer) { - var bytes = Uint8List.view(value); - if (_isEncoded(bytes)) { - var reader = BinaryReaderImpl(bytes, _registry); - reader.skip(2); - if (_cipher == null) { - return reader.read(); - } else { - return reader.readEncrypted(_cipher!); - } - } else { - return bytes; - } - } else { - return value; - } - } - - /// Not part of public API - @visibleForTesting - Future> getKeys({bool cursor = false}) { - return _interface.query('getAllKeys', _db, objectStoreName); - } - - /// Not part of public API - @visibleForTesting - Future> getValues({bool cursor = false}) async { - final values = await _interface.query>( - 'getAll', _db, objectStoreName); - return values.map(decodeValue); - } - - @override - Future initialize( - TypeRegistry registry, Keystore keystore, bool lazy) async { - _registry = registry; - var keys = await getKeys(); - if (!lazy) { - var i = 0; - var values = await getValues(); - for (var value in values) { - var key = keys[i++]; - keystore.insert(Frame(key, value), notify: false); - } - } else { - for (var key in keys) { - keystore.insert(Frame.lazy(key), notify: false); - } - } - - return 0; - } - - @override - Future readValue(Frame frame) async { - var value = await _interface.query('get', _db, objectStoreName, frame.key); - return decodeValue(value); - } - - @override - Future writeFrames(List frames) async { - // TODO(TheOneWithTheBraid): implement as transaction - final deleted = frames.where((element) => element.deleted).toList(); - final notDeleted = frames.where((element) => !element.deleted).toList(); - - if (deleted.isNotEmpty) { - await _interface.query('deleteKey', _db, objectStoreName, - deleted.map((e) => e.key).toList()); - } - if (notDeleted.isNotEmpty) { - await _interface.query( - 'put', - _db, - objectStoreName, - notDeleted.map((e) => e.key).toList(), - notDeleted.map(encodeValue).toList()); - } - } - - @override - Future> compact(Iterable frames) { - throw UnsupportedError('Not supported'); - } - - @override - Future clear() async { - await _interface.query('clear', _db, objectStoreName); - } - - @override - Future close() async { - await _interface.query('close', _db, objectStoreName); - } - - @override - Future deleteFromDisk() async { - await _interface.query('delete', _db, objectStoreName); - } - - @override - Future flush() => Future.value(); -} diff --git a/hive/lib/src/backend/js/web_worker/web_worker.dart b/hive/lib/src/backend/js/web_worker/web_worker.dart deleted file mode 100644 index 57b3110ef..000000000 --- a/hive/lib/src/backend/js/web_worker/web_worker.dart +++ /dev/null @@ -1,363 +0,0 @@ -import 'dart:async'; -import 'dart:collection'; -import 'dart:html'; -import 'dart:indexed_db'; - -import 'dart:js'; -import 'package:hive/src/backend/js/web_worker/web_worker_operation.dart'; -import 'package:js/js.dart'; -import 'package:js/js_util.dart'; - -/// -/// -/// CAUTION: THIS FILE NEEDS TO BE MANUALLY COMPILED -/// -/// 1. in your project, create a file `web/web_worker.dart` -/// 2. add the following contents: -/// ```dart -/// import 'package:hive/hive.dart'; -/// -/// Future main() => startWebWorker(); -/// ``` -/// 3. compile the file using: -/// ```shell -/// dart compile js -o web/web_worker.dart.js -m web/web_worker.dart -/// ``` -/// -/// You should not check in that file into your VCS. Instead, you should compile -/// the web worker in your CI pipeline. -/// - -@pragma('dart2js:tryInline') -Future startWebWorker() async { - print('[web worker]: Starting...'); - setProperty( - context['self'] as Object, - 'onmessage', - allowInterop( - (MessageEvent event) async { - final data = event.data; - try { - final operation = WebWorkerOperation.fromJson(Map.from(data as Map)); - - void respond([Object? response]) { - allowInterop(sendResponse).call(operation.label, response); - } - - switch (operation.command) { - case 'open': - try { - Set storeNames = {}; - - if (operation.objectStore != null) { - storeNames.add(operation.objectStore!); - } else if (operation.value is Iterable) { - storeNames.addAll( - (operation.value as Iterable).whereType()); - } else { - storeNames.add('box'); - } - - var version = await getDatabaseVersion(operation.database); - - var db = await indexedDB!.open(operation.database, - version: version, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - for (var objectStoreName in storeNames) { - if (!(db.objectStoreNames ?? []) - .contains(objectStoreName)) { - db.createObjectStore(objectStoreName); - } - } - }, onBlocked: (e) { - print('[web worker] Error opening indexed DB: ${event.data}'); - }); - - // in case the objectStore is not contained, re-open the db and - // update version - if (!(storeNames.every((objectStoreName) => - (db.objectStoreNames ?? []).contains(objectStoreName)))) { - db.close(); - version++; - setDatabaseVersion(operation.database, version); - db = await indexedDB!.open(operation.database, - version: version, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - for (var objectStoreName in storeNames) { - if (!(db.objectStoreNames ?? []) - .contains(objectStoreName)) { - db.createObjectStore(objectStoreName); - } - } - }, onBlocked: (e) { - print( - '[web worker] Error opening indexed DB: ${event.data}'); - }); - } - _databases[operation.database] = db; - respond(db.name); - } on Event catch (e, s) { - print('[web worker]: Runtime error:' - ' ${(e.target as Request).error}, $s'); - } - break; - - case 'close': - _databases[operation.database]!.close(); - respond(); - break; - - case 'createObjectStore': - final result = _databases[operation.database]! - .createObjectStore(operation.objectStore!); - respond(result); - break; - - case 'add': - await getObjectStore( - operation.database, operation.objectStore!, true) - .add(operation.value, operation.key); - respond(); - break; - - case 'clear': - await getObjectStore( - operation.database, operation.objectStore!, true) - .clear(); - respond(); - break; - - case 'delete': - try { - final _db = _databases[operation.database] ?? - await indexedDB!.open(operation.database); - - // directly deleting the entire DB if a non-collection Box - if (_db.objectStoreNames?.length == 1) { - await indexedDB!.deleteDatabase(_db.name!); - } else { - final db = await indexedDB!.open(_db.name!, version: 1, - onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if ((db.objectStoreNames ?? []) - .contains(operation.objectStore)) { - db.deleteObjectStore(operation.objectStore!); - } - }); - if ((db.objectStoreNames ?? []).isEmpty) { - await indexedDB!.deleteDatabase(_db.name!); - } - } - } finally {} - respond(); - break; - - case 'getAll': - var completer = Completer>(); - var request = getObjectStore( - operation.database, operation.objectStore!, false) - .getAll(null); - request.onSuccess.listen((_) { - completer.complete(request.result as List?); - }); - request.onError.listen((_) { - completer.completeError(request.error!); - }); - final result = await completer.future; - respond(result); - break; - - case 'getAllKeys': - var completer = Completer>(); - try { - var request = getObjectStore( - operation.database, operation.objectStore!, false) - .getAllKeys(null); - request.onSuccess.listen((_) { - completer.complete(request.result as List?); - }); - request.onError.listen((_) { - completer.completeError(request.error!); - }); - } catch (e) { - print('[web worker] $e'); - } - final result = await completer.future; - respond(result); - break; - - case 'put': - final objectStore = getObjectStore( - operation.database, operation.objectStore!, true); - final keys = List.from(operation.key as Iterable); - final values = List.from(operation.value as Iterable); - final futures = []; - for (var i = 0; i < keys.length; i++) { - futures.add(objectStore.put(values[i], keys[i])); - } - await Future.wait(futures); - - respond(); - break; - - case 'deleteKey': - final objectStore = getObjectStore( - operation.database, operation.objectStore!, true); - final keys = List.from(operation.key as Iterable); - final futures = []; - for (var i = 0; i < keys.length; i++) { - futures.add(objectStore.delete(keys[i])); - } - await Future.wait(futures); - - respond(); - break; - - case 'get': - final store = getObjectStore( - operation.database, operation.objectStore!, false); - final response = await store.getObject(operation.key); - respond(response); - - break; - - case 'startTransaction': - final db = _databases[operation.database]!; - // value represents all affected objectStoreNames here, as - // [operation.objectStore] is not a `List` - final txn = db.transaction(operation.value, 'read-write'); - _transactions[operation.transaction!] = txn; - respond(); - break; - - case 'stopTransaction': - _transactions.remove(operation.transaction!); - respond(); - break; - - default: - print('[web worker] Unknown command ${operation.command}'); - respond(); - break; - } - } on Event catch (e, s) { - allowInterop(_replyError) - .call((e.target as Request).error, s, data['label'] as double); - } catch (e, s) { - allowInterop(_replyError).call(e, s, data['label'] as double); - } - }, - ), - ); -} - -final Map _versionCache = {}; - -Future getDatabaseVersion(String database) async { - if (_versionCache.isEmpty) { - final db = await indexedDB!.open('hive_web_worker_database_versions', - version: 1, onUpgradeNeeded: (e) { - final db = (e.target.result as Database); - db.createObjectStore('versions'); - }); - _databases['hive_web_worker_database_versions'] = db; - final txn = db.transaction('versions', 'readonly'); - - final versions = await txn.objectStore('versions').getObject('versions'); - - if (versions is LinkedHashMap) { - _versionCache.addAll(versions.cast()); - } - } - if (!_versionCache.containsKey(database)) { - setDatabaseVersion(database, 1); - } - return _versionCache[database]!; -} - -// caches background put operation of database versions -Future? _putVersionsFuture; - -void setDatabaseVersion(String database, int version) { - _versionCache[database] = version; - (_putVersionsFuture ?? Future.value(null)).then((value) { - final db = _databases['hive_web_worker_database_versions']!; - final txn = db.transaction('versions', 'readwrite'); - _putVersionsFuture = - txn.objectStore('versions').put(jsify(_versionCache), 'versions'); - _putVersionsFuture?.then((value) => _putVersionsFuture = null); - }); -} - -void sendResponse(double label, dynamic response) { - try { - self.postMessage({ - 'label': label, - 'response': response, - }); - } catch (e, s) { - print('[web worker] Error responding: $e, $s'); - } -} - -void _replyError(Object? error, StackTrace stackTrace, double origin) { - if (error != null) { - try { - final jsError = jsify(error); - if (jsError != null) { - error = jsError; - } - } catch (e) { - error = error.toString(); - } - } - try { - self.postMessage({ - 'label': 'stacktrace', - 'origin': origin, - 'error': error, - 'stacktrace': stackTrace.toString(), - }); - } catch (e, s) { - print('[web worker] Error responding: $e, $s'); - } -} - -/// represents the [WorkerGlobalScope] the worker currently runs in. -@JS('self') -external WorkerGlobalScope get self; - -/// adding all missing WebWorker-only properties to the [WorkerGlobalScope] -extension on WorkerGlobalScope { - void postMessage(Object data) { - callMethod(self, 'postMessage', [jsify(data)]); - } -} - -IdbFactory? get indexedDB => self.indexedDB; - -Map _databases = {}; - -Map _transactions = {}; - -ObjectStore getObjectStore( - String database, - String box, - bool write, [ - String? transaction, -]) { - if (transaction != null) { - if (_transactions.containsKey(transaction)) { - return _transactions[transaction]!.objectStore(box); - } else { - final txn = _databases[database]! - .transaction(box, write ? 'readwrite' : 'readonly'); - _transactions[transaction] = txn; - return txn.objectStore(box); - } - } else { - return _databases[database]! - .transaction(box, write ? 'readwrite' : 'readonly') - .objectStore(box); - } -} diff --git a/hive/lib/src/backend/js/web_worker/web_worker_interface.dart b/hive/lib/src/backend/js/web_worker/web_worker_interface.dart deleted file mode 100644 index 0988214ed..000000000 --- a/hive/lib/src/backend/js/web_worker/web_worker_interface.dart +++ /dev/null @@ -1,80 +0,0 @@ -import 'dart:async'; -import 'dart:html'; -import 'dart:js_util'; - -import 'dart:math'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/js/web_worker/web_worker_operation.dart'; - -class WebWorkerInterface { - final WebWorkerStackTraceCallback onStackTrace; - final Worker _worker; - final Random _random = Random(); - - final Map _queries = {}; - - WebWorkerInterface(String href, this.onStackTrace) : _worker = Worker(href) { - print('[hive] Created Worker($href)'); - _worker.onMessage.listen(_handleMessage); - } - - Future query(String command, String database, - [String? objectStore, Object? key, Object? value, String? transaction]) { - final label = _random.nextDouble(); - final completer = Completer(); - _queries[label] = completer; - - final operation = WebWorkerOperation( - command: command, - label: label, - database: database, - objectStore: objectStore, - key: key, - value: value, - transaction: transaction, - ); - - _worker.postMessage(jsify(operation.toJson())); - return completer.future.timeout(Duration(seconds: 45)); - } - - void _handleMessage(MessageEvent event) { - final label = event.data['label']; - // don't forget handling errors of our second thread... - if (label == 'stacktrace') { - final origin = event.data['origin']; - final completer = _queries[origin]; - - final error = event.data['error']!; - - Future.value( - onStackTrace.call(event.data['stacktrace'] as String), - ).then( - (stackTrace) => completer?.completeError( - WebWorkerError(error: error, stackTrace: stackTrace), - ), - ); - } - final completer = _queries[label]; - var response = event.data['response']; - completer?.complete(response); - _queries.remove(label); - } -} - -class WebWorkerError extends Error { - /// the error thrown in the web worker. Usually a [String] - final Object? error; - - /// de-serialized [StackTrace] - @override - final StackTrace stackTrace; - - WebWorkerError({required this.error, required this.stackTrace}); - - @override - String toString() { - return '$error, $stackTrace'; - } -} diff --git a/hive/lib/src/backend/js/web_worker/web_worker_operation.dart b/hive/lib/src/backend/js/web_worker/web_worker_operation.dart deleted file mode 100644 index 89f294dae..000000000 --- a/hive/lib/src/backend/js/web_worker/web_worker_operation.dart +++ /dev/null @@ -1,40 +0,0 @@ -class WebWorkerOperation { - final String command; - final double label; - final String database; - final String? objectStore; - final Object? key; - final Object? value; - final String? transaction; - - const WebWorkerOperation({ - required this.command, - required this.label, - required this.database, - this.objectStore, - this.key, - this.value, - this.transaction, - }); - - factory WebWorkerOperation.fromJson(Map json) => - WebWorkerOperation( - command: json['command'] as String, - label: json['label'] as double, - database: json['database'] as String, - objectStore: json['objectStore'] as String?, - key: json['key'], - value: json['value'], - transaction: json['transaction'] as String?, - ); - - Map toJson() => { - "command": command, - "label": label, - 'database': database, - 'objectStore': objectStore, - 'key': key, - 'value': value, - 'transaction': transaction, - }; -} diff --git a/hive/lib/src/backend/js/web_worker/web_worker_stub.dart b/hive/lib/src/backend/js/web_worker/web_worker_stub.dart deleted file mode 100644 index ad7c3af18..000000000 --- a/hive/lib/src/backend/js/web_worker/web_worker_stub.dart +++ /dev/null @@ -1,23 +0,0 @@ -import 'dart:async'; - -/// -/// -/// CAUTION: THIS FILE NEEDS TO BE MANUALLY COMPILED -/// -/// 1. in your project, create a file `web/web_worker.dart` -/// 2. add the following contents: -/// ```dart -/// import 'package:hive/hive.dart'; -/// -/// Future main() => startWebWorker(); -/// ``` -/// 3. compile the file using: -/// ```shell -/// dart compile js -o web/web_worker.dart.js -m web/web_worker.dart -/// ``` -/// -/// You should not check in that file into your VCS. Instead, you should compile -/// the web worker in your CI pipeline. -/// - -Future startWebWorker() => Future.value(); diff --git a/hive/lib/src/backend/storage_backend.dart b/hive/lib/src/backend/storage_backend.dart deleted file mode 100644 index fbe92b2b4..000000000 --- a/hive/lib/src/backend/storage_backend.dart +++ /dev/null @@ -1,59 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/keystore.dart'; - -export 'package:hive/src/backend/stub/backend_manager.dart' - if (dart.library.io) 'package:hive/src/backend/vm/backend_manager.dart' - if (dart.library.html) 'package:hive/src/backend/js/backend_manager.dart'; - -/// Abstract storage backend -abstract class StorageBackend { - /// The path where the database is stored - String? get path; - - /// Whether the database can be compacted - bool get supportsCompaction; - - /// Prepare backend - Future initialize(TypeRegistry registry, Keystore keystore, bool lazy); - - /// Read value from backend - Future readValue(Frame frame); - - /// Write a list of frames to the backend - Future writeFrames(List frames); - - /// Compact database - Future compact(Iterable frames); - - /// Clear database - Future clear(); - - /// Close database - Future close(); - - /// Clear database and delete from disk - Future deleteFromDisk(); - - /// Flush all changes to disk - Future flush(); -} - -/// Abstract database manager -abstract class BackendManagerInterface { - /// Opens database connection and creates StorageBackend - Future open(String name, String? path, bool crashRecovery, - HiveCipher? cipher, String? collection); - - /// Opens database collection connection and creates StorageBackends for each - Future> openCollection(Set names, - String? path, bool crashRecovery, HiveCipher? cipher, String collection); - - /// Deletes database - Future deleteBox(String name, String? path, String? collection); - - /// Checks if box exists - Future boxExists(String name, String? path, String? collection); -} diff --git a/hive/lib/src/backend/stub/backend_manager.dart b/hive/lib/src/backend/stub/backend_manager.dart deleted file mode 100644 index d646e5d7b..000000000 --- a/hive/lib/src/backend/stub/backend_manager.dart +++ /dev/null @@ -1,37 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; - -/// Not part of public API -class BackendManager implements BackendManagerInterface { - static BackendManager select( - [HiveStorageBackendPreference? backendPreference]) => - BackendManager(); - - @override - Future open(String name, String? path, bool crashRecovery, - HiveCipher? cipher, String? collection) { - throw UnimplementedError(); - } - - @override - Future> openCollection( - Set names, - String? path, - bool crashRecovery, - HiveCipher? cipher, - String? collection) { - throw UnimplementedError(); - } - - @override - Future deleteBox(String name, String? path, String? collection) { - throw UnimplementedError(); - } - - @override - Future boxExists(String name, String? path, String? collection) { - throw UnimplementedError(); - } -} diff --git a/hive/lib/src/backend/stub/backend_manager_memory.dart b/hive/lib/src/backend/stub/backend_manager_memory.dart deleted file mode 100644 index 58acfcf2c..000000000 --- a/hive/lib/src/backend/stub/backend_manager_memory.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; - -/// Not part of public API -class BackendManagerMemory implements BackendManagerInterface { - static final Map> _backends = {}; - - @override - Future open(String name, String? path, bool crashRecovery, - HiveCipher? cipher, String? collection) async { - final backend = StorageBackendMemory(null, cipher); - _backends[collection] ??= {}; - _backends[collection]![name] = backend; - return backend; - } - - @override - Future> openCollection( - Set names, - String? path, - bool crashRecovery, - HiveCipher? cipher, - String? collection) async { - return Map.fromEntries(await Future.wait(names.map((e) => - open(e, path, crashRecovery, cipher, collection) - .then((value) => MapEntry(e, value))))); - } - - @override - Future deleteBox(String name, String? path, String? collection) async { - _backends[collection]?.remove(name); - } - - @override - Future boxExists(String name, String? path, String? collection) async { - return _backends[collection]?.containsKey(name) ?? false; - } -} diff --git a/hive/lib/src/backend/stub/random_access_buffer.dart b/hive/lib/src/backend/stub/random_access_buffer.dart deleted file mode 100644 index d4dd25f40..000000000 --- a/hive/lib/src/backend/stub/random_access_buffer.dart +++ /dev/null @@ -1,100 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/vm/read_write_sync.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/binary/frame_helper.dart'; -import 'package:hive/src/box/keystore.dart'; - -/// Implementation of [RandomAccessBuffer] -/// -/// Provides simplified access to raw [Uint8List] for storing -/// [Frame] data. For main usage in [StorageBackendMemory]. -class RandomAccessBuffer { - final ReadWriteSync _sync; - - final FrameHelper _frameHelper; - - Uint8List _bytes; - - int _writeOffset; - - RandomAccessBuffer(Uint8List? bytes, {int initialOffset = 0}) - : _bytes = bytes ?? Uint8List(0), - _sync = ReadWriteSync(), - _writeOffset = initialOffset, - _frameHelper = FrameHelper(); - - /// Get the current length in bytes - int get length => _bytes.lengthInBytes; - - /// Get the current write offset - int get writeOffset => _writeOffset; - - /// Read a [Frame] from the [RandomAccessBuffer] - Future read( - Frame frame, TypeRegistry typeRegistry, HiveCipher? cipher) { - return _sync.syncRead(() async { - var bytes = _bytes.sublist(frame.offset, frame.offset + frame.length!); - var reader = BinaryReaderImpl(bytes, typeRegistry); - var readFrame = await reader.readFrame(cipher: cipher, lazy: false); - - if (readFrame == null) { - throw HiveError('Could not read value from box. ' - 'Maybe your box is corrupted.'); - } - - return readFrame.value; - }); - } - - /// Write a list of [Frame] to the [RandomAccessBuffer] - Future write( - List frames, TypeRegistry typeRegistry, HiveCipher? cipher) { - return _sync.syncWrite(() async { - var writer = BinaryWriterImpl(typeRegistry); - - for (var frame in frames) { - frame.length = await writer.writeFrame(frame, cipher: cipher); - } - - var b = BytesBuilder(); - b.add(_bytes); - b.add(writer.toBytes()); - _bytes = b.toBytes(); - - for (var frame in frames) { - frame.offset = _writeOffset; - _writeOffset += frame.length!; - } - }); - } - - /// Perform the recovery check - /// Throws an [HiveError] on box corruption - Future recoveryCheck( - TypeRegistry typeRegistry, Keystore? keystore, HiveCipher? cipher) async { - var recoveryOffset = await _frameHelper.framesFromBytes( - _bytes, - keystore, - typeRegistry, - cipher, - ); - - if (recoveryOffset != -1) { - throw HiveError('Wrong checksum in bytes. Box may be corrupted.'); - } - - _writeOffset = _bytes.offsetInBytes; - } - - /// Clear the buffer and reset the write offset - Future clear() async { - return _sync.syncReadWrite(() async { - _bytes = Uint8List(0); - _writeOffset = 0; - }); - } -} diff --git a/hive/lib/src/backend/stub/storage_backend_memory.dart b/hive/lib/src/backend/stub/storage_backend_memory.dart deleted file mode 100644 index 7ea77f3d4..000000000 --- a/hive/lib/src/backend/stub/storage_backend_memory.dart +++ /dev/null @@ -1,79 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/backend/stub/random_access_buffer.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/keystore.dart'; - -/// In-memory storage backend -/// -/// This storage memory uses the [RandomAccessBuffer] which is an abstraction -/// for managing data in [Uint8List] as a mean of storage. Data will not be -/// persisted and is lost on close. Data is not stored, is highly volatile -/// and does not survive a page refresh. -class StorageBackendMemory extends StorageBackend { - late final RandomAccessBuffer _randomAccessBuffer; - - final HiveCipher? _cipher; - - TypeRegistry? _typeRegistry; - - /// Default constructor for the [StorageBackendMemory] - /// - /// If needed, provide a [Uint8List] with already existing bytes and the - /// corresponding [initialOffset]. For encryption, provide a [_cipher]. - StorageBackendMemory(Uint8List? bytes, this._cipher, - [int initialOffset = 0]) { - _randomAccessBuffer = - RandomAccessBuffer(bytes, initialOffset: initialOffset); - } - - /// An in-memory storage has no path - @override - final path = null; - - @override - final bool supportsCompaction = false; - - @override - Future initialize( - TypeRegistry registry, Keystore keystore, bool lazy) async { - _typeRegistry = registry; - await _randomAccessBuffer.recoveryCheck(registry, keystore, _cipher); - } - - @override - Future readValue(Frame frame) async { - return _randomAccessBuffer.read(frame, _typeRegistry!, _cipher); - } - - @override - Future writeFrames(List frames) { - return _randomAccessBuffer.write(frames, _typeRegistry!, _cipher); - } - - @override - Future compact(Iterable frames) { - throw UnsupportedError('This operation is unsupported for memory boxes.'); - } - - @override - Future clear() { - return _randomAccessBuffer.clear(); - } - - @override - Future close() { - return _randomAccessBuffer.clear(); - } - - @override - Future deleteFromDisk() { - return _randomAccessBuffer.clear(); - } - - /// Nothing to flush to disk as we act directly in-memory - @override - Future flush() => Future.value(); -} diff --git a/hive/lib/src/backend/vm/backend_manager.dart b/hive/lib/src/backend/vm/backend_manager.dart deleted file mode 100644 index 490ef78f2..000000000 --- a/hive/lib/src/backend/vm/backend_manager.dart +++ /dev/null @@ -1,144 +0,0 @@ -import 'dart:async'; -import 'dart:io'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/backend/vm/storage_backend_vm.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -class BackendManager implements BackendManagerInterface { - final _delimiter = Platform.isWindows ? '\\' : '/'; - - // caching the backend manager as it makes no sense to have different backends - // within the same application - static HiveStorageBackendPreference? _backend; - - BackendManager(); - - static BackendManager select( - [HiveStorageBackendPreference? backendPreference]) { - _backend ??= backendPreference; - return BackendManager(); - } - - @override - Future open(String name, String? path, bool crashRecovery, - HiveCipher? cipher, String? collection) async { - late StorageBackend backend; - if (_backend == HiveStorageBackendPreference.native || - _backend is HiveStorageBackendPreferenceWebWorker || - _backend == null) { - if (path == null) { - throw HiveError('You need to initialize Hive or ' - 'provide a path to store the box.'); - } - - if (path.endsWith(_delimiter)) path = path.substring(0, path.length - 1); - - if (collection != null) { - path = '$path/$collection'; - } - - var dir = Directory(path); - - if (!await dir.exists()) { - await dir.create(recursive: true); - } - - var file = await findHiveFileAndCleanUp(name, path); - var lockFile = File('$path$_delimiter$name.lock'); - - final backendVm = StorageBackendVm(file, lockFile, crashRecovery, cipher); - await backendVm.open(); - backend = backendVm; - } else if (_backend == HiveStorageBackendPreference.memory) { - backend = StorageBackendMemory(null, cipher); - } else { - throw UnimplementedError( - '$_backend is not a known HiveStorageBackendPreference'); - } - return backend; - } - - @override - Future> openCollection( - Set names, - String? path, - bool crashRecovery, - HiveCipher? cipher, - String collection) async { - return Map.fromEntries( - await Future.wait( - names.map( - (e) => open( - e, - path ?? (Hive as HiveImpl).homePath, - crashRecovery, - cipher, - collection, - ).then( - (value) => MapEntry(e, value), - ), - ), - ), - ); - } - - /// Not part of public API - @visibleForTesting - Future findHiveFileAndCleanUp(String name, String path) async { - var hiveFile = File('$path$_delimiter$name.hive'); - var compactedFile = File('$path$_delimiter$name.hivec'); - - if (await hiveFile.exists()) { - if (await compactedFile.exists()) { - await compactedFile.delete(); - } - return hiveFile; - } else if (await compactedFile.exists()) { - print('Restoring compacted file.'); - return await compactedFile.rename(hiveFile.path); - } else { - await hiveFile.create(); - return hiveFile; - } - } - - @override - Future deleteBox(String name, String? path, String? collection) async { - ArgumentError.checkNotNull(path, 'path'); - - if (path!.endsWith(_delimiter)) path = path.substring(0, path.length - 1); - - if (collection != null) { - path = path + collection; - } - - await _deleteFileIfExists(File('$path$_delimiter$name.hive')); - await _deleteFileIfExists(File('$path$_delimiter$name.hivec')); - await _deleteFileIfExists(File('$path$_delimiter$name.lock')); - } - - Future _deleteFileIfExists(File file) async { - if (await file.exists()) { - await file.delete(); - } - } - - @override - Future boxExists(String name, String? path, String? collection) async { - ArgumentError.checkNotNull(path, 'path'); - - if (path!.endsWith(_delimiter)) path = path.substring(0, path.length - 1); - - if (collection != null) { - path = path + collection; - } - - return await File('$path$_delimiter$name.hive').exists() || - await File('$path$_delimiter$name.hivec').exists() || - await File('$path$_delimiter$name.lock').exists(); - } -} diff --git a/hive/lib/src/backend/vm/read_write_sync.dart b/hive/lib/src/backend/vm/read_write_sync.dart deleted file mode 100644 index 7c4a4d8b4..000000000 --- a/hive/lib/src/backend/vm/read_write_sync.dart +++ /dev/null @@ -1,43 +0,0 @@ -import 'dart:async'; - -/// Lock mechanism to ensure correct order of execution -class ReadWriteSync { - Future _readTask = Future.value(); - - Future _writeTask = Future.value(); - - /// Run operation with read lock - Future syncRead(Future Function() task) { - var previousTask = _readTask; - - var completer = Completer(); - _readTask = completer.future; - - return previousTask.then((_) => task()).whenComplete(completer.complete); - } - - /// Run operation with write lock - Future syncWrite(Future Function() task) { - var previousTask = _writeTask; - - var completer = Completer(); - _writeTask = completer.future; - - return previousTask.then((_) => task()).whenComplete(completer.complete); - } - - /// Run operation with read and write lock - Future syncReadWrite(FutureOr Function() task) { - var previousReadTask = _readTask; - var previousWriteTask = _writeTask; - - var completer = Completer(); - var future = completer.future; - _readTask = future; - _writeTask = future; - - return previousReadTask.then((_) { - return previousWriteTask.then((_) => task()); - }).whenComplete(completer.complete); - } -} diff --git a/hive/lib/src/backend/vm/storage_backend_vm.dart b/hive/lib/src/backend/vm/storage_backend_vm.dart deleted file mode 100644 index db09c5270..000000000 --- a/hive/lib/src/backend/vm/storage_backend_vm.dart +++ /dev/null @@ -1,243 +0,0 @@ -import 'dart:async'; -import 'dart:io'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/backend/vm/read_write_sync.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:hive/src/io/buffered_file_reader.dart'; -import 'package:hive/src/io/buffered_file_writer.dart'; -import 'package:hive/src/io/frame_io_helper.dart'; -import 'package:meta/meta.dart'; - -/// Storage backend for the Dart VM -class StorageBackendVm extends StorageBackend { - final File _file; - final File _lockFile; - final bool _crashRecovery; - final HiveCipher? _cipher; - final FrameIoHelper _frameHelper; - - final ReadWriteSync _sync; - - /// Not part of public API - /// - /// Not `late final` for testing - @visibleForTesting - late RandomAccessFile readRaf; - - /// Not part of public API - /// - /// Not `late final` for testing - @visibleForTesting - late RandomAccessFile writeRaf; - - /// Not part of public API - @visibleForTesting - late RandomAccessFile lockRaf; - - /// Not part of public API - @visibleForTesting - int writeOffset = 0; - - /// Not part of public API - @visibleForTesting - late TypeRegistry registry; - - bool _compactionScheduled = false; - - /// Not part of public API - StorageBackendVm( - this._file, this._lockFile, this._crashRecovery, this._cipher) - : _frameHelper = FrameIoHelper(), - _sync = ReadWriteSync(); - - /// Not part of public API - StorageBackendVm.debug(this._file, this._lockFile, this._crashRecovery, - this._cipher, this._frameHelper, this._sync); - - @override - String get path => _file.path; - - @override - bool supportsCompaction = true; - - /// Not part of public API - Future open() async { - readRaf = await _file.open(); - writeRaf = await _file.open(mode: FileMode.writeOnlyAppend); - writeOffset = await writeRaf.length(); - } - - @override - Future initialize( - TypeRegistry registry, Keystore keystore, bool lazy) async { - this.registry = registry; - - lockRaf = await _lockFile.open(mode: FileMode.write); - if ((Hive as HiveImpl).useLocks) { - await lockRaf.lock(); - } - - int recoveryOffset; - if (!lazy) { - recoveryOffset = - await _frameHelper.framesFromFile(path, keystore, registry, _cipher); - } else { - recoveryOffset = await _frameHelper.keysFromFile(path, keystore, _cipher); - } - - if (recoveryOffset != -1) { - if (_crashRecovery) { - print('Recovering corrupted box.'); - await writeRaf.truncate(recoveryOffset); - await writeRaf.setPosition(recoveryOffset); - writeOffset = recoveryOffset; - } else { - throw HiveError('Wrong checksum in hive file. Box may be corrupted.'); - } - } - } - - @override - Future readValue(Frame frame) async { - return await _sync.syncRead(() async { - await readRaf.setPosition(frame.offset); - - var bytes = await readRaf.read(frame.length!); - - var reader = BinaryReaderImpl(bytes, registry); - var readFrame = await reader.readFrame(cipher: _cipher, lazy: false); - - if (readFrame == null) { - throw HiveError( - 'Could not read value from box. Maybe your box is corrupted.'); - } - - return readFrame.value; - }); - } - - @override - Future writeFrames(List frames) async { - var writer = BinaryWriterImpl(registry); - - for (var frame in frames) { - frame.length = await writer.writeFrame(frame, cipher: _cipher); - } - await _sync.syncWrite(() async { - final bytes = writer.toBytes(); - - final cachedOffset = writeOffset; - try { - /// TODO(TheOneWithTheBraid): implement real transactions with cache - await writeRaf.writeFrom(bytes); - } catch (e) { - await writeRaf.setPosition(cachedOffset); - rethrow; - } - }); - - for (var frame in frames) { - frame.offset = writeOffset; - writeOffset += frame.length!; - } - } - - @override - Future compact(Iterable frames) async { - if (_compactionScheduled) return Future.value(); - _compactionScheduled = true; - - await _sync.syncReadWrite(() async { - await readRaf.setPosition(0); - var reader = BufferedFileReader(readRaf); - - var fileDirectory = path.substring(0, path.length - 5); - var compactFile = File('$fileDirectory.hivec'); - var compactRaf = await compactFile.open(mode: FileMode.write); - var writer = BufferedFileWriter(compactRaf); - - var sortedFrames = frames.toList(); - sortedFrames.sort((a, b) => a.offset.compareTo(b.offset)); - try { - for (var frame in sortedFrames) { - if (frame.offset == -1) continue; // Frame has not been written yet - if (frame.offset != reader.offset) { - var skip = frame.offset - reader.offset; - if (reader.remainingInBuffer < skip) { - if (await reader.loadBytes(skip) < skip) { - throw HiveError('Could not compact box: Unexpected EOF.'); - } - } - reader.skip(skip); - } - - if (reader.remainingInBuffer < frame.length!) { - if (await reader.loadBytes(frame.length!) < frame.length!) { - throw HiveError('Could not compact box: Unexpected EOF.'); - } - } - await writer.write(reader.viewBytes(frame.length!)); - } - await writer.flush(); - } finally { - await compactRaf.close(); - } - - await readRaf.close(); - await writeRaf.close(); - await compactFile.rename(path); - await open(); - - var offset = 0; - for (var frame in sortedFrames) { - if (frame.offset == -1) continue; - frame.offset = offset; - offset += frame.length!; - } - _compactionScheduled = false; - }); - } - - @override - Future clear() { - return _sync.syncReadWrite(() async { - await writeRaf.truncate(0); - await writeRaf.setPosition(0); - writeOffset = 0; - }); - } - - Future _closeInternal() async { - await readRaf.close(); - await writeRaf.close(); - - await lockRaf.close(); - await _lockFile.delete(); - } - - @override - Future close() async { - return _sync.syncReadWrite(_closeInternal); - } - - @override - Future deleteFromDisk() { - return _sync.syncReadWrite(() async { - await _closeInternal(); - await _file.delete(); - }); - } - - @override - Future flush() async { - await _sync.syncWrite(() async { - await writeRaf.flush(); - }); - } -} diff --git a/hive/lib/src/binary/binary_reader.dart b/hive/lib/src/binary/binary_reader.dart deleted file mode 100644 index 486792b0e..000000000 --- a/hive/lib/src/binary/binary_reader.dart +++ /dev/null @@ -1,101 +0,0 @@ -part of hive; - -/// The [BinaryReader] is used to bring data back from the binary format on the -/// disk. -abstract class BinaryReader { - /// The UTF-8 decoder is used to decode Strings. - static const utf8Decoder = Utf8Decoder(); - - /// The number of bytes left in this entry. - int get availableBytes; - - /// The number of read bytes. - int get usedBytes; - - /// Skip n bytes. - void skip(int bytes); - - /// Read a single byte. - int readByte(); - - /// Get a [Uint8List] view which contains the next [bytes] bytes. - Uint8List viewBytes(int bytes); - - /// Get a [Uint8List] view which contains the next [bytes] bytes. This does - /// not advance the internal read position. - Uint8List peekBytes(int bytes); - - /// Read two bytes as 16-bit unsigned integer. - int readWord(); - - /// Read four bytes as 32-bit signed integer. - int readInt32(); - - /// Read four bytes as 32-bit unsigned integer. - int readUint32(); - - /// Read eight bytes as 64-bit signed integer. - int readInt(); - - /// Read eight bytes as 64-bit double. - double readDouble(); - - /// Read a boolean. - bool readBool(); - - /// Read [byteCount] bytes and decode an UTF-8 String. - /// - /// If [byteCount] is not provided, it is read first. - String readString([ - int? byteCount, - Converter, String> decoder = utf8Decoder, - ]); - - /// Read a list of bytes with [length]. - /// - /// If [length] is not provided, it is read first. - Uint8List readByteList([int? length]); - - /// Read a list of integers with [length]. - /// - /// If [length] is not provided, it is read first. - List readIntList([int? length]); - - /// Read a list of doubles with [length]. - /// - /// If [length] is not provided, it is read first. - List readDoubleList([int? length]); - - /// Read a list of booleans with [length]. - /// - /// If [length] is not provided, it is read first. - List readBoolList([int? length]); - - /// Read a list of Strings with [length]. - /// - /// If [length] is not provided, it is read first. - List readStringList([ - int? length, - Converter, String> decoder = utf8Decoder, - ]); - - /// Read a list with [length]. - /// - /// If [length] is not provided, it is read first. - List readList([int? length]); - - /// Read a map with [length] entries. - /// - /// If [length] is not provided, it is read first. - Map readMap([int? length]); - - /// Read and decode any value. - /// - /// If [typeId] is not provided, it is read first. - dynamic read([int? typeId]); - - /// Read a [HiveList] with [length]. - /// - /// If [length] is not provided, it is read first. - HiveList readHiveList([int? length]); -} diff --git a/hive/lib/src/binary/binary_reader_impl.dart b/hive/lib/src/binary/binary_reader_impl.dart deleted file mode 100644 index 9268e6a75..000000000 --- a/hive/lib/src/binary/binary_reader_impl.dart +++ /dev/null @@ -1,344 +0,0 @@ -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/crypto/crc32.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:hive/src/util/extensions.dart'; - -/// Not part of public API -class BinaryReaderImpl extends BinaryReader { - final Uint8List _buffer; - final ByteData _byteData; - final int _bufferLength; - final TypeRegistryImpl _typeRegistry; - - int _bufferLimit; - int _offset = 0; - - /// Not part of public API - BinaryReaderImpl(this._buffer, TypeRegistry typeRegistry, [int? bufferLength]) - : _byteData = ByteData.view(_buffer.buffer, _buffer.offsetInBytes), - _bufferLength = bufferLength ?? _buffer.length, - _bufferLimit = bufferLength ?? _buffer.length, - _typeRegistry = typeRegistry as TypeRegistryImpl; - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - int get availableBytes => _bufferLimit - _offset; - - @override - int get usedBytes => _offset; - - void _limitAvailableBytes(int bytes) { - _requireBytes(bytes); - _bufferLimit = _offset + bytes; - } - - void _resetLimit() { - _bufferLimit = _bufferLength; - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void _requireBytes(int bytes) { - if (_offset + bytes > _bufferLimit) { - throw RangeError('Not enough bytes available.'); - } - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - void skip(int bytes) { - _requireBytes(bytes); - _offset += bytes; - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - int readByte() { - _requireBytes(1); - return _buffer[_offset++]; - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - Uint8List viewBytes(int bytes) { - _requireBytes(bytes); - _offset += bytes; - return _buffer.view(_offset - bytes, bytes); - } - - @override - Uint8List peekBytes(int bytes) { - _requireBytes(bytes); - return _buffer.view(_offset, bytes); - } - - @override - int readWord() { - _requireBytes(2); - return _buffer[_offset++] | _buffer[_offset++] << 8; - } - - @override - int readInt32() { - _requireBytes(4); - _offset += 4; - return _byteData.getInt32(_offset - 4, Endian.little); - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - int readUint32() { - _requireBytes(4); - _offset += 4; - return _buffer.readUint32(_offset - 4); - } - - /// Not part of public API - int peekUint32() { - _requireBytes(4); - return _buffer.readUint32(_offset); - } - - @override - int readInt() { - return readDouble().toInt(); - } - - @override - double readDouble() { - _requireBytes(8); - var value = _byteData.getFloat64(_offset, Endian.little); - _offset += 8; - return value; - } - - @override - bool readBool() { - return readByte() > 0; - } - - @override - String readString( - [int? byteCount, - Converter, String> decoder = BinaryReader.utf8Decoder]) { - byteCount ??= readUint32(); - var view = viewBytes(byteCount); - return decoder.convert(view); - } - - @override - Uint8List readByteList([int? length]) { - length ??= readUint32(); - _requireBytes(length); - var byteList = _buffer.sublist(_offset, _offset + length); - _offset += length; - return byteList; - } - - @override - List readIntList([int? length]) { - length ??= readUint32(); - _requireBytes(length * 8); - var byteData = _byteData; - var list = List.filled(length, 0, growable: true); - for (var i = 0; i < length; i++) { - list[i] = byteData.getFloat64(_offset, Endian.little).toInt(); - _offset += 8; - } - return list; - } - - @override - List readDoubleList([int? length]) { - length ??= readUint32(); - _requireBytes(length * 8); - var byteData = _byteData; - var list = List.filled(length, 0.0, growable: true); - for (var i = 0; i < length; i++) { - list[i] = byteData.getFloat64(_offset, Endian.little); - _offset += 8; - } - return list; - } - - @override - List readBoolList([int? length]) { - length ??= readUint32(); - _requireBytes(length); - var list = List.filled(length, false, growable: true); - for (var i = 0; i < length; i++) { - list[i] = _buffer[_offset++] > 0; - } - return list; - } - - @override - List readStringList( - [int? length, - Converter, String> decoder = BinaryReader.utf8Decoder]) { - length ??= readUint32(); - var list = List.filled(length, '', growable: true); - for (var i = 0; i < length; i++) { - list[i] = readString(null, decoder); - } - return list; - } - - @override - List readList([int? length]) { - length ??= readUint32(); - var list = List.filled(length, null, growable: true); - for (var i = 0; i < length; i++) { - list[i] = read(); - } - return list; - } - - @override - Map readMap([int? length]) { - length ??= readUint32(); - var map = {}; - for (var i = 0; i < length; i++) { - map[read()] = read(); - } - return map; - } - - /// Not part of public API - dynamic readKey() { - var keyType = readByte(); - if (keyType == FrameKeyType.uintT) { - return readUint32(); - } else if (keyType == FrameKeyType.utf8StringT) { - var byteCount = readByte(); - return BinaryReader.utf8Decoder.convert(viewBytes(byteCount)); - } else { - throw HiveError('Unsupported key type. Frame might be corrupted.'); - } - } - - @override - HiveList readHiveList([int? length]) { - length ??= readUint32(); - var boxNameLength = readByte(); - var boxName = String.fromCharCodes(viewBytes(boxNameLength)); - var keys = List.filled(length, null, growable: true); - for (var i = 0; i < length; i++) { - keys[i] = readKey(); - } - - return HiveListImpl.lazy(boxName, keys); - } - - /// Not part of public API - Future readFrame( - {HiveCipher? cipher, bool lazy = false, int frameOffset = 0}) async { - // frame length is stored on 4 bytes - if (availableBytes < 4) return null; - - // frame length should be at least 8 bytes - var frameLength = readUint32(); - if (frameLength < 8) return null; - - // frame is bigger than avaible bytes - if (availableBytes < frameLength - 4) return null; - - var crc = _buffer.readUint32(_offset + frameLength - 8); - var computedCrc = Crc32.compute( - _buffer, - offset: _offset - 4, - length: frameLength - 4, - crc: cipher?.calculateKeyCrc() ?? 0, - ); - - // frame is corrupted or provided chiper is different - if (computedCrc != crc) return null; - - _limitAvailableBytes(frameLength - 8); - Frame frame; - dynamic key = readKey(); - - if (availableBytes == 0) { - frame = Frame.deleted(key); - } else if (lazy) { - frame = Frame.lazy(key); - } else if (cipher == null) { - frame = Frame(key, read()); - } else { - frame = Frame(key, await readEncrypted(cipher)); - } - - frame - ..length = frameLength - ..offset = frameOffset; - - skip(availableBytes); - _resetLimit(); - skip(4); // Skip CRC - - return frame; - } - - @override - dynamic read([int? typeId]) { - typeId ??= readByte(); - switch (typeId) { - case FrameValueType.nullT: - return null; - case FrameValueType.intT: - return readInt(); - case FrameValueType.doubleT: - return readDouble(); - case FrameValueType.boolT: - return readBool(); - case FrameValueType.stringT: - return readString(); - case FrameValueType.byteListT: - return readByteList(); - case FrameValueType.intListT: - return readIntList(); - case FrameValueType.doubleListT: - return readDoubleList(); - case FrameValueType.boolListT: - return readBoolList(); - case FrameValueType.stringListT: - return readStringList(); - case FrameValueType.listT: - return readList(); - case FrameValueType.mapT: - return readMap(); - case FrameValueType.hiveListT: - return readHiveList(); - default: - var resolved = _typeRegistry.findAdapterForTypeId(typeId); - if (resolved == null) { - throw HiveError('Cannot read, unknown typeId: $typeId. ' - 'Did you forget to register an adapter?'); - } - return resolved.adapter.read(this); - } - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Future readEncrypted(HiveCipher cipher) async { - var inpLength = availableBytes; - var out = Uint8List(inpLength); - var outLength = await cipher.decrypt(_buffer, _offset, inpLength, out, 0); - _offset += inpLength; - - var valueReader = BinaryReaderImpl(out, _typeRegistry, outLength); - return valueReader.read(); - } -} diff --git a/hive/lib/src/binary/binary_writer.dart b/hive/lib/src/binary/binary_writer.dart deleted file mode 100644 index c40a8cb24..000000000 --- a/hive/lib/src/binary/binary_writer.dart +++ /dev/null @@ -1,66 +0,0 @@ -part of hive; - -/// The [BinaryWriter] is used to encode data to the binary format. -abstract class BinaryWriter { - /// The UTF-8 encoder is used to encode Strings. - static const utf8Encoder = Utf8Encoder(); - - /// Write a single byte. - void writeByte(int byte); - - /// Write a 16-bit unsigned integer as two bytes. - void writeWord(int value); - - /// Write a 32-bit signed integer as four bytes. - void writeInt32(int value); - - /// Write a 32-bit unsigned integer as four bytes. - void writeUint32(int value); - - /// Write a 64-bit signed integer as eight bytes. - void writeInt(int value); - - /// Write a 64-bit double as eight bytes. - void writeDouble(double value); - - /// Write a boolean. - void writeBool(bool value); - - /// Encode the UTF-8 String [value] and write its bytes. - void writeString( - String value, { - bool writeByteCount = true, - Converter> encoder = utf8Encoder, - }); - - /// Write a list of [bytes]. - void writeByteList(List bytes, {bool writeLength = true}); - - /// Write a [list] of integers. - void writeIntList(List list, {bool writeLength = true}); - - /// Write a [list] of doubles. - void writeDoubleList(List list, {bool writeLength = true}); - - /// Write a [list] of booleans. - void writeBoolList(List list, {bool writeLength = true}); - - /// Write a [list] of Strings. - void writeStringList( - List list, { - bool writeLength = true, - Converter> encoder = utf8Encoder, - }); - - /// Write a [list]. - void writeList(List list, {bool writeLength = true}); - - /// Write a [map]. - void writeMap(Map map, {bool writeLength = true}); - - /// Write a [HiveList]. - void writeHiveList(HiveList list, {bool writeLength = true}); - - /// Write any [value]. - void write(T value, {bool writeTypeId = true}); -} diff --git a/hive/lib/src/binary/binary_writer_impl.dart b/hive/lib/src/binary/binary_writer_impl.dart deleted file mode 100644 index e13f6975e..000000000 --- a/hive/lib/src/binary/binary_writer_impl.dart +++ /dev/null @@ -1,429 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/crypto/crc32.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:hive/src/util/extensions.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -class BinaryWriterImpl extends BinaryWriter { - static const _initBufferSize = 4096; - - final TypeRegistryImpl _typeRegistry; - Uint8List _buffer = Uint8List(_initBufferSize); - - ByteData? _byteDataInstance; - - int _offset = 0; - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - ByteData get _byteData { - _byteDataInstance ??= ByteData.view(_buffer.buffer); - return _byteDataInstance!; - } - - /// Not part of public API - BinaryWriterImpl(TypeRegistry typeRegistry) - : _typeRegistry = typeRegistry as TypeRegistryImpl; - - /// Not part of public API - @visibleForTesting - BinaryWriterImpl.withBuffer(this._buffer, this._typeRegistry); - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void _reserveBytes(int count) { - if (_buffer.length - _offset < count) { - _increaseBufferSize(count); - } - } - - void _increaseBufferSize(int count) { -// We will create a list in the range of 2-4 times larger than required. - var newSize = _pow2roundup((_offset + count) * 2); - var newBuffer = Uint8List(newSize); - newBuffer.setRange(0, _offset, _buffer); - _buffer = newBuffer; - _byteDataInstance = null; - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void _addBytes(List bytes) { - ArgumentError.checkNotNull(bytes); - - var length = bytes.length; - _reserveBytes(length); - _buffer.setRange(_offset, _offset + length, bytes); - _offset += length; - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - void writeByte(int byte) { - ArgumentError.checkNotNull(byte); - - _reserveBytes(1); - _buffer[_offset++] = byte; - } - - @override - void writeWord(int value) { - ArgumentError.checkNotNull(value); - - _reserveBytes(2); - _buffer[_offset++] = value; - _buffer[_offset++] = value >> 8; - } - - @override - void writeInt32(int value) { - ArgumentError.checkNotNull(value); - - _reserveBytes(4); - _byteData.setInt32(_offset, value, Endian.little); - _offset += 4; - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - @override - void writeUint32(int value) { - ArgumentError.checkNotNull(value); - - _reserveBytes(4); - _buffer.writeUint32(_offset, value); - _offset += 4; - } - - @override - void writeInt(int value) { - writeDouble(value.toDouble()); - } - - @override - void writeDouble(double value) { - ArgumentError.checkNotNull(value); - - _reserveBytes(8); - _byteData.setFloat64(_offset, value, Endian.little); - _offset += 8; - } - - @override - void writeBool(bool value) { - ArgumentError.checkNotNull(value); - - writeByte(value ? 1 : 0); - } - - @override - void writeString( - String value, { - bool writeByteCount = true, - Converter> encoder = BinaryWriter.utf8Encoder, - }) { - ArgumentError.checkNotNull(value); - - var bytes = encoder.convert(value); - if (writeByteCount) { - writeUint32(bytes.length); - } - _addBytes(bytes); - } - - @override - void writeByteList(List bytes, {bool writeLength = true}) { - ArgumentError.checkNotNull(bytes); - - if (writeLength) { - writeUint32(bytes.length); - } - _addBytes(bytes); - } - - @override - void writeIntList(List list, {bool writeLength = true}) { - ArgumentError.checkNotNull(list); - - var length = list.length; - if (writeLength) { - writeUint32(length); - } - _reserveBytes(length * 8); - var byteData = _byteData; - for (var i = 0; i < length; i++) { - byteData.setFloat64(_offset, list[i].toDouble(), Endian.little); - _offset += 8; - } - } - - @override - void writeDoubleList(List list, {bool writeLength = true}) { - ArgumentError.checkNotNull(list); - - var length = list.length; - if (writeLength) { - writeUint32(length); - } - _reserveBytes(length * 8); - var byteData = _byteData; - for (var i = 0; i < length; i++) { - byteData.setFloat64(_offset, list[i], Endian.little); - _offset += 8; - } - } - - @override - void writeBoolList(List list, {bool writeLength = true}) { - ArgumentError.checkNotNull(list); - - var length = list.length; - if (writeLength) { - writeUint32(length); - } - _reserveBytes(length); - for (var i = 0; i < length; i++) { - _buffer[_offset++] = list[i] ? 1 : 0; - } - } - - @override - void writeStringList( - List list, { - bool writeLength = true, - Converter> encoder = BinaryWriter.utf8Encoder, - }) { - ArgumentError.checkNotNull(list); - - if (writeLength) { - writeUint32(list.length); - } - for (var str in list) { - var strBytes = encoder.convert(str); - writeUint32(strBytes.length); - _addBytes(strBytes); - } - } - - @override - void writeList(List list, {bool writeLength = true}) { - ArgumentError.checkNotNull(list); - - if (writeLength) { - writeUint32(list.length); - } - for (var i = 0; i < list.length; i++) { - write(list[i]); - } - } - - @override - void writeMap(Map map, {bool writeLength = true}) { - ArgumentError.checkNotNull(map); - - if (writeLength) { - writeUint32(map.length); - } - for (var key in map.keys) { - write(key); - write(map[key]); - } - } - - /// Not part of public API - void writeKey(dynamic key) { - ArgumentError.checkNotNull(key); - - if (key is String) { - writeByte(FrameKeyType.utf8StringT); - var bytes = BinaryWriter.utf8Encoder.convert(key); - writeByte(bytes.length); - _addBytes(bytes); - } else { - writeByte(FrameKeyType.uintT); - writeUint32(key as int); - } - } - - @override - void writeHiveList(HiveList list, {bool writeLength = true}) { - ArgumentError.checkNotNull(list); - - if (writeLength) { - writeUint32(list.length); - } - var boxName = (list as HiveListImpl).boxName; - writeByte(boxName.length); - _addBytes(boxName.codeUnits); - for (var obj in list) { - writeKey(obj.key); - } - } - - /// Not part of public API - Future writeFrame(Frame frame, {HiveCipher? cipher}) async { - ArgumentError.checkNotNull(frame); - - var startOffset = _offset; - _reserveBytes(4); - _offset += 4; // reserve bytes for length - - writeKey(frame.key); - - if (!frame.deleted) { - if (cipher == null) { - write(frame.value); - } else { - await writeEncrypted(frame.value, cipher); - } - } - - var frameLength = _offset - startOffset + 4; - _buffer.writeUint32(startOffset, frameLength); - - var crc = Crc32.compute( - _buffer, - offset: startOffset, - length: frameLength - 4, - crc: cipher?.calculateKeyCrc() ?? 0, - ); - writeUint32(crc); - - return frameLength; - } - - @override - void write(T value, {bool writeTypeId = true}) { - if (value == null) { - if (writeTypeId) { - writeByte(FrameValueType.nullT); - } - } else if (value is int) { - if (writeTypeId) { - writeByte(FrameValueType.intT); - } - writeInt(value); - } else if (value is double) { - if (writeTypeId) { - writeByte(FrameValueType.doubleT); - } - writeDouble(value); - } else if (value is bool) { - if (writeTypeId) { - writeByte(FrameValueType.boolT); - } - writeBool(value); - } else if (value is String) { - if (writeTypeId) { - writeByte(FrameValueType.stringT); - } - writeString(value); - } else if (value is List) { - _writeList(value, writeTypeId: writeTypeId); - } else if (value is Map) { - if (writeTypeId) { - writeByte(FrameValueType.mapT); - } - writeMap(value); - } else { - var resolved = _typeRegistry.findAdapterForValue(value); - if (resolved == null) { - throw HiveError('Cannot write, unknown type: ${value.runtimeType}. ' - 'Did you forget to register an adapter?'); - } - if (writeTypeId) { - writeByte(resolved.typeId); - } - resolved.adapter.write(this, value); - } - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void _writeList(List value, {bool writeTypeId = true}) { - if (value is HiveList) { - if (writeTypeId) { - writeByte(FrameValueType.hiveListT); - } - writeHiveList(value); - } else if (value.contains(null)) { - if (writeTypeId) { - writeByte(FrameValueType.listT); - } - writeList(value); - } else if (value is Uint8List) { - if (writeTypeId) { - writeByte(FrameValueType.byteListT); - } - writeByteList(value); - } else if (value is List) { - if (writeTypeId) { - writeByte(FrameValueType.intListT); - } - writeIntList(value); - } else if (value is List) { - if (writeTypeId) { - writeByte(FrameValueType.doubleListT); - } - writeDoubleList(value); - } else if (value is List) { - if (writeTypeId) { - writeByte(FrameValueType.boolListT); - } - writeBoolList(value); - } else if (value is List) { - if (writeTypeId) { - writeByte(FrameValueType.stringListT); - } - writeStringList(value); - } else { - if (writeTypeId) { - writeByte(FrameValueType.listT); - } - writeList(value); - } - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - FutureOr writeEncrypted(dynamic value, HiveCipher cipher, - {bool writeTypeId = true}) async { - var valueWriter = BinaryWriterImpl(_typeRegistry) - ..write(value, writeTypeId: writeTypeId); - var inp = valueWriter._buffer; - var inpLength = valueWriter._offset; - - _reserveBytes(cipher.maxEncryptedSize(inp)); - - var len = await cipher.encrypt(inp, 0, inpLength, _buffer, _offset); - - _offset += len; - } - - /// Not part of public API - Uint8List toBytes() { - return Uint8List.view(_buffer.buffer, 0, _offset); - } - - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - static int _pow2roundup(int x) { - assert(x > 0); - --x; - x |= x >> 1; - x |= x >> 2; - x |= x >> 4; - x |= x >> 8; - x |= x >> 16; - return x + 1; - } -} diff --git a/hive/lib/src/binary/frame.dart b/hive/lib/src/binary/frame.dart deleted file mode 100644 index 5130f4700..000000000 --- a/hive/lib/src/binary/frame.dart +++ /dev/null @@ -1,156 +0,0 @@ -import 'package:hive/hive.dart'; - -/// Not part of public API -class Frame { - /// Not part of public API - final dynamic key; - - /// Not part of public API - final dynamic value; - - /// Not part of public API - final bool deleted; - - /// Not part of public API - final bool lazy; - - /// Not part of public API - int? length; - - /// Not part of public API - int offset = -1; - - /// Not part of public API - Frame(this.key, this.value, {this.length, this.offset = -1}) - : lazy = false, - deleted = false { - assert(assertKey(key)); - } - - /// Not part of public API - Frame.deleted(this.key, {this.length}) - : value = null, - lazy = false, - deleted = true, - offset = -1 { - assert(assertKey(key)); - } - - /// Not part of public API - Frame.lazy(this.key, {this.length, this.offset = -1}) - : value = null, - lazy = true, - deleted = false { - assert(assertKey(key)); - } - - /// Not part of public API - static bool assertKey(dynamic key) { - if (key is int) { - if (key < 0 || key > 0xFFFFFFFF) { - throw HiveError('Integer keys need to be in the range 0 - 0xFFFFFFFF'); - } - } else if (key is String) { - if (key.length > 0xFF) { - throw HiveError('String keys need to be a max length of 255'); - } - } else { - throw HiveError('Keys need to be Strings or integers'); - } - - return true; - } - - /// Not part of public API - Frame toLazy() { - if (deleted) return this; - return Frame.lazy( - key, - length: length, - offset: offset, - ); - } - - @override - bool operator ==(dynamic other) { - if (other is Frame) { - return key == other.key && - value == other.value && - length == other.length && - deleted == other.deleted; - } else { - return false; - } - } - - @override - String toString() { - if (deleted) { - return 'Frame.deleted(key: $key, length: $length)'; - } else if (lazy) { - return 'Frame.lazy(key: $key, length: $length, offset: $offset)'; - } else { - return 'Frame(key: $key, value: $value, ' - 'length: $length, offset: $offset)'; - } - } - - @override - int get hashCode => - runtimeType.hashCode ^ - key.hashCode ^ - value.hashCode ^ - length.hashCode ^ - deleted.hashCode; -} - -/// Possible Key types -class FrameKeyType { - /// Integer key - static const uintT = 0; - - /// String key - static const utf8StringT = 1; -} - -/// Possible value types -class FrameValueType { - /// null - static const nullT = 0; - - /// int - static const intT = 1; - - /// double - static const doubleT = 2; - - /// bool - static const boolT = 3; - - /// String - static const stringT = 4; - - /// Uint8List - static const byteListT = 5; - - /// List - static const intListT = 6; - - /// List - static const doubleListT = 7; - - /// List - static const boolListT = 8; - - /// List - static const stringListT = 9; - - /// List - static const listT = 10; - - /// Map - static const mapT = 11; - - /// List - static const hiveListT = 12; -} diff --git a/hive/lib/src/binary/frame_helper.dart b/hive/lib/src/binary/frame_helper.dart deleted file mode 100644 index 2552ce278..000000000 --- a/hive/lib/src/binary/frame_helper.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/box/keystore.dart'; - -/// Not part of public API -class FrameHelper { - /// Not part of public API - Future framesFromBytes( - Uint8List bytes, - Keystore? keystore, - TypeRegistry registry, - HiveCipher? cipher, - ) async { - var reader = BinaryReaderImpl(bytes, registry); - - while (reader.availableBytes != 0) { - var frameOffset = reader.usedBytes; - - var frame = await reader.readFrame( - cipher: cipher, - lazy: false, - frameOffset: frameOffset, - ); - if (frame == null) return frameOffset; - - keystore!.insert(frame, notify: false); - } - - return -1; - } -} diff --git a/hive/lib/src/box/box.dart b/hive/lib/src/box/box.dart deleted file mode 100644 index 697667868..000000000 --- a/hive/lib/src/box/box.dart +++ /dev/null @@ -1,41 +0,0 @@ -part of hive; - -/// Boxes contain all of your data. In the browser, each box has its own -/// IndexedDB database. On all other platforms, each Box is stored in a -/// separate file in the Hive home directory. -/// -/// Write operations are asynchronous but the new values are immediately -/// available. The returned `Future` finishes when the change is written to -/// the backend. If this operation fails, the changes are being reverted. -/// -/// Read operations for normal boxes are synchronous (the entries are in -/// memory). Lazy boxes have asynchronous read operations. -abstract class Box implements BoxBase { - /// All the values in the box. - /// - /// The values are in the same order as their keys. - Iterable get values; - - /// Returns an iterable which contains all values starting with the value - /// associated with [startKey] (inclusive) to the value associated with - /// [endKey] (inclusive). - /// - /// If [startKey] does not exist, an empty iterable is returned. If [endKey] - /// does not exist or is before [startKey], it is ignored. - /// - /// The values are in the same order as their keys. - Iterable valuesBetween({dynamic startKey, dynamic endKey}); - - /// Returns the value associated with the given [key]. If the key does not - /// exist, `null` is returned. - /// - /// If [defaultValue] is specified, it is returned in case the key does not - /// exist. - E? get(dynamic key, {E? defaultValue}); - - /// Returns the value associated with the n-th key. - E? getAt(int index); - - /// Returns a map which contains all key - value pairs of the box. - Map toMap(); -} diff --git a/hive/lib/src/box/box_base.dart b/hive/lib/src/box/box_base.dart deleted file mode 100644 index 21dbafaa1..000000000 --- a/hive/lib/src/box/box_base.dart +++ /dev/null @@ -1,165 +0,0 @@ -part of hive; - -/// A event representing a change in a box. -class BoxEvent { - /// The key of the changed entry - final dynamic key; - - /// The value of a new entry of `null` if the entry has been deleted - final dynamic value; - - /// Whether the entry has been deleted - final bool deleted; - - /// Create a new BoxEvent (Hive internal) - BoxEvent(this.key, this.value, this.deleted); - - @override - bool operator ==(dynamic other) { - if (other is BoxEvent) { - return other.key == key && other.value == value; - } - return false; - } - - @override - int get hashCode => runtimeType.hashCode ^ key.hashCode ^ value.hashCode; -} - -/// Boxes contain all of your data. In the browser, each box has its own -/// IndexedDB database. On all other platforms, each Box is stored in a -/// seperate file in the Hive home directory. -/// -/// Write operations are asynchronous but the new values are immediately -/// available. The returned `Future` finishes when the change is written to -/// the backend. If this operation fails, the changes are being reverted. -/// -/// Read operations for normal boxes are synchronous (the entries are in -/// memory). Lazy boxes have asynchronous read operations. -abstract class BoxBase { - /// The name of the box. Names are always lowercase. - String get name; - - /// Whether this box is currently open. - /// - /// Most of the operations on a box require it to be open. - bool get isOpen; - - /// The location of the box in the file system. In the browser, this is null. - String? get path; - - /// Whether this box is lazy or not. - /// - /// This is equivalent to `box is LazyBox`. - bool get lazy; - - /// All the keys in the box. - /// - /// The keys are sorted alphabetically in ascending order. - Iterable get keys; - - /// The number of entries in the box. - int get length; - - /// Returns `true` if there are no entries in this box. - bool get isEmpty; - - /// Returns true if there is at least one entries in this box. - bool get isNotEmpty; - - /// Get the n-th key in the box. - dynamic keyAt(int index); - - /// Returns a broadcast stream of change events. - /// - /// If the [key] parameter is provided, only events for the specified key are - /// broadcasted. - Stream watch({dynamic key}); - - /// Checks whether the box contains the [key]. - bool containsKey(dynamic key); - - /// Saves the [key] - [value] pair. - Future put( - dynamic key, - E value, { - bool notify = true, - }); - - /// Associates the [value] with the n-th key. An exception is raised if the - /// key does not exist. - Future putAt( - int index, - E value, { - bool notify = true, - }); - - /// Saves all the key - value pairs in the [entries] map. - Future putAll( - Map entries, { - bool notify = true, - }); - - /// Saves the [value] with an auto-increment key. - Future add( - E value, { - bool notify = true, - }); - - /// Saves all the [values] with auto-increment keys. - Future> addAll( - Iterable values, { - bool notify = true, - }); - - /// Beta: Performs the following write-only operations in memory and later - /// flushes - /// - /// This can be used to improve speed on many small write operations - // Future transaction(Future Function() operation); - - /// Deletes the given [key] from the box. - /// - /// If it does not exist, nothing happens. - Future delete( - dynamic key, { - bool notify = true, - }); - - /// Deletes the n-th key from the box. - /// - /// If it does not exist, nothing happens. - Future deleteAt( - int index, { - bool notify = true, - }); - - /// Deletes all the given [keys] from the box. - /// - /// If a key does not exist, it is skipped. - Future deleteAll( - Iterable keys, { - bool notify = true, - }); - - /// Induces compaction manually. This is rarely needed. You should consider - /// providing a custom compaction strategy instead. - Future compact(); - - /// Removes all entries from the box. - Future clear({bool notify = true}); - - /// Closes the box. - /// - /// Be careful, this closes all instances of this box. You have to make sure - /// that you don't access the box anywhere else after that. - Future close(); - - /// Removes the file which contains the box and closes the box. - /// - /// In the browser, the IndexedDB database is being removed. - Future deleteFromDisk(); - - /// Flushes all pending changes of the box to disk. - Future flush(); -} diff --git a/hive/lib/src/box/box_base_impl.dart b/hive/lib/src/box/box_base_impl.dart deleted file mode 100644 index 58ae86856..000000000 --- a/hive/lib/src/box/box_base_impl.dart +++ /dev/null @@ -1,331 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -abstract class BoxBaseImpl implements BoxBase { - static BoxBase nullImpl() => _NullBoxBase(); - - @override - final String name; - - /// Not part of public API - @visibleForTesting - final HiveImpl hive; - - final CompactionStrategy _compactionStrategy; - - /// Not part of public API - @protected - final StorageBackend backend; - - /// Not part of public API - /// - /// Not `late final` for testing - @protected - @visibleForTesting - late Keystore keystore; - - bool _open = true; - - /// Not part of public API - BoxBaseImpl( - this.hive, - this.name, - KeyComparator? keyComparator, - this._compactionStrategy, - this.backend, - ) { - keystore = Keystore(this, ChangeNotifier(), keyComparator); - } - - /// Not part of public API - Type get valueType => E; - - @override - bool get isOpen => _open; - - @override - String? get path => backend.path; - - @override - Iterable get keys { - checkOpen(); - return keystore.getKeys(); - } - - @override - int get length { - checkOpen(); - return keystore.length; - } - - @override - bool get isEmpty => length == 0; - - @override - bool get isNotEmpty => length > 0; - - /// Not part of public API - @protected - void checkOpen() { - if (!_open) { - throw HiveError('Box has already been closed.'); - } - } - - @override - Stream watch({dynamic key}) { - checkOpen(); - return keystore.watch(key: key); - } - - @override - dynamic keyAt(int index) { - checkOpen(); - return keystore.getAt(index)!.key; - } - - /// Not part of public API - Future initialize() { - return backend.initialize(hive, keystore, lazy); - } - - @override - bool containsKey(dynamic key) { - checkOpen(); - return keystore.containsKey(key); - } - - @override - Future put( - dynamic key, - E value, { - bool notify = true, - }) => - putAll( - {key: value}, - notify: notify, - ); - - @override - Future delete( - dynamic key, { - bool notify = true, - }) => - deleteAll( - [key], - notify: notify, - ); - - @override - Future add( - E value, { - bool notify = true, - }) async { - var key = keystore.autoIncrement(); - await put(key, value, notify: notify); - return key; - } - - @override - Future> addAll( - Iterable values, { - bool notify = true, - }) async { - var entries = {}; - for (var value in values) { - entries[keystore.autoIncrement()] = value; - } - await putAll(entries, notify: notify); - return entries.keys; - } - - @override - Future putAt( - int index, - E value, { - bool notify = true, - }) { - return putAll({keystore.getAt(index)!.key: value}, notify: notify); - } - - @override - Future deleteAt( - int index, { - bool notify = true, - }) { - return delete(keystore.getAt(index)!.key, notify: notify); - } - - @override - Future clear({bool notify = true}) async { - checkOpen(); - - await backend.clear(); - return keystore.clear(notify: notify); - } - - @override - Future compact() async { - checkOpen(); - - if (!backend.supportsCompaction) return; - if (keystore.deletedEntries == 0) return; - - await flush(); - - await backend.compact(keystore.frames); - keystore.resetDeletedEntries(); - } - - /// Not part of public API - @protected - FutureOr performCompactionIfNeeded() { - if (_compactionStrategy(keystore.length, keystore.deletedEntries)) { - return compact(); - } - } - - @override - Future close() async { - if (!_open) return; - - try { - await flush(); - } finally { - _open = false; - } - - await keystore.close(); - hive.unregisterBox(name); - - await backend.close(); - } - - @override - Future deleteFromDisk() async { - if (_open) { - // deleteFromDisk does not work in some cases - await clear(); - _open = false; - await keystore.close(); - hive.unregisterBox(name); - } - - await backend.deleteFromDisk(); - } -} - -class _NullBoxBase implements BoxBase { - @override - Never add( - E value, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never addAll( - Iterable values, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never clear({bool notify = true}) => throw UnimplementedError(); - - @override - Never close() => throw UnimplementedError(); - - @override - Never compact() => throw UnimplementedError(); - - @override - Never containsKey(key) => throw UnimplementedError(); - - @override - Never delete( - key, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never deleteAll( - Iterable keys, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never deleteAt( - int index, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never deleteFromDisk() => throw UnimplementedError(); - - @override - Never get isEmpty => throw UnimplementedError(); - - @override - Never get isNotEmpty => throw UnimplementedError(); - - @override - Never get isOpen => throw UnimplementedError(); - - @override - Never keyAt(int index) => throw UnimplementedError(); - - @override - Never get keys => throw UnimplementedError(); - - @override - Never get lazy => throw UnimplementedError(); - - @override - Never get length => throw UnimplementedError(); - - @override - Never get name => throw UnimplementedError(); - - @override - Never get path => throw UnimplementedError(); - - @override - Never put( - key, - E value, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never putAll( - Map entries, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never putAt( - int index, - E value, { - bool notify = true, - }) => - throw UnimplementedError(); - - @override - Never watch({key}) => throw UnimplementedError(); - - @override - Never flush() => throw UnimplementedError(); -} diff --git a/hive/lib/src/box/box_impl.dart b/hive/lib/src/box/box_impl.dart deleted file mode 100644 index 7a5222dca..000000000 --- a/hive/lib/src/box/box_impl.dart +++ /dev/null @@ -1,117 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/box_base_impl.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:hive/src/object/hive_object.dart'; - -/// Not part of public API -class BoxImpl extends BoxBaseImpl implements Box { - /// Not part of public API - BoxImpl( - HiveImpl hive, - String name, - KeyComparator? keyComparator, - CompactionStrategy compactionStrategy, - StorageBackend backend, - ) : super(hive, name, keyComparator, compactionStrategy, backend); - - @override - final bool lazy = false; - - @override - Iterable get values { - checkOpen(); - - return keystore.getValues(); - } - - @override - Iterable valuesBetween({dynamic startKey, dynamic endKey}) { - checkOpen(); - - return keystore.getValuesBetween(startKey, endKey); - } - - @override - E? get(dynamic key, {E? defaultValue}) { - checkOpen(); - - var frame = keystore.get(key); - if (frame != null) { - return frame.value as E?; - } else { - if (defaultValue != null && defaultValue is HiveObjectMixin) { - defaultValue.init(key, this); - } - return defaultValue; - } - } - - @override - E? getAt(int index) { - checkOpen(); - - return keystore.getAt(index)?.value as E?; - } - - @override - Future putAll( - Map kvPairs, { - bool notify = true, - }) { - var frames = []; - for (var key in kvPairs.keys) { - frames.add(Frame(key, kvPairs[key])); - } - - return _writeFrames(frames, notify: notify); - } - - @override - Future deleteAll(Iterable keys, {bool notify = true}) { - var frames = []; - for (var key in keys) { - if (keystore.containsKey(key)) { - frames.add(Frame.deleted(key)); - } - } - - return _writeFrames(frames, notify: notify); - } - - Future _writeFrames( - List frames, { - bool notify = true, - }) async { - checkOpen(); - - if (!keystore.beginTransaction(frames, notify: notify)) return; - - try { - await backend.writeFrames(frames); - keystore.commitTransaction(); - } catch (e) { - keystore.cancelTransaction(); - rethrow; - } - - await performCompactionIfNeeded(); - } - - @override - Map toMap() { - var map = {}; - for (var frame in keystore.frames) { - map[frame.key] = frame.value as E; - } - return map; - } - - @override - Future flush() async { - await backend.flush(); - } -} diff --git a/hive/lib/src/box/change_notifier.dart b/hive/lib/src/box/change_notifier.dart deleted file mode 100644 index d04e9da2d..000000000 --- a/hive/lib/src/box/change_notifier.dart +++ /dev/null @@ -1,38 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -class ChangeNotifier { - final StreamController _streamController; - - /// Not part of public API - ChangeNotifier() : _streamController = StreamController.broadcast(); - - /// Not part of public API - @visibleForTesting - ChangeNotifier.debug(this._streamController); - - /// Not part of public API - void notify(Frame frame) { - if (!_streamController.isClosed) { - _streamController.add(BoxEvent(frame.key, frame.value, frame.deleted)); - } - } - - /// Not part of public API - Stream watch({dynamic key}) { - if (key != null) { - return _streamController.stream.where((it) => it.key == key); - } else { - return _streamController.stream; - } - } - - /// Not part of public API - Future close() { - return _streamController.close(); - } -} diff --git a/hive/lib/src/box/default_compaction_strategy.dart b/hive/lib/src/box/default_compaction_strategy.dart deleted file mode 100644 index 0bcd0ecdb..000000000 --- a/hive/lib/src/box/default_compaction_strategy.dart +++ /dev/null @@ -1,9 +0,0 @@ -const _deletedRatio = 0.15; -const _deletedThreshold = 60; - -/// Default compaction strategy compacts if 15% of total values and at least 60 -/// values have been deleted -bool defaultCompactionStrategy(int entries, int deletedEntries) { - return deletedEntries > _deletedThreshold && - deletedEntries / entries > _deletedRatio; -} diff --git a/hive/lib/src/box/default_key_comparator.dart b/hive/lib/src/box/default_key_comparator.dart deleted file mode 100644 index d7833ff53..000000000 --- a/hive/lib/src/box/default_key_comparator.dart +++ /dev/null @@ -1,20 +0,0 @@ -/// Efficient default implementation to compare keys -int defaultKeyComparator(dynamic k1, dynamic k2) { - if (k1 is int) { - if (k2 is int) { - if (k1 > k2) { - return 1; - } else if (k1 < k2) { - return -1; - } else { - return 0; - } - } else { - return -1; - } - } else if (k2 is String) { - return (k1 as String).compareTo(k2); - } else { - return 1; - } -} diff --git a/hive/lib/src/box/keystore.dart b/hive/lib/src/box/keystore.dart deleted file mode 100644 index 75d6a024c..000000000 --- a/hive/lib/src/box/keystore.dart +++ /dev/null @@ -1,284 +0,0 @@ -// ignore_for_file: invalid_use_of_protected_member -// ignore_for_file: invalid_use_of_visible_for_testing_member - -import 'dart:collection'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/default_key_comparator.dart'; -import 'package:hive/src/object/hive_object.dart'; -import 'package:hive/src/util/indexable_skip_list.dart'; -import 'package:meta/meta.dart'; - -import 'box_base_impl.dart'; - -/// Not part of public API -class KeyTransaction { - /// The values that have been added - final List added = []; - - /// The frames that have been deleted - final Map deleted = HashMap(); - - /// Not part of public API - @visibleForTesting - KeyTransaction(); -} - -/// Not part of public API -class Keystore { - final BoxBase _box; - - final ChangeNotifier _notifier; - - final IndexableSkipList _store; - - /// Not part of public API - @visibleForTesting - final ListQueue> transactions = ListQueue(); - - var _deletedEntries = 0; - var _autoIncrement = -1; - - /// Not part of public API - Keystore(this._box, this._notifier, KeyComparator? keyComparator) - : _store = IndexableSkipList(keyComparator ?? defaultKeyComparator); - - /// Not part of public API - factory Keystore.debug({ - Iterable frames = const [], - BoxBase? box, - ChangeNotifier? notifier, - KeyComparator keyComparator = defaultKeyComparator, - }) { - var keystore = Keystore(box ?? BoxBaseImpl.nullImpl(), - notifier ?? ChangeNotifier(), keyComparator); - for (var frame in frames) { - keystore.insert(frame); - } - return keystore; - } - - /// Not part of public API - int get deletedEntries => _deletedEntries; - - /// Not part of public API - int get length => _store.length; - - /// Not part of public API - Iterable get frames => _store.values; - - /// Not part of public API - void resetDeletedEntries() { - _deletedEntries = 0; - } - - /// Not part of public API - int autoIncrement() { - return ++_autoIncrement; - } - - /// Not part of public API - void updateAutoIncrement(int key) { - if (key > _autoIncrement) { - _autoIncrement = key; - } - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - bool containsKey(dynamic key) { - return _store.get(key) != null; - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - dynamic keyAt(int index) { - return _store.getKeyAt(index); - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Frame? get(dynamic key) { - return _store.get(key); - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Frame? getAt(int index) { - return _store.getAt(index); - } - - /// Not part of public API - Iterable getKeys() { - return _store.keys; - } - - /// Not part of public API - Iterable getValues() { - return _store.values.map((e) => e.value as E); - } - - /// Not part of public API - Iterable getValuesBetween([dynamic startKey, dynamic endKey]) sync* { - Iterable iterable; - if (startKey != null) { - iterable = _store.valuesFromKey(startKey); - } else { - iterable = _store.values; - } - - for (var frame in iterable) { - yield frame.value as E; - - if (frame.key == endKey) break; - } - } - - /// Not part of public API - Stream watch({dynamic key}) { - return _notifier.watch(key: key); - } - - /// Not part of public API - Frame? insert(Frame frame, {bool notify = true, bool lazy = false}) { - var value = frame.value; - Frame? deletedFrame; - - if (!frame.deleted) { - var key = frame.key; - if (key is int && key > _autoIncrement) { - _autoIncrement = key; - } - - if (value is HiveObjectMixin) { - value.init(key, _box); - } - - deletedFrame = _store.insert(key, lazy ? frame.toLazy() : frame); - } else { - deletedFrame = _store.delete(frame.key); - } - - if (deletedFrame != null) { - _deletedEntries++; - if (deletedFrame.value is HiveObjectMixin && - !identical(deletedFrame.value, value)) { - (deletedFrame.value as HiveObjectMixin).dispose(); - } - } - - if (notify && (!frame.deleted || deletedFrame != null)) { - _notifier.notify(frame); - } - - return deletedFrame; - } - - /// Not part of public API - bool beginTransaction( - List newFrames, { - bool notify = true, - }) { - var transaction = KeyTransaction(); - for (var frame in newFrames) { - if (!frame.deleted) { - transaction.added.add(frame.key); - } - - var deletedFrame = insert(frame, notify: notify); - if (deletedFrame != null) { - transaction.deleted[frame.key] = deletedFrame; - } - } - - if (transaction.added.isNotEmpty || transaction.deleted.isNotEmpty) { - transactions.add(transaction); - return true; - } else { - return false; - } - } - - /// Not part of public API - void commitTransaction() { - transactions.removeFirst(); - } - - /// Not part of public API - void cancelTransaction({bool notify = true}) { - var canceled = transactions.removeFirst(); - - deleted_loop: - for (var key in canceled.deleted.keys) { - var deletedFrame = canceled.deleted[key]; - for (var t in transactions) { - if (t.deleted.containsKey(key)) { - t.deleted[key] = deletedFrame!; - continue deleted_loop; - } - if (t.added.contains(key)) { - t.deleted[key] = deletedFrame!; - continue deleted_loop; - } - } - - _store.insert(key, deletedFrame); - if (notify) { - _notifier.notify(deletedFrame!); - } - } - - added_loop: - for (var key in canceled.added) { - var isOverride = canceled.deleted.containsKey(key); - for (var t in transactions) { - if (t.deleted.containsKey(key)) { - if (!isOverride) { - t.deleted.remove(key); - } - continue added_loop; - } - if (t.added.contains(key)) { - continue added_loop; - } - } - if (!isOverride) { - _store.delete(key); - if (notify) { - _notifier.notify(Frame.deleted(key)); - } - } - } - } - - /// Not part of public API - int clear({bool notify = true}) { - var frameList = frames.toList(); - - _store.clear(); - - for (var frame in frameList) { - if (frame.value is HiveObjectMixin) { - (frame.value as HiveObjectMixin).dispose(); - } - if (notify) { - _notifier.notify(Frame.deleted(frame.key)); - } - } - - _deletedEntries = 0; - _autoIncrement = -1; - return frameList.length; - } - - /// Not part of public API - Future close() { - return _notifier.close(); - } -} diff --git a/hive/lib/src/box/lazy_box.dart b/hive/lib/src/box/lazy_box.dart deleted file mode 100644 index d08176d6b..000000000 --- a/hive/lib/src/box/lazy_box.dart +++ /dev/null @@ -1,15 +0,0 @@ -part of hive; - -/// [LazyBox]es don't keep the values in memory like normal boxes. Each time a -/// value is read, it is loaded from the backend. -abstract class LazyBox extends BoxBase { - /// Returns the value associated with the given [key]. If the key does not - /// exist, `null` is returned. - /// - /// If [defaultValue] is specified, it is returned in case the key does not - /// exist. - Future get(dynamic key, {E? defaultValue}); - - /// Returns the value associated with the n-th key. - Future getAt(int index); -} diff --git a/hive/lib/src/box/lazy_box_impl.dart b/hive/lib/src/box/lazy_box_impl.dart deleted file mode 100644 index 47ff569fa..000000000 --- a/hive/lib/src/box/lazy_box_impl.dart +++ /dev/null @@ -1,113 +0,0 @@ -// ignore_for_file: invalid_use_of_protected_member -// ignore_for_file: invalid_use_of_visible_for_testing_member - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/box_base_impl.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:hive/src/object/hive_object.dart'; - -/// Not part of public API -class LazyBoxImpl extends BoxBaseImpl implements LazyBox { - /// Not part of public API - LazyBoxImpl( - HiveImpl hive, - String name, - KeyComparator? keyComparator, - CompactionStrategy compactionStrategy, - StorageBackend backend, - ) : super(hive, name, keyComparator, compactionStrategy, backend); - - @override - final bool lazy = true; - - @override - Future get(dynamic key, {E? defaultValue}) async { - checkOpen(); - - var frame = keystore.get(key); - - if (frame != null) { - var value = await backend.readValue(frame); - if (value is HiveObjectMixin) { - value.init(key, this); - } - return value as E?; - } else { - if (defaultValue != null && defaultValue is HiveObjectMixin) { - defaultValue.init(key, this); - } - return defaultValue; - } - } - - @override - Future getAt(int index) { - return get(keystore.keyAt(index)); - } - - @override - Future putAll( - Map kvPairs, { - bool notify = true, - }) async { - checkOpen(); - - var frames = []; - for (var key in kvPairs.keys) { - frames.add(Frame(key, kvPairs[key])); - if (key is int) { - keystore.updateAutoIncrement(key); - } - } - - if (frames.isEmpty) return; - await backend.writeFrames(frames); - - for (var frame in frames) { - if (frame.value is HiveObjectMixin) { - (frame.value as HiveObjectMixin).init(frame.key, this); - } - keystore.insert( - frame, - lazy: true, - notify: notify, - ); - } - - await performCompactionIfNeeded(); - } - - @override - Future deleteAll( - Iterable keys, { - bool notify = true, - }) async { - checkOpen(); - - var frames = []; - for (var key in keys) { - if (keystore.containsKey(key)) { - frames.add(Frame.deleted(key)); - } - } - - if (frames.isEmpty) return; - await backend.writeFrames(frames); - - for (var frame in frames) { - keystore.insert( - frame, - notify: notify, - ); - } - - await performCompactionIfNeeded(); - } - - @override - Future flush() async { - await backend.flush(); - } -} diff --git a/hive/lib/src/box_collection/box_collection.dart b/hive/lib/src/box_collection/box_collection.dart deleted file mode 100644 index afc8e8a75..000000000 --- a/hive/lib/src/box_collection/box_collection.dart +++ /dev/null @@ -1,296 +0,0 @@ -import 'dart:async'; -import 'dart:convert'; - -import 'package:crypto/crypto.dart'; -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/hive_impl.dart'; - -import 'box_collection_stub.dart' as implementation; - -class BoxCollection implements implementation.BoxCollection { - @override - final String name; - - @override - Set get boxNames => Set.from(_backends.keys); - - final Map _backends; - - HiveCipher? _cipher; - - BoxCollection(this.name, this._backends); - - late CollectionBox _badKeyBox; - - static Future open( - String name, - Set boxNames, { - String? path, - @Deprecated('Use [cipher] instead') HiveCipher? key, - HiveCipher? cipher, - bool useLocks = true, - }) async { - if (name.contains('/') || name.trim() != name || name.isEmpty) { - throw HiveError('Invalid collection name "$name"'); - } - // compatibility for [key] - cipher ??= key; - - final hive = Hive as HiveImpl; - - if (!hive.wasInitialized && path != null) { - throw HiveError( - 'You need to initialize Hive or ' - 'provide a path to store the box.', - ); - } - final names = boxNames..add('bad_keys'); - final backends = await hive.manager - .openCollection(names, path ?? hive.homePath, false, cipher, name); - - final collection = BoxCollection(name, backends); - if (cipher != null) { - collection._cipher = cipher; - } - - collection._badKeyBox = await collection.openBox('bad_keys'); - - return collection; - } - - @override - Future> openBox(String name, - {bool preload = false, - implementation.CollectionBox Function(String, BoxCollection)? - boxCreator}) async { - if (!boxNames.contains(name) && name != 'bad_keys') { - throw Exception('Box with name $name is not in the known' - 'box names of this collection.'); - } - final i = _openBoxes.indexWhere((box) => box.name == name); - if (i != -1) { - return _openBoxes[i] as CollectionBox; - } - final boxIdentifier = name; - final box = boxCreator?.call(boxIdentifier, this) as CollectionBox? ?? - CollectionBox(boxIdentifier, this); - if (preload) { - final hive = Hive as HiveImpl; - box._cachedBox = hive.isBoxOpen(box.name, this.name) - ? hive.lazyBox(box.name, this.name) - : await hive.openBox( - box.name, - encryptionCipher: _cipher, - collection: this.name, - backend: _backends[name]!, - ); - } - _openBoxes.add(box); - return box; - } - - final List _openBoxes = []; - - @override - Future transaction( - Future Function() action, { - List? boxNames, - bool readOnly = false, - }) async { - await runZoned(() async { - try { - CollectionBox.transactionBoxes[Zone.current] = {}; - await action(); - } finally { - final flushFutures = >[]; - for (final boxName in CollectionBox.transactionBoxes[Zone.current]!) { - final i = _openBoxes.indexWhere((box) => box.name == boxName); - if (i != -1) { - flushFutures.add(_openBoxes[i].flush()); - } - } - await Future.wait(flushFutures); - CollectionBox.transactionBoxes.remove(Zone.current); - } - }); - } - - @override - void close() { - for (final box in _openBoxes) { - box._cachedBox?.close(); - } - } - - @override - Future deleteFromDisk() => Future.wait( - boxNames.map((box) => Hive.deleteBoxFromDisk(box, collection: name)), - ); -} - -/// represents a [Box] being part of a [BoxCollection] -class CollectionBox implements implementation.CollectionBox { - @override - final String name; - @override - final BoxCollection boxCollection; - - static final transactionBoxes = >{}; - - BoxBase? _cachedBox; - - Future _getBox() async { - if (_cachedBox == null || !_cachedBox!.isOpen) { - final hive = Hive as HiveImpl; - _cachedBox = hive.isBoxOpen(name, boxCollection.name) - ? hive.lazyBox(name, boxCollection.name) - : await hive.openLazyBox( - name, - encryptionCipher: boxCollection._cipher, - collection: boxCollection.name, - backend: boxCollection._backends[name]!, - ); - } - - return _cachedBox!; - } - - CollectionBox(this.name, this.boxCollection); - - @override - Future> getAllKeys() async { - final box = await _getBox(); - - return (await Future.wait( - box.keys.whereType().map( - (key) async { - if (key.startsWith(_badKeyPrefix)) { - key = await boxCollection._badKeyBox.get(key) ?? key; - } - return key; - }, - ), - )) - .map(Uri.decodeComponent) - .toList(); - } - - @override - Future> getAllValues() async { - final box = await _getBox(); - final keys = box.keys.toList(); - if (box is LazyBox) { - final values = await Future.wait(keys.map(box.get)); - return { - for (var i = 0; i < values.length; i++) - Uri.decodeComponent(keys[i] as String): values[i] as V - }; - } - return (box as Box) - .toMap() - .map((k, v) => MapEntry(Uri.decodeComponent(k.toString()), v as V)); - } - - @override - Future get(String key) async { - key = _toHiveKey(key); - final box = await _getBox(); - if (box is LazyBox) return await box.get(key) as V?; - return (box as Box).get(key) as V?; - } - - @override - Future> getAll( - List keys, - ) async { - final box = await _getBox(); - final values = []; - for (var key in keys) { - key = _toHiveKey(key); - if (box is LazyBox) { - values.add(await box.get(key) as V?); - } else { - values.add((box as Box).get(key) as V?); - } - } - return values; - } - - @override - Future put(String key, V val, [Object? transaction]) async { - if (val == null) { - return delete(key); - } - final box = await _getBox(); - await box.put(_toHiveKey(key), val); - await _flushOrMark(); - } - - @override - Future delete(String key) async { - final box = await _getBox(); - await box.delete(_toHiveKey(key)); - await _flushOrMark(); - } - - @override - Future deleteAll(List keys) async { - final hiveKeys = keys.map(_toHiveKey); - final box = await _getBox(); - await box.deleteAll(hiveKeys); - await _flushOrMark(); - } - - @override - Future clear() async { - final box = await _getBox(); - await box.deleteAll(box.keys); - await _flushOrMark(); - } - - @override - Future flush() async { - // we do *not* await the flushing here. That makes it so that we can execute - // other stuff while the flushing is still in progress. Fortunately, hive - // has a proper read / write queue, meaning that if we do actually want to - // write something again, it'll wait until the flush is completed. - _getBox().then((box) => box.flush()); - } - - Future _flushOrMark() async { - final zone = _getTransactionZone(); - if (zone == null) { - await flush(); - } else { - transactionBoxes[zone]!.add(name); - } - } - - Zone? _getTransactionZone([Zone? testZone]) { - testZone ??= Zone.current; - if (testZone == Zone.root) { - return null; - } - if (transactionBoxes.keys.contains(testZone)) { - return testZone; - } - return _getTransactionZone(testZone.parent); - } - - static const int _maxKeyLength = 255; - static const String _badKeyPrefix = '_bad_key_'; - - String _calcHashKey(String encodedKey) => - _badKeyPrefix + sha256.convert(utf8.encode(encodedKey)).toString(); - - String _toHiveKey(String key) { - final encodedKey = key.split('|').map(Uri.encodeComponent).join('|'); - if (encodedKey.length >= _maxKeyLength) { - final hashKey = _calcHashKey(encodedKey); - boxCollection._badKeyBox.put(hashKey, encodedKey); - return hashKey; - } - return encodedKey; - } -} diff --git a/hive/lib/src/box_collection/box_collection_stub.dart b/hive/lib/src/box_collection/box_collection_stub.dart deleted file mode 100644 index e6ee2303a..000000000 --- a/hive/lib/src/box_collection/box_collection_stub.dart +++ /dev/null @@ -1,57 +0,0 @@ -import 'dart:async'; -import 'package:hive/hive.dart'; - -abstract class BoxCollection { - String get name; - Set get boxNames; - - static Future open( - String name, - Set boxNames, { - String? path, - HiveCipher? key, - bool useLocks = true, - }) { - throw UnimplementedError(); - } - - Future> openBox(String name, - {bool preload = false, - CollectionBox Function(String, BoxCollection)? boxCreator}); - - Future transaction( - Future Function() action, { - List? boxNames, - bool readOnly = false, - }); - - void close(); - - Future deleteFromDisk(); -} - -/// represents a [Box] being part of a [BoxCollection] -abstract class CollectionBox { - String get name; - BoxCollection get boxCollection; - - Future> getAllKeys(); - - Future> getAllValues(); - - Future get(String key); - - Future> getAll( - List keys, - ); - - Future put(String key, V val, [Object? transaction]); - - Future delete(String key); - - Future deleteAll(List keys); - - Future clear(); - - Future flush(); -} diff --git a/hive/lib/src/crypto/aes_cbc_pkcs7.dart b/hive/lib/src/crypto/aes_cbc_pkcs7.dart deleted file mode 100644 index c200e2909..000000000 --- a/hive/lib/src/crypto/aes_cbc_pkcs7.dart +++ /dev/null @@ -1,87 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/crypto/aes_engine.dart'; - -/// AES CBC implementation with PKCS7 padding -class AesCbcPkcs7 { - static final _lastInputBlockBuffer = Uint8List(16); - - final Uint8List _keyBytes; - - late final List _encryptionKey = - AesEngine.generateWorkingKey(_keyBytes, true); - late final List _decryptionKey = - AesEngine.generateWorkingKey(_keyBytes, false); - - /// Not part of public API - AesCbcPkcs7(this._keyBytes); - - /// Not part of public API - int encrypt(Uint8List iv, Uint8List inp, int inpOff, int inpLength, - Uint8List out, int outOff) { - var cbcV = Uint8List.fromList(iv); - - var inputBlocks = (inpLength + aesBlockSize) ~/ aesBlockSize; - var remaining = inpLength % aesBlockSize; - - var offset = 0; - for (var i = 0; i < inputBlocks - 1; i++) { - // XOR the cbcV and the input, then encrypt the cbcV - for (var i = 0; i < aesBlockSize; i++) { - cbcV[i] ^= inp[inpOff + offset + i]; - } - - AesEngine.encryptBlock(_encryptionKey, cbcV, 0, out, outOff + offset); - - // copy ciphertext to cbcV - cbcV.setRange(0, aesBlockSize, out, outOff + offset); - offset += aesBlockSize; - } - - var lastInputBlock = _lastInputBlockBuffer; - lastInputBlock.setRange(0, remaining, inp, inpOff + offset); - lastInputBlock.fillRange(remaining, aesBlockSize, aesBlockSize - remaining); - - for (var i = 0; i < aesBlockSize; i++) { - cbcV[i] ^= lastInputBlock[i]; - } - AesEngine.encryptBlock(_encryptionKey, cbcV, 0, out, outOff + offset); - - return offset + aesBlockSize; - } - - /// Not part of public API - int decrypt(Uint8List iv, Uint8List inp, int inpOff, int inpLength, - Uint8List out, int outOff) { - var inputBlocks = (inpLength + aesBlockSize - 1) ~/ aesBlockSize; - - var offset = 0; - - AesEngine.decryptBlock(_decryptionKey, inp, inpOff, out, outOff); - for (var i = 0; i < aesBlockSize; i++) { - out[outOff + i] ^= iv[i]; - } - offset += aesBlockSize; - - for (var i = 0; i < inputBlocks - 1; i++) { - AesEngine.decryptBlock( - _decryptionKey, inp, inpOff + offset, out, outOff + offset); - for (var i = 0; i < aesBlockSize; i++) { - out[outOff + offset + i] ^= inp[inpOff - aesBlockSize + offset + i]; - } - offset += aesBlockSize; - } - - var lastDecryptedByte = out[outOff + offset - 1]; - if (lastDecryptedByte > aesBlockSize) { - throw ArgumentError('Invalid or corrupted pad block'); - } - for (var i = 0; i < lastDecryptedByte; i++) { - if (out[outOff + offset - i - 1] != lastDecryptedByte) { - throw ArgumentError('Invalid or corrupted pad block'); - } - } - - return offset - lastDecryptedByte; - } -} diff --git a/hive/lib/src/crypto/aes_engine.dart b/hive/lib/src/crypto/aes_engine.dart deleted file mode 100644 index 371a5380a..000000000 --- a/hive/lib/src/crypto/aes_engine.dart +++ /dev/null @@ -1,324 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/crypto/aes_tables.dart'; -import 'package:hive/src/util/extensions.dart'; - -/// The block size of an AES block -const aesBlockSize = 16; - -/// The number of encryption rounds -const rounds = 14; - -const _m1 = 0x80808080; -const _m2 = 0x7f7f7f7f; -const _m3 = 0x0000001b; - -const _mask8 = 0xff; -const _mask16 = 0xffff; -const _mask32 = 0xffffffff; - -@pragma('vm:prefer-inline') -@pragma('dart2js:tryInline') -int _subWord(int x) { - return sBox[x & 255] | - (sBox[(x >> 8) & 255] << 8) | - (sBox[(x >> 16) & 255] << 16) | - sBox[(x >> 24) & 255] << 24; -} - -@pragma('vm:prefer-inline') -@pragma('dart2js:tryInline') -int _invMcol(int x) { - var f2 = ((x & _m2) << 1) ^ (((x & _m1) >> 7) * _m3); - var f4 = ((f2 & _m2) << 1) ^ (((f2 & _m1) >> 7) * _m3); - var f8 = ((f4 & _m2) << 1) ^ (((f4 & _m1) >> 7) * _m3); - var f9 = x ^ f8; - - var s1 = ((f2 ^ f9) >> 8) | ((((f2 ^ f9) & _mask8) << 24) & _mask32); - var s2 = ((f4 ^ f9) >> 16) | ((((f4 ^ f9) & _mask16) << 16) & _mask32); - var s3 = (f9 >> 24) | (((f9 & _mask32) << 8) & _mask32); - - return f2 ^ f4 ^ f8 ^ s1 ^ s2 ^ s3; -} - -/// AES implementation (some of the code is from Bouncycastle) -class AesEngine { - /// Expand an encryption or decryption key. - static List generateWorkingKey( - List key, bool forEncryption) { - var w = List.generate(rounds + 1, (_) => Uint32List(4)); - var t0 = key.readUint32(0); - var t1 = key.readUint32(4); - var t2 = key.readUint32(8); - var t3 = key.readUint32(12); - var t4 = key.readUint32(16); - var t5 = key.readUint32(20); - var t6 = key.readUint32(24); - var t7 = key.readUint32(28); - - w[0][0] = t0; - w[0][1] = t1; - w[0][2] = t2; - w[0][3] = t3; - - w[1][0] = t4; - w[1][1] = t5; - w[1][2] = t6; - w[1][3] = t7; - - int u, rcon = 1; - - for (var i = 2; i < 14; i += 2) { - u = _subWord((t7 >> 8) | (((t7 & _mask8) << 24) & _mask32)) ^ rcon; - rcon <<= 1; - t0 ^= u; - w[i][0] = t0; - t1 ^= t0; - w[i][1] = t1; - t2 ^= t1; - w[i][2] = t2; - t3 ^= t2; - w[i][3] = t3; - u = _subWord(t3); - t4 ^= u; - w[i + 1][0] = t4; - t5 ^= t4; - w[i + 1][1] = t5; - t6 ^= t5; - w[i + 1][2] = t6; - t7 ^= t6; - w[i + 1][3] = t7; - } - - u = _subWord((t7 >> 8) | (((t7 & _mask8) << 24) & _mask32)) ^ rcon; - t0 ^= u; - w[14][0] = t0; - t1 ^= t0; - w[14][1] = t1; - t2 ^= t1; - w[14][2] = t2; - t3 ^= t2; - w[14][3] = t3; - - if (!forEncryption) { - for (var j = 1; j < rounds; j++) { - for (var i = 0; i < 4; i++) { - w[j][i] = _invMcol(w[j][i]); - } - } - } - - return w; - } - - /// Encrypt a single block with the [workingKey]. - static void encryptBlock(List> workingKey, Uint8List inp, - int inpOff, Uint8List out, int outOff) { - var c0 = inp.readUint32(inpOff) ^ workingKey[0][0]; - var c1 = inp.readUint32(inpOff + 4) ^ workingKey[0][1]; - var c2 = inp.readUint32(inpOff + 8) ^ workingKey[0][2]; - var c3 = inp.readUint32(inpOff + 12) ^ workingKey[0][3]; - - int r0, r1, r2, r3; - var r = 1; - while (r < rounds - 1) { - r0 = table0[c0 & 255] ^ - table1[(c1 >> 8) & 255] ^ - table2[(c2 >> 16) & 255] ^ - table3[(c3 >> 24) & 255] ^ - workingKey[r][0]; - r1 = table0[c1 & 255] ^ - table1[(c2 >> 8) & 255] ^ - table2[(c3 >> 16) & 255] ^ - table3[(c0 >> 24) & 255] ^ - workingKey[r][1]; - r2 = table0[c2 & 255] ^ - table1[(c3 >> 8) & 255] ^ - table2[(c0 >> 16) & 255] ^ - table3[(c1 >> 24) & 255] ^ - workingKey[r][2]; - r3 = table0[c3 & 255] ^ - table1[(c0 >> 8) & 255] ^ - table2[(c1 >> 16) & 255] ^ - table3[(c2 >> 24) & 255] ^ - workingKey[r][3]; - r++; - c0 = table0[r0 & 255] ^ - table1[(r1 >> 8) & 255] ^ - table2[(r2 >> 16) & 255] ^ - table3[(r3 >> 24) & 255] ^ - workingKey[r][0]; - c1 = table0[r1 & 255] ^ - table1[(r2 >> 8) & 255] ^ - table2[(r3 >> 16) & 255] ^ - table3[(r0 >> 24) & 255] ^ - workingKey[r][1]; - c2 = table0[r2 & 255] ^ - table1[(r3 >> 8) & 255] ^ - table2[(r0 >> 16) & 255] ^ - table3[(r1 >> 24) & 255] ^ - workingKey[r][2]; - c3 = table0[r3 & 255] ^ - table1[(r0 >> 8) & 255] ^ - table2[(r1 >> 16) & 255] ^ - table3[(r2 >> 24) & 255] ^ - workingKey[r][3]; - r++; - } - - r0 = table0[c0 & 255] ^ - table1[(c1 >> 8) & 255] ^ - table2[(c2 >> 16) & 255] ^ - table3[(c3 >> 24) & 255] ^ - workingKey[r][0]; - r1 = table0[c1 & 255] ^ - table1[(c2 >> 8) & 255] ^ - table2[(c3 >> 16) & 255] ^ - table3[(c0 >> 24) & 255] ^ - workingKey[r][1]; - r2 = table0[c2 & 255] ^ - table1[(c3 >> 8) & 255] ^ - table2[(c0 >> 16) & 255] ^ - table3[(c1 >> 24) & 255] ^ - workingKey[r][2]; - r3 = table0[c3 & 255] ^ - table1[(c0 >> 8) & 255] ^ - table2[(c1 >> 16) & 255] ^ - table3[(c2 >> 24) & 255] ^ - workingKey[r][3]; - r++; - - // the final round's table is a simple function of S so we don't use a - // whole other four tables for it - c0 = (sBox[r0 & 255] & 255) ^ - (sBox[(r1 >> 8) & 255] << 8) ^ - (sBox[(r2 >> 16) & 255] << 16) ^ - (sBox[(r3 >> 24) & 255] << 24) ^ - workingKey[r][0]; - c1 = (sBox[r1 & 255] & 255) ^ - (sBox[(r2 >> 8) & 255] << 8) ^ - (sBox[(r3 >> 16) & 255] << 16) ^ - (sBox[(r0 >> 24) & 255] << 24) ^ - workingKey[r][1]; - c2 = (sBox[r2 & 255] & 255) ^ - (sBox[(r3 >> 8) & 255] << 8) ^ - (sBox[(r0 >> 16) & 255] << 16) ^ - (sBox[(r1 >> 24) & 255] << 24) ^ - workingKey[r][2]; - c3 = (sBox[r3 & 255] & 255) ^ - (sBox[(r0 >> 8) & 255] << 8) ^ - (sBox[(r1 >> 16) & 255] << 16) ^ - (sBox[(r2 >> 24) & 255] << 24) ^ - workingKey[r][3]; - - out.writeUint32(outOff, c0); - out.writeUint32(outOff + 4, c1); - out.writeUint32(outOff + 8, c2); - out.writeUint32(outOff + 12, c3); - } - - /// Decrypt a single block with [workingKey]. - static void decryptBlock(List> workingKey, Uint8List inp, - int inpOff, Uint8List out, int outOff) { - var c0 = inp.readUint32(inpOff) ^ workingKey[rounds][0]; - var c1 = inp.readUint32(inpOff + 4) ^ workingKey[rounds][1]; - var c2 = inp.readUint32(inpOff + 8) ^ workingKey[rounds][2]; - var c3 = inp.readUint32(inpOff + 12) ^ workingKey[rounds][3]; - - int r0, r1, r2, r3; - var r = rounds - 1; - while (r > 1) { - r0 = table0Inv[c0 & 255] ^ - table1Inv[(c3 >> 8) & 255] ^ - table2Inv[(c2 >> 16) & 255] ^ - table3Inv[(c1 >> 24) & 255] ^ - workingKey[r][0]; - r1 = table0Inv[c1 & 255] ^ - table1Inv[(c0 >> 8) & 255] ^ - table2Inv[(c3 >> 16) & 255] ^ - table3Inv[(c2 >> 24) & 255] ^ - workingKey[r][1]; - r2 = table0Inv[c2 & 255] ^ - table1Inv[(c1 >> 8) & 255] ^ - table2Inv[(c0 >> 16) & 255] ^ - table3Inv[(c3 >> 24) & 255] ^ - workingKey[r][2]; - r3 = table0Inv[c3 & 255] ^ - table1Inv[(c2 >> 8) & 255] ^ - table2Inv[(c1 >> 16) & 255] ^ - table3Inv[(c0 >> 24) & 255] ^ - workingKey[r][3]; - r--; - c0 = table0Inv[r0 & 255] ^ - table1Inv[(r3 >> 8) & 255] ^ - table2Inv[(r2 >> 16) & 255] ^ - table3Inv[(r1 >> 24) & 255] ^ - workingKey[r][0]; - c1 = table0Inv[r1 & 255] ^ - table1Inv[(r0 >> 8) & 255] ^ - table2Inv[(r3 >> 16) & 255] ^ - table3Inv[(r2 >> 24) & 255] ^ - workingKey[r][1]; - c2 = table0Inv[r2 & 255] ^ - table1Inv[(r1 >> 8) & 255] ^ - table2Inv[(r0 >> 16) & 255] ^ - table3Inv[(r3 >> 24) & 255] ^ - workingKey[r][2]; - c3 = table0Inv[r3 & 255] ^ - table1Inv[(r2 >> 8) & 255] ^ - table2Inv[(r1 >> 16) & 255] ^ - table3Inv[(r0 >> 24) & 255] ^ - workingKey[r][3]; - r--; - } - - r0 = table0Inv[c0 & 255] ^ - table1Inv[(c3 >> 8) & 255] ^ - table2Inv[(c2 >> 16) & 255] ^ - table3Inv[(c1 >> 24) & 255] ^ - workingKey[r][0]; - r1 = table0Inv[c1 & 255] ^ - table1Inv[(c0 >> 8) & 255] ^ - table2Inv[(c3 >> 16) & 255] ^ - table3Inv[(c2 >> 24) & 255] ^ - workingKey[r][1]; - r2 = table0Inv[c2 & 255] ^ - table1Inv[(c1 >> 8) & 255] ^ - table2Inv[(c0 >> 16) & 255] ^ - table3Inv[(c3 >> 24) & 255] ^ - workingKey[r][2]; - r3 = table0Inv[c3 & 255] ^ - table1Inv[(c2 >> 8) & 255] ^ - table2Inv[(c1 >> 16) & 255] ^ - table3Inv[(c0 >> 24) & 255] ^ - workingKey[r][3]; - - // the final round's table is a simple function of Si so we don't use a - // whole other four tables for it - c0 = sBoxInv[r0 & 255] ^ - (sBoxInv[(r3 >> 8) & 255] << 8) ^ - (sBoxInv[(r2 >> 16) & 255] << 16) ^ - (sBoxInv[(r1 >> 24) & 255] << 24) ^ - workingKey[0][0]; - c1 = (sBoxInv[r1 & 255] & 255) ^ - (sBoxInv[(r0 >> 8) & 255] << 8) ^ - (sBoxInv[(r3 >> 16) & 255] << 16) ^ - (sBoxInv[(r2 >> 24) & 255] << 24) ^ - workingKey[0][1]; - c2 = (sBoxInv[r2 & 255] & 255) ^ - (sBoxInv[(r1 >> 8) & 255] << 8) ^ - (sBoxInv[(r0 >> 16) & 255] << 16) ^ - (sBoxInv[(r3 >> 24) & 255] << 24) ^ - workingKey[0][2]; - c3 = (sBoxInv[r3 & 255] & 255) ^ - (sBoxInv[(r2 >> 8) & 255] << 8) ^ - (sBoxInv[(r1 >> 16) & 255] << 16) ^ - (sBoxInv[(r0 >> 24) & 255] << 24) ^ - workingKey[0][3]; - - out.writeUint32(outOff, c0); - out.writeUint32(outOff + 4, c1); - out.writeUint32(outOff + 8, c2); - out.writeUint32(outOff + 12, c3); - } -} diff --git a/hive/lib/src/crypto/aes_tables.dart b/hive/lib/src/crypto/aes_tables.dart deleted file mode 100644 index 57ec588df..000000000 --- a/hive/lib/src/crypto/aes_tables.dart +++ /dev/null @@ -1,537 +0,0 @@ -/// The S box -const sBox = [ - // - 99, 124, 119, 123, 242, 107, 111, 197, - 48, 1, 103, 43, 254, 215, 171, 118, - 202, 130, 201, 125, 250, 89, 71, 240, - 173, 212, 162, 175, 156, 164, 114, 192, - 183, 253, 147, 38, 54, 63, 247, 204, - 52, 165, 229, 241, 113, 216, 49, 21, - 4, 199, 35, 195, 24, 150, 5, 154, - 7, 18, 128, 226, 235, 39, 178, 117, - 9, 131, 44, 26, 27, 110, 90, 160, - 82, 59, 214, 179, 41, 227, 47, 132, - 83, 209, 0, 237, 32, 252, 177, 91, - 106, 203, 190, 57, 74, 76, 88, 207, - 208, 239, 170, 251, 67, 77, 51, 133, - 69, 249, 2, 127, 80, 60, 159, 168, - 81, 163, 64, 143, 146, 157, 56, 245, - 188, 182, 218, 33, 16, 255, 243, 210, - 205, 12, 19, 236, 95, 151, 68, 23, - 196, 167, 126, 61, 100, 93, 25, 115, - 96, 129, 79, 220, 34, 42, 144, 136, - 70, 238, 184, 20, 222, 94, 11, 219, - 224, 50, 58, 10, 73, 6, 36, 92, - 194, 211, 172, 98, 145, 149, 228, 121, - 231, 200, 55, 109, 141, 213, 78, 169, - 108, 86, 244, 234, 101, 122, 174, 8, - 186, 120, 37, 46, 28, 166, 180, 198, - 232, 221, 116, 31, 75, 189, 139, 138, - 112, 62, 181, 102, 72, 3, 246, 14, - 97, 53, 87, 185, 134, 193, 29, 158, - 225, 248, 152, 17, 105, 217, 142, 148, - 155, 30, 135, 233, 206, 85, 40, 223, - 140, 161, 137, 13, 191, 230, 66, 104, - 65, 153, 45, 15, 176, 84, 187, 22, -]; - -/// The inverse S-box -const sBoxInv = [ - // - 82, 9, 106, 213, 48, 54, 165, 56, - 191, 64, 163, 158, 129, 243, 215, 251, - 124, 227, 57, 130, 155, 47, 255, 135, - 52, 142, 67, 68, 196, 222, 233, 203, - 84, 123, 148, 50, 166, 194, 35, 61, - 238, 76, 149, 11, 66, 250, 195, 78, - 8, 46, 161, 102, 40, 217, 36, 178, - 118, 91, 162, 73, 109, 139, 209, 37, - 114, 248, 246, 100, 134, 104, 152, 22, - 212, 164, 92, 204, 93, 101, 182, 146, - 108, 112, 72, 80, 253, 237, 185, 218, - 94, 21, 70, 87, 167, 141, 157, 132, - 144, 216, 171, 0, 140, 188, 211, 10, - 247, 228, 88, 5, 184, 179, 69, 6, - 208, 44, 30, 143, 202, 63, 15, 2, - 193, 175, 189, 3, 1, 19, 138, 107, - 58, 145, 17, 65, 79, 103, 220, 234, - 151, 242, 207, 206, 240, 180, 230, 115, - 150, 172, 116, 34, 231, 173, 53, 133, - 226, 249, 55, 232, 28, 117, 223, 110, - 71, 241, 26, 113, 29, 41, 197, 137, - 111, 183, 98, 14, 170, 24, 190, 27, - 252, 86, 62, 75, 198, 210, 121, 32, - 154, 219, 192, 254, 120, 205, 90, 244, - 31, 221, 168, 51, 136, 7, 199, 49, - 177, 18, 16, 89, 39, 128, 236, 95, - 96, 81, 127, 169, 25, 181, 74, 13, - 45, 229, 122, 159, 147, 201, 156, 239, - 160, 224, 59, 77, 174, 42, 245, 176, - 200, 235, 187, 60, 131, 83, 153, 97, - 23, 43, 4, 126, 186, 119, 214, 38, - 225, 105, 20, 99, 85, 33, 12, 125, -]; - -/// -const rcon = [ - // - 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, - 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, - 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91, -]; - -/// precomputation tables of calculations for rounds -const table0 = [ - // - 0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff, - 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591, 0x50303060, 0x03010102, - 0xa96767ce, 0x7d2b2b56, 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, - 0x9a7676ec, 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa, - 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb, 0xecadad41, - 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453, - 0x967272e4, 0x5bc0c09b, 0xc2b7b775, 0x1cfdfde1, 0xae93933d, - 0x6a26264c, 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83, - 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9, 0x937171e2, - 0x73d8d8ab, 0x53313162, 0x3f15152a, 0x0c040408, 0x52c7c795, - 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, - 0xb59a9a2f, 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, - 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, 0x1b090912, - 0x9e83831d, 0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc, - 0xee5a5ab4, 0xfba0a05b, 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, - 0xceb3b37d, 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413, - 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1, 0x60202040, - 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6, 0xbe6a6ad4, 0x46cbcb8d, - 0xd9bebe67, 0x4b393972, 0xde4a4a94, 0xd44c4c98, 0xe85858b0, - 0x4acfcf85, 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed, - 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511, 0xcf45458a, - 0x10f9f9e9, 0x06020204, 0x817f7ffe, 0xf05050a0, 0x443c3c78, - 0xba9f9f25, 0xe3a8a84b, 0xf35151a2, 0xfea3a35d, 0xc0404080, - 0x8a8f8f05, 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1, - 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142, 0x30101020, - 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf, 0x4ccdcd81, 0x140c0c18, - 0x35131326, 0x2fececc3, 0xe15f5fbe, 0xa2979735, 0xcc444488, - 0x3917172e, 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a, - 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6, 0xa06060c0, - 0x98818119, 0xd14f4f9e, 0x7fdcdca3, 0x66222244, 0x7e2a2a54, - 0xab90903b, 0x8388880b, 0xca46468c, 0x29eeeec7, 0xd3b8b86b, - 0x3c141428, 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad, - 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14, 0xdb494992, - 0x0a06060c, 0x6c242448, 0xe45c5cb8, 0x5dc2c29f, 0x6ed3d3bd, - 0xefacac43, 0xa66262c4, 0xa8919139, 0xa4959531, 0x37e4e4d3, - 0x8b7979f2, 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda, - 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949, 0xb46c6cd8, - 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf, 0xaf6565ca, 0x8e7a7af4, - 0xe9aeae47, 0x18080810, 0xd5baba6f, 0x887878f0, 0x6f25254a, - 0x722e2e5c, 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697, - 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e, 0xdd4b4b96, - 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f, 0x907070e0, 0x423e3e7c, - 0xc4b5b571, 0xaa6666cc, 0xd8484890, 0x05030306, 0x01f6f6f7, - 0x120e0e1c, 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969, - 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27, 0x38e1e1d9, - 0x13f8f8eb, 0xb398982b, 0x33111122, 0xbb6969d2, 0x70d9d9a9, - 0x898e8e07, 0xa7949433, 0xb69b9b2d, 0x221e1e3c, 0x92878715, - 0x20e9e9c9, 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5, - 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a, 0xdabfbf65, - 0x31e6e6d7, 0xc6424284, 0xb86868d0, 0xc3414182, 0xb0999929, - 0x772d2d5a, 0x110f0f1e, 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, - 0x3a16162c, -]; - -/// -const table1 = [ - // - 0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d, 0xf2f2ff0d, - 0x6b6bd6bd, 0x6f6fdeb1, 0xc5c59154, 0x30306050, 0x01010203, - 0x6767cea9, 0x2b2b567d, 0xfefee719, 0xd7d7b562, 0xabab4de6, - 0x7676ec9a, 0xcaca8f45, 0x82821f9d, 0xc9c98940, 0x7d7dfa87, - 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b, 0xadad41ec, - 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea, 0x9c9c23bf, 0xa4a453f7, - 0x7272e496, 0xc0c09b5b, 0xb7b775c2, 0xfdfde11c, 0x93933dae, - 0x26264c6a, 0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f, - 0x3434685c, 0xa5a551f4, 0xe5e5d134, 0xf1f1f908, 0x7171e293, - 0xd8d8ab73, 0x31316253, 0x15152a3f, 0x0404080c, 0xc7c79552, - 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1, 0x05050a0f, - 0x9a9a2fb5, 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d, - 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f, 0x0909121b, - 0x83831d9e, 0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, - 0x5a5ab4ee, 0xa0a05bfb, 0x5252a4f6, 0x3b3b764d, 0xd6d6b761, - 0xb3b37dce, 0x2929527b, 0xe3e3dd3e, 0x2f2f5e71, 0x84841397, - 0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c, 0x20204060, - 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed, 0x6a6ad4be, 0xcbcb8d46, - 0xbebe67d9, 0x3939724b, 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, - 0xcfcf854a, 0xd0d0bb6b, 0xefefc52a, 0xaaaa4fe5, 0xfbfbed16, - 0x434386c5, 0x4d4d9ad7, 0x33336655, 0x85851194, 0x45458acf, - 0xf9f9e910, 0x02020406, 0x7f7ffe81, 0x5050a0f0, 0x3c3c7844, - 0x9f9f25ba, 0xa8a84be3, 0x5151a2f3, 0xa3a35dfe, 0x404080c0, - 0x8f8f058a, 0x92923fad, 0x9d9d21bc, 0x38387048, 0xf5f5f104, - 0xbcbc63df, 0xb6b677c1, 0xdadaaf75, 0x21214263, 0x10102030, - 0xffffe51a, 0xf3f3fd0e, 0xd2d2bf6d, 0xcdcd814c, 0x0c0c1814, - 0x13132635, 0xececc32f, 0x5f5fbee1, 0x979735a2, 0x444488cc, - 0x17172e39, 0xc4c49357, 0xa7a755f2, 0x7e7efc82, 0x3d3d7a47, - 0x6464c8ac, 0x5d5dbae7, 0x1919322b, 0x7373e695, 0x6060c0a0, - 0x81811998, 0x4f4f9ed1, 0xdcdca37f, 0x22224466, 0x2a2a547e, - 0x90903bab, 0x88880b83, 0x46468cca, 0xeeeec729, 0xb8b86bd3, - 0x1414283c, 0xdedea779, 0x5e5ebce2, 0x0b0b161d, 0xdbdbad76, - 0xe0e0db3b, 0x32326456, 0x3a3a744e, 0x0a0a141e, 0x494992db, - 0x06060c0a, 0x2424486c, 0x5c5cb8e4, 0xc2c29f5d, 0xd3d3bd6e, - 0xacac43ef, 0x6262c4a6, 0x919139a8, 0x959531a4, 0xe4e4d337, - 0x7979f28b, 0xe7e7d532, 0xc8c88b43, 0x37376e59, 0x6d6ddab7, - 0x8d8d018c, 0xd5d5b164, 0x4e4e9cd2, 0xa9a949e0, 0x6c6cd8b4, - 0x5656acfa, 0xf4f4f307, 0xeaeacf25, 0x6565caaf, 0x7a7af48e, - 0xaeae47e9, 0x08081018, 0xbaba6fd5, 0x7878f088, 0x25254a6f, - 0x2e2e5c72, 0x1c1c3824, 0xa6a657f1, 0xb4b473c7, 0xc6c69751, - 0xe8e8cb23, 0xdddda17c, 0x7474e89c, 0x1f1f3e21, 0x4b4b96dd, - 0xbdbd61dc, 0x8b8b0d86, 0x8a8a0f85, 0x7070e090, 0x3e3e7c42, - 0xb5b571c4, 0x6666ccaa, 0x484890d8, 0x03030605, 0xf6f6f701, - 0x0e0e1c12, 0x6161c2a3, 0x35356a5f, 0x5757aef9, 0xb9b969d0, - 0x86861791, 0xc1c19958, 0x1d1d3a27, 0x9e9e27b9, 0xe1e1d938, - 0xf8f8eb13, 0x98982bb3, 0x11112233, 0x6969d2bb, 0xd9d9a970, - 0x8e8e0789, 0x949433a7, 0x9b9b2db6, 0x1e1e3c22, 0x87871592, - 0xe9e9c920, 0xcece8749, 0x5555aaff, 0x28285078, 0xdfdfa57a, - 0x8c8c038f, 0xa1a159f8, 0x89890980, 0x0d0d1a17, 0xbfbf65da, - 0xe6e6d731, 0x424284c6, 0x6868d0b8, 0x414182c3, 0x999929b0, - 0x2d2d5a77, 0x0f0f1e11, 0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, - 0x16162c3a, -]; - -/// -const table2 = [ - // - 0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b, 0xf2ff0df2, - 0x6bd6bd6b, 0x6fdeb16f, 0xc59154c5, 0x30605030, 0x01020301, - 0x67cea967, 0x2b567d2b, 0xfee719fe, 0xd7b562d7, 0xab4de6ab, - 0x76ec9a76, 0xca8f45ca, 0x821f9d82, 0xc98940c9, 0x7dfa877d, - 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0, 0xad41ecad, - 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf, 0x9c23bf9c, 0xa453f7a4, - 0x72e49672, 0xc09b5bc0, 0xb775c2b7, 0xfde11cfd, 0x933dae93, - 0x264c6a26, 0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc, - 0x34685c34, 0xa551f4a5, 0xe5d134e5, 0xf1f908f1, 0x71e29371, - 0xd8ab73d8, 0x31625331, 0x152a3f15, 0x04080c04, 0xc79552c7, - 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196, 0x050a0f05, - 0x9a2fb59a, 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2, - 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75, 0x09121b09, - 0x831d9e83, 0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, - 0x5ab4ee5a, 0xa05bfba0, 0x52a4f652, 0x3b764d3b, 0xd6b761d6, - 0xb37dceb3, 0x29527b29, 0xe3dd3ee3, 0x2f5e712f, 0x84139784, - 0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced, 0x20406020, - 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b, 0x6ad4be6a, 0xcb8d46cb, - 0xbe67d9be, 0x39724b39, 0x4a94de4a, 0x4c98d44c, 0x58b0e858, - 0xcf854acf, 0xd0bb6bd0, 0xefc52aef, 0xaa4fe5aa, 0xfbed16fb, - 0x4386c543, 0x4d9ad74d, 0x33665533, 0x85119485, 0x458acf45, - 0xf9e910f9, 0x02040602, 0x7ffe817f, 0x50a0f050, 0x3c78443c, - 0x9f25ba9f, 0xa84be3a8, 0x51a2f351, 0xa35dfea3, 0x4080c040, - 0x8f058a8f, 0x923fad92, 0x9d21bc9d, 0x38704838, 0xf5f104f5, - 0xbc63dfbc, 0xb677c1b6, 0xdaaf75da, 0x21426321, 0x10203010, - 0xffe51aff, 0xf3fd0ef3, 0xd2bf6dd2, 0xcd814ccd, 0x0c18140c, - 0x13263513, 0xecc32fec, 0x5fbee15f, 0x9735a297, 0x4488cc44, - 0x172e3917, 0xc49357c4, 0xa755f2a7, 0x7efc827e, 0x3d7a473d, - 0x64c8ac64, 0x5dbae75d, 0x19322b19, 0x73e69573, 0x60c0a060, - 0x81199881, 0x4f9ed14f, 0xdca37fdc, 0x22446622, 0x2a547e2a, - 0x903bab90, 0x880b8388, 0x468cca46, 0xeec729ee, 0xb86bd3b8, - 0x14283c14, 0xdea779de, 0x5ebce25e, 0x0b161d0b, 0xdbad76db, - 0xe0db3be0, 0x32645632, 0x3a744e3a, 0x0a141e0a, 0x4992db49, - 0x060c0a06, 0x24486c24, 0x5cb8e45c, 0xc29f5dc2, 0xd3bd6ed3, - 0xac43efac, 0x62c4a662, 0x9139a891, 0x9531a495, 0xe4d337e4, - 0x79f28b79, 0xe7d532e7, 0xc88b43c8, 0x376e5937, 0x6ddab76d, - 0x8d018c8d, 0xd5b164d5, 0x4e9cd24e, 0xa949e0a9, 0x6cd8b46c, - 0x56acfa56, 0xf4f307f4, 0xeacf25ea, 0x65caaf65, 0x7af48e7a, - 0xae47e9ae, 0x08101808, 0xba6fd5ba, 0x78f08878, 0x254a6f25, - 0x2e5c722e, 0x1c38241c, 0xa657f1a6, 0xb473c7b4, 0xc69751c6, - 0xe8cb23e8, 0xdda17cdd, 0x74e89c74, 0x1f3e211f, 0x4b96dd4b, - 0xbd61dcbd, 0x8b0d868b, 0x8a0f858a, 0x70e09070, 0x3e7c423e, - 0xb571c4b5, 0x66ccaa66, 0x4890d848, 0x03060503, 0xf6f701f6, - 0x0e1c120e, 0x61c2a361, 0x356a5f35, 0x57aef957, 0xb969d0b9, - 0x86179186, 0xc19958c1, 0x1d3a271d, 0x9e27b99e, 0xe1d938e1, - 0xf8eb13f8, 0x982bb398, 0x11223311, 0x69d2bb69, 0xd9a970d9, - 0x8e07898e, 0x9433a794, 0x9b2db69b, 0x1e3c221e, 0x87159287, - 0xe9c920e9, 0xce8749ce, 0x55aaff55, 0x28507828, 0xdfa57adf, - 0x8c038f8c, 0xa159f8a1, 0x89098089, 0x0d1a170d, 0xbf65dabf, - 0xe6d731e6, 0x4284c642, 0x68d0b868, 0x4182c341, 0x9929b099, - 0x2d5a772d, 0x0f1e110f, 0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, - 0x162c3a16, -]; - -/// -const table3 = [ - // - 0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b, 0xff0df2f2, - 0xd6bd6b6b, 0xdeb16f6f, 0x9154c5c5, 0x60503030, 0x02030101, - 0xcea96767, 0x567d2b2b, 0xe719fefe, 0xb562d7d7, 0x4de6abab, - 0xec9a7676, 0x8f45caca, 0x1f9d8282, 0x8940c9c9, 0xfa877d7d, - 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0, 0x41ecadad, - 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf, 0x23bf9c9c, 0x53f7a4a4, - 0xe4967272, 0x9b5bc0c0, 0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, - 0x4c6a2626, 0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc, - 0x685c3434, 0x51f4a5a5, 0xd134e5e5, 0xf908f1f1, 0xe2937171, - 0xab73d8d8, 0x62533131, 0x2a3f1515, 0x080c0404, 0x9552c7c7, - 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696, 0x0a0f0505, - 0x2fb59a9a, 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2, - 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575, 0x121b0909, - 0x1d9e8383, 0x58742c2c, 0x342e1a1a, 0x362d1b1b, 0xdcb26e6e, - 0xb4ee5a5a, 0x5bfba0a0, 0xa4f65252, 0x764d3b3b, 0xb761d6d6, - 0x7dceb3b3, 0x527b2929, 0xdd3ee3e3, 0x5e712f2f, 0x13978484, - 0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded, 0x40602020, - 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b, 0xd4be6a6a, 0x8d46cbcb, - 0x67d9bebe, 0x724b3939, 0x94de4a4a, 0x98d44c4c, 0xb0e85858, - 0x854acfcf, 0xbb6bd0d0, 0xc52aefef, 0x4fe5aaaa, 0xed16fbfb, - 0x86c54343, 0x9ad74d4d, 0x66553333, 0x11948585, 0x8acf4545, - 0xe910f9f9, 0x04060202, 0xfe817f7f, 0xa0f05050, 0x78443c3c, - 0x25ba9f9f, 0x4be3a8a8, 0xa2f35151, 0x5dfea3a3, 0x80c04040, - 0x058a8f8f, 0x3fad9292, 0x21bc9d9d, 0x70483838, 0xf104f5f5, - 0x63dfbcbc, 0x77c1b6b6, 0xaf75dada, 0x42632121, 0x20301010, - 0xe51affff, 0xfd0ef3f3, 0xbf6dd2d2, 0x814ccdcd, 0x18140c0c, - 0x26351313, 0xc32fecec, 0xbee15f5f, 0x35a29797, 0x88cc4444, - 0x2e391717, 0x9357c4c4, 0x55f2a7a7, 0xfc827e7e, 0x7a473d3d, - 0xc8ac6464, 0xbae75d5d, 0x322b1919, 0xe6957373, 0xc0a06060, - 0x19988181, 0x9ed14f4f, 0xa37fdcdc, 0x44662222, 0x547e2a2a, - 0x3bab9090, 0x0b838888, 0x8cca4646, 0xc729eeee, 0x6bd3b8b8, - 0x283c1414, 0xa779dede, 0xbce25e5e, 0x161d0b0b, 0xad76dbdb, - 0xdb3be0e0, 0x64563232, 0x744e3a3a, 0x141e0a0a, 0x92db4949, - 0x0c0a0606, 0x486c2424, 0xb8e45c5c, 0x9f5dc2c2, 0xbd6ed3d3, - 0x43efacac, 0xc4a66262, 0x39a89191, 0x31a49595, 0xd337e4e4, - 0xf28b7979, 0xd532e7e7, 0x8b43c8c8, 0x6e593737, 0xdab76d6d, - 0x018c8d8d, 0xb164d5d5, 0x9cd24e4e, 0x49e0a9a9, 0xd8b46c6c, - 0xacfa5656, 0xf307f4f4, 0xcf25eaea, 0xcaaf6565, 0xf48e7a7a, - 0x47e9aeae, 0x10180808, 0x6fd5baba, 0xf0887878, 0x4a6f2525, - 0x5c722e2e, 0x38241c1c, 0x57f1a6a6, 0x73c7b4b4, 0x9751c6c6, - 0xcb23e8e8, 0xa17cdddd, 0xe89c7474, 0x3e211f1f, 0x96dd4b4b, - 0x61dcbdbd, 0x0d868b8b, 0x0f858a8a, 0xe0907070, 0x7c423e3e, - 0x71c4b5b5, 0xccaa6666, 0x90d84848, 0x06050303, 0xf701f6f6, - 0x1c120e0e, 0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9, - 0x17918686, 0x9958c1c1, 0x3a271d1d, 0x27b99e9e, 0xd938e1e1, - 0xeb13f8f8, 0x2bb39898, 0x22331111, 0xd2bb6969, 0xa970d9d9, - 0x07898e8e, 0x33a79494, 0x2db69b9b, 0x3c221e1e, 0x15928787, - 0xc920e9e9, 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf, - 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, - 0xd731e6e6, 0x84c64242, 0xd0b86868, 0x82c34141, 0x29b09999, - 0x5a772d2d, 0x1e110f0f, 0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, - 0x2c3a1616 -]; - -/// -const table0Inv = [ - // - 0x50a7f451, 0x5365417e, 0xc3a4171a, 0x965e273a, 0xcb6bab3b, - 0xf1459d1f, 0xab58faac, 0x9303e34b, 0x55fa3020, 0xf66d76ad, - 0x9176cc88, 0x254c02f5, 0xfcd7e54f, 0xd7cb2ac5, 0x80443526, - 0x8fa362b5, 0x495ab1de, 0x671bba25, 0x980eea45, 0xe1c0fe5d, - 0x02752fc3, 0x12f04c81, 0xa397468d, 0xc6f9d36b, 0xe75f8f03, - 0x959c9215, 0xeb7a6dbf, 0xda595295, 0x2d83bed4, 0xd3217458, - 0x2969e049, 0x44c8c98e, 0x6a89c275, 0x78798ef4, 0x6b3e5899, - 0xdd71b927, 0xb64fe1be, 0x17ad88f0, 0x66ac20c9, 0xb43ace7d, - 0x184adf63, 0x82311ae5, 0x60335197, 0x457f5362, 0xe07764b1, - 0x84ae6bbb, 0x1ca081fe, 0x942b08f9, 0x58684870, 0x19fd458f, - 0x876cde94, 0xb7f87b52, 0x23d373ab, 0xe2024b72, 0x578f1fe3, - 0x2aab5566, 0x0728ebb2, 0x03c2b52f, 0x9a7bc586, 0xa50837d3, - 0xf2872830, 0xb2a5bf23, 0xba6a0302, 0x5c8216ed, 0x2b1ccf8a, - 0x92b479a7, 0xf0f207f3, 0xa1e2694e, 0xcdf4da65, 0xd5be0506, - 0x1f6234d1, 0x8afea6c4, 0x9d532e34, 0xa055f3a2, 0x32e18a05, - 0x75ebf6a4, 0x39ec830b, 0xaaef6040, 0x069f715e, 0x51106ebd, - 0xf98a213e, 0x3d06dd96, 0xae053edd, 0x46bde64d, 0xb58d5491, - 0x055dc471, 0x6fd40604, 0xff155060, 0x24fb9819, 0x97e9bdd6, - 0xcc434089, 0x779ed967, 0xbd42e8b0, 0x888b8907, 0x385b19e7, - 0xdbeec879, 0x470a7ca1, 0xe90f427c, 0xc91e84f8, 0x00000000, - 0x83868009, 0x48ed2b32, 0xac70111e, 0x4e725a6c, 0xfbff0efd, - 0x5638850f, 0x1ed5ae3d, 0x27392d36, 0x64d90f0a, 0x21a65c68, - 0xd1545b9b, 0x3a2e3624, 0xb1670a0c, 0x0fe75793, 0xd296eeb4, - 0x9e919b1b, 0x4fc5c080, 0xa220dc61, 0x694b775a, 0x161a121c, - 0x0aba93e2, 0xe52aa0c0, 0x43e0223c, 0x1d171b12, 0x0b0d090e, - 0xadc78bf2, 0xb9a8b62d, 0xc8a91e14, 0x8519f157, 0x4c0775af, - 0xbbdd99ee, 0xfd607fa3, 0x9f2601f7, 0xbcf5725c, 0xc53b6644, - 0x347efb5b, 0x7629438b, 0xdcc623cb, 0x68fcedb6, 0x63f1e4b8, - 0xcadc31d7, 0x10856342, 0x40229713, 0x2011c684, 0x7d244a85, - 0xf83dbbd2, 0x1132f9ae, 0x6da129c7, 0x4b2f9e1d, 0xf330b2dc, - 0xec52860d, 0xd0e3c177, 0x6c16b32b, 0x99b970a9, 0xfa489411, - 0x2264e947, 0xc48cfca8, 0x1a3ff0a0, 0xd82c7d56, 0xef903322, - 0xc74e4987, 0xc1d138d9, 0xfea2ca8c, 0x360bd498, 0xcf81f5a6, - 0x28de7aa5, 0x268eb7da, 0xa4bfad3f, 0xe49d3a2c, 0x0d927850, - 0x9bcc5f6a, 0x62467e54, 0xc2138df6, 0xe8b8d890, 0x5ef7392e, - 0xf5afc382, 0xbe805d9f, 0x7c93d069, 0xa92dd56f, 0xb31225cf, - 0x3b99acc8, 0xa77d1810, 0x6e639ce8, 0x7bbb3bdb, 0x097826cd, - 0xf418596e, 0x01b79aec, 0xa89a4f83, 0x656e95e6, 0x7ee6ffaa, - 0x08cfbc21, 0xe6e815ef, 0xd99be7ba, 0xce366f4a, 0xd4099fea, - 0xd67cb029, 0xafb2a431, 0x31233f2a, 0x3094a5c6, 0xc066a235, - 0x37bc4e74, 0xa6ca82fc, 0xb0d090e0, 0x15d8a733, 0x4a9804f1, - 0xf7daec41, 0x0e50cd7f, 0x2ff69117, 0x8dd64d76, 0x4db0ef43, - 0x544daacc, 0xdf0496e4, 0xe3b5d19e, 0x1b886a4c, 0xb81f2cc1, - 0x7f516546, 0x04ea5e9d, 0x5d358c01, 0x737487fa, 0x2e410bfb, - 0x5a1d67b3, 0x52d2db92, 0x335610e9, 0x1347d66d, 0x8c61d79a, - 0x7a0ca137, 0x8e14f859, 0x893c13eb, 0xee27a9ce, 0x35c961b7, - 0xede51ce1, 0x3cb1477a, 0x59dfd29c, 0x3f73f255, 0x79ce1418, - 0xbf37c773, 0xeacdf753, 0x5baafd5f, 0x146f3ddf, 0x86db4478, - 0x81f3afca, 0x3ec468b9, 0x2c342438, 0x5f40a3c2, 0x72c31d16, - 0x0c25e2bc, 0x8b493c28, 0x41950dff, 0x7101a839, 0xdeb30c08, - 0x9ce4b4d8, 0x90c15664, 0x6184cb7b, 0x70b632d5, 0x745c6c48, - 0x4257b8d0, -]; - -/// -const table1Inv = [ - // - 0xa7f45150, 0x65417e53, 0xa4171ac3, 0x5e273a96, 0x6bab3bcb, - 0x459d1ff1, 0x58faacab, 0x03e34b93, 0xfa302055, 0x6d76adf6, - 0x76cc8891, 0x4c02f525, 0xd7e54ffc, 0xcb2ac5d7, 0x44352680, - 0xa362b58f, 0x5ab1de49, 0x1bba2567, 0x0eea4598, 0xc0fe5de1, - 0x752fc302, 0xf04c8112, 0x97468da3, 0xf9d36bc6, 0x5f8f03e7, - 0x9c921595, 0x7a6dbfeb, 0x595295da, 0x83bed42d, 0x217458d3, - 0x69e04929, 0xc8c98e44, 0x89c2756a, 0x798ef478, 0x3e58996b, - 0x71b927dd, 0x4fe1beb6, 0xad88f017, 0xac20c966, 0x3ace7db4, - 0x4adf6318, 0x311ae582, 0x33519760, 0x7f536245, 0x7764b1e0, - 0xae6bbb84, 0xa081fe1c, 0x2b08f994, 0x68487058, 0xfd458f19, - 0x6cde9487, 0xf87b52b7, 0xd373ab23, 0x024b72e2, 0x8f1fe357, - 0xab55662a, 0x28ebb207, 0xc2b52f03, 0x7bc5869a, 0x0837d3a5, - 0x872830f2, 0xa5bf23b2, 0x6a0302ba, 0x8216ed5c, 0x1ccf8a2b, - 0xb479a792, 0xf207f3f0, 0xe2694ea1, 0xf4da65cd, 0xbe0506d5, - 0x6234d11f, 0xfea6c48a, 0x532e349d, 0x55f3a2a0, 0xe18a0532, - 0xebf6a475, 0xec830b39, 0xef6040aa, 0x9f715e06, 0x106ebd51, - 0x8a213ef9, 0x06dd963d, 0x053eddae, 0xbde64d46, 0x8d5491b5, - 0x5dc47105, 0xd406046f, 0x155060ff, 0xfb981924, 0xe9bdd697, - 0x434089cc, 0x9ed96777, 0x42e8b0bd, 0x8b890788, 0x5b19e738, - 0xeec879db, 0x0a7ca147, 0x0f427ce9, 0x1e84f8c9, 0x00000000, - 0x86800983, 0xed2b3248, 0x70111eac, 0x725a6c4e, 0xff0efdfb, - 0x38850f56, 0xd5ae3d1e, 0x392d3627, 0xd90f0a64, 0xa65c6821, - 0x545b9bd1, 0x2e36243a, 0x670a0cb1, 0xe757930f, 0x96eeb4d2, - 0x919b1b9e, 0xc5c0804f, 0x20dc61a2, 0x4b775a69, 0x1a121c16, - 0xba93e20a, 0x2aa0c0e5, 0xe0223c43, 0x171b121d, 0x0d090e0b, - 0xc78bf2ad, 0xa8b62db9, 0xa91e14c8, 0x19f15785, 0x0775af4c, - 0xdd99eebb, 0x607fa3fd, 0x2601f79f, 0xf5725cbc, 0x3b6644c5, - 0x7efb5b34, 0x29438b76, 0xc623cbdc, 0xfcedb668, 0xf1e4b863, - 0xdc31d7ca, 0x85634210, 0x22971340, 0x11c68420, 0x244a857d, - 0x3dbbd2f8, 0x32f9ae11, 0xa129c76d, 0x2f9e1d4b, 0x30b2dcf3, - 0x52860dec, 0xe3c177d0, 0x16b32b6c, 0xb970a999, 0x489411fa, - 0x64e94722, 0x8cfca8c4, 0x3ff0a01a, 0x2c7d56d8, 0x903322ef, - 0x4e4987c7, 0xd138d9c1, 0xa2ca8cfe, 0x0bd49836, 0x81f5a6cf, - 0xde7aa528, 0x8eb7da26, 0xbfad3fa4, 0x9d3a2ce4, 0x9278500d, - 0xcc5f6a9b, 0x467e5462, 0x138df6c2, 0xb8d890e8, 0xf7392e5e, - 0xafc382f5, 0x805d9fbe, 0x93d0697c, 0x2dd56fa9, 0x1225cfb3, - 0x99acc83b, 0x7d1810a7, 0x639ce86e, 0xbb3bdb7b, 0x7826cd09, - 0x18596ef4, 0xb79aec01, 0x9a4f83a8, 0x6e95e665, 0xe6ffaa7e, - 0xcfbc2108, 0xe815efe6, 0x9be7bad9, 0x366f4ace, 0x099fead4, - 0x7cb029d6, 0xb2a431af, 0x233f2a31, 0x94a5c630, 0x66a235c0, - 0xbc4e7437, 0xca82fca6, 0xd090e0b0, 0xd8a73315, 0x9804f14a, - 0xdaec41f7, 0x50cd7f0e, 0xf691172f, 0xd64d768d, 0xb0ef434d, - 0x4daacc54, 0x0496e4df, 0xb5d19ee3, 0x886a4c1b, 0x1f2cc1b8, - 0x5165467f, 0xea5e9d04, 0x358c015d, 0x7487fa73, 0x410bfb2e, - 0x1d67b35a, 0xd2db9252, 0x5610e933, 0x47d66d13, 0x61d79a8c, - 0x0ca1377a, 0x14f8598e, 0x3c13eb89, 0x27a9ceee, 0xc961b735, - 0xe51ce1ed, 0xb1477a3c, 0xdfd29c59, 0x73f2553f, 0xce141879, - 0x37c773bf, 0xcdf753ea, 0xaafd5f5b, 0x6f3ddf14, 0xdb447886, - 0xf3afca81, 0xc468b93e, 0x3424382c, 0x40a3c25f, 0xc31d1672, - 0x25e2bc0c, 0x493c288b, 0x950dff41, 0x01a83971, 0xb30c08de, - 0xe4b4d89c, 0xc1566490, 0x84cb7b61, 0xb632d570, 0x5c6c4874, - 0x57b8d042, -]; - -/// -const table2Inv = [ - // - 0xf45150a7, 0x417e5365, 0x171ac3a4, 0x273a965e, 0xab3bcb6b, - 0x9d1ff145, 0xfaacab58, 0xe34b9303, 0x302055fa, 0x76adf66d, - 0xcc889176, 0x02f5254c, 0xe54ffcd7, 0x2ac5d7cb, 0x35268044, - 0x62b58fa3, 0xb1de495a, 0xba25671b, 0xea45980e, 0xfe5de1c0, - 0x2fc30275, 0x4c8112f0, 0x468da397, 0xd36bc6f9, 0x8f03e75f, - 0x9215959c, 0x6dbfeb7a, 0x5295da59, 0xbed42d83, 0x7458d321, - 0xe0492969, 0xc98e44c8, 0xc2756a89, 0x8ef47879, 0x58996b3e, - 0xb927dd71, 0xe1beb64f, 0x88f017ad, 0x20c966ac, 0xce7db43a, - 0xdf63184a, 0x1ae58231, 0x51976033, 0x5362457f, 0x64b1e077, - 0x6bbb84ae, 0x81fe1ca0, 0x08f9942b, 0x48705868, 0x458f19fd, - 0xde94876c, 0x7b52b7f8, 0x73ab23d3, 0x4b72e202, 0x1fe3578f, - 0x55662aab, 0xebb20728, 0xb52f03c2, 0xc5869a7b, 0x37d3a508, - 0x2830f287, 0xbf23b2a5, 0x0302ba6a, 0x16ed5c82, 0xcf8a2b1c, - 0x79a792b4, 0x07f3f0f2, 0x694ea1e2, 0xda65cdf4, 0x0506d5be, - 0x34d11f62, 0xa6c48afe, 0x2e349d53, 0xf3a2a055, 0x8a0532e1, - 0xf6a475eb, 0x830b39ec, 0x6040aaef, 0x715e069f, 0x6ebd5110, - 0x213ef98a, 0xdd963d06, 0x3eddae05, 0xe64d46bd, 0x5491b58d, - 0xc471055d, 0x06046fd4, 0x5060ff15, 0x981924fb, 0xbdd697e9, - 0x4089cc43, 0xd967779e, 0xe8b0bd42, 0x8907888b, 0x19e7385b, - 0xc879dbee, 0x7ca1470a, 0x427ce90f, 0x84f8c91e, 0x00000000, - 0x80098386, 0x2b3248ed, 0x111eac70, 0x5a6c4e72, 0x0efdfbff, - 0x850f5638, 0xae3d1ed5, 0x2d362739, 0x0f0a64d9, 0x5c6821a6, - 0x5b9bd154, 0x36243a2e, 0x0a0cb167, 0x57930fe7, 0xeeb4d296, - 0x9b1b9e91, 0xc0804fc5, 0xdc61a220, 0x775a694b, 0x121c161a, - 0x93e20aba, 0xa0c0e52a, 0x223c43e0, 0x1b121d17, 0x090e0b0d, - 0x8bf2adc7, 0xb62db9a8, 0x1e14c8a9, 0xf1578519, 0x75af4c07, - 0x99eebbdd, 0x7fa3fd60, 0x01f79f26, 0x725cbcf5, 0x6644c53b, - 0xfb5b347e, 0x438b7629, 0x23cbdcc6, 0xedb668fc, 0xe4b863f1, - 0x31d7cadc, 0x63421085, 0x97134022, 0xc6842011, 0x4a857d24, - 0xbbd2f83d, 0xf9ae1132, 0x29c76da1, 0x9e1d4b2f, 0xb2dcf330, - 0x860dec52, 0xc177d0e3, 0xb32b6c16, 0x70a999b9, 0x9411fa48, - 0xe9472264, 0xfca8c48c, 0xf0a01a3f, 0x7d56d82c, 0x3322ef90, - 0x4987c74e, 0x38d9c1d1, 0xca8cfea2, 0xd498360b, 0xf5a6cf81, - 0x7aa528de, 0xb7da268e, 0xad3fa4bf, 0x3a2ce49d, 0x78500d92, - 0x5f6a9bcc, 0x7e546246, 0x8df6c213, 0xd890e8b8, 0x392e5ef7, - 0xc382f5af, 0x5d9fbe80, 0xd0697c93, 0xd56fa92d, 0x25cfb312, - 0xacc83b99, 0x1810a77d, 0x9ce86e63, 0x3bdb7bbb, 0x26cd0978, - 0x596ef418, 0x9aec01b7, 0x4f83a89a, 0x95e6656e, 0xffaa7ee6, - 0xbc2108cf, 0x15efe6e8, 0xe7bad99b, 0x6f4ace36, 0x9fead409, - 0xb029d67c, 0xa431afb2, 0x3f2a3123, 0xa5c63094, 0xa235c066, - 0x4e7437bc, 0x82fca6ca, 0x90e0b0d0, 0xa73315d8, 0x04f14a98, - 0xec41f7da, 0xcd7f0e50, 0x91172ff6, 0x4d768dd6, 0xef434db0, - 0xaacc544d, 0x96e4df04, 0xd19ee3b5, 0x6a4c1b88, 0x2cc1b81f, - 0x65467f51, 0x5e9d04ea, 0x8c015d35, 0x87fa7374, 0x0bfb2e41, - 0x67b35a1d, 0xdb9252d2, 0x10e93356, 0xd66d1347, 0xd79a8c61, - 0xa1377a0c, 0xf8598e14, 0x13eb893c, 0xa9ceee27, 0x61b735c9, - 0x1ce1ede5, 0x477a3cb1, 0xd29c59df, 0xf2553f73, 0x141879ce, - 0xc773bf37, 0xf753eacd, 0xfd5f5baa, 0x3ddf146f, 0x447886db, - 0xafca81f3, 0x68b93ec4, 0x24382c34, 0xa3c25f40, 0x1d1672c3, - 0xe2bc0c25, 0x3c288b49, 0x0dff4195, 0xa8397101, 0x0c08deb3, - 0xb4d89ce4, 0x566490c1, 0xcb7b6184, 0x32d570b6, 0x6c48745c, - 0xb8d04257, -]; - -/// -const table3Inv = [ - // - 0x5150a7f4, 0x7e536541, 0x1ac3a417, 0x3a965e27, 0x3bcb6bab, - 0x1ff1459d, 0xacab58fa, 0x4b9303e3, 0x2055fa30, 0xadf66d76, - 0x889176cc, 0xf5254c02, 0x4ffcd7e5, 0xc5d7cb2a, 0x26804435, - 0xb58fa362, 0xde495ab1, 0x25671bba, 0x45980eea, 0x5de1c0fe, - 0xc302752f, 0x8112f04c, 0x8da39746, 0x6bc6f9d3, 0x03e75f8f, - 0x15959c92, 0xbfeb7a6d, 0x95da5952, 0xd42d83be, 0x58d32174, - 0x492969e0, 0x8e44c8c9, 0x756a89c2, 0xf478798e, 0x996b3e58, - 0x27dd71b9, 0xbeb64fe1, 0xf017ad88, 0xc966ac20, 0x7db43ace, - 0x63184adf, 0xe582311a, 0x97603351, 0x62457f53, 0xb1e07764, - 0xbb84ae6b, 0xfe1ca081, 0xf9942b08, 0x70586848, 0x8f19fd45, - 0x94876cde, 0x52b7f87b, 0xab23d373, 0x72e2024b, 0xe3578f1f, - 0x662aab55, 0xb20728eb, 0x2f03c2b5, 0x869a7bc5, 0xd3a50837, - 0x30f28728, 0x23b2a5bf, 0x02ba6a03, 0xed5c8216, 0x8a2b1ccf, - 0xa792b479, 0xf3f0f207, 0x4ea1e269, 0x65cdf4da, 0x06d5be05, - 0xd11f6234, 0xc48afea6, 0x349d532e, 0xa2a055f3, 0x0532e18a, - 0xa475ebf6, 0x0b39ec83, 0x40aaef60, 0x5e069f71, 0xbd51106e, - 0x3ef98a21, 0x963d06dd, 0xddae053e, 0x4d46bde6, 0x91b58d54, - 0x71055dc4, 0x046fd406, 0x60ff1550, 0x1924fb98, 0xd697e9bd, - 0x89cc4340, 0x67779ed9, 0xb0bd42e8, 0x07888b89, 0xe7385b19, - 0x79dbeec8, 0xa1470a7c, 0x7ce90f42, 0xf8c91e84, 0x00000000, - 0x09838680, 0x3248ed2b, 0x1eac7011, 0x6c4e725a, 0xfdfbff0e, - 0x0f563885, 0x3d1ed5ae, 0x3627392d, 0x0a64d90f, 0x6821a65c, - 0x9bd1545b, 0x243a2e36, 0x0cb1670a, 0x930fe757, 0xb4d296ee, - 0x1b9e919b, 0x804fc5c0, 0x61a220dc, 0x5a694b77, 0x1c161a12, - 0xe20aba93, 0xc0e52aa0, 0x3c43e022, 0x121d171b, 0x0e0b0d09, - 0xf2adc78b, 0x2db9a8b6, 0x14c8a91e, 0x578519f1, 0xaf4c0775, - 0xeebbdd99, 0xa3fd607f, 0xf79f2601, 0x5cbcf572, 0x44c53b66, - 0x5b347efb, 0x8b762943, 0xcbdcc623, 0xb668fced, 0xb863f1e4, - 0xd7cadc31, 0x42108563, 0x13402297, 0x842011c6, 0x857d244a, - 0xd2f83dbb, 0xae1132f9, 0xc76da129, 0x1d4b2f9e, 0xdcf330b2, - 0x0dec5286, 0x77d0e3c1, 0x2b6c16b3, 0xa999b970, 0x11fa4894, - 0x472264e9, 0xa8c48cfc, 0xa01a3ff0, 0x56d82c7d, 0x22ef9033, - 0x87c74e49, 0xd9c1d138, 0x8cfea2ca, 0x98360bd4, 0xa6cf81f5, - 0xa528de7a, 0xda268eb7, 0x3fa4bfad, 0x2ce49d3a, 0x500d9278, - 0x6a9bcc5f, 0x5462467e, 0xf6c2138d, 0x90e8b8d8, 0x2e5ef739, - 0x82f5afc3, 0x9fbe805d, 0x697c93d0, 0x6fa92dd5, 0xcfb31225, - 0xc83b99ac, 0x10a77d18, 0xe86e639c, 0xdb7bbb3b, 0xcd097826, - 0x6ef41859, 0xec01b79a, 0x83a89a4f, 0xe6656e95, 0xaa7ee6ff, - 0x2108cfbc, 0xefe6e815, 0xbad99be7, 0x4ace366f, 0xead4099f, - 0x29d67cb0, 0x31afb2a4, 0x2a31233f, 0xc63094a5, 0x35c066a2, - 0x7437bc4e, 0xfca6ca82, 0xe0b0d090, 0x3315d8a7, 0xf14a9804, - 0x41f7daec, 0x7f0e50cd, 0x172ff691, 0x768dd64d, 0x434db0ef, - 0xcc544daa, 0xe4df0496, 0x9ee3b5d1, 0x4c1b886a, 0xc1b81f2c, - 0x467f5165, 0x9d04ea5e, 0x015d358c, 0xfa737487, 0xfb2e410b, - 0xb35a1d67, 0x9252d2db, 0xe9335610, 0x6d1347d6, 0x9a8c61d7, - 0x377a0ca1, 0x598e14f8, 0xeb893c13, 0xceee27a9, 0xb735c961, - 0xe1ede51c, 0x7a3cb147, 0x9c59dfd2, 0x553f73f2, 0x1879ce14, - 0x73bf37c7, 0x53eacdf7, 0x5f5baafd, 0xdf146f3d, 0x7886db44, - 0xca81f3af, 0xb93ec468, 0x382c3424, 0xc25f40a3, 0x1672c31d, - 0xbc0c25e2, 0x288b493c, 0xff41950d, 0x397101a8, 0x08deb30c, - 0xd89ce4b4, 0x6490c156, 0x7b6184cb, 0xd570b632, 0x48745c6c, - 0xd04257b8, -]; diff --git a/hive/lib/src/crypto/crc32.dart b/hive/lib/src/crypto/crc32.dart deleted file mode 100644 index 88f5d65bf..000000000 --- a/hive/lib/src/crypto/crc32.dart +++ /dev/null @@ -1,70 +0,0 @@ -import 'dart:typed_data'; - -/// Efficient CRC32 implementation -class Crc32 { - /// Computes the CRC32 checksum of [bytes] using a previous [crc] value. - static int compute( - Uint8List bytes, { - int crc = 0, - int offset = 0, - int? length, - }) { - crc = crc ^ 0xffffffff; - - length ??= bytes.length; - - for (var i = offset; i < offset + length; i++) { - crc = _table[(crc ^ bytes[i]) & 0xff] ^ (crc >> 8); - } - - return crc ^ 0xffffffff; - } - - /// Precalculated CRC table. - static const List _table = [ - // - 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, - 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, - 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91, 0x1db71064, 0x6ab020f2, - 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, - 0x136c9856, 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9, - 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4, 0xa2677172, - 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b, 0x35b5a8fa, 0x42b2986c, - 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3, 0x45df5c75, 0xdcd60dcf, 0xabd13d59, - 0x26d930ac, 0x51de003a, 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, - 0xcfba9599, 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924, - 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190, 0x01db7106, - 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f, 0x9fbfe4a5, 0xe8b8d433, - 0x7807c9a2, 0x0f00f934, 0x9609a88e, 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, - 0x91646c97, 0xe6635c01, 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, - 0x6c0695ed, 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950, - 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3, 0xfbd44c65, - 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2, 0x4adfa541, 0x3dd895d7, - 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a, 0x346ed9fc, 0xad678846, 0xda60b8d0, - 0x44042d73, 0x33031de5, 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, - 0xbe0b1010, 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f, - 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17, 0x2eb40d81, - 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6, 0x03b6e20c, 0x74b1d29a, - 0xead54739, 0x9dd277af, 0x04db2615, 0x73dc1683, 0xe3630b12, 0x94643b84, - 0x0d6d6a3e, 0x7a6a5aa8, 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, - 0xf00f9344, 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb, - 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a, 0x67dd4acc, - 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5, 0xd6d6a3e8, 0xa1d1937e, - 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1, 0xa6bc5767, 0x3fb506dd, 0x48b2364b, - 0xd80d2bda, 0xaf0a1b4c, 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, - 0x316e8eef, 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236, - 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe, 0xb2bd0b28, - 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31, 0x2cd99e8b, 0x5bdeae1d, - 0x9b64c2b0, 0xec63f226, 0x756aa39c, 0x026d930a, 0x9c0906a9, 0xeb0e363f, - 0x72076785, 0x05005713, 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, - 0x92d28e9b, 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242, - 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1, 0x18b74777, - 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c, 0x8f659eff, 0xf862ae69, - 0x616bffd3, 0x166ccf45, 0xa00ae278, 0xd70dd2ee, 0x4e048354, 0x3903b3c2, - 0xa7672661, 0xd06016f7, 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, - 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9, - 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, - 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, - 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d - ]; -} diff --git a/hive/lib/src/crypto/hive_aes_cipher.dart b/hive/lib/src/crypto/hive_aes_cipher.dart deleted file mode 100644 index bdd4e071e..000000000 --- a/hive/lib/src/crypto/hive_aes_cipher.dart +++ /dev/null @@ -1,53 +0,0 @@ -part of hive; - -/// Default encryption algorithm. Uses AES256 CBC with PKCS7 padding. -class HiveAesCipher implements HiveCipher { - static final _ivRandom = Random.secure(); - - late final AesCbcPkcs7 _cipher; - - late final int _keyCrc; - - /// Create a cipher with the given [key]. - HiveAesCipher(List key) { - if (key.length != 32 || key.any((it) => it < 0 || it > 255)) { - throw ArgumentError( - 'The encryption key has to be a 32 byte (256 bit) array.'); - } - - var keyBytes = Uint8List.fromList(key); - _cipher = AesCbcPkcs7(keyBytes); - _keyCrc = Crc32.compute(sha256.convert(keyBytes).bytes as Uint8List); - } - - @override - int calculateKeyCrc() => _keyCrc; - - @override - FutureOr decrypt( - Uint8List inp, int inpOff, int inpLength, Uint8List out, int outOff) { - var iv = inp.view(inpOff, 16); - - return _cipher.decrypt(iv, inp, inpOff + 16, inpLength - 16, out, 0); - } - - /// Generates a random initialization vector (internal) - @visibleForTesting - Uint8List generateIv() => _ivRandom.nextBytes(16); - - @override - FutureOr encrypt( - Uint8List inp, int inpOff, int inpLength, Uint8List out, int outOff) { - var iv = generateIv(); - out.setAll(outOff, iv); - - var len = _cipher.encrypt(iv, inp, 0, inpLength, out, outOff + 16); - - return len + 16; - } - - @override - int maxEncryptedSize(Uint8List inp) { - return inp.length + 32; // 16 IV + 16 extra for padding - } -} diff --git a/hive/lib/src/crypto/hive_cipher.dart b/hive/lib/src/crypto/hive_cipher.dart deleted file mode 100644 index d257b5505..000000000 --- a/hive/lib/src/crypto/hive_cipher.dart +++ /dev/null @@ -1,37 +0,0 @@ -part of hive; - -/// Abstract cipher can be implemented to customize encryption. -/// -/// Encryption and decryption can be either synchronous (e.g. with the -/// [HiveAesCipher] implementation) or asynchronous for use with -/// hardware-accelerated crypto implementations. -abstract class HiveCipher { - /// Calculate a hash of the key. Make sure to use a secure hash. - int calculateKeyCrc(); - - /// The maximum size the input can have after it has been encrypted. - int maxEncryptedSize(Uint8List inp); - - /// - /// - [inp]: the total bytes in plain text - /// - [inpOff]: the byte offset to start encryption at - /// - [inpLength]: the number of bytes (length) to encrypt - /// - [out]: the buffer to write the encrypted output in - /// - [outOff]: the byte offset to write the encrypted output to - /// - /// returns the length of the new encrypted output - FutureOr encrypt( - Uint8List inp, int inpOff, int inpLength, Uint8List out, int outOff); - - /// Decrypt the given bytes. - /// - /// - [inp]: the total encrypted bytes - /// - [inpOff]: the byte offset to start decryption at - /// - [inpLength]: the number of bytes (length) to decrypt - /// - [out]: the buffer to write the decrypted output in - /// - [outOff]: the byte offset to write the decrypted output to - /// - /// returns the length of the new decrypted output - FutureOr decrypt( - Uint8List inp, int inpOff, int inpLength, Uint8List out, int outOff); -} diff --git a/hive/lib/src/hive.dart b/hive/lib/src/hive.dart deleted file mode 100644 index 755fc41f9..000000000 --- a/hive/lib/src/hive.dart +++ /dev/null @@ -1,106 +0,0 @@ -part of hive; - -/// The main API interface of Hive. Available through the `Hive` constant. -abstract class HiveInterface implements TypeRegistry { - /// Initialize Hive by giving it a home directory. - /// - /// (Not necessary in the browser) - /// - /// The [useLocks] parameter can be used to turn off lockfiles for the - /// database. Locks are used to prevent other processes from opening the - /// database and in turn protect the database from corruption. If you turn - /// this off, you need to externally synchronize accesses to the database. - /// There is no performance benefit from disabling locks. The only benefit is - /// that some platforms don't allow you to suspend while holding a lock for a - /// file in a shared folder (i.e. on iOS, if you move the database to a - /// shared container). If your app isn't crashing because it holds a lock - /// during suspension and you don't know that you need this feature and don't - /// understand the consequences, don't turn locking off. - void init( - String? path, { - HiveStorageBackendPreference backendPreference = - HiveStorageBackendPreference.native, - bool useLocks = true, - }); - - /// Opens a box. - /// - /// If the box is already open, the instance is returned and all provided - /// parameters are being ignored. - Future> openBox( - String name, { - HiveCipher? encryptionCipher, - KeyComparator keyComparator = defaultKeyComparator, - CompactionStrategy compactionStrategy = defaultCompactionStrategy, - bool crashRecovery = true, - String? path, - @Deprecated('Use [backend] with a [StorageBackendMemory] instead') - Uint8List? bytes, - StorageBackend? backend, - String? collection, - @Deprecated('Use encryptionCipher instead') List? encryptionKey, - }); - - /// Opens a lazy box. - /// - /// If the box is already open, the instance is returned and all provided - /// parameters are being ignored. - Future> openLazyBox( - String name, { - HiveCipher? encryptionCipher, - KeyComparator keyComparator = defaultKeyComparator, - CompactionStrategy compactionStrategy = defaultCompactionStrategy, - bool crashRecovery = true, - String? path, - String? collection, - @Deprecated('Use encryptionCipher instead') List? encryptionKey, - StorageBackend? backend, - }); - - /// Returns a previously opened box. - Box box(String name); - - /// Returns a previously opened lazy box. - LazyBox lazyBox(String name); - - /// Checks if a specific box is currently open. - bool isBoxOpen(String name); - - /// Closes all open boxes. - Future close(); - - /// Removes the file which contains the box and closes the box. - /// - /// In the browser, the IndexedDB database is being removed. - Future deleteBoxFromDisk(String name, - {String? path, String? collection}); - - /// Deletes all currently open boxes from disk. - /// - /// The home directory will not be deleted. - Future deleteFromDisk(); - - /// Generates a secure encryption key using the fortuna random algorithm. - List generateSecureKey(); - - /// Checks if a box exists - Future boxExists(String name, {String? path}); - - /// Clears all registered adapters. - /// - /// To register an adapter use [registerAdapter]. - /// - /// NOTE: [resetAdapters] also clears the default adapters registered - /// by Hive. - /// - /// WARNING: This method is only intended to be used for integration and - /// unit tests and SHOULD not be used in production code. - @visibleForTesting - void resetAdapters(); -} - -/// -typedef KeyComparator = int Function(dynamic key1, dynamic key2); - -/// A function which decides when to compact a box. -typedef CompactionStrategy = bool Function(int entries, int deletedEntries); diff --git a/hive/lib/src/hive_error.dart b/hive/lib/src/hive_error.dart deleted file mode 100644 index 66ed5f519..000000000 --- a/hive/lib/src/hive_error.dart +++ /dev/null @@ -1,15 +0,0 @@ -part of hive; - -/// An error related to Hive. -class HiveError extends Error { - /// A description of the error. - final String message; - - /// Create a new Hive error (internal) - HiveError(this.message); - - @override - String toString() { - return 'HiveError: $message'; - } -} diff --git a/hive/lib/src/hive_impl.dart b/hive/lib/src/hive_impl.dart deleted file mode 100644 index e11875ef0..000000000 --- a/hive/lib/src/hive_impl.dart +++ /dev/null @@ -1,292 +0,0 @@ -import 'dart:async'; -import 'dart:collection'; -import 'dart:math'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/adapters/big_int_adapter.dart'; -import 'package:hive/src/adapters/date_time_adapter.dart'; -import 'package:hive/src/box/box_base_impl.dart'; -import 'package:hive/src/box/box_impl.dart'; -import 'package:hive/src/box/default_compaction_strategy.dart'; -import 'package:hive/src/box/default_key_comparator.dart'; -import 'package:hive/src/box/lazy_box_impl.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:hive/src/util/extensions.dart'; - -import 'backend/storage_backend.dart'; - -/// Not part of public API -class HiveImpl extends TypeRegistryImpl implements HiveInterface { - static final BackendManagerInterface _defaultBackendManager = - BackendManager.select(); - - final _boxes = HashMap(); - final _openingBoxes = HashMap>(); - BackendManagerInterface? _managerOverride; - final Random _secureRandom = Random.secure(); - - String? homePath; - - bool useLocks = true; - - bool wasInitialized=false; - - /// Not part of public API - HiveImpl() { - _registerDefaultAdapters(); - } - - /// either returns the preferred [BackendManagerInterface] or the - /// platform default fallback - BackendManagerInterface get manager => - _managerOverride ?? _defaultBackendManager; - - void _registerDefaultAdapters() { - registerAdapter(DateTimeWithTimezoneAdapter(), internal: true); - registerAdapter(DateTimeAdapter(), internal: true); - registerAdapter(BigIntAdapter(), internal: true); - } - - @override - void init( - String? path, { - HiveStorageBackendPreference backendPreference = - HiveStorageBackendPreference.native, - bool useLocks = true, - }) { - homePath = path; - _managerOverride = BackendManager.select(backendPreference); - this.useLocks = useLocks; - wasInitialized=true; - } - - Future> _openBox( - String name, - bool lazy, - HiveCipher? cipher, - KeyComparator comparator, - CompactionStrategy compaction, - bool recovery, - String? path, - StorageBackend? backend, - String? collection, - ) async { - assert(path == null || backend == null); - assert(name.length <= 255, 'Box names need to be a max length of 255.'); - name = name.toLowerCase(); - if (isBoxOpen(name)) { - if (lazy) { - return lazyBox(name); - } else { - return box(name); - } - } else { - if (_openingBoxes.containsKey(TupleBoxKey(name, collection))) { - bool? opened = await _openingBoxes[TupleBoxKey(name, collection)]; - if (opened ?? false) { - if (lazy) { - return lazyBox(name); - } else { - return box(name); - } - } else { - throw HiveError('The opening of the box $name failed previously.'); - } - } - - var completer = Completer(); - _openingBoxes[TupleBoxKey(name, collection)] = completer.future; - - BoxBaseImpl? newBox; - try { - backend ??= await manager.open( - name, path ?? homePath, recovery, cipher, collection); - - if (lazy) { - newBox = LazyBoxImpl(this, name, comparator, compaction, backend); - } else { - newBox = BoxImpl(this, name, comparator, compaction, backend); - } - - await newBox.initialize(); - _boxes[TupleBoxKey(name, collection)] = newBox; - - completer.complete(true); - return newBox; - } catch (error) { - // Finish by signaling an error has occurred. We complete before closing - // the box, because that can fail and this the Completer would never get - // completed. - completer.complete(false); - // Await the closing of the box to prevent leaving a hanging Future - // which could not be caught. - await newBox?.close(); - rethrow; - } finally { - unawaited(_openingBoxes.remove(TupleBoxKey(name, collection))); - } - } - } - - @override - Future> openBox( - String name, { - HiveCipher? encryptionCipher, - KeyComparator keyComparator = defaultKeyComparator, - CompactionStrategy compactionStrategy = defaultCompactionStrategy, - bool crashRecovery = true, - String? path, - @Deprecated('Use [backend] with a [StorageBackendMemory] instead') - Uint8List? bytes, - StorageBackend? backend, - String? collection, - @Deprecated('Use encryptionCipher instead') List? encryptionKey, - }) async { - if (encryptionKey != null) { - encryptionCipher = HiveAesCipher(encryptionKey); - } - if (backend == null && bytes != null) { - backend = StorageBackendMemory(bytes, encryptionCipher); - } - return await _openBox(name, false, encryptionCipher, keyComparator, - compactionStrategy, crashRecovery, path, backend, collection) as Box; - } - - @override - Future> openLazyBox( - String name, { - HiveCipher? encryptionCipher, - KeyComparator keyComparator = defaultKeyComparator, - CompactionStrategy compactionStrategy = defaultCompactionStrategy, - bool crashRecovery = true, - String? path, - String? collection, - @Deprecated('Use encryptionCipher instead') List? encryptionKey, - StorageBackend? backend, - }) async { - if (encryptionKey != null) { - encryptionCipher = HiveAesCipher(encryptionKey); - } - return await _openBox( - name, - true, - encryptionCipher, - keyComparator, - compactionStrategy, - crashRecovery, - path, - backend, - collection) as LazyBox; - } - - BoxBase _getBoxInternal(String name, [bool? lazy, String? collection]) { - var lowerCaseName = name.toLowerCase(); - var box = _boxes[TupleBoxKey(lowerCaseName, collection)]; - if (box != null) { - if ((lazy == null || box.lazy == lazy) && box.valueType == E) { - return box as BoxBase; - } else { - var typeName = box is LazyBox - ? 'LazyBox<${box.valueType}>' - : 'Box<${box.valueType}>'; - throw HiveError('The box "$lowerCaseName" is already open ' - 'and of type $typeName.'); - } - } else { - throw HiveError('Box not found. Did you forget to call Hive.openBox()?'); - } - } - - /// Not part of public API - BoxBase? getBoxWithoutCheckInternal(String name, [String? collection]) { - print('Fetching box $name'); - print(_boxes.keys); - var lowerCaseName = name.toLowerCase(); - print(_boxes.containsKey(TupleBoxKey(lowerCaseName, collection))); - return _boxes[TupleBoxKey(lowerCaseName, collection)]; - } - - @override - Box box(String name, [String? collection]) => - _getBoxInternal(name, false, collection) as Box; - - @override - LazyBox lazyBox(String name, [String? collection]) => - _getBoxInternal(name, true, collection) as LazyBox; - - @override - bool isBoxOpen(String name, [String? collection]) { - return _boxes.containsKey(TupleBoxKey(name.toLowerCase(), collection)); - } - - @override - Future close() { - var closeFutures = _boxes.values.map((box) { - return box.close(); - }); - - return Future.wait(closeFutures); - } - - /// Not part of public API - void unregisterBox(String name, [String? collection]) { - name = name.toLowerCase(); - _openingBoxes.remove(TupleBoxKey(name, collection)); - _boxes.remove(TupleBoxKey(name, collection)); - } - - @override - Future deleteBoxFromDisk(String name, - {String? path, String? collection}) async { - var lowerCaseName = name.toLowerCase(); - var box = _boxes[TupleBoxKey(lowerCaseName, collection)]; - if (box != null) { - await box.deleteFromDisk(); - } else { - await manager.deleteBox(lowerCaseName, path ?? homePath, collection); - } - } - - @override - Future deleteFromDisk() { - var deleteFutures = _boxes.values.toList().map((box) { - return box.deleteFromDisk(); - }); - - return Future.wait(deleteFutures); - } - - @override - List generateSecureKey() { - return _secureRandom.nextBytes(32); - } - - @override - Future boxExists(String name, - {String? path, String? collection}) async { - var lowerCaseName = name.toLowerCase(); - return await manager.boxExists(lowerCaseName, path ?? homePath, collection); - } -} - -/// tiny helper for map key management... -class TupleBoxKey { - final String box; - final String? collection; - - TupleBoxKey(this.box, this.collection); - - @override - String toString() => collection == null ? box : '$collection.$box'; - - @override - int get hashCode => collection == null - ? box.hashCode - : [box.hashCode, collection.hashCode].hashCode; - - @override - bool operator ==(Object other) { - return hashCode == other.hashCode; - } -} diff --git a/hive/lib/src/io/buffered_file_reader.dart b/hive/lib/src/io/buffered_file_reader.dart deleted file mode 100644 index e52f73ab2..000000000 --- a/hive/lib/src/io/buffered_file_reader.dart +++ /dev/null @@ -1,88 +0,0 @@ -import 'dart:io'; - -import 'dart:typed_data'; - -import 'package:meta/meta.dart'; - -/// Not part of public API -class BufferedFileReader { - /// Not part of public API - @visibleForTesting - static const defaultChunkSize = 1000 * 64; - - /// Not part of public API - /// - /// Nullable because of testing. [loadBytes] can throw if the count is not in - /// the buffer. - @visibleForTesting - final RandomAccessFile? file; - - /// Not part of public API - @visibleForTesting - Uint8List buffer; - - int _bufferSize = 0; - int _bufferOffset = 0; - int _fileOffset = 0; - - /// Not part of public API - int get remainingInBuffer => _bufferSize - _bufferOffset; - - /// Not part of public API - int get offset => _fileOffset - remainingInBuffer; - - /// Not part of public API - BufferedFileReader(this.file, [int bufferSize = defaultChunkSize]) - : buffer = Uint8List(bufferSize); - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void skip(int bytes) { - assert(bytes >= 0 && remainingInBuffer >= bytes); - _bufferOffset += bytes; - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Uint8List viewBytes(int bytes) { - assert(bytes >= 0 && remainingInBuffer >= bytes); - var view = Uint8List.view(buffer.buffer, _bufferOffset, bytes); - _bufferOffset += bytes; - return view; - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Uint8List peekBytes(int bytes) { - assert(bytes >= 0 && remainingInBuffer >= bytes); - return Uint8List.view(buffer.buffer, _bufferOffset, bytes); - } - - /// Not part of public API - Future loadBytes(int bytes) async { - assert(bytes > 0); - var remaining = remainingInBuffer; - if (remaining >= bytes) { - return remaining; - } else { - var oldBuffer = buffer; - if (buffer.length < bytes) { - buffer = Uint8List(bytes); - } - - for (var i = 0; i < remaining; i++) { - buffer[i] = oldBuffer[_bufferOffset + i]; - } - - _bufferOffset = 0; - var readBytes = await file!.readInto(buffer, remaining); - _bufferSize = remaining + readBytes; - _fileOffset += readBytes; - - return _bufferSize; - } - } -} diff --git a/hive/lib/src/io/buffered_file_writer.dart b/hive/lib/src/io/buffered_file_writer.dart deleted file mode 100644 index 2a495ee9e..000000000 --- a/hive/lib/src/io/buffered_file_writer.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'dart:io'; -// for [BytesBuilder] -// ignore: unnecessary_import -import 'dart:typed_data'; - -/// Not part of public API -class BufferedFileWriter { - static const _defaultMaxBufferSize = 64000; - - final RandomAccessFile _file; - - final int _maxBufferSize; - - final _buffer = BytesBuilder(copy: true); - - /// Not part of public API - BufferedFileWriter(this._file, [this._maxBufferSize = _defaultMaxBufferSize]); - - /// Not part of public API - Future write(List bytes) { - _buffer.add(bytes); - if (_buffer.length >= _maxBufferSize) { - return flush(); - } - return Future.value(); - } - - /// Not part of public API - Future flush() { - if (_buffer.isNotEmpty) { - return _file.writeFrom(_buffer.takeBytes()); - } else { - return Future.value(); - } - } -} diff --git a/hive/lib/src/io/frame_io_helper.dart b/hive/lib/src/io/frame_io_helper.dart deleted file mode 100644 index d7cb14760..000000000 --- a/hive/lib/src/io/frame_io_helper.dart +++ /dev/null @@ -1,95 +0,0 @@ -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/frame_helper.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/io/buffered_file_reader.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -class FrameIoHelper extends FrameHelper { - /// Not part of public API - @visibleForTesting - Future openFile(String path) { - return File(path).open(); - } - - /// Not part of public API - @visibleForTesting - Future> readFile(String path) { - return File(path).readAsBytes(); - } - - /// Not part of public API - Future keysFromFile( - String path, Keystore keystore, HiveCipher? cipher) async { - var raf = await openFile(path); - var fileReader = BufferedFileReader(raf); - try { - return await _KeyReader(fileReader).readKeys(keystore, cipher); - } finally { - await raf.close(); - } - } - - /// Not part of public API - Future framesFromFile(String path, Keystore keystore, - TypeRegistry registry, HiveCipher? cipher) async { - var bytes = await readFile(path); - return framesFromBytes(bytes as Uint8List, keystore, registry, cipher); - } -} - -class _KeyReader { - final BufferedFileReader fileReader; - - late BinaryReaderImpl _reader; - - _KeyReader(this.fileReader); - - Future readKeys(Keystore keystore, HiveCipher? cipher) async { - await _load(4); - while (true) { - var frameOffset = fileReader.offset; - - if (_reader.availableBytes < 4) { - var available = await _load(4); - if (available == 0) { - break; - } else if (available < 4) { - return frameOffset; - } - } - - var frameLength = _reader.peekUint32(); - if (_reader.availableBytes < frameLength) { - var available = await _load(frameLength); - if (available < frameLength) return frameOffset; - } - - var frame = await _reader.readFrame( - cipher: cipher, - lazy: true, - frameOffset: frameOffset, - ); - if (frame == null) return frameOffset; - - keystore.insert(frame, notify: false); - - fileReader.skip(frameLength); - } - - return -1; - } - - Future _load(int bytes) async { - var loadedBytes = await fileReader.loadBytes(bytes); - var buffer = fileReader.peekBytes(loadedBytes); - _reader = BinaryReaderImpl(buffer, TypeRegistryImpl.nullImpl); - - return loadedBytes; - } -} diff --git a/hive/lib/src/object/hive_collection.dart b/hive/lib/src/object/hive_collection.dart deleted file mode 100644 index 8595b748e..000000000 --- a/hive/lib/src/object/hive_collection.dart +++ /dev/null @@ -1,25 +0,0 @@ -part of hive; - -/// List containing [HiveObjectMixin]s. -abstract class HiveCollection implements List { - /// The box which contains all the objects in this collection - BoxBase get box; - - /// The keys of all the objects in this collection. - Iterable get keys; - - /// Delete all objects in this collection from Hive. - Future deleteAllFromHive(); - - /// Delete the first object in this collection from Hive. - Future deleteFirstFromHive(); - - /// Delete the last object in this collection from Hive. - Future deleteLastFromHive(); - - /// Delete the object at [index] from Hive. - Future deleteFromHive(int index); - - /// Converts this collection to a Map. - Map toMap(); -} diff --git a/hive/lib/src/object/hive_collection_mixin.dart b/hive/lib/src/object/hive_collection_mixin.dart deleted file mode 100644 index 86890f016..000000000 --- a/hive/lib/src/object/hive_collection_mixin.dart +++ /dev/null @@ -1,41 +0,0 @@ -import 'package:hive/hive.dart'; - -/// Implemetation of [HiveCollection]. -abstract class HiveCollectionMixin - implements HiveCollection { - @override - Iterable get keys sync* { - for (var value in this) { - yield value.key; - } - } - - @override - Future deleteAllFromHive() { - return box.deleteAll(keys); - } - - @override - Future deleteFirstFromHive() { - return first.delete(); - } - - @override - Future deleteLastFromHive() { - return last.delete(); - } - - @override - Future deleteFromHive(int index) { - return this[index].delete(); - } - - @override - Map toMap() { - var map = {}; - for (var item in this) { - map[item.key] = item; - } - return map; - } -} diff --git a/hive/lib/src/object/hive_list.dart b/hive/lib/src/object/hive_list.dart deleted file mode 100644 index 772915060..000000000 --- a/hive/lib/src/object/hive_list.dart +++ /dev/null @@ -1,17 +0,0 @@ -part of hive; - -/// Allows defining references to other [HiveObjectMixin]s. -@experimental -abstract class HiveList extends HiveCollection - implements List { - /// Create a new HiveList which can contain HiveObjects from [box]. - factory HiveList(Box box, {List? objects}) => - HiveListImpl(box, objects: objects); - - /// Disposes this list. It is important to call this method when the list is - /// no longer used to avoid memory leaks. - void dispose(); - - /// Casts the list to a new HiveList. - HiveList castHiveList(); -} diff --git a/hive/lib/src/object/hive_list_impl.dart b/hive/lib/src/object/hive_list_impl.dart deleted file mode 100644 index d5ef34cf6..000000000 --- a/hive/lib/src/object/hive_list_impl.dart +++ /dev/null @@ -1,173 +0,0 @@ -import 'dart:collection'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:hive/src/object/hive_collection_mixin.dart'; -import 'package:hive/src/object/hive_object.dart'; -import 'package:hive/src/util/delegating_list_view_mixin.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -class HiveListImpl - with HiveCollectionMixin, ListMixin, DelegatingListViewMixin - implements HiveList { - /// Not part of public API - final String boxName; - - final List? _keys; - - HiveInterface _hive = Hive; - - List? _delegate; - - Box? _box; - - bool _invalidated = false; - - bool _disposed = false; - - /// Not part of public API - HiveListImpl(Box box, {List? objects}) - : boxName = box.name, - _keys = null, - _delegate = [], - _box = box { - if (objects != null) { - addAll(objects); - } - } - - /// Not part of public API - HiveListImpl.lazy(this.boxName, List? keys) : _keys = keys; - - @override - Iterable get keys { - if (_delegate == null) { - return _keys!; - } else { - return super.keys; - } - } - - @override - Box get box { - if (_box == null) { - var box = (_hive as HiveImpl).getBoxWithoutCheckInternal(boxName); - if (box == null) { - throw HiveError( - 'To use this list, you have to open the box "$boxName" first.'); - } else if (box is! Box) { - throw HiveError('The box "$boxName" is a lazy box. ' - 'You can only use HiveLists with normal boxes.'); - } else { - _box = box; - } - } - return _box!; - } - - @override - List get delegate { - if (_disposed) { - throw HiveError('HiveList has already been disposed.'); - } - - if (_invalidated) { - var retained = []; - for (var obj in _delegate!) { - if (obj.isInHiveList(this)) { - retained.add(obj); - } - } - _delegate = retained; - _invalidated = false; - } else if (_delegate == null) { - var list = []; - for (var key in _keys!) { - if (box.containsKey(key)) { - var obj = box.get(key) as E; - obj.linkHiveList(this); - list.add(obj); - } - } - _delegate = list; - } - - return _delegate!; - } - - @override - void dispose() { - if (_delegate != null) { - for (var element in _delegate!) { - element.unlinkHiveList(this); - } - _delegate = null; - } - - _disposed = true; - } - - /// Not part of public API - void invalidate() { - if (_delegate != null) { - _invalidated = true; - } - } - - void _checkElementIsValid(E obj) { - if (obj.box != box) { - throw HiveError('HiveObjects needs to be in the box "$boxName".'); - } - } - - @override - set length(int newLength) { - if (newLength < delegate.length) { - for (var i = newLength; i < delegate.length; i++) { - delegate[i].unlinkHiveList(this); - } - } - delegate.length = newLength; - } - - @override - void operator []=(int index, E value) { - _checkElementIsValid(value); - value.linkHiveList(this); - - var oldValue = delegate[index]; - delegate[index] = value; - - oldValue.unlinkHiveList(this); - } - - @override - void add(E element) { - _checkElementIsValid(element); - element.linkHiveList(this); - delegate.add(element); - } - - @override - void addAll(Iterable iterable) { - for (var element in iterable) { - _checkElementIsValid(element); - element.linkHiveList(this); - } - delegate.addAll(iterable); - } - - @override - HiveList castHiveList() { - if (_delegate != null) { - return HiveListImpl(box, objects: _delegate!.cast()); - } else { - return HiveListImpl.lazy(boxName, _keys); - } - } - - /// Not part of public API - @visibleForTesting - set debugHive(HiveInterface hive) => _hive = hive; -} diff --git a/hive/lib/src/object/hive_object.dart b/hive/lib/src/object/hive_object.dart deleted file mode 100644 index 7fcaa0281..000000000 --- a/hive/lib/src/object/hive_object.dart +++ /dev/null @@ -1,61 +0,0 @@ -library hive_object_internal; - -import 'package:hive/hive.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:meta/meta.dart'; - -part 'hive_object_internal.dart'; - -/// Extend `HiveObject` to add useful methods to the objects you want to store -/// in Hive -mixin HiveObjectMixin { - BoxBase? _box; - - dynamic _key; - - // HiveLists containing this object - final _hiveLists = {}; - - /// Get the box in which this object is stored. Returns `null` if object has - /// not been added to a box yet. - BoxBase? get box => _box; - - /// Get the key associated with this object. Returns `null` if object has - /// not been added to a box yet. - dynamic get key => _key; - - void _requireInitialized() { - if (_box == null) { - throw HiveError('This object is currently not in a box.'); - } - } - - /// Persists this object. - Future save() { - _requireInitialized(); - return _box!.put(_key, this); - } - - /// Deletes this object from the box it is stored in. - Future delete() { - _requireInitialized(); - return _box!.delete(_key); - } - - /// Returns whether this object is currently stored in a box. - /// - /// For lazy boxes this only checks if the key exists in the box and NOT - /// whether this instance is actually stored in the box. - bool get isInBox { - if (_box != null) { - if (_box!.lazy) { - return _box!.containsKey(_key); - } else { - return true; - } - } - return false; - } -} - -abstract class HiveObject with HiveObjectMixin {} diff --git a/hive/lib/src/object/hive_object_internal.dart b/hive/lib/src/object/hive_object_internal.dart deleted file mode 100644 index d3f322c14..000000000 --- a/hive/lib/src/object/hive_object_internal.dart +++ /dev/null @@ -1,57 +0,0 @@ -part of hive_object_internal; - -/// Not part of public API -extension HiveObjectInternal on HiveObjectMixin { - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void init(dynamic key, BoxBase box) { - if (_box != null) { - if (_box != box) { - throw HiveError('The same instance of an HiveObject cannot ' - 'be stored in two different boxes.'); - } else if (_key != key) { - throw HiveError('The same instance of an HiveObject cannot ' - 'be stored with two different keys ("$_key" and "$key").'); - } - } - _box = box; - _key = key; - } - - /// Not part of public API - void dispose() { - for (var list in _hiveLists.keys) { - (list as HiveListImpl).invalidate(); - } - - _hiveLists.clear(); - - _box = null; - _key = null; - } - - /// Not part of public API - void linkHiveList(HiveList list) { - _requireInitialized(); - _hiveLists[list] = (_hiveLists[list] ?? 0) + 1; - } - - /// Not part of public API - void unlinkHiveList(HiveList list) { - final currentIndex = _hiveLists[list]!; - final newIndex = _hiveLists[list] = currentIndex - 1; - if (newIndex <= 0) { - _hiveLists.remove(list); - } - } - - /// Not part of public API - bool isInHiveList(HiveList list) { - return _hiveLists.containsKey(list); - } - - /// Not part of public API - @visibleForTesting - Map get debugHiveLists => _hiveLists; -} diff --git a/hive/lib/src/object/hive_storage_backend_preference.dart b/hive/lib/src/object/hive_storage_backend_preference.dart deleted file mode 100644 index 5ba9484db..000000000 --- a/hive/lib/src/object/hive_storage_backend_preference.dart +++ /dev/null @@ -1,90 +0,0 @@ -part of hive; - -/// converts a stringifyed, obfuscated [StackTrace] into a [StackTrace] -typedef WebWorkerStackTraceCallback = FutureOr Function( - String obfuscatedStackTrace); - -/// declares the preferred JS StorageBackend to be used -/// -/// - [HiveStorageBackendPreference.native] causes almost no startup delay while -/// being slow with huge DB transactions -/// - [HiveStorageBackendPreference.webWorker] has got a small startup delay but -/// is much quicker with huge DB transactions -abstract class HiveStorageBackendPreference { - // this pretty-much represents an enum with constructor options - const HiveStorageBackendPreference._(); - - /// runs the DB transaction the the main thread - static const HiveStorageBackendPreference native = - _HiveStorageBackendPreferenceNative._(); - - /// runs the DB in RAM - /// - /// Caution: DB will *not* persist - static const HiveStorageBackendPreference memory = - _HiveStorageBackendPreferenceMemory._(); - - /// uses a web worker for DB transactions - /// - /// the [path] points to the compiled `web_worker.dart.js` file calling - /// [startWebWorker]. - /// - /// the [onStackTrace] callback provides a stringifyed [StackTrace] in case - /// the web worker encounters an error. Together with the source map (the - /// `.dart.js` file), this can be used to properly capture errors in the web - /// worker. - /// - /// Recommended packages on pub.dev: - /// - [source_map_stack_trace](https://pub.dev/source_map_stack_trace) - /// - [source_maps](https://pub.dev/source_maps) - /// - /// Default to: - /// [HiveStorageBackendPreferenceWebWorker.defaultStackTraceHandler] - factory HiveStorageBackendPreference.webWorker( - String path, { - WebWorkerStackTraceCallback onStackTrace = - HiveStorageBackendPreferenceWebWorker.defaultStackTraceHandler, - }) => - HiveStorageBackendPreferenceWebWorker._(path, onStackTrace: onStackTrace); - - @override - int get hashCode => runtimeType.hashCode; - - @override - bool operator ==(Object other) => hashCode == other.hashCode; -} - -class _HiveStorageBackendPreferenceNative extends HiveStorageBackendPreference { - const _HiveStorageBackendPreferenceNative._() : super._(); -} - -class _HiveStorageBackendPreferenceMemory extends HiveStorageBackendPreference { - const _HiveStorageBackendPreferenceMemory._() : super._(); -} - -class HiveStorageBackendPreferenceWebWorker - extends HiveStorageBackendPreference { - /// the default handler for stackTraces in web workers - static StackTrace defaultStackTraceHandler(String obfuscatedStackTrace) { - return StackTrace.fromString(obfuscatedStackTrace); - } - - /// the [String] of the web worker path - final String path; - - /// a handler converting stringigyed stack traces into a [StackTrace] - final WebWorkerStackTraceCallback onStackTrace; - - const HiveStorageBackendPreferenceWebWorker._(this.path, - {required this.onStackTrace}) - : super._(); - - @override - String toString() => 'HiveStorageBackendPreferenceWebWorker( $path )'; - - @override - int get hashCode => path.hashCode; - - @override - bool operator ==(Object other) => hashCode == other.hashCode; -} diff --git a/hive/lib/src/registry/type_adapter.dart b/hive/lib/src/registry/type_adapter.dart deleted file mode 100644 index 0af4416d7..000000000 --- a/hive/lib/src/registry/type_adapter.dart +++ /dev/null @@ -1,14 +0,0 @@ -part of hive; - -/// Type adapters can be implemented to support non primitive values. -@immutable -abstract class TypeAdapter { - /// Called for type registration - int get typeId; - - /// Is called when a value has to be decoded. - T read(BinaryReader reader); - - /// Is called when a value has to be encoded. - void write(BinaryWriter writer, T obj); -} diff --git a/hive/lib/src/registry/type_registry.dart b/hive/lib/src/registry/type_registry.dart deleted file mode 100644 index 83a0361c0..000000000 --- a/hive/lib/src/registry/type_registry.dart +++ /dev/null @@ -1,25 +0,0 @@ -part of hive; - -/// TypeRegistries contain the [TypeAdapter]s associated with a typeId. -/// -/// TypeIds have to be unique and must not change. -abstract class TypeRegistry { - /// Register a [TypeAdapter] to announce it to Hive. - /// - /// Tries to register [adapter] to registry. If another adapter with same - /// typeId had been already registered an exception will thrown or the adapter - /// will be overridden if [override] set to `true`. Please note that internal - /// adapters are registered and maintained by hive itself. Use [internal] - /// parameter only if you want to override existing adapter implementation. - void registerAdapter( - TypeAdapter adapter, { - bool internal = false, - bool override = false, - }); - - /// Returns true if a [TypeAdapter] is registered - bool isAdapterRegistered(int typeId); - - /// Ignore type - void ignoreTypeId(int typeId); -} diff --git a/hive/lib/src/registry/type_registry_impl.dart b/hive/lib/src/registry/type_registry_impl.dart deleted file mode 100644 index 267fbff64..000000000 --- a/hive/lib/src/registry/type_registry_impl.dart +++ /dev/null @@ -1,143 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/adapters/ignored_type_adapter.dart'; -import 'package:meta/meta.dart'; - -/// Not part of public API -/// -/// Needed to codegen the TypeRegistry mock -@visibleForTesting -class ResolvedAdapter { - final TypeAdapter adapter; - final int typeId; - - ResolvedAdapter(this.adapter, this.typeId); - - bool matchesRuntimeType(dynamic value) => value.runtimeType == T; - - bool matchesType(dynamic value) => value is T; -} - -class _NullTypeRegistry implements TypeRegistryImpl { - const _NullTypeRegistry(); - - @override - Never get _typeAdapters => throw UnimplementedError(); - - @override - Never findAdapterForTypeId(int typeId) => throw UnimplementedError(); - - @override - Never findAdapterForValue(value) => throw UnimplementedError(); - - @override - Never ignoreTypeId(int typeId) => throw UnimplementedError(); - - @override - Never isAdapterRegistered(int typeId, {bool internal = false}) => - throw UnimplementedError(); - - @override - Never registerAdapter(TypeAdapter adapter, - {bool internal = false, bool override = false}) => - throw UnimplementedError(); - - @override - Never resetAdapters() => throw UnimplementedError(); -} - -/// Not part of public API -class TypeRegistryImpl implements TypeRegistry { - /// Not part of public API - static const TypeRegistryImpl nullImpl = _NullTypeRegistry(); - - /// Not part of public API - @visibleForTesting - static const reservedTypeIds = 32; - - final _typeAdapters = {}; - - /// Not part of public API - ResolvedAdapter? findAdapterForValue(dynamic value) { - ResolvedAdapter? match; - for (var adapter in _typeAdapters.values) { - if (adapter.matchesRuntimeType(value)) { - return adapter; - } - if (adapter.matchesType(value) && match == null) { - match = adapter; - } - } - return match; - } - - /// Not part of public API - ResolvedAdapter? findAdapterForTypeId(int typeId) { - return _typeAdapters[typeId]; - } - - @override - void registerAdapter( - TypeAdapter adapter, { - bool internal = false, - bool override = false, - }) { - if (T == dynamic || T == Object) { - print( - 'Registering type adapters for dynamic type is must be avoided, ' - 'otherwise all the write requests to Hive will be handled by given ' - 'adapter. Please explicitly provide adapter type on registerAdapter ' - 'method to avoid this kind of issues. For example if you want to ' - 'register MyTypeAdapter for MyType class you can call like this: ' - 'registerAdapter(MyTypeAdapter())', - ); - } - var typeId = adapter.typeId; - if (!internal) { - if (typeId < 0 || typeId > 223) { - throw HiveError('TypeId $typeId not allowed.'); - } - typeId = typeId + reservedTypeIds; - - var oldAdapter = findAdapterForTypeId(typeId); - if (oldAdapter != null) { - if (override) { - print( - 'You are trying to override ${oldAdapter.runtimeType.toString()}' - 'with ${adapter.runtimeType.toString()} for typeId: ' - '${adapter.typeId}. Please note that overriding adapters might ' - 'cause weird errors. Try to avoid overriding adapters unless not ' - 'required.', - ); - } else { - throw HiveError('There is already a TypeAdapter for ' - 'typeId ${typeId - reservedTypeIds}.'); - } - } - } - - var resolved = ResolvedAdapter(adapter, typeId); - _typeAdapters[typeId] = resolved; - } - - @override - bool isAdapterRegistered(int typeId, {bool internal = false}) { - if (!internal) { - if (typeId < 0 || typeId > 223) { - throw HiveError('TypeId $typeId not allowed.'); - } - - typeId = typeId + reservedTypeIds; - } - - return findAdapterForTypeId(typeId) != null; - } - - void resetAdapters() { - _typeAdapters.clear(); - } - - @override - void ignoreTypeId(int typeId) { - registerAdapter(IgnoredTypeAdapter(typeId)); - } -} diff --git a/hive/lib/src/util/delegating_list_view_mixin.dart b/hive/lib/src/util/delegating_list_view_mixin.dart deleted file mode 100644 index 43cc86c54..000000000 --- a/hive/lib/src/util/delegating_list_view_mixin.dart +++ /dev/null @@ -1,140 +0,0 @@ -import 'package:meta/meta.dart'; - -/// Not part of public API -abstract class DelegatingListViewMixin implements List { - /// Not part of public API - @protected - @visibleForTesting - List get delegate; - - @override - E get first => delegate.first; - - @override - E get last => delegate.last; - - @override - int get length => delegate.length; - - @override - List operator +(List other) => delegate + other; - - @override - E operator [](int index) => delegate[index]; - - @override - bool any(bool Function(E element) test) => delegate.any(test); - - @override - Map asMap() => delegate.asMap(); - - @override - List cast() => delegate.cast(); - - @override - bool contains(Object? element) => delegate.contains(element); - - @override - E elementAt(int index) => delegate.elementAt(index); - - @override - bool every(bool Function(E element) test) => delegate.every(test); - - @override - Iterable expand(Iterable Function(E element) f) => - delegate.expand(f); - - @override - E firstWhere(bool Function(E element) test, {E Function()? orElse}) => - delegate.firstWhere(test, orElse: orElse); - - @override - T fold(T initialValue, T Function(T previousValue, E element) combine) => - delegate.fold(initialValue, combine); - - @override - Iterable followedBy(Iterable other) => delegate.followedBy(other); - - @override - void forEach(void Function(E element) f) => delegate.forEach(f); - - @override - Iterable getRange(int start, int end) => delegate.getRange(start, end); - - @override - int indexOf(Object? element, [int start = 0]) => - delegate.indexOf(element as E, start); - - @override - int indexWhere(bool Function(E element) test, [int start = 0]) => - delegate.indexWhere(test, start); - - @override - bool get isEmpty => delegate.isEmpty; - - @override - bool get isNotEmpty => delegate.isNotEmpty; - - @override - Iterator get iterator => delegate.iterator; - - @override - String join([String separator = '']) => delegate.join(separator); - - @override - int lastIndexOf(Object? element, [int? start]) => - delegate.lastIndexOf(element as E, start); - - @override - int lastIndexWhere(bool Function(E element) test, [int? start]) => - delegate.lastIndexWhere(test, start); - - @override - E lastWhere(bool Function(E element) test, {E Function()? orElse}) => - delegate.lastWhere(test, orElse: orElse); - - @override - Iterable map(T Function(E e) f) => delegate.map(f); - - @override - E reduce(E Function(E value, E element) combine) => delegate.reduce(combine); - - @override - Iterable get reversed => delegate.reversed; - - @override - E get single => delegate.single; - - @override - E singleWhere(bool Function(E element) test, {E Function()? orElse}) => - delegate.singleWhere(test, orElse: orElse); - - @override - Iterable skip(int count) => delegate.skip(count); - - @override - Iterable skipWhile(bool Function(E value) test) => - delegate.skipWhile(test); - - @override - List sublist(int start, [int? end]) => delegate.sublist(start, end); - - @override - Iterable take(int count) => delegate.take(count); - - @override - Iterable takeWhile(bool Function(E value) test) => - delegate.takeWhile(test); - - @override - List toList({bool growable = true}) => delegate.toList(growable: growable); - - @override - Set toSet() => delegate.toSet(); - - @override - Iterable where(bool Function(E element) test) => delegate.where(test); - - @override - Iterable whereType() => delegate.whereType(); -} diff --git a/hive/lib/src/util/extensions.dart b/hive/lib/src/util/extensions.dart deleted file mode 100644 index 202151c43..000000000 --- a/hive/lib/src/util/extensions.dart +++ /dev/null @@ -1,58 +0,0 @@ -import 'dart:math'; -import 'dart:typed_data'; - -/// Not part of public API -extension StringX on String { - /// Not part of public API - bool get isAscii { - for (var cu in codeUnits) { - if (cu > 127) return false; - } - return true; - } -} - -/// Not part of public API -extension ListIntX on List { - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - int readUint32(int offset) { - return this[offset] | - this[offset + 1] << 8 | - this[offset + 2] << 16 | - this[offset + 3] << 24; - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - void writeUint32(int offset, int value) { - this[offset] = value; - this[offset + 1] = value >> 8; - this[offset + 2] = value >> 16; - this[offset + 3] = value >> 24; - } -} - -/// Not part of public API -extension Uint8ListX on Uint8List { - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Uint8List view(int offset, int bytes) { - return Uint8List.view(buffer, offsetInBytes + offset, bytes); - } -} - -/// Not part of public API -extension RandomX on Random { - /// Not part of public API - Uint8List nextBytes(int bytes) { - var buffer = Uint8List(bytes); - for (var i = 0; i < bytes; i++) { - buffer[i] = nextInt(0xFF + 1); - } - return buffer; - } -} diff --git a/hive/lib/src/util/indexable_skip_list.dart b/hive/lib/src/util/indexable_skip_list.dart deleted file mode 100644 index 4cd86739d..000000000 --- a/hive/lib/src/util/indexable_skip_list.dart +++ /dev/null @@ -1,277 +0,0 @@ -import 'dart:collection'; -import 'dart:math'; - -/// Not part of public API -class IndexableSkipList { - static const _maxHeight = 12; - - final _Node _head = _Node( - null, - null, - List.filled(_maxHeight, null), - List.filled(_maxHeight, 0), - ); - - final Random _random; - - final Comparator _comparator; - - int _height = 1; - - int _length = 0; - - /// Not part of public API - IndexableSkipList(this._comparator, [Random? random]) - : _random = random ?? Random(); - - /// Not part of public API - int get length => _length; - - /// Not part of public API - Iterable get keys => _KeyIterable(_head); - - /// Not part of public API - Iterable get values => _ValueIterable(_head); - - /// Not part of public API - V? insert(K key, V? value) { - var existingNode = _getNode(key); - if (existingNode != null) { - var oldValue = existingNode.value; - existingNode.value = value; - return oldValue; - } - - // calculate this new node's level - var newLevel = 0; - while (_random.nextBool() && newLevel < _maxHeight - 1) { - newLevel++; - } - if (newLevel >= _height) { - newLevel = _height++; - } - - var newNode = _Node( - key, - value, - List.filled(newLevel + 1, null), - List.filled(newLevel + 1, 0), - ); - - var current = _head; - // Next & Down - for (var level = _height - 1; level >= 0; level--) { - while (true) { - var next = current.next[level]; - if (next == null || _comparator(key, next.key!) < 0) break; - current = next; - } - - // CHANGE 1 - Increase all the above node's width by 1 - if (level > newLevel) { - var next = current.next[level]; - if (next != null) { - next.width[level]++; - } - continue; - } - - if (level == 0) { - // CHANGE 2 - Nodes at level 0 always have a width of 1 - newNode.width[0] = 1; - } else { - // CHANGE 3 - Calculate the width of the level - var width = 0; - var node = current.next[level - 1]; - while (node != null && _comparator(key, node.key!) >= 0) { - width += node.width[level - 1]; - node = node.next[level - 1]; - } - - for (var j = level; j <= newLevel; j++) { - newNode.width[j] += width; - } - newNode.width[level] += 1; - } - - // Insert new node at the correct position in this level - newNode.next[level] = current.next[level]; - current.next[level] = newNode; - } - - // CHANGE 4 - Adjust the width of all next nodes - for (var i = 1; i <= newLevel; i++) { - var next = newNode.next[i]; - if (next != null) { - next.width[i] -= newNode.width[i] - 1; - } - } - - _length++; - return null; - } - - /// Not part of public API - V? delete(K key) { - var node = _getNode(key); - if (node == null) return null; - - var current = _head; - // Next & Down - for (var level = _height - 1; level >= 0; level--) { - while (true) { - var next = current.next[level]; - if (next == null || _comparator(key, next.key!) <= 0) break; - current = next; - } - - if (level > node.level) { - var next = current.next[level]; - if (next != null) { - next.width[level]--; - } - } else { - var next = node.next[level]; - current.next[level] = next; - if (next != null) { - next.width[level] += node.width[level] - 1; - } - } - } - - if (node.level == _height - 1 && - _height > 1 && - _head.next[node.level] == null) { - _height--; - } - - _length--; - return node.value; - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - V? get(K key) => _getNode(key)?.value; - - /// Not part of public API - // TODO: write test - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Iterable valuesFromKey(K key) { - var node = _getNode(key); - var virtualHead = _Node(null, null, [node], [0]); - return _ValueIterable(virtualHead); - } - - _Node? _getNode(K key) { - var prev = _head; - _Node? node; - for (var i = _height - 1; i >= 0; i--) { - node = prev.next[i]; - - while (node != null && _comparator(key, node.key!) > 0) { - prev = node; - node = node.next[i]; - } - } - - if (node != null && _comparator(key, node.key!) == 0) { - return node; - } - return null; - } - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - V? getAt(int index) => _getNodeAt(index).value; - - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - K? getKeyAt(int index) => _getNodeAt(index).key; - - _Node _getNodeAt(int index) { - RangeError.checkValidIndex(index, this); - - var prev = _head; - _Node? node; - for (var level = _height - 1; level >= 0; level--) { - node = prev.next[level]; - - while (node != null && index >= node.width[level]) { - index -= node.width[level]; - prev = node; - node = node.next[level]; - } - } - - return node!; - } - - /// Not part of public API - void clear() { - _height = 1; - for (var i = 0; i < _maxHeight; i++) { - _head.next[i] = null; - } - _height = 1; - _length = 0; - } -} - -class _Node { - final K? key; - - V? value; - - final List<_Node?> next; - - final List width; - - int get level => next.length - 1; - - _Node(this.key, this.value, this.next, this.width); -} - -abstract class _Iterator implements Iterator { - _Node? node; - - _Iterator(this.node); - - @override - bool moveNext() => (node = node!.next[0]) != null; -} - -class _KeyIterator extends _Iterator { - _KeyIterator(_Node node) : super(node); - - @override - K get current => node!.key!; -} - -class _KeyIterable extends IterableBase { - final _Node head; - - _KeyIterable(this.head); - - @override - Iterator get iterator => _KeyIterator(head); -} - -class _ValueIterator extends _Iterator { - _ValueIterator(_Node node) : super(node); - - @override - V get current => node!.value!; -} - -class _ValueIterable extends IterableBase { - final _Node head; - - _ValueIterable(this.head); - - @override - Iterator get iterator => _ValueIterator(head); -} diff --git a/hive/test/assets/.gitignore b/hive/test/assets/.gitignore deleted file mode 100644 index 51ffc8355..000000000 --- a/hive/test/assets/.gitignore +++ /dev/null @@ -1 +0,0 @@ -!*.hive \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/hive_file/after/testBox.hive b/hive/test/assets/findHiveFileAndCleanUp/hive_file/after/testBox.hive deleted file mode 100644 index afcfdb1ab..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/hive_file/after/testBox.hive +++ /dev/null @@ -1 +0,0 @@ -Just a Test \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/hive_file/before/testBox.hive b/hive/test/assets/findHiveFileAndCleanUp/hive_file/before/testBox.hive deleted file mode 100644 index afcfdb1ab..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/hive_file/before/testBox.hive +++ /dev/null @@ -1 +0,0 @@ -Just a Test \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/after/testBox.hive b/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/after/testBox.hive deleted file mode 100644 index 6adb2bbc1..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/after/testBox.hive +++ /dev/null @@ -1 +0,0 @@ -This is a test box... \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hive b/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hive deleted file mode 100644 index 6adb2bbc1..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hive +++ /dev/null @@ -1 +0,0 @@ -This is a test box... \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hivec b/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hivec deleted file mode 100644 index 7afc03333..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/hive_file_and_compact_file/before/testBox.hivec +++ /dev/null @@ -1 +0,0 @@ -A compacted hive file. \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/somethingElse.hive b/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/somethingElse.hive deleted file mode 100644 index bfae821ff..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/somethingElse.hive +++ /dev/null @@ -1 +0,0 @@ -12341234 \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/testBox.hive b/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/after/testBox.hive deleted file mode 100644 index e69de29bb..000000000 diff --git a/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/before/somethingElse.hive b/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/before/somethingElse.hive deleted file mode 100644 index bfae821ff..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/no_hive_file/before/somethingElse.hive +++ /dev/null @@ -1 +0,0 @@ -12341234 \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/only_compact_file/after/testBox.hive b/hive/test/assets/findHiveFileAndCleanUp/only_compact_file/after/testBox.hive deleted file mode 100644 index 7f6ec1ad9..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/only_compact_file/after/testBox.hive +++ /dev/null @@ -1 +0,0 @@ -This compact file should become a hive file. \ No newline at end of file diff --git a/hive/test/assets/findHiveFileAndCleanUp/only_compact_file/before/testBox.hivec b/hive/test/assets/findHiveFileAndCleanUp/only_compact_file/before/testBox.hivec deleted file mode 100644 index 7f6ec1ad9..000000000 --- a/hive/test/assets/findHiveFileAndCleanUp/only_compact_file/before/testBox.hivec +++ /dev/null @@ -1 +0,0 @@ -This compact file should become a hive file. \ No newline at end of file diff --git a/hive/test/generated/frame_values.g.dart b/hive/test/generated/frame_values.g.dart deleted file mode 100644 index 41d1cf30c..000000000 --- a/hive/test/generated/frame_values.g.dart +++ /dev/null @@ -1,733 +0,0 @@ -import 'dart:typed_data'; - -final frameValuesBytes = [ -// 0 - Uint8List.fromList([0]), -// 555 - Uint8List.fromList([0]), -// 123 - Uint8List.fromList([0]), -// 0 - Uint8List.fromList([4, 8, 0, 0, 0, 73, 110, 116, 32, 107, 101, 121, 49]), -// 1 - Uint8List.fromList([4, 8, 0, 0, 0, 73, 110, 116, 32, 107, 101, 121, 50]), -// 29 - Uint8List.fromList([4, 8, 0, 0, 0, 73, 110, 116, 32, 107, 101, 121, 51]), -// Tombstone frame - Uint8List.fromList([0]), -// Null frame - Uint8List.fromList([0]), -// Int - Uint8List.fromList([1, 0, 0, 0, 204, 214, 90, 157, 65]), -// Large int - Uint8List.fromList([1, 0, 0, 0, 0, 0, 0, 65, 64]), -// Bool true - Uint8List.fromList([3, 1]), -// Bool false - Uint8List.fromList([3, 0]), -// Float - Uint8List.fromList([2, 10, 129, 92, 226, 126, 12, 200, 64]), -// Unicode string - Uint8List.fromList([ - 4, - 80, - 0, - 0, - 0, - 65, - 32, - 102, - 101, - 119, - 32, - 99, - 104, - 97, - 114, - 97, - 99, - 116, - 101, - 114, - 115, - 32, - 119, - 104, - 105, - 99, - 104, - 32, - 97, - 114, - 101, - 32, - 110, - 111, - 116, - 32, - 65, - 83, - 67, - 73, - 73, - 58, - 32, - 240, - 159, - 135, - 181, - 240, - 159, - 135, - 172, - 32, - 240, - 159, - 152, - 128, - 32, - 240, - 159, - 144, - 157, - 32, - 234, - 177, - 159, - 32, - 239, - 188, - 132, - 32, - 228, - 185, - 189, - 32, - 240, - 159, - 145, - 168, - 226, - 128, - 141, - 240, - 159, - 154, - 128 - ]), -// Empty list - Uint8List.fromList([10, 0, 0, 0, 0]), -// Byte list - Uint8List.fromList([5, 4, 0, 0, 0, 1, 12, 123, 210]), -// Byte list with mask - Uint8List.fromList([5, 5, 0, 0, 0, 144, 169, 1, 2, 3]), -// Int list - Uint8List.fromList([ - 6, - 3, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 192, - 94, - 64, - 0, - 0, - 0, - 0, - 0, - 128, - 124, - 64, - 0, - 0, - 0, - 120, - 245, - 212, - 158, - 65 - ]), -// Bool list - Uint8List.fromList([8, 4, 0, 0, 0, 1, 0, 0, 1]), -// Double list - Uint8List.fromList([ - 7, - 5, - 0, - 0, - 0, - 246, - 227, - 64, - 89, - 66, - 88, - 36, - 64, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 127, - 255, - 255, - 255, - 255, - 255, - 255, - 239, - 127, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 255 - ]), -// String list - Uint8List.fromList([ - 9, - 6, - 0, - 0, - 0, - 5, - 0, - 0, - 0, - 104, - 101, - 108, - 108, - 111, - 40, - 0, - 0, - 0, - 240, - 159, - 167, - 153, - 226, - 128, - 141, - 226, - 153, - 130, - 239, - 184, - 143, - 32, - 240, - 159, - 145, - 168, - 226, - 128, - 141, - 240, - 159, - 145, - 168, - 226, - 128, - 141, - 240, - 159, - 145, - 167, - 226, - 128, - 141, - 240, - 159, - 145, - 166, - 32, - 24, - 0, - 0, - 0, - 32, - 239, - 187, - 172, - 32, - 239, - 187, - 173, - 32, - 239, - 187, - 174, - 32, - 239, - 187, - 175, - 32, - 239, - 187, - 176, - 32, - 239, - 187, - 177, - 12, - 0, - 0, - 0, - 224, - 180, - 133, - 32, - 224, - 180, - 134, - 32, - 224, - 180, - 135, - 32, - 17, - 0, - 0, - 0, - 32, - 239, - 173, - 134, - 32, - 239, - 173, - 135, - 32, - 239, - 173, - 136, - 32, - 239, - 173, - 137, - 32, - 32, - 0, - 0, - 0, - 239, - 189, - 169, - 32, - 239, - 189, - 170, - 32, - 239, - 189, - 171, - 32, - 239, - 189, - 172, - 32, - 239, - 189, - 173, - 32, - 239, - 189, - 174, - 32, - 239, - 189, - 175, - 32, - 239, - 189, - 176, - 32 - ]), -// List with null - Uint8List.fromList([ - 10, - 5, - 0, - 0, - 0, - 4, - 4, - 0, - 0, - 0, - 84, - 104, - 105, - 115, - 4, - 2, - 0, - 0, - 0, - 105, - 115, - 4, - 1, - 0, - 0, - 0, - 97, - 4, - 4, - 0, - 0, - 0, - 116, - 101, - 115, - 116, - 0 - ]), -// List with different types - Uint8List.fromList([ - 10, - 6, - 0, - 0, - 0, - 4, - 4, - 0, - 0, - 0, - 76, - 105, - 115, - 116, - 6, - 3, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 63, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 64, - 0, - 0, - 0, - 0, - 0, - 0, - 8, - 64, - 2, - 51, - 51, - 51, - 51, - 51, - 51, - 23, - 64, - 3, - 1, - 1, - 0, - 0, - 0, - 64, - 254, - 137, - 103, - 65, - 11, - 2, - 0, - 0, - 0, - 4, - 1, - 0, - 0, - 0, - 116, - 3, - 1, - 4, - 1, - 0, - 0, - 0, - 102, - 3, - 0 - ]), -// Map - Uint8List.fromList([ - 11, - 7, - 0, - 0, - 0, - 4, - 4, - 0, - 0, - 0, - 66, - 111, - 111, - 108, - 3, - 1, - 4, - 3, - 0, - 0, - 0, - 73, - 110, - 116, - 1, - 0, - 0, - 0, - 0, - 0, - 72, - 147, - 64, - 4, - 6, - 0, - 0, - 0, - 68, - 111, - 117, - 98, - 108, - 101, - 2, - 102, - 102, - 102, - 102, - 102, - 102, - 47, - 64, - 4, - 6, - 0, - 0, - 0, - 83, - 116, - 114, - 105, - 110, - 103, - 4, - 5, - 0, - 0, - 0, - 72, - 101, - 108, - 108, - 111, - 4, - 4, - 0, - 0, - 0, - 76, - 105, - 115, - 116, - 10, - 3, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 63, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 64, - 0, - 4, - 4, - 0, - 0, - 0, - 78, - 117, - 108, - 108, - 0, - 4, - 3, - 0, - 0, - 0, - 77, - 97, - 112, - 11, - 2, - 0, - 0, - 0, - 4, - 3, - 0, - 0, - 0, - 75, - 101, - 121, - 4, - 3, - 0, - 0, - 0, - 86, - 97, - 108, - 4, - 4, - 0, - 0, - 0, - 75, - 101, - 121, - 50, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 64 - ]), -// DateTime test - Uint8List.fromList([ - 10, - 2, - 0, - 0, - 0, - 16, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 16, - 0, - 192, - 26, - 47, - 64, - 204, - 118, - 66 - ]), -// BigInt Test - Uint8List.fromList([ - 17, - 40, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48 - ]), -]; diff --git a/hive/test/generated/frame_values_encrypted.g.dart b/hive/test/generated/frame_values_encrypted.g.dart deleted file mode 100644 index 74ce1e11d..000000000 --- a/hive/test/generated/frame_values_encrypted.g.dart +++ /dev/null @@ -1,1506 +0,0 @@ -import 'dart:typed_data'; - -final frameValuesBytesEncrypted = [ -// 0 - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45 - ]), -// 555 - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45 - ]), -// 123 - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45 - ]), -// 0 - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 39, - 52, - 236, - 118, - 160, - 170, - 77, - 53, - 136, - 190, - 175, - 13, - 119, - 91, - 48, - 72 - ]), -// 1 - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 154, - 27, - 49, - 187, - 31, - 92, - 220, - 13, - 154, - 113, - 43, - 85, - 17, - 13, - 147, - 24 - ]), -// 29 - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 93, - 221, - 111, - 93, - 153, - 230, - 104, - 3, - 116, - 177, - 45, - 231, - 102, - 255, - 6, - 109 - ]), -// Tombstone frame - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45 - ]), -// Null frame - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45 - ]), -// Int - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 82, - 198, - 151, - 23, - 112, - 174, - 237, - 78, - 222, - 220, - 78, - 115, - 15, - 194, - 219, - 229 - ]), -// Large int - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 80, - 221, - 135, - 59, - 2, - 74, - 46, - 88, - 85, - 32, - 125, - 127, - 92, - 215, - 26, - 237 - ]), -// Bool true - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 203, - 6, - 35, - 157, - 211, - 201, - 172, - 60, - 80, - 166, - 35, - 211, - 224, - 91, - 108, - 121 - ]), -// Bool false - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 255, - 235, - 95, - 200, - 65, - 58, - 194, - 202, - 156, - 255, - 95, - 219, - 140, - 255, - 51, - 40 - ]), -// Float - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 202, - 28, - 71, - 108, - 114, - 57, - 192, - 220, - 208, - 36, - 166, - 43, - 155, - 145, - 116, - 95 - ]), -// Unicode string - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 205, - 165, - 174, - 224, - 221, - 138, - 211, - 194, - 194, - 145, - 89, - 6, - 232, - 128, - 23, - 195, - 87, - 96, - 142, - 116, - 239, - 43, - 16, - 145, - 119, - 40, - 210, - 163, - 26, - 54, - 204, - 73, - 193, - 17, - 20, - 128, - 240, - 181, - 6, - 123, - 108, - 176, - 121, - 237, - 54, - 107, - 227, - 222, - 154, - 82, - 182, - 136, - 196, - 49, - 154, - 150, - 70, - 170, - 32, - 49, - 40, - 226, - 2, - 4, - 135, - 180, - 160, - 65, - 83, - 58, - 49, - 0, - 81, - 105, - 142, - 25, - 33, - 179, - 219, - 142, - 158, - 227, - 63, - 251, - 130, - 110, - 255, - 233, - 164, - 193, - 118, - 29, - 227, - 191, - 93, - 11 - ]), -// Empty list - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 199, - 194, - 103, - 243, - 41, - 19, - 66, - 102, - 71, - 203, - 97, - 239, - 11, - 171, - 248, - 209 - ]), -// Byte list - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 169, - 179, - 212, - 241, - 244, - 75, - 236, - 54, - 102, - 149, - 243, - 229, - 135, - 112, - 181, - 125 - ]), -// Byte list with mask - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 144, - 130, - 119, - 83, - 23, - 134, - 43, - 66, - 68, - 214, - 224, - 19, - 80, - 99, - 100, - 191 - ]), -// Int list - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 64, - 0, - 126, - 245, - 119, - 226, - 220, - 101, - 189, - 12, - 129, - 196, - 196, - 40, - 151, - 100, - 56, - 99, - 82, - 68, - 219, - 64, - 197, - 11, - 17, - 201, - 198, - 181, - 151, - 177, - 58, - 173 - ]), -// Bool list - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 252, - 21, - 80, - 221, - 182, - 107, - 163, - 156, - 97, - 189, - 148, - 86, - 48, - 169, - 77, - 35 - ]), -// Double list - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 213, - 249, - 15, - 61, - 223, - 183, - 56, - 209, - 66, - 16, - 19, - 234, - 62, - 117, - 173, - 12, - 104, - 235, - 232, - 106, - 44, - 164, - 101, - 41, - 61, - 88, - 32, - 110, - 153, - 203, - 242, - 147, - 100, - 241, - 42, - 67, - 170, - 161, - 212, - 186, - 162, - 66, - 224, - 4, - 226, - 27, - 201, - 176 - ]), -// String list - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 153, - 150, - 253, - 78, - 106, - 101, - 109, - 26, - 206, - 172, - 48, - 41, - 124, - 201, - 248, - 47, - 69, - 93, - 1, - 35, - 207, - 3, - 203, - 213, - 183, - 25, - 62, - 151, - 119, - 159, - 105, - 163, - 32, - 4, - 204, - 87, - 103, - 164, - 36, - 110, - 151, - 251, - 178, - 153, - 145, - 250, - 201, - 153, - 44, - 82, - 73, - 241, - 70, - 157, - 252, - 205, - 207, - 9, - 239, - 149, - 2, - 231, - 0, - 31, - 21, - 224, - 240, - 97, - 211, - 203, - 177, - 85, - 184, - 23, - 191, - 14, - 169, - 104, - 6, - 72, - 200, - 169, - 190, - 201, - 166, - 74, - 169, - 87, - 78, - 130, - 247, - 29, - 95, - 78, - 77, - 110, - 110, - 237, - 93, - 219, - 36, - 157, - 77, - 89, - 21, - 139, - 95, - 140, - 12, - 72, - 176, - 150, - 182, - 115, - 97, - 17, - 106, - 1, - 70, - 143, - 87, - 44, - 136, - 208, - 171, - 82, - 222, - 240, - 252, - 37, - 80, - 158, - 125, - 207, - 248, - 121, - 164, - 224, - 79, - 247, - 122, - 36, - 106, - 109, - 232, - 11, - 10, - 110, - 226, - 2, - 185, - 94, - 35, - 255, - 111, - 93, - 237, - 120, - 1, - 208 - ]), -// List with null - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 225, - 213, - 185, - 248, - 156, - 156, - 222, - 100, - 44, - 8, - 160, - 172, - 158, - 230, - 229, - 149, - 188, - 123, - 223, - 163, - 122, - 78, - 162, - 202, - 142, - 27, - 253, - 165, - 13, - 140, - 65, - 233, - 29, - 92, - 178, - 164, - 228, - 201, - 211, - 12, - 97, - 236, - 110, - 61, - 135, - 56, - 74, - 178 - ]), -// List with different types - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 162, - 44, - 10, - 205, - 135, - 89, - 106, - 78, - 241, - 135, - 17, - 15, - 155, - 107, - 162, - 13, - 81, - 31, - 198, - 131, - 84, - 96, - 234, - 9, - 206, - 118, - 3, - 239, - 65, - 220, - 91, - 112, - 77, - 245, - 236, - 56, - 187, - 27, - 196, - 100, - 214, - 161, - 244, - 51, - 115, - 66, - 150, - 32, - 8, - 19, - 182, - 95, - 197, - 126, - 114, - 233, - 248, - 30, - 120, - 37, - 145, - 189, - 231, - 48, - 70, - 127, - 105, - 232, - 197, - 117, - 169, - 30, - 98, - 43, - 197, - 132, - 88, - 251, - 86, - 227, - 41, - 137, - 115, - 219, - 77, - 226, - 111, - 143, - 54, - 34, - 24, - 37, - 220, - 243, - 244, - 183 - ]), -// Map - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 13, - 37, - 197, - 127, - 137, - 83, - 219, - 85, - 165, - 224, - 139, - 227, - 168, - 247, - 122, - 173, - 159, - 125, - 186, - 212, - 237, - 61, - 184, - 146, - 219, - 212, - 193, - 3, - 131, - 71, - 167, - 69, - 72, - 68, - 61, - 110, - 226, - 177, - 245, - 40, - 22, - 64, - 222, - 206, - 18, - 187, - 144, - 52, - 117, - 219, - 163, - 20, - 172, - 147, - 223, - 24, - 196, - 56, - 90, - 185, - 134, - 127, - 233, - 62, - 232, - 152, - 130, - 0, - 225, - 179, - 251, - 62, - 5, - 56, - 18, - 75, - 114, - 254, - 126, - 88, - 228, - 224, - 98, - 166, - 213, - 81, - 26, - 179, - 113, - 106, - 87, - 10, - 251, - 92, - 247, - 98, - 106, - 46, - 28, - 240, - 129, - 41, - 216, - 128, - 131, - 86, - 69, - 158, - 26, - 228, - 162, - 202, - 130, - 44, - 16, - 171, - 34, - 32, - 155, - 91, - 168, - 50, - 50, - 133, - 140, - 234, - 193, - 8, - 136, - 126, - 30, - 138, - 110, - 138, - 143, - 37, - 15, - 129, - 207, - 48, - 14, - 43, - 118, - 118, - 108, - 26, - 182, - 218, - 53, - 118, - 111, - 14, - 23, - 188, - 115, - 229, - 48, - 9, - 121, - 148, - 87, - 176, - 7, - 44, - 97, - 201, - 216, - 31, - 30, - 237, - 69, - 66, - 56, - 123, - 115, - 17 - ]), -// DateTime test - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 49, - 1, - 86, - 197, - 233, - 67, - 235, - 208, - 152, - 138, - 141, - 53, - 104, - 120, - 4, - 205, - 53, - 148, - 224, - 218, - 111, - 99, - 191, - 123, - 147, - 13, - 68, - 127, - 76, - 87, - 220, - 8 - ]), -// BigInt Test - Uint8List.fromList([ - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 92, - 197, - 108, - 246, - 190, - 129, - 94, - 5, - 62, - 37, - 237, - 35, - 48, - 69, - 227, - 238, - 46, - 181, - 251, - 153, - 42, - 12, - 188, - 181, - 65, - 254, - 237, - 238, - 144, - 189, - 230, - 159, - 77, - 241, - 22, - 110, - 106, - 204, - 207, - 172, - 102, - 136, - 240, - 71, - 172, - 126, - 39, - 159 - ]), -]; diff --git a/hive/test/generated/frames.g.dart b/hive/test/generated/frames.g.dart deleted file mode 100644 index 5c3f06048..000000000 --- a/hive/test/generated/frames.g.dart +++ /dev/null @@ -1,1308 +0,0 @@ -import 'dart:typed_data'; - -final frameBytes = [ -// 0 - Uint8List.fromList([13, 0, 0, 0, 0, 0, 0, 0, 0, 249, 243, 70, 23]), -// 555 - Uint8List.fromList([13, 0, 0, 0, 0, 43, 2, 0, 0, 168, 15, 241, 99]), -// 123 - Uint8List.fromList([14, 0, 0, 0, 0, 123, 0, 0, 0, 0, 27, 137, 65, 115]), -// 0 - Uint8List.fromList([ - 26, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 4, - 8, - 0, - 0, - 0, - 73, - 110, - 116, - 32, - 107, - 101, - 121, - 49, - 168, - 61, - 73, - 225 - ]), -// 1 - Uint8List.fromList([ - 26, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 4, - 8, - 0, - 0, - 0, - 73, - 110, - 116, - 32, - 107, - 101, - 121, - 50, - 81, - 167, - 230, - 255 - ]), -// 29 - Uint8List.fromList([ - 26, - 0, - 0, - 0, - 0, - 29, - 0, - 0, - 0, - 4, - 8, - 0, - 0, - 0, - 73, - 110, - 116, - 32, - 107, - 101, - 121, - 51, - 235, - 102, - 168, - 10 - ]), -// Tombstone frame - Uint8List.fromList([ - 25, - 0, - 0, - 0, - 1, - 15, - 84, - 111, - 109, - 98, - 115, - 116, - 111, - 110, - 101, - 32, - 102, - 114, - 97, - 109, - 101, - 135, - 67, - 191, - 153 - ]), -// Null frame - Uint8List.fromList([ - 21, - 0, - 0, - 0, - 1, - 10, - 78, - 117, - 108, - 108, - 32, - 102, - 114, - 97, - 109, - 101, - 0, - 90, - 63, - 225, - 253 - ]), -// Int - Uint8List.fromList([ - 22, - 0, - 0, - 0, - 1, - 3, - 73, - 110, - 116, - 1, - 0, - 0, - 0, - 204, - 214, - 90, - 157, - 65, - 62, - 110, - 117, - 171 - ]), -// Large int - Uint8List.fromList([ - 28, - 0, - 0, - 0, - 1, - 9, - 76, - 97, - 114, - 103, - 101, - 32, - 105, - 110, - 116, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 65, - 64, - 160, - 183, - 117, - 116 - ]), -// Bool true - Uint8List.fromList([ - 21, - 0, - 0, - 0, - 1, - 9, - 66, - 111, - 111, - 108, - 32, - 116, - 114, - 117, - 101, - 3, - 1, - 222, - 156, - 178, - 82 - ]), -// Bool false - Uint8List.fromList([ - 22, - 0, - 0, - 0, - 1, - 10, - 66, - 111, - 111, - 108, - 32, - 102, - 97, - 108, - 115, - 101, - 3, - 0, - 28, - 198, - 154, - 247 - ]), -// Float - Uint8List.fromList([ - 24, - 0, - 0, - 0, - 1, - 5, - 70, - 108, - 111, - 97, - 116, - 2, - 10, - 129, - 92, - 226, - 126, - 12, - 200, - 64, - 167, - 50, - 33, - 185 - ]), -// Unicode string - Uint8List.fromList([ - 109, - 0, - 0, - 0, - 1, - 14, - 85, - 110, - 105, - 99, - 111, - 100, - 101, - 32, - 115, - 116, - 114, - 105, - 110, - 103, - 4, - 80, - 0, - 0, - 0, - 65, - 32, - 102, - 101, - 119, - 32, - 99, - 104, - 97, - 114, - 97, - 99, - 116, - 101, - 114, - 115, - 32, - 119, - 104, - 105, - 99, - 104, - 32, - 97, - 114, - 101, - 32, - 110, - 111, - 116, - 32, - 65, - 83, - 67, - 73, - 73, - 58, - 32, - 240, - 159, - 135, - 181, - 240, - 159, - 135, - 172, - 32, - 240, - 159, - 152, - 128, - 32, - 240, - 159, - 144, - 157, - 32, - 234, - 177, - 159, - 32, - 239, - 188, - 132, - 32, - 228, - 185, - 189, - 32, - 240, - 159, - 145, - 168, - 226, - 128, - 141, - 240, - 159, - 154, - 128, - 90, - 101, - 86, - 178 - ]), -// Empty list - Uint8List.fromList([ - 25, - 0, - 0, - 0, - 1, - 10, - 69, - 109, - 112, - 116, - 121, - 32, - 108, - 105, - 115, - 116, - 10, - 0, - 0, - 0, - 0, - 45, - 235, - 124, - 36 - ]), -// Byte list - Uint8List.fromList([ - 28, - 0, - 0, - 0, - 1, - 9, - 66, - 121, - 116, - 101, - 32, - 108, - 105, - 115, - 116, - 5, - 4, - 0, - 0, - 0, - 1, - 12, - 123, - 210, - 89, - 126, - 233, - 179 - ]), -// Byte list with mask - Uint8List.fromList([ - 39, - 0, - 0, - 0, - 1, - 19, - 66, - 121, - 116, - 101, - 32, - 108, - 105, - 115, - 116, - 32, - 119, - 105, - 116, - 104, - 32, - 109, - 97, - 115, - 107, - 5, - 5, - 0, - 0, - 0, - 144, - 169, - 1, - 2, - 3, - 71, - 62, - 39, - 158 - ]), -// Int list - Uint8List.fromList([ - 47, - 0, - 0, - 0, - 1, - 8, - 73, - 110, - 116, - 32, - 108, - 105, - 115, - 116, - 6, - 3, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 192, - 94, - 64, - 0, - 0, - 0, - 0, - 0, - 128, - 124, - 64, - 0, - 0, - 0, - 120, - 245, - 212, - 158, - 65, - 52, - 114, - 43, - 173 - ]), -// Bool list - Uint8List.fromList([ - 28, - 0, - 0, - 0, - 1, - 9, - 66, - 111, - 111, - 108, - 32, - 108, - 105, - 115, - 116, - 8, - 4, - 0, - 0, - 0, - 1, - 0, - 0, - 1, - 0, - 111, - 70, - 226 - ]), -// Double list - Uint8List.fromList([ - 66, - 0, - 0, - 0, - 1, - 11, - 68, - 111, - 117, - 98, - 108, - 101, - 32, - 108, - 105, - 115, - 116, - 7, - 5, - 0, - 0, - 0, - 246, - 227, - 64, - 89, - 66, - 88, - 36, - 64, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 127, - 255, - 255, - 255, - 255, - 255, - 255, - 239, - 127, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 255, - 152, - 0, - 20, - 188 - ]), -// String list - Uint8List.fromList([ - 180, - 0, - 0, - 0, - 1, - 11, - 83, - 116, - 114, - 105, - 110, - 103, - 32, - 108, - 105, - 115, - 116, - 9, - 6, - 0, - 0, - 0, - 5, - 0, - 0, - 0, - 104, - 101, - 108, - 108, - 111, - 40, - 0, - 0, - 0, - 240, - 159, - 167, - 153, - 226, - 128, - 141, - 226, - 153, - 130, - 239, - 184, - 143, - 32, - 240, - 159, - 145, - 168, - 226, - 128, - 141, - 240, - 159, - 145, - 168, - 226, - 128, - 141, - 240, - 159, - 145, - 167, - 226, - 128, - 141, - 240, - 159, - 145, - 166, - 32, - 24, - 0, - 0, - 0, - 32, - 239, - 187, - 172, - 32, - 239, - 187, - 173, - 32, - 239, - 187, - 174, - 32, - 239, - 187, - 175, - 32, - 239, - 187, - 176, - 32, - 239, - 187, - 177, - 12, - 0, - 0, - 0, - 224, - 180, - 133, - 32, - 224, - 180, - 134, - 32, - 224, - 180, - 135, - 32, - 17, - 0, - 0, - 0, - 32, - 239, - 173, - 134, - 32, - 239, - 173, - 135, - 32, - 239, - 173, - 136, - 32, - 239, - 173, - 137, - 32, - 32, - 0, - 0, - 0, - 239, - 189, - 169, - 32, - 239, - 189, - 170, - 32, - 239, - 189, - 171, - 32, - 239, - 189, - 172, - 32, - 239, - 189, - 173, - 32, - 239, - 189, - 174, - 32, - 239, - 189, - 175, - 32, - 239, - 189, - 176, - 32, - 27, - 122, - 122, - 169 - ]), -// List with null - Uint8List.fromList([ - 61, - 0, - 0, - 0, - 1, - 14, - 76, - 105, - 115, - 116, - 32, - 119, - 105, - 116, - 104, - 32, - 110, - 117, - 108, - 108, - 10, - 5, - 0, - 0, - 0, - 4, - 4, - 0, - 0, - 0, - 84, - 104, - 105, - 115, - 4, - 2, - 0, - 0, - 0, - 105, - 115, - 4, - 1, - 0, - 0, - 0, - 97, - 4, - 4, - 0, - 0, - 0, - 116, - 101, - 115, - 116, - 0, - 148, - 41, - 18, - 249 - ]), -// List with different types - Uint8List.fromList([ - 119, - 0, - 0, - 0, - 1, - 25, - 76, - 105, - 115, - 116, - 32, - 119, - 105, - 116, - 104, - 32, - 100, - 105, - 102, - 102, - 101, - 114, - 101, - 110, - 116, - 32, - 116, - 121, - 112, - 101, - 115, - 10, - 6, - 0, - 0, - 0, - 4, - 4, - 0, - 0, - 0, - 76, - 105, - 115, - 116, - 6, - 3, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 63, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 64, - 0, - 0, - 0, - 0, - 0, - 0, - 8, - 64, - 2, - 51, - 51, - 51, - 51, - 51, - 51, - 23, - 64, - 3, - 1, - 1, - 0, - 0, - 0, - 64, - 254, - 137, - 103, - 65, - 11, - 2, - 0, - 0, - 0, - 4, - 1, - 0, - 0, - 0, - 116, - 3, - 1, - 4, - 1, - 0, - 0, - 0, - 102, - 3, - 0, - 126, - 221, - 94, - 200 - ]), -// Map - Uint8List.fromList([ - 177, - 0, - 0, - 0, - 1, - 3, - 77, - 97, - 112, - 11, - 7, - 0, - 0, - 0, - 4, - 4, - 0, - 0, - 0, - 66, - 111, - 111, - 108, - 3, - 1, - 4, - 3, - 0, - 0, - 0, - 73, - 110, - 116, - 1, - 0, - 0, - 0, - 0, - 0, - 72, - 147, - 64, - 4, - 6, - 0, - 0, - 0, - 68, - 111, - 117, - 98, - 108, - 101, - 2, - 102, - 102, - 102, - 102, - 102, - 102, - 47, - 64, - 4, - 6, - 0, - 0, - 0, - 83, - 116, - 114, - 105, - 110, - 103, - 4, - 5, - 0, - 0, - 0, - 72, - 101, - 108, - 108, - 111, - 4, - 4, - 0, - 0, - 0, - 76, - 105, - 115, - 116, - 10, - 3, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 240, - 63, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 64, - 0, - 4, - 4, - 0, - 0, - 0, - 78, - 117, - 108, - 108, - 0, - 4, - 3, - 0, - 0, - 0, - 77, - 97, - 112, - 11, - 2, - 0, - 0, - 0, - 4, - 3, - 0, - 0, - 0, - 75, - 101, - 121, - 4, - 3, - 0, - 0, - 0, - 86, - 97, - 108, - 4, - 4, - 0, - 0, - 0, - 75, - 101, - 121, - 50, - 1, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 64, - 253, - 16, - 244, - 158 - ]), -// DateTime test - Uint8List.fromList([ - 46, - 0, - 0, - 0, - 1, - 13, - 68, - 97, - 116, - 101, - 84, - 105, - 109, - 101, - 32, - 116, - 101, - 115, - 116, - 10, - 2, - 0, - 0, - 0, - 16, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 16, - 0, - 192, - 26, - 47, - 64, - 204, - 118, - 66, - 92, - 55, - 96, - 194 - ]), -// BigInt Test - Uint8List.fromList([ - 63, - 0, - 0, - 0, - 1, - 11, - 66, - 105, - 103, - 73, - 110, - 116, - 32, - 84, - 101, - 115, - 116, - 17, - 40, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 49, - 50, - 51, - 52, - 53, - 54, - 55, - 56, - 57, - 48, - 114, - 147, - 35, - 23 - ]), -]; diff --git a/hive/test/generated/frames_encrypted.g.dart b/hive/test/generated/frames_encrypted.g.dart deleted file mode 100644 index d5d5fabb9..000000000 --- a/hive/test/generated/frames_encrypted.g.dart +++ /dev/null @@ -1,1878 +0,0 @@ -import 'dart:typed_data'; - -final frameBytesEncrypted = [ -// 0 - Uint8List.fromList([13, 0, 0, 0, 0, 0, 0, 0, 0, 168, 93, 129, 29]), -// 555 - Uint8List.fromList([13, 0, 0, 0, 0, 43, 2, 0, 0, 249, 161, 54, 105]), -// 123 - Uint8List.fromList([ - 45, - 0, - 0, - 0, - 0, - 123, - 0, - 0, - 0, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45, - 222, - 138, - 43, - 135 - ]), -// 0 - Uint8List.fromList([ - 45, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 0, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 39, - 52, - 236, - 118, - 160, - 170, - 77, - 53, - 136, - 190, - 175, - 13, - 119, - 91, - 48, - 72, - 187, - 116, - 197, - 169 - ]), -// 1 - Uint8List.fromList([ - 45, - 0, - 0, - 0, - 0, - 1, - 0, - 0, - 0, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 154, - 27, - 49, - 187, - 31, - 92, - 220, - 13, - 154, - 113, - 43, - 85, - 17, - 13, - 147, - 24, - 209, - 146, - 38, - 192 - ]), -// 29 - Uint8List.fromList([ - 45, - 0, - 0, - 0, - 0, - 29, - 0, - 0, - 0, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 93, - 221, - 111, - 93, - 153, - 230, - 104, - 3, - 116, - 177, - 45, - 231, - 102, - 255, - 6, - 109, - 226, - 211, - 110, - 85 - ]), -// Tombstone frame - Uint8List.fromList([ - 25, - 0, - 0, - 0, - 1, - 15, - 84, - 111, - 109, - 98, - 115, - 116, - 111, - 110, - 101, - 32, - 102, - 114, - 97, - 109, - 101, - 236, - 91, - 39, - 146 - ]), -// Null frame - Uint8List.fromList([ - 52, - 0, - 0, - 0, - 1, - 10, - 78, - 117, - 108, - 108, - 32, - 102, - 114, - 97, - 109, - 101, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 155, - 148, - 109, - 138, - 118, - 157, - 170, - 18, - 217, - 31, - 31, - 167, - 126, - 0, - 163, - 45, - 203, - 53, - 41, - 16 - ]), -// Int - Uint8List.fromList([ - 45, - 0, - 0, - 0, - 1, - 3, - 73, - 110, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 82, - 198, - 151, - 23, - 112, - 174, - 237, - 78, - 222, - 220, - 78, - 115, - 15, - 194, - 219, - 229, - 83, - 102, - 193, - 183 - ]), -// Large int - Uint8List.fromList([ - 51, - 0, - 0, - 0, - 1, - 9, - 76, - 97, - 114, - 103, - 101, - 32, - 105, - 110, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 80, - 221, - 135, - 59, - 2, - 74, - 46, - 88, - 85, - 32, - 125, - 127, - 92, - 215, - 26, - 237, - 105, - 30, - 86, - 128 - ]), -// Bool true - Uint8List.fromList([ - 51, - 0, - 0, - 0, - 1, - 9, - 66, - 111, - 111, - 108, - 32, - 116, - 114, - 117, - 101, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 203, - 6, - 35, - 157, - 211, - 201, - 172, - 60, - 80, - 166, - 35, - 211, - 224, - 91, - 108, - 121, - 253, - 29, - 16, - 32 - ]), -// Bool false - Uint8List.fromList([ - 52, - 0, - 0, - 0, - 1, - 10, - 66, - 111, - 111, - 108, - 32, - 102, - 97, - 108, - 115, - 101, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 255, - 235, - 95, - 200, - 65, - 58, - 194, - 202, - 156, - 255, - 95, - 219, - 140, - 255, - 51, - 40, - 246, - 245, - 58, - 144 - ]), -// Float - Uint8List.fromList([ - 47, - 0, - 0, - 0, - 1, - 5, - 70, - 108, - 111, - 97, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 202, - 28, - 71, - 108, - 114, - 57, - 192, - 220, - 208, - 36, - 166, - 43, - 155, - 145, - 116, - 95, - 236, - 8, - 206, - 55 - ]), -// Unicode string - Uint8List.fromList([ - 136, - 0, - 0, - 0, - 1, - 14, - 85, - 110, - 105, - 99, - 111, - 100, - 101, - 32, - 115, - 116, - 114, - 105, - 110, - 103, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 205, - 165, - 174, - 224, - 221, - 138, - 211, - 194, - 194, - 145, - 89, - 6, - 232, - 128, - 23, - 195, - 87, - 96, - 142, - 116, - 239, - 43, - 16, - 145, - 119, - 40, - 210, - 163, - 26, - 54, - 204, - 73, - 193, - 17, - 20, - 128, - 240, - 181, - 6, - 123, - 108, - 176, - 121, - 237, - 54, - 107, - 227, - 222, - 154, - 82, - 182, - 136, - 196, - 49, - 154, - 150, - 70, - 170, - 32, - 49, - 40, - 226, - 2, - 4, - 135, - 180, - 160, - 65, - 83, - 58, - 49, - 0, - 81, - 105, - 142, - 25, - 33, - 179, - 219, - 142, - 158, - 227, - 63, - 251, - 130, - 110, - 255, - 233, - 164, - 193, - 118, - 29, - 227, - 191, - 93, - 11, - 158, - 177, - 157, - 66 - ]), -// Empty list - Uint8List.fromList([ - 52, - 0, - 0, - 0, - 1, - 10, - 69, - 109, - 112, - 116, - 121, - 32, - 108, - 105, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 199, - 194, - 103, - 243, - 41, - 19, - 66, - 102, - 71, - 203, - 97, - 239, - 11, - 171, - 248, - 209, - 103, - 237, - 85, - 27 - ]), -// Byte list - Uint8List.fromList([ - 51, - 0, - 0, - 0, - 1, - 9, - 66, - 121, - 116, - 101, - 32, - 108, - 105, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 169, - 179, - 212, - 241, - 244, - 75, - 236, - 54, - 102, - 149, - 243, - 229, - 135, - 112, - 181, - 125, - 201, - 104, - 78, - 21 - ]), -// Byte list with mask - Uint8List.fromList([ - 61, - 0, - 0, - 0, - 1, - 19, - 66, - 121, - 116, - 101, - 32, - 108, - 105, - 115, - 116, - 32, - 119, - 105, - 116, - 104, - 32, - 109, - 97, - 115, - 107, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 144, - 130, - 119, - 83, - 23, - 134, - 43, - 66, - 68, - 214, - 224, - 19, - 80, - 99, - 100, - 191, - 166, - 155, - 69, - 17 - ]), -// Int list - Uint8List.fromList([ - 66, - 0, - 0, - 0, - 1, - 8, - 73, - 110, - 116, - 32, - 108, - 105, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 64, - 0, - 126, - 245, - 119, - 226, - 220, - 101, - 189, - 12, - 129, - 196, - 196, - 40, - 151, - 100, - 56, - 99, - 82, - 68, - 219, - 64, - 197, - 11, - 17, - 201, - 198, - 181, - 151, - 177, - 58, - 173, - 148, - 87, - 51, - 81 - ]), -// Bool list - Uint8List.fromList([ - 51, - 0, - 0, - 0, - 1, - 9, - 66, - 111, - 111, - 108, - 32, - 108, - 105, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 252, - 21, - 80, - 221, - 182, - 107, - 163, - 156, - 97, - 189, - 148, - 86, - 48, - 169, - 77, - 35, - 158, - 218, - 217, - 48 - ]), -// Double list - Uint8List.fromList([ - 85, - 0, - 0, - 0, - 1, - 11, - 68, - 111, - 117, - 98, - 108, - 101, - 32, - 108, - 105, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 213, - 249, - 15, - 61, - 223, - 183, - 56, - 209, - 66, - 16, - 19, - 234, - 62, - 117, - 173, - 12, - 104, - 235, - 232, - 106, - 44, - 164, - 101, - 41, - 61, - 88, - 32, - 110, - 153, - 203, - 242, - 147, - 100, - 241, - 42, - 67, - 170, - 161, - 212, - 186, - 162, - 66, - 224, - 4, - 226, - 27, - 201, - 176, - 218, - 186, - 46, - 154 - ]), -// String list - Uint8List.fromList([ - 197, - 0, - 0, - 0, - 1, - 11, - 83, - 116, - 114, - 105, - 110, - 103, - 32, - 108, - 105, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 153, - 150, - 253, - 78, - 106, - 101, - 109, - 26, - 206, - 172, - 48, - 41, - 124, - 201, - 248, - 47, - 69, - 93, - 1, - 35, - 207, - 3, - 203, - 213, - 183, - 25, - 62, - 151, - 119, - 159, - 105, - 163, - 32, - 4, - 204, - 87, - 103, - 164, - 36, - 110, - 151, - 251, - 178, - 153, - 145, - 250, - 201, - 153, - 44, - 82, - 73, - 241, - 70, - 157, - 252, - 205, - 207, - 9, - 239, - 149, - 2, - 231, - 0, - 31, - 21, - 224, - 240, - 97, - 211, - 203, - 177, - 85, - 184, - 23, - 191, - 14, - 169, - 104, - 6, - 72, - 200, - 169, - 190, - 201, - 166, - 74, - 169, - 87, - 78, - 130, - 247, - 29, - 95, - 78, - 77, - 110, - 110, - 237, - 93, - 219, - 36, - 157, - 77, - 89, - 21, - 139, - 95, - 140, - 12, - 72, - 176, - 150, - 182, - 115, - 97, - 17, - 106, - 1, - 70, - 143, - 87, - 44, - 136, - 208, - 171, - 82, - 222, - 240, - 252, - 37, - 80, - 158, - 125, - 207, - 248, - 121, - 164, - 224, - 79, - 247, - 122, - 36, - 106, - 109, - 232, - 11, - 10, - 110, - 226, - 2, - 185, - 94, - 35, - 255, - 111, - 93, - 237, - 120, - 1, - 208, - 231, - 126, - 1, - 176 - ]), -// List with null - Uint8List.fromList([ - 88, - 0, - 0, - 0, - 1, - 14, - 76, - 105, - 115, - 116, - 32, - 119, - 105, - 116, - 104, - 32, - 110, - 117, - 108, - 108, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 225, - 213, - 185, - 248, - 156, - 156, - 222, - 100, - 44, - 8, - 160, - 172, - 158, - 230, - 229, - 149, - 188, - 123, - 223, - 163, - 122, - 78, - 162, - 202, - 142, - 27, - 253, - 165, - 13, - 140, - 65, - 233, - 29, - 92, - 178, - 164, - 228, - 201, - 211, - 12, - 97, - 236, - 110, - 61, - 135, - 56, - 74, - 178, - 55, - 142, - 101, - 155 - ]), -// List with different types - Uint8List.fromList([ - 147, - 0, - 0, - 0, - 1, - 25, - 76, - 105, - 115, - 116, - 32, - 119, - 105, - 116, - 104, - 32, - 100, - 105, - 102, - 102, - 101, - 114, - 101, - 110, - 116, - 32, - 116, - 121, - 112, - 101, - 115, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 162, - 44, - 10, - 205, - 135, - 89, - 106, - 78, - 241, - 135, - 17, - 15, - 155, - 107, - 162, - 13, - 81, - 31, - 198, - 131, - 84, - 96, - 234, - 9, - 206, - 118, - 3, - 239, - 65, - 220, - 91, - 112, - 77, - 245, - 236, - 56, - 187, - 27, - 196, - 100, - 214, - 161, - 244, - 51, - 115, - 66, - 150, - 32, - 8, - 19, - 182, - 95, - 197, - 126, - 114, - 233, - 248, - 30, - 120, - 37, - 145, - 189, - 231, - 48, - 70, - 127, - 105, - 232, - 197, - 117, - 169, - 30, - 98, - 43, - 197, - 132, - 88, - 251, - 86, - 227, - 41, - 137, - 115, - 219, - 77, - 226, - 111, - 143, - 54, - 34, - 24, - 37, - 220, - 243, - 244, - 183, - 241, - 244, - 232, - 38 - ]), -// Map - Uint8List.fromList([ - 205, - 0, - 0, - 0, - 1, - 3, - 77, - 97, - 112, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 13, - 37, - 197, - 127, - 137, - 83, - 219, - 85, - 165, - 224, - 139, - 227, - 168, - 247, - 122, - 173, - 159, - 125, - 186, - 212, - 237, - 61, - 184, - 146, - 219, - 212, - 193, - 3, - 131, - 71, - 167, - 69, - 72, - 68, - 61, - 110, - 226, - 177, - 245, - 40, - 22, - 64, - 222, - 206, - 18, - 187, - 144, - 52, - 117, - 219, - 163, - 20, - 172, - 147, - 223, - 24, - 196, - 56, - 90, - 185, - 134, - 127, - 233, - 62, - 232, - 152, - 130, - 0, - 225, - 179, - 251, - 62, - 5, - 56, - 18, - 75, - 114, - 254, - 126, - 88, - 228, - 224, - 98, - 166, - 213, - 81, - 26, - 179, - 113, - 106, - 87, - 10, - 251, - 92, - 247, - 98, - 106, - 46, - 28, - 240, - 129, - 41, - 216, - 128, - 131, - 86, - 69, - 158, - 26, - 228, - 162, - 202, - 130, - 44, - 16, - 171, - 34, - 32, - 155, - 91, - 168, - 50, - 50, - 133, - 140, - 234, - 193, - 8, - 136, - 126, - 30, - 138, - 110, - 138, - 143, - 37, - 15, - 129, - 207, - 48, - 14, - 43, - 118, - 118, - 108, - 26, - 182, - 218, - 53, - 118, - 111, - 14, - 23, - 188, - 115, - 229, - 48, - 9, - 121, - 148, - 87, - 176, - 7, - 44, - 97, - 201, - 216, - 31, - 30, - 237, - 69, - 66, - 56, - 123, - 115, - 17, - 79, - 167, - 14, - 40 - ]), -// DateTime test - Uint8List.fromList([ - 71, - 0, - 0, - 0, - 1, - 13, - 68, - 97, - 116, - 101, - 84, - 105, - 109, - 101, - 32, - 116, - 101, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 49, - 1, - 86, - 197, - 233, - 67, - 235, - 208, - 152, - 138, - 141, - 53, - 104, - 120, - 4, - 205, - 53, - 148, - 224, - 218, - 111, - 99, - 191, - 123, - 147, - 13, - 68, - 127, - 76, - 87, - 220, - 8, - 39, - 63, - 133, - 196 - ]), -// BigInt Test - Uint8List.fromList([ - 85, - 0, - 0, - 0, - 1, - 11, - 66, - 105, - 103, - 73, - 110, - 116, - 32, - 84, - 101, - 115, - 116, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 4, - 92, - 197, - 108, - 246, - 190, - 129, - 94, - 5, - 62, - 37, - 237, - 35, - 48, - 69, - 227, - 238, - 46, - 181, - 251, - 153, - 42, - 12, - 188, - 181, - 65, - 254, - 237, - 238, - 144, - 189, - 230, - 159, - 77, - 241, - 22, - 110, - 106, - 204, - 207, - 172, - 102, - 136, - 240, - 71, - 172, - 126, - 39, - 159, - 181, - 146, - 50, - 178 - ]), -]; diff --git a/hive/test/integration/date_time_adapter_upgrade.dart b/hive/test/integration/date_time_adapter_upgrade.dart deleted file mode 100644 index 4047ab49c..000000000 --- a/hive/test/integration/date_time_adapter_upgrade.dart +++ /dev/null @@ -1,39 +0,0 @@ -import 'package:hive/src/adapters/date_time_adapter.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - -void main() { - group('upgrading DateTimeAdapter to DateTimeWithTimeZoneAdapter', () { - group('TypeRegistry', () { - late TypeRegistryImpl registry; - - setUp(() { - registry = TypeRegistryImpl(); - registry.registerAdapter(DateTimeWithTimezoneAdapter(), internal: true); - registry.registerAdapter(DateTimeAdapter(), internal: true); - }); - - tearDown(() { - registry.resetAdapters(); - }); - - test('uses DateTimeWithTimeZoneAdapter for writing new values', () { - var result = registry.findAdapterForValue(DateTime.now())!; - expect(result, isNotNull); - expect(result.adapter, isA()); - }); - - test('uses DateTimeWithTimeZoneAdapter for reading if typeId = 18', () { - var result = registry.findAdapterForTypeId(18)!; - expect(result, isNotNull); - expect(result.adapter, isA()); - }); - - test('uses DateTimeAdapter for reading if typeId = 16', () { - var result = registry.findAdapterForTypeId(16)!; - expect(result, isNotNull); - expect(result.adapter, isA()); - }); - }); - }); -} diff --git a/hive/test/integration/delete_many_single_batch_test.dart b/hive/test/integration/delete_many_single_batch_test.dart deleted file mode 100644 index a2c152100..000000000 --- a/hive/test/integration/delete_many_single_batch_test.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'package:test/test.dart'; - -import '../util/is_browser.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var amount = isBrowser ? 500 : 20000; - var box = await openBox(lazy); - var entries = {}; - for (var i = 0; i < amount; i++) { - entries['string$i'] = 'test'; - entries['int$i'] = -i; - entries['bool$i'] = i % 2 == 0; - entries['null$i'] = null; - } - await box.putAll(entries); - await box.put('123123', 'value'); - - box = await box.reopen(); - await box.deleteAll(entries.keys); - - box = await box.reopen(); - for (var i = 0; i < amount; i++) { - expect(box.containsKey('string$i'), false); - expect(box.containsKey('int$i'), false); - expect(box.containsKey('bool$i'), false); - expect(box.containsKey('null$i'), false); - } - expect(await await box.get('123123'), 'value'); - - await box.close(); -} - -void main() { - group('delete many entries in a single batch', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/delete_many_test.dart b/hive/test/integration/delete_many_test.dart deleted file mode 100644 index ef58651d0..000000000 --- a/hive/test/integration/delete_many_test.dart +++ /dev/null @@ -1,46 +0,0 @@ -import 'package:test/test.dart'; - -import '../util/is_browser.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var amount = isBrowser ? 1000 : 20000; - var box = await openBox(lazy); - var entries = {}; - for (var i = 0; i < amount; i++) { - entries['string$i'] = 'test'; - entries['int$i'] = -i; - entries['bool$i'] = i % 2 == 0; - entries['null$i'] = null; - } - await box.putAll(entries); - await box.put('123123', 'value'); - - box = await box.reopen(); - for (var i = 0; i < amount; i++) { - await box.delete('string$i'); - await box.delete('int$i'); - await box.delete('bool$i'); - await box.delete('null$i'); - } - - await box.flush(); - - box = await box.reopen(); - for (var i = 0; i < amount; i++) { - expect(box.containsKey('string$i'), false); - expect(box.containsKey('int$i'), false); - expect(box.containsKey('bool$i'), false); - expect(box.containsKey('null$i'), false); - } - expect(await await box.get('123123'), 'value'); - await box.close(); -} - -void main() { - group('delete many entries', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/different_frame_types_test.dart b/hive/test/integration/different_frame_types_test.dart deleted file mode 100644 index 7f9c6684f..000000000 --- a/hive/test/integration/different_frame_types_test.dart +++ /dev/null @@ -1,38 +0,0 @@ -import 'package:test/test.dart'; - -import '../tests/frames.dart'; -import 'integration.dart'; - -Future _performTest(bool encrypted, bool lazy) async { - var encryptionKey = encrypted ? List.generate(32, (i) => i) : null; - var box = await openBox(lazy, encryptionKey: encryptionKey); - for (var frame in valueTestFrames) { - if (frame.deleted) continue; - await box.put(frame.key, frame.value); - } - - box = await box.reopen(encryptionKey: encryptionKey); - - for (var frame in valueTestFrames) { - if (frame.deleted) continue; - var f = await await box.get(frame.key); - expect(f, frame.value); - } - await box.close(); -} - -void main() { - group('different frame types', () { - group('encrypted', () { - test('normal box', () => _performTest(true, false)); - - test('lazy box', () => _performTest(true, true)); - }); - - group('not encrypted', () { - test('normal box', () => _performTest(false, false)); - - test('lazy box', () => _performTest(false, true)); - }); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/hive_list_test.dart b/hive/test/integration/hive_list_test.dart deleted file mode 100644 index 80a22bc73..000000000 --- a/hive/test/integration/hive_list_test.dart +++ /dev/null @@ -1,73 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:test/test.dart'; - -import 'integration.dart'; - -class _TestObject extends HiveObject { - String? name; - - HiveList<_TestObject>? list; - - _TestObject(this.name); - - @override - bool operator ==(dynamic other) => other is _TestObject && other.name == name; - - @override - int get hashCode => runtimeType.hashCode ^ name.hashCode; -} - -class _TestObjectAdapter extends TypeAdapter<_TestObject> { - @override - int get typeId => 0; - - @override - _TestObject read(BinaryReader reader) { - return _TestObject(reader.read() as String?) - ..list = (reader.read() as HiveList?)?.castHiveList(); - } - - @override - void write(BinaryWriter writer, _TestObject obj) { - writer.write(obj.name); - writer.write(obj.list); - } -} - -void main() { - test('add and remove objects to / from HiveList', () async { - var hive = await createHive(); - hive.registerAdapter(_TestObjectAdapter()); - var box = await openBox<_TestObject>(false, hive: hive) as Box<_TestObject>; - - var obj = _TestObject('obj'); - obj.list = HiveListImpl(box); - await box.put('obj', obj); - - for (var i = 0; i < 100; i++) { - var element = _TestObject('element$i'); - await box.add(element); - obj.list!.add(element); - } - - await obj.save(); - - box = (await box.reopen()) as Box<_TestObject>; - obj = box.get('obj')!; - (obj.list as HiveListImpl).debugHive = hive; - - for (var i = 0; i < 100; i++) { - expect(obj.list![i].name, 'element$i'); - } - - await obj.list![99].delete(); - expect(obj.list!.length, 99); - - await obj.list![50].delete(); - expect(obj.list![50].name, 'element51'); - - await obj.list![0].delete(); - expect(obj.list![0].name, 'element1'); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/hive_object_test.dart b/hive/test/integration/hive_object_test.dart deleted file mode 100644 index 886cc53e2..000000000 --- a/hive/test/integration/hive_object_test.dart +++ /dev/null @@ -1,71 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:test/test.dart'; - -import 'integration.dart'; - -class _TestObject with HiveObjectMixin { - String name; - - _TestObject(this.name); - - @override - bool operator ==(dynamic other) => other is _TestObject && other.name == name; - - @override - int get hashCode => runtimeType.hashCode ^ name.hashCode; -} - -class _TestObjectAdapter extends TypeAdapter<_TestObject> { - @override - int get typeId => 0; - - @override - _TestObject read(BinaryReader reader) { - return _TestObject(reader.readString()); - } - - @override - void write(BinaryWriter writer, _TestObject obj) { - writer.writeString(obj.name); - } -} - -Future _performTest(bool lazy) async { - var hive = await createHive(); - hive.registerAdapter<_TestObject>(_TestObjectAdapter()); - var box = await openBox(lazy, hive: hive); - - var obj1 = _TestObject('test1'); - await box.add(obj1); - expect(obj1.key, 0); - - var obj2 = _TestObject('test2'); - await box.put('someKey', obj2); - expect(obj2.key, 'someKey'); - - box = await box.reopen(); - obj1 = await box.get(0) as _TestObject; - obj2 = await box.get('someKey') as _TestObject; - expect(obj1.name, 'test1'); - expect(obj2.name, 'test2'); - - obj1.name = 'test1 updated'; - await obj1.save(); - await obj2.delete(); - - box = await box.reopen(); - final newObj1 = await box.get(0) as _TestObject; - final newObj2 = await box.get('someKey') as _TestObject?; - expect(newObj1.name, 'test1 updated'); - expect(newObj2, null); - - await box.close(); -} - -void main() { - group('use HiveObject to update and delete entries', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/ignore_type_id_test.dart b/hive/test/integration/ignore_type_id_test.dart deleted file mode 100644 index 2ecd25dda..000000000 --- a/hive/test/integration/ignore_type_id_test.dart +++ /dev/null @@ -1,51 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:test/test.dart'; - -import 'integration.dart'; - -class TestObject { - TestObject(this.value); - - final int value; -} - -class TestAdapter extends TypeAdapter { - TestAdapter(); - - @override - final int typeId = 10; - - @override - TestObject read(BinaryReader reader) { - return TestObject(reader.readInt32()); - } - - @override - void write(BinaryWriter writer, obj) { - writer.writeInt32(obj.value); - } -} - -void main() { - test('ignore typeId with IgnoredTypeAdapter', () async { - var hive = await createHive(); - var box1 = await hive.openBox('test'); - - hive.registerAdapter(TestAdapter()); - - await box1.put(1, TestObject(5)); - await box1.put(2, 42); - await box1.put(3, 78); - await box1.close(); - - hive.resetAdapters(); - hive.ignoreTypeId(10); - - var box2 = await hive.openBox('test'); - - expect(box2, isNotNull); - expect(box2.get(1), null); - expect(box2.get(2), 42); - expect(box2.get(3), 78); - }); -} diff --git a/hive/test/integration/integration.dart b/hive/test/integration/integration.dart deleted file mode 100644 index 108a2ded1..000000000 --- a/hive/test/integration/integration.dart +++ /dev/null @@ -1,67 +0,0 @@ -import 'dart:math'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/box/box_base_impl.dart'; -import 'package:hive/src/box/lazy_box_impl.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:test/test.dart'; - -import '../tests/common.dart'; -import '../util/is_browser.dart'; - -Future createHive() async { - final hive = HiveImpl(); - if (!isBrowser) { - var dir = await getTempDir(); - hive.init(dir.path); - } else { - hive.init(null); - } - return hive; -} - -Future> openBox(bool lazy, - {HiveInterface? hive, List? encryptionKey}) async { - hive ??= await createHive(); - var id = Random().nextInt(99999999); - HiveCipher? cipher; - if (encryptionKey != null) { - cipher = HiveAesCipher(encryptionKey); - } - if (lazy) { - return await hive.openLazyBox('box$id', - crashRecovery: false, encryptionCipher: cipher); - } else { - return await hive.openBox('box$id', - crashRecovery: false, encryptionCipher: cipher); - } -} - -extension BoxBaseX on BoxBase { - Future> reopen({List? encryptionKey}) async { - await close(); - var hive = (this as BoxBaseImpl).hive; - HiveCipher? cipher; - if (encryptionKey != null) { - cipher = HiveAesCipher(encryptionKey); - } - if (this is LazyBoxImpl) { - return await hive.openLazyBox(name, - crashRecovery: false, encryptionCipher: cipher); - } else { - return await hive.openBox(name, - crashRecovery: false, encryptionCipher: cipher); - } - } - - Future get(dynamic key, {dynamic defaultValue}) { - if (this is LazyBox) { - return (this as LazyBox).get(key, defaultValue: defaultValue); - } else if (this is Box) { - return Future.value((this as Box).get(key, defaultValue: defaultValue)); - } - throw ArgumentError('not possible'); - } -} - -const longTimeout = Timeout(Duration(minutes: 2)); diff --git a/hive/test/integration/put_large_lists_test.dart b/hive/test/integration/put_large_lists_test.dart deleted file mode 100644 index caf75a0db..000000000 --- a/hive/test/integration/put_large_lists_test.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'dart:typed_data'; - -import 'package:test/test.dart'; - -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var box = await openBox(lazy); - - var nullableStringList = List.filled(1000000, 'test', growable: true) - ..add(null); - var doubleList = List.filled(1000000, 1.212312); - var byteList = Uint8List.fromList(List.filled(1000000, 123)); - - for (var i = 0; i < 5; i++) { - await box.put('stringList$i', nullableStringList); - await box.put('doubleList$i', doubleList); - await box.put('byteList$i', byteList); - } - - box = await box.reopen(); - for (var i = 0; i < 5; i++) { - var readStringList = await await box.get('stringList$i'); - var readDoubleList = await await box.get('doubleList$i'); - var readByteList = await await box.get('byteList$i'); - - expect(readStringList, nullableStringList); - expect(readDoubleList, doubleList); - expect(readByteList, byteList); - } - await box.close(); -} - -void main() { - group('put large lists', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/put_large_strings_test.dart b/hive/test/integration/put_large_strings_test.dart deleted file mode 100644 index 3dd5c1f82..000000000 --- a/hive/test/integration/put_large_strings_test.dart +++ /dev/null @@ -1,27 +0,0 @@ -import 'package:test/test.dart'; - -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var box = await openBox(lazy); - for (var i = 0; i < 5; i++) { - var largeString = i.toString() * 1000000; - await box.put('string$i', largeString); - } - - box = await box.reopen(); - for (var i = 0; i < 5; i++) { - var largeString = await await box.get('string$i'); - - expect(largeString == i.toString() * 1000000, true); - } - await box.close(); -} - -void main() { - group('put large strings', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/put_many_same_key_test.dart b/hive/test/integration/put_many_same_key_test.dart deleted file mode 100644 index 1d5f72409..000000000 --- a/hive/test/integration/put_many_same_key_test.dart +++ /dev/null @@ -1,51 +0,0 @@ -import 'dart:async'; - -import 'package:test/test.dart'; - -import '../util/is_browser.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var amount = isBrowser ? 5 : 100; - var box = await openBox(lazy); - - for (var i = 0; i < amount; i++) { - for (var n = 0; n < 100; n++) { - var completer = Completer(); - scheduleMicrotask(() async { - await box.put('string$i', 'test$n'); - await box.put('int$i', n); - await box.put('bool$i', n % 2 == 0); - await box.put('null$i', null); - await box.flush(); - - expect(await await box.get('string$i'), 'test$n'); - expect(await await box.get('int$i'), n); - expect(await await box.get('bool$i'), n % 2 == 0); - expect(await await box.get('null$i', defaultValue: 0), null); - - completer.complete(); - }); - await completer.future; - } - } - - await box.flush(); - - box = await box.reopen(); - for (var i = 0; i < amount; i++) { - expect(await await box.get('string$i'), 'test99'); - expect(await await box.get('int$i'), 99); - expect(await await box.get('bool$i'), false); - expect(await await box.get('null$i', defaultValue: 0), null); - } - await box.close(); -} - -void main() { - group('put many entries with the same key', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/put_many_simultaneously_test.dart b/hive/test/integration/put_many_simultaneously_test.dart deleted file mode 100644 index 3ec060013..000000000 --- a/hive/test/integration/put_many_simultaneously_test.dart +++ /dev/null @@ -1,37 +0,0 @@ -import 'package:test/test.dart'; - -import '../util/is_browser.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var amount = isBrowser ? 10 : 100; - var box = await openBox(lazy); - - Future putEntries() async { - for (var i = 0; i < amount; i++) { - await box.put('key$i', 'value$i'); - } - } - - var futures = []; - for (var i = 0; i < 10; i++) { - futures.add(putEntries()); - } - await Future.wait(futures); - - await box.flush(); - - box = await box.reopen(); - for (var i = 0; i < amount; i++) { - expect(await box.get('key$i'), 'value$i'); - } - await box.close(); -} - -void main() { - group('put many entries simultaneously', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/put_many_single_batch_test.dart b/hive/test/integration/put_many_single_batch_test.dart deleted file mode 100644 index 32711e518..000000000 --- a/hive/test/integration/put_many_single_batch_test.dart +++ /dev/null @@ -1,33 +0,0 @@ -import 'package:test/test.dart'; - -import '../tests/frames.dart'; -import '../util/is_browser.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var repeat = isBrowser ? 20 : 1000; - var box = await openBox(lazy); - var entries = {}; - for (var i = 0; i < repeat; i++) { - for (var frame in valueTestFrames) { - entries['${frame.key}n$i'] = frame.value; - } - } - await box.putAll(entries); - - box = await box.reopen(); - for (var i = 0; i < repeat; i++) { - for (var frame in valueTestFrames) { - expect(await await box.get('${frame.key}n$i'), frame.value); - } - } - await box.close(); -} - -void main() { - group('put many entries in a single batch', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/put_many_strings_test.dart b/hive/test/integration/put_many_strings_test.dart deleted file mode 100644 index e389f37ac..000000000 --- a/hive/test/integration/put_many_strings_test.dart +++ /dev/null @@ -1,32 +0,0 @@ -import 'package:test/test.dart'; - -import '../tests/frames.dart'; -import '../util/is_browser.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var hive = await createHive(); - var repeat = isBrowser ? 20 : 1000; - var box = await openBox(lazy, hive: hive); - for (var i = 0; i < repeat; i++) { - for (var frame in valueTestFrames) { - await box.put('${frame.key}n$i', frame.value); - } - } - - box = await box.reopen(); - for (var i = 0; i < repeat; i++) { - for (var frame in valueTestFrames) { - expect(await await box.get('${frame.key}n$i'), frame.value); - } - } - await box.close(); -} - -void main() { - group('put many strings', () { - test('normal box', () => _performTest(false)); - - test('lazy box', () => _performTest(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/recovery_test.dart b/hive/test/integration/recovery_test.dart deleted file mode 100644 index f4848c234..000000000 --- a/hive/test/integration/recovery_test.dart +++ /dev/null @@ -1,68 +0,0 @@ -@TestOn('vm') - -import 'dart:async'; -import 'dart:io'; - -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:path/path.dart' as path; -import 'package:test/test.dart'; - -import '../tests/backend/vm/storage_backend_vm_test.dart'; -import '../tests/common.dart'; -import '../tests/frames.dart'; -import 'integration.dart'; - -Future _performTest(bool lazy) async { - var bytes = await getFrameBytes(testFrames); - var frames = testFrames; - - framesSetLengthOffset(frames, frameBytes); - - var dir = await getTempDir(); - var hive = HiveImpl(); - hive.init(dir.path); - - for (var i = 0; i < bytes.length; i++) { - var subBytes = bytes.sublist(0, i + 1); - var boxFile = File(path.join(dir.path, 'testbox$i.hive')); - await boxFile.writeAsBytes(subBytes); - - var subFrames = frames.takeWhile((f) => f.offset + f.length! <= i + 1); - var subKeystore = Keystore.debug(frames: subFrames); - if (lazy) { - var box = await hive.openLazyBox('testbox$i'); - expect(box.keys, subKeystore.getKeys()); - await box.compact(); - await box.close(); - } else { - var box = await hive.openBox('testbox$i'); - var map = Map.fromIterables( - subKeystore.getKeys(), - subKeystore.getValues(), - ); - expect(box.toMap(), map); - await box.compact(); - await box.close(); - } - - expect(await boxFile.readAsBytes(), await getFrameBytes(subFrames)); - } -} - -Future _performTestWithoutOutput(bool lazy) { - return runZoned( - () => _performTest(lazy), - zoneSpecification: ZoneSpecification( - print: (self, parent, zone, message) {}, - ), - ); -} - -void main() { - group('test recovery', () { - test('normal box', () => _performTestWithoutOutput(false)); - - test('lazy box', () => _performTestWithoutOutput(true)); - }, timeout: longTimeout); -} diff --git a/hive/test/integration/watch_box_updates.dart b/hive/test/integration/watch_box_updates.dart deleted file mode 100644 index c6c03c2bc..000000000 --- a/hive/test/integration/watch_box_updates.dart +++ /dev/null @@ -1,87 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:test/test.dart'; - -import 'integration.dart'; - -Future expectNextEvent(Stream stream, FutureOr Function() cb, - {Object? key, Object? value, bool? deleted}) async { - late StreamSubscription subscription; - final completer = Completer(); - - subscription = stream.listen((event) async { - await subscription.cancel(); - try { - if (key != null) expect(event.key, key); - if (value != null) expect(event.value, value); - if (deleted != null) expect(event.deleted, deleted); - } finally { - completer.complete(); - } - }); - - await cb(); - await completer.future; -} - -void main() { - group('watch() emits box updates when', () { - late BoxBase box; - - setUp(() async { - box = await openBox(false); - }); - - tearDown(() async { - await box.close(); - }); - - test('.put() is called', () async { - await expectNextEvent( - box.watch(), - () async { - await box.put('key', 'value'); - }, - key: 'key', - value: 'value', - deleted: false, - ); - }); - - test('.add() is called', () async { - await expectNextEvent( - box.watch(), - () async { - await box.add('value'); - }, - value: 'value', - deleted: false, - ); - }); - - test('.putAt() is called', () async { - await expectNextEvent( - box.watch().skip(1), - () async { - await box.add(null); - await box.putAt(0, 'value'); - }, - value: 'value', - deleted: false, - ); - }); - - test('.delete() is called', () async { - await expectNextEvent( - box.watch().skip(1), - () async { - await box.put('key', 'value'); - await box.delete('key'); - }, - key: 'key', - deleted: true, - ); - }); - }); -} diff --git a/hive/test/tests/adapters/big_int_adapter_test.dart b/hive/test/tests/adapters/big_int_adapter_test.dart deleted file mode 100644 index 63be3fc80..000000000 --- a/hive/test/tests/adapters/big_int_adapter_test.dart +++ /dev/null @@ -1,36 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/adapters/big_int_adapter.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - -void main() { - group('BigIntAdapter', () { - group('reads', () { - test('positive BigInts', () { - var numberStr = '123456789123456789'; - var bytes = - Uint8List.fromList([numberStr.length, ...numberStr.codeUnits]); - var reader = BinaryReaderImpl(bytes, TypeRegistryImpl.nullImpl); - expect(BigIntAdapter().read(reader), BigInt.parse(numberStr)); - }); - - test('negative BigInts', () { - var numberStr = '-123456789123456789'; - var bytes = - Uint8List.fromList([numberStr.length, ...numberStr.codeUnits]); - var reader = BinaryReaderImpl(bytes, TypeRegistryImpl.nullImpl); - expect(BigIntAdapter().read(reader), BigInt.parse(numberStr)); - }); - }); - - test('writes BigInts', () { - var numberStr = '123456789123456789'; - var writer = BinaryWriterImpl(TypeRegistryImpl.nullImpl); - BigIntAdapter().write(writer, BigInt.parse(numberStr)); - expect(writer.toBytes(), [numberStr.length, ...numberStr.codeUnits]); - }); - }); -} diff --git a/hive/test/tests/adapters/date_time_adapter_test.dart b/hive/test/tests/adapters/date_time_adapter_test.dart deleted file mode 100644 index fafe02176..000000000 --- a/hive/test/tests/adapters/date_time_adapter_test.dart +++ /dev/null @@ -1,86 +0,0 @@ -import 'package:hive/src/adapters/date_time_adapter.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../mocks.dart'; - -void main() { - group('DateTimeAdapter', () { - test('.read()', () { - var now = DateTime.now(); - var binaryReader = MockBinaryReader(); - when(() => binaryReader.readInt()).thenReturn(now.millisecondsSinceEpoch); - - var date = DateTimeAdapter().read(binaryReader); - verify(() => binaryReader.readInt()); - expect(date, now.subtract(Duration(microseconds: now.microsecond))); - }); - - test('.write()', () { - var now = DateTime.now(); - var binaryWriter = MockBinaryWriter(); - - DateTimeAdapter().write(binaryWriter, now); - verify(() => binaryWriter.writeInt(now.millisecondsSinceEpoch)); - }); - }); - - group('DateTimeWithTimezoneAdapter', () { - group('.read()', () { - test('local', () { - var now = DateTime.now(); - var binaryReader = MockBinaryReader(); - when(() => binaryReader.readInt()) - .thenReturn(now.millisecondsSinceEpoch); - when(() => binaryReader.readBool()).thenReturn(false); - - var date = DateTimeWithTimezoneAdapter().read(binaryReader); - verifyInOrder([ - () => binaryReader.readInt(), - () => binaryReader.readBool(), - ]); - expect(date, now.subtract(Duration(microseconds: now.microsecond))); - }); - - test('UTC', () { - var now = DateTime.now().toUtc(); - var binaryReader = MockBinaryReader(); - when(() => binaryReader.readInt()) - .thenReturn(now.millisecondsSinceEpoch); - when(() => binaryReader.readBool()).thenReturn(true); - - var date = DateTimeWithTimezoneAdapter().read(binaryReader); - verifyInOrder([ - () => binaryReader.readInt(), - () => binaryReader.readBool(), - ]); - expect(date, now.subtract(Duration(microseconds: now.microsecond))); - expect(date.isUtc, true); - }); - }); - - group('.write()', () { - test('local', () { - var now = DateTime.now(); - var binaryWriter = MockBinaryWriter(); - - DateTimeWithTimezoneAdapter().write(binaryWriter, now); - verifyInOrder([ - () => binaryWriter.writeInt(now.millisecondsSinceEpoch), - () => binaryWriter.writeBool(false), - ]); - }); - - test('UTC', () { - var now = DateTime.now().toUtc(); - var binaryWriter = MockBinaryWriter(); - - DateTimeWithTimezoneAdapter().write(binaryWriter, now); - verifyInOrder([ - () => binaryWriter.writeInt(now.millisecondsSinceEpoch), - () => binaryWriter.writeBool(true), - ]); - }); - }); - }); -} diff --git a/hive/test/tests/adapters/ignored_type_adapter_test.dart b/hive/test/tests/adapters/ignored_type_adapter_test.dart deleted file mode 100644 index e8a85ded4..000000000 --- a/hive/test/tests/adapters/ignored_type_adapter_test.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'package:hive/src/adapters/ignored_type_adapter.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../mocks.dart'; - -void main() { - group('IgnoredTypeAdapter', () { - test('.read()', () { - var binaryReader = MockBinaryReader(); - var value = IgnoredTypeAdapter().read(binaryReader); - verifyNever(() => binaryReader.read()); - expect(value, null); - }); - - test('.write()', () { - var binaryWriter = MockBinaryWriter(); - IgnoredTypeAdapter().write(binaryWriter, 42); - verifyNever(() => binaryWriter.writeInt(42)); - }); - }); -} diff --git a/hive/test/tests/backend/js/backend_manager_test.dart b/hive/test/tests/backend/js/backend_manager_test.dart deleted file mode 100644 index 2af99f4fd..000000000 --- a/hive/test/tests/backend/js/backend_manager_test.dart +++ /dev/null @@ -1,35 +0,0 @@ -@TestOn('browser') -import 'dart:html'; -import 'dart:indexed_db'; - -import 'package:hive/src/backend/js/backend_manager.dart'; -import 'package:test/test.dart'; - -Future _openDb() async { - return await window.indexedDB!.open('testBox', version: 1, - onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if (!db.objectStoreNames!.contains('box')) { - db.createObjectStore('box'); - } - }); -} - -void main() { - group('BackendManager', () { - group('.boxExists()', () { - test('returns true', () async { - var backendManager = BackendManager.select(); - var db = await _openDb(); - db.close(); - expect(await backendManager.boxExists('testBox', null, null), isTrue); - }); - - test('returns false', () async { - var backendManager = BackendManager.select(); - var boxName = 'notexists-${DateTime.now().millisecondsSinceEpoch}'; - expect(await backendManager.boxExists(boxName, null, null), isFalse); - }); - }); - }); -} diff --git a/hive/test/tests/backend/js/storage_backend_js_test.dart b/hive/test/tests/backend/js/storage_backend_js_test.dart deleted file mode 100644 index 25d3c34f6..000000000 --- a/hive/test/tests/backend/js/storage_backend_js_test.dart +++ /dev/null @@ -1,262 +0,0 @@ -@TestOn('browser') -import 'dart:async' show Future; -import 'dart:html'; -import 'dart:indexed_db'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/js/native/storage_backend_js.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - -import '../../frames.dart'; - -late final Database _nullDatabase; - -StorageBackendJs _getBackend({ - Database? db, - HiveCipher? cipher, - TypeRegistry registry = TypeRegistryImpl.nullImpl, -}) { - return StorageBackendJs(db ?? _nullDatabase, cipher, 'box', registry); -} - -Future _openDb([String name = 'testBox']) async { - return await window.indexedDB!.open(name, version: 1, onUpgradeNeeded: (e) { - var db = e.target.result as Database; - if (!db.objectStoreNames!.contains('box')) { - db.createObjectStore('box'); - } - }); -} - -ObjectStore _getStore(Database db) { - return db.transaction('box', 'readwrite').objectStore('box'); -} - -Future _getDbWith(Map content) async { - var db = await _openDb(); - var store = _getStore(db); - await store.clear(); - content.forEach((k, v) => store.put(v, k)); - return db; -} - -void main() async { - _nullDatabase = await _openDb('nullTestBox'); - group('StorageBackendJs', () { - test('.path', () { - expect(_getBackend().path, null); - }); - - group('.encodeValue()', () { - test('primitive', () async { - var values = [ - null, 11, 17.25, true, 'hello', // - [11, 12, 13], [17.25, 17.26], [true, false], ['str1', 'str2'] // - ]; - var backend = _getBackend(); - for (var value in values) { - expect(await backend.encodeValue(Frame('key', value)), value); - } - - var bytes = Uint8List.fromList([1, 2, 3]); - var buffer = - await backend.encodeValue(Frame('key', bytes)) as ByteBuffer; - expect(Uint8List.view(buffer), [1, 2, 3]); - }); - - test('crypto', () async { - var backend = - StorageBackendJs(_nullDatabase, testCipher, 'box', testRegistry); - var i = 0; - for (var frame in testFrames) { - var buffer = await backend.encodeValue(frame) as ByteBuffer; - var bytes = Uint8List.view(buffer); - expect(bytes.sublist(28), - [0x90, 0xA9, ...frameValuesBytesEncrypted[i]].sublist(28)); - i++; - } - }); - - group('non primitive', () { - test('map', () async { - var frame = Frame(0, { - 'key': Uint8List.fromList([1, 2, 3]), - 'otherKey': null - }); - var backend = StorageBackendJs(_nullDatabase, null, 'box'); - var encoded = - Uint8List.view(await backend.encodeValue(frame) as ByteBuffer); - - var writer = BinaryWriterImpl(TypeRegistryImpl.nullImpl) - ..write(frame.value); - expect(encoded, [0x90, 0xA9, ...writer.toBytes()]); - }); - - test('bytes which start with signature', () async { - var frame = Frame(0, Uint8List.fromList([0x90, 0xA9, 1, 2, 3])); - var backend = _getBackend(); - var encoded = - Uint8List.view(await backend.encodeValue(frame) as ByteBuffer); - - var writer = BinaryWriterImpl(TypeRegistryImpl.nullImpl) - ..write(frame.value); - expect(encoded, [0x90, 0xA9, ...writer.toBytes()]); - }); - }); - }); - - group('.decodeValue()', () { - test('primitive', () async { - var backend = _getBackend(); - expect(await backend.decodeValue(null), null); - expect(await backend.decodeValue(11), 11); - expect(await backend.decodeValue(17.25), 17.25); - expect(await backend.decodeValue(true), true); - expect(await backend.decodeValue('hello'), 'hello'); - expect(await backend.decodeValue([11, 12, 13]), [11, 12, 13]); - expect(await backend.decodeValue([17.25, 17.26]), [17.25, 17.26]); - - var bytes = Uint8List.fromList([1, 2, 3]); - expect(await backend.decodeValue(bytes.buffer), [1, 2, 3]); - }); - - test('crypto', () async { - var cipher = HiveAesCipher(Uint8List.fromList(List.filled(32, 1))); - var backend = _getBackend(cipher: cipher, registry: testRegistry); - var i = 0; - for (var testFrame in testFrames) { - var bytes = [0x90, 0xA9, ...frameValuesBytesEncrypted[i]]; - var value = - await backend.decodeValue(Uint8List.fromList(bytes).buffer); - expect(value, testFrame.value); - i++; - } - }); - - test('non primitive', () async { - var backend = _getBackend(registry: testRegistry); - for (var testFrame in testFrames) { - var bytes = await backend.encodeValue(testFrame); - var value = await backend.decodeValue(bytes); - expect(value, testFrame.value); - } - }); - }); - - group('.getKeys()', () { - test('with cursor', () async { - var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3}); - var backend = _getBackend(db: db); - - expect(await backend.getKeys(cursor: true), ['key1', 'key2', 'key3']); - }); - - test('without cursor', () async { - var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3}); - var backend = _getBackend(db: db); - - expect(await backend.getKeys(), ['key1', 'key2', 'key3']); - }); - }); - - group('.getValues()', () { - test('with cursor', () async { - var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3}); - var backend = _getBackend(db: db); - - expect(await backend.getValues(cursor: true), [1, null, 3]); - }); - - test('without cursor', () async { - var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3}); - var backend = _getBackend(db: db); - - expect(await backend.getValues(), [1, null, 3]); - }); - }); - - group('.initialize()', () { - test('not lazy', () async { - var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3}); - var backend = _getBackend(db: db); - - var keystore = Keystore.debug(notifier: ChangeNotifier()); - expect( - await backend.initialize( - TypeRegistryImpl.nullImpl, keystore, false), - 0); - expect(keystore.frames, [ - Frame('key1', 1), - Frame('key2', null), - Frame('key3', 3), - ]); - }); - - test('lazy', () async { - var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3}); - var backend = _getBackend(db: db); - - var keystore = Keystore.debug(notifier: ChangeNotifier()); - expect( - await backend.initialize(TypeRegistryImpl.nullImpl, keystore, true), - 0); - expect(keystore.frames, [ - Frame.lazy('key1'), - Frame.lazy('key2'), - Frame.lazy('key3'), - ]); - }); - }); - - test('.readValue()', () async { - var db = await _getDbWith({'key1': 1, 'key2': null, 'key3': 3}); - var backend = _getBackend(db: db); - - expect(await backend.readValue(Frame('key1', null)), 1); - expect(await backend.readValue(Frame('key2', null)), null); - }); - - test('.writeFrames()', () async { - var db = await _getDbWith({}); - var backend = _getBackend(db: db); - - var frames = [Frame('key1', 123), Frame('key2', null)]; - await backend.writeFrames(frames); - expect(frames, [Frame('key1', 123), Frame('key2', null)]); - expect(await backend.getKeys(), ['key1', 'key2']); - - await backend.writeFrames([Frame.deleted('key1')]); - expect(await backend.getKeys(), ['key2']); - }); - - test('.compact()', () async { - var db = await _getDbWith({}); - var backend = _getBackend(db: db); - expect( - () async => await backend.compact({}), - throwsUnsupportedError, - ); - }); - - test('.clear()', () async { - var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3}); - var backend = _getBackend(db: db); - await backend.clear(); - expect(await backend.getKeys(), []); - }); - - test('.close()', () async { - var db = await _getDbWith({'key1': 1, 'key2': 2, 'key3': 3}); - var backend = _getBackend(db: db); - await backend.close(); - - await expectLater(() async => await backend.getKeys(), throwsA(anything)); - }); - }); -} diff --git a/hive/test/tests/backend/memory/random_access_buffer_test.dart b/hive/test/tests/backend/memory/random_access_buffer_test.dart deleted file mode 100644 index 32591190a..000000000 --- a/hive/test/tests/backend/memory/random_access_buffer_test.dart +++ /dev/null @@ -1,73 +0,0 @@ -import 'package:hive/src/backend/stub/random_access_buffer.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - - -void main() { - group('RandomAccessBuffer', () { - test('empty random access buffer', () { - final randomAccessBuffer = RandomAccessBuffer(null); - expect(randomAccessBuffer.length, 0); - expect(randomAccessBuffer.writeOffset, 0); - }); - - test('write() writes to random access buffer', () async { - final typeRegistry = TypeRegistryImpl.nullImpl; - - final randomAccessBuffer = RandomAccessBuffer(null); - randomAccessBuffer.recoveryCheck(typeRegistry, null, null); - - final frame = Frame("abc", "123"); - await randomAccessBuffer.write([frame], typeRegistry, null); - - expect(randomAccessBuffer.length, getFrameBytes([frame])); - expect(randomAccessBuffer.writeOffset, getFrameBytes([frame])); - }); - - test('read() reads data from random access buffer', () async { - final typeRegistry = TypeRegistryImpl.nullImpl; - - final randomAccessBuffer = RandomAccessBuffer(null); - randomAccessBuffer.recoveryCheck(typeRegistry, null, null); - - final saveFrame = Frame("abc", "123", offset: 0, length: 3); - await randomAccessBuffer.write([saveFrame], typeRegistry, null); - - expect(randomAccessBuffer.length, getFrameBytes([saveFrame])); - expect(randomAccessBuffer.writeOffset, getFrameBytes([saveFrame])); - - final storedValue = await randomAccessBuffer.read( - Frame("abc", "abc", offset: 0, length: 21), typeRegistry, null); - - expect(storedValue, "123"); - }); - - test('clear() resets random access buffer', () async { - final typeRegistry = TypeRegistryImpl.nullImpl; - - final randomAccessBuffer = RandomAccessBuffer(null); - randomAccessBuffer.recoveryCheck(typeRegistry, null, null); - - final frame = Frame("abc", "123"); - await randomAccessBuffer.write([frame], typeRegistry, null); - - expect(randomAccessBuffer.length, getFrameBytes([frame])); - expect(randomAccessBuffer.writeOffset, getFrameBytes([frame])); - - await randomAccessBuffer.clear(); - - expect(randomAccessBuffer.length, 0); - expect(randomAccessBuffer.writeOffset, 0); - }); - }); -} - -int getFrameBytes(Iterable frames) { - var writer = BinaryWriterImpl(TypeRegistryImpl.nullImpl); - for (var frame in frames) { - writer.writeFrame(frame); - } - return writer.toBytes().lengthInBytes; -} diff --git a/hive/test/tests/backend/memory/storage_backend_memory_test.dart b/hive/test/tests/backend/memory/storage_backend_memory_test.dart deleted file mode 100644 index 141fbe45e..000000000 --- a/hive/test/tests/backend/memory/storage_backend_memory_test.dart +++ /dev/null @@ -1,111 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/backend/stub/storage_backend_memory.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - -import '../../common.dart'; -import '../../mocks.dart'; - -void main() { - group('StorageBackendMemory', () { - test('.path is null', () { - var backend = StorageBackendMemory(null, null); - expect(backend.path, null); - }); - - test('.supportsCompaction is false', () { - var backend = StorageBackendMemory(null, null); - expect(backend.supportsCompaction, false); - }); - - group('.initialize()', () { - test('throws if frames cannot be decoded', () { - var bytes = Uint8List.fromList([1, 2, 3, 4]); - var backend = StorageBackendMemory(bytes, null); - expect( - () => backend.initialize( - TypeRegistryImpl.nullImpl, KeystoreFake(), false), - throwsHiveError('Wrong checksum'), - ); - }); - }); - - test('.readValue() returns previously stored value', () async { - var backend = StorageBackendMemory(null, null); - await backend.initialize( - TypeRegistryImpl.nullImpl, MockKeystore(), false); - var testFrame = Frame('key', 'val'); - var frameBytes = getFrameBytes([testFrame]); - testFrame.length = frameBytes.lengthInBytes; - testFrame.offset = 0; - await backend.writeFrames([testFrame]); - expect( - await backend.readValue( - Frame('key', 'key', offset: 0, length: frameBytes.lengthInBytes)), - 'val'); - }); - - test('.readValue() returns previously stored value - multiple frames', - () async { - var backend = StorageBackendMemory(null, null); - await backend.initialize( - TypeRegistryImpl.nullImpl, MockKeystore(), false); - - var frame1 = Frame('key', 'val'); - var frame1Bytes = getFrameBytes([frame1]); - - var frame2 = Frame('123', 'abc'); - var frame2Bytes = getFrameBytes([frame2]); - - var frame3 = Frame('apfelkuchen', 'lecker'); - - await backend.writeFrames([frame1, frame2, frame3]); - final value = await backend.readValue(Frame('123', '123', - offset: frame1Bytes.length, length: frame2Bytes.lengthInBytes)); - expect('abc', value); - }); - - test('.writeFrames() writes data', () async { - var backend = StorageBackendMemory(null, null); - await backend.initialize( - TypeRegistryImpl.nullImpl, MockKeystore(), false); - await backend.writeFrames([Frame('key', 'val')]); - }); - - test('.compact() throws UnsupportedError', () { - var backend = StorageBackendMemory(null, null); - expect(() => backend.compact([]), throwsUnsupportedError); - }); - - test('.deleteFromDisk() does not throw', () async { - var backend = StorageBackendMemory(null, null); - await backend.deleteFromDisk(); - }); - - test('.clear() does not throw', () async { - var backend = StorageBackendMemory(null, null); - await backend.clear(); - }); - - test('.close() does not throw', () async { - var backend = StorageBackendMemory(null, null); - await backend.close(); - }); - - test('.flush() does nothing', () async { - var backend = StorageBackendMemory(null, null); - await backend.flush(); - }); - }); -} - -Uint8List getFrameBytes(Iterable frames) { - var writer = BinaryWriterImpl(TypeRegistryImpl.nullImpl); - for (var frame in frames) { - writer.writeFrame(frame); - } - return writer.toBytes(); -} diff --git a/hive/test/tests/backend/vm/backend_manager_test.dart b/hive/test/tests/backend/vm/backend_manager_test.dart deleted file mode 100644 index f08023346..000000000 --- a/hive/test/tests/backend/vm/backend_manager_test.dart +++ /dev/null @@ -1,39 +0,0 @@ -@TestOn('vm') - -import 'package:hive/src/backend/vm/backend_manager.dart'; -import 'package:path/path.dart' as path; -import 'package:test/test.dart'; - -import '../../common.dart'; - -void main() { - group('BackendManager', () { - group('findHiveFileAndCleanUp', () { - Future checkFindHiveFileAndCleanUp(String folder) async { - var hiveFileDir = - await getAssetDir('findHiveFileAndCleanUp', folder, 'before'); - var hiveFile = await BackendManager() - .findHiveFileAndCleanUp('testBox', hiveFileDir.path); - expect(hiveFile.path, path.join(hiveFileDir.path, 'testBox.hive')); - await expectDirEqualsAssetDir( - hiveFileDir, 'findHiveFileAndCleanUp', folder, 'after'); - } - - test('no hive file', () async { - await checkFindHiveFileAndCleanUp('no_hive_file'); - }); - - test('hive file', () async { - await checkFindHiveFileAndCleanUp('hive_file'); - }); - - test('hive file and compact file', () async { - await checkFindHiveFileAndCleanUp('hive_file_and_compact_file'); - }); - - test('only compact file', () async { - await checkFindHiveFileAndCleanUp('only_compact_file'); - }); - }); - }); -} diff --git a/hive/test/tests/backend/vm/read_write_sync_test.dart b/hive/test/tests/backend/vm/read_write_sync_test.dart deleted file mode 100644 index 7b45a68da..000000000 --- a/hive/test/tests/backend/vm/read_write_sync_test.dart +++ /dev/null @@ -1,202 +0,0 @@ -import 'package:hive/src/backend/vm/read_write_sync.dart'; -import 'package:test/test.dart'; - -Future _asyncRead(ReadWriteSync rw, int id, List history, - {bool? throwError = false}) { - return rw.syncRead(() async { - history.add('startread$id'); - await Future.delayed(Duration(milliseconds: 10)); - if (throwError!) { - throw 'error$id'; // ignore: only_throw_errors - } - history.add('stopread$id'); - }); -} - -Future _asyncWrite(ReadWriteSync rw, int id, List history, - {bool? throwError = false}) { - return rw.syncWrite(() async { - history.add('startwrite$id'); - await Future.delayed(Duration(milliseconds: 10)); - if (throwError!) { - throw 'error$id'; // ignore: only_throw_errors - } - history.add('stopwrite$id'); - }); -} - -Future _asyncReadWrite(ReadWriteSync rw, int id, List history, - {bool? throwError = false}) { - return rw.syncReadWrite(() async { - history.add('startreadwrite$id'); - await Future.delayed(Duration(milliseconds: 10)); - if (throwError!) { - throw 'error$id'; // ignore: only_throw_errors - } - history.add('stopreadwrite$id'); - }); -} - -typedef _Operation = Future Function( - ReadWriteSync rw, - int id, - List history, { - bool? throwError, -}); - -Future _asyncOperation( - ReadWriteSync rw, _Operation operation, int id, List history, - {bool throwError = false}) async { - history.add('before$id'); - try { - await operation(rw, id, history, throwError: throwError); - } catch (e) { - history.add('$e'); - } - history.add('after$id'); -} - -void main() { - group('ReadWriteSync', () { - group('.syncRead()', () { - test('runs in sequence with other read operations', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncRead, 0, history); - var r2 = _asyncOperation(rw, _asyncRead, 1, history); - var r3 = _asyncOperation(rw, _asyncRead, 2, history); - await Future.wait([r1, r2, r3]); - - expect(history, [ - // - 'before0', 'before1', 'before2', - 'startread0', 'stopread0', 'after0', - 'startread1', 'stopread1', 'after1', - 'startread2', 'stopread2', 'after2' - ]); - }); - - test('runs parallel to write operations', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncRead, 0, history); - var r2 = _asyncOperation(rw, _asyncWrite, 1, history); - await Future.wait([r1, r2]); - - expect(history[0], 'before0'); - expect(history[1], 'before1'); - expect(history.indexOf('stopread0') + 1, history.indexOf('after0')); - expect(history.indexOf('stopwrite1') + 1, history.indexOf('after1')); - }); - - test('handles errors', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncRead, 0, history); - var r2 = _asyncOperation(rw, _asyncRead, 1, history, throwError: true); - var r3 = _asyncOperation(rw, _asyncRead, 2, history); - await Future.wait([r1, r2, r3]); - - expect(history, [ - // - 'before0', 'before1', 'before2', - 'startread0', 'stopread0', 'after0', - 'startread1', 'error1', 'after1', - 'startread2', 'stopread2', 'after2' - ]); - }); - }); - - group('.syncWrite()', () { - test('runs in sequence with other write operations', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncWrite, 0, history); - var r2 = _asyncOperation(rw, _asyncWrite, 1, history); - var r3 = _asyncOperation(rw, _asyncWrite, 2, history); - await Future.wait([r1, r2, r3]); - - expect(history, [ - // - 'before0', 'before1', 'before2', - 'startwrite0', 'stopwrite0', 'after0', - 'startwrite1', 'stopwrite1', 'after1', - 'startwrite2', 'stopwrite2', 'after2' - ]); - }); - - test('runs parallel to read operations', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncWrite, 0, history); - var r2 = _asyncOperation(rw, _asyncRead, 1, history); - await Future.wait([r1, r2]); - - expect(history[0], 'before0'); - expect(history[1], 'before1'); - expect(history.indexOf('stopwrite0') + 1, history.indexOf('after0')); - expect(history.indexOf('stopread1') + 1, history.indexOf('after1')); - }); - - test('handles errors', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncWrite, 0, history); - var r2 = _asyncOperation(rw, _asyncWrite, 1, history, throwError: true); - var r3 = _asyncOperation(rw, _asyncWrite, 2, history); - await Future.wait([r1, r2, r3]); - - expect(history, [ - // - 'before0', 'before1', 'before2', - 'startwrite0', 'stopwrite0', 'after0', - 'startwrite1', 'error1', 'after1', - 'startwrite2', 'stopwrite2', 'after2' - ]); - }); - }); - - group('.syncReadWrite()', () { - test('runs in sequence with read and write operations', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncWrite, 0, history); - var r2 = _asyncOperation(rw, _asyncRead, 1, history); - var r3 = _asyncOperation(rw, _asyncReadWrite, 2, history); - var r4 = _asyncOperation(rw, _asyncWrite, 3, history); - var r5 = _asyncOperation(rw, _asyncReadWrite, 4, history); - var r6 = _asyncOperation(rw, _asyncRead, 5, history); - await Future.wait([r1, r2, r3, r4, r5, r6]); - - int _index(String str) => history.indexOf(str); - - expect(_index('stopwrite0') < _index('startreadwrite2'), true); - expect(_index('after0') < _index('startreadwrite2'), true); - expect(_index('stopread1') < _index('startreadwrite2'), true); - expect(_index('after1') < _index('startreadwrite2'), true); - - expect(_index('after2') < _index('startwrite3'), true); - expect(_index('after3') < _index('startreadwrite4'), true); - expect(_index('after4') < _index('startread5'), true); - }); - - test('handles errors', () async { - var rw = ReadWriteSync(); - var history = []; - var r1 = _asyncOperation(rw, _asyncReadWrite, 0, history); - var r2 = - _asyncOperation(rw, _asyncReadWrite, 1, history, throwError: true); - var r3 = _asyncOperation(rw, _asyncReadWrite, 2, history); - await Future.wait([r1, r2, r3]); - - expect(history, [ - // - 'before0', 'before1', 'before2', - 'startreadwrite0', 'stopreadwrite0', 'after0', - 'startreadwrite1', 'error1', 'after1', - 'startreadwrite2', 'stopreadwrite2', 'after2' - ]); - }); - }); - }); -} diff --git a/hive/test/tests/backend/vm/storage_backend_vm_test.dart b/hive/test/tests/backend/vm/storage_backend_vm_test.dart deleted file mode 100644 index 3f8ad6de5..000000000 --- a/hive/test/tests/backend/vm/storage_backend_vm_test.dart +++ /dev/null @@ -1,449 +0,0 @@ -@TestOn('vm') -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/vm/read_write_sync.dart'; -import 'package:hive/src/backend/vm/storage_backend_vm.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/io/frame_io_helper.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../../common.dart'; -import '../../frames.dart'; -import '../../mocks.dart'; - -const testMap = { - 'SomeKey': 123, - 'AnotherKey': ['Just', 456, 'a', 333, 'List'], - 'Random Double list': [1.0, 2.0, 10.0, double.infinity], - 'Unicode:': '👋', - 'Null': null, - 'LastKey': true, -}; - -Future getFrameBytes(Iterable frames) async { - var writer = BinaryWriterImpl(testRegistry); - for (var frame in frames) { - writer.writeFrame(frame); - } - return writer.toBytes(); -} - -StorageBackendVm _getBackend({ - File? file, - File? lockFile, - bool crashRecovery = false, - HiveCipher? cipher, - FrameIoHelper? ioHelper, - ReadWriteSync? sync, - RandomAccessFile? readRaf, - RandomAccessFile? writeRaf, -}) { - final backend = StorageBackendVm.debug( - file ?? MockFile(), - lockFile ?? MockFile(), - crashRecovery, - cipher, - ioHelper ?? MockFrameIoHelper(), - sync ?? ReadWriteSync(), - ); - if (readRaf != null) { - backend.readRaf = readRaf; - } - if (writeRaf != null) { - backend.writeRaf = writeRaf; - } - return backend; -} - -void main() { - setUpAll(() { - registerFallbackValue(KeystoreFake()); - registerFallbackValue(TypeRegistryFake()); - }); - - group('StorageBackendVm', () { - test('.path returns path for of open box file', () { - var file = File('some/path'); - var backend = _getBackend(file: file); - expect(backend.path, 'some/path'); - }); - - test('.supportsCompaction is true', () { - var backend = _getBackend(); - expect(backend.supportsCompaction, true); - }); - - group('.open()', () { - test('readFile & writeFile', () async { - var file = MockFile(); - var readRaf = MockRandomAccessFile(); - var writeRaf = MockRandomAccessFile(); - when(() => file.open()).thenAnswer((i) => Future.value(readRaf)); - when(() => file.open(mode: FileMode.writeOnlyAppend)) - .thenAnswer((i) => Future.value(writeRaf)); - when(() => writeRaf.length()).thenAnswer((_) => Future.value(0)); - - var backend = _getBackend(file: file); - await backend.open(); - expect(backend.readRaf, readRaf); - expect(backend.writeRaf, writeRaf); - }); - - test('writeOffset', () async { - var file = MockFile(); - var writeFile = MockRandomAccessFile(); - var readRaf = MockRandomAccessFile(); - when(() => file.open(mode: FileMode.writeOnlyAppend)) - .thenAnswer((i) => Future.value(writeFile)); - when(() => file.open()).thenAnswer((i) => Future.value(readRaf)); - when(() => writeFile.length()).thenAnswer((i) => Future.value(123)); - - var backend = _getBackend(file: file); - await backend.open(); - expect(backend.writeOffset, 123); - }); - }); - - group('.initialize()', () { - File getLockFile() { - var lockMockFile = MockFile(); - when(() => lockMockFile.open(mode: FileMode.write)) - .thenAnswer((i) => Future.value(MockRandomAccessFile())); - return lockMockFile; - } - - FrameIoHelper getFrameIoHelper(int recoveryOffset) { - var helper = MockFrameIoHelper(); - when(() => helper.framesFromFile( - any(), - any(), - any(), - any(), - )).thenAnswer((i) => Future.value(recoveryOffset)); - when(() => helper.keysFromFile( - any(), - any(), - any(), - )).thenAnswer((i) => Future.value(recoveryOffset)); - return helper; - } - - void runTests(bool lazy) { - test('opens lock file and acquires lock', () async { - var lockFile = MockFile(); - var lockRaf = MockRandomAccessFile(); - when(() => lockFile.open(mode: FileMode.write)) - .thenAnswer((i) => Future.value(lockRaf)); - when(() => lockRaf.lock()).thenAnswer((i) => Future.value(lockRaf)); - - var backend = _getBackend( - lockFile: lockFile, - ioHelper: getFrameIoHelper(-1), - ); - when(() => backend.path).thenReturn('nullPath'); - - await backend.initialize( - TypeRegistryImpl.nullImpl, MockKeystore(), lazy); - verify(() => lockRaf.lock()); - }); - - test('recoveryOffset with crash recovery', () async { - var writeRaf = MockRandomAccessFile(); - var lockFile = getLockFile(); - var lockRaf = MockRandomAccessFile(); - - var backend = _getBackend( - lockFile: lockFile, - ioHelper: getFrameIoHelper(20), - crashRecovery: true, - writeRaf: writeRaf, - ); - when(() => backend.path).thenReturn('nullPath'); - when(() => lockFile.open(mode: FileMode.write)) - .thenAnswer((i) => Future.value(lockRaf)); - when(() => lockRaf.lock()).thenAnswer((i) => Future.value(lockRaf)); - when(() => writeRaf.truncate(20)) - .thenAnswer((i) => Future.value(writeRaf)); - when(() => writeRaf.setPosition(20)) - .thenAnswer((i) => Future.value(writeRaf)); - - await backend.initialize( - TypeRegistryImpl.nullImpl, MockKeystore(), lazy); - verify(() => writeRaf.truncate(20)); - verify(() => writeRaf.setPosition(20)); - }); - - test('recoveryOffset without crash recovery', () async { - var lockFile = getLockFile(); - var lockRaf = MockRandomAccessFile(); - - var backend = _getBackend( - lockFile: lockFile, - ioHelper: getFrameIoHelper(20), - crashRecovery: false, - ); - when(() => backend.path).thenReturn('nullPath'); - when(() => lockFile.open(mode: FileMode.write)) - .thenAnswer((i) => Future.value(lockRaf)); - when(() => lockRaf.lock()).thenAnswer((i) => Future.value(lockRaf)); - - await expectLater( - () => backend.initialize( - TypeRegistryImpl.nullImpl, MockKeystore(), lazy), - throwsHiveError('corrupted')); - }); - } - - group('(not lazy)', () { - runTests(false); - }); - - group('(lazy)', () { - runTests(true); - }); - }); - - group('.readValue()', () { - test('reads value with offset', () async { - var frameBytes = getFrameBytes([Frame('test', 123)]); - var readRaf = await getTempRaf([1, 2, 3, 4, 5, ...await frameBytes]); - - var backend = _getBackend(readRaf: readRaf) - // The registry needs to be initialized before reading values, and - // because we do not call StorageBackendVM.initialize(), we set it - // manually. - ..registry = TypeRegistryImpl.nullImpl; - var value = await backend.readValue( - Frame('test', 123, length: (await frameBytes).length, offset: 5), - ); - expect(value, 123); - - await readRaf.close(); - }); - - test('throws exception when frame cannot be read', () async { - var readRaf = await getTempRaf([1, 2, 3, 4, 5]); - var backend = _getBackend(readRaf: readRaf) - // The registry needs to be initialized before reading values, and - // because we do not call StorageBackendVM.initialize(), we set it - // manually. - ..registry = TypeRegistryImpl.nullImpl; - - var frame = Frame('test', 123, length: frameBytes.length, offset: 0); - await expectLater( - () => backend.readValue(frame), throwsHiveError('corrupted')); - - await readRaf.close(); - }); - }); - - group('.writeFrames()', () { - test('writes bytes', () async { - var frames = [Frame('key1', 'value'), Frame('key2', null)]; - var bytes = await getFrameBytes(frames); - - var writeRaf = MockRandomAccessFile(); - when(() => writeRaf.setPosition(0)) - .thenAnswer((i) => Future.value(writeRaf)); - when(() => writeRaf.writeFrom(bytes)) - .thenAnswer((i) => Future.value(writeRaf)); - when(() => writeRaf.flush()) - .thenAnswer((i) => Future.value(writeRaf)); - - var backend = _getBackend(writeRaf: writeRaf) - // The registry needs to be initialized before writing values, and - // because we do not call StorageBackendVM.initialize(), we set it - // manually. - ..registry = TypeRegistryImpl.nullImpl; - - await backend.writeFrames(frames); - await backend.flush(); - verify(() => writeRaf.writeFrom(bytes)); - }); - - test('updates offsets', () async { - var frames = [Frame('key1', 'value'), Frame('key2', null)]; - - var writeRaf = MockRandomAccessFile(); - when(() => writeRaf.setPosition(5)) - .thenAnswer((i) => Future.value(writeRaf)); - when(() => writeRaf.writeFrom(any())) - .thenAnswer((i) => Future.value(writeRaf)); - - var backend = _getBackend(writeRaf: writeRaf) - // The registry needs to be initialized before writing values, and - // because we do not call StorageBackendVM.initialize(), we set it - // manually. - ..registry = TypeRegistryImpl.nullImpl; - backend.writeOffset = 5; - - await backend.writeFrames(frames); - expect(frames, [ - Frame('key1', 'value', length: 24, offset: 5), - Frame('key2', null, length: 15, offset: 29), - ]); - expect(backend.writeOffset, 44); - }); - - test('resets writeOffset on error', () async { - var writeRaf = MockRandomAccessFile(); - when(() => writeRaf.writeFrom(any())).thenThrow('error'); - var backend = _getBackend(writeRaf: writeRaf) - // The registry needs to be initialized before writing values, and - // because we do not call StorageBackendVM.initialize(), we set it - // manually. - ..registry = TypeRegistryImpl.nullImpl; - backend.writeOffset = 123; - - await expectLater(() => backend.writeFrames([Frame('key1', 'value')]), - throwsA(anything)); - verify(() => writeRaf.setPosition(123)); - expect(backend.writeOffset, 123); - }); - }); - - /*group('.compact()', () { - //TODO improve this test - test('check compaction', () async { - var bytes = BytesBuilder(); - var comparisonBytes = BytesBuilder(); - var entries = {}; - - void addFrame(String key, dynamic val, [bool keep = false]) { - var frameBytes = Frame(key, val).toBytes(null, null); - if (keep) { - entries[key] = Frame(key, val, - length: frameBytes.length, offset: bytes.length); - comparisonBytes.add(frameBytes); - } else { - entries.remove(key); - } - bytes.add(frameBytes); - } - - for (var i = 0; i < 1000; i++) { - for (var key in testMap.keys) { - addFrame(key, testMap[key]); - addFrame(key, null); - } - } - - for (var key in testMap.keys) { - addFrame(key, 12345); - addFrame(key, null); - addFrame(key, 'This is a test'); - addFrame(key, testMap[key], true); - } - - var boxFile = await getTempFile(); - await boxFile.writeAsBytes(bytes.toBytes()); - - var syncedFile = SyncedFile(boxFile.path); - await syncedFile.open(); - var backend = StorageBackendVm(syncedFile, null); - - await backend.compact(entries.values); - - var compactedBytes = await File(backend.path).readAsBytes(); - expect(compactedBytes, comparisonBytes.toBytes()); - - await backend.close(); - }); - - test('throws error if corrupted', () async { - var bytes = BytesBuilder(); - var boxFile = await getTempFile(); - var syncedFile = SyncedFile(boxFile.path); - await syncedFile.open(); - - var box = BoxImplVm( - HiveImpl(), path.basename(boxFile.path), BoxOptions(), syncedFile); - await box.put('test', true); - await box.put('test2', 'hello'); - await box.put('test', 'world'); - - await syncedFile.truncate(await boxFile.length() - 1); - - expect(() => box.compact(), throwsHiveError('unexpected eof')); - }); - });*/ - - test('.clear()', () async { - var writeRaf = MockRandomAccessFile(); - when(() => writeRaf.truncate(0)) - .thenAnswer((i) => Future.value(writeRaf)); - when(() => writeRaf.setPosition(0)) - .thenAnswer((i) => Future.value(writeRaf)); - var backend = _getBackend(writeRaf: writeRaf); - backend.writeOffset = 111; - - await backend.clear(); - verify(() => writeRaf.truncate(0)); - verify(() => writeRaf.setPosition(0)); - expect(backend.writeOffset, 0); - }); - - test('.close()', () async { - var readRaf = MockRandomAccessFile(); - var writeRaf = MockRandomAccessFile(); - var lockRaf = MockRandomAccessFile(); - var lockFile = MockFile(); - - returnFutureVoid(when(() => readRaf.close())); - returnFutureVoid(when(() => writeRaf.close())); - returnFutureVoid(when(() => lockRaf.close())); - when(() => lockFile.delete()).thenAnswer((i) => Future.value(lockFile)); - - var backend = _getBackend( - lockFile: lockFile, - readRaf: readRaf, - writeRaf: writeRaf, - ); - backend.lockRaf = lockRaf; - - await backend.close(); - verifyInOrder([ - () => readRaf.close(), - () => writeRaf.close(), - () => lockRaf.close(), - () => lockFile.delete(), - ]); - }); - - test('.deleteFromDisk()', () async { - var readRaf = MockRandomAccessFile(); - var writeRaf = MockRandomAccessFile(); - var lockRaf = MockRandomAccessFile(); - var lockFile = MockFile(); - var file = MockFile(); - - returnFutureVoid(when(() => readRaf.close())); - returnFutureVoid(when(() => writeRaf.close())); - returnFutureVoid(when(() => lockRaf.close())); - when(() => lockFile.delete()).thenAnswer((i) => Future.value(lockFile)); - when(() => file.delete()).thenAnswer((i) => Future.value(file)); - - var backend = _getBackend( - file: file, - lockFile: lockFile, - readRaf: readRaf, - writeRaf: writeRaf, - ); - backend.lockRaf = lockRaf; - - await backend.deleteFromDisk(); - verifyInOrder([ - () => readRaf.close(), - () => writeRaf.close(), - () => lockRaf.close(), - () => lockFile.delete(), - () => file.delete() - ]); - }); - }); -} diff --git a/hive/test/tests/binary/binary_reader_test.dart b/hive/test/tests/binary/binary_reader_test.dart deleted file mode 100644 index dcb461ffd..000000000 --- a/hive/test/tests/binary/binary_reader_test.dart +++ /dev/null @@ -1,625 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/binary_reader_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; -import '../frames.dart'; - -BinaryReader fromByteData(ByteData byteData) { - return BinaryReaderImpl(byteData.buffer.asUint8List(), TypeRegistryImpl()); -} - -BinaryReaderImpl fromBytes(List bytes) { - return BinaryReaderImpl(Uint8List.fromList(bytes), TypeRegistryImpl()); -} - -void main() { - group('BinaryReader', () { - test('.skip()', () { - var byteData = ByteData(20); - var br = fromByteData(byteData); - - expect(br.availableBytes, 20); - expect(br.usedBytes, 0); - - br.skip(5); - expect(br.availableBytes, 15); - expect(br.usedBytes, 5); - - br.skip(0); - expect(br.availableBytes, 15); - expect(br.usedBytes, 5); - - br.skip(15); - expect(br.availableBytes, 0); - expect(br.usedBytes, 20); - - expect(() => br.skip(1), throwsA(anything)); - }); - - test('.readByte()', () { - var byteData = ByteData(3) - ..setUint8(0, 0) - ..setUint8(1, 17) - ..setUint8(2, 255); - var br = fromByteData(byteData); - - expect(br.readByte(), 0); - expect(br.readByte(), 17); - expect(br.readByte(), 255); - expect(() => br.readByte(), throwsA(anything)); - }); - - test('.viewBytes()', () { - var byteData = ByteData(3) - ..setUint8(0, 0) - ..setUint8(1, 17) - ..setUint8(2, 255); - var br = fromByteData(byteData); - - var bytes = br.viewBytes(3); - expect(bytes, [0, 17, 255]); - - byteData.setUint8(1, 57); - expect(bytes, [0, 57, 255]); - - expect(() => br.viewBytes(1), throwsA(anything)); - }); - - test('.peekBytes()', () { - var byteData = ByteData(3) - ..setUint8(0, 0) - ..setUint8(1, 17) - ..setUint8(2, 255); - var br = fromByteData(byteData); - - expect(br.peekBytes(3), [0, 17, 255]); - expect(br.viewBytes(3), [0, 17, 255]); - }); - - test('.readWord()', () { - var byteData = ByteData(4) - ..setUint16(0, 0, Endian.little) - ..setUint16(2, 65535, Endian.little); - var br = fromByteData(byteData); - - expect(br.readWord(), 0); - expect(br.readWord(), 65535); - expect(() => br.readWord(), throwsA(anything)); - }); - - test('.readInt32()', () { - var byteData = ByteData(12) - ..setInt32(0, 0, Endian.little) - ..setInt32(4, 65535, Endian.little) - ..setInt32(8, -65536, Endian.little); - var br = fromByteData(byteData); - - expect(br.readInt32(), 0); - expect(br.readInt32(), 65535); - expect(br.readInt32(), -65536); - expect(() => br.readInt32(), throwsA(anything)); - }); - - test('.readUint32()', () { - var byteData = ByteData(8) - ..setUint32(0, 0, Endian.little) - ..setUint32(4, 4294967295, Endian.little); - var br = fromByteData(byteData); - - expect(br.readUint32(), 0); - expect(br.readUint32(), 4294967295); - expect(() => br.readUint32(), throwsA(anything)); - }); - - test('.readInt()', () { - var byteData = ByteData(24) - ..setFloat64(0, 0, Endian.little) - ..setFloat64(8, (2 ^ 53).toDouble(), Endian.little) - ..setFloat64(16, (-2 ^ 53).toDouble(), Endian.little); - var br = fromByteData(byteData); - - expect(br.readInt(), 0); - expect(br.readInt(), 2 ^ 53); - expect(br.readInt(), -2 ^ 53); - expect(() => br.readInt(), throwsA(anything)); - }); - - test('.readDouble()', () { - var byteData = ByteData(48) - ..setFloat64(0, 0, Endian.little) - ..setFloat64(8, double.nan, Endian.little) - ..setFloat64(16, double.infinity, Endian.little) - ..setFloat64(24, double.negativeInfinity, Endian.little) - ..setFloat64(32, double.maxFinite, Endian.little) - ..setFloat64(40, double.minPositive, Endian.little); - var br = fromByteData(byteData); - - expect(br.readDouble(), 0); - expect(br.readDouble().isNaN, true); - expect(br.readDouble(), double.infinity); - expect(br.readDouble(), double.negativeInfinity); - expect(br.readDouble(), double.maxFinite); - expect(br.readDouble(), double.minPositive); - expect(() => br.readDouble(), throwsA(anything)); - }); - - test('.readBool()', () { - var byteData = ByteData(3) - ..setUint8(0, 1) - ..setUint8(1, 0) - ..setUint8(2, 2); - var br = fromByteData(byteData); - - expect(br.readBool(), true); - expect(br.readBool(), false); - expect(br.readBool(), true); - expect(() => br.readBool(), throwsA(anything)); - }); - - test('.readString()', () { - var br = fromBytes([0, 0, 0, 0]); - expect(br.readString(), ''); - - br = fromBytes([]); - expect(br.readString(0), ''); - - br = fromBytes([ - 12, 0, 0, 0, 0xf0, 0xa0, 0x81, 0xa0, 0xf0, // - 0x9f, 0x87, 0xac, 0xf0, 0x9f, 0x87, 0xb5 // - ]); - expect(br.readString(), '𠁠🇬🇵'); - - br = fromBytes([ - 0xf0, 0x9f, 0x91, 0xa8, 0xe2, 0x80, 0x8d, 0xf0, 0x9f, 0x91, 0xa8, // - 0xe2, 0x80, 0x8d, 0xf0, 0x9f, 0x91, 0xa7, 0xe2, 0x80, 0x8d, 0xf0, // - 0x9f, 0x91, 0xa6 // - ]); - expect(br.readString(25), '👨‍👨‍👧‍👦'); - - expect(() => br.readString(), throwsRangeError); - }); - - test('.readByteList()', () { - var br = fromBytes([0, 0, 0, 0]); - expect(br.readByteList(), []); - - br = fromBytes([]); - expect(br.readByteList(0), []); - - br = fromBytes([4, 0, 0, 0, 1, 2, 3, 4]); - expect(br.readByteList(), [1, 2, 3, 4]); - - br = fromBytes([1, 2, 3, 4]); - expect(br.readByteList(4), [1, 2, 3, 4]); - - expect(() => br.readByteList(1), throwsRangeError); - }); - - test('.readIntList()', () { - var br = fromBytes([0, 0, 0, 0]); - expect(br.readIntList(), []); - - br = fromBytes([]); - expect(br.readIntList(0), []); - - br = fromBytes([ - 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, // - 0, 0, 0, 0, 0, 0, 0, 64 - ]); - expect(br.readIntList(), [1, 2]); - - br = fromBytes([0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64]); - expect(br.readIntList(2), [1, 2]); - - expect(() => br.readIntList(), throwsRangeError); - }); - - test('.readDoubleList()', () { - var br = fromBytes([0, 0, 0, 0]); - expect(br.readDoubleList(), []); - - br = fromBytes([]); - expect(br.readDoubleList(0), []); - - br = fromBytes([1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63]); - expect(br.readDoubleList(), [1.0]); - - br = fromBytes([0, 0, 0, 0, 0, 0, 240, 63]); - expect(br.readDoubleList(1), [1.0]); - - expect(() => br.readDoubleList(), throwsRangeError); - }); - - test('.readBoolList()', () { - var br = fromBytes([0, 0, 0, 0]); - expect(br.readBoolList(), []); - - br = fromBytes([]); - expect(br.readBoolList(0), []); - - br = fromBytes([3, 0, 0, 0, 1, 0, 25]); - expect(br.readBoolList(), [true, false, true]); - - br = fromBytes([1, 0, 136]); - expect(br.readBoolList(3), [true, false, true]); - - expect(() => br.readBoolList(), throwsRangeError); - }); - - test('.readStringList()', () { - var br = fromBytes([0, 0, 0, 0]); - expect(br.readStringList(), []); - - br = fromBytes([]); - expect(br.readStringList(0), []); - - br = fromBytes([ - 2, 0, 0, 0, 1, 0, 0, 0, 97, 13, 0, 0, 0, 0xf0, 0x9f, 0xa7, 0x99, // - 0xe2, 0x80, 0x8d, 0xe2, 0x99, 0x82, 0xef, 0xb8, 0x8f // - ]); - expect(br.readStringList(), ['a', '🧙‍♂️']); - - br = fromBytes([1, 0, 0, 0, 97, 2, 0, 0, 0, 97, 98]); - expect(br.readStringList(2), ['a', 'ab']); - - expect(() => br.readStringList(), throwsRangeError); - }); - - test('.readList()', () { - var br = fromBytes([ - 2, 0, 0, 0, FrameValueType.boolT, 1, // - FrameValueType.stringT, 2, 0, 0, 0, 104, 105 // - ]); - expect(br.readList(), [true, 'hi']); - - br = fromBytes([ - FrameValueType.boolT, 1, // - FrameValueType.stringT, 2, 0, 0, 0, 104, 105 // - ]); - expect(br.readList(2), [true, 'hi']); - - expect(() => br.readList(), throwsRangeError); - }); - - test('.readMap()', () { - var br = fromBytes([ - 2, 0, 0, 0, // - FrameValueType.stringT, 2, 0, 0, 0, 104, 105, // - FrameValueType.boolT, 1, // - FrameValueType.boolT, 0, // - FrameValueType.stringT, 2, 0, 0, 0, 104, 105 // - ]); - expect(br.readMap(), {'hi': true, false: 'hi'}); - - br = fromBytes([ - FrameValueType.stringT, 2, 0, 0, 0, 104, 105, // - FrameValueType.boolT, 1, // - FrameValueType.boolT, 0, // - FrameValueType.stringT, 2, 0, 0, 0, 104, 105 // - ]); - expect(br.readMap(2), {'hi': true, false: 'hi'}); - - expect(() => br.readMap(), throwsA(anything)); - }); - - group('.readKey()', () { - test('int key', () { - var br = fromBytes([0, 123, 0, 0, 0]); - expect(br.readKey(), 123); - }); - - test('string key', () { - var br = fromBytes([1, 2, 104, 105]); - expect(br.readKey(), 'hi'); - }); - - test('wrong key type', () { - var br = fromBytes([2, 0, 0, 0, 0]); - expect(() => br.readKey(), throwsHiveError('unsupported key type')); - }); - }); - - group('.readHiveList()', () { - test('read length', () { - var br = fromBytes([ - 2, 0, 0, 0, // - 3, 66, 111, 120, // - 0, 123, 0, 0, 0, // - 1, 2, 104, 105, // - ]); - var hiveList = br.readHiveList() as HiveListImpl; - expect(hiveList.boxName, 'Box'); - expect(hiveList.keys, [123, 'hi']); - }); - - test('given length', () { - var br = fromBytes([ - 3, 66, 111, 120, // - 0, 123, 0, 0, 0, // - 1, 2, 104, 105, // - ]); - var hiveList = br.readHiveList(2) as HiveListImpl; - expect(hiveList.boxName, 'Box'); - expect(hiveList.keys, [123, 'hi']); - }); - }); - - group('.readFrame()', () { - final List nullFramesBytes = [ - // availableBytes < 4 - // there is ONLY 3 bytes provided - Uint8List.fromList([8, 0, 0]), - // frameLength < 8 - // frame is 7 length - Uint8List.fromList([7, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - // availableBytes < frameLength - 4 - // frame is 10 length however ONLY 9 bytes provided - Uint8List.fromList([10, 0, 0, 0, 0, 0, 0, 0, 0]), - // computedCrc != crc - // 0, 0, 0, 0 crc is: 0 and computedCrc is: 274301637 - Uint8List.fromList([10, 0, 0, 0, 0, 0, 0, 0, 0, 0]), - ]; - - test('null', () async { - for (final bytes in nullFramesBytes) { - final reader = BinaryReaderImpl(bytes, testRegistry); - final frame = await reader.readFrame(lazy: false); - - expect(frame, null); - } - }); - - test('null lazy', () async { - for (final bytes in nullFramesBytes) { - final reader = BinaryReaderImpl(bytes, testRegistry); - final frame = await reader.readFrame(lazy: true); - - expect(frame, null); - } - }); - - test('normal', () async { - var frames = framesSetLengthOffset(testFrames, frameBytes); - var offset = 0; - for (var i = 0; i < frames.length; i++) { - final frame = frames[i]; - var reader = BinaryReaderImpl(frameBytes[i], testRegistry); - final actual = - await reader.readFrame(lazy: false, frameOffset: offset); - expectFrame( - actual!, - frame, - ); - offset += frameBytes[i].length; - } - }); - - test('lazy', () async { - var frames = framesSetLengthOffset(testFrames, frameBytes); - var offset = 0; - for (var i = 0; i < frames.length; i++) { - final frame = frames[i]; - var reader = BinaryReaderImpl(frameBytes[i], testRegistry); - final actual = - await reader.readFrame(lazy: true, frameOffset: offset); - expectFrame( - actual!, - frame.toLazy(), - ); - offset += frameBytes[i].length; - } - }); - - test('encrypted', () async { - var frames = framesSetLengthOffset(testFrames, frameBytesEncrypted); - var offset = 0; - for (var i = 0; i < frames.length; i++) { - final frame = frames[i]; - var reader = BinaryReaderImpl(frameBytesEncrypted[i], testRegistry); - final actual = await reader.readFrame( - lazy: false, - frameOffset: offset, - cipher: testCipher, - ); - expectFrame( - actual!, - frame, - ); - offset += frameBytesEncrypted[i].length; - } - }); - - test('encrypted lazy', () async { - var frames = framesSetLengthOffset(testFrames, frameBytesEncrypted); - var offset = 0; - for (var i = 0; i < frames.length; i++) { - final frame = frames[i]; - var reader = BinaryReaderImpl(frameBytesEncrypted[i], testRegistry); - final actual = await reader.readFrame( - lazy: true, - frameOffset: offset, - cipher: testCipher, - ); - expectFrame( - actual!, - frame.toLazy(), - ); - offset += frameBytesEncrypted[i].length; - } - }); - }); - - group('.read()', () { - test('null', () { - var br = fromBytes([]); - expect(br.read(FrameValueType.nullT), null); - - br = fromBytes([FrameValueType.nullT]); - expect(br.read(), null); - }); - - test('int', () { - var byteData = ByteData(8)..setFloat64(0, 12345, Endian.little); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.intT), 12345); - - byteData = ByteData(9) - ..setUint8(0, FrameValueType.intT) - ..setFloat64(1, 12345, Endian.little); - br = fromByteData(byteData); - expect(br.read(), 12345); - }); - - test('double', () { - var byteData = ByteData(8)..setFloat64(0, 234.99283, Endian.little); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.doubleT), 234.99283); - - byteData = ByteData(9) - ..setUint8(0, FrameValueType.doubleT) - ..setFloat64(1, 234.99283, Endian.little); - br = fromByteData(byteData); - expect(br.read(), 234.99283); - }); - - test('bool', () { - var byteData = ByteData(2)..setUint8(0, 1); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.boolT), true); - - byteData = ByteData(2) - ..setUint8(0, FrameValueType.boolT) - ..setInt8(1, 1); - br = fromByteData(byteData); - expect(br.read(), true); - }); - - test('string', () { - var br = fromBytes([2, 0, 0, 0, 104, 105]); - expect(br.read(FrameValueType.stringT), 'hi'); - - br = fromBytes([FrameValueType.stringT, 2, 0, 0, 0, 104, 105]); - expect(br.read(), 'hi'); - }); - - test('byte list', () { - var br = fromBytes([ - 5, 0, 0, 0, // - 1, 2, 3, 4, 5, // - ]); - expect(br.read(FrameValueType.byteListT), [1, 2, 3, 4, 5]); - - br = fromBytes([ - FrameValueType.byteListT, // - 5, 0, 0, 0, // - 1, 2, 3, 4, 5, // - ]); - expect(br.read(), [1, 2, 3, 4, 5]); - }); - - test('int list', () { - var byteData = ByteData(20) - ..setUint32(0, 2, Endian.little) - ..setFloat64(4, 12345, Endian.little) - ..setFloat64(12, 123, Endian.little); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.intListT), [12345, 123]); - - byteData = ByteData(21) - ..setUint8(0, FrameValueType.intListT) - ..setUint32(1, 2, Endian.little) - ..setFloat64(5, 12345, Endian.little) - ..setFloat64(13, 123, Endian.little); - br = fromByteData(byteData); - expect(br.read(), [12345, 123]); - }); - - test('double list', () { - var byteData = ByteData(20) - ..setUint32(0, 2, Endian.little) - ..setFloat64(4, 11.11, Endian.little) - ..setFloat64(12, 12.12, Endian.little); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.doubleListT), [11.11, 12.12]); - - byteData = ByteData(21) - ..setUint8(0, FrameValueType.doubleListT) - ..setUint32(1, 2, Endian.little) - ..setFloat64(5, 11.11, Endian.little) - ..setFloat64(13, 12.12, Endian.little); - br = fromByteData(byteData); - expect(br.read(), [11.11, 12.12]); - }); - - test('bool list', () { - var byteData = ByteData(6) - ..setUint32(0, 2, Endian.little) - ..setUint8(4, 0) - ..setUint8(5, 1); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.boolListT), [false, true]); - - byteData = ByteData(7) - ..setUint8(0, FrameValueType.boolListT) - ..setUint32(1, 2, Endian.little) - ..setUint8(5, 0) - ..setUint8(6, 1); - br = fromByteData(byteData); - expect(br.read(), [false, true]); - }); - - test('string list', () { - var br = fromBytes([2, 0, 0, 0, 2, 0, 0, 0, 104, 105, 1, 0, 0, 0, 104]); - expect(br.read(FrameValueType.stringListT), ['hi', 'h']); - - br = fromBytes([ - FrameValueType.stringListT, - 2, 0, 0, 0, 2, 0, 0, 0, 104, 105, 1, 0, 0, 0, 104 // - ]); - expect(br.read(), ['hi', 'h']); - }); - - test('list with null', () { - var byteData = ByteData(23) - ..setUint32(0, 3, Endian.little) - ..setUint8(4, FrameValueType.intT) - ..setFloat64(5, 12345, Endian.little) - ..setUint8(13, FrameValueType.intT) - ..setFloat64(14, 123, Endian.little) - ..setUint8(22, FrameValueType.nullT); - var br = fromByteData(byteData); - expect(br.read(FrameValueType.listT), [12345, 123, null]); - - byteData = ByteData(24) - ..setInt8(0, FrameValueType.listT) - ..setUint32(1, 3, Endian.little) - ..setUint8(5, FrameValueType.intT) - ..setFloat64(6, 12345, Endian.little) - ..setUint8(14, FrameValueType.intT) - ..setFloat64(15, 123, Endian.little) - ..setUint8(23, FrameValueType.nullT); - br = fromByteData(byteData); - expect(br.read(), [12345, 123, null]); - }); - - test('HiveList', () { - var br = fromBytes([ - FrameValueType.hiveListT, 2, 0, 0, 0, // - 3, 66, 111, 120, // - 0, 123, 0, 0, 0, // - 1, 2, 104, 105, // - ]); - - var hiveList = br.read() as HiveListImpl; - expect(hiveList.boxName, 'Box'); - expect(hiveList.keys, [123, 'hi']); - }); - }); - }); -} diff --git a/hive/test/tests/binary/binary_writer_test.dart b/hive/test/tests/binary/binary_writer_test.dart deleted file mode 100644 index 3df8ad38d..000000000 --- a/hive/test/tests/binary/binary_writer_test.dart +++ /dev/null @@ -1,568 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/object/hive_object.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../frames.dart'; -import '../mocks.dart'; - -List bytes(ByteData byteData) => byteData.buffer.asUint8List(); - -BinaryWriterImpl getWriter() => BinaryWriterImpl(TypeRegistryImpl()); -void main() { - group('BinaryWriter', () { - test('.writeByte()', () { - var bw = getWriter(); - bw.writeByte(0); - expect(bw.toBytes(), [0]); - - bw = getWriter(); - bw.writeByte(17); - expect(bw.toBytes(), [17]); - - bw = getWriter(); - bw.writeByte(255); - expect(bw.toBytes(), [255]); - - bw = getWriter(); - bw.writeByte(257); - expect(bw.toBytes(), [1]); - }); - - test('.writeWord()', () { - var bw = getWriter(); - bw.writeWord(0); - expect(bw.toBytes(), [0, 0]); - - bw = getWriter(); - bw.writeWord(256); - expect(bw.toBytes(), [0, 1]); - - bw = getWriter(); - bw.writeWord(65535); - expect(bw.toBytes(), [255, 255]); - - bw = getWriter(); - bw.writeWord(65536); - expect(bw.toBytes(), [0, 0]); - }); - - test('.writeInt32()', () { - var bd = ByteData(4); - - var bw = getWriter(); - bw.writeInt32(0); - bd.setInt32(0, 0, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt32(1); - bd.setInt32(0, 1, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt32(-1); - bd.setInt32(0, -1, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt32(65535); - bd.setInt32(0, 65535, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt32(65536); - bd.setInt32(0, 65536, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt32(-65536); - bd.setInt32(0, -65536, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt32(-65537); - bd.setInt32(0, -65537, Endian.little); - expect(bw.toBytes(), bytes(bd)); - }); - - test('.writeUint32()', () { - var bd = ByteData(4); - - var bw = getWriter(); - bw.writeUint32(0); - bd.setUint32(0, 0, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeUint32(1); - bd.setUint32(0, 1, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeUint32(2147483647); - bd.setUint32(0, 2147483647, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeUint32(-2147483648); - bd.setUint32(0, -2147483648, Endian.little); - expect(bw.toBytes(), bytes(bd)); - }); - - test('.writeInt()', () { - var bd = ByteData(8); - - var bw = getWriter(); - bw.writeInt(0); - bd.setFloat64(0, 0, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt(1); - bd.setFloat64(0, 1, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt(-1); - bd.setFloat64(0, -1, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt(2 ^ 53); - bd.setFloat64(0, (2 ^ 53).toDouble(), Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeInt(-2 ^ 53); - bd.setFloat64(0, (-2 ^ 53).toDouble(), Endian.little); - expect(bw.toBytes(), bytes(bd)); - }); - - test('.writeDouble()', () { - var bd = ByteData(8); - - var bw = getWriter(); - bw.writeDouble(0); - bd.setFloat64(0, 0, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeDouble(16.399483); - bd.setFloat64(0, 16.399483, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeDouble(double.nan); - bd.setFloat64(0, double.nan, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeDouble(double.infinity); - bd.setFloat64(0, double.infinity, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeDouble(double.negativeInfinity); - bd.setFloat64(0, double.negativeInfinity, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeDouble(double.maxFinite); - bd.setFloat64(0, double.maxFinite, Endian.little); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.writeDouble(double.minPositive); - bd.setFloat64(0, double.minPositive, Endian.little); - expect(bw.toBytes(), bytes(bd)); - }); - - test('.writeBool()', () { - var bw = getWriter(); - bw.writeBool(true); - expect(bw.toBytes(), [1]); - - bw = getWriter(); - bw.writeBool(false); - expect(bw.toBytes(), [0]); - }); - - test('.writeString()', () { - var bw = getWriter(); - bw.writeString(''); - expect(bw.toBytes(), [0, 0, 0, 0]); - - bw = getWriter(); - bw.writeString('', writeByteCount: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.writeString('𠁠🇬🇵'); - expect(bw.toBytes(), [ - 12, 0, 0, 0, 0xf0, 0xa0, 0x81, 0xa0, 0xf0, // - 0x9f, 0x87, 0xac, 0xf0, 0x9f, 0x87, 0xb5 // - ]); - - bw = getWriter(); - bw.writeString('👨‍👨‍👧‍👦', writeByteCount: false); - expect(bw.toBytes(), [ - 0xf0, 0x9f, 0x91, 0xa8, 0xe2, 0x80, 0x8d, 0xf0, 0x9f, 0x91, 0xa8, // - 0xe2, 0x80, 0x8d, 0xf0, 0x9f, 0x91, 0xa7, 0xe2, 0x80, 0x8d, 0xf0, // - 0x9f, 0x91, 0xa6 // - ]); - }); - - test('.writeByteList()', () { - var bw = getWriter(); - bw.writeByteList([]); - expect(bw.toBytes(), [0, 0, 0, 0]); - - bw = getWriter(); - bw.writeByteList([], writeLength: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.writeByteList([1, 2, 3, 4]); - expect(bw.toBytes(), [4, 0, 0, 0, 1, 2, 3, 4]); - - bw = getWriter(); - bw.writeByteList([1, 2, 3, 4], writeLength: false); - expect(bw.toBytes(), [1, 2, 3, 4]); - }); - - test('.writeIntList()', () { - var bw = getWriter(); - bw.writeIntList([]); - expect(bw.toBytes(), [0, 0, 0, 0]); - - bw = getWriter(); - bw.writeIntList([], writeLength: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.writeIntList([1, 2]); - expect(bw.toBytes(), - [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64]); - - bw = getWriter(); - bw.writeIntList([1, 2], writeLength: false); - expect( - bw.toBytes(), [0, 0, 0, 0, 0, 0, 240, 63, 0, 0, 0, 0, 0, 0, 0, 64]); - }); - - test('.writeDoubleList()', () { - var bw = getWriter(); - bw.writeDoubleList([]); - expect(bw.toBytes(), [0, 0, 0, 0]); - - bw = getWriter(); - bw.writeDoubleList([], writeLength: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.writeDoubleList([1.0]); - expect(bw.toBytes(), [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 240, 63]); - - bw = getWriter(); - bw.writeDoubleList([1.0], writeLength: false); - expect(bw.toBytes(), [0, 0, 0, 0, 0, 0, 240, 63]); - }); - - test('.writeBoolList()', () { - var bw = getWriter(); - bw.writeBoolList([]); - expect(bw.toBytes(), [0, 0, 0, 0]); - - bw = getWriter(); - bw.writeBoolList([], writeLength: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.writeBoolList([true, false, true]); - expect(bw.toBytes(), [3, 0, 0, 0, 1, 0, 1]); - - bw = getWriter(); - bw.writeBoolList([true, false, true], writeLength: false); - expect(bw.toBytes(), [1, 0, 1]); - }); - - test('.writeStringList()', () { - var bw = getWriter(); - bw.writeStringList([]); - expect(bw.toBytes(), [0, 0, 0, 0]); - - bw = getWriter(); - bw.writeStringList([], writeLength: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.writeStringList(['a', '🧙‍♂️']); - expect(bw.toBytes(), [ - 2, 0, 0, 0, 1, 0, 0, 0, 97, 13, 0, 0, 0, 0xf0, 0x9f, 0xa7, // - 0x99, 0xe2, 0x80, 0x8d, 0xe2, 0x99, 0x82, 0xef, 0xb8, 0x8f // - ]); - - bw = getWriter(); - bw.writeStringList(['a', 'ab'], writeLength: false); - expect(bw.toBytes(), [1, 0, 0, 0, 97, 2, 0, 0, 0, 97, 98]); - }); - - test('.writeList()', () { - var bw = getWriter(); - bw.writeList(['h', true]); - expect(bw.toBytes(), [ - 2, 0, 0, 0, // - FrameValueType.stringT, 1, 0, 0, 0, 0x68, // - FrameValueType.boolT, 1 // - ]); - - bw = getWriter(); - bw.writeList(['h', true], writeLength: false); - expect(bw.toBytes(), [ - FrameValueType.stringT, 1, 0, 0, 0, 0x68, // - FrameValueType.boolT, 1 // - ]); - }); - - test('.writeMap()', () { - var bw = getWriter(); - bw.writeMap({true: 'h', 'hi': true}); - expect(bw.toBytes(), [ - 2, 0, 0, 0, // - FrameValueType.boolT, 1, // - FrameValueType.stringT, 1, 0, 0, 0, 0x68, // - FrameValueType.stringT, 2, 0, 0, 0, 0x68, 0x69, // - FrameValueType.boolT, 1 // - ]); - - bw = getWriter(); - bw.writeMap({true: 'h', 'hi': true}, writeLength: false); - expect(bw.toBytes(), [ - FrameValueType.boolT, 1, // - FrameValueType.stringT, 1, 0, 0, 0, 0x68, // - FrameValueType.stringT, 2, 0, 0, 0, 0x68, 0x69, // - FrameValueType.boolT, 1 // - ]); - }); - - group('.writeHiveList()', () { - var box = MockBox(); - when(() => box.name).thenReturn('Box'); - - var obj = TestHiveObject()..init('key', box); - - test('write length', () { - var list = HiveList(box, objects: [obj]); - var bw = getWriter(); - bw.writeHiveList(list); - - expect(bw.toBytes(), [ - 1, 0, 0, 0, 3, 66, 111, 120, // - 1, 3, 107, 101, 121, // - ]); - }); - - test('omit length', () { - var list = HiveList(box, objects: [obj]); - var bw = getWriter(); - bw.writeHiveList(list, writeLength: false); - - expect(bw.toBytes(), [ - 3, 66, 111, 120, // - 1, 3, 107, 101, 121, // - ]); - }); - }); - - group('.writeFrame()', () { - test('normal', () async { - for (var i = 0; i < testFrames.length; i++) { - final frame = testFrames[i]; - var writer = BinaryWriterImpl(testRegistry); - expect(await writer.writeFrame(frame), frameBytes[i].length); - expect(writer.toBytes(), frameBytes[i]); - } - }); - - test('encrypted', () async { - for (var i = 0; i < testFrames.length; i++) { - final frame = testFrames[i]; - var writer = BinaryWriterImpl(testRegistry); - expect(await writer.writeFrame(frame, cipher: testCipher), - frameBytesEncrypted[i].length); - expect(writer.toBytes(), frameBytesEncrypted[i]); - } - }); - }); - - group('.write()', () { - test('null', () { - var bw = getWriter(); - bw.write(null, writeTypeId: false); - expect(bw.toBytes(), []); - - bw = getWriter(); - bw.write(null, writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.nullT]); - }); - - test('int', () { - var bd = ByteData(8)..setFloat64(0, 12345, Endian.little); - - var bw = getWriter(); - bw.write(12345, writeTypeId: false); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.write(12345, writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.intT, ...bytes(bd)]); - }); - - test('double', () { - var bd = ByteData(8)..setFloat64(0, 123.456, Endian.little); - - var bw = getWriter(); - bw.write(123.456, writeTypeId: false); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.write(123.456, writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.doubleT, ...bytes(bd)]); - }); - - test('bool', () { - var bw = getWriter(); - bw.write(true, writeTypeId: false); - expect(bw.toBytes(), [1]); - - bw = getWriter(); - bw.write(true, writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.boolT, 1]); - }); - - test('string', () { - var bw = getWriter(); - bw.write('hi', writeTypeId: false); - expect(bw.toBytes(), [2, 0, 0, 0, 0x68, 0x69]); - - bw = getWriter(); - bw.write('hi', writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.stringT, 2, 0, 0, 0, 0x68, 0x69]); - }); - - test('HiveList', () { - var box = MockBox(); - when(() => box.name).thenReturn('Box'); - - var obj = TestHiveObject()..init('key', box); - var list = HiveList(box, objects: [obj]); - var bw = getWriter(); - bw.write(list); - - expect(bw.toBytes(), [ - FrameValueType.hiveListT, - 1, 0, 0, 0, 3, 66, 111, 120, // - 1, 3, 107, 101, 121, // - ]); - }); - - test('byte list', () { - var bw = getWriter(); - bw.write(Uint8List.fromList([1, 2, 3, 4]), writeTypeId: false); - expect(bw.toBytes(), [4, 0, 0, 0, 1, 2, 3, 4]); - - bw = getWriter(); - bw.write(Uint8List.fromList([1, 2, 3, 4]), writeTypeId: true); - expect( - bw.toBytes(), [FrameValueType.byteListT, 4, 0, 0, 0, 1, 2, 3, 4]); - }); - - test('int list', () { - var bd = ByteData(20) - ..setUint32(0, 2, Endian.little) - ..setFloat64(4, 123, Endian.little) - ..setFloat64(12, 45, Endian.little); - - var bw = getWriter(); - bw.write([123, 45], writeTypeId: false); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.write([123, 45], writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.intListT, ...bytes(bd)]); - }); - - test('double list', () { - var bd = ByteData(20) - ..setUint32(0, 2, Endian.little) - ..setFloat64(4, 123.456, Endian.little) - ..setFloat64(12, 456.321, Endian.little); - - var bw = getWriter(); - bw.write([123.456, 456.321], writeTypeId: false); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.write([123.456, 456.321], writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.doubleListT, ...bytes(bd)]); - }); - - test('bool list', () { - var bd = ByteData(6) - ..setUint32(0, 2, Endian.little) - ..setUint8(4, 0) - ..setUint8(5, 1); - - var bw = getWriter(); - bw.write([false, true], writeTypeId: false); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.write([false, true], writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.boolListT, ...bytes(bd)]); - }); - - test('string list', () { - var bw = getWriter(); - bw.write(['h', 'hi'], writeTypeId: false); - expect(bw.toBytes(), [ - 2, 0, 0, 0, // - 1, 0, 0, 0, 0x68, // - 2, 0, 0, 0, 0x68, 0x69, // - ]); - - bw = getWriter(); - bw.write(['h', 'hi'], writeTypeId: true); - expect(bw.toBytes(), [ - FrameValueType.stringListT, 2, 0, 0, 0, // - 1, 0, 0, 0, 0x68, // - 2, 0, 0, 0, 0x68, 0x69, // - ]); - }); - - test('list with null', () { - var bd = ByteData(23) - ..setUint32(0, 3, Endian.little) - ..setUint8(4, FrameValueType.intT) - ..setFloat64(5, 123, Endian.little) - ..setUint8(13, FrameValueType.intT) - ..setFloat64(14, 45, Endian.little) - ..setUint8(22, FrameValueType.nullT); - - var bw = getWriter(); - bw.write([123, 45, null], writeTypeId: false); - expect(bw.toBytes(), bytes(bd)); - - bw = getWriter(); - bw.write([123, 45, null], writeTypeId: true); - expect(bw.toBytes(), [FrameValueType.listT, ...bytes(bd)]); - }); - }); - }); -} diff --git a/hive/test/tests/binary/frame_test.dart b/hive/test/tests/binary/frame_test.dart deleted file mode 100644 index d4f9d7a71..000000000 --- a/hive/test/tests/binary/frame_test.dart +++ /dev/null @@ -1,63 +0,0 @@ -import 'package:hive/src/binary/frame.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; - -void main() { - group('Frame', () { - group('constructors verifies', () { - test('int keys', () { - Frame(0, null); - Frame.lazy(0); - Frame.deleted(0); - - Frame(4294967295, null); - Frame.lazy(4294967295); - Frame.deleted(4294967295); - - expect(() => Frame(-1, null), throwsHiveError()); - expect(() => Frame.lazy(-1), throwsHiveError()); - expect(() => Frame.deleted(-1), throwsHiveError()); - - expect(() => Frame(4294967296, null), throwsHiveError()); - expect(() => Frame.lazy(4294967296), throwsHiveError()); - expect(() => Frame.deleted(4294967296), throwsHiveError()); - }); - - test('string keys', () { - Frame('', null); - Frame.lazy(''); - Frame.deleted(''); - - Frame('a' * 255, null); - Frame.lazy('a' * 255); - Frame.deleted('a' * 255); - - Frame('hellö', null); - Frame.lazy('hellö'); - Frame.deleted('hellö'); - - expect(() => Frame('a' * 256, null), throwsHiveError()); - expect(() => Frame.lazy('a' * 256), throwsHiveError()); - expect(() => Frame.deleted('a' * 256), throwsHiveError()); - }); - - test('non int or string keys', () { - expect(() => Frame(null, null), throwsHiveError()); - expect(() => Frame(true, null), throwsHiveError()); - expect(() => Frame(Object(), null), throwsHiveError()); - expect(() => Frame(() => 0, null), throwsHiveError()); - expect(() => Frame(Frame('test', null), null), throwsHiveError()); - }); - }); - - test('.toString()', () { - expect(Frame('key', 'val', offset: 1, length: 2).toString(), - 'Frame(key: key, value: val, length: 2, offset: 1)'); - expect(Frame.lazy('key', offset: 1, length: 2).toString(), - 'Frame.lazy(key: key, length: 2, offset: 1)'); - expect(Frame.deleted('key', length: 2).toString(), - 'Frame.deleted(key: key, length: 2)'); - }); - }); -} diff --git a/hive/test/tests/box/box_base_test.dart b/hive/test/tests/box/box_base_test.dart deleted file mode 100644 index c31917d65..000000000 --- a/hive/test/tests/box/box_base_test.dart +++ /dev/null @@ -1,442 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/box_base_impl.dart'; - -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; -import '../common.dart'; - -import '../mocks.dart'; - -class _BoxBaseMock extends BoxBaseImpl with Mock { - _BoxBaseMock( - HiveImpl hive, - String name, - KeyComparator? keyComparator, - CompactionStrategy compactionStrategy, - StorageBackend backend, - ) : super( - hive, - name, - keyComparator, - compactionStrategy, - backend, - ); - - @override - Future flush() => Future.value(); -} - -_BoxBaseMock _openBoxBaseMock({ - HiveImpl? hive, - String? name, - Keystore? keystore, - CompactionStrategy? cStrategy, - StorageBackend? backend, -}) { - hive ??= HiveImpl(); - name ??= 'testBox'; - backend ??= MockStorageBackend(); - var mock = _BoxBaseMock( - hive, - name, - null, - cStrategy ?? (_, __) => false, - backend, - ); - mock.keystore = keystore ?? Keystore(mock, ChangeNotifier(), null); - return mock; -} - -void main() { - setUpAll(() { - registerFallbackValue(KeystoreFake()); - registerFallbackValue(TypeRegistryFake()); - }); - - group('BoxBase', () { - test('.name', () { - var box = _openBoxBaseMock(name: 'testName'); - expect(box.name, 'testName'); - }); - - test('.path', () { - var backend = MockStorageBackend(); - when(() => backend.path).thenReturn('some/path'); - - var box = _openBoxBaseMock(backend: backend); - expect(box.path, 'some/path'); - }); - - group('.keys', () { - test('returns keys from keystore', () { - var box = _openBoxBaseMock(); - box.keystore - ..insert(Frame.lazy('key1')) - ..insert(Frame.lazy('key4')) - ..insert(Frame.lazy('key2')); - expect(box.keys, ['key1', 'key2', 'key4']); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.keys, throwsHiveError('closed')); - }); - }); - - group('.length / .isEmpty / .isNotEmpty', () { - test('empty box', () { - var box = _openBoxBaseMock(); - expect(box.length, 0); - expect(box.isEmpty, true); - expect(box.isNotEmpty, false); - }); - - test('non empty box', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', null), - Frame('key2', null), - ]); - var box = _openBoxBaseMock(keystore: keystore); - expect(box.length, 2); - expect(box.isEmpty, false); - expect(box.isNotEmpty, true); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.length, throwsHiveError('closed')); - expect(() => box.isEmpty, throwsHiveError('closed')); - expect(() => box.isNotEmpty, throwsHiveError('closed')); - }); - }); - - group('.watch()', () { - test('calls keystore.watch()', () { - var keystore = MockKeystore(); - var box = _openBoxBaseMock(keystore: keystore); - when(() => keystore.watch(key: 123)).thenAnswer((_) => Stream.empty()); - - box.watch(key: 123); - verify(() => keystore.watch(key: 123)); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.watch(), throwsHiveError('closed')); - }); - }); - - group('.keyAt()', () { - test('returns key at index', () { - var box = _openBoxBaseMock(); - box.keystore - ..insert(Frame.lazy(0)) - ..insert(Frame.lazy('test')); - expect(box.keyAt(1), 'test'); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.keyAt(0), throwsHiveError('closed')); - }); - }); - - test('.initialize()', () async { - var backend = MockStorageBackend(); - var box = _openBoxBaseMock(backend: backend); - when(() => box.lazy).thenReturn(false); - - when(() => backend.initialize(any(), any(), any())).thenAnswer((i) async { - i.positionalArguments[1].insert(Frame('key1', 1)); - }); - - await box.initialize(); - expect(box.keystore.frames, [Frame('key1', 1)]); - }); - - group('.containsKey()', () { - test('returns true if key exists', () { - var box = _openBoxBaseMock(); - box.keystore.insert(Frame.lazy('existingKey')); - expect(box.containsKey('existingKey'), true); - }); - - test('returns false if key does not exist', () { - var box = _openBoxBaseMock(); - box.keystore.insert(Frame.lazy('existingKey')); - expect(box.containsKey('nonExistingKey'), false); - }); - - test('does not use backend', () { - var backend = MockStorageBackend(); - var box = _openBoxBaseMock(backend: backend); - box.keystore.insert(Frame.lazy('existingKey')); - - box.containsKey('existingKey'); - box.containsKey('nonExistingKey'); - verifyZeroInteractions(backend); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.containsKey(0), throwsHiveError('closed')); - }); - }); - - group('.add()', () { - test('calls put()', () async { - var box = _openBoxBaseMock(); - when(() => box.put(0, 123)).thenAnswer((i) => Future.value()); - - expect(await box.add(123), 0); - verify(() => box.put(0, 123)); - }); - - test('updates auto increment', () async { - var box = _openBoxBaseMock(); - returnFutureVoid(when(() => box.putAll({5: 123}))); - - box.keystore.updateAutoIncrement(4); - expect(await box.add(123), 5); - }); - }); - - test('.addAll()', () async { - var box = _openBoxBaseMock(); - box.keystore.updateAutoIncrement(4); - final vals = {5: 1, 6: 2, 7: 3}; - returnFutureVoid(when(() => box.putAll(vals))); - - expect(await box.addAll([1, 2, 3]), [5, 6, 7]); - expect(box.keystore.autoIncrement(), 8); - verify(() => box.putAll(vals)); - }); - - group('.putAt()', () { - test('override existing', () async { - var box = _openBoxBaseMock(); - returnFutureVoid(when(() => box.putAll({'b': 'test'}))); - - box.keystore.insert(Frame.lazy('a')); - box.keystore.insert(Frame.lazy('b')); - - await box.putAt(1, 'test'); - verify(() => box.put('b', 'test')); - }); - - test('throws RangeError for negative index', () async { - var box = _openBoxBaseMock(); - box.keystore.insert(Frame.lazy('a')); - - await expectLater( - () async => await box.putAt(-1, 'test'), throwsRangeError); - }); - - test('throws RangeError for index out of bounds', () async { - var box = _openBoxBaseMock(); - box.keystore.insert(Frame.lazy('a')); - - await expectLater( - () async => await box.putAt(1, 'test'), throwsRangeError); - }); - }); - - group('.deleteAt()', () { - test('delete frame', () async { - var box = _openBoxBaseMock(); - returnFutureVoid(when(() => box.deleteAll(['b']))); - - box.keystore.insert(Frame.lazy('a')); - box.keystore.insert(Frame.lazy('b')); - - await box.deleteAt(1); - verify(() => box.delete('b')); - }); - - test('throws RangeError for negative index', () async { - var box = _openBoxBaseMock(); - box.keystore.insert(Frame.lazy('a')); - - await expectLater(() async => await box.deleteAt(-1), throwsRangeError); - }); - - test('throws RangeError for index out of bounds', () async { - var box = _openBoxBaseMock(); - box.keystore.insert(Frame.lazy('a')); - - await expectLater(() async => await box.deleteAt(1), throwsRangeError); - }); - }); - - group('.clear()', () { - test('clears keystore and backend', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - - returnFutureVoid(when(() => backend.clear())); - - when(() => keystore.clear()).thenReturn(2); - - var box = _openBoxBaseMock(backend: backend, keystore: keystore); - - expect(await box.clear(), 2); - verifyInOrder([ - () => backend.clear(), - () => keystore.clear(), - ]); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.clear(), throwsHiveError('closed')); - }); - }); - - group('.compact()', () { - test('does nothing if backend does not support compaction', () async { - var backend = MockStorageBackend(); - when(() => backend.supportsCompaction).thenReturn(false); - var box = _openBoxBaseMock(backend: backend); - - await box.compact(); - verify(() => backend.supportsCompaction); - verifyNoMoreInteractions(backend); - }); - - test('does nothing if there are no deleted entries', () async { - var backend = MockStorageBackend(); - when(() => backend.supportsCompaction).thenReturn(true); - var box = _openBoxBaseMock(backend: backend); - box.keystore.insert(Frame.lazy('key1')); - - await box.compact(); - verify(() => backend.supportsCompaction); - verifyNoMoreInteractions(backend); - }); - - test('compact', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - - when(() => keystore.frames) - .thenReturn([Frame('key', 1, length: 22, offset: 33)]); - when(() => backend.supportsCompaction).thenReturn(true); - // In case it is 0, we will bail out before compaction - when(() => keystore.deletedEntries).thenReturn(1); - returnFutureVoid( - when( - () => backend.compact([Frame('key', 1, length: 22, offset: 33)])), - ); - - var box = _openBoxBaseMock(backend: backend, keystore: keystore); - - await box.compact(); - verify( - () => backend.compact([Frame('key', 1, length: 22, offset: 33)])); - verify(() => keystore.resetDeletedEntries()); - }); - - test('throws if box is closed', () async { - var backend = MockStorageBackend(); - returnFutureVoid(when(() => backend.close())); - - var box = _openBoxBaseMock(backend: backend); - await box.close(); - expect(() => box.compact(), throwsHiveError('closed')); - }); - }); - - test('.close()', () async { - var hive = MockHiveImpl(); - var keystore = MockKeystore(); - var backend = MockStorageBackend(); - var box = _openBoxBaseMock( - name: 'myBox', - hive: hive, - keystore: keystore, - backend: backend, - ); - returnFutureVoid(when(() => keystore.close())); - returnFutureVoid(when(() => backend.close())); - - await box.close(); - verifyInOrder([ - () => keystore.close(), - () => hive.unregisterBox('myBox'), - () => backend.close(), - ]); - expect(box.isOpen, false); - }); - - group('.deleteFromDisk()', () { - test('only deleted file if box is closed', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - var box = _openBoxBaseMock(backend: backend, keystore: keystore); - returnFutureVoid(when(() => keystore.close())); - returnFutureVoid(when(() => backend.close())); - - returnFutureVoid(when(() => backend.deleteFromDisk())); - - await box.close(); - - await box.deleteFromDisk(); - verify(() => backend.deleteFromDisk()); - }); - - test('closes and deletes box', () async { - var hive = MockHiveImpl(); - var keystore = MockKeystore(); - var backend = MockStorageBackend(); - var box = _openBoxBaseMock( - name: 'myBox', - hive: hive, - keystore: keystore, - backend: backend, - ); - when(() => keystore.clear()).thenReturn(0); - returnFutureVoid(when(() => keystore.close())); - returnFutureVoid(when(() => backend.close())); - returnFutureVoid(when(() => backend.clear())); - returnFutureVoid(when(() => backend.deleteFromDisk())); - - await box.deleteFromDisk(); - verifyInOrder([ - () => keystore.close(), - () => hive.unregisterBox('myBox'), - () => backend.deleteFromDisk(), - ]); - expect(box.isOpen, false); - }); - }); - }); -} diff --git a/hive/test/tests/box/box_impl_test.dart b/hive/test/tests/box/box_impl_test.dart deleted file mode 100644 index 7b4385604..000000000 --- a/hive/test/tests/box/box_impl_test.dart +++ /dev/null @@ -1,216 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/box_impl.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; -import '../common.dart'; -import '../mocks.dart'; - -BoxImpl _getBox({ - String? name, - HiveImpl? hive, - Keystore? keystore, - CompactionStrategy? cStrategy, - StorageBackend? backend, -}) { - var box = BoxImpl( - hive ?? HiveImpl(), - name ?? 'testBox', - null, - cStrategy ?? (total, deleted) => false, - backend ?? MockStorageBackend(), - ); - box.keystore = keystore ?? Keystore(box, ChangeNotifier(), null); - return box; -} - -void main() { - group('BoxImpl', () { - test('.values', () { - var keystore = Keystore.debug(frames: [ - Frame(0, 123), - Frame('key1', 'value1'), - Frame(1, null), - ]); - var box = _getBox(keystore: keystore); - - expect(box.values, [123, null, 'value1']); - }); - - test('.valuesBetween()', () { - var keystore = Keystore.debug(frames: [ - Frame(0, 0), - Frame(1, 1), - Frame('0', 2), - Frame('1', 3), - ]); - var box = _getBox(keystore: keystore); - - expect(box.valuesBetween(startKey: 1, endKey: '0'), [1, 2]); - }); - - group('.get()', () { - test('returns defaultValue if key does not exist', () { - var backend = MockStorageBackend(); - var box = _getBox(backend: backend); - - expect(box.get('someKey'), null); - expect(box.get('otherKey', defaultValue: -12), -12); - verifyZeroInteractions(backend); - }); - - test('returns cached value if it exists', () { - var backend = MockStorageBackend(); - var box = _getBox( - backend: backend, - keystore: Keystore.debug(frames: [ - Frame('testKey', 'testVal'), - Frame(123, 456), - ]), - ); - - expect(box.get('testKey'), 'testVal'); - expect(box.get(123), 456); - verifyZeroInteractions(backend); - }); - }); - - test('.getAt() returns value at given index', () { - var keystore = - Keystore.debug(frames: [Frame(0, 'zero'), Frame('a', 'A')]); - var box = _getBox(keystore: keystore); - - expect(box.getAt(0), 'zero'); - expect(box.getAt(1), 'A'); - }); - - group('.putAll()', () { - test('values', () async { - var frames = [Frame('key1', 'value1'), Frame('key2', 'value2')]; - var keystoreFrames = [Frame('keystoreFrames', 123)]; - - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - when(() => keystore.frames).thenReturn(keystoreFrames); - when(() => keystore.beginTransaction(any())).thenReturn(true); - returnFutureVoid(when(() => backend.writeFrames(frames))); - returnFutureVoid(when(() => backend.compact(keystoreFrames))); - when(() => backend.supportsCompaction).thenReturn(true); - when(() => keystore.length).thenReturn(-1); - when(() => keystore.deletedEntries).thenReturn(-1); - - var box = _getBox( - keystore: keystore, - backend: backend, - cStrategy: (a, b) => true, - ); - - await box.putAll({'key1': 'value1', 'key2': 'value2'}); - await box.flush(); - verifyInOrder([ - () => keystore.beginTransaction(frames), - () => backend.writeFrames(frames), - () => keystore.commitTransaction(), - () => backend.compact([Frame('keystoreFrames', 123)]), - ]); - }); - - test('does nothing if no frames are provided', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - when(() => keystore.beginTransaction([])).thenReturn(false); - - var box = _getBox(backend: backend, keystore: keystore); - - await box.putAll({}); - verify(() => keystore.beginTransaction([])); - verifyZeroInteractions(backend); - }); - - test('handles exceptions', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - - when(() => backend.writeFrames(any())).thenThrow('Some error'); - when(() => keystore.beginTransaction(any())).thenReturn(true); - - var box = _getBox(backend: backend, keystore: keystore); - - await expectLater( - () async => await box.putAll({'key1': 'value1', 'key2': 'value2'}), - throwsA(anything), - ); - var frames = [Frame('key1', 'value1'), Frame('key2', 'value2')]; - verifyInOrder([ - () => keystore.beginTransaction(frames), - () => backend.writeFrames(frames), - () => keystore.cancelTransaction(), - ]); - }); - }); - - group('.deleteAll()', () { - test('do nothing when deleting non existing keys', () async { - var frames = []; - - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - var box = _getBox(backend: backend, keystore: keystore); - when(() => keystore.frames).thenReturn(frames); - when(() => keystore.containsKey(any())).thenReturn(false); - returnFutureVoid(when(() => backend.compact(frames))); - when(() => keystore.beginTransaction(frames)).thenReturn(false); - - await box.deleteAll(['key1', 'key2', 'key3']); - verifyZeroInteractions(backend); - }); - - test('delete keys', () async { - var frames = [Frame.deleted('key1'), Frame.deleted('key2')]; - - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - when(() => backend.supportsCompaction).thenReturn(true); - when(() => keystore.beginTransaction(any())).thenReturn(true); - returnFutureVoid(when(() => backend.writeFrames(frames))); - when(() => keystore.containsKey(any())).thenReturn(true); - when(() => keystore.length).thenReturn(-1); - when(() => keystore.deletedEntries).thenReturn(-1); - when(() => keystore.frames).thenReturn(frames); - returnFutureVoid(when(() => backend.compact(frames))); - - var box = _getBox( - backend: backend, - keystore: keystore, - cStrategy: (a, b) => true, - ); - - await box.deleteAll(['key1', 'key2']); - await box.flush(); - verifyInOrder([ - () => keystore.containsKey('key1'), - () => keystore.containsKey('key2'), - () => keystore.beginTransaction(frames), - () => backend.writeFrames(frames), - () => keystore.commitTransaction(), - () => backend.compact(any()), - ]); - }); - }); - - test('.toMap()', () { - var box = _getBox( - keystore: Keystore.debug(frames: [ - Frame('key1', 1), - Frame('key2', 2), - Frame('key4', 444), - ]), - ); - expect(box.toMap(), {'key1': 1, 'key2': 2, 'key4': 444}); - }); - }); -} diff --git a/hive/test/tests/box/change_notifier_test.dart b/hive/test/tests/box/change_notifier_test.dart deleted file mode 100644 index 1a8686ecb..000000000 --- a/hive/test/tests/box/change_notifier_test.dart +++ /dev/null @@ -1,55 +0,0 @@ -import 'dart:async'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; - -class StreamControllerMock extends Mock implements StreamController {} - -void main() { - group('ChangeNotifier', () { - test('.watch()', () async { - var notifier = ChangeNotifier(); - - var allEvents = []; - notifier.watch().listen((e) { - allEvents.add(e); - }); - - var filteredEvents = []; - notifier.watch(key: 'key1').listen((e) { - filteredEvents.add(e); - }); - - notifier.notify(Frame.deleted('key1')); - notifier.notify(Frame('key1', 'newVal')); - notifier.notify(Frame('key2', 'newVal2')); - - await Future.delayed(Duration(milliseconds: 1)); - - expect(allEvents, [ - BoxEvent('key1', null, true), - BoxEvent('key1', 'newVal', false), - BoxEvent('key2', 'newVal2', false), - ]); - - expect(filteredEvents, [ - BoxEvent('key1', null, true), - BoxEvent('key1', 'newVal', false), - ]); - }); - - test('close', () async { - var controller = StreamControllerMock(); - returnFutureVoid(when(() => controller.close())); - var notifier = ChangeNotifier.debug(controller); - - await notifier.close(); - verify(() => controller.close()); - }); - }); -} diff --git a/hive/test/tests/box/keystore_test.dart b/hive/test/tests/box/keystore_test.dart deleted file mode 100644 index 0bfdb9d3b..000000000 --- a/hive/test/tests/box/keystore_test.dart +++ /dev/null @@ -1,620 +0,0 @@ -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../mocks.dart'; - -void main() { - void expectTrx(Iterable i1, Iterable i2) { - expect(i1.length, i2.length); - var l1 = i1.toList(); - var l2 = i2.toList(); - for (var i = 0; i < i1.length; i++) { - expect(l1[i].added, l2[i].added); - expect(l1[i].deleted, l2[i].deleted); - } - } - - group('Keystore', () { - test('.length returns the number of frames in the store', () { - var keystore = Keystore.debug(frames: [Frame('a', 1), Frame(1, 'a')]); - expect(keystore.length, 2); - expect(Keystore.debug().length, 0); - }); - - test('.autoIncrement() updates auto increment value', () { - var keystore = Keystore.debug(); - expect(keystore.autoIncrement(), 0); - expect(keystore.autoIncrement(), 1); - expect(keystore.autoIncrement(), 2); - - keystore.updateAutoIncrement(5); - expect(keystore.autoIncrement(), 6); - expect(keystore.autoIncrement(), 7); - - keystore.updateAutoIncrement(7); - expect(keystore.autoIncrement(), 8); - expect(keystore.autoIncrement(), 9); - }); - - group('.updateAutoIncrement()', () { - test('increases auto increment value if given key is bigger', () { - var keystore = Keystore.debug(); - expect(keystore.autoIncrement(), 0); - keystore.updateAutoIncrement(5); - expect(keystore.autoIncrement(), 6); - }); - - test('does nothing if given key is lower', () { - var keystore = Keystore.debug(); - - keystore.updateAutoIncrement(20); - expect(keystore.autoIncrement(), 21); - - keystore.updateAutoIncrement(20); - expect(keystore.autoIncrement(), 22); - }); - }); - - test('.containsKey() returns whether store has key', () { - var keystore = Keystore.debug(frames: [Frame('key1', null)]); - - expect(keystore.containsKey('key1'), true); - expect(keystore.containsKey('key2'), false); - }); - - group('.keyAt()', () { - test('returns the key at the given index', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', null), - Frame(2, null), - Frame(0, null), - Frame('0', null), - ]); - - expect(keystore.keyAt(0), 0); - expect(keystore.keyAt(1), 2); - expect(keystore.keyAt(2), '0'); - expect(keystore.keyAt(3), 'key1'); - }); - - test('throws RangeError if the index does not exist', () { - var keystore = Keystore.debug(frames: [Frame('key1', null)]); - - expect(() => keystore.keyAt(1), throwsRangeError); - expect(() => keystore.keyAt(999), throwsRangeError); - expect(() => Keystore.debug().keyAt(0), throwsRangeError); - }); - }); - - group('.get()', () { - test('returns the frame of the given key', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', 'value1'), - Frame(1, 'value2'), - ]); - - expect(keystore.get('key1'), Frame('key1', 'value1')); - expect(keystore.get(1), Frame(1, 'value2')); - }); - - test('returns null if there is no such key', () { - var keystore = Keystore.debug(frames: [Frame('key', 'value')]); - expect(keystore.get('key2'), null); - expect(Keystore.debug().get('someKey'), null); - }); - }); - - group('.getAt()', () { - test('returns the frame at the given index', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', 'value1'), - Frame(4, 'value2'), - ]); - - expect(keystore.getAt(0), Frame(4, 'value2')); - expect(keystore.getAt(1), Frame('key1', 'value1')); - }); - - test('throws RangeError index does not exist', () { - var keystore = Keystore.debug(frames: [Frame('key1', 'value1')]); - expect(() => keystore.getAt(1), throwsRangeError); - expect(() => Keystore.debug().getAt(0), throwsRangeError); - }); - }); - - test('.getKeys() returns the keys in the correct order', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', null), - Frame(2, null), - Frame(0, null), - Frame('0', null), - ]); - - expect(keystore.getKeys(), [0, 2, '0', 'key1']); - }); - - test('.getValues() returns the values in the order of their keys', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', 4), - Frame(2, 2), - Frame(0, null), - Frame('0', 3), - ]); - - expect(keystore.getValues(), [null, 2, 3, 4]); - }); - - group('.getValuesBetween()', () { - Keystore keystore() => Keystore.debug(frames: [ - Frame('key1', 4), - Frame(2, 2), - Frame(0, null), - Frame('0', 3), - ]); - - test('startKey and endKey specified', () { - expect(keystore().getValuesBetween(2, '0'), [2, 3]); - }); - - test('only startKey specified', () { - expect(keystore().getValuesBetween(2, null), [2, 3, 4]); - }); - - test('only endKey specified', () { - expect(keystore().getValuesBetween(null, '0'), [null, 2, 3]); - }); - - test('endKey before startKey', () { - expect(keystore().getValuesBetween(2, 0), [2, 3, 4]); - }); - }); - - group('.insert()', () { - group('add', () { - test('updates auto increment', () { - var keystore = Keystore.debug(); - expect(keystore.autoIncrement(), 0); - - keystore.insert(Frame(123, 'val')); - expect(keystore.autoIncrement(), 124); - - keystore.insert(Frame('500', 'val')); - expect(keystore.autoIncrement(), 125); - }); - - test('initializes HiveObject', () { - var box = MockBox(); - var keystore = Keystore.debug(box: box); - - var hiveObject = TestHiveObject(); - keystore.insert(Frame('key', hiveObject)); - - expect(hiveObject.key, 'key'); - expect(hiveObject.box, box); - }); - - test('adds frame to store', () { - var keystore = Keystore.debug(); - keystore.insert(Frame('key2', 'val2')); - keystore.insert(Frame('key1', 'val1')); - - expect( - keystore.frames, [Frame('key1', 'val1'), Frame('key2', 'val2')]); - }); - - test('returns overridden Frame', () { - var keystore = Keystore.debug(); - - var frame = Frame('key', 'val'); - expect(keystore.insert(frame), null); - expect(keystore.insert(Frame('key', 'val2')), frame); - }); - - test('unloads previous HiveObject', () { - var box = MockBox(); - var keystore = Keystore.debug(box: box); - - var hiveObject = TestHiveObject(); - keystore.insert(Frame('key', hiveObject)); - keystore.insert(Frame('key', TestHiveObject())); - - expect(hiveObject.key, null); - expect(hiveObject.box, null); - }); - - test('does not unload HiveObject if it is the same instance', () { - var box = MockBox(); - var keystore = Keystore.debug(box: box); - - var hiveObject = TestHiveObject(); - keystore.insert(Frame('key', hiveObject)); - keystore.insert(Frame('key', hiveObject)); - - expect(hiveObject.key, 'key'); - expect(hiveObject.box, box); - }); - - test('increases deletedEntries', () { - var keystore = Keystore.debug(); - expect(keystore.deletedEntries, 0); - - keystore.insert(Frame('key1', 'val1')); - expect(keystore.deletedEntries, 0); - - keystore.insert(Frame('key1', 'val2')); - expect(keystore.deletedEntries, 1); - }); - - test('broadcasts change event', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug(notifier: notifier); - - keystore.insert(Frame('key1', 'val1')); - verify(() => notifier.notify(Frame('key1', 'val1'))); - - keystore.insert(Frame('key1', 'val2')); - verify(() => notifier.notify(Frame('key1', 'val2'))); - }); - }); - - group('delete', () { - test('deletes frame from store', () { - var keystore = Keystore.debug(frames: [ - Frame('key2', 'val2'), - Frame('key1', 'val1'), - ]); - - keystore.insert(Frame.deleted('key2')); - expect(keystore.frames, [Frame('key1', 'val1')]); - }); - - test('returns deleted Frame', () { - var frame = Frame('key', 'val'); - var keystore = Keystore.debug(frames: [frame]); - - expect(keystore.insert(Frame.deleted('key')), frame); - expect(keystore.insert(Frame.deleted('key')), null); - }); - - test('unloads deleted HiveObject', () { - var box = MockBox(); - var hiveObject = TestHiveObject(); - var keystore = - Keystore.debug(frames: [Frame('key', hiveObject)], box: box); - - keystore.insert(Frame.deleted('key')); - expect(hiveObject.key, null); - expect(hiveObject.box, null); - }); - - test('increases deletedEntries', () { - var keystore = Keystore.debug(frames: [Frame('key1', 'val1')]); - expect(keystore.deletedEntries, 0); - - keystore.insert(Frame.deleted('key1')); - expect(keystore.deletedEntries, 1); - }); - - test('broadcasts change event', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key1', 'val1')], - notifier: notifier, - ); - - reset(notifier); - - keystore.insert(Frame.deleted('key1')); - verify(() => notifier.notify(Frame.deleted('key1'))); - - keystore.insert(Frame.deleted('key1')); - verifyNoMoreInteractions(notifier); - }); - }); - }); - - group('.beginTransaction()', () { - test('adding new frames', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug(notifier: notifier); - - var created = keystore.beginTransaction([ - Frame('key1', 'val1'), - Frame('key2', 'val2'), - ]); - - expect(created, true); - expect(keystore.transactions.first.added, ['key1', 'key2']); - expect(keystore.frames, [Frame('key1', 'val1'), Frame('key2', 'val2')]); - verify(() => notifier.notify(Frame('key1', 'val1'))); - verify(() => notifier.notify(Frame('key2', 'val2'))); - }); - - test('overriding existing keys', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key1', 'val1')], - notifier: notifier, - ); - reset(notifier); - - var created = keystore.beginTransaction([ - Frame('key1', 'val2'), - Frame('key2', 'val3'), - ]); - - expect(created, true); - expect(keystore.transactions.first.deleted, { - 'key1': Frame('key1', 'val1'), - }); - expect(keystore.frames, [Frame('key1', 'val2'), Frame('key2', 'val3')]); - verify(() => notifier.notify(Frame('key1', 'val2'))); - verify(() => notifier.notify(Frame('key2', 'val3'))); - }); - - test('empty transaction', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key1', 'val1')], - notifier: notifier, - ); - reset(notifier); - - var created = keystore.beginTransaction([]); - - expect(created, false); - expect(keystore.frames, [Frame('key1', 'val1')]); - verifyZeroInteractions(notifier); - }); - - test('deleting frames', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [ - Frame('key1', 'val1'), - Frame('key2', 'val2'), - ], - notifier: notifier, - ); - reset(notifier); - - var created = keystore.beginTransaction([ - Frame.deleted('key1'), - Frame.deleted('key3'), - ]); - - expect(created, true); - expect(keystore.transactions.first.deleted, { - 'key1': Frame('key1', 'val1'), - }); - expect(keystore.frames, [Frame('key2', 'val2')]); - verify(() => notifier.notify(Frame.deleted('key1'))); - }); - }); - - group('.commitTransaction()', () { - test('removes the oldest transaction', () { - var keystore = Keystore.debug(); - keystore.beginTransaction([Frame('key1', 'val1')]); - keystore.beginTransaction([Frame('key2', 'val2')]); - - expectTrx(keystore.transactions, [ - KeyTransaction()..added.add('key1'), - KeyTransaction()..added.add('key2'), - ]); - - keystore.commitTransaction(); - expectTrx(keystore.transactions, [KeyTransaction()..added.add('key2')]); - }); - - test('fails if there are no pending transactions', () { - var keystore = Keystore.debug(); - expect(() => keystore.commitTransaction(), throwsStateError); - }); - }); - - group('.cancelTransaction()', () { - test('add', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug(notifier: notifier); - keystore.beginTransaction([Frame('key', 'val1')]); - keystore.beginTransaction([Frame('otherKey', 'otherVal')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [Frame('otherKey', 'otherVal')]); - expectTrx( - keystore.transactions, - [KeyTransaction()..added.add('otherKey')], - ); - verify(() => notifier.notify(Frame.deleted('key'))); - }); - - test('add then override', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug(notifier: notifier); - keystore.beginTransaction([Frame('key', 'val1')]); - keystore.beginTransaction([Frame('key', 'val2')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [Frame('key', 'val2')]); - expectTrx(keystore.transactions, [KeyTransaction()..added.add('key')]); - verifyZeroInteractions(notifier); - }); - - test('add then delete', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug(notifier: notifier); - keystore.beginTransaction([Frame('key', 'val1')]); - keystore.beginTransaction([ - Frame('otherKey', 'otherVal'), - Frame.deleted('key'), - ]); - keystore.beginTransaction([ - Frame('key', 'val2'), - ]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [ - Frame('key', 'val2'), - Frame('otherKey', 'otherVal'), - ]); - expectTrx(keystore.transactions, [ - KeyTransaction()..added.add('otherKey'), - KeyTransaction()..added.add('key'), - ]); - verifyZeroInteractions(notifier); - }); - - test('override', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key', 'val1')], - notifier: notifier, - ); - keystore.beginTransaction([Frame('key', 'val2')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [Frame('key', 'val1')]); - expectTrx(keystore.transactions, []); - verify(() => notifier.notify(Frame('key', 'val1'))); - }); - - test('override then add', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key', 'val1')], - notifier: notifier, - ); - keystore.beginTransaction([Frame('key', 'val2')]); - keystore.beginTransaction([Frame('key', 'val3')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [Frame('key', 'val3')]); - expectTrx(keystore.transactions, [ - KeyTransaction() - ..added.add('key') - ..deleted['key'] = Frame('key', 'val1'), - ]); - verifyZeroInteractions(notifier); - }); - - test('override then delete', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key', 'val1')], - notifier: notifier, - ); - keystore.beginTransaction([Frame('key', 'val2')]); - keystore.beginTransaction([Frame.deleted('key')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, []); - expectTrx(keystore.transactions, [ - KeyTransaction()..deleted['key'] = Frame('key', 'val1'), - ]); - verifyZeroInteractions(notifier); - }); - - test('delete', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key', 'val1')], - notifier: notifier, - ); - keystore.beginTransaction([Frame.deleted('key')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [Frame('key', 'val1')]); - expectTrx(keystore.transactions, []); - verify(() => notifier.notify(Frame('key', 'val1'))); - }); - - test('delete then add', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug(frames: [Frame('key', 'val1')]); - keystore.beginTransaction([Frame.deleted('key')]); - keystore.beginTransaction([Frame('key', 'val2')]); - reset(notifier); - - keystore.cancelTransaction(); - expect(keystore.frames, [Frame('key', 'val2')]); - expectTrx(keystore.transactions, [ - KeyTransaction() - ..added.add('key') - ..deleted['key'] = Frame('key', 'val1'), - ]); - verifyZeroInteractions(notifier); - }); - }); - - group('.clear()', () { - test('clears store', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', 'val1'), - Frame('key2', 'val2'), - ]); - keystore.clear(); - expect(keystore.frames, []); - }); - - test('unloads HiveObjects', () { - var hiveObject = TestHiveObject(); - var box = MockBox(); - var keystore = Keystore.debug(frames: [ - Frame('key1', 'val1'), - Frame('key2', hiveObject), - ], box: box); - expect(hiveObject.key, 'key2'); - expect(hiveObject.box, box); - - keystore.clear(); - expect(hiveObject.key, null); - expect(hiveObject.box, null); - }); - - test('resets deleted entries', () { - var keystore = Keystore.debug(frames: [ - Frame('key1', 'val1'), - Frame('key2', 'val2'), - ]); - - keystore.insert(Frame.deleted('key1')); - expect(keystore.deletedEntries, 1); - - keystore.clear(); - expect(keystore.deletedEntries, 0); - }); - - test('resets auto increment counter', () { - var keystore = Keystore.debug(); - expect(keystore.autoIncrement(), 0); - expect(keystore.autoIncrement(), 1); - - keystore.clear(); - expect(keystore.autoIncrement(), 0); - }); - - test('broadcasts change event', () { - var notifier = MockChangeNotifier(); - var keystore = Keystore.debug( - frames: [Frame('key1', 'val1'), Frame('key2', 'val2')], - notifier: notifier, - ); - reset(notifier); - - keystore.clear(); - verify(() => notifier.notify(Frame.deleted('key1'))); - verify(() => notifier.notify(Frame.deleted('key2'))); - }); - }); - }); -} diff --git a/hive/test/tests/box/lazy_box_impl_test.dart b/hive/test/tests/box/lazy_box_impl_test.dart deleted file mode 100644 index cdb8f2241..000000000 --- a/hive/test/tests/box/lazy_box_impl_test.dart +++ /dev/null @@ -1,168 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/box/lazy_box_impl.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; -import '../mocks.dart'; - -LazyBoxImpl _getBox({ - String? name, - HiveImpl? hive, - Keystore? keystore, - CompactionStrategy? cStrategy, - StorageBackend? backend, -}) { - var box = LazyBoxImpl( - hive ?? HiveImpl(), - name ?? 'testBox', - null, - cStrategy ?? (total, deleted) => false, - backend ?? MockStorageBackend(), - ); - box.keystore = keystore ?? Keystore(box, ChangeNotifier(), null); - return box; -} - -void main() { - setUpAll(() { - registerFallbackValue(Frame.deleted(0)); - }); - - group('LazyBoxImpl', () { - group('.get()', () { - test('returns defaultValue if key does not exist', () async { - var backend = MockStorageBackend(); - var box = _getBox(backend: backend); - - expect(await box.get('someKey'), null); - expect(await box.get('otherKey', defaultValue: -12), -12); - verifyZeroInteractions(backend); - }); - - test('reads value from backend', () async { - var backend = MockStorageBackend(); - when(() => backend.readValue(any())).thenAnswer((i) async => 'testVal'); - - var box = _getBox(backend: backend); - var frame = Frame.lazy('testKey', length: 123, offset: 456); - box.keystore.insert(frame); - - expect(await box.get('testKey'), 'testVal'); - verify(() => backend.readValue(frame)); - }); - }); - - test('.getAt()', () async { - var keystore = Keystore.debug(frames: [ - Frame.lazy(0), - Frame.lazy('a'), - ]); - var backend = MockStorageBackend(); - when(() => backend.readValue(any())).thenAnswer((i) { - return Future.value('A'); - }); - var box = _getBox(keystore: keystore, backend: backend); - - expect(await box.getAt(1), 'A'); - }); - - group('.putAll()', () { - test('values', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - when(() => keystore.containsKey(any())).thenReturn(false); - returnFutureVoid(when(() => backend.writeFrames(any()))); - when(() => keystore.length).thenReturn(-1); - when(() => keystore.deletedEntries).thenReturn(-1); - - var box = _getBox( - backend: backend, - keystore: keystore, - ); - - await box.putAll({'key1': 'value1', 'key2': 'value2'}); - await box.flush(); - verifyInOrder([ - () => backend.writeFrames([ - Frame('key1', 'value1'), - Frame('key2', 'value2'), - ]), - () => keystore.insert(Frame('key1', 'value1'), lazy: true), - () => keystore.insert(Frame('key2', 'value2'), lazy: true), - ]); - }); - - test('handles exceptions', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - final theError = 'Some error'; - - when(() => backend.writeFrames(any())).thenThrow(theError); - when(() => keystore.containsKey(any())).thenReturn(true); - - var box = _getBox( - backend: backend, - keystore: keystore, - ); - - await expectLater( - () async => await box.putAll( - {'key1': 'value1', 'key2': 'value2'}, - ), - throwsA(theError), - ); - verify(() => backend.writeFrames([ - Frame('key1', 'value1'), - Frame('key2', 'value2'), - ])); - verifyNoMoreInteractions(keystore); - }); - }); - - group('.deleteAll()', () { - test('does nothing when deleting non existing keys', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - when(() => keystore.containsKey(any())).thenReturn(false); - var box = _getBox( - backend: backend, - keystore: keystore, - ); - - await box.deleteAll(['key1', 'key2', 'key3']); - verifyZeroInteractions(backend); - }); - - test('delete keys', () async { - var backend = MockStorageBackend(); - var keystore = MockKeystore(); - when(() => keystore.containsKey(any())).thenReturn(true); - returnFutureVoid(when(() => backend.writeFrames(any()))); - when(() => keystore.length).thenReturn(-1); - when(() => keystore.deletedEntries).thenReturn(-1); - - var box = _getBox( - backend: backend, - keystore: keystore, - ); - - await box.deleteAll(['key1', 'key2']); - await box.flush(); - verifyInOrder([ - () => keystore.containsKey('key1'), - () => keystore.containsKey('key2'), - () => backend - .writeFrames([Frame.deleted('key1'), Frame.deleted('key2')]), - () => keystore.insert(Frame.deleted('key1')), - () => keystore.insert(Frame.deleted('key2')), - ]); - }); - }); - }); -} diff --git a/hive/test/tests/common.dart b/hive/test/tests/common.dart deleted file mode 100644 index b415b3065..000000000 --- a/hive/test/tests/common.dart +++ /dev/null @@ -1,126 +0,0 @@ -import 'dart:io'; -import 'dart:math'; - -import 'package:hive/hive.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:path/path.dart' as path; -import 'package:test/test.dart'; - -Matcher isAHiveError([String? contains]) { - return allOf( - isA(), - predicate((HiveError e) => - contains == null || - e.toString().toLowerCase().contains(contains.toLowerCase()))); -} - -Matcher throwsHiveError([String? contains]) { - return throwsA(isAHiveError(contains)); -} - -final random = Random(); -String tempPath = - path.join(Directory.current.path, '.dart_tool', 'test', 'tmp'); -String assetsPath = path.join(Directory.current.path, 'test', 'assets'); - -Future getTempFile([List? bytes]) async { - var name = random.nextInt(pow(2, 32) as int); - var file = File(path.join(tempPath, '$name.tmp')); - await file.create(recursive: true); - - if (bytes != null) { - await file.writeAsBytes(bytes); - } - - return file; -} - -Future getTempRaf(List bytes, - {FileMode mode = FileMode.read}) async { - var file = await getTempFile(bytes); - return await file.open(mode: mode); -} - -Future getTempDir() async { - var name = random.nextInt(pow(2, 32) as int); - var dir = Directory(path.join(tempPath, '${name}_tmp')); - if (await dir.exists()) { - await dir.delete(recursive: true); - } - await dir.create(recursive: true); - return dir; -} - -File getAssetFile(String part1, [String? part2, String? part3, String? part4]) { - return File(path.join(assetsPath, part1, part2, part3, part4)); -} - -Future getTempAssetFile(String part1, - [String? part2, String? part3, String? part4]) async { - var assetFile = getAssetFile(part1, part2, part3, part4); - var tempFile = await getTempFile(); - - return await assetFile.copy(tempFile.path); -} - -Future getAssetDir(String part1, - [String? part2, String? part3, String? part4]) async { - var assetDir = Directory(path.join(assetsPath, part1, part2, part3, part4)); - var tempDir = await getTempDir(); - - await for (var file in assetDir.list(recursive: true)) { - if (file is File) { - var relative = path.relative(file.path, from: assetDir.path); - var tempFile = File(path.join(tempDir.path, relative)); - await tempFile.create(recursive: true); - await file.copy(tempFile.path); - } - } - - return tempDir; -} - -Future expectDirsEqual(Directory dir1, Directory dir2) { - return _expectDirsEqual(dir1, dir2, false); -} - -Future _expectDirsEqual( - Directory dir1, Directory dir2, bool round2) async { - await for (var entity in dir1.list(recursive: true)) { - if (entity is File) { - var fileName = path.basename(entity.path); - var otherFile = File(path.join(dir2.path, fileName)); - - var entityBytes = await entity.readAsBytes(); - var otherBytes = await otherFile.readAsBytes(); - expect(entityBytes, otherBytes); - } else if (entity is Directory) { - var dir2Entity = - Directory(path.join(dir2.path, path.basename(entity.path))); - await expectDirsEqual(entity, dir2Entity); - } - } - - if (!round2) { - await _expectDirsEqual(dir2, dir1, true); - } -} - -Future expectDirEqualsAssetDir(Directory dir1, String part1, - [String? part2, String? part3, String? part4]) { - var assetDir = Directory(path.join(assetsPath, part1, part2, part3, part4)); - return expectDirsEqual(dir1, assetDir); -} - -void returnFutureVoid(When> v) => - v.thenAnswer((i) => Future.value(null)); - -final bool soundNullSafety = (() { - try { - // ignore: cast_from_null_always_fails - null as Object; - return false; - } on TypeError { - return true; - } -})(); diff --git a/hive/test/tests/crypto/aes_cbc_pkcs7_test.dart b/hive/test/tests/crypto/aes_cbc_pkcs7_test.dart deleted file mode 100644 index 976a396c1..000000000 --- a/hive/test/tests/crypto/aes_cbc_pkcs7_test.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/crypto/aes_cbc_pkcs7.dart'; -import 'package:hive/src/util/extensions.dart'; -import 'package:pointycastle/export.dart'; -import 'package:test/test.dart'; - -import 'message.dart'; - -PaddedBlockCipherImpl getCipher() { - var pcCipher = PaddedBlockCipherImpl( - PKCS7Padding(), - CBCBlockCipher(AESEngine()), - ); - pcCipher.init( - true, - PaddedBlockCipherParameters( - ParametersWithIV(KeyParameter(key), iv), - null, - ), - ); - return pcCipher; -} - -void main() { - group('AesCbcPkcs7', () { - test('.encrypt()', () { - var out = Uint8List(1100); - var cipher = AesCbcPkcs7(key); - for (var i = 1; i < 1000; i++) { - var input = message.view(0, i); - var outLen = cipher.encrypt(iv, input, 0, i, out, 0); - var pcOut = getCipher().process(input); - - expect(out.view(0, outLen), pcOut); - } - }); - - test('.decrypt()', () { - var out = Uint8List(1100); - var cipher = AesCbcPkcs7(key); - for (var i = 1; i < 1000; i++) { - var input = message.view(0, i); - var encrypted = getCipher().process(input); - var outLen = cipher.decrypt(iv, encrypted, 0, encrypted.length, out, 0); - expect(out.view(0, outLen), input); - } - }); - }); -} diff --git a/hive/test/tests/crypto/aes_engine_test.dart b/hive/test/tests/crypto/aes_engine_test.dart deleted file mode 100644 index c5564cbf4..000000000 --- a/hive/test/tests/crypto/aes_engine_test.dart +++ /dev/null @@ -1,49 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/crypto/aes_engine.dart'; -import 'package:pointycastle/api.dart'; -import 'package:pointycastle/block/aes.dart'; -import 'package:test/test.dart'; - -import 'message.dart'; - -void main() { - group('AesEngine', () { - test('.generateWorkingKey()', () { - expect(AesEngine.generateWorkingKey(key, true), encryptionKey); - expect(AesEngine.generateWorkingKey(key, false), decryptionKey); - }); - - test('.encryptBlock()', () { - var out = Uint8List(message.length); - - var pcEngine = AESEngine(); - var outPc = Uint8List(message.length); - - for (var i = 0; i < message.length; i += aesBlockSize) { - AesEngine.encryptBlock(encryptionKey, message, i, out, i); - pcEngine.init(true, KeyParameter(key)); - pcEngine.processBlock(message, i, outPc, i); - } - expect(out, outPc); - }); - - test('.decryptBlock()', () { - var out = Uint8List(message.length); - - var pcEngine = AESEngine(); - var encrypted = Uint8List(message.length); - - for (var i = 0; i < message.length; i += aesBlockSize) { - AesEngine.decryptBlock(encryptionKey, message, i, out, i); - pcEngine.init(true, KeyParameter(key)); - pcEngine.processBlock(message, i, encrypted, i); - } - - for (var i = 0; i < encrypted.length; i += aesBlockSize) { - AesEngine.decryptBlock(decryptionKey, encrypted, i, out, i); - } - expect(out, message); - }); - }); -} diff --git a/hive/test/tests/crypto/crc32_test.dart b/hive/test/tests/crypto/crc32_test.dart deleted file mode 100644 index f0e7c18a4..000000000 --- a/hive/test/tests/crypto/crc32_test.dart +++ /dev/null @@ -1,22 +0,0 @@ -import 'dart:typed_data'; - -import 'package:hive/src/crypto/crc32.dart'; -import 'package:test/test.dart'; - -void main() { - group('Crc32', () { - test('compute', () { - expect(Crc32.compute(Uint8List(0)), equals(0)); - expect( - Crc32.compute(Uint8List.fromList('123456789'.codeUnits)), - 0xcbf43926, - ); - - var crc = Crc32.compute(Uint8List.fromList('12345'.codeUnits)); - expect( - Crc32.compute(Uint8List.fromList('6789'.codeUnits), crc: crc), - equals(0xcbf43926), - ); - }); - }); -} diff --git a/hive/test/tests/crypto/message.dart b/hive/test/tests/crypto/message.dart deleted file mode 100644 index 2aee464f6..000000000 --- a/hive/test/tests/crypto/message.dart +++ /dev/null @@ -1,45 +0,0 @@ -import 'dart:math'; -import 'dart:typed_data'; - -import 'package:hive/src/util/extensions.dart'; - -final key = Uint8List.fromList(List.generate(32, (i) => i)); - -const encryptionKey = [ - [50462976, 117835012, 185207048, 252579084], - [319951120, 387323156, 454695192, 522067228], - [2680320933, 2563012257, 2479783849, 2629857957], - [3450360086, 3669902338, 3248774426, 3736748038], - [4041181102, 1746661647, 4225076902, 1729494019], - [1223811437, 2454693231, 1407973493, 2370943091], - [2139248326, 395945929, 3964414319, 2337920364], - [1966793277, 3883222866, 3030302503, 969869140], - [1603329035, 1208581058, 2756006573, 790398913], - [1621554501, 2278797847, 860687664, 176294500], - [486002556, 1425978558, 4038845971, 3752288722], - [4277803760, 2032773351, 1248110039, 1088860083], - [1912488229, 620819867, 3585807240, 169636442], - [2573621838, 3763335849, 2854967166, 3939367117], - [3430546468, 3917023679, 1019353655, 920545389] -]; -const decryptionKey = [ - [50462976, 117835012, 185207048, 252579084], - [488120090, 421272350, 353376018, 286528278], - [3376425002, 3226215204, 3310101542, 3293062944], - [260429695, 379178081, 59366259, 312330341], - [3177760277, 2099569969, 3093872919, 2082530871], - [374920622, 12655055, 55100092, 299174617], - [2587912670, 3882126575, 1594641912, 590130127], - [4089733112, 4077209143, 4031612043, 3784871506], - [362260028, 4076022483, 2919196459, 2396245220], - [2742803060, 1350474819, 2687703240, 1101764250], - [328102069, 3783088742, 1283603789, 3260150185], - [2147853256, 3497738123, 1883776835, 837005785], - [3947370078, 171265080, 1186427253, 2229698780], - [4291948852, 799723199, 1608706556, 1845581861], - [3430546468, 3917023679, 1019353655, 920545389] -]; - -final iv = Uint8List.fromList(List.generate(16, (i) => i * 7)); - -final message = Random(123).nextBytes(1024); diff --git a/hive/test/tests/frames.dart b/hive/test/tests/frames.dart deleted file mode 100644 index 719d48cda..000000000 --- a/hive/test/tests/frames.dart +++ /dev/null @@ -1,198 +0,0 @@ -import 'dart:async'; -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/adapters/date_time_adapter.dart'; -import 'package:hive/src/binary/binary_writer_impl.dart'; -import 'package:hive/src/binary/frame.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:test/test.dart'; - -export '../generated/frame_values.g.dart'; -export '../generated/frame_values_encrypted.g.dart'; -export '../generated/frames.g.dart'; -export '../generated/frames_encrypted.g.dart'; - -TypeRegistry get testRegistry => HiveImpl(); - -class _HiveAesCipherStaticIV extends HiveAesCipher { - _HiveAesCipherStaticIV() : super(Uint8List.fromList(List.filled(32, 1))); - - @override - Uint8List generateIv() => Uint8List.fromList(List.filled(16, 4)); -} - -HiveCipher get testCipher => _HiveAesCipherStaticIV(); - -List get testFrames => [ - Frame.deleted(0), - Frame.deleted(555), - Frame(123, null), - Frame(0, 'Int key1'), - Frame(1, 'Int key2'), - Frame(2 ^ 32 - 1, 'Int key3'), - Frame.deleted('Tombstone frame'), - Frame('Null frame', null), - Frame('Int', 123123123), - Frame('Large int', 2 ^ 32), - Frame('Bool true', true), - Frame('Bool false', false), - Frame('Float', 12312.991283), - Frame('Unicode string', - 'A few characters which are not ASCII: 🇵🇬 😀 🐝 걟 $ 乽 👨‍🚀'), - Frame('Empty list', []), - Frame('Byte list', Uint8List.fromList([1, 12, 123, 1234])), - Frame('Byte list with mask', Uint8List.fromList([0x90, 0xA9, 1, 2, 3])), - Frame('Int list', [123, 456, 129318238]), - Frame('Bool list', [true, false, false, true]), - Frame('Double list', [ - 10.1723812, - double.infinity, - double.maxFinite, - double.minPositive, - double.negativeInfinity - ]), - Frame('String list', [ - 'hello', - '🧙‍♂️ 👨‍👨‍👧‍👦 ', - ' ﻬ ﻭ ﻮ ﻯ ﻰ ﻱ', - 'അ ആ ഇ ', - ' צּ קּ רּ שּ ', - 'ゥ ェ ォ ャ ュ ョ ッ ー ' - ]), - Frame('List with null', ['This', 'is', 'a', 'test', null]), - Frame('List with different types', [ - 'List', - [1, 2, 3], - 5.8, - true, - 12341234, - {'t': true, 'f': false}, - ]), - Frame('Map', { - 'Bool': true, - 'Int': 1234, - 'Double': 15.7, - 'String': 'Hello', - 'List': [1, 2, null], - 'Null': null, - 'Map': {'Key': 'Val', 'Key2': 2} - }), - Frame('DateTime test', [ - DateTimeWithoutTZ.fromMillisecondsSinceEpoch(0), - DateTimeWithoutTZ.fromMillisecondsSinceEpoch(1566656623020), - ]), - Frame('BigInt Test', - BigInt.parse('1234567890123456789012345678901234567890')) - ]; - -List framesSetLengthOffset(List frames, List bytes) { - var offset = 0; - for (var i = 0; i < frames.length; i++) { - var length = bytes[i].length; - frames[i] - ..offset = offset - ..length = length; - offset += length; - } - return frames; -} - -List lazyFrames(List frames) { - return frames.map((f) { - if (f.deleted) { - return f; - } else { - return Frame.lazy(f.key, offset: f.offset, length: f.length); - } - }).toList(); -} - -List get valueTestFrames => - testFrames.where((it) => !it.deleted).toList(); - -Frame frameWithLength(Frame frame, int length) { - if (frame.deleted) { - return Frame.deleted(frame.key, length: length); - } else { - return Frame(frame.key, frame.value, length: length); - } -} - -Frame frameBodyWithLength(Frame frame, int length) { - if (frame.deleted) { - return Frame.deleted(null, length: length); - } else { - return Frame(null, frame.value, length: length); - } -} - -Frame lazyFrameWithLength(Frame frame, int length) { - if (frame.deleted) { - return Frame.deleted(frame.key, length: length); - } else { - return Frame.lazy(frame.key, length: length); - } -} - -void expectFrame(Frame f1, Frame f2) { - expect(f1.key, f2.key); - if (f1.value is double && f2.value is double) { - if (f1.value.isNaN as bool && f1.value.isNaN as bool) return; - } - expect(f1.value, f2.value); - expect(f1.length, f2.length); - expect(f1.deleted, f2.deleted); - expect(f1.lazy, f2.lazy); -} - -void expectFrames(Iterable f1, Iterable f2) { - var frames1 = f1.toList(); - var frames2 = f2.toList(); - - expect(frames1.length, f2.length); - for (var i = 0; i < frames2.length; i++) { - expectFrame(frames1[i], frames2[i]); - } -} - -void buildGoldens() async { - Future generate(String fileName, String varName, - FutureOr Function(Frame frame) transformer) async { - var file = File('test/generated/$fileName.g.dart'); - await file.create(); - var code = StringBuffer(); - code.writeln("import 'dart:typed_data';\n"); - code.writeln('final $varName = ['); - for (var frame in testFrames) { - code.writeln('// ${frame.key}'); - var bytes = await transformer(frame); - code.writeln('Uint8List.fromList(${bytes.toString()}),'); - } - code.writeln('];'); - file.writeAsStringSync(code.toString(), flush: true); - } - - await generate('frames', 'frameBytes', (f) { - var writer = BinaryWriterImpl(testRegistry); - writer.writeFrame(f); - return writer.toBytes(); - }); - await generate('frame_values', 'frameValuesBytes', (f) { - var writer = BinaryWriterImpl(HiveImpl()) - ..write(f.value, writeTypeId: false); - return writer.toBytes(); - }); - await generate('frames_encrypted', 'frameBytesEncrypted', (f) { - var writer = BinaryWriterImpl(testRegistry); - writer.writeFrame(f, cipher: testCipher); - return writer.toBytes(); - }); - await generate('frame_values_encrypted', 'frameValuesBytesEncrypted', - (f) async { - var writer = BinaryWriterImpl(HiveImpl()); - await writer.writeEncrypted(f.value, testCipher, writeTypeId: false); - return writer.toBytes(); - }); -} diff --git a/hive/test/tests/hive_impl_test.dart b/hive/test/tests/hive_impl_test.dart deleted file mode 100644 index a9a113962..000000000 --- a/hive/test/tests/hive_impl_test.dart +++ /dev/null @@ -1,352 +0,0 @@ -@TestOn('vm') - -import 'dart:io'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/adapters/date_time_adapter.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:test/test.dart'; - -import 'common.dart'; - -class _TestAdapter extends TypeAdapter { - _TestAdapter([this.typeId = 0]); - - @override - final int typeId; - - @override - int read(_) => 5; - - @override - void write(_, __) {} -} - -void main() { - group('HiveImpl', () { - Future initHive() async { - var tempDir = await getTempDir(); - var hive = HiveImpl(); - hive.init(tempDir.path); - return hive; - } - - test('.init()', () { - var hive = HiveImpl(); - - expect(() => hive.init('MYPATH'), returnsNormally); - expect(hive.homePath, 'MYPATH'); - - expect( - hive.findAdapterForValue(DateTime.now())!.adapter, - isA(), - ); - expect(hive.findAdapterForTypeId(16)!.adapter, isA()); - }); - - group('.openBox()', () { - group('box already open', () { - test('opened box is returned if it exists', () async { - var hive = await initHive(); - - var testBox = await hive.openBox('TESTBOX'); - var testBox2 = await hive.openBox('testBox'); - expect(testBox == testBox2, true); - - await hive.close(); - }); - - test('throw HiveError if opened box is lazy', () async { - var hive = await initHive(); - - await hive.openLazyBox('LAZYBOX'); - await expectLater( - () => hive.openBox('lazyBox'), - throwsHiveError('is already open and of type LazyBox'), - ); - - await hive.close(); - }); - - test('throw HiveError if already opening box is lazy', () async { - var hive = await initHive(); - - await Future.wait([ - hive.openLazyBox('TESTBOX'), - expectLater(hive.openBox('testbox'), - throwsHiveError('is already open and of type LazyBox')) - ]); - }); - - test('same box returned if it is already opening', () async { - var hive = await initHive(); - - Box? box1; - Box? box2; - await Future.wait([ - hive.openBox('TESTBOX').then((value) => box1 = value), - hive.openBox('testbox').then((value) => box2 = value) - ]); - - expect(box1 == box2, true); - }); - }); - }); - - group('.openLazyBox()', () { - group('box already open', () { - test('opened box is returned if it exists', () async { - var hive = await initHive(); - - var testBox = await hive.openLazyBox('TESTBOX'); - var testBox2 = await hive.openLazyBox('testBox'); - expect(testBox == testBox2, true); - - await hive.close(); - }); - - test('same box returned if it is already opening', () async { - LazyBox? box1; - LazyBox? box2; - - var hive = await initHive(); - await Future.wait([ - hive.openLazyBox('LAZYBOX').then((value) => box1 = value), - hive.openLazyBox('lazyBox').then((value) => box2 = value) - ]); - - expect(box1 == box2, true); - }); - - test('throw HiveError if opened box is not lazy', () async { - var hive = await initHive(); - - await hive.openBox('LAZYBOX'); - await expectLater( - () => hive.openLazyBox('lazyBox'), - throwsHiveError('is already open and of type Box'), - ); - - await hive.close(); - }); - - test('throw HiveError if already opening box is not lazy', () async { - var hive = await initHive(); - - await Future.wait([ - hive.openBox('LAZYBOX'), - expectLater(hive.openLazyBox('lazyBox'), - throwsHiveError('is already open and of type Box')) - ]); - }); - }); - }); - - group('.box()', () { - test('returns already opened box', () async { - var hive = await initHive(); - - var box = await hive.openBox('TESTBOX'); - expect(hive.box('testBox'), box); - expect(() => hive.box('other'), throwsHiveError('not found')); - - await hive.close(); - }); - - test('throws HiveError if box type does not match', () async { - var hive = await initHive(); - - await hive.openBox('INTBOX'); - expect( - () => hive.box('intBox'), - throwsHiveError('is already open and of type Box'), - ); - - await hive.openBox('DYNAMICBOX'); - expect( - () => hive.box('dynamicBox'), - throwsHiveError('is already open and of type Box'), - ); - - await hive.openLazyBox('LAZYBOX'); - expect( - () => hive.box('lazyBox'), - throwsHiveError('is already open and of type LazyBox'), - ); - - await hive.close(); - }); - }); - - group('.lazyBox()', () { - test('returns already opened box', () async { - var hive = await initHive(); - - var box = await hive.openLazyBox('TESTBOX'); - expect(hive.lazyBox('testBox'), box); - expect(() => hive.lazyBox('other'), throwsHiveError('not found')); - - await hive.close(); - }); - - test('throws HiveError if box type does not match', () async { - var hive = await initHive(); - - await hive.openLazyBox('INTBOX'); - expect( - () => hive.lazyBox('intBox'), - throwsHiveError('is already open and of type LazyBox'), - ); - - await hive.openLazyBox('DYNAMICBOX'); - expect( - () => hive.lazyBox('dynamicBox'), - throwsHiveError('is already open and of type LazyBox'), - ); - - await hive.openBox('BOX'); - expect( - () => hive.lazyBox('box'), - throwsHiveError('is already open and of type Box'), - ); - - await hive.close(); - }); - }); - - test('isBoxOpen()', () async { - var hive = await initHive(); - - await hive.openBox('testBox'); - - expect(hive.isBoxOpen('testBox'), true); - expect(hive.isBoxOpen('nonExistingBox'), false); - - await hive.close(); - }); - - test('.close()', () async { - var hive = await initHive(); - - var box1 = await hive.openBox('box1'); - var box2 = await hive.openBox('box2'); - expect(box1.isOpen, true); - expect(box2.isOpen, true); - - await hive.close(); - expect(box1.isOpen, false); - expect(box2.isOpen, false); - }); - - test('.generateSecureKey()', () { - var hive = HiveImpl(); - - var key1 = hive.generateSecureKey(); - var key2 = hive.generateSecureKey(); - - expect(key1.length, 32); - expect(key2.length, 32); - expect(key1, isNot(key2)); - }); - - group('.deleteBoxFromDisk()', () { - test('deletes open box', () async { - var hive = await initHive(); - - var box1 = await hive.openBox('testBox1'); - await box1.put('key', 'value'); - var box1File = File(box1.path!); - - await hive.deleteBoxFromDisk('testBox1'); - expect(await box1File.exists(), false); - expect(hive.isBoxOpen('testBox1'), false); - - await hive.close(); - }); - - test('deletes closed box', () async { - var hive = await initHive(); - - var box1 = await hive.openBox('testBox1'); - await box1.put('key', 'value'); - var path = box1.path!; - await box1.close(); - var box1File = File(path); - - await hive.deleteBoxFromDisk('testBox1'); - expect(await box1File.exists(), false); - expect(hive.isBoxOpen('testBox1'), false); - - await hive.close(); - }); - - test('does nothing if files do not exist', () async { - var hive = await initHive(); - await hive.deleteBoxFromDisk('testBox1'); - await hive.close(); - }); - }); - - test('.deleteFromDisk()', () async { - var hive = await initHive(); - - var box1 = await hive.openBox('testBox1'); - await box1.put('key', 'value'); - var box1File = File(box1.path!); - - var box2 = await hive.openBox('testBox2'); - await box2.put('key', 'value'); - var box2File = File(box1.path!); - - await hive.deleteFromDisk(); - expect(await box1File.exists(), false); - expect(await box2File.exists(), false); - expect(hive.isBoxOpen('testBox1'), false); - expect(hive.isBoxOpen('testBox2'), false); - - await hive.close(); - }); - - group('.boxExists()', () { - test('returns true if a box was created', () async { - var hive = await initHive(); - await hive.openBox('testBox1'); - expect(await hive.boxExists('testBox1'), true); - await hive.close(); - }); - - test('returns false if no box was created', () async { - var hive = await initHive(); - expect(await hive.boxExists('testBox1'), false); - await hive.close(); - }); - - test('returns false if box was created and then deleted', () async { - var hive = await initHive(); - await hive.openBox('testBox1'); - await hive.deleteBoxFromDisk('testBox1'); - expect(await hive.boxExists('testBox1'), false); - await hive.close(); - }); - }); - - group('.resetAdapters()', () { - test('returns normally', () async { - final hive = await initHive(); - expect(hive.resetAdapters, returnsNormally); - }); - - test('clears an adapter', () async { - final hive = await initHive(); - final adapter = _TestAdapter(1); - - expect(hive.isAdapterRegistered(adapter.typeId), isFalse); - hive.registerAdapter(adapter); - expect(hive.isAdapterRegistered(adapter.typeId), isTrue); - - hive.resetAdapters(); - expect(hive.isAdapterRegistered(adapter.typeId), isFalse); - }); - }); - }); -} diff --git a/hive/test/tests/io/buffered_file_reader_test.dart b/hive/test/tests/io/buffered_file_reader_test.dart deleted file mode 100644 index 36bae0fa7..000000000 --- a/hive/test/tests/io/buffered_file_reader_test.dart +++ /dev/null @@ -1,117 +0,0 @@ -@TestOn('vm') -import 'package:hive/src/io/buffered_file_reader.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; - -Future openReader(List bytes, - [int chunkSize = BufferedFileReader.defaultChunkSize]) async { - var file = await getTempFile(bytes); - var raf = await file.open(); - return BufferedFileReader(raf, chunkSize); -} - -void main() { - group('BufferedFileReader', () { - test('constructor creates buffer with correct size', () { - var reader = BufferedFileReader(null); - expect(reader.buffer.length, BufferedFileReader.defaultChunkSize); - - reader = BufferedFileReader(null, 10); - expect(reader.buffer.length, 10); - }); - - group('.skip()', () { - test('increases offset', () async { - var reader = await openReader([1, 2, 3, 4, 5]); - await reader.loadBytes(5); - expect(reader.remainingInBuffer, 5); - expect(reader.offset, 0); - - reader.skip(2); - expect(reader.remainingInBuffer, 3); - expect(reader.offset, 2); - - reader.skip(3); - expect(reader.remainingInBuffer, 0); - expect(reader.offset, 5); - - await reader.file!.close(); - }); - - test('fails if not enough bytes available', () async { - var reader = await openReader([1, 2, 3]); - await reader.loadBytes(5); - expect(reader.remainingInBuffer, 3); - expect(reader.offset, 0); - - expect(() => reader.skip(4), throwsA(anything)); - - await reader.file!.close(); - }); - }); - - group('.viewBytes()', () { - test('returns a view with the requested size', () async { - var reader = await openReader([1, 2, 3, 4, 5]); - await reader.loadBytes(5); - expect(reader.offset, 0); - - expect(reader.viewBytes(2), [1, 2]); - expect(reader.offset, 2); - - expect(reader.viewBytes(3), [3, 4, 5]); - expect(reader.offset, 5); - - await reader.file!.close(); - }); - - test('fails if not enough bytes available', () async { - var reader = await openReader([1, 2, 3, 4, 5]); - await reader.loadBytes(5); - - expect(() => reader.viewBytes(6), throwsA(anything)); - - await reader.file!.close(); - }); - }); - - group('.loadBytes()', () { - test('returns remaining bytes if enough bytes available', () async { - var reader = await openReader([1, 2, 3, 4, 5], 3); - expect(await reader.loadBytes(3), 3); - expect(await reader.loadBytes(2), 3); - - expect(reader.viewBytes(2), [1, 2]); - expect(reader.viewBytes(1), [3]); - - await reader.file!.close(); - }); - - test('increases the buffer if it is too small', () async { - var reader = await openReader([1, 2, 3, 4, 5], 2); - await reader.loadBytes(2); - expect(reader.viewBytes(2), [1, 2]); - - expect(await reader.loadBytes(3), 3); - expect(reader.viewBytes(3), [3, 4, 5]); - - await reader.file!.close(); - }); - - test('copies unused bytes', () async { - var reader = await openReader([1, 2, 3, 4, 5, 6, 7], 3); - await reader.loadBytes(3); - expect(reader.viewBytes(1), [1]); - - expect(await reader.loadBytes(1), 2); - expect(await reader.loadBytes(3), 3); - expect(reader.viewBytes(2), [2, 3]); - expect(await reader.loadBytes(5), 4); - expect(reader.viewBytes(3), [4, 5, 6]); - - await reader.file!.close(); - }); - }); - }); -} diff --git a/hive/test/tests/io/buffered_file_writer_test.dart b/hive/test/tests/io/buffered_file_writer_test.dart deleted file mode 100644 index 9d2514c86..000000000 --- a/hive/test/tests/io/buffered_file_writer_test.dart +++ /dev/null @@ -1,47 +0,0 @@ -@TestOn('vm') -import 'package:hive/src/io/buffered_file_writer.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../mocks.dart'; - -void main() { - group('BufferedFileWriter', () { - test('.write()', () async { - var file = MockRandomAccessFile(); - var writer = BufferedFileWriter(file, 10); - - await writer.write([1, 2, 3, 4, 5, 6]); - await writer.write([7, 8, 9]); - verifyZeroInteractions(file); - - when(() => file.writeFrom(any())).thenAnswer((i) => Future.value(file)); - await writer.write([10]); - verify(() => file.writeFrom([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])); - - reset(file); - await writer.flush(); - verifyZeroInteractions(file); - - when(() => file.writeFrom(any())).thenAnswer((i) => Future.value(file)); - await writer.write([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]); - verify(() => file.writeFrom([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13])); - }); - - test('flush()', () async { - var file = MockRandomAccessFile(); - when(() => file.writeFrom(any())).thenAnswer((i) => Future.value(file)); - var writer = BufferedFileWriter(file, 10); - - await writer.flush(); - verifyZeroInteractions(file); - - await writer.write([1, 2, 3]); - await writer.flush(); - verify(() => file.writeFrom([1, 2, 3])); - - await writer.flush(); - verifyNoMoreInteractions(file); - }); - }); -} diff --git a/hive/test/tests/io/frame_io_helper_test.dart b/hive/test/tests/io/frame_io_helper_test.dart deleted file mode 100644 index 1aae25884..000000000 --- a/hive/test/tests/io/frame_io_helper_test.dart +++ /dev/null @@ -1,104 +0,0 @@ -@TestOn('vm') -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/io/frame_io_helper.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; -import '../frames.dart'; - -Uint8List _getBytes(List list) { - var builder = BytesBuilder(); - for (var b in list) { - builder.add(b); - } - return builder.toBytes(); -} - -class _FrameIoHelperTest extends FrameIoHelper { - final Uint8List bytes; - - _FrameIoHelperTest(this.bytes); - - @override - Future openFile(String path) async { - return getTempRaf(bytes); - } - - @override - Future readFile(String path) async { - return bytes; - } -} - -void main() { - group('FrameIoHelper', () { - group('.keysFromFile()', () { - test('frame', () async { - var keystore = Keystore.debug(); - var ioHelper = _FrameIoHelperTest(_getBytes(frameBytes)); - var recoveryOffset = - await ioHelper.keysFromFile('null', keystore, null); - expect(recoveryOffset, -1); - - var testKeystore = Keystore.debug( - frames: lazyFrames(framesSetLengthOffset(testFrames, frameBytes)), - ); - - expectFrames(keystore.frames, testKeystore.frames); - }); - - test('encrypted', () async { - var keystore = Keystore.debug(); - var ioHelper = _FrameIoHelperTest(_getBytes(frameBytesEncrypted)); - var recoveryOffset = - await ioHelper.keysFromFile('null', keystore, testCipher); - expect(recoveryOffset, -1); - - var testKeystore = Keystore.debug( - frames: lazyFrames( - framesSetLengthOffset(testFrames, frameBytesEncrypted), - ), - ); - - expectFrames(keystore.frames, testKeystore.frames); - }); - - test('returns offset if problem occurs', () async {}); - }); - - group('.allFromFile()', () { - test('frame', () async { - var keystore = Keystore.debug(); - var ioHelper = _FrameIoHelperTest(_getBytes(frameBytes)); - var recoveryOffset = - await ioHelper.framesFromFile('null', keystore, testRegistry, null); - expect(recoveryOffset, -1); - - var testKeystore = Keystore.debug( - frames: framesSetLengthOffset(testFrames, frameBytes), - ); - - expectFrames(keystore.frames, testKeystore.frames); - }); - - test('encrypted', () async { - var keystore = Keystore.debug(); - var ioHelper = _FrameIoHelperTest(_getBytes(frameBytesEncrypted)); - var recoveryOffset = await ioHelper.framesFromFile( - 'null', keystore, testRegistry, testCipher); - expect(recoveryOffset, -1); - - var testKeystore = Keystore.debug( - frames: framesSetLengthOffset(testFrames, frameBytesEncrypted), - ); - - expectFrames(keystore.frames, testKeystore.frames); - }); - - test('returns offset if problem occurs', () async {}); - }); - }); -} diff --git a/hive/test/tests/mocks.dart b/hive/test/tests/mocks.dart deleted file mode 100644 index e8f8b668a..000000000 --- a/hive/test/tests/mocks.dart +++ /dev/null @@ -1,51 +0,0 @@ -library hive.test.mocks; - -import 'dart:io'; - -import 'package:hive/hive.dart'; -import 'package:hive/src/backend/storage_backend.dart'; -import 'package:hive/src/box/change_notifier.dart'; -import 'package:hive/src/box/keystore.dart'; -import 'package:hive/src/hive_impl.dart'; -import 'package:hive/src/io/frame_io_helper.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:mocktail/mocktail.dart'; - -// Mocks - -class MockBox extends Mock implements Box {} - -class MockChangeNotifier extends Mock implements ChangeNotifier {} - -class MockStorageBackend extends Mock implements StorageBackend { - @override - flush() async {} -} - -class MockKeystore extends Mock implements Keystore {} - -class MockHiveImpl extends Mock implements HiveImpl {} - -class MockHiveList extends Mock implements HiveList {} - -class MockHiveListImpl extends Mock implements HiveListImpl {} - -class MockRandomAccessFile extends Mock implements RandomAccessFile {} - -class MockBinaryReader extends Mock implements BinaryReader {} - -class MockBinaryWriter extends Mock implements BinaryWriter {} - -class MockFile extends Mock implements File {} - -class MockFrameIoHelper extends Mock implements FrameIoHelper {} - -// Fakes - -class KeystoreFake extends Fake implements Keystore {} - -class TypeRegistryFake extends Fake implements TypeRegistry {} - -// Dumb objects - -class TestHiveObject extends HiveObject {} diff --git a/hive/test/tests/object/hive_collection_mixin_test.dart b/hive/test/tests/object/hive_collection_mixin_test.dart deleted file mode 100644 index 4406b2fc1..000000000 --- a/hive/test/tests/object/hive_collection_mixin_test.dart +++ /dev/null @@ -1,81 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/object/hive_object.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; -import '../common.dart'; -import '../mocks.dart'; - -HiveList _getTestList(MockBox box) { - when(() => box.name).thenReturn('testBox'); - var obj1 = TestHiveObject(); - obj1.init('key1', box); - var obj2 = TestHiveObject(); - obj2.init('key2', box); - var obj3 = TestHiveObject(); - obj3.init('key3', box); - - return HiveList(box, objects: [obj1, obj2, obj3]); -} - -void main() { - group('HiveCollectionMixin', () { - test('.keys', () { - var box = MockBox(); - var hiveList = _getTestList(box); - - expect(hiveList.keys, ['key1', 'key2', 'key3']); - }); - - test('.deleteAllFromHive()', () { - final keys = ['key1', 'key2', 'key3']; - var box = MockBox(); - var hiveList = _getTestList(box); - returnFutureVoid(when(() => box.deleteAll( - keys.map((e) => e), // Turn the List into an regular Iterable - ))); - - hiveList.deleteAllFromHive(); - verify(() => box.deleteAll(keys)); - }); - - test('.deleteFirstFromHive()', () { - var box = MockBox(); - var hiveList = _getTestList(box); - returnFutureVoid(when(() => box.delete('key1'))); - - hiveList.deleteFirstFromHive(); - verify(() => box.delete('key1')); - }); - - test('.deleteLastFromHive()', () { - var box = MockBox(); - var hiveList = _getTestList(box); - returnFutureVoid(when(() => box.delete('key3'))); - - hiveList.deleteLastFromHive(); - verify(() => box.delete('key3')); - }); - - test('.deleteFromHive()', () { - var box = MockBox(); - var hiveList = _getTestList(box); - returnFutureVoid(when(() => box.delete('key2'))); - - hiveList.deleteFromHive(1); - verify(() => box.delete('key2')); - }); - - test('.toMap()', () { - var box = MockBox(); - when(() => box.name).thenReturn('testBox'); - var obj1 = TestHiveObject(); - obj1.init('key1', box); - var obj2 = TestHiveObject(); - obj2.init('key2', box); - - var hiveList = HiveList(box, objects: [obj1, obj2]); - - expect(hiveList.toMap(), {'key1': obj1, 'key2': obj2}); - }); - }); -} diff --git a/hive/test/tests/object/hive_list_impl_test.dart b/hive/test/tests/object/hive_list_impl_test.dart deleted file mode 100644 index 918714046..000000000 --- a/hive/test/tests/object/hive_list_impl_test.dart +++ /dev/null @@ -1,196 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/object/hive_list_impl.dart'; -import 'package:hive/src/object/hive_object.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../../integration/integration.dart'; -import '../common.dart'; -import '../mocks.dart'; - -HiveObject _getHiveObject(String key, MockBox box) { - var hiveObject = TestHiveObject(); - hiveObject.init(key, box); - when(() => box.get(key, - defaultValue: captureAny(that: isNotNull, named: 'defaultValue'))) - .thenReturn(hiveObject); - when(() => box.get(key)).thenReturn(hiveObject); - return hiveObject; -} - -MockBox _mockBox() { - var box = MockBox(); - // The HiveListImpl constructor sets the boxName property to box.name, - // therefore we need to return an valid String on sound null safety. - when(() => box.name).thenReturn('testBox'); - return box; -} - -void main() { - group('HiveListImpl', () { - test('HiveListImpl()', () { - var box = _mockBox(); - - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - var list = HiveListImpl(box, objects: [item1, item2, item1]); - - expect(item1.debugHiveLists, {list: 2}); - expect(item2.debugHiveLists, {list: 1}); - }); - - test('HiveListImpl.lazy()', () { - var list = HiveListImpl.lazy('testBox', ['key1', 'key2']); - expect(list.boxName, 'testBox'); - expect(list.keys, ['key1', 'key2']); - }); - - group('.box', () { - test('throws HiveError if box is not open', () async { - var hive = await createHive(); - var hiveList = HiveListImpl.lazy('someBox', [])..debugHive = hive; - expect(() => hiveList.box, throwsHiveError('you have to open the box')); - }); - - test('returns the box', () async { - var hive = await createHive(); - var box = await hive.openBox('someBox', - backend: StorageBackendMemory(null, null)); - var hiveList = HiveListImpl.lazy('someBox', [])..debugHive = hive; - expect(hiveList.box, box); - }); - }); - - group('.delegate', () { - test('throws exception if HiveList is disposed', () { - var list = HiveListImpl.lazy('box', []); - list.dispose(); - expect(() => list.delegate, throwsHiveError('already been disposed')); - }); - - test('removes correct elements if invalidated', () { - var box = _mockBox(); - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - var list = HiveListImpl(box, objects: [item1, item2, item1]); - - item1.debugHiveLists.clear(); - expect(list.delegate, [item1, item2, item1]); - list.invalidate(); - expect(list.delegate, [item2]); - }); - - test('creates delegate and links HiveList if delegate == null', () { - var hive = MockHiveImpl(); - var box = _mockBox(); - when(() => box.containsKey('item1')).thenReturn(true); - when(() => box.containsKey('item2')).thenReturn(true); - when(() => box.containsKey('none')).thenReturn(false); - when(() => hive.getBoxWithoutCheckInternal('box')).thenReturn(box); - - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - - var list = HiveListImpl.lazy('box', ['item1', 'none', 'item2', 'item1']) - ..debugHive = hive; - expect(list.delegate, [item1, item2, item1]); - expect(item1.debugHiveLists, {list: 2}); - expect(item2.debugHiveLists, {list: 1}); - }); - }); - - group('.dispose()', () { - test('unlinks remote HiveObjects if delegate exists', () { - var box = _mockBox(); - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - - var list = HiveListImpl(box, objects: [item1, item2, item1]); - list.dispose(); - - expect(item1.debugHiveLists, {}); - expect(item2.debugHiveLists, {}); - }); - }); - - test('set length', () { - var box = _mockBox(); - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - - var list = HiveListImpl(box, objects: [item1, item2]); - list.length = 1; - - expect(item2.debugHiveLists, {}); - expect(list, [item1]); - }); - - group('operator []=', () { - test('sets key at index', () { - var box = _mockBox(); - var oldItem = _getHiveObject('old', box); - var newItem = _getHiveObject('new', box); - - var list = HiveListImpl(box, objects: [oldItem]); - list[0] = newItem; - - expect(oldItem.debugHiveLists, {}); - expect(newItem.debugHiveLists, {list: 1}); - expect(list, [newItem]); - }); - - test('throws HiveError if HiveObject is not valid', () { - var box = _mockBox(); - var oldItem = _getHiveObject('old', box); - var newItem = _getHiveObject('new', MockBox()); - - var list = HiveListImpl(box, objects: [oldItem]); - expect(() => list[0] = newItem, throwsHiveError()); - }); - }); - - group('.add()', () { - test('adds key', () { - var box = _mockBox(); - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - - var list = HiveListImpl(box, objects: [item1]); - list.add(item2); - - expect(item2.debugHiveLists, {list: 1}); - expect(list, [item1, item2]); - }); - - test('throws HiveError if HiveObject is not valid', () { - var box = _mockBox(); - var item = _getHiveObject('item', MockBox()); - var list = HiveListImpl(box); - expect(() => list.add(item), throwsHiveError('needs to be in the box')); - }); - }); - - group('.addAll()', () { - test('adds keys', () { - var box = _mockBox(); - var item1 = _getHiveObject('item1', box); - var item2 = _getHiveObject('item2', box); - - var list = HiveListImpl(box, objects: [item1]); - list.addAll([item2, item2]); - - expect(item2.debugHiveLists, {list: 2}); - expect(list, [item1, item2, item2]); - }); - - test('throws HiveError if HiveObject is not valid', () { - var box = _mockBox(); - var item = _getHiveObject('item', MockBox()); - - var list = HiveListImpl(box); - expect(() => list.addAll([item]), - throwsHiveError('needs to be in the box')); - }); - }); - }); -} diff --git a/hive/test/tests/object/hive_object_test.dart b/hive/test/tests/object/hive_object_test.dart deleted file mode 100644 index bfa5e3278..000000000 --- a/hive/test/tests/object/hive_object_test.dart +++ /dev/null @@ -1,173 +0,0 @@ -import 'package:hive/src/object/hive_object.dart'; -import 'package:mocktail/mocktail.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; -import '../mocks.dart'; - -void main() { - group('HiveObject', () { - group('.init()', () { - test('adds key and box to HiveObject', () { - var obj = TestHiveObject(); - var box = MockBox(); - - obj.init('someKey', box); - - expect(obj.key, 'someKey'); - expect(obj.box, box); - }); - - test('does nothing if old key and box are equal to new key and box', () { - var obj = TestHiveObject(); - var box = MockBox(); - - obj.init('someKey', box); - obj.init('someKey', box); - - expect(obj.key, 'someKey'); - expect(obj.box, box); - }); - - test('throws exception if object is already in a different box', () { - var obj = TestHiveObject(); - var box1 = MockBox(); - var box2 = MockBox(); - - obj.init('someKey', box1); - expect(() => obj.init('someKey', box2), - throwsHiveError('two different boxes')); - }); - - test('throws exception if object has already different key', () { - var obj = TestHiveObject(); - var box = MockBox(); - - obj.init('key1', box); - expect( - () => obj.init('key2', box), throwsHiveError('two different keys')); - }); - }); - - group('.dispose()', () { - test('removes key and box', () { - var obj = TestHiveObject(); - var box = MockBox(); - - obj.init('key', box); - obj.dispose(); - - expect(obj.key, null); - expect(obj.box, null); - }); - - test('notifies remote HiveLists', () { - var obj = TestHiveObject(); - var box = MockBox(); - obj.init('key', box); - - var list = MockHiveListImpl(); - obj.linkHiveList(list); - obj.dispose(); - - verify(() => list.invalidate()); - }); - }); - - test('.linkHiveList()', () { - var box = MockBox(); - var obj = TestHiveObject(); - obj.init('key', box); - var hiveList = MockHiveListImpl(); - - obj.linkHiveList(hiveList); - expect(obj.debugHiveLists, {hiveList: 1}); - obj.linkHiveList(hiveList); - expect(obj.debugHiveLists, {hiveList: 2}); - }); - - test('.unlinkHiveList()', () { - var box = MockBox(); - var obj = TestHiveObject(); - obj.init('key', box); - var hiveList = MockHiveListImpl(); - - obj.linkHiveList(hiveList); - obj.linkHiveList(hiveList); - expect(obj.debugHiveLists, {hiveList: 2}); - - obj.unlinkHiveList(hiveList); - expect(obj.debugHiveLists, {hiveList: 1}); - obj.unlinkHiveList(hiveList); - expect(obj.debugHiveLists, {}); - }); - - group('.save()', () { - test('updates object in box', () { - var obj = TestHiveObject(); - var box = MockBox(); - returnFutureVoid(when(() => box.put('key', obj))); - - obj.init('key', box); - verifyZeroInteractions(box); - - obj.save(); - verify(() => box.put('key', obj)); - }); - - test('throws HiveError if object is not in a box', () async { - var obj = TestHiveObject(); - await expectLater(() => obj.save(), throwsHiveError('not in a box')); - }); - }); - - group('.delete()', () { - test('removes object from box', () { - var obj = TestHiveObject(); - var box = MockBox(); - returnFutureVoid(when(() => box.delete('key'))); - - obj.init('key', box); - verifyZeroInteractions(box); - - obj.delete(); - verify(() => box.delete('key')); - }); - - test('throws HiveError if object is not in a box', () async { - var obj = TestHiveObject(); - await expectLater(() => obj.delete(), throwsHiveError('not in a box')); - }); - }); - - group('.isInBox', () { - test('returns false if box is not set', () { - var obj = TestHiveObject(); - expect(obj.isInBox, false); - }); - - test('returns true if object is in normal box', () { - var obj = TestHiveObject(); - var box = MockBox(); - when(() => box.lazy).thenReturn(false); - obj.init('key', box); - - expect(obj.isInBox, true); - }); - - test('returns the result ob box.containsKey() if object is in lazy box', - () { - var obj = TestHiveObject(); - var box = MockBox(); - when(() => box.lazy).thenReturn(true); - obj.init('key', box); - - when(() => box.containsKey('key')).thenReturn(true); - expect(obj.isInBox, true); - - when(() => box.containsKey('key')).thenReturn(false); - expect(obj.isInBox, false); - }); - }); - }); -} diff --git a/hive/test/tests/registry/type_registry_impl_test.dart b/hive/test/tests/registry/type_registry_impl_test.dart deleted file mode 100644 index 08f052546..000000000 --- a/hive/test/tests/registry/type_registry_impl_test.dart +++ /dev/null @@ -1,211 +0,0 @@ -import 'package:hive/hive.dart'; -import 'package:hive/src/adapters/ignored_type_adapter.dart'; -import 'package:hive/src/registry/type_registry_impl.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; - -class TestAdapter extends TypeAdapter { - TestAdapter([this.typeId = 0]); - - @override - final int typeId; - - @override - int read(BinaryReader reader) { - return 5; - } - - @override - void write(BinaryWriter writer, obj) {} -} - -class TestAdapter2 extends TypeAdapter { - @override - int get typeId => 1; - - @override - int read(BinaryReader reader) { - return 5; - } - - @override - void write(BinaryWriter writer, obj) {} -} - -class Parent {} - -class Child extends Parent {} - -class ParentAdapter extends TypeAdapter { - ParentAdapter([this.typeId = 0]); - - @override - final int typeId; - - @override - Parent read(BinaryReader reader) { - return Parent(); - } - - @override - void write(BinaryWriter writer, Parent obj) {} -} - -class ChildAdapter extends TypeAdapter { - ChildAdapter([this.typeId = 0]); - - @override - final int typeId; - - @override - Child read(BinaryReader reader) { - return Child(); - } - - @override - void write(BinaryWriter writer, Child obj) {} -} - -void main() { - group('TypeRegistryImpl', () { - group('.registerAdapter()', () { - test('register', () { - var registry = TypeRegistryImpl(); - var adapter = TestAdapter(); - registry.registerAdapter(adapter); - - var resolved = registry.findAdapterForValue(123)!; - expect(resolved.typeId, 32); - expect(resolved.adapter, adapter); - }); - - test('unsupported typeId', () { - var registry = TypeRegistryImpl(); - expect(() => registry.registerAdapter(TestAdapter(-1)), - throwsHiveError('not allowed')); - expect(() => registry.registerAdapter(TestAdapter(224)), - throwsHiveError('not allowed')); - }); - - test('duplicate typeId', () { - var registry = TypeRegistryImpl(); - registry.registerAdapter(TestAdapter()); - expect(() => registry.registerAdapter(TestAdapter()), - throwsHiveError('already a TypeAdapter for typeId')); - }); - - test('dynamic type', () { - var registry = TypeRegistryImpl(); - registry.registerAdapter(TestAdapter()); - }); - }); - - test('.findAdapterForTypeId()', () { - var registry = TypeRegistryImpl(); - var adapter = TestAdapter(); - registry.registerAdapter(adapter); - - var resolvedAdapter = registry.findAdapterForTypeId(32)!; - expect(resolvedAdapter.typeId, 32); - expect(resolvedAdapter.adapter, adapter); - }); - - group('.findAdapterForValue()', () { - test('finds adapter', () { - var registry = TypeRegistryImpl(); - var adapter = TestAdapter(); - registry.registerAdapter(adapter); - - var resolvedAdapter = registry.findAdapterForValue(123)!; - expect(resolvedAdapter.typeId, 32); - expect(resolvedAdapter.adapter, adapter); - }); - - test('returns first matching adapter', () { - var registry = TypeRegistryImpl(); - var adapter1 = TestAdapter(0); - var adapter2 = TestAdapter(1); - registry.registerAdapter(adapter1); - registry.registerAdapter(adapter2); - - var resolvedAdapter = registry.findAdapterForValue(123)!; - expect(resolvedAdapter.typeId, 32); - expect(resolvedAdapter.adapter, adapter1); - }); - - test( - 'returns adapter if exact runtime type of value matches ignoring ' - 'registration order', () { - final registry = TypeRegistryImpl(); - final parentAdapter = ParentAdapter(0); - final childAdapter = ChildAdapter(1); - registry.registerAdapter(parentAdapter); - registry.registerAdapter(childAdapter); - - final resolvedAdapter = registry.findAdapterForValue(Child()); - expect(resolvedAdapter?.typeId, 33); - expect(resolvedAdapter?.adapter, childAdapter); - }); - - test('returns super type adapter for subtype', () { - final registry = TypeRegistryImpl(); - final parentAdapter = ParentAdapter(0); - registry.registerAdapter(parentAdapter); - - final resolvedAdapter = registry.findAdapterForValue(Child()); - expect(resolvedAdapter?.typeId, 32); - expect(resolvedAdapter?.adapter, parentAdapter); - }); - }); - - test('.resetAdapters()', () { - var registry = TypeRegistryImpl(); - var adapter = TestAdapter(); - registry.registerAdapter(adapter); - - registry.resetAdapters(); - expect(registry.findAdapterForValue(123), null); - }); - - group('.isAdapterRegistered()', () { - test('returns false if adapter is not found', () { - var registry = TypeRegistryImpl(); - - expect(registry.isAdapterRegistered(0), false); - }); - - test('returns true if adapter is found', () { - var registry = TypeRegistryImpl(); - var adapter = TestAdapter(); - registry.registerAdapter(adapter); - - expect(registry.isAdapterRegistered(0), true); - }); - - test('unsupported typeId', () { - var registry = TypeRegistryImpl(); - expect(() => registry.isAdapterRegistered(-1), - throwsHiveError('not allowed')); - expect(() => registry.isAdapterRegistered(224), - throwsHiveError('not allowed')); - }); - }); - - group('.ignoreTypeId()', () { - test('registers IgnoredTypeAdapter', () { - var registry = TypeRegistryImpl(); - registry.ignoreTypeId(0); - var resolved = registry.findAdapterForTypeId(32)!; - expect(resolved.adapter is IgnoredTypeAdapter, true); - }); - - test('duplicte typeId', () { - var registry = TypeRegistryImpl(); - registry.registerAdapter(TestAdapter()); - expect(() => registry.ignoreTypeId(0), - throwsHiveError('already a TypeAdapter for typeId')); - }); - }); - }); -} diff --git a/hive/test/tests/util/delegating_list_view_mixin_test.dart b/hive/test/tests/util/delegating_list_view_mixin_test.dart deleted file mode 100644 index fa675b9d5..000000000 --- a/hive/test/tests/util/delegating_list_view_mixin_test.dart +++ /dev/null @@ -1,283 +0,0 @@ -import 'dart:math'; - -import 'package:hive/src/util/delegating_list_view_mixin.dart'; -import 'package:test/test.dart'; - -import '../common.dart'; - -void main() { - group('DelegatingIterable', () { - late _TestList testList; - - setUp(() { - testList = _TestList(['a', 'b', 'cc']); - }); - - test('operator +', () { - expect(testList + ['d', 'q'], ['a', 'b', 'cc', 'd', 'q']); - }); - - test('.any()', () { - expect(testList.any((e) => e == 'b'), isTrue); - expect(testList.any((e) => e == 'd'), isFalse); - }); - - test('.asMap()', () { - expect(testList.asMap(), {0: 'a', 1: 'b', 2: 'cc'}); - }); - - test('.cast()', () { - List dynamicList = testList; - expect(dynamicList.cast(), isA>()); - }); - - test('.contains()', () { - expect(testList.contains('b'), isTrue); - expect(testList.contains('d'), isFalse); - }); - - test('.elementAt()', () { - expect(testList.elementAt(1), 'b'); - }); - - test('.every()', () { - expect(testList.every((e) => e == 'b'), isFalse); - }); - - test('.expand()', () { - expect(testList.expand((e) => e.codeUnits), [97, 98, 99, 99]); - }); - - test('.first()', () { - expect(testList.first, 'a'); - }); - - test('.firstWhere()', () { - expect(testList.firstWhere((e) => e == 'b'), 'b'); - expect(testList.firstWhere((e) => e == 'd', orElse: () => 'e'), 'e'); - }); - - test('.fold()', () { - expect(testList.fold('z', (dynamic p, e) => p + e), 'zabcc'); - }); - - test('.forEach()', () { - final s = StringBuffer(); - testList.forEach(s.write); - expect(s.toString(), 'abcc'); - }); - - test('.getRange()', () { - expect(testList.getRange(1, 2), ['b']); - }); - - test('.indexOf()', () { - expect(testList.indexOf('b'), 1); - expect(testList.indexOf('x'), -1); - }); - - test('.indexWhere()', () { - expect(testList.indexWhere((e) => e == 'b'), 1); - expect(testList.indexWhere((e) => e == 'x'), -1); - }); - - test('.isEmpty', () { - expect(testList.isEmpty, isFalse); - expect(_TestList([]).isEmpty, isTrue); - }); - - test('.isNotEmpty', () { - expect(testList.isNotEmpty, isTrue); - expect(_TestList([]).isNotEmpty, isFalse); - }); - - test('.followedBy()', () { - expect(testList.followedBy(['d', 'e']), ['a', 'b', 'cc', 'd', 'e']); - expect(testList.followedBy(testList), ['a', 'b', 'cc', 'a', 'b', 'cc']); - }); - - test('.forEach()', () { - final it = testList.iterator; - if (soundNullSafety) { - expect(() => it.current, throwsA(anything)); - } else { - expect(it.current, null); - } - expect(it.moveNext(), isTrue); - expect(it.current, 'a'); - expect(it.moveNext(), isTrue); - expect(it.current, 'b'); - expect(it.moveNext(), isTrue); - expect(it.current, 'cc'); - expect(it.moveNext(), isFalse); - if (soundNullSafety) { - expect(() => it.current, throwsA(anything)); - } else { - expect(it.current, null); - } - }); - - test('.join()', () { - expect(testList.join(), 'abcc'); - expect(testList.join(','), 'a,b,cc'); - }); - - test('.indexOf()', () { - expect(testList.lastIndexOf('b'), 1); - expect(testList.lastIndexOf('x'), -1); - }); - - test('.indexWhere()', () { - expect(testList.lastIndexWhere((e) => e == 'b'), 1); - expect(testList.lastIndexWhere((e) => e == 'x'), -1); - }); - - test('.last', () { - expect(testList.last, 'cc'); - }); - - test('.lastWhere()', () { - expect(testList.lastWhere((e) => e == 'b'), 'b'); - expect(testList.lastWhere((e) => e == 'd', orElse: () => 'e'), 'e'); - }); - - test('.length', () { - expect(testList.length, 3); - }); - - test('.map()', () { - expect(testList.map((e) => e.toUpperCase()), ['A', 'B', 'CC']); - }); - - test('.reduce()', () { - expect(testList.reduce((value, element) => value + element), 'abcc'); - }); - - test('.reversed ', () { - expect(testList.reversed, ['cc', 'b', 'a']); - }); - - test('single', () { - expect(() => testList.single, throwsStateError); - expect(_TestList(['a']).single, 'a'); - }); - - test('.singleWhere()', () { - expect(testList.singleWhere((e) => e == 'b'), 'b'); - expect(() => testList.singleWhere((e) => e == 'd'), throwsStateError); - expect(testList.singleWhere((e) => e == 'd', orElse: () => 'X'), 'X'); - }); - - test('.skip()', () { - expect(testList.skip(1), ['b', 'cc']); - }); - - test('.skipWhile()', () { - expect(testList.skipWhile((e) => e == 'a'), ['b', 'cc']); - }); - - test('.sublist()', () { - expect(testList.sublist(1, 2), ['b']); - }); - - test('.take()', () { - expect(testList.take(1), ['a']); - }); - - test('.skipWhile()', () { - expect(testList.takeWhile((e) => e == 'a'), ['a']); - }); - - test('.toList()', () { - expect(testList.toList(), ['a', 'b', 'cc']); - }); - - test('.toSet()', () { - expect(testList.toSet(), {'a', 'b', 'cc'}); - }); - - test('.where()', () { - expect(testList.where((e) => e.length == 1), ['a', 'b']); - }); - - test('.whereType()', () { - expect(testList.whereType(), ['a', 'b', 'cc']); - }); - }); -} - -class _TestList with DelegatingListViewMixin { - final List _delegate; - - _TestList(this._delegate); - - @override - List get delegate => _delegate; - - @override - void operator []=(int index, T value) => throw UnimplementedError(); - - @override - void add(T value) => throw UnimplementedError(); - - @override - void addAll(Iterable iterable) => throw UnimplementedError(); - - @override - void clear() => throw UnimplementedError(); - - @override - void fillRange(int start, int end, [T? fillValue]) => - throw UnimplementedError(); - - @override - set first(T value) => throw UnimplementedError(); - - @override - void insert(int index, T element) => throw UnimplementedError(); - - @override - void insertAll(int index, Iterable iterable) => throw UnimplementedError(); - - @override - set last(T value) => throw UnimplementedError(); - - @override - set length(int newLength) => throw UnimplementedError(); - - @override - bool remove(Object? value) => throw UnimplementedError(); - - @override - T removeAt(int index) => throw UnimplementedError(); - - @override - T removeLast() => throw UnimplementedError(); - - @override - void removeRange(int start, int end) => throw UnimplementedError(); - - @override - void removeWhere(bool Function(T element) test) => throw UnimplementedError(); - - @override - void replaceRange(int start, int end, Iterable replacement) => - throw UnimplementedError(); - - @override - void retainWhere(bool Function(T element) test) => throw UnimplementedError(); - - @override - void setAll(int index, Iterable iterable) => throw UnimplementedError(); - - @override - void setRange(int start, int end, Iterable iterable, - [int skipCount = 0]) => - throw UnimplementedError(); - - @override - void shuffle([Random? random]) => throw UnimplementedError(); - - @override - void sort([int Function(T a, T b)? compare]) => throw UnimplementedError(); -} diff --git a/hive/test/tests/util/indexable_skip_list_test.dart b/hive/test/tests/util/indexable_skip_list_test.dart deleted file mode 100644 index a77927eeb..000000000 --- a/hive/test/tests/util/indexable_skip_list_test.dart +++ /dev/null @@ -1,55 +0,0 @@ -import 'dart:math'; - -import 'package:hive/src/util/indexable_skip_list.dart'; -import 'package:test/test.dart'; - -void main() { - group('IndexableSkipList', () { - List getRandomList() { - var rand = Random(); - var data = List.generate(1000, (i) => i); - data.addAll(List.generate(500, (i) => rand.nextInt(1000))); - data.addAll(List.generate(250, (i) => 1000 - i % 50)); - data.addAll(List.generate(250, (i) => i)); - data.shuffle(); - return data; - } - - void checkList(IndexableSkipList list, List keys) { - var sortedKeys = keys.toSet().toList()..sort(); - expect(list.keys, sortedKeys); - for (var n = 0; n < sortedKeys.length; n++) { - var key = sortedKeys[n]; - expect(list.get(key), '$key'); - expect(list.getAt(n), '$key'); - } - } - - test('.insert() puts value at the correct position', () { - var list = IndexableSkipList(Comparable.compare); - var data = getRandomList(); - - for (var i = 0; i < data.length; i++) { - list.insert(data[i], '${data[i]}'); - var alreadyAdded = data.sublist(0, i + 1); - checkList(list, alreadyAdded); - } - }); - - test('.delete() removes key', () { - var list = IndexableSkipList(Comparable.compare); - var data = getRandomList(); - for (var key in data) { - list.insert(key, '$key'); - } - - var keys = data.toSet().toList()..shuffle(); - while (keys.isNotEmpty) { - var key = keys.first; - expect(list.delete(key), '$key'); - keys.remove(key); - checkList(list, keys); - } - }); - }); -} diff --git a/hive/test/util/is_browser.dart b/hive/test/util/is_browser.dart deleted file mode 100644 index 5f9cb392e..000000000 --- a/hive/test/util/is_browser.dart +++ /dev/null @@ -1 +0,0 @@ -export 'is_browser_vm.dart' if (dart.library.html) 'is_browser_js.dart'; diff --git a/hive/test/util/is_browser_js.dart b/hive/test/util/is_browser_js.dart deleted file mode 100644 index 632b0edb0..000000000 --- a/hive/test/util/is_browser_js.dart +++ /dev/null @@ -1 +0,0 @@ -const isBrowser = true; diff --git a/hive/test/util/is_browser_vm.dart b/hive/test/util/is_browser_vm.dart deleted file mode 100644 index 0712d4eca..000000000 --- a/hive/test/util/is_browser_vm.dart +++ /dev/null @@ -1 +0,0 @@ -const isBrowser = false; diff --git a/hive_flutter/.gitignore b/hive_flutter/.gitignore deleted file mode 100644 index f8012584c..000000000 --- a/hive_flutter/.gitignore +++ /dev/null @@ -1,32 +0,0 @@ -# Miscellaneous -*.class -*.log -*.pyc -*.swp -.DS_Store -.atom/ -.buildlog/ -.history -.svn/ -migrate_working_dir/ - -# IntelliJ related -*.iml -*.ipr -*.iws -.idea/ - -# The .vscode folder contains launch configuration and tasks you configure in -# VS Code which you may wish to be included in version control, so this line -# is commented out by default. -#.vscode/ - -# Flutter/Dart/Pub related -# Libraries should not include pubspec.lock, per https://dart.dev/guides/libraries/private-files#pubspeclock. -/pubspec.lock -**/doc/api/ -.dart_tool/ -.packages -build/ - -.flutter-plugins-dependencies diff --git a/hive_flutter/.metadata b/hive_flutter/.metadata deleted file mode 100644 index 45789c722..000000000 --- a/hive_flutter/.metadata +++ /dev/null @@ -1,10 +0,0 @@ -# This file tracks properties of this Flutter project. -# Used by Flutter tool to assess capabilities and perform upgrades etc. -# -# This file should be version controlled and should not be manually edited. - -version: - revision: 20e59316b8b8474554b38493b8ca888794b0234a - channel: stable - -project_type: package diff --git a/hive_flutter/CHANGELOG.md b/hive_flutter/CHANGELOG.md deleted file mode 100644 index d6d3dda0e..000000000 --- a/hive_flutter/CHANGELOG.md +++ /dev/null @@ -1,46 +0,0 @@ -## 2.0.0-dev - -- Threaded AesCipher support - -## 1.1.0 - -- Exports `Color` and `TimeOfDay` adapters - [#698](https://github.com/hivedb/hive/pull/698) -- Fixed null safety score fail - [#698](https://github.com/hivedb/hive/pull/698) -- Fixed minor linter warnings -- Replaced `package:pedantic` with `package:linter` -- Exports `package:hive/hive.dart` - -## 1.0.0 - -- Stable null-safety version - -## 0.1.0-nullsafety.0 - -- Null safety support - -## 0.3.1 - -- Added Time and Color adapters - -## 0.3.0+2 - -- Fixed GitHub homepage path - -## 0.3.0+1 - -- Added `box.listenable()` which returns a `ValueListenable` -- Added `Hive.initFlutter()` -- **Breaking** Deprecated `WatchBoxBuilder` in favor of `box.listenable()`. Will be removed in a future version - -## 0.2.1 - -- Bump Hive version -- Changed WatchBoxBuilderState to \_WatchBoxBuilderState - -## 0.2.0 - -- Breaking API change, renamed `HiveBuilder` to `BoxWidgetBuilder`. - -## 0.1.0 - -- First version diff --git a/hive_flutter/LICENSE b/hive_flutter/LICENSE deleted file mode 100644 index bd506236f..000000000 --- a/hive_flutter/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2019 Simon Leier - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file diff --git a/hive_flutter/README.md b/hive_flutter/README.md deleted file mode 100644 index 7731e79e5..000000000 --- a/hive_flutter/README.md +++ /dev/null @@ -1 +0,0 @@ -### Extension for [hive](https://github.com/leisim/hive) please go there for documentation. \ No newline at end of file diff --git a/hive_flutter/analysis_options.yaml b/hive_flutter/analysis_options.yaml deleted file mode 100644 index 3c7d4d6c2..000000000 --- a/hive_flutter/analysis_options.yaml +++ /dev/null @@ -1,80 +0,0 @@ -include: package:lints/recommended.yaml -analyzer: - exclude: - - "**/*.g.dart" - - "example/*" - strong-mode: - implicit-casts: false - errors: - invalid_use_of_protected_member: error - always_declare_return_types: error -linter: - rules: - - always_declare_return_types - - annotate_overrides - - avoid_empty_else - - avoid_function_literals_in_foreach_calls - - avoid_init_to_null - - avoid_null_checks_in_equality_operators - - avoid_relative_lib_imports - - avoid_renaming_method_parameters - - avoid_return_types_on_setters - - avoid_returning_null - - avoid_types_as_parameter_names - - avoid_unused_constructor_parameters - - await_only_futures - - camel_case_types - - cancel_subscriptions - - comment_references - - constant_identifier_names - - control_flow_in_finally - - directives_ordering - - empty_catches - - empty_constructor_bodies - - empty_statements - - implementation_imports - - invariant_booleans - - iterable_contains_unrelated_type - - library_names - - library_prefixes - - list_remove_unrelated_type - - lines_longer_than_80_chars - - no_adjacent_strings_in_list - - no_duplicate_case_values - - non_constant_identifier_names - - null_closures - - omit_local_variable_types - - only_throw_errors - - overridden_fields - - package_api_docs - - package_names - - package_prefixed_library_names - - prefer_adjacent_string_concatenation - - prefer_collection_literals - - prefer_conditional_assignment - - prefer_contains - - prefer_equal_for_default_values - - prefer_final_fields - - prefer_initializing_formals - - prefer_interpolation_to_compose_strings - - prefer_is_empty - - prefer_is_not_empty - - prefer_single_quotes - - prefer_typing_uninitialized_variables - - recursive_getters - - slash_for_doc_comments - - test_types_in_equals - - throw_in_finally - - type_init_formals - - unawaited_futures - - unnecessary_brace_in_string_interps - - unnecessary_const - - unnecessary_getters_setters - - unnecessary_lambdas - - unnecessary_new - - unnecessary_null_aware_assignments - - unnecessary_statements - - unnecessary_this - - unrelated_type_equality_checks - - use_rethrow_when_possible - - valid_regexps diff --git a/hive_flutter/lib/adapters.dart b/hive_flutter/lib/adapters.dart deleted file mode 100644 index 0d3bbfbcf..000000000 --- a/hive_flutter/lib/adapters.dart +++ /dev/null @@ -1,9 +0,0 @@ -library hive_flutter_adapters; - -import 'package:flutter/material.dart' show Color, TimeOfDay; -import 'package:hive/hive.dart' show TypeAdapter, BinaryReader, BinaryWriter; - -export 'hive_flutter.dart'; - -part 'src/adapters/color_adapter.dart'; -part 'src/adapters/time_adapter.dart'; diff --git a/hive_flutter/lib/hive_flutter.dart b/hive_flutter/lib/hive_flutter.dart deleted file mode 100644 index 555efd51f..000000000 --- a/hive_flutter/lib/hive_flutter.dart +++ /dev/null @@ -1,19 +0,0 @@ -library hive_flutter; - -import 'dart:async'; - -import 'package:flutter/foundation.dart'; -import 'package:flutter/widgets.dart'; -import 'package:hive/hive.dart'; -import 'package:path/path.dart' if (dart.library.html) 'src/stub/path.dart' - as path_helper; -import 'package:path_provider/path_provider.dart' - if (dart.library.html) 'src/stub/path_provider.dart'; - -export 'package:hive/hive.dart'; -export 'src/crypto/hive_aes_native_cbc_cipher.dart'; -export 'src/crypto/hive_aes_native_gcm_cipher.dart'; - -part 'src/box_extensions.dart'; -part 'src/hive_extensions.dart'; -part 'src/watch_box_builder.dart'; diff --git a/hive_flutter/lib/src/adapters/color_adapter.dart b/hive_flutter/lib/src/adapters/color_adapter.dart deleted file mode 100644 index ff92586ef..000000000 --- a/hive_flutter/lib/src/adapters/color_adapter.dart +++ /dev/null @@ -1,12 +0,0 @@ -part of hive_flutter_adapters; - -class ColorAdapter extends TypeAdapter { - @override - Color read(BinaryReader reader) => Color(reader.readInt()); - - @override - void write(BinaryWriter writer, Color obj) => writer.writeInt(obj.value); - - @override - int get typeId => 200; -} diff --git a/hive_flutter/lib/src/adapters/time_adapter.dart b/hive_flutter/lib/src/adapters/time_adapter.dart deleted file mode 100644 index 5dfa040d1..000000000 --- a/hive_flutter/lib/src/adapters/time_adapter.dart +++ /dev/null @@ -1,17 +0,0 @@ -part of hive_flutter_adapters; - -class TimeOfDayAdapter extends TypeAdapter { - @override - TimeOfDay read(BinaryReader reader) { - final totalMinutes = reader.readInt(); - return TimeOfDay(hour: totalMinutes ~/ 60, minute: totalMinutes % 60); - } - - @override - void write(BinaryWriter writer, TimeOfDay obj) { - writer.writeInt(obj.hour * 60 + obj.minute); - } - - @override - int get typeId => 201; -} diff --git a/hive_flutter/lib/src/box_extensions.dart b/hive_flutter/lib/src/box_extensions.dart deleted file mode 100644 index 269e8d19d..000000000 --- a/hive_flutter/lib/src/box_extensions.dart +++ /dev/null @@ -1,71 +0,0 @@ -part of hive_flutter; - -/// Flutter extensions for boxes. -extension BoxX on Box { - /// Returns a [ValueListenable] which notifies its listeners when an entry - /// in the box changes. - /// - /// If [keys] filter is provided, only changes to entries with the - /// specified keys notify the listeners. - ValueListenable> listenable({List? keys}) => - _BoxListenable(this, keys?.toSet()); -} - -/// Flutter extensions for lazy boxes. -extension LazyBoxX on LazyBox { - /// Returns a [ValueListenable] which notifies its listeners when an entry - /// in the box changes. - /// - /// If [keys] filter is provided, only changes to entries with the - /// specified keys notify the listeners. - ValueListenable> listenable({List? keys}) => - _BoxListenable(this, keys?.toSet()); -} - -class _BoxListenable> extends ValueListenable { - final B box; - - final Set? keys; - - final List _listeners = []; - - StreamSubscription? _subscription; - - _BoxListenable(this.box, this.keys); - - @override - void addListener(VoidCallback listener) { - if (_listeners.isEmpty) { - if (keys != null) { - _subscription = box.watch().listen((event) { - if (keys!.contains(event.key)) { - for (var listener in _listeners) { - listener(); - } - } - }); - } else { - _subscription = box.watch().listen((_) { - for (var listener in _listeners) { - listener(); - } - }); - } - } - - _listeners.add(listener); - } - - @override - void removeListener(VoidCallback listener) { - _listeners.remove(listener); - - if (_listeners.isEmpty) { - _subscription?.cancel(); - _subscription = null; - } - } - - @override - B get value => box; -} diff --git a/hive_flutter/lib/src/crypto/extensions.dart b/hive_flutter/lib/src/crypto/extensions.dart deleted file mode 100644 index eec527dc3..000000000 --- a/hive_flutter/lib/src/crypto/extensions.dart +++ /dev/null @@ -1,11 +0,0 @@ -import 'dart:typed_data'; - -/// Not part of public API -extension Uint8ListX on Uint8List { - /// Not part of public API - @pragma('vm:prefer-inline') - @pragma('dart2js:tryInline') - Uint8List view(int offset, int bytes) { - return Uint8List.view(buffer, offsetInBytes + offset, bytes); - } -} diff --git a/hive_flutter/lib/src/crypto/hive_aes_native_cbc_cipher.dart b/hive_flutter/lib/src/crypto/hive_aes_native_cbc_cipher.dart deleted file mode 100644 index 812b8ba1e..000000000 --- a/hive_flutter/lib/src/crypto/hive_aes_native_cbc_cipher.dart +++ /dev/null @@ -1,104 +0,0 @@ -import 'dart:async'; -import 'dart:typed_data'; - -import 'package:cryptography/cryptography.dart'; -import 'package:hive_flutter/hive_flutter.dart'; -import 'package:hive_flutter/src/crypto/extensions.dart'; - -/// Multi-threaded cipher. Uses AES256 CBC -/// -/// **IMPORTANT**: Setup required: -/// https://pub.dev/packages/cryptography_flutter#getting-started -/// -/// 1. add dependency to pubspec.yaml -/// -/// ```yaml -/// dependencies: -/// cryptography_flutter: ^2.0.2 -/// ``` -/// -/// 2. enable native implementations -/// -/// ```dart -/// import 'package:cryptography_flutter/cryptography_flutter.dart'; -/// -/// void main() { -/// // Enable Flutter cryptography -/// FlutterCryptography.enable(); -/// -/// // .... -/// } -/// ``` -/// -/// This implementation extends the [HiveCipher] with native AES implementations -/// on Android, iOS, macOS as well as on web using [AesCbc]. -/// -/// This may highly decrease the delay in the event loop of the Flutter -/// application. A disadvantage is the fact that each computation takes a bit -/// longer than its main-thread equivalent because spawning a new thread -/// takes its time. -class HiveAesNativeCbcCipher extends HiveAesCipher { - late final List _key; - final Completer secretKey = Completer(); - - HiveAesNativeCbcCipher(List key) - : assert(key.length == 32 && key.every((it) => it > 0 || it <= 255), - 'The encryption key has to be a 32 byte (256 bit) array.'), - _key = key, - super(key) { - _algorithm.newSecretKeyFromBytes(_key).then(secretKey.complete); - } - - /// AES-CBC with 256 bit keys - AesCbc get _algorithm => AesCbc.with256bits(macAlgorithm: MacAlgorithm.empty); - - @override - Future decrypt(Uint8List inp, int inpOff, int inpLength, Uint8List out, - int outOff) async { - final bytes = inp.view(inpOff, inpLength); - - /// preparing the [SecretBox] with the range from the [inpOff] in the [inp] - final secretBox = - SecretBox.fromConcatenation(bytes, nonceLength: 16, macLength: 0); - - /// decrypting - final result = - await _algorithm.decrypt(secretBox, secretKey: await secretKey.future); - - /// save the clear text in the [out] - out.setRange(outOff, outOff + result.length, result); - - /// return the clear text length as new offset - return result.length; - } - - @override - Future encrypt(Uint8List inp, int inpOff, int inpLength, Uint8List out, - int outOff) async { - /// generating new nonce / iv - final nonce = _algorithm.newNonce(); - - /// encrypt the next 16 bytes of the [inp] starting from [inpOff] - final secretBox = await _algorithm.encrypt( - inp.view(inpOff, inpLength), - secretKey: await secretKey.future, - nonce: nonce, - ); - - assert(secretBox.mac.bytes.isEmpty && secretBox.nonce.length == 16, - 'The AesCbc nonce / iv and mac should be 16 / 0 bytes long.'); - - /// concentrating the encrypted text, nonce / iv and mac - final concentration = secretBox.concatenation(); - - /// save the encrypted bytes in the [out] - final outOffset = outOff + concentration.length; - if (outOffset > out.length) { - final previousOut = Uint8List.fromList(out); - out = Uint8List(outOffset)..setRange(0, previousOut.length, previousOut); - } - out.setRange(outOff, outOffset, concentration); - - return concentration.length; - } -} diff --git a/hive_flutter/lib/src/crypto/hive_aes_native_gcm_cipher.dart b/hive_flutter/lib/src/crypto/hive_aes_native_gcm_cipher.dart deleted file mode 100644 index 0c072c883..000000000 --- a/hive_flutter/lib/src/crypto/hive_aes_native_gcm_cipher.dart +++ /dev/null @@ -1,104 +0,0 @@ -import 'dart:async'; -import 'dart:typed_data'; - -import 'package:cryptography/cryptography.dart'; -import 'package:hive_flutter/hive_flutter.dart'; -import 'package:hive_flutter/src/crypto/extensions.dart'; - -/// Multi-threaded cipher. Uses AES256 GCM -/// -/// **IMPORTANT**: Setup required: -/// https://pub.dev/packages/cryptography_flutter#getting-started -/// -/// 1. add dependency to pubspec.yaml -/// -/// ```yaml -/// dependencies: -/// cryptography_flutter: ^2.0.2 -/// ``` -/// -/// 2. enable native implementations -/// -/// ```dart -/// import 'package:cryptography_flutter/cryptography_flutter.dart'; -/// -/// void main() { -/// // Enable Flutter cryptography -/// FlutterCryptography.enable(); -/// -/// // .... -/// } -/// ``` -/// -/// This implementation extends the [HiveCipher] with native AES implementations -/// on Android, iOS, macOS as well as on web using [AesGcm]. -/// -/// This may highly decrease the delay in the event loop of the Flutter -/// application. A disadvantage is the fact that each computation takes a bit -/// longer than its main-thread equivalent because spawning a new thread -/// takes its time. -class HiveAesNativeGcmCipher extends HiveAesCipher { - late final List _key; - final Completer secretKey = Completer(); - - HiveAesNativeGcmCipher(List key) - : assert(key.length == 32 && key.every((it) => it > 0 || it <= 255), - 'The encryption key has to be a 32 byte (256 bit) array.'), - _key = key, - super(key) { - _algorithm.newSecretKeyFromBytes(_key).then(secretKey.complete); - } - - /// AES-GCM with 256 bit keys - AesGcm get _algorithm => AesGcm.with256bits(nonceLength: 16); - - @override - Future decrypt(Uint8List inp, int inpOff, int inpLength, Uint8List out, - int outOff) async { - final bytes = inp.view(inpOff, inpLength); - - /// preparing the [SecretBox] with the range from the [inpOff] in the [inp] - final secretBox = - SecretBox.fromConcatenation(bytes, nonceLength: 16, macLength: 16); - - /// decrypting - final result = - await _algorithm.decrypt(secretBox, secretKey: await secretKey.future); - - /// save the clear text in the [out] - out.setRange(outOff, outOff + result.length, result); - - /// return the clear text length as new offset - return result.length; - } - - @override - Future encrypt(Uint8List inp, int inpOff, int inpLength, Uint8List out, - int outOff) async { - /// generating new nonce / iv - final nonce = _algorithm.newNonce(); - - /// encrypt the next 16 bytes of the [inp] starting from [inpOff] - final secretBox = await _algorithm.encrypt( - inp.view(inpOff, inpLength), - secretKey: await secretKey.future, - nonce: nonce, - ); - - assert(secretBox.mac.bytes.length == 16 && secretBox.nonce.length == 16, - 'The AesGcm nonce / iv and mac should be 16 bytes long.'); - - /// concentrating the encrypted text, nonce / iv and mac - final concentration = secretBox.concatenation(); - - /// save the encrypted bytes in the [out] - final outOffset = outOff + concentration.length; - if (outOffset > out.length) { - final previousOut = Uint8List.fromList(out); - out = Uint8List(outOffset)..setRange(0, previousOut.length, previousOut); - } - out.setRange(outOff, outOffset, concentration); - - return concentration.length; - } -} diff --git a/hive_flutter/lib/src/hive_extensions.dart b/hive_flutter/lib/src/hive_extensions.dart deleted file mode 100644 index 148e76098..000000000 --- a/hive_flutter/lib/src/hive_extensions.dart +++ /dev/null @@ -1,25 +0,0 @@ -part of hive_flutter; - -/// Flutter extensions for Hive. -extension HiveX on HiveInterface { - /// Initializes Hive with the path from [getApplicationDocumentsDirectory]. - /// - /// You can provide a [subDir] where the boxes should be stored. - Future initFlutter( - [String? subDir, - HiveStorageBackendPreference backendPreference = - HiveStorageBackendPreference.native]) async { - WidgetsFlutterBinding.ensureInitialized(); - - String? path; - if (!kIsWeb) { - var appDir = await getApplicationDocumentsDirectory(); - path = path_helper.join(appDir.path, subDir); - } - - init( - path, - backendPreference: backendPreference, - ); - } -} diff --git a/hive_flutter/lib/src/stub/path.dart b/hive_flutter/lib/src/stub/path.dart deleted file mode 100644 index e6935f832..000000000 --- a/hive_flutter/lib/src/stub/path.dart +++ /dev/null @@ -1,14 +0,0 @@ -String join( - String part1, [ - String? part2, - String? part3, - String? part4, - String? part5, - String? part6, - String? part7, - String? part8, -]) { - throw UnimplementedError( - '[Hive Error] Tried to use the `path` package from Flutter Web.', - ); -} diff --git a/hive_flutter/lib/src/stub/path_provider.dart b/hive_flutter/lib/src/stub/path_provider.dart deleted file mode 100644 index 7defb7d96..000000000 --- a/hive_flutter/lib/src/stub/path_provider.dart +++ /dev/null @@ -1,9 +0,0 @@ -abstract class Directory { - String get path; -} - -Future getApplicationDocumentsDirectory() { - throw UnimplementedError( - '[Hive Error] Tried to use the `path_provider` package from Flutter Web.', - ); -} diff --git a/hive_flutter/lib/src/watch_box_builder.dart b/hive_flutter/lib/src/watch_box_builder.dart deleted file mode 100644 index b34cef168..000000000 --- a/hive_flutter/lib/src/watch_box_builder.dart +++ /dev/null @@ -1,84 +0,0 @@ -part of hive_flutter; - -/// Signature for a function that builds a widget given a [Box]. -@Deprecated('Use [ValueListenableBuilder] and `box.listenable()` instead') -typedef BoxWidgetBuilder = Widget Function(BuildContext context, Box box); - -/// A general-purpose widget which rebuilds itself when the box or a specific -/// key change. -/// -/// Deprecated: Use [ValueListenableBuilder] and `box.listenable()` instead -@Deprecated('Use [ValueListenableBuilder] and `box.listenable()` instead') -class WatchBoxBuilder extends StatefulWidget { - /// Creates a widget that rebuilds itself when a value in the [box] changes. - /// - /// If you specify [watchKeys], the widget only refreshes when a value - /// associated to a key in [watchKeys] changes. - WatchBoxBuilder({ - Key? key, - required this.box, - required this.builder, - this.watchKeys, - }) : super(key: key); - - /// The box which should be watched. - final Box box; - - /// Called every time the box changes. The builder must not return null. - final BoxWidgetBuilder builder; - - /// Specifies which keys should be watched. - final List? watchKeys; - - @override - _WatchBoxBuilderState createState() => _WatchBoxBuilderState(); -} - -@Deprecated('Use [ValueListenableBuilder] and `box.listenable()` instead') -class _WatchBoxBuilderState extends State { - @visibleForTesting - StreamSubscription? subscription; - - @override - void initState() { - super.initState(); - - _subscribe(); - } - - @override - // ignore: deprecated_member_use - void didUpdateWidget(WatchBoxBuilder oldWidget) { - super.didUpdateWidget(oldWidget); - - if (widget.box != oldWidget.box) { - _unsubscribe(); - _subscribe(); - } - } - - void _subscribe() { - subscription = widget.box.watch().listen((event) { - if (widget.watchKeys != null && !widget.watchKeys!.contains(event.key)) { - return; - } - - setState(() {}); - }); - } - - void _unsubscribe() { - subscription?.cancel(); - } - - @override - Widget build(BuildContext context) { - return widget.builder(context, widget.box); - } - - @override - void dispose() { - _unsubscribe(); - super.dispose(); - } -} diff --git a/hive_flutter/pubspec.yaml b/hive_flutter/pubspec.yaml deleted file mode 100644 index 52c8a241b..000000000 --- a/hive_flutter/pubspec.yaml +++ /dev/null @@ -1,23 +0,0 @@ -name: hive_flutter -description: Extension for Hive. Makes it easier to use Hive in Flutter apps. -version: 2.0.0-dev -homepage: https://github.com/hivedb/hive/tree/master/hive_flutter -documentation: https://docs.hivedb.dev/ - -environment: - sdk: ">=2.12.0 <3.0.0" - -dependencies: - flutter: - sdk: flutter - - cryptography: ^2.0.5 - hive: ^3.0.0-dev - path_provider: ^2.0.10 - path: ^1.8.1 - -dev_dependencies: - test: ^1.21.1 - lints: ">=1.0.0" - mockito: ^5.2.0 - build_runner: ^2.1.11 diff --git a/hive_flutter/test/adapters/color_adapter_test.dart b/hive_flutter/test/adapters/color_adapter_test.dart deleted file mode 100644 index 9c8a6251b..000000000 --- a/hive_flutter/test/adapters/color_adapter_test.dart +++ /dev/null @@ -1,29 +0,0 @@ -import 'dart:ui' show Color; - -import 'package:hive_flutter/adapters.dart'; -import 'package:mockito/mockito.dart'; -import 'package:test/test.dart'; - -import '../mocks.dart'; - -void main() { - group('ColorAdapter', () { - test('.read()', () { - const color = Color(0xFF000000); - final BinaryReader binaryReader = MockBinaryReader(); - when(binaryReader.readInt()).thenReturn(color.value); - - final readColor = ColorAdapter().read(binaryReader); - verify(binaryReader.readInt()); - expect(readColor, color); - }); - - test('.write()', () { - const color = Color(0xFF000000); - final BinaryWriter binaryWriter = MockBinaryWriter(); - - ColorAdapter().write(binaryWriter, color); - verify(binaryWriter.writeInt(color.value)); - }); - }); -} diff --git a/hive_flutter/test/adapters/time_adapter_test.dart b/hive_flutter/test/adapters/time_adapter_test.dart deleted file mode 100644 index 72ae9546d..000000000 --- a/hive_flutter/test/adapters/time_adapter_test.dart +++ /dev/null @@ -1,34 +0,0 @@ -import 'package:flutter/material.dart' show TimeOfDay; -import 'package:hive_flutter/adapters.dart'; -import 'package:mockito/mockito.dart'; -import 'package:test/test.dart'; - -import '../mocks.dart'; - -void main() { - group('TimeOfDayAdapter', () { - late TimeOfDay time; - late int totalMinutes; - - setUp(() { - time = TimeOfDay(hour: 8, minute: 0); - totalMinutes = time.hour * 60 + time.minute; - }); - - test('.read()', () { - final BinaryReader binaryReader = MockBinaryReader(); - when(binaryReader.readInt()).thenReturn(totalMinutes); - - final readTime = TimeOfDayAdapter().read(binaryReader); - verify(binaryReader.readInt()).called(1); - expect(readTime, time); - }); - - test('.write()', () { - final BinaryWriter binaryWriter = MockBinaryWriter(); - - TimeOfDayAdapter().write(binaryWriter, time); - verify(binaryWriter.writeInt(totalMinutes)); - }); - }); -} diff --git a/hive_flutter/test/crypto/directory.dart b/hive_flutter/test/crypto/directory.dart deleted file mode 100644 index 7f9964919..000000000 --- a/hive_flutter/test/crypto/directory.dart +++ /dev/null @@ -1,19 +0,0 @@ -import 'dart:io'; -import 'dart:math'; -import 'package:path/path.dart' as path; - -String tempPath = - path.join(Directory.current.path, '.dart_tool', 'test', 'tmp'); - -Future getTempDir() async { - final random = Random(); - var name = random.nextInt(pow(2, 32) as int); - var dir = Directory(path.join(tempPath, '${name}_tmp')); - if (await dir.exists()) { - await dir.delete(recursive: true); - } - await dir.create(recursive: true); - return dir; -} - -const boxName = 'test'; diff --git a/hive_flutter/test/crypto/hive_aes_native_cipher_test.dart b/hive_flutter/test/crypto/hive_aes_native_cipher_test.dart deleted file mode 100644 index 664020f3c..000000000 --- a/hive_flutter/test/crypto/hive_aes_native_cipher_test.dart +++ /dev/null @@ -1,37 +0,0 @@ -import 'dart:math'; - -import 'package:hive_flutter/adapters.dart'; -import 'package:test/test.dart'; - -import 'directory.dart'; - -void main() { - group('HiveAesNativeCbcCipher', () { - late HiveAesNativeCbcCipher cipher; - late Box box; - late List message; - - setUp(() async { - final random = Random.secure(); - cipher = HiveAesNativeCbcCipher( - List.generate(32, (index) => random.nextInt(255)), - ); - - var dir = await getTempDir(); - Hive.init(dir.path); - - box = await Hive.openBox>(boxName, encryptionCipher: cipher); - - message = List.generate(64, (index) => random.nextInt(255)); - }); - test('encrypt()', () async { - final index = await box.add(message); - - await box.close(); - - box = await Hive.openBox>(boxName, encryptionCipher: cipher); - final value = box.getAt(index); - expect(value, message); - }); - }); -} diff --git a/hive_flutter/test/crypto/hive_aes_native_gcm_cipher_test.dart b/hive_flutter/test/crypto/hive_aes_native_gcm_cipher_test.dart deleted file mode 100644 index 9a68e5a08..000000000 --- a/hive_flutter/test/crypto/hive_aes_native_gcm_cipher_test.dart +++ /dev/null @@ -1,37 +0,0 @@ -import 'dart:math'; - -import 'package:hive_flutter/adapters.dart'; -import 'package:test/test.dart'; - -import 'directory.dart'; - -void main() { - group('HiveAesNativeGcmCipher', () { - late HiveAesNativeGcmCipher cipher; - late Box box; - late List message; - - setUp(() async { - final random = Random.secure(); - cipher = HiveAesNativeGcmCipher( - List.generate(32, (index) => random.nextInt(255)), - ); - - var dir = await getTempDir(); - Hive.init(dir.path); - - box = await Hive.openBox>(boxName, encryptionCipher: cipher); - - message = List.generate(64, (index) => random.nextInt(255)); - }); - test('encrypt()', () async { - final index = await box.add(message); - - await box.close(); - - box = await Hive.openBox>(boxName, encryptionCipher: cipher); - final value = box.getAt(index); - expect(value, message); - }); - }); -} diff --git a/hive_flutter/test/mocks.dart b/hive_flutter/test/mocks.dart deleted file mode 100644 index 0eb720890..000000000 --- a/hive_flutter/test/mocks.dart +++ /dev/null @@ -1,13 +0,0 @@ -library hive_flutter.test.mocks; - -import 'package:hive/hive.dart'; -import 'package:mockito/annotations.dart'; - -export 'mocks.mocks.dart'; - -@GenerateMocks([], customMocks: [ - MockSpec(returnNullOnMissingStub: true), - MockSpec(returnNullOnMissingStub: true), -]) -// ignore: prefer_typing_uninitialized_variables, unused_element -var _mocks; diff --git a/hive_flutter/test/mocks.mocks.dart b/hive_flutter/test/mocks.mocks.dart deleted file mode 100644 index 55c1225ee..000000000 --- a/hive_flutter/test/mocks.mocks.dart +++ /dev/null @@ -1,211 +0,0 @@ -// Mocks generated by Mockito 5.0.10 from annotations -// in hive_flutter/test/mocks.dart. -// Do not manually edit this file. - -import 'dart:convert' as _i5; -import 'dart:typed_data' as _i4; - -import 'package:hive/hive.dart' as _i3; -import 'package:hive/src/object/hive_object.dart' as _i1; -import 'package:mockito/mockito.dart' as _i2; - -// ignore_for_file: avoid_redundant_argument_values -// ignore_for_file: comment_references -// ignore_for_file: invalid_use_of_visible_for_testing_member -// ignore_for_file: prefer_const_constructors -// ignore_for_file: unnecessary_parenthesis - -class _FakeHiveList extends _i2.Fake - implements _i3.HiveList {} - -/// A class which mocks [BinaryReader]. -/// -/// See the documentation for Mockito's code generation for more information. -class MockBinaryReader extends _i2.Mock implements _i3.BinaryReader { - @override - int get availableBytes => - (super.noSuchMethod(Invocation.getter(#availableBytes), returnValue: 0) - as int); - @override - int get usedBytes => - (super.noSuchMethod(Invocation.getter(#usedBytes), returnValue: 0) - as int); - @override - void skip(int? bytes) => super.noSuchMethod(Invocation.method(#skip, [bytes]), - returnValueForMissingStub: null); - @override - int readByte() => - (super.noSuchMethod(Invocation.method(#readByte, []), returnValue: 0) - as int); - @override - _i4.Uint8List viewBytes(int? bytes) => - (super.noSuchMethod(Invocation.method(#viewBytes, [bytes]), - returnValue: _i4.Uint8List(0)) as _i4.Uint8List); - @override - _i4.Uint8List peekBytes(int? bytes) => - (super.noSuchMethod(Invocation.method(#peekBytes, [bytes]), - returnValue: _i4.Uint8List(0)) as _i4.Uint8List); - @override - int readWord() => - (super.noSuchMethod(Invocation.method(#readWord, []), returnValue: 0) - as int); - @override - int readInt32() => - (super.noSuchMethod(Invocation.method(#readInt32, []), returnValue: 0) - as int); - @override - int readUint32() => - (super.noSuchMethod(Invocation.method(#readUint32, []), returnValue: 0) - as int); - @override - int readInt() => - (super.noSuchMethod(Invocation.method(#readInt, []), returnValue: 0) - as int); - @override - double readDouble() => - (super.noSuchMethod(Invocation.method(#readDouble, []), returnValue: 0.0) - as double); - @override - bool readBool() => - (super.noSuchMethod(Invocation.method(#readBool, []), returnValue: false) - as bool); - @override - String readString( - [int? byteCount, - _i5.Converter, String>? decoder = - const _i5.Utf8Decoder()]) => - (super.noSuchMethod(Invocation.method(#readString, [byteCount, decoder]), - returnValue: '') as String); - @override - _i4.Uint8List readByteList([int? length]) => - (super.noSuchMethod(Invocation.method(#readByteList, [length]), - returnValue: _i4.Uint8List(0)) as _i4.Uint8List); - @override - List readIntList([int? length]) => - (super.noSuchMethod(Invocation.method(#readIntList, [length]), - returnValue: []) as List); - @override - List readDoubleList([int? length]) => - (super.noSuchMethod(Invocation.method(#readDoubleList, [length]), - returnValue: []) as List); - @override - List readBoolList([int? length]) => - (super.noSuchMethod(Invocation.method(#readBoolList, [length]), - returnValue: []) as List); - @override - List readStringList( - [int? length, - _i5.Converter, String>? decoder = - const _i5.Utf8Decoder()]) => - (super.noSuchMethod(Invocation.method(#readStringList, [length, decoder]), - returnValue: []) as List); - @override - List readList([int? length]) => - (super.noSuchMethod(Invocation.method(#readList, [length]), - returnValue: []) as List); - @override - Map readMap([int? length]) => - (super.noSuchMethod(Invocation.method(#readMap, [length]), - returnValue: {}) as Map); - @override - _i3.HiveList<_i1.HiveObjectMixin> readHiveList([int? length]) => - (super.noSuchMethod(Invocation.method(#readHiveList, [length]), - returnValue: _FakeHiveList<_i1.HiveObjectMixin>()) - as _i3.HiveList<_i1.HiveObjectMixin>); -} - -/// A class which mocks [BinaryWriter]. -/// -/// See the documentation for Mockito's code generation for more information. -class MockBinaryWriter extends _i2.Mock implements _i3.BinaryWriter { - @override - void writeByte(int? byte) => - super.noSuchMethod(Invocation.method(#writeByte, [byte]), - returnValueForMissingStub: null); - @override - void writeWord(int? value) => - super.noSuchMethod(Invocation.method(#writeWord, [value]), - returnValueForMissingStub: null); - @override - void writeInt32(int? value) => - super.noSuchMethod(Invocation.method(#writeInt32, [value]), - returnValueForMissingStub: null); - @override - void writeUint32(int? value) => - super.noSuchMethod(Invocation.method(#writeUint32, [value]), - returnValueForMissingStub: null); - @override - void writeInt(int? value) => - super.noSuchMethod(Invocation.method(#writeInt, [value]), - returnValueForMissingStub: null); - @override - void writeDouble(double? value) => - super.noSuchMethod(Invocation.method(#writeDouble, [value]), - returnValueForMissingStub: null); - @override - void writeBool(bool? value) => - super.noSuchMethod(Invocation.method(#writeBool, [value]), - returnValueForMissingStub: null); - @override - void writeString(String? value, - {bool? writeByteCount = true, - _i5.Converter>? encoder = - const _i5.Utf8Encoder()}) => - super.noSuchMethod( - Invocation.method(#writeString, [value], - {#writeByteCount: writeByteCount, #encoder: encoder}), - returnValueForMissingStub: null); - @override - void writeByteList(List? bytes, {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method( - #writeByteList, [bytes], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void writeIntList(List? list, {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method(#writeIntList, [list], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void writeDoubleList(List? list, {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method( - #writeDoubleList, [list], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void writeBoolList(List? list, {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method( - #writeBoolList, [list], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void writeStringList(List? list, - {bool? writeLength = true, - _i5.Converter>? encoder = - const _i5.Utf8Encoder()}) => - super.noSuchMethod( - Invocation.method(#writeStringList, [list], - {#writeLength: writeLength, #encoder: encoder}), - returnValueForMissingStub: null); - @override - void writeList(List? list, {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method(#writeList, [list], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void writeMap(Map? map, {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method(#writeMap, [map], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void writeHiveList(_i3.HiveList<_i1.HiveObjectMixin>? list, - {bool? writeLength = true}) => - super.noSuchMethod( - Invocation.method( - #writeHiveList, [list], {#writeLength: writeLength}), - returnValueForMissingStub: null); - @override - void write(T? value, {bool? writeTypeId = true}) => super.noSuchMethod( - Invocation.method(#write, [value], {#writeTypeId: writeTypeId}), - returnValueForMissingStub: null); -} diff --git a/hive_generator/CHANGELOG.md b/hive_generator/CHANGELOG.md deleted file mode 100644 index 31da2108a..000000000 --- a/hive_generator/CHANGELOG.md +++ /dev/null @@ -1,119 +0,0 @@ -## 2.0.1 - -- Updated `analyzer` version constraints to `>=4.6.0 <7.0.0` -- Updates SDK version - -## 1.1.3 - -- Updated `analyzer` version constraints to `<5.0.0` - -## 1.1.2 - -- Updated `analyzer` version constraints - -## 1.1.1 - -- Updated dependency versions - -## 1.1.0 - -- Fixes empty class generates adapter with warnings - [#638](https://github.com/hivedb/hive/issues/638) -- Default value support for class type adapter generators - [#625](https://github.com/hivedb/hive/issues/625) - -## 1.0.1 - -- Fixed dependencies for null-safety compatibility - -## 1.0.0 - -- Stable null-safety version - -## 0.9.0-nullsafety.1 - -- Enum nullsafety support - [#555](https://github.com/hivedb/hive/issues/555) - -## 0.9.0-nullsafety.0 - -- Support generating null-safe code for libraries using Dart >= 2.12.0 -- Does not generate incompatible code for legacy libraries, so this is **NOT** a breaking change. - -## 0.8.2 - -- Inheritance support. Fixes [#442](https://github.com/hivedb/hive/issues/442) -- Support for both older and newer analyzer versions - -## 0.8.1 - -- Fixes [#455](https://github.com/hivedb/hive/issues/455) - -## 0.8.0 - -- `analyzer: ^0.40.0` version support - [#455](https://github.com/hivedb/hive/issues/455) - -## 0.7.2+1 - -- Fixed [#225] - -## 0.7.2 - -- Fixed [#225](https://github.com/hivedb/hive/issues/225) - -## 0.7.1 - -- Fixed dependency issues - -## 0.7.0+2 - -- Fixed Changelog - -## 0.7.0+2 - -- Fixed GitHub homepage path - -## 0.7.0 - -- Fixed final fields in constructor -- Support for `typeId` parameter - -## 0.6.0 - -- Support for HiveLists -- Support for getters & setters -- Support for inheritance - -## 0.5.2 - -- Fix bug with Uint8Lists - -## 0.5.1 - -- Bump Hive version - -## 0.5.0 - -- Support final members - -## 0.4.0+2 - -- Support for Lists and Maps in strong mode - -## 0.4.0+1 - -- Support for enums -- Sanity checks -- Bugfixes - -## 0.3.0 - -- Support for typed lists -- Bugfixes -- **Breaking:** Not compatible to previous adapters - -## 0.2.0 - -- Improve performance of adapers -- Cast lists to the correct type - -## 0.1.0+1 - -- First release diff --git a/hive_generator/LICENSE b/hive_generator/LICENSE deleted file mode 100644 index bd506236f..000000000 --- a/hive_generator/LICENSE +++ /dev/null @@ -1,13 +0,0 @@ -Copyright 2019 Simon Leier - -Licensed under the Apache License, Version 2.0 (the "License"); -you may not use this file except in compliance with the License. -You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - -Unless required by applicable law or agreed to in writing, software -distributed under the License is distributed on an "AS IS" BASIS, -WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -See the License for the specific language governing permissions and -limitations under the License. \ No newline at end of file diff --git a/hive_generator/README.md b/hive_generator/README.md deleted file mode 100644 index 7731e79e5..000000000 --- a/hive_generator/README.md +++ /dev/null @@ -1 +0,0 @@ -### Extension for [hive](https://github.com/leisim/hive) please go there for documentation. \ No newline at end of file diff --git a/hive_generator/analysis_options.yaml b/hive_generator/analysis_options.yaml deleted file mode 100644 index e9a15ec19..000000000 --- a/hive_generator/analysis_options.yaml +++ /dev/null @@ -1,80 +0,0 @@ -include: package:pedantic/analysis_options.yaml -analyzer: - exclude: - - "**/*.g.dart" - strong-mode: - implicit-casts: false - errors: - include_file_not_found: ignore -linter: - rules: - #- public_member_api_docs - - always_declare_return_types - - annotate_overrides - - avoid_empty_else - - avoid_function_literals_in_foreach_calls - - avoid_init_to_null - - avoid_null_checks_in_equality_operators - - avoid_relative_lib_imports - - avoid_renaming_method_parameters - - avoid_return_types_on_setters - - avoid_returning_null - - avoid_types_as_parameter_names - - avoid_unused_constructor_parameters - - await_only_futures - - camel_case_types - - cancel_subscriptions - - comment_references - - constant_identifier_names - - control_flow_in_finally - - directives_ordering - - empty_catches - - empty_constructor_bodies - - empty_statements - - implementation_imports - - invariant_booleans - - iterable_contains_unrelated_type - - library_names - - library_prefixes - - list_remove_unrelated_type - - lines_longer_than_80_chars - - no_adjacent_strings_in_list - - no_duplicate_case_values - - non_constant_identifier_names - - null_closures - - omit_local_variable_types - - only_throw_errors - - overridden_fields - - package_api_docs - - package_names - - package_prefixed_library_names - - prefer_adjacent_string_concatenation - - prefer_collection_literals - - prefer_conditional_assignment - - prefer_const_constructors - - prefer_contains - - prefer_equal_for_default_values - - prefer_final_fields - - prefer_initializing_formals - - prefer_interpolation_to_compose_strings - - prefer_is_empty - - prefer_is_not_empty - - prefer_single_quotes - - prefer_typing_uninitialized_variables - - recursive_getters - - slash_for_doc_comments - - test_types_in_equals - - throw_in_finally - - type_init_formals - - unawaited_futures - - unnecessary_brace_in_string_interps - - unnecessary_const - - unnecessary_getters_setters - - unnecessary_lambdas - - unnecessary_new - - unnecessary_null_aware_assignments - - unnecessary_statements - - unnecessary_this - - unrelated_type_equality_checks - - use_rethrow_when_possible - - valid_regexps \ No newline at end of file diff --git a/hive_generator/build.yaml b/hive_generator/build.yaml deleted file mode 100644 index 95ae54411..000000000 --- a/hive_generator/build.yaml +++ /dev/null @@ -1,8 +0,0 @@ -builders: - hive_generator: - import: "package:hive_generator/hive_generator.dart" - builder_factories: ["getBuilder"] - build_extensions: {".dart": ["hive_generator.g.part"]} - auto_apply: dependents - build_to: cache - applies_builders: ["source_gen|combining_builder"] \ No newline at end of file diff --git a/hive_generator/example/README.md b/hive_generator/example/README.md deleted file mode 100644 index 194ba01f0..000000000 --- a/hive_generator/example/README.md +++ /dev/null @@ -1,3 +0,0 @@ -### Hive samples - -Go [here](https://github.com/leisim/hive/tree/master/examples) for samples using Hive. \ No newline at end of file diff --git a/hive_generator/example/lib/types.dart b/hive_generator/example/lib/types.dart deleted file mode 100644 index b902d79ad..000000000 --- a/hive_generator/example/lib/types.dart +++ /dev/null @@ -1,63 +0,0 @@ -import 'package:hive/hive.dart'; - -part 'types.g.dart'; - -@HiveType(typeId: 1) -class Class1 { - const Class1(this.nested, [this.enum1]); - - @HiveField( - 0, - defaultValue: Class2( - 4, - 'param', - >>{ - 5: >{ - 'magic': [ - Class1(Class2(5, 'sad')), - Class1(Class2(5, 'sad'), Enum1.emumValue1), - ], - }, - 67: >{ - 'hold': [ - Class1(Class2(42, 'meaning of life')), - ], - }, - }, - ), - ) - final Class2 nested; - - final Enum1? enum1; -} - -@HiveType(typeId: 2) -class Class2 { - const Class2(this.param1, this.param2, [this.what]); - - @HiveField(0, defaultValue: 0) - final int param1; - - @HiveField(1) - final String param2; - - @HiveField(6) - final Map>>? what; -} - -@HiveType(typeId: 3) -enum Enum1 { - @HiveField(0) - emumValue1, - - @HiveField(1, defaultValue: true) - emumValue2, - - @HiveField(2) - emumValue3, -} - -@HiveType(typeId: 4) -class EmptyClass { - EmptyClass(); -} diff --git a/hive_generator/example/lib/types.g.dart b/hive_generator/example/lib/types.g.dart deleted file mode 100644 index 8cca919d8..000000000 --- a/hive_generator/example/lib/types.g.dart +++ /dev/null @@ -1,165 +0,0 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'types.dart'; - -// ************************************************************************** -// TypeAdapterGenerator -// ************************************************************************** - -class Enum1Adapter extends TypeAdapter { - @override - final int typeId = 3; - - @override - Enum1 read(BinaryReader reader) { - switch (reader.readByte()) { - case 0: - return Enum1.emumValue1; - case 1: - return Enum1.emumValue2; - case 2: - return Enum1.emumValue3; - default: - return Enum1.emumValue2; - } - } - - @override - void write(BinaryWriter writer, Enum1 obj) { - switch (obj) { - case Enum1.emumValue1: - writer.writeByte(0); - break; - case Enum1.emumValue2: - writer.writeByte(1); - break; - case Enum1.emumValue3: - writer.writeByte(2); - break; - } - } - - @override - int get hashCode => typeId.hashCode; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is Enum1Adapter && - runtimeType == other.runtimeType && - typeId == other.typeId; -} - -class Class1Adapter extends TypeAdapter { - @override - final int typeId = 1; - - @override - Class1 read(BinaryReader reader) { - final numOfFields = reader.readByte(); - final fields = { - for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), - }; - return Class1( - fields[0] == null - ? const Class2(4, 'param', { - 5: { - 'magic': [ - const Class1(const Class2(5, 'sad')), - const Class1(const Class2(5, 'sad'), Enum1.emumValue1) - ] - }, - 67: { - 'hold': [const Class1(const Class2(42, 'meaning of life'))] - } - }) - : fields[0] as Class2, - ); - } - - @override - void write(BinaryWriter writer, Class1 obj) { - writer - ..writeByte(1) - ..writeByte(0) - ..write(obj.nested); - } - - @override - int get hashCode => typeId.hashCode; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is Class1Adapter && - runtimeType == other.runtimeType && - typeId == other.typeId; -} - -class Class2Adapter extends TypeAdapter { - @override - final int typeId = 2; - - @override - Class2 read(BinaryReader reader) { - final numOfFields = reader.readByte(); - final fields = { - for (int i = 0; i < numOfFields; i++) reader.readByte(): reader.read(), - }; - return Class2( - fields[0] == null ? 0 : fields[0] as int, - fields[1] as String, - (fields[6] as Map?)?.map((dynamic k, dynamic v) => MapEntry( - k as int, - (v as Map).map((dynamic k, dynamic v) => - MapEntry(k as String, (v as List).cast())))), - ); - } - - @override - void write(BinaryWriter writer, Class2 obj) { - writer - ..writeByte(3) - ..writeByte(0) - ..write(obj.param1) - ..writeByte(1) - ..write(obj.param2) - ..writeByte(6) - ..write(obj.what); - } - - @override - int get hashCode => typeId.hashCode; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is Class2Adapter && - runtimeType == other.runtimeType && - typeId == other.typeId; -} - -class EmptyClassAdapter extends TypeAdapter { - @override - final int typeId = 4; - - @override - EmptyClass read(BinaryReader reader) { - return EmptyClass(); - } - - @override - void write(BinaryWriter writer, EmptyClass obj) { - writer..writeByte(0); - } - - @override - int get hashCode => typeId.hashCode; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is EmptyClassAdapter && - runtimeType == other.runtimeType && - typeId == other.typeId; -} diff --git a/hive_generator/example/pubspec.yaml b/hive_generator/example/pubspec.yaml deleted file mode 100644 index 3727b308a..000000000 --- a/hive_generator/example/pubspec.yaml +++ /dev/null @@ -1,14 +0,0 @@ -name: example -dependencies: - hive: any -dev_dependencies: - build_runner: any - hive_generator: - path: .. -environment: - sdk: '>=2.12.0-0 <3.0.0' -dependency_overrides: - hive: - path: ../../hive - hive_generator: - path: .. \ No newline at end of file diff --git a/hive_generator/lib/hive_generator.dart b/hive_generator/lib/hive_generator.dart deleted file mode 100644 index 23619fb50..000000000 --- a/hive_generator/lib/hive_generator.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:build/build.dart'; -import 'package:hive_generator/src/type_adapter_generator.dart'; -import 'package:source_gen/source_gen.dart'; - -Builder getBuilder(BuilderOptions options) => - SharedPartBuilder([TypeAdapterGenerator()], 'hive_generator'); diff --git a/hive_generator/lib/src/builder.dart b/hive_generator/lib/src/builder.dart deleted file mode 100644 index 9066977b6..000000000 --- a/hive_generator/lib/src/builder.dart +++ /dev/null @@ -1,25 +0,0 @@ -import 'package:analyzer/dart/constant/value.dart'; -import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/dart/element/type.dart'; - -class AdapterField { - final int index; - final String name; - final DartType type; - final DartObject? defaultValue; - - AdapterField(this.index, this.name, this.type, this.defaultValue); -} - -abstract class Builder { - final InterfaceElement interface; - final List getters; - final List setters; - - Builder(this.interface, this.getters, - [this.setters = const []]); - - String buildRead(); - - String buildWrite(); -} diff --git a/hive_generator/lib/src/class_builder.dart b/hive_generator/lib/src/class_builder.dart deleted file mode 100644 index 2975710c7..000000000 --- a/hive_generator/lib/src/class_builder.dart +++ /dev/null @@ -1,223 +0,0 @@ -import 'dart:typed_data'; - -import 'package:analyzer/dart/constant/value.dart'; -import 'package:analyzer/dart/element/element.dart'; -import 'package:analyzer/dart/element/nullability_suffix.dart'; -import 'package:analyzer/dart/element/type.dart'; -import 'package:hive/hive.dart'; -import 'package:hive_generator/src/builder.dart'; -import 'package:hive_generator/src/helper.dart'; -import 'package:source_gen/source_gen.dart'; - -import 'type_helper.dart'; - -class ClassBuilder extends Builder { - ClassBuilder( - InterfaceElement interface, - List getters, - List setters, - ) : super(interface, getters, setters); - - var hiveListChecker = const TypeChecker.fromRuntime(HiveList); - var listChecker = const TypeChecker.fromRuntime(List); - var mapChecker = const TypeChecker.fromRuntime(Map); - var setChecker = const TypeChecker.fromRuntime(Set); - var iterableChecker = const TypeChecker.fromRuntime(Iterable); - var uint8ListChecker = const TypeChecker.fromRuntime(Uint8List); - - @override - String buildRead() { - var constr = - interface.constructors.firstOrNullWhere((it) => it.name.isEmpty); - check(constr != null, 'Provide an unnamed constructor.'); - - // The remaining fields to initialize. - var fields = setters.toList(); - - // Empty classes - if (constr!.parameters.isEmpty && fields.isEmpty) { - return 'return ${interface.name}();'; - } - - var code = StringBuffer(); - code.writeln(''' - final numOfFields = reader.readByte(); - final fields = { - for (int i = 0; i < numOfFields; i++) - reader.readByte(): reader.read(), - }; - return ${interface.name}( - '''); - - for (var param in constr.parameters) { - var field = fields.firstOrNullWhere((it) => it.name == param.name); - // Final fields - field ??= getters.firstOrNullWhere((it) => it.name == param.name); - if (field != null) { - if (param.isNamed) { - code.write('${param.name}: '); - } - code.write(_value( - param.type, - 'fields[${field.index}]', - field.defaultValue, - )); - code.writeln(','); - fields.remove(field); - } - } - - code.writeln(')'); - - // There may still be fields to initialize that were not in the constructor - // as initializing formals. We do so using cascades. - for (var field in fields) { - code.write('..${field.name} = '); - code.writeln(_value( - field.type, - 'fields[${field.index}]', - field.defaultValue, - )); - } - - code.writeln(';'); - - return code.toString(); - } - - String _value(DartType type, String variable, DartObject? defaultValue) { - var value = _cast(type, variable); - if (defaultValue?.isNull != false) return value; - return '$variable == null ? ${constantToString(defaultValue!)} : $value'; - } - - String _cast(DartType type, String variable) { - var suffix = _suffixFromType(type); - if (hiveListChecker.isAssignableFromType(type)) { - return '($variable as HiveList$suffix)$suffix.castHiveList()'; - } else if (iterableChecker.isAssignableFromType(type) && - !isUint8List(type)) { - return '($variable as List$suffix)${_castIterable(type)}'; - } else if (mapChecker.isAssignableFromType(type)) { - return '($variable as Map$suffix)${_castMap(type)}'; - } else { - return '$variable as ${_displayString(type)}'; - } - } - - bool isMapOrIterable(DartType type) { - return iterableChecker.isAssignableFromType(type) || - mapChecker.isAssignableFromType(type); - } - - bool isUint8List(DartType type) { - return uint8ListChecker.isExactlyType(type); - } - - String _castIterable(DartType type) { - var paramType = type as ParameterizedType; - var arg = paramType.typeArguments.first; - var suffix = _accessorSuffixFromType(type); - if (isMapOrIterable(arg) && !isUint8List(arg)) { - var cast = ''; - // Using assignable because List? is not exactly List - if (listChecker.isAssignableFromType(type)) { - cast = '.toList()'; - // Using assignable because Set? is not exactly Set - } else if (setChecker.isAssignableFromType(type)) { - cast = '.toSet()'; - } - // The suffix is not needed with nnbd on $cast becauuse it short circuits, - // otherwise it is needed. - var castWithSuffix = isLibraryNNBD(interface) ? '$cast' : '$suffix$cast'; - return '$suffix.map((dynamic e)=> ${_cast(arg, 'e')})$castWithSuffix'; - } else { - return '$suffix.cast<${_displayString(arg)}>()'; - } - } - - String _castMap(DartType type) { - var paramType = type as ParameterizedType; - var arg1 = paramType.typeArguments[0]; - var arg2 = paramType.typeArguments[1]; - var suffix = _accessorSuffixFromType(type); - if (isMapOrIterable(arg1) || isMapOrIterable(arg2)) { - return '$suffix.map((dynamic k, dynamic v)=>' - 'MapEntry(${_cast(arg1, 'k')},${_cast(arg2, 'v')}))'; - } else { - return '$suffix.cast<${_displayString(arg1)}, ' - '${_displayString(arg2)}>()'; - } - } - - @override - String buildWrite() { - var code = StringBuffer(); - code.writeln('writer'); - code.writeln('..writeByte(${getters.length})'); - for (var field in getters) { - var value = _convertIterable(field.type, 'obj.${field.name}'); - code.writeln(''' - ..writeByte(${field.index}) - ..write($value)'''); - } - code.writeln(';'); - - return code.toString(); - } - - String _convertIterable(DartType type, String accessor) { - if (listChecker.isAssignableFromType(type)) { - return accessor; - } else - // Using assignable because Set? and Iterable? are not exactly Set and - // Iterable - if (setChecker.isAssignableFromType(type) || - iterableChecker.isAssignableFromType(type)) { - var suffix = _accessorSuffixFromType(type); - return '$accessor$suffix.toList()'; - } else { - return accessor; - } - } -} - -extension _FirstOrNullWhere on Iterable { - T? firstOrNullWhere(bool Function(T) predicate) { - for (var it in this) { - if (predicate(it)) { - return it; - } - } - return null; - } -} - -/// Suffix to use when accessing a field in [type]. -/// $variable$suffix.field -String _accessorSuffixFromType(DartType type) { - if (type.nullabilitySuffix == NullabilitySuffix.star) { - return '?'; - } - if (type.nullabilitySuffix == NullabilitySuffix.question) { - return '?'; - } - return ''; -} - -/// Suffix to use when casting a value to [type]. -/// $variable as $type$suffix -String _suffixFromType(DartType type) { - if (type.nullabilitySuffix == NullabilitySuffix.star) { - return ''; - } - if (type.nullabilitySuffix == NullabilitySuffix.question) { - return '?'; - } - return ''; -} - -String _displayString(DartType e) { - var suffix = _suffixFromType(e); - return '${e.getDisplayString(withNullability: false)}$suffix'; -} diff --git a/hive_generator/lib/src/enum_builder.dart b/hive_generator/lib/src/enum_builder.dart deleted file mode 100644 index c45bb2f07..000000000 --- a/hive_generator/lib/src/enum_builder.dart +++ /dev/null @@ -1,50 +0,0 @@ -import 'package:analyzer/dart/element/element.dart'; -import 'package:hive_generator/src/builder.dart'; -import 'package:hive_generator/src/helper.dart'; - -class EnumBuilder extends Builder { - EnumBuilder(InterfaceElement interface, List getters) - : super(interface, getters); - - @override - String buildRead() { - check( - getters.isNotEmpty, '${interface.name} does not have any enum value.'); - - var code = StringBuffer(); - code.writeln('switch (reader.readByte()) {'); - - for (var field in getters) { - code.writeln(''' - case ${field.index}: - return ${interface.name}.${field.name};'''); - } - - var defaultField = getters.firstWhere( - (it) => it.defaultValue?.toBoolValue() == true, - orElse: () => getters.first); - code.writeln(''' - default: - return ${interface.name}.${defaultField.name}; - }'''); - - return code.toString(); - } - - @override - String buildWrite() { - var code = StringBuffer(); - code.writeln('switch (obj) {'); - - for (var field in getters) { - code.writeln(''' - case ${interface.name}.${field.name}: - writer.writeByte(${field.index}); - break;'''); - } - - code.writeln('}'); - - return code.toString(); - } -} diff --git a/hive_generator/lib/src/helper.dart b/hive_generator/lib/src/helper.dart deleted file mode 100644 index 3fb569864..000000000 --- a/hive_generator/lib/src/helper.dart +++ /dev/null @@ -1,40 +0,0 @@ -import 'package:analyzer/dart/constant/value.dart'; -import 'package:analyzer/dart/element/element.dart'; -import 'package:hive/hive.dart'; -import 'package:source_gen/source_gen.dart'; - -final _hiveFieldChecker = const TypeChecker.fromRuntime(HiveField); - -class HiveFieldInfo { - HiveFieldInfo(this.index, this.defaultValue); - - final int index; - final DartObject? defaultValue; -} - -HiveFieldInfo? getHiveFieldAnn(Element element) { - var obj = _hiveFieldChecker.firstAnnotationOfExact(element); - if (obj == null) return null; - - return HiveFieldInfo( - obj.getField('index')!.toIntValue()!, - obj.getField('defaultValue'), - ); -} - -bool isLibraryNNBD(Element element) { - final dartVersion = element.library!.languageVersion.effective; - // Libraries with the dart version >= 2.12 are nnbd - if (dartVersion.major >= 2 && dartVersion.minor >= 12) { - return true; - } else { - return false; - } -} - -void check(bool condition, Object error) { - if (!condition) { - // ignore: only_throw_errors - throw error; - } -} diff --git a/hive_generator/lib/src/type_adapter_generator.dart b/hive_generator/lib/src/type_adapter_generator.dart deleted file mode 100644 index 1b21f716e..000000000 --- a/hive_generator/lib/src/type_adapter_generator.dart +++ /dev/null @@ -1,170 +0,0 @@ -import 'package:analyzer/dart/element/element.dart'; -import 'package:build/build.dart'; -import 'package:hive/hive.dart'; -import 'package:hive_generator/src/builder.dart'; -import 'package:hive_generator/src/class_builder.dart'; -import 'package:hive_generator/src/enum_builder.dart'; -import 'package:hive_generator/src/helper.dart'; -import 'package:source_gen/source_gen.dart'; - -class TypeAdapterGenerator extends GeneratorForAnnotation { - static String generateName(String typeName) { - var adapterName = - '${typeName}Adapter'.replaceAll(RegExp(r'[^A-Za-z0-9]+'), ''); - if (adapterName.startsWith('_')) { - adapterName = adapterName.substring(1); - } - if (adapterName.startsWith(r'$')) { - adapterName = adapterName.substring(1); - } - return adapterName; - } - - @override - Future generateForAnnotatedElement( - Element element, ConstantReader annotation, BuildStep buildStep) async { - var interface = getInterface(element); - var library = await buildStep.inputLibrary; - var gettersAndSetters = getAccessors(interface, library); - - var getters = gettersAndSetters[0]; - verifyFieldIndices(getters); - - var setters = gettersAndSetters[1]; - verifyFieldIndices(setters); - - var typeId = getTypeId(annotation); - - var adapterName = getAdapterName(interface.name, annotation); - var builder = interface is EnumElement - ? EnumBuilder(interface, getters) - : ClassBuilder(interface, getters, setters); - - return ''' - class $adapterName extends TypeAdapter<${interface.name}> { - @override - final int typeId = $typeId; - - @override - ${interface.name} read(BinaryReader reader) { - ${builder.buildRead()} - } - - @override - void write(BinaryWriter writer, ${interface.name} obj) { - ${builder.buildWrite()} - } - - @override - int get hashCode => typeId.hashCode; - - @override - bool operator ==(Object other) => - identical(this, other) || - other is $adapterName && - runtimeType == other.runtimeType && - typeId == other.typeId; - } - '''; - } - - InterfaceElement getInterface(Element element) { - check(element.kind == ElementKind.CLASS || element.kind == ElementKind.ENUM, - 'Only classes or enums are allowed to be annotated with @HiveType.'); - - return element as InterfaceElement; - } - - Set getAllAccessorNames(InterfaceElement interface) { - var accessorNames = {}; - - var supertypes = interface.allSupertypes.map((it) => it.element2); - for (var type in [interface, ...supertypes]) { - for (var accessor in type.accessors) { - if (accessor.isSetter) { - var name = accessor.name; - accessorNames.add(name.substring(0, name.length - 1)); - } else { - accessorNames.add(accessor.name); - } - } - } - - return accessorNames; - } - - List> getAccessors( - InterfaceElement interface, LibraryElement library) { - var accessorNames = getAllAccessorNames(interface); - - var getters = []; - var setters = []; - for (var name in accessorNames) { - var getter = interface.lookUpGetter(name, library); - if (getter != null) { - var getterAnn = - getHiveFieldAnn(getter.variable) ?? getHiveFieldAnn(getter); - if (getterAnn != null) { - var field = getter.variable; - getters.add(AdapterField( - getterAnn.index, - field.name, - field.type, - getterAnn.defaultValue, - )); - } - } - - var setter = interface.lookUpSetter('$name=', library); - if (setter != null) { - var setterAnn = - getHiveFieldAnn(setter.variable) ?? getHiveFieldAnn(setter); - if (setterAnn != null) { - var field = setter.variable; - setters.add(AdapterField( - setterAnn.index, - field.name, - field.type, - setterAnn.defaultValue, - )); - } - } - } - - return [getters, setters]; - } - - void verifyFieldIndices(List fields) { - for (var field in fields) { - check(field.index >= 0 && field.index <= 255, - 'Field numbers can only be in the range 0-255.'); - - for (var otherField in fields) { - if (otherField == field) continue; - if (otherField.index == field.index) { - throw HiveError( - 'Duplicate field number: ${field.index}. Fields "${field.name}" ' - 'and "${otherField.name}" have the same number.', - ); - } - } - } - } - - String getAdapterName(String typeName, ConstantReader annotation) { - var annAdapterName = annotation.read('adapterName'); - if (annAdapterName.isNull) { - return generateName(typeName); - } else { - return annAdapterName.stringValue; - } - } - - int getTypeId(ConstantReader annotation) { - check( - !annotation.read('typeId').isNull, - 'You have to provide a non-null typeId.', - ); - return annotation.read('typeId').intValue; - } -} diff --git a/hive_generator/lib/src/type_helper.dart b/hive_generator/lib/src/type_helper.dart deleted file mode 100644 index ba4cf1112..000000000 --- a/hive_generator/lib/src/type_helper.dart +++ /dev/null @@ -1,133 +0,0 @@ -import 'package:analyzer/dart/constant/value.dart'; -import 'package:analyzer/dart/element/type.dart'; -import 'package:source_gen/source_gen.dart'; -import 'package:source_helper/source_helper.dart'; - -const bool kConstConstructors = true; - -String constantToString( - DartObject? object, [ - List typeInformation = const [], -]) { - if (object == null || object.isNull) return 'null'; - final reader = ConstantReader(object); - return reader.isLiteral - ? literalToString(object, typeInformation) - : revivableToString(object, typeInformation); -} - -String revivableToString(DartObject? object, List typeInformation) { - final reader = ConstantReader(object); - final revivable = reader.revive(); - - if (revivable.source.fragment.isEmpty) { - // Enums - return revivable.accessor; - } else { - // Classes - final nextTypeInformation = [...typeInformation, '$object']; - final prefix = kConstConstructors ? 'const ' : ''; - final ctor = revivable.accessor.isEmpty ? '' : '.${revivable.accessor}'; - final arguments = [ - for (var arg in revivable.positionalArguments) - constantToString(arg, nextTypeInformation), - for (var kv in revivable.namedArguments.entries) - '${kv.key}: ${constantToString(kv.value, nextTypeInformation)}' - ]; - - return '$prefix${revivable.source.fragment}$ctor(${arguments.join(', ')})'; - } -} - -// The code below is based on code from https://github.com/google/json_serializable.dart/blob/df60c2a95c4c0054d6ab785849937d7f5ade39fe/json_serializable/lib/src/json_key_utils.dart#L43 - -String literalToString(DartObject object, List typeInformation) { - final reader = ConstantReader(object); - - String? badType; - if (reader.isSymbol) { - badType = 'Symbol'; - } else if (reader.isType) { - badType = 'Type'; - } else if (object.type is FunctionType) { - badType = 'Function'; - } else if (!reader.isLiteral) { - badType = object.type!.element2!.name; - } - - if (badType != null) { - badType = typeInformation.followedBy([badType]).join(' > '); - throwUnsupported('`defaultValue` is `$badType`, it must be a literal.'); - } - - if (reader.isDouble || reader.isInt || reader.isString || reader.isBool) { - final value = reader.literalValue; - - if (value is String) return escapeDartString(value); - - if (value is double) { - if (value.isNaN) { - return 'double.nan'; - } - - if (value.isInfinite) { - if (value.isNegative) { - return 'double.negativeInfinity'; - } - return 'double.infinity'; - } - } - - if (value is bool || value is num) return value.toString(); - } - - if (reader.isList) { - final listTypeInformation = [...typeInformation, 'List']; - final listItems = reader.listValue - .map((it) => constantToString(it, listTypeInformation)) - .join(', '); - return '[$listItems]'; - } - - if (reader.isSet) { - final setTypeInformation = [...typeInformation, 'Set']; - final setItems = reader.setValue - .map((it) => constantToString(it, setTypeInformation)) - .join(', '); - return '{$setItems}'; - } - - if (reader.isMap) { - final mapTypeInformation = [...typeInformation, 'Map']; - final buffer = StringBuffer('{'); - - var first = true; - - reader.mapValue.forEach((key, value) { - if (first) { - first = false; - } else { - buffer.writeln(','); - } - - buffer - ..write(constantToString(key, mapTypeInformation)) - ..write(': ') - ..write(constantToString(value, mapTypeInformation)); - }); - - buffer.write('}'); - - return buffer.toString(); - } - - badType = typeInformation.followedBy(['$object']).join(' > '); - throwUnsupported( - 'The provided value is not supported: $badType. ' - 'This may be an error in package:hive_generator. ' - 'Please rerun your build with `--verbose` and file an issue.', - ); -} - -Never throwUnsupported(String message) => - throw InvalidGenerationSourceError('Error with `@HiveField`. $message'); diff --git a/hive_generator/pubspec.yaml b/hive_generator/pubspec.yaml deleted file mode 100644 index a60873fc9..000000000 --- a/hive_generator/pubspec.yaml +++ /dev/null @@ -1,21 +0,0 @@ -name: hive_generator -description: Extension for Hive. Automatically generates TypeAdapters to store any class. -version: 2.0.1 -homepage: https://github.com/hivedb/hive/tree/master/hive_generator -documentation: https://docs.hivedb.dev/ - -environment: - sdk: ">=2.12.0 <4.0.0" - -dependencies: - build: ^2.0.0 - source_gen: ^1.0.0 - hive: ^2.0.4 - analyzer: ">=4.6.0 <7.0.0" - source_helper: ^1.1.0 - -dev_dependencies: - test: ^1.17.11 - build_test: any - build_runner: any - pedantic: ^1.11.1 diff --git a/hive_generator/test/type_adapter_generator_test.dart b/hive_generator/test/type_adapter_generator_test.dart deleted file mode 100644 index 13f59c294..000000000 --- a/hive_generator/test/type_adapter_generator_test.dart +++ /dev/null @@ -1,12 +0,0 @@ -import 'package:hive_generator/src/type_adapter_generator.dart'; -import 'package:test/test.dart'; - -void main() { - group('generateName', () { - test('.generateName()', () { - expect(TypeAdapterGenerator.generateName(r'_$User'), 'UserAdapter'); - expect(TypeAdapterGenerator.generateName(r'_$_SomeClass'), - 'SomeClassAdapter'); - }); - }); -} diff --git a/lcov_isar_test.info b/lcov_isar_test.info new file mode 100644 index 000000000..753e901ab --- /dev/null +++ b/lcov_isar_test.info @@ -0,0 +1,245 @@ +SF:lib/src/impl/type_registry.dart +DA:15,0 +DA:16,0 +DA:17,0 +DA:20,0 +DA:21,0 +DA:22,0 +DA:25,1 +DA:28,0 +DA:30,0 +DA:34,0 +DA:35,0 +DA:37,0 +DA:38,0 +DA:42,1 +DA:45,0 +DA:46,0 +DA:51,1 +DA:52,3 +DA:54,1 +DA:57,0 +DA:58,0 +DA:59,0 +DA:60,0 +DA:61,0 +DA:68,0 +DA:69,0 +DA:70,0 +DA:71,0 +DA:75,0 +DA:76,0 +DA:80,0 +DA:82,1 +DA:90,0 +DA:91,0 +LF:34 +LH:6 +end_of_record +SF:lib/src/impl/box_impl.dart +DA:4,2 +DA:11,1 +DA:12,2 +DA:14,1 +DA:15,2 +DA:17,1 +DA:18,2 +DA:20,1 +DA:21,2 +DA:23,0 +DA:24,0 +DA:26,0 +DA:27,0 +DA:29,0 +DA:31,0 +DA:32,0 +DA:33,0 +DA:35,0 +DA:37,0 +DA:41,0 +DA:45,1 +DA:47,1 +DA:48,3 +DA:49,1 +DA:51,1 +DA:53,1 +DA:56,0 +DA:57,0 +DA:59,0 +DA:63,1 +DA:65,6 +DA:68,1 +DA:70,3 +DA:73,2 +DA:76,1 +DA:79,1 +DA:80,4 +DA:83,1 +DA:85,4 +DA:88,1 +DA:90,4 +DA:91,1 +DA:94,1 +DA:96,3 +DA:99,2 +DA:102,1 +DA:105,1 +DA:107,1 +DA:108,1 +DA:109,1 +DA:110,1 +DA:112,1 +DA:116,1 +DA:118,2 +DA:120,1 +DA:121,1 +DA:123,1 +DA:125,2 +DA:127,1 +DA:129,3 +DA:132,1 +DA:134,2 +DA:135,1 +DA:138,1 +DA:139,1 +DA:140,2 +DA:141,2 +DA:142,1 +DA:144,3 +DA:145,2 +DA:151,1 +DA:153,1 +DA:154,1 +DA:155,1 +DA:157,2 +DA:159,1 +DA:161,2 +DA:163,1 +DA:164,2 +DA:165,1 +DA:170,1 +DA:172,2 +DA:173,1 +DA:174,2 +DA:175,2 +DA:179,2 +DA:183,1 +DA:185,2 +DA:187,4 +DA:189,2 +DA:192,1 +DA:194,2 +DA:197,2 +DA:201,1 +DA:203,1 +DA:204,1 +DA:205,1 +DA:206,1 +DA:208,1 +DA:212,1 +DA:214,2 +DA:215,1 +DA:217,1 +DA:218,2 +DA:219,1 +DA:220,2 +DA:221,3 +DA:222,1 +DA:223,1 +DA:225,1 +DA:227,2 +DA:231,1 +DA:233,2 +DA:234,2 +DA:238,1 +DA:239,1 +DA:240,1 +DA:241,2 +DA:243,3 +DA:244,2 +DA:245,0 +DA:252,1 +DA:253,2 +DA:254,1 +DA:255,2 +DA:256,2 +DA:259,1 +DA:261,2 +DA:265,1 +DA:267,2 +DA:268,1 +DA:269,2 +DA:270,2 +DA:274,2 +DA:278,1 +DA:280,2 +DA:281,1 +DA:282,2 +DA:283,1 +DA:284,2 +DA:285,2 +DA:288,1 +DA:290,2 +DA:294,1 +DA:296,2 +DA:297,4 +DA:301,1 +DA:303,2 +DA:304,3 +DA:306,2 +DA:311,1 +DA:313,2 +DA:314,1 +DA:315,1 +DA:316,1 +DA:318,3 +DA:319,1 +DA:323,1 +DA:325,2 +DA:326,2 +DA:331,4 +DA:333,2 +DA:334,2 +DA:339,0 +DA:341,0 +DA:342,0 +DA:346,0 +DA:348,0 +DA:349,0 +DA:352,1 +DA:354,3 +DA:355,2 +DA:358,0 +DA:360,0 +LF:174 +LH:151 +end_of_record +SF:lib/src/hive.dart +DA:5,3 +DA:6,3 +DA:38,0 +DA:42,0 +DA:56,1 +DA:62,2 +DA:64,0 +DA:67,0 +DA:68,0 +DA:74,0 +DA:80,1 +DA:82,1 +DA:88,1 +DA:89,2 +DA:94,0 +DA:95,0 +DA:96,0 +DA:103,0 +DA:104,0 +DA:105,0 +LF:20 +LH:8 +end_of_record +SF:lib/src/impl/frame.dart +DA:9,1 +LF:1 +LH:1 +end_of_record diff --git a/lib/hive.dart b/lib/hive.dart new file mode 100644 index 000000000..44b345d05 --- /dev/null +++ b/lib/hive.dart @@ -0,0 +1,11 @@ +/// Hive is a lightweight and blazing fast key-value store written in pure Dart. +/// It is strongly encrypted using AES-256. +library hive; + +import 'package:hive/src/impl/frame.dart'; +import 'package:isar/isar.dart'; + +part 'src/impl/box_impl.dart'; +part 'src/impl/type_registry.dart'; +part 'src/box.dart'; +part 'src/hive.dart'; diff --git a/lib/src/box.dart b/lib/src/box.dart new file mode 100644 index 000000000..90c63e81c --- /dev/null +++ b/lib/src/box.dart @@ -0,0 +1,134 @@ +part of hive; + +/// A box contains and manages a collection of key-value pairs. +abstract interface class Box { + /// Whether this box is currently open. + /// + /// Most of the operations on a box require it to be open. + bool get isOpen; + + /// The name of the box. + String get name; + + /// The location of the box in the file system. In the browser, this is null. + String? get directory; + + /// The number of entries in the box. + int get length; + + /// Whether the box is empty. + bool get isEmpty; + + /// Whether the box is not empty. + bool get isNotEmpty; + + /// Start a read transaction on the box. + /// + /// Transactions provides and atomic view on the box. All read operations + /// inside the transaction will see the same state of the box. + T read(T Function() callback); + + /// Start a write transaction on the box. + /// + /// Transactions provides and atomic view on the box. All read operations + /// inside the transaction will see the same state of the box. + T write(T Function() callback); + + /// All the keys in the box. + /// + /// The keys are sorted by their insertion order. + List get keys; + + /// Get the n-th key in the box. + String? keyAt(int index); + + /// Checks whether the box contains the [key]. + bool containsKey(String key); + + // Returns the value associated with the given [key]. If the key does not + /// exist, `null` is returned. + /// + /// If [defaultValue] is specified, it is returned in case the key does not + /// exist. + E? get(String key, {E? defaultValue}); + + /// Returns the value associated with the n-th key. + E getAt(int index); + + /// Returns the value associated with the given [key]. The key can either be + /// a [String] or an [int] to get an entry by its index. + E? operator [](Object key); + + /// Returns all values associated with the given [keys] or `null` if a key + /// does not exist. + List getAll(Iterable keys); + + /// Returns all values in the given range. + /// + /// Throws a [RangeError] if [start] or [end] are out of bounds. + List getRange(int start, int end); + + /// Returns all values between [startKey] and [endKey] (inclusive). + List getBetween({String? startKey, String? endKey}); + + /// Saves the [key] - [value] pair. + void put(String key, E value); + + /// Associates the [value] with the n-th key. An exception is raised if the + /// key does not exist. + void putAt(int index, E value); + + /// Saves the [key] - [value] pair. The key can either be a [String] or an + /// [int] to save an entry by its index. + void operator []=(Object key, E value); + + /// Saves all the key - value pairs in the [entries] map. + void putAll(Map entries); + + /// Overwrites the values in the given range with the given [values]. + void putRange(int start, int end, Iterable values); + + /// Saves the [value] with an auto-increment key. + void add(E value); + + /// Saves all the [values] with auto-increment keys. + void addAll(Iterable values); + + /// Deletes the given [key] from the box. + /// + /// If it does not exist, nothing happens. + bool delete(String key); + + /// Deletes the n-th key from the box. + /// + /// If it does not exist, nothing happens. + void deleteAt(int index); + + /// Deletes all the given [keys] from the box. + /// + /// If a key does not exist, it is skipped. + int deleteAll(Iterable keys); + + /// Deletes all the entries in the given range. + void deleteRange(int start, int end); + + /// Removes all entries from the box. + void clear({bool notify = true}); + + /// Closes the box. + /// + /// Be careful, this closes all instances of this box. You have to make sure + /// that you don't access the box anywhere else after that. + void close(); + + /// Removes the file which contains the box and closes the box. + /// + /// If a box is still open in another isolate, it will not be deleted. + void deleteFromDisk(); + + /// Returns a broadcast stream of change events. + /// + /// If the [key] parameter is provided, only events for the specified key are + /// broadcasted. + Stream<(dynamic, E?)> watch({dynamic key}); +} diff --git a/lib/src/hive.dart b/lib/src/hive.dart new file mode 100644 index 000000000..c6b9267ea --- /dev/null +++ b/lib/src/hive.dart @@ -0,0 +1,108 @@ +part of hive; + +/// Open boxes and register adapters. +class Hive { + static final _typeRegistry = _TypeRegistry(); + static final _openBoxes = >{}; + + /// The default name if you don't specify a name for a box. + static const defaultName = 'hive'; + + /// The default directory for all boxes. + static String? defaultDirectory; + + /// Registers a type adapter to allow Hive to (de)serialize your objects. + /// + /// Example: + /// ```dart + /// class Person { + /// String name; + /// int age; + /// + /// factory Person.fromJson(Map json) { + /// return Person() + /// ..name = json['name'] as String + /// ..age = json['age'] as int; + /// } + /// + /// Map toJson() { + /// return { + /// 'name': name, + /// 'age': age, + /// }; + /// } + /// } + /// + /// Hive.registerAdapter('Person', Person.fromJson); + /// ``` + static void registerAdapter( + String typeName, + T? Function(dynamic json) fromJson, + ) { + _typeRegistry.register(Isar.fastHash(typeName), fromJson); + } + + /// Get or open the box with [name] in the given [directory]. If no directory + /// is specified, the default directory is used. + /// + /// If the box is already open, the same instance is returned. + /// + /// The [encryptionKey] is used to encrypt the box. If the box was already + /// opened with a different encryption key, an error is thrown. + /// + /// The [maxSizeMiB] is the maximum size of the box in MiB. If the box grows + /// bigger than this, an exception is thrown. It is recommended to set this + /// value to a small value if possible. + static Box box({ + String name = defaultName, + String? directory, + String? encryptionKey, + int maxSizeMiB = 5, + }) { + final box = _openBoxes[name]; + if (box != null) { + if (box is Box) { + return box; + } else { + throw ArgumentError('Box was already opened with a different type. ' + 'Expected Box<${box.runtimeType}> but got Box<$E>.'); + } + } + + final dir = directory ?? defaultDirectory; + if (dir == null) { + throw ArgumentError( + 'No directory specified and no default directory set.', + 'directory', + ); + } + + final isar = Isar.open( + name: name, + schemas: [FrameSchema], + directory: dir, + engine: encryptionKey != null ? IsarEngine.sqlite : IsarEngine.isar, + maxSizeMiB: maxSizeMiB, + encryptionKey: encryptionKey, + ); + final newBox = _BoxImpl(isar); + _openBoxes[name] = newBox; + return newBox; + } + + /// Closes all open boxes. + static void closeAllBoxes() { + for (final box in _openBoxes.values) { + box.close(); + } + } + + /// Closes all open boxes and delete their data. + /// + /// If a box is still open in another isolate, it will not be deleted. + static void deleteAllBoxesFromDisk() { + for (final box in _openBoxes.values) { + box.deleteFromDisk(); + } + } +} diff --git a/lib/src/impl/box_impl.dart b/lib/src/impl/box_impl.dart new file mode 100644 index 000000000..2f2d15651 --- /dev/null +++ b/lib/src/impl/box_impl.dart @@ -0,0 +1,362 @@ +part of hive; + +class _BoxImpl implements Box { + _BoxImpl(this.isar) : collection = isar.frames; + + final Isar isar; + final IsarCollection collection; + + bool? writeTxn; + + @override + bool get isOpen => isar.isOpen; + + @override + String get name => isar.name; + + @override + String get directory => isar.directory; + + @override + int get length => collection.count(); + + @override + bool get isEmpty => length == 0; + + @override + bool get isNotEmpty => length != 0; + + @override + T read(T Function() callback) { + if (writeTxn == null) { + return isar.read((isar) { + writeTxn = false; + try { + return callback(); + } finally { + writeTxn = null; + } + }); + } else { + return callback(); + } + } + + @override + T write(T Function() callback) { + if (writeTxn == null) { + return isar.write((isar) { + writeTxn = true; + try { + return callback(); + } finally { + writeTxn = null; + } + }); + } else if (writeTxn!) { + return callback(); + } else { + throw StateError('Cannot write inside a read transaction.'); + } + } + + @override + List get keys { + return collection.where().keyIsNotNull().keyProperty().findAll().cast(); + } + + @override + String? keyAt(int index) { + final frame = collection.where().findFirst(offset: index); + + if (frame == null) { + throw IndexError.withLength(index, length); + } + + return frame.key; + } + + E _frameFromJson(Frame frame) { + return Hive._typeRegistry.fromJson(frame.typeId, frame.value); + } + + @override + bool containsKey(String key) { + return !collection.where().keyEqualTo(key).isEmpty(); + } + + @override + E? get(String key, {E? defaultValue}) { + final frame = collection.where().keyEqualTo(key).findFirst(); + return frame != null ? _frameFromJson(frame) : defaultValue; + } + + @override + E getAt(int index) { + final frame = collection.where().findFirst(offset: index); + + if (frame == null) { + throw IndexError.withLength(index, length); + } + + return _frameFromJson(frame); + } + + @override + E? operator [](dynamic key) { + if (key is int) { + return getAt(key); + } else if (key is String) { + return get(key); + } else { + throw ArgumentError.value(key, 'key', 'must be a String or int'); + } + } + + @override + List getAll(Iterable keys) { + if (keys.isEmpty) return []; + + final frames = collection + .where() + // ignore: inference_failure_on_function_invocation + .anyOf( + keys, + (q, key) => q.keyEqualTo(key), + ) + .findAll(); + + return frames.map(_frameFromJson).toList(); + } + + @override + List getRange(int start, int end) { + if (start == 0 && end == 0) { + return []; + } + + final frames = collection + .where() + .findAll(offset: start, limit: end - start) + .map(_frameFromJson) + .toList(); + + if (frames.length != end - start) { + RangeError.checkValidRange(start, end, length); + } + + return frames; + } + + @override + List getBetween({String? startKey, String? endKey}) { + final frames = collection + .where() + .optional( + endKey != null, + (q) => q.keyBetween(startKey ?? '', endKey), + ) + .optional( + endKey == null, + (q) => q.keyGreaterThanOrEqualTo(startKey ?? ''), + ) + .findAll() + .map(_frameFromJson) + .toList(); + + return frames; + } + + @override + void put(String key, E value) { + write(() { + final frame = Frame( + id: collection.autoIncrement(), + typeId: Hive._typeRegistry.findTypeId(value), + key: key, + value: value, + ); + collection.put(frame); + }); + } + + @override + void putAt(int index, E value) { + write(() { + final idAtIndex = + collection.where().idProperty().findFirst(offset: index); + if (idAtIndex == null) { + throw IndexError.withLength(index, length); + } + + final frame = Frame( + id: idAtIndex, + typeId: Hive._typeRegistry.findTypeId(value), + value: value, + ); + collection.put(frame); + }); + } + + @override + void operator []=(Object key, E value) { + if (key is int) { + putAt(key, value); + } else if (key is String) { + put(key, value); + } else { + throw ArgumentError.value(key, 'key', 'must be a String or int'); + } + } + + @override + void putAll(Map entries) { + write(() { + if (entries.isEmpty) return; + + final frames = []; + for (final entry in entries.entries) { + final frame = Frame( + id: collection.autoIncrement(), + typeId: Hive._typeRegistry.findTypeId(entry.value), + key: entry.key, + value: entry.value, + ); + frames.add(frame); + } + collection.putAll(frames); + }); + } + + @override + void putRange(int start, int end, Iterable values) { + write(() { + if (start == 0 && end == 0) { + return; + } + + final idsInRange = collection + .where() + .idProperty() + .findAll(offset: start, limit: end - start); + + if (idsInRange.length != end - start) { + RangeError.checkValidRange(start, end, length); + throw ArgumentError.value( + values, + 'values', + 'must have the same length as the range', + ); + } + + final frames = []; + for (final value in values) { + final frame = Frame( + id: idsInRange[frames.length], + typeId: Hive._typeRegistry.findTypeId(value), + value: value, + ); + frames.add(frame); + } + collection.putAll(frames); + }); + } + + @override + void add(E value, {String? key}) { + write(() { + final frame = Frame( + id: collection.autoIncrement(), + typeId: Hive._typeRegistry.findTypeId(value), + key: key, + value: value, + ); + collection.put(frame); + }); + } + + @override + void addAll(Iterable values) { + write(() { + final frames = []; + for (final value in values) { + final frame = Frame( + id: collection.autoIncrement(), + typeId: Hive._typeRegistry.findTypeId(value), + value: value, + ); + frames.add(frame); + } + collection.putAll(frames); + }); + } + + @override + bool delete(String key) { + return write(() { + return collection.where().keyEqualTo(key).deleteFirst(); + }); + } + + @override + void deleteAt(int index) { + return write(() { + final deleted = collection.where().deleteFirst(offset: index); + if (!deleted) { + throw IndexError.withLength(index, length); + } + }); + } + + @override + int deleteAll(Iterable keys) { + return write(() { + if (keys.isEmpty) return 0; + return collection + .where() + // ignore: inference_failure_on_function_invocation + .anyOf(keys, (q, key) => q.keyEqualTo(key)) + .deleteAll(); + }); + } + + @override + void deleteRange(int start, int end) { + return write(() { + if (start == 0 && end == 0) { + return; + } + + final deleted = + collection.where().deleteAll(offset: start, limit: end - start); + + if (deleted != end - start) { + RangeError.checkValidRange(start, end, length); + } + }); + } + + @override + void clear({bool notify = true}) { + isar.write((isar) { + collection.clear(); + }); + } + + @override + void close() { + Hive._openBoxes.remove(name); + isar.close(); + } + + @override + void deleteFromDisk() { + Hive._openBoxes.remove(name); + isar.close(deleteFromDisk: true); + } + + @override + Stream<(Object, E?)> watch({Object? key}) { + throw UnimplementedError(); + } +} diff --git a/lib/src/impl/frame.dart b/lib/src/impl/frame.dart new file mode 100644 index 000000000..084ba1f48 --- /dev/null +++ b/lib/src/impl/frame.dart @@ -0,0 +1,28 @@ +import 'package:isar/isar.dart'; + +part 'frame.g.dart'; + +/// @nodoc +@collection +class Frame { + /// @nodoc + const Frame({ + required this.id, + required this.typeId, + this.key, + required this.value, + }); + + /// @nodoc + final int id; + + /// @nodoc + final int? typeId; + + /// @nodoc + @Index(unique: true, hash: true) + final String? key; + + /// @nodoc + final dynamic value; +} diff --git a/lib/src/impl/frame.g.dart b/lib/src/impl/frame.g.dart new file mode 100644 index 000000000..8a679165e --- /dev/null +++ b/lib/src/impl/frame.g.dart @@ -0,0 +1,808 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'frame.dart'; + +// ************************************************************************** +// _IsarCollectionGenerator +// ************************************************************************** + +// coverage:ignore-file +// ignore_for_file: duplicate_ignore, invalid_use_of_protected_member, lines_longer_than_80_chars, constant_identifier_names, avoid_js_rounded_ints, no_leading_underscores_for_local_identifiers, require_trailing_commas, unnecessary_parenthesis, unnecessary_raw_strings, unnecessary_null_in_if_null_operators, library_private_types_in_public_api, prefer_const_constructors +// ignore_for_file: type=lint + +extension GetFrameCollection on Isar { + IsarCollection get frames => this.collection(); +} + +const FrameSchema = IsarGeneratedSchema( + schema: IsarSchema( + name: 'Frame', + idName: 'id', + embedded: false, + properties: [ + IsarPropertySchema( + name: 'typeId', + type: IsarType.long, + ), + IsarPropertySchema( + name: 'key', + type: IsarType.string, + ), + IsarPropertySchema( + name: 'value', + type: IsarType.json, + ), + ], + indexes: [ + IsarIndexSchema( + name: 'key', + properties: [ + "key", + ], + unique: true, + hash: true, + ), + ], + ), + converter: IsarObjectConverter( + serialize: serializeFrame, + deserialize: deserializeFrame, + deserializeProperty: deserializeFrameProp, + ), + embeddedSchemas: [], +); + +@isarProtected +int serializeFrame(IsarWriter writer, Frame object) { + IsarCore.writeLong(writer, 1, object.typeId ?? -9223372036854775808); + { + final value = object.key; + if (value == null) { + IsarCore.writeNull(writer, 2); + } else { + IsarCore.writeString(writer, 2, value); + } + } + IsarCore.writeString(writer, 3, isarJsonEncode(object.value)); + return object.id; +} + +@isarProtected +Frame deserializeFrame(IsarReader reader) { + final int _id; + _id = IsarCore.readId(reader); + final int? _typeId; + { + final value = IsarCore.readLong(reader, 1); + if (value == -9223372036854775808) { + _typeId = null; + } else { + _typeId = value; + } + } + final String? _key; + _key = IsarCore.readString(reader, 2); + final dynamic _value; + _value = isarJsonDecode(IsarCore.readString(reader, 3) ?? 'null') ?? null; + final object = Frame( + id: _id, + typeId: _typeId, + key: _key, + value: _value, + ); + return object; +} + +@isarProtected +dynamic deserializeFrameProp(IsarReader reader, int property) { + switch (property) { + case 0: + return IsarCore.readId(reader); + case 1: + { + final value = IsarCore.readLong(reader, 1); + if (value == -9223372036854775808) { + return null; + } else { + return value; + } + } + case 2: + return IsarCore.readString(reader, 2); + case 3: + return isarJsonDecode(IsarCore.readString(reader, 3) ?? 'null') ?? null; + default: + throw ArgumentError('Unknown property: $property'); + } +} + +sealed class _FrameUpdate { + bool call({ + required int id, + int? typeId, + String? key, + }); +} + +class _FrameUpdateImpl implements _FrameUpdate { + const _FrameUpdateImpl(this.collection); + + final IsarCollection collection; + + @override + bool call({ + required int id, + Object? typeId = ignore, + Object? key = ignore, + }) { + return collection.updateProperties([ + id + ], { + if (typeId != ignore) 1: typeId as int?, + if (key != ignore) 2: key as String?, + }) > + 0; + } +} + +sealed class _FrameUpdateAll { + int call({ + required List id, + int? typeId, + String? key, + }); +} + +class _FrameUpdateAllImpl implements _FrameUpdateAll { + const _FrameUpdateAllImpl(this.collection); + + final IsarCollection collection; + + @override + int call({ + required List id, + Object? typeId = ignore, + Object? key = ignore, + }) { + return collection.updateProperties(id, { + if (typeId != ignore) 1: typeId as int?, + if (key != ignore) 2: key as String?, + }); + } +} + +extension FrameUpdate on IsarCollection { + _FrameUpdate get update => _FrameUpdateImpl(this); + + _FrameUpdateAll get updateAll => _FrameUpdateAllImpl(this); +} + +sealed class _FrameQueryUpdate { + int call({ + int? typeId, + String? key, + }); +} + +class _FrameQueryUpdateImpl implements _FrameQueryUpdate { + const _FrameQueryUpdateImpl(this.query, {this.limit}); + + final IsarQuery query; + final int? limit; + + @override + int call({ + Object? typeId = ignore, + Object? key = ignore, + }) { + return query.updateProperties(limit: limit, { + if (typeId != ignore) 1: typeId as int?, + if (key != ignore) 2: key as String?, + }); + } +} + +extension FrameQueryUpdate on IsarQuery { + _FrameQueryUpdate get updateFirst => _FrameQueryUpdateImpl(this, limit: 1); + + _FrameQueryUpdate get updateAll => _FrameQueryUpdateImpl(this); +} + +class _FrameQueryBuilderUpdateImpl implements _FrameQueryUpdate { + const _FrameQueryBuilderUpdateImpl(this.query, {this.limit}); + + final QueryBuilder query; + final int? limit; + + @override + int call({ + Object? typeId = ignore, + Object? key = ignore, + }) { + final q = query.build(); + try { + return q.updateProperties(limit: limit, { + if (typeId != ignore) 1: typeId as int?, + if (key != ignore) 2: key as String?, + }); + } finally { + q.close(); + } + } +} + +extension FrameQueryBuilderUpdate on QueryBuilder { + _FrameQueryUpdate get updateFirst => + _FrameQueryBuilderUpdateImpl(this, limit: 1); + + _FrameQueryUpdate get updateAll => _FrameQueryBuilderUpdateImpl(this); +} + +extension FrameQueryFilter on QueryBuilder { + QueryBuilder idEqualTo( + int value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + EqualCondition( + property: 0, + value: value, + ), + ); + }); + } + + QueryBuilder idGreaterThan( + int value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + GreaterCondition( + property: 0, + value: value, + ), + ); + }); + } + + QueryBuilder idGreaterThanOrEqualTo( + int value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + GreaterOrEqualCondition( + property: 0, + value: value, + ), + ); + }); + } + + QueryBuilder idLessThan( + int value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + LessCondition( + property: 0, + value: value, + ), + ); + }); + } + + QueryBuilder idLessThanOrEqualTo( + int value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + LessOrEqualCondition( + property: 0, + value: value, + ), + ); + }); + } + + QueryBuilder idBetween( + int lower, + int upper, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + BetweenCondition( + property: 0, + lower: lower, + upper: upper, + ), + ); + }); + } + + QueryBuilder typeIdIsNull() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(const IsNullCondition(property: 1)); + }); + } + + QueryBuilder typeIdIsNotNull() { + return QueryBuilder.apply(not(), (query) { + return query.addFilterCondition(const IsNullCondition(property: 1)); + }); + } + + QueryBuilder typeIdEqualTo( + int? value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + EqualCondition( + property: 1, + value: value, + ), + ); + }); + } + + QueryBuilder typeIdGreaterThan( + int? value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + GreaterCondition( + property: 1, + value: value, + ), + ); + }); + } + + QueryBuilder typeIdGreaterThanOrEqualTo( + int? value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + GreaterOrEqualCondition( + property: 1, + value: value, + ), + ); + }); + } + + QueryBuilder typeIdLessThan( + int? value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + LessCondition( + property: 1, + value: value, + ), + ); + }); + } + + QueryBuilder typeIdLessThanOrEqualTo( + int? value, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + LessOrEqualCondition( + property: 1, + value: value, + ), + ); + }); + } + + QueryBuilder typeIdBetween( + int? lower, + int? upper, + ) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + BetweenCondition( + property: 1, + lower: lower, + upper: upper, + ), + ); + }); + } + + QueryBuilder keyIsNull() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition(const IsNullCondition(property: 2)); + }); + } + + QueryBuilder keyIsNotNull() { + return QueryBuilder.apply(not(), (query) { + return query.addFilterCondition(const IsNullCondition(property: 2)); + }); + } + + QueryBuilder keyEqualTo( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + EqualCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyGreaterThan( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + GreaterCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyGreaterThanOrEqualTo( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + GreaterOrEqualCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyLessThan( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + LessCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyLessThanOrEqualTo( + String? value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + LessOrEqualCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyBetween( + String? lower, + String? upper, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + BetweenCondition( + property: 2, + lower: lower, + upper: upper, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyStartsWith( + String value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + StartsWithCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyEndsWith( + String value, { + bool caseSensitive = true, + }) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + EndsWithCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyContains(String value, + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + ContainsCondition( + property: 2, + value: value, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyMatches(String pattern, + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + MatchesCondition( + property: 2, + wildcard: pattern, + caseSensitive: caseSensitive, + ), + ); + }); + } + + QueryBuilder keyIsEmpty() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + const EqualCondition( + property: 2, + value: '', + ), + ); + }); + } + + QueryBuilder keyIsNotEmpty() { + return QueryBuilder.apply(this, (query) { + return query.addFilterCondition( + const GreaterCondition( + property: 2, + value: '', + ), + ); + }); + } +} + +extension FrameQueryObject on QueryBuilder {} + +extension FrameQuerySortBy on QueryBuilder { + QueryBuilder sortById() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(0); + }); + } + + QueryBuilder sortByIdDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(0, sort: Sort.desc); + }); + } + + QueryBuilder sortByTypeId() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(1); + }); + } + + QueryBuilder sortByTypeIdDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(1, sort: Sort.desc); + }); + } + + QueryBuilder sortByKey( + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addSortBy( + 2, + caseSensitive: caseSensitive, + ); + }); + } + + QueryBuilder sortByKeyDesc( + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addSortBy( + 2, + sort: Sort.desc, + caseSensitive: caseSensitive, + ); + }); + } + + QueryBuilder sortByValue() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(3); + }); + } + + QueryBuilder sortByValueDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(3, sort: Sort.desc); + }); + } +} + +extension FrameQuerySortThenBy on QueryBuilder { + QueryBuilder thenById() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(0); + }); + } + + QueryBuilder thenByIdDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(0, sort: Sort.desc); + }); + } + + QueryBuilder thenByTypeId() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(1); + }); + } + + QueryBuilder thenByTypeIdDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(1, sort: Sort.desc); + }); + } + + QueryBuilder thenByKey( + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(2, caseSensitive: caseSensitive); + }); + } + + QueryBuilder thenByKeyDesc( + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(2, sort: Sort.desc, caseSensitive: caseSensitive); + }); + } + + QueryBuilder thenByValue() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(3); + }); + } + + QueryBuilder thenByValueDesc() { + return QueryBuilder.apply(this, (query) { + return query.addSortBy(3, sort: Sort.desc); + }); + } +} + +extension FrameQueryWhereDistinct on QueryBuilder { + QueryBuilder distinctByTypeId() { + return QueryBuilder.apply(this, (query) { + return query.addDistinctBy(1); + }); + } + + QueryBuilder distinctByKey( + {bool caseSensitive = true}) { + return QueryBuilder.apply(this, (query) { + return query.addDistinctBy(2, caseSensitive: caseSensitive); + }); + } + + QueryBuilder distinctByValue() { + return QueryBuilder.apply(this, (query) { + return query.addDistinctBy(3); + }); + } +} + +extension FrameQueryProperty1 on QueryBuilder { + QueryBuilder idProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(0); + }); + } + + QueryBuilder typeIdProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(1); + }); + } + + QueryBuilder keyProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(2); + }); + } + + QueryBuilder valueProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(3); + }); + } +} + +extension FrameQueryProperty2 on QueryBuilder { + QueryBuilder idProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(0); + }); + } + + QueryBuilder typeIdProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(1); + }); + } + + QueryBuilder keyProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(2); + }); + } + + QueryBuilder valueProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(3); + }); + } +} + +extension FrameQueryProperty3 + on QueryBuilder { + QueryBuilder idProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(0); + }); + } + + QueryBuilder typeIdProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(1); + }); + } + + QueryBuilder keyProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(2); + }); + } + + QueryBuilder valueProperty() { + return QueryBuilder.apply(this, (query) { + return query.addProperty(3); + }); + } +} diff --git a/lib/src/impl/type_registry.dart b/lib/src/impl/type_registry.dart new file mode 100644 index 000000000..c8e0d7e8b --- /dev/null +++ b/lib/src/impl/type_registry.dart @@ -0,0 +1,93 @@ +part of hive; + +const _builtinTypes = { + bool: _TypeHandler.builtin(), + num: _TypeHandler.builtin(), + String: _TypeHandler.builtin(), + List: _TypeHandler>.builtin(), + Map: _TypeHandler>.builtin(), +}; + +class _TypeRegistry { + final Map> _registry = {}; + final Map> _reverseRegistry = {..._builtinTypes}; + + void register(int typeId, T? Function(dynamic json) fromJson) { + if (T == dynamic) { + throw ArgumentError('Cannot register dynamic type.'); + } + + final handler = _TypeHandler(typeId, fromJson); + _registry[typeId] = handler; + _reverseRegistry[T] = handler; + } + + T fromJson(int? typeId, dynamic json) { + dynamic value = json; + if (typeId != null) { + final handler = _registry[typeId]; + if (handler == null) { + throw StateError( + 'Type is not registered. Did you forget to register it?', + ); + } + if (json is Map) { + value = handler.fromJson(json); + } else { + throw ArgumentError('Type mismatch. Expected Map ' + 'but got ${json.runtimeType}.'); + } + } + + if (value is T) { + return value; + } else { + throw ArgumentError( + 'Type mismatch. Expected $T but got ${value.runtimeType}.', + ); + } + } + + int? findTypeId(dynamic value) { + final handler = _reverseRegistry[value.runtimeType]; + if (handler != null) { + return handler.typeId; + } + + for (final MapEntry(key: type, value: handler) + in _reverseRegistry.entries) { + if (handler.handlesValue(value)) { + _reverseRegistry[type] = handler; + return handler.typeId; + } + } + + return null; + } + + void reset() { + _registry.clear(); + _reverseRegistry.clear(); + _reverseRegistry.addAll(_builtinTypes); + } +} + +T? _noop(Map json) { + throw UnimplementedError(); +} + +class _TypeHandler { + const _TypeHandler(this.typeId, this.fromJson); + + const _TypeHandler.builtin() + : typeId = null, + fromJson = _noop; + + final int? typeId; + + final T? Function(Map json) fromJson; + + bool handlesValue(dynamic value) { + return value is T; + } +} diff --git a/hive/pubspec.yaml b/pubspec.yaml similarity index 57% rename from hive/pubspec.yaml rename to pubspec.yaml index 22c7433b1..daa6a00cc 100644 --- a/hive/pubspec.yaml +++ b/pubspec.yaml @@ -1,21 +1,17 @@ name: hive description: Lightweight and blazing fast key-value database written in pure Dart. Strongly encrypted using AES-256. -version: 3.0.0-dev +version: 4.0.0-dev.0 homepage: https://github.com/hivedb/hive/tree/master/hive documentation: https://docs.hivedb.dev/ environment: - sdk: ">=2.12.0 <3.0.0" + sdk: ">=3.0.0 <4.0.0" dependencies: - meta: ^1.3.0 - crypto: ^3.0.0 - js: ^0.6.4 + isar: ^4.0.0-dev.13 + meta: ^1.9.0 dev_dependencies: - test: ^1.17.12 - mocktail: ^0.2.0 - lints: ^1.0.0 - path: ^1.7.0 - pointycastle: ^3.0.1 - build_runner: ^2.1.2 + build_runner: ^2.4.6 + test: ^1.23.0 + very_good_analysis: ^5.0.0 diff --git a/test/box_test.dart b/test/box_test.dart new file mode 100644 index 000000000..120504043 --- /dev/null +++ b/test/box_test.dart @@ -0,0 +1,489 @@ +import 'dart:io'; + +import 'package:test/test.dart'; + +import 'common.dart'; + +void main() { + group('Box', () { + test('.isOpen', () async { + final box = await openTestBox(); + expect(box.isOpen, true); + box.deleteFromDisk(); + expect(box.isOpen, false); + }); + + test('.name', () async { + final box = await openTestBox(name: 'testBox'); + expect(box.name, 'testBox'); + }); + + test('.directory', () async { + final box = await openTestBox(); + expect(box.directory, Directory.systemTemp.path); + }); + + test('.length', () async { + final box = await openTestBox(); + expect(box.length, 0); + box.put('key1', 'hello'); + expect(box.length, 1); + box.put('key2', 'world'); + expect(box.length, 2); + box.delete('key1'); + expect(box.length, 1); + box.delete('key2'); + expect(box.length, 0); + }); + + group('.keys', () { + test('empty', () async { + final box = await openTestBox(); + expect(box.keys, isEmpty); + }); + + test('contains all String only keys', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key1', 'hello2'); + expect(box.keys, ['key2', 'key1']); + }); + + test('contains no int keys', () async { + final box = await openTestBox(); + box.add('hello'); + box.add('world'); + box.add('hello2'); + expect(box.keys, isEmpty); + }); + + test('contains only String keys if there are both', () async { + final box = await openTestBox(); + box.put('key2', 'hello'); + box.add('world'); + box.put('key2', 'hello2'); + expect(box.keys, ['key2']); + }); + }); + + group('.keyAt()', () { + test('throws IndexError for non-existing index', () async { + final box = await openTestBox(); + expect(() => box.keyAt(0), throwsA(isA())); + }); + + test('returns null if key is not set', () async { + final box = await openTestBox(); + box.put('key2', 'hello'); + box.add('world'); + expect(box.keyAt(1), null); + }); + + test('returns key', () async { + final box = await openTestBox(); + box.put('key2', 'hello'); + box.put('key1', 'world'); + box.put('key2', 'hello2'); + expect(box.keyAt(0), 'key1'); + expect(box.keyAt(1), 'key2'); + }); + }); + + test('.containsKey()', () async { + final box = await openTestBox(); + expect(box.containsKey('key1'), false); + box.put('key1', 'hello'); + expect(box.containsKey('key1'), true); + }); + + group('.get()', () { + test('non-existing key returns null', () async { + final box = await openTestBox(); + expect(box.get('key1'), null); + box.put('key1', true); + expect(box.get('key2'), null); + }); + + test('non-existing key returns default value', () async { + final box = await openTestBox(); + expect(box.get('key1', defaultValue: 'hello'), 'hello'); + box.put('key1', true); + expect(box.get('key2', defaultValue: 'hello'), 'hello'); + }); + + test('returns value', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + expect(box.get('key1'), 'hello'); + }); + }); + + group('.getAt()', () { + test('throws IndexError for non-existing index', () async { + final box = await openTestBox(); + expect(() => box.getAt(0), throwsA(isA())); + }); + + test('returns value', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + expect(box.getAt(0), 'hello'); + expect(box.getAt(1), 'world'); + + box.putAt(1, 'world2'); + expect(box.getAt(1), 'world2'); + }); + }); + + group('operator []', () { + test('throws ArgumentError for non-int and non-String key', () async { + final box = await openTestBox(); + expect(() => box[true], throwsArgumentError); + }); + + test('returns null for non-existing key', () async { + final box = await openTestBox(); + expect(box['key1'], null); + box.put('key1', true); + expect(box['key2'], null); + }); + + test('throws IndexError for non-existing index', () async { + final box = await openTestBox(); + expect(() => box[0], throwsA(isA())); + }); + + test('returns value', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + expect(box['key1'], 'hello'); + expect(box[0], 'hello'); + }); + }); + + group('.getAll()', () { + test('returns empty list for empty keys', () async { + final box = await openTestBox(); + expect(box.getAll([]), isEmpty); + }); + + test('returns empty list for non-existing keys', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + expect(box.getAll(['key3', 'key4']), isEmpty); + }); + + test('returns values', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + expect(box.getAll(['key1', 'key3']), ['hello', '!']); + }); + }); + + group('.getRange()', () { + test('throws RangeError for invalid range', () async { + final box = await openTestBox(); + expect(() => box.getRange(-1, 1), throwsRangeError); + expect(() => box.getRange(0, -1), throwsRangeError); + expect(() => box.getRange(1, 0), throwsRangeError); + expect(() => box.getRange(0, 1), throwsRangeError); + }); + + test('returns empty list for empty range', () async { + final box = await openTestBox(); + box.add('hello'); + expect(box.getRange(0, 0), isEmpty); + }); + + test('returns values', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + expect(box.getRange(0, 2), ['hello', 'world']); + }); + }); + + group('.getBetween()', () { + test('returns all values if start end end key are null', () async { + final box = await openTestBox(); + box.add('hello'); + box.put('key', 'value'); + expect(box.getBetween(), ['value']); + }); + + test('returns empty list if start key is greater than end key', () async { + final box = await openTestBox(); + box.put('a', 'value1'); + box.put('b', 'value2'); + expect(box.getBetween(startKey: 'b', endKey: 'a'), isEmpty); + }); + + test('returns values', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + expect(box.getBetween(), ['hello', 'world', '!']); + expect(box.getBetween(startKey: 'key2'), ['world', '!']); + expect(box.getBetween(endKey: 'key2'), ['hello', 'world']); + expect(box.getBetween(startKey: 'key2', endKey: 'key2'), ['world']); + }); + }); + + group('.put()', () { + test('overrides existing entry if it exists', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + box.put('key2', 'world2'); + expect(box.getAt(2), 'world2'); + expect(box.keys, ['key1', 'key3', 'key2']); + }); + + test('adds new entry if it does not exist', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + box.put('key4', 'world2'); + expect(box.getAt(3), 'world2'); + expect(box.keys, ['key1', 'key2', 'key3', 'key4']); + }); + }); + + group('.putAt()', () { + test('throws IndexError for non-existing index', () async { + final box = await openTestBox(); + expect(() => box.putAt(0, 'hello'), throwsA(isA())); + }); + + test('overrides existing entry', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', 'world'); + box.put('key3', '!'); + box.putAt(1, 'world2'); + expect(box.getAt(1), 'world2'); + expect(box.keys, ['key1', 'key3']); + }); + }); + + group('operator []=', () { + test('throws ArgumentError for non-int and non-String key', () async { + final box = await openTestBox(); + expect(() => box[true] = 'hello', throwsArgumentError); + }); + + test('overrides existing entry if String key exists', () async { + final box = await openTestBox(); + box['key1'] = 'hello'; + box['key2'] = 'world'; + box['key3'] = '!'; + box['key2'] = 'world2'; + expect(box.getAt(2), 'world2'); + expect(box.keys, ['key1', 'key3', 'key2']); + }); + + test('adds new entry if String key does not exist', () async { + final box = await openTestBox(); + box['key1'] = 'hello'; + box['key2'] = 'world'; + box['key3'] = '!'; + box['key4'] = 'world2'; + expect(box.getAt(3), 'world2'); + expect(box.keys, ['key1', 'key2', 'key3', 'key4']); + }); + + test('throws IndexError for non-existing index', () async { + final box = await openTestBox(); + expect(() => box[0] = 'hello', throwsA(isA())); + }); + + test('overrides existing entry if int key exists', () async { + final box = await openTestBox(); + box.add('hello'); + box.add('world'); + box.add('!'); + box[1] = 'world2'; + expect(box.getAt(1), 'world2'); + expect(box.keys, isEmpty); + }); + }); + + test('.putAll()', () async { + final box = await openTestBox(); + box.putAll({'key1': 'hello', 'key2': 'world'}); + expect(box.getAt(0), 'hello'); + expect(box.getAt(1), 'world'); + expect(box.keys, ['key1', 'key2']); + + box.putAll({'key3': '!', 'key1': 'hello2'}); + expect(box.keys, ['key2', 'key3', 'key1']); + expect(box.getAt(0), 'world'); + expect(box.getAt(1), '!'); + expect(box.getAt(2), 'hello2'); + }); + + group('.putRange()', () { + test('throws RangeError for invalid range', () async { + final box = await openTestBox(); + expect(() => box.putRange(-1, 1, ['hello']), throwsRangeError); + expect(() => box.putRange(0, -1, ['hello']), throwsRangeError); + expect(() => box.putRange(1, 0, ['hello']), throwsRangeError); + expect(() => box.putRange(0, 1, ['hello']), throwsRangeError); + }); + + test('does nothing for empty range', () async { + final box = await openTestBox(); + box.putRange(0, 0, ['hello']); + expect(box.keys, isEmpty); + }); + + test('overrides existing entries', () async { + final box = await openTestBox(); + box.putAll({ + 'key1': 'value1', + 'key2': 'value2', + 'key3': 'value3', + 'key4': 'value4', + }); + box.putRange(1, 3, ['newValue2', 'newValue3']); + expect(box.getAt(0), 'value1'); + expect(box.getAt(1), 'newValue2'); + expect(box.getAt(2), 'newValue3'); + expect(box.getAt(3), 'value4'); + expect(box.keys, ['key1', 'key4']); + }); + }); + + test('.add()', () async { + final box = await openTestBox(); + box.add('hello'); + box.add('world'); + box.add('!'); + expect(box.getAt(0), 'hello'); + expect(box.getAt(1), 'world'); + expect(box.getAt(2), '!'); + expect(box.keys, isEmpty); + }); + + test('.addAll()', () async { + final box = await openTestBox(); + box.addAll(['hello', 'world']); + expect(box.getAt(0), 'hello'); + expect(box.getAt(1), 'world'); + expect(box.keys, isEmpty); + + box.addAll(['!', 'hello2']); + expect(box.getAt(0), 'hello'); + expect(box.getAt(1), 'world'); + expect(box.getAt(2), '!'); + expect(box.getAt(3), 'hello2'); + expect(box.keys, isEmpty); + }); + + group('.delete()', () { + test('returns false for non-existing key', () async { + final box = await openTestBox(); + box.put('key2', true); + box.add(false); + expect(box.delete('key1'), false); + expect(box.length, 2); + }); + + test('returns true for existing key', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', true); + box.add(false); + expect(box.delete('key1'), true); + expect(box.length, 2); + }); + }); + + group('.deleteAt()', () { + test('throws IndexError for non-existing index', () async { + final box = await openTestBox(); + expect(() => box.deleteAt(0), throwsA(isA())); + }); + + test('deletes entry', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', true); + box.add(false); + box.deleteAt(1); + expect(box.length, 2); + expect(box.keys, ['key1']); + }); + }); + + group('.deleteAll()', () { + test('returns 0 for empty keys', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', true); + box.add(false); + expect(box.deleteAll([]), 0); + expect(box.length, 3); + }); + + test('returns 0 for non-existing keys', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', true); + box.add(false); + expect(box.deleteAll(['key3', 'key4']), 0); + expect(box.length, 3); + }); + + test('returns number of deleted entries', () async { + final box = await openTestBox(); + box.put('key1', 'hello'); + box.put('key2', true); + box.add(false); + expect(box.deleteAll(['key1', 'key3', 'key2']), 2); + expect(box.length, 1); + expect(box.keys, isEmpty); + }); + }); + + group('.deleteRange()', () { + test('throws RangeError for invalid range', () async { + final box = await openTestBox(); + expect(() => box.deleteRange(-1, 1), throwsRangeError); + expect(() => box.deleteRange(0, -1), throwsRangeError); + expect(() => box.deleteRange(1, 0), throwsRangeError); + expect(() => box.deleteRange(0, 1), throwsRangeError); + }); + + test('does nothing for empty range', () async { + final box = await openTestBox(); + box.add('hello'); + box.deleteRange(0, 0); + expect(box.length, 1); + }); + + test('deletes entries', () async { + final box = await openTestBox(); + box.putAll({ + 'key1': 'value1', + 'key2': 'value2', + 'key3': 'value3', + 'key4': 'value4', + }); + box.deleteRange(1, 3); + expect(box.length, 2); + expect(box.keys, ['key1', 'key4']); + }); + }); + }); +} diff --git a/test/common.dart b/test/common.dart new file mode 100644 index 000000000..2a6d503c4 --- /dev/null +++ b/test/common.dart @@ -0,0 +1,55 @@ +import 'dart:ffi'; +import 'dart:io'; +import 'dart:math'; + +import 'package:hive/hive.dart'; +import 'package:hive/src/impl/frame.dart'; +import 'package:isar/isar.dart'; +import 'package:test/test.dart'; + +const _releases = 'https://github.com/isar/isar/releases/download/'; + +Future initTests() async { + final lib = switch (Abi.current()) { + Abi.macosArm64 || Abi.macosX64 => 'libisar_macos.dylib', + Abi.linuxX64 => 'libisar_linux_x64.so', + Abi.windowsX64 => 'isar_windows_x64.dll', + _ => throw UnsupportedError('Unsupported test platform'), + }; + + final libPath = Directory.current.path + Platform.pathSeparator + lib; + final file = File(libPath); + if (!file.existsSync()) { + final uri = Uri.parse('$_releases/${Isar.version}/$lib'); + final request = await HttpClient().getUrl(uri); + final response = await request.close(); + await response.pipe(file.openWrite()); + } + + Isar.initialize(file.path); + Hive.defaultDirectory = Directory.systemTemp.path; +} + +Future> openTestBox({String? name}) async { + await initTests(); + name ??= Random().nextInt(999999).toString(); + final box = Hive.box(name: name); + box.verify(); + addTearDown(() async { + if (box.isOpen) { + box.verify(); + box.deleteFromDisk(); + } + }); + + return box; +} + +extension BoxVerify on Box { + void verify() { + final isar = Isar.get(schemas: [FrameSchema], name: name); + final keys = + isar.frames.where().keyProperty().findAll().whereType(); + expect(keys.length, keys.toSet().length); + } +}