Skip to content
Snippets Groups Projects
Unverified Commit 57ade2b3 authored by Sergei Zharinov's avatar Sergei Zharinov Committed by GitHub
Browse files

refactor(cache): Restore each revision with own separate method (#17364)

parent 9e2c81f0
No related branches found
No related tags found
No related merge requests found
...@@ -10,7 +10,14 @@ import { ...@@ -10,7 +10,14 @@ import {
isValidRev11, isValidRev11,
isValidRev12, isValidRev12,
} from '../common'; } from '../common';
import type { RepoCache, RepoCacheData, RepoCacheRecord } from '../types'; import type {
RepoCache,
RepoCacheData,
RepoCacheRecord,
RepoCacheRecordV10,
RepoCacheRecordV11,
RepoCacheRecordV12,
} from '../types';
const compress = promisify(zlib.brotliCompress); const compress = promisify(zlib.brotliCompress);
const decompress = promisify(zlib.brotliDecompress); const decompress = promisify(zlib.brotliDecompress);
...@@ -26,6 +33,24 @@ export abstract class RepoCacheBase implements RepoCache { ...@@ -26,6 +33,24 @@ export abstract class RepoCacheBase implements RepoCache {
protected abstract write(data: RepoCacheRecord): Promise<void>; protected abstract write(data: RepoCacheRecord): Promise<void>;
private async restoreFromRev12(oldCache: RepoCacheRecordV12): Promise<void> {
const compressed = Buffer.from(oldCache.payload, 'base64');
const uncompressed = await decompress(compressed);
const jsonStr = uncompressed.toString('utf8');
this.data = JSON.parse(jsonStr);
this.oldHash = oldCache.hash;
}
private restoreFromRev11(oldCache: RepoCacheRecordV11): void {
this.data = oldCache.data;
}
private restoreFromRev10(oldCache: RepoCacheRecordV10): void {
delete oldCache.repository;
delete oldCache.revision;
this.data = oldCache;
}
async load(): Promise<void> { async load(): Promise<void> {
try { try {
const data = await this.read(); const data = await this.read();
...@@ -35,29 +60,23 @@ export abstract class RepoCacheBase implements RepoCache { ...@@ -35,29 +60,23 @@ export abstract class RepoCacheBase implements RepoCache {
); );
return; return;
} }
const oldCache = JSON.parse(data); const oldCache = JSON.parse(data) as unknown;
if (isValidRev12(oldCache, this.repository)) { if (isValidRev12(oldCache, this.repository)) {
const compressed = Buffer.from(oldCache.payload, 'base64'); await this.restoreFromRev12(oldCache);
const uncompressed = await decompress(compressed); logger.debug('Repository cache is restored from revision 12');
const jsonStr = uncompressed.toString('utf8');
this.data = JSON.parse(jsonStr);
this.oldHash = oldCache.hash;
logger.debug('Repository cache is valid');
return; return;
} }
if (isValidRev11(oldCache, this.repository)) { if (isValidRev11(oldCache, this.repository)) {
this.data = oldCache.data; this.restoreFromRev11(oldCache);
logger.debug('Repository cache is migrated from 11 revision'); logger.debug('Repository cache is restored from revision 11');
return; return;
} }
if (isValidRev10(oldCache, this.repository)) { if (isValidRev10(oldCache, this.repository)) {
delete oldCache.repository; this.restoreFromRev10(oldCache);
delete oldCache.revision; logger.debug('Repository cache is restored from revision 10');
this.data = oldCache;
logger.debug('Repository cache is migrated from 10 revision');
return; return;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment