Skip to content

Commit

Permalink
Refactor API exporter 'upsert'. (#7257)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos authored Dec 6, 2023
1 parent df1828b commit 511db4c
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions app/lib/package/export_api_to_bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,8 @@ class ApiExporter {
final bytes = await searchBackend.getPackageNameCompletitionDataJsonGz();
final bytesAndHash = _BytesAndHash(bytes);
for (final objectName in _apiPkgNameCompletitionDataNames()) {
if (await _isSameContent(objectName, bytesAndHash)) {
continue;
}
await uploadWithRetry(
_bucket,
objectName,
bytes.length,
() => Stream.value(bytes),
metadata: ObjectMetadata(
contentType: 'application/json; charset="utf-8"',
contentEncoding: 'gzip',
cacheControl:
'public, max-age=${_pkgNameCompletitionDataMaxAge.inSeconds}',
),
);
await _upsert(objectName, bytesAndHash,
maxAge: _pkgNameCompletitionDataMaxAge);
}
}

Expand Down Expand Up @@ -200,21 +187,7 @@ class ApiExporter {
final bytesAndHash = _BytesAndHash(bytes);

for (final objectName in _apiPkgObjectNames(package)) {
if (await _isSameContent(objectName, bytesAndHash)) {
continue;
}

await uploadWithRetry(
_bucket,
objectName,
bytes.length,
() => Stream.value(bytes),
metadata: ObjectMetadata(
contentType: 'application/json; charset="utf-8"',
contentEncoding: 'gzip',
cacheControl: 'public, max-age=${_pkgApiMaxCacheAge.inSeconds}',
),
);
await _upsert(objectName, bytesAndHash, maxAge: _pkgApiMaxCacheAge);
}
}

Expand Down Expand Up @@ -273,6 +246,27 @@ class ApiExporter {
}
}

Future<void> _upsert(
String objectName,
_BytesAndHash bytesAndHash, {
required Duration maxAge,
}) async {
if (await _isSameContent(objectName, bytesAndHash)) {
return;
}
await uploadWithRetry(
_bucket,
objectName,
bytesAndHash.bytes.length,
() => Stream.value(bytesAndHash.bytes),
metadata: ObjectMetadata(
contentType: 'application/json; charset="utf-8"',
contentEncoding: 'gzip',
cacheControl: 'public, max-age=${maxAge.inSeconds}',
),
);
}

Future<bool> _isSameContent(
String objectName, _BytesAndHash bytesAndHash) async {
final info = await _bucket.tryInfo(objectName);
Expand Down

0 comments on commit 511db4c

Please sign in to comment.