Skip to content

Commit

Permalink
Merge pull request isar#1038 from hivedb/braid/open-box-store
Browse files Browse the repository at this point in the history
feat: store open boxes including collection
  • Loading branch information
The one with the braid (she/her) | Dфҿ mit dem Zopf (sie/ihr) authored Jul 20, 2022
2 parents 5228aa4 + dbf5093 commit ba33b58
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class WebWorkerInterface {
);

_worker.postMessage(jsify(operation.toJson()));
return completer.future;
return completer.future.timeout(Duration(seconds: 45));
}

void _handleMessage(MessageEvent event) {
Expand Down
17 changes: 10 additions & 7 deletions hive/lib/src/box_collection/box_collection.dart
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,15 @@ class BoxCollection implements implementation.BoxCollection {
final box = boxCreator?.call(boxIdentifier, this) as CollectionBox<V>? ??
CollectionBox<V>(boxIdentifier, this);
if (preload) {
box._cachedBox = await Hive.openBox(
box.name,
encryptionCipher: _cipher,
collection: name,
backend: _backends[name]!,
);
final hive = Hive as HiveImpl;
box._cachedBox = Hive.isBoxOpen(box.name)
? hive.lazyBox(box.name, name)
: await Hive.openBox(
box.name,
encryptionCipher: _cipher,
collection: name,
backend: _backends[name]!,
);
}
_openBoxes.add(box);
return box;
Expand Down Expand Up @@ -117,7 +120,7 @@ class BoxCollection implements implementation.BoxCollection {

@override
Future<void> deleteFromDisk() => Future.wait(
boxNames.map(Hive.deleteBoxFromDisk),
boxNames.map((box) => Hive.deleteBoxFromDisk(box, collection: name)),
);
}

Expand Down
3 changes: 2 additions & 1 deletion hive/lib/src/hive.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ abstract class HiveInterface implements TypeRegistry {
/// Removes the file which contains the box and closes the box.
///
/// In the browser, the IndexedDB database is being removed.
Future<void> deleteBoxFromDisk(String name, {String? path});
Future<void> deleteBoxFromDisk(String name,
{String? path, String? collection});

/// Deletes all currently open boxes from disk.
///
Expand Down
46 changes: 32 additions & 14 deletions hive/lib/src/hive_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
static final BackendManagerInterface _defaultBackendManager =
BackendManager.select();

final _boxes = HashMap<String, BoxBaseImpl>();
final _boxes = HashMap<TupleBoxKey, BoxBaseImpl>();
final _openingBoxes = HashMap<String, Future>();
BackendManagerInterface? _managerOverride;
final Random _secureRandom = Random.secure();
Expand Down Expand Up @@ -103,7 +103,7 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
}

await newBox.initialize();
_boxes[name] = newBox;
_boxes[TupleBoxKey(name, collection)] = newBox;

completer.complete();
return newBox;
Expand Down Expand Up @@ -213,9 +213,9 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
})));
}*/

BoxBase<E> _getBoxInternal<E>(String name, [bool? lazy]) {
BoxBase<E> _getBoxInternal<E>(String name, [bool? lazy, String? collection]) {
var lowerCaseName = name.toLowerCase();
var box = _boxes[lowerCaseName];
var box = _boxes[TupleBoxKey(lowerCaseName, collection)];
if (box != null) {
if ((lazy == null || box.lazy == lazy) && box.valueType == E) {
return box as BoxBase<E>;
Expand All @@ -232,21 +232,22 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
}

/// Not part of public API
BoxBase? getBoxWithoutCheckInternal(String name) {
BoxBase? getBoxWithoutCheckInternal(String name, [String? collection]) {
var lowerCaseName = name.toLowerCase();
return _boxes[lowerCaseName];
return _boxes[TupleBoxKey(lowerCaseName, collection)];
}

@override
Box<E> box<E>(String name) => _getBoxInternal<E>(name, false) as Box<E>;
Box<E> box<E>(String name, [String? collection]) =>
_getBoxInternal<E>(name, false, collection) as Box<E>;

@override
LazyBox<E> lazyBox<E>(String name) =>
_getBoxInternal<E>(name, true) as LazyBox<E>;
LazyBox<E> lazyBox<E>(String name, [String? collection]) =>
_getBoxInternal<E>(name, true, collection) as LazyBox<E>;

@override
bool isBoxOpen(String name) {
return _boxes.containsKey(name.toLowerCase());
bool isBoxOpen(String name, [String? collection]) {
return _boxes.containsKey(TupleBoxKey(name.toLowerCase(), collection));
}

@override
Expand All @@ -259,17 +260,17 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
}

/// Not part of public API
void unregisterBox(String name) {
void unregisterBox(String name, [String? collection]) {
name = name.toLowerCase();
_openingBoxes.remove(name);
_boxes.remove(name);
_boxes.remove(TupleBoxKey(name, collection));
}

@override
Future<void> deleteBoxFromDisk(String name,
{String? path, String? collection}) async {
var lowerCaseName = name.toLowerCase();
var box = _boxes[lowerCaseName];
var box = _boxes[TupleBoxKey(lowerCaseName, collection)];
if (box != null) {
await box.deleteFromDisk();
} else {
Expand Down Expand Up @@ -298,3 +299,20 @@ class HiveImpl extends TypeRegistryImpl implements HiveInterface {
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
int get hashCode => box.hashCode + collection.hashCode;

@override
bool operator ==(Object other) {
return hashCode == other.hashCode;
}
}

0 comments on commit ba33b58

Please sign in to comment.