1/*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import { IndexedDBHelp } from './IndexedDBHelp';
17
18export class LongTraceDBUtils {
19  public static instance: LongTraceDBUtils | undefined;
20  dbVersion: number = 1;
21  dbName: string = 'sp';
22  fileType: string = 'trace';
23  tableName: string = 'longTable';
24  indexedDBHelp: IndexedDBHelp = new IndexedDBHelp();
25
26  public static getInstance(): LongTraceDBUtils {
27    if (!this.instance) {
28      this.instance = new LongTraceDBUtils();
29    }
30    return this.instance;
31  }
32
33  createDBAndTable(): Promise<IDBDatabase> {
34    return this.indexedDBHelp.open(this.dbName, this.dbVersion, [
35      {
36        name: this.tableName,
37        objectStoreParameters: { keyPath: 'id' },
38        dataItems: [
39          { name: 'QueryCompleteFile', keypath: ['timStamp', 'fileType', 'pageNum', 'index'] },
40          { name: 'QueryFileByPage', keypath: ['timStamp', 'fileType', 'pageNum'] },
41        ],
42      },
43    ]);
44  }
45
46  getByRange(range: IDBKeyRange): // @ts-ignore
47  Promise<unknown> {
48    return this.indexedDBHelp.get(this.tableName, range, 'QueryFileByPage');
49  }
50
51  addLongTableData(
52    data: ArrayBuffer,
53    fileType: string,
54    timStamp: number,
55    pageNumber: number,
56    index: number,
57    offset: number,
58    sliceLen: number
59  ): // @ts-ignore
60  Promise<unknown> {
61    return this.indexedDBHelp.add(this.tableName, {
62      buf: data,
63      id: `${fileType}_${timStamp}_${pageNumber}_${index}`,
64      fileType: fileType,
65      pageNum: pageNumber,
66      startOffset: offset,
67      endOffset: offset + sliceLen,
68      index: index,
69      timStamp: timStamp,
70    });
71  }
72}
73