isar / isar/hive

Hive Storage size

Open
#1,174 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
Dart
Stars
4.4k
Forks
449
PR merge metrics
No merged PRs in 30d

Description

**Question**
Hello everyone, I'm facing the following problem: the size of my hive storage reaches up to 9GB in 5 minutes of work.
My problem is the result of saving up to 150 Uin8Frames to storage under a specific identifier. For saving, I use the "put" method.

**Code sample**
```dart
class VideoSharedStorage implements SharedStorageInterface> {
static const String storageName = "video_module";
static const Duration sessionLifetime = Duration(hours: 2);

late final Box _storage;

Future init() async {
_storage = await Hive.openBox(storageName);
await clearDeprecatedSessions();
if (_storage.isOpen) LogCollector.success("Box for $storageName has been opened!", name: "Hive");
}

@override
Future clear() async {
if (_storage.isOpen) {
await _storage.clear();
}
}

@override
Future write({
required String id,
required Map data,
}) async {
try {
if (_storage.isOpen) {
final Map stored = read(id);

_registerAppointment(id);

await _storage.put(id, json.encode({...stored, ...data}));
}
} catch (_) {
LogCollector.error("Error during writing data into the Hive Video storage");
}
}

void _registerAppointment(String id) {
Map register = _readRegister();
register.addAll({id: DateTime.now().toIso8601String()});

_storage.put(_kStorageRegisterKey, json.encode(register));

LogCollector.success("Appointment for $id has been registered!", name: "VideoSharedStorage");
}

void _unregisterAppointment(String id) async {
Map register = _readRegister();

register.remove(id);
_storage.put(_kStorageRegisterKey, json.encode(register));
LogCollector.success("Appointment $id has been unregistered!", name: "VideoSharedStorage");
}

Map _readRegister() => jsonDecode(_storage.get(_kStorageRegisterKey) ?? "{}");

@override
Map read(String id) {
try {
if (_storage.isOpen) {
return jsonDecode(_storage.get(id) ?? "{}");
}
return {};
} catch (_) {
LogCollector.error("Error during reading data from the Hive Video storage");
return {};
}
}

Future clearDeprecatedSessions() async {
try {
final Map register = _readRegister();

for (final id in register.keys) {
final time = DateTime.parse(register[id]);
if (time.add(sessionLifetime).isBefore(DateTime.now())) {
delete(id);
}
}
} catch (_) {
LogCollector.error("An error occurred during the outdated sessions cleaning");
}
}

@override
Future delete(String id) async {
try {
if (_storage.isOpen) {
await _storage.delete(id);

_unregisterAppointment(id);
}
} catch (_) {
LogCollector.error("Error during deleting data from the Hive Video storage");
}
}
}
```

**Version**
- Platform: iOS, Android
- Flutter version: 3.7.3
- Hive version: ^2.2.3

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with the VideoSharedStorage write() method and trace each _storage.put call, including the separate register update in _registerAppointment(). Reproduce the reported growth by repeatedly saving Uint8Frames under one identifier on Hive 2.2.3, then compare the box size with the stored value size. Done means identifying whether the reported growth is expected or isolating a reproducible storage issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart, flutter
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.