Skip to content
This repository has been archived by the owner on Apr 16, 2021. It is now read-only.

Commit

Permalink
Test, fix limiting number of saved positions
Browse files Browse the repository at this point in the history
  • Loading branch information
yringler committed Jun 22, 2020
1 parent 0abbb7a commit c9795c1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
10 changes: 5 additions & 5 deletions lib/position-manager/position-data-manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class PositionDataManager extends IPositionDataManager {
class HivePositionDataManager extends IPositionDataManager {
static final positionHive = HiveImpl();
static const positionBoxName = "positions";
static const int maxSavedPositions = 2000;
static const int maxSavedPositions = 200;

Box<Position> positionBox;
HivePositionDataManager();
Expand Down Expand Up @@ -120,22 +120,22 @@ class HivePositionDataManager extends IPositionDataManager {
await positionBox.put(position.id.limitFromEnd(255), position);
} else {
// No need to clutter the DB with zeros - that's the default, anyway.
await positionBox.delete(position.id);
await positionBox.delete(position.id.limitFromEnd(255));
}
}

/// Make sure we don't try to hold too many positions in memory.
Future<void> _constrainBoxSize() async {
// E.g: There are 3 items, we're aloud max of 2, we need to delete 1.
int amountPositionsToDelete = positionBox.length - maxSavedPositions;

if (amountPositionsToDelete < 1) {
return;
}

final positions = positionBox.values.toList();
positions.sort((p1, p2) =>
p2.createdDate.millisecondsSinceEpoch -
p1.createdDate.millisecondsSinceEpoch);

positions.sort((p1, p2) => p1.createdDate.compareTo(p2.createdDate));
final positionsToDelete = positions.take(amountPositionsToDelete);

await Stream.fromFutures(
Expand Down
20 changes: 10 additions & 10 deletions lib/position-manager/position.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ part 'position.g.dart';
/// Keep track of the last position of an audio file.
@HiveType(typeId: 0)
class Position extends HiveObject {
Position({Duration position, String id}) {
Position({Duration position, this.id}) {
if (position != null) {
this.position = position;
}
this.id = id;

_createdDate = DateTime.now().millisecondsSinceEpoch;
}

/// When the file was created, or the position was last updated.
DateTime get createdDate => DateTime.fromMillisecondsSinceEpoch(_createdDate);
/// If [_createdDate] isn't set, consider it as being 0, which makes the record very old, and will probably be
/// deleted soon.
DateTime get createdDate =>
DateTime.fromMillisecondsSinceEpoch(_createdDate ?? 0);

Duration get position => Duration(milliseconds: _position ?? 0);

Expand All @@ -22,17 +26,13 @@ class Position extends HiveObject {
}

/// Expose the id whose position is being described, independent of hive storage.
String get id {
return key ?? _id;
}

set id(String id) => _id = id;
/// This is important because the hive id is truncated according to hive requirements.
@HiveField(2)
String id;

@HiveField(0)
int _position;

@HiveField(1)
int _createdDate;

String _id;
}
16 changes: 15 additions & 1 deletion lib/position-manager/position.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c9795c1

Please sign in to comment.