Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix: missing TupleBoxKey for opening boxes #1041

Merged
merged 1 commit into from
Jul 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions hive/lib/src/box_collection/box_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ class BoxCollection implements implementation.BoxCollection {
CollectionBox<V>(boxIdentifier, this);
if (preload) {
final hive = Hive as HiveImpl;
box._cachedBox = Hive.isBoxOpen(box.name)
? hive.lazyBox(box.name, name)
: await Hive.openBox(
box._cachedBox = hive.isBoxOpen(box.name, this.name)
? hive.lazyBox(box.name, this.name)
: await hive.openBox(
box.name,
encryptionCipher: _cipher,
collection: name,
collection: this.name,
backend: _backends[name]!,
);
}
Expand Down Expand Up @@ -137,12 +137,15 @@ class CollectionBox<V> implements implementation.CollectionBox<V> {

Future<BoxBase> _getBox() async {
if (_cachedBox == null || !_cachedBox!.isOpen) {
_cachedBox = await Hive.openLazyBox<V>(
name,
encryptionCipher: boxCollection._cipher,
collection: boxCollection.name,
backend: boxCollection._backends[name]!,
);
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!;
Expand Down
23 changes: 15 additions & 8 deletions hive/lib/src/hive_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
BackendManager.select();

final _boxes = HashMap<TupleBoxKey, BoxBaseImpl>();
final _openingBoxes = HashMap<String, Future>();
final _openingBoxes = HashMap<TupleBoxKey, Future>();
BackendManagerInterface? _managerOverride;
final Random _secureRandom = Random.secure();

Expand Down Expand Up @@ -79,8 +79,8 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
return box(name);
}
} else {
if (_openingBoxes.containsKey(name)) {
await _openingBoxes[name];
if (_openingBoxes.containsKey(TupleBoxKey(name, collection))) {
await _openingBoxes[TupleBoxKey(name, collection)];
if (lazy) {
return lazyBox(name);
} else {
Expand All @@ -89,7 +89,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
}

var completer = Completer();
_openingBoxes[name] = completer.future;
_openingBoxes[TupleBoxKey(name, collection)] = completer.future;

BoxBaseImpl<E>? newBox;
try {
Expand All @@ -113,7 +113,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
rethrow;
} finally {
// ignore: unawaited_futures
_openingBoxes.remove(name);
_openingBoxes.remove(TupleBoxKey(name, collection));
}
}
}
Expand Down Expand Up @@ -233,7 +233,10 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {

/// 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)];
}

Expand Down Expand Up @@ -262,7 +265,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
/// Not part of public API
void unregisterBox(String name, [String? collection]) {
name = name.toLowerCase();
_openingBoxes.remove(name);
_openingBoxes.remove(TupleBoxKey(name, collection));
_boxes.remove(TupleBoxKey(name, collection));
}

Expand Down Expand Up @@ -300,7 +303,6 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
}
}


/// tiny helper for map key management...
class TupleBoxKey {
final String box;
Expand All @@ -309,7 +311,12 @@ class TupleBoxKey {
TupleBoxKey(this.box, this.collection);

@override
int get hashCode => box.hashCode + collection.hashCode;
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) {
Expand Down