Skip to content

Commit

Permalink
Create empty blob index in cache when task index is missing. (#6811)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Jul 6, 2023
1 parent cf9016f commit dc77612
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/lib/task/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 17 additions & 4 deletions pkg/indexed_blob/lib/indexed_blob.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> index,
}) {
final out = StringBuffer();
final w = jsonStringWriter(out);
w.startObject();
Expand All @@ -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());
}
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dc77612

Please sign in to comment.