Skip to content

Commit

Permalink
_BytesAndHash with lazy md5 hash calculation.
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed Oct 20, 2023
1 parent 42e67a1 commit 1d9991b
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions app/lib/package/export_api_to_bucket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ class ApiExporter {
/// Gets and uploads the package name completion data.
Future<void> uploadPkgNameCompletionData() async {
final bytes = await searchBackend.getPackageNameCompletitionDataJsonGz();
final bytesAndHash = _BytesAndHash(bytes);
for (final objectName in _apiPkgNameCompletitionDataNames()) {
if (await _isSameContent(objectName, bytes)) {
if (await _isSameContent(objectName, bytesAndHash)) {
continue;
}
await uploadWithRetry(
Expand Down Expand Up @@ -187,18 +188,19 @@ class ApiExporter {
Future<void> _uploadPackageToBucket(String package) async {
final data = await retry(() => packageBackend.listVersions(package));
final rawBytes = jsonUtf8Encoder.convert(data.toJson());
final gzippedBytes = gzip.encode(rawBytes);
final bytes = gzip.encode(rawBytes);
final bytesAndHash = _BytesAndHash(bytes);

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

await uploadWithRetry(
_bucket,
objectName,
gzippedBytes.length,
() => Stream.value(gzippedBytes),
bytes.length,
() => Stream.value(bytes),
metadata: ObjectMetadata(
contentType: 'application/json; charset="utf-8"',
contentEncoding: 'gzip',
Expand Down Expand Up @@ -263,22 +265,22 @@ class ApiExporter {
}
}

Future<bool> _isSameContent(String objectName, List<int> bytes) async {
Future<bool> _isSameContent(
String objectName, _BytesAndHash bytesAndHash) async {
final info = await _bucket.tryInfo(objectName);
if (info == null) {
return false;
}
if (info.length != bytes.length) {
if (info.length != bytesAndHash.length) {
return false;
}
final md5Hash = md5.convert(bytes).bytes;
if (md5Hash.length != info.md5Hash.length) {
if (bytesAndHash.md5Hash.length != info.md5Hash.length) {
return false;
}
// making sure the timing is fixed
var isSame = true;
for (var i = 0; i < md5Hash.length; i++) {
if (md5Hash[i] != info.md5Hash[i]) {
for (var i = 0; i < bytesAndHash.md5Hash.length; i++) {
if (bytesAndHash.md5Hash[i] != info.md5Hash[i]) {
isSame = false;
}
}
Expand All @@ -297,3 +299,12 @@ extension on Package {
_PkgUpdatedEvent asPkgUpdatedEvent() =>
(package: name!, updated: updated!, isVisible: isVisible);
}

class _BytesAndHash {
final List<int> bytes;

_BytesAndHash(this.bytes);

late final length = bytes.length;
late final md5Hash = md5.convert(bytes).bytes;
}

0 comments on commit 1d9991b

Please sign in to comment.