Skip to content

Commit 1d6b190

Browse files
committed
fix cache clean
1 parent 83d9e35 commit 1d6b190

8 files changed

Lines changed: 93 additions & 5 deletions

File tree

src/main/webapp/app/dbsqlite.worker.ts

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { map } from 'rxjs/operators';
12
/* eslint-disable no-console */
23
/* eslint-disable spaced-comment */
34
/* eslint-disable @typescript-eslint/explicit-function-return-type */
@@ -128,6 +129,12 @@ addEventListener('message', e => {
128129
db1.addNonAligneImage(_sqlite3, e.data);
129130
break;
130131
}
132+
case 'cleanOutDatedCached': {
133+
console.error('cleanOutDatedCached', e.data);
134+
cleanOutdatedCache(e.data);
135+
136+
break;
137+
}
131138

132139
case 'resetDatabase': {
133140
let db1 = dbs.get(e.data.payload.examId);
@@ -480,6 +487,32 @@ addEventListener('message', e => {
480487
}
481488
});
482489

490+
async function cleanOutdatedCache(data: any) {
491+
try {
492+
console.error('cleanOutdatedCache for ', data);
493+
494+
const examIds = data.payload.examIds;
495+
const cacheNames: string[] = examIds.map((id: number) => '' + id + '.sqlite3');
496+
const cacheJournalNames = examIds.map((id: number) => '' + id + '.sqlite3-journal');
497+
cacheNames.push(...cacheJournalNames);
498+
if (navigator.storage.getDirectory !== undefined) {
499+
const root = await navigator.storage.getDirectory();
500+
const keys = (root as any).keys();
501+
let next = await keys.next();
502+
while (next.value) {
503+
if (next.value.endsWith('.sqlite3') || (next.value.endsWith('.sqlite3-journal') && !cacheNames.includes(next.value))) {
504+
await root.removeEntry(next.value);
505+
}
506+
next = await keys.next();
507+
}
508+
}
509+
} finally {
510+
postMessage({
511+
msg: data.msg,
512+
uid: data.uid,
513+
});
514+
}
515+
}
483516
/*
484517
addTemplate(elt: AlignImage ): Promise<number>;
485518
countNonAlignWithPageNumber(examId:number, pageInscan:number): Promise<number>;
@@ -623,9 +656,9 @@ class DB {
623656
const keys = (root as any).keys();
624657
let next = await keys.next();
625658
while (next.value !== undefined) {
626-
if (next.value.endsWith('.sqlite3')) {
659+
if (next.value === this.examName + '.sqlite3') {
627660
await root.removeEntry(next.value);
628-
} else if (next.value.endsWith('sqlite3-journal')) {
661+
} else if (next.value === this.examName + 'sqlite3-journal') {
629662
await root.removeEntry(next.value);
630663
}
631664
next = await keys.next();

src/main/webapp/app/home/home.component.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ import { DrawerModule } from 'primeng/drawer';
2626
import { HasAnyAuthorityDirective } from '../shared/auth/has-any-authority.directive';
2727

2828
import { ButtonModule } from 'primeng/button';
29+
import { ExamService } from 'app/entities/exam/service/exam.service';
30+
import { CacheService } from '../scanexam/db/db';
31+
import { CacheServiceImpl } from 'app/scanexam/db/CacheServiceImpl';
2932

3033
interface Upload {
3134
progress: number;
@@ -117,6 +120,8 @@ export class HomeComponent implements OnInit, OnDestroy {
117120
private loginService: LoginService,
118121
private zone: NgZone,
119122
private http: HttpClient,
123+
private examService: ExamService,
124+
private db: CacheServiceImpl,
120125
) {}
121126

122127
ngOnInit(): void {
@@ -153,7 +158,19 @@ export class HomeComponent implements OnInit, OnDestroy {
153158
this.accountService
154159
.getAuthenticationState()
155160
.pipe(takeUntil(this.destroy$))
156-
.subscribe(account => (this.account = account));
161+
.subscribe(account => {
162+
this.account = account;
163+
if (account) {
164+
this.examService.query().subscribe(res => {
165+
if (res.body) {
166+
const examIds = res.body.map(exam => (exam.id ? exam.id : 0));
167+
this.db.cleanOutDatedCached(examIds).then(() => {
168+
console.log('cleanOutDatedCached done');
169+
});
170+
}
171+
});
172+
}
173+
});
157174

158175
this.translateService.get('home.creercours').subscribe(() => {
159176
this.initCmpt();

src/main/webapp/app/scanexam/coursdetail/coursdetails.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,10 @@ export class CoursdetailsComponent implements OnInit {
302302
message: data,
303303
accept: () => {
304304
if (this.course !== undefined) {
305+
this.blocked = true;
305306
this.courseService.delete(this.course.id!).subscribe(e => {
307+
this.blocked = false;
308+
306309
this.router.navigateByUrl('/');
307310
});
308311
}

src/main/webapp/app/scanexam/db/CacheServiceImpl.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ export class CacheServiceImpl implements CacheService {
1717
(this.service as SqliteCacheService).load();
1818
}
1919
}
20+
21+
cleanOutDatedCached(examIds: number[]): Promise<void> {
22+
return this.service.cleanOutDatedCached(examIds);
23+
}
24+
2025
getNonAlignImagesForPageNumbers(examId: number, pages: number[]): Promise<ImageDB[]> {
2126
return this.service.getNonAlignImagesForPageNumbers(examId, pages);
2227
}

src/main/webapp/app/scanexam/db/db.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ export interface NonAlignImage {
3434
value: string;
3535
}
3636

37+
export async function cleanOutDatedCached(examIds: number[]) {
38+
const databasesNamedToKeep = examIds.map(id => 'correctExam' + id);
39+
const databases = await Dexie.getDatabaseNames();
40+
const databasesToRemove = databases.filter(dbname => dbname.startsWith('correctExam') && !databasesNamedToKeep.includes(dbname));
41+
for (let i = 0; i < databasesToRemove.length; i++) {
42+
try {
43+
await Dexie.delete(databasesToRemove[i]);
44+
// eslint-disable-next-line no-console
45+
console.log('remove old database', databasesToRemove[i]);
46+
} catch (e) {
47+
console.error('unable to remove old database', databasesToRemove[i], e);
48+
}
49+
}
50+
}
51+
3752
export class ExamIndexDB extends Dexie {
3853
private exams!: Table<Exam, number>;
3954
private templates!: Table<Template, number>;
@@ -393,6 +408,10 @@ export class AppDB implements CacheService {
393408
/* async populate() {
394409
} */
395410

411+
cleanOutDatedCached(examIds: number[]): Promise<void> {
412+
return cleanOutDatedCached(examIds);
413+
}
414+
396415
async resetDatabase(examId: number): Promise<void> {
397416
let db1 = this.dbs.get(examId);
398417
if (db1 === undefined) {
@@ -730,6 +749,7 @@ export class AppDB implements CacheService {
730749
}
731750

732751
export interface CacheService {
752+
cleanOutDatedCached(examIds: number[]): Promise<void>;
733753
resetDatabase(examId: number): Promise<void>;
734754
removeExam(examId: number): Promise<void>;
735755
removeElementForExam(examId: number): Promise<void>;

src/main/webapp/app/scanexam/db/dbsqlite.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ export class SqliteCacheService implements CacheService {
201201
workersqllite.postMessage({ msg: 'load', uid: '0' });
202202
}
203203

204+
async cleanOutDatedCached(examIds: number[]) {
205+
const el1 = {
206+
examIds: examIds,
207+
};
208+
console.error('cleanOutDatedCached', el1);
209+
return this._dispatch<void>('cleanOutDatedCached', el1).toPromise();
210+
}
211+
204212
async addAligneImage(elt: ImageDB) {
205213
const enc = new TextEncoder(); // always utf-8
206214
const el1 = {

src/main/webapp/app/scanexam/exam-detail/cacheUpload.service.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ export class CacheUploadService {
316316
let data: Blob = (await firstValueFrom(this.getCache(examId + 'indexdb.json'))) as Blob;
317317
if (data.size === 0) {
318318
data = await firstValueFrom(this.getCache(examId + '_exam_template_indexdb.json'));
319-
319+
console.error('data', data, showFailMessage);
320320
if (data.size === 0) {
321321
if (showFailMessage) {
322322
messageService.add({

src/main/webapp/app/scanexam/exam-detail/exam-detail.component.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ export class ExamDetailComponent implements OnInit, CacheUploadNotification, Cac
190190
});
191191
} else {
192192
if (this.exam?.scanfileId) {
193-
this.cacheUploadService.importCache(+this.examId, this.translateService, this.messageService, this, false);
193+
this.cacheUploadService.importCache(+this.examId, this.translateService, this.messageService, this, true);
194194
} else {
195195
this.blocked = false;
196196
}
@@ -309,10 +309,12 @@ export class ExamDetailComponent implements OnInit, CacheUploadNotification, Cac
309309
this.confirmationService.confirm({
310310
message: data,
311311
accept: () => {
312+
this.blocked = true;
312313
this.examService.delete(this.exam!.id!).subscribe(() => {
313314
this.db
314315
.removeElementForExam(+this.examId)
315316
.then(() => {
317+
this.blocked = false;
316318
this.router.navigateByUrl('/course/' + this.course!.id);
317319
})
318320
.catch(() => {

0 commit comments

Comments
 (0)