From dc7761211fdd6add9e88d8b87188d1825970d5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Istv=C3=A1n=20So=C3=B3s?= Date: Thu, 6 Jul 2023 12:19:41 +0200 Subject: [PATCH] Create empty blob index in cache when task index is missing. (#6811) --- app/lib/task/backend.dart | 4 ++-- pkg/indexed_blob/lib/indexed_blob.dart | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/lib/task/backend.dart b/app/lib/task/backend.dart index f29f78eed9..954db9a176 100644 --- a/app/lib/task/backend.dart +++ b/app/lib/task/backend.dart @@ -767,13 +767,13 @@ class TaskBackend { if (bytes != null) break; } if (bytes == null) { - return null; + return BlobIndex.empty(blobId: ''); } final index = BlobIndex.fromBytes(bytes); final blobId = index.blobId; if (!_blobIdPattern.hasMatch(blobId)) { _log.warning('invalid blobId: "$blobId" in index in "$path"'); - return null; + return BlobIndex.empty(blobId: ''); } // We change the [blobId] when we store in the cache, because this frees // us from having to cache the selected [runtimeVersion] next to the diff --git a/pkg/indexed_blob/lib/indexed_blob.dart b/pkg/indexed_blob/lib/indexed_blob.dart index cfd43f8242..2465409a64 100644 --- a/pkg/indexed_blob/lib/indexed_blob.dart +++ b/pkg/indexed_blob/lib/indexed_blob.dart @@ -124,6 +124,17 @@ class IndexedBlobBuilder { _finished = true; await _blob.close(); + final bytes = _buildIndexBytes( + blobId: blobId, + index: _index, + ); + return BlobIndex.fromBytes(bytes); + } + + static Uint8List _buildIndexBytes({ + required String blobId, + required Map index, + }) { final out = StringBuffer(); final w = jsonStringWriter(out); w.startObject(); @@ -143,13 +154,11 @@ class IndexedBlobBuilder { // Add index w.addKey('index'); - _addMap(w, _index); + _addMap(w, index); w.endObject(); - final bytes = utf8.encoder.convert(out.toString()); - - return BlobIndex.fromBytes(bytes); + return utf8.encoder.convert(out.toString()); } } @@ -232,6 +241,10 @@ class BlobIndex { : _indexFile = indexFile is Uint8List ? indexFile : Uint8List.fromList(indexFile); + BlobIndex.empty({required String blobId}) + : _indexFile = + IndexedBlobBuilder._buildIndexBytes(blobId: blobId, index: {}); + /// Get the free-form [String] given as `blobId` when the blob was built. /// /// This is intended for an identifier or URL that can be used to find the