MCPcopy Create free account
hub / github.com/hydro-dev/Hydro / cacheOpen

Method cacheOpen

packages/hydrojudge/src/task.ts:118–168  ·  view source on GitHub ↗
(source: string, files: FileInfo[])

Source from the content-addressed store, hash-verified

116 }
117
118 async cacheOpen(source: string, files: FileInfo[]) {
119 // Backward compatibility for vj4
120 if ((this.session as any).cacheOpen) return (this.session as any).cacheOpen(source, files);
121 using span = this.startChildSpan('judge.cacheOpen', { source, fileCount: files?.length || 0 });
122 const host = this.session.config?.host || 'local';
123 const filePath = join(getConfig('cache_dir'), host, source);
124 await Lock.acquire(filePath);
125 try {
126 await fs.ensureDir(filePath);
127 if (!files?.length) throw new FormatError('Problem data not found.');
128 let etags: Record<string, string> = {};
129 try {
130 etags = JSON.parse(await fs.readFile(join(filePath, 'etags'), 'utf-8'));
131 } catch (e) { /* ignore */ }
132 this.compileCache = etags['*cache'] as any || {};
133 delete etags['*cache'];
134 const version = {};
135 const filenames = [];
136 const allFiles = new Set<string>();
137 for (const file of files) {
138 allFiles.add(file.name);
139 version[file.name] = file.etag + file.lastModified;
140 if (etags[file.name] !== file.etag + file.lastModified) filenames.push(file.name);
141 }
142 const allFilesToRemove = Object.keys(etags).filter((name) => !allFiles.has(name) && fs.existsSync(join(filePath, name)));
143 await Promise.all(allFilesToRemove.map((name) => fs.remove(join(filePath, name))));
144 if (filenames.length) {
145 span.setAttribute('files', filenames);
146 logger.info(`Getting problem data: ${this.session?.config.host || 'local'}/${source}`);
147 this.next({ message: 'Syncing testdata, please wait...' });
148 this.mainContext = trace.setSpan(context.active(), span);
149 await this.session.fetchFile(source, Object.fromEntries(
150 files.filter((i) => filenames.includes(i.name))
151 .map((i) => [i.name, join(filePath, i.name)]),
152 ), this);
153 this.compileCache = {};
154 }
155 if (allFilesToRemove.length || filenames.length) {
156 await fs.writeFile(join(filePath, 'etags'), JSON.stringify(version));
157 }
158 await fs.writeFile(join(filePath, 'lastUsage'), Date.now().toString());
159 return filePath;
160 } catch (e) {
161 span.recordException(e);
162 logger.warn('CacheOpen Fail: %s %o %o', source, files, e);
163 throw e;
164 } finally {
165 this.mainContext = trace.setSpan(context.active(), this.span);
166 Lock.release(filePath);
167 }
168 }
169
170 async doSubmission() {
171 this.folder = await this.cacheOpen(this.source, this.data);

Callers 1

doSubmissionMethod · 0.95

Calls 9

startChildSpanMethod · 0.95
getConfigFunction · 0.90
addMethod · 0.45
pushMethod · 0.45
allMethod · 0.45
infoMethod · 0.45
nextMethod · 0.45
fetchFileMethod · 0.45
warnMethod · 0.45

Tested by

no test coverage detected