mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2024-10-01 06:55:46 -04:00
better cleanup
This commit is contained in:
parent
672bcd5c3d
commit
8952751cb0
@ -32,9 +32,12 @@ class DHTShortArray {
|
|||||||
case DHTSchemaSMPL():
|
case DHTSchemaSMPL():
|
||||||
throw StateError('Wrote kind of DHT record for DHTShortArray');
|
throw StateError('Wrote kind of DHT record for DHTShortArray');
|
||||||
}
|
}
|
||||||
|
assert(stride <= MAX_ELEMENTS, 'stride too long');
|
||||||
_stride = stride;
|
_stride = stride;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static const MAX_ELEMENTS = 256;
|
||||||
|
|
||||||
// Head DHT record
|
// Head DHT record
|
||||||
final DHTRecord _headRecord;
|
final DHTRecord _headRecord;
|
||||||
late final int _stride;
|
late final int _stride;
|
||||||
@ -44,10 +47,16 @@ class DHTShortArray {
|
|||||||
|
|
||||||
static Future<DHTShortArray> create(VeilidRoutingContext dhtctx, int stride,
|
static Future<DHTShortArray> create(VeilidRoutingContext dhtctx, int stride,
|
||||||
{DHTRecordCrypto? crypto}) async {
|
{DHTRecordCrypto? crypto}) async {
|
||||||
|
assert(stride <= MAX_ELEMENTS, 'stride too long');
|
||||||
final dhtRecord = await DHTRecord.create(dhtctx,
|
final dhtRecord = await DHTRecord.create(dhtctx,
|
||||||
schema: DHTSchema.dflt(oCnt: stride + 1), crypto: crypto);
|
schema: DHTSchema.dflt(oCnt: stride + 1), crypto: crypto);
|
||||||
final dhtShortArray = DHTShortArray(dhtRecord: dhtRecord);
|
try {
|
||||||
return dhtShortArray;
|
final dhtShortArray = DHTShortArray(dhtRecord: dhtRecord);
|
||||||
|
return dhtShortArray;
|
||||||
|
} on Exception catch (_) {
|
||||||
|
await dhtRecord.delete();
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<DHTShortArray> openRead(
|
static Future<DHTShortArray> openRead(
|
||||||
@ -55,9 +64,14 @@ class DHTShortArray {
|
|||||||
{DHTRecordCrypto? crypto}) async {
|
{DHTRecordCrypto? crypto}) async {
|
||||||
final dhtRecord =
|
final dhtRecord =
|
||||||
await DHTRecord.openRead(dhtctx, dhtRecordKey, crypto: crypto);
|
await DHTRecord.openRead(dhtctx, dhtRecordKey, crypto: crypto);
|
||||||
final dhtShortArray = DHTShortArray(dhtRecord: dhtRecord);
|
try {
|
||||||
await dhtShortArray._refreshHead();
|
final dhtShortArray = DHTShortArray(dhtRecord: dhtRecord);
|
||||||
return dhtShortArray;
|
await dhtShortArray._refreshHead();
|
||||||
|
return dhtShortArray;
|
||||||
|
} on Exception catch (_) {
|
||||||
|
await dhtRecord.close();
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static Future<DHTShortArray> openWrite(
|
static Future<DHTShortArray> openWrite(
|
||||||
@ -68,9 +82,14 @@ class DHTShortArray {
|
|||||||
}) async {
|
}) async {
|
||||||
final dhtRecord =
|
final dhtRecord =
|
||||||
await DHTRecord.openWrite(dhtctx, dhtRecordKey, writer, crypto: crypto);
|
await DHTRecord.openWrite(dhtctx, dhtRecordKey, writer, crypto: crypto);
|
||||||
final dhtShortArray = DHTShortArray(dhtRecord: dhtRecord);
|
try {
|
||||||
await dhtShortArray._refreshHead();
|
final dhtShortArray = DHTShortArray(dhtRecord: dhtRecord);
|
||||||
return dhtShortArray;
|
await dhtShortArray._refreshHead();
|
||||||
|
return dhtShortArray;
|
||||||
|
} on Exception catch (_) {
|
||||||
|
await dhtRecord.close();
|
||||||
|
rethrow;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////
|
||||||
@ -232,6 +251,7 @@ class DHTShortArray {
|
|||||||
// xxx: swap
|
// xxx: swap
|
||||||
// xxx: remove
|
// xxx: remove
|
||||||
// xxx: clear
|
// xxx: clear
|
||||||
|
// xxx ensure these write the head back out because they change it
|
||||||
|
|
||||||
Future<Uint8List?> getItem(int index, {bool forceRefresh = false}) async {
|
Future<Uint8List?> getItem(int index, {bool forceRefresh = false}) async {
|
||||||
await _refreshHead(forceRefresh: forceRefresh, onlyUpdates: true);
|
await _refreshHead(forceRefresh: forceRefresh, onlyUpdates: true);
|
||||||
|
Loading…
Reference in New Issue
Block a user