From ca072881d2915e68dc1cc1ec74d56ff801e0dca7 Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 6 Feb 2025 11:41:07 -0500 Subject: [PATCH 1/5] Ensure HiveObject disposal in lazy boxes --- hive/lib/src/object/hive_object.dart | 8 +++++-- hive/test/integration/hive_object_test.dart | 24 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/hive/lib/src/object/hive_object.dart b/hive/lib/src/object/hive_object.dart index b168d53d..c3a5f4bc 100644 --- a/hive/lib/src/object/hive_object.dart +++ b/hive/lib/src/object/hive_object.dart @@ -35,9 +35,13 @@ mixin HiveObjectMixin { } /// Deletes this object from the box it is stored in. - Future delete() { + Future delete() async { _requireInitialized(); - return _box!.delete(_key); + await _box!.delete(_key); + if (_box?.lazy == true) { + // Lazy boxes won't automatically dispose their HiveObjects + dispose(); + } } /// Returns whether this object is currently stored in a box. diff --git a/hive/test/integration/hive_object_test.dart b/hive/test/integration/hive_object_test.dart index 8aeb1351..e83c0b41 100644 --- a/hive/test/integration/hive_object_test.dart +++ b/hive/test/integration/hive_object_test.dart @@ -72,4 +72,28 @@ void main() { }, timeout: longTimeout, ); + + test('move HiveObject between lazy boxes', () async { + final hive = await createHive(); + hive.registerAdapter<_TestObject>(_TestObjectAdapter()); + + final box1 = await openBox(true, hive: hive); + final box2 = await openBox(true, hive: hive); + + final obj = _TestObject('test'); + + expect(obj.box, null); + expect(obj.key, null); + final key1 = await box1.add(obj); + expect(obj.box, box1); + expect(obj.key, key1); + + await obj.delete(); + expect(obj.box, null); + expect(obj.key, null); + + final key2 = await box2.add(obj); + expect(obj.box, box2); + expect(obj.key, key2); + }); } From 32923aacfc79d4922907916dcdafe190e416003a Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 6 Feb 2025 11:51:29 -0500 Subject: [PATCH 2/5] Fix mocks --- hive_flutter/test/mocks.mocks.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hive_flutter/test/mocks.mocks.dart b/hive_flutter/test/mocks.mocks.dart index 8ceaa07a..7693c486 100644 --- a/hive_flutter/test/mocks.mocks.dart +++ b/hive_flutter/test/mocks.mocks.dart @@ -338,8 +338,8 @@ class MockBinaryWriter extends _i2.Mock implements _i8.BinaryWriter { ); @override - void write(T? value, {bool? writeTypeId = true}) => super.noSuchMethod( - Invocation.method(#write, [value], {#writeTypeId: writeTypeId}), + void write(T? value, {bool? withTypeId = true}) => super.noSuchMethod( + Invocation.method(#write, [value], {#withTypeId: withTypeId}), returnValueForMissingStub: null, ); } From 4d3d798fee51d7710eaeeb1d01d785480930e58b Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 6 Feb 2025 12:08:20 -0500 Subject: [PATCH 3/5] Fix tests --- hive/test/tests/box/box_base_test.dart | 4 +++- hive/test/tests/mocks.dart | 7 ++++++- hive/test/tests/object/hive_object_test.dart | 4 +--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/hive/test/tests/box/box_base_test.dart b/hive/test/tests/box/box_base_test.dart index 6d408bdd..ff1c4a7c 100644 --- a/hive/test/tests/box/box_base_test.dart +++ b/hive/test/tests/box/box_base_test.dart @@ -13,6 +13,9 @@ import '../common.dart'; import '../mocks.dart'; class _BoxBaseMock extends BoxBaseImpl with Mock { + @override + final lazy = false; + _BoxBaseMock( super.hive, super.name, @@ -158,7 +161,6 @@ void main() { test('.initialize()', () async { final backend = MockStorageBackend(); final 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)); diff --git a/hive/test/tests/mocks.dart b/hive/test/tests/mocks.dart index f1e2722f..d93f8ba4 100644 --- a/hive/test/tests/mocks.dart +++ b/hive/test/tests/mocks.dart @@ -11,7 +11,12 @@ import 'package:mocktail/mocktail.dart'; // Mocks -class MockBox extends Mock implements Box {} +class MockBox extends Mock implements Box { + @override + final bool lazy; + + MockBox({this.lazy = false}); +} class MockChangeNotifier extends Mock implements ChangeNotifier {} diff --git a/hive/test/tests/object/hive_object_test.dart b/hive/test/tests/object/hive_object_test.dart index 58c501fd..c36cd6eb 100644 --- a/hive/test/tests/object/hive_object_test.dart +++ b/hive/test/tests/object/hive_object_test.dart @@ -153,7 +153,6 @@ void main() { test('returns true if object is in normal box', () { final obj = TestHiveObject(); final box = MockBox(); - when(() => box.lazy).thenReturn(false); obj.init('key', box); expect(obj.isInBox, true); @@ -162,8 +161,7 @@ void main() { test('returns the result ob box.containsKey() if object is in lazy box', () { final obj = TestHiveObject(); - final box = MockBox(); - when(() => box.lazy).thenReturn(true); + final box = MockBox(lazy: true); obj.init('key', box); when(() => box.containsKey('key')).thenReturn(true); From 2e136d0d43a0218901fdbaa06c7ec5cdb2864489 Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 6 Feb 2025 12:09:23 -0500 Subject: [PATCH 4/5] Preparing for release --- hive/CHANGELOG.md | 4 ++++ hive/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/hive/CHANGELOG.md b/hive/CHANGELOG.md index 360b96e3..cd60a72a 100644 --- a/hive/CHANGELOG.md +++ b/hive/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.10.1 + +- Fixes `HiveObject` disposal in lazy boxes + ## 2.10.0 - Raises the maximum type ID from 223 to 65439 diff --git a/hive/pubspec.yaml b/hive/pubspec.yaml index 24c85468..793023e5 100644 --- a/hive/pubspec.yaml +++ b/hive/pubspec.yaml @@ -1,6 +1,6 @@ name: hive_ce description: Hive Community Edition - A spiritual continuation of Hive v2 -version: 2.10.0 +version: 2.10.1 homepage: https://github.com/IO-Design-Team/hive_ce/tree/main/hive documentation: https://docs.hivedb.dev/ From 2ab4c370b279529155c1196321cc45682fe0c054 Mon Sep 17 00:00:00 2001 From: Rexios Date: Thu, 6 Feb 2025 12:18:39 -0500 Subject: [PATCH 5/5] Formatting --- hive/test/integration/hive_object_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hive/test/integration/hive_object_test.dart b/hive/test/integration/hive_object_test.dart index e83c0b41..7e8abf27 100644 --- a/hive/test/integration/hive_object_test.dart +++ b/hive/test/integration/hive_object_test.dart @@ -81,9 +81,9 @@ void main() { final box2 = await openBox(true, hive: hive); final obj = _TestObject('test'); - expect(obj.box, null); expect(obj.key, null); + final key1 = await box1.add(obj); expect(obj.box, box1); expect(obj.key, key1);