• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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): Promise<any> {
47    return this.indexedDBHelp.get(this.tableName, range, 'QueryFileByPage');
48  }
49
50  addLongTableData(data: ArrayBuffer, fileType: string, timStamp: number, pageNumber: number, index: number, offset: number, sliceLen: number): Promise<any> {
51    return this.indexedDBHelp.add(this.tableName, {
52      buf: data,
53      id: `${fileType}_${timStamp}_${pageNumber}_${index}`,
54      fileType: fileType,
55      pageNum: pageNumber,
56      startOffset: offset,
57      endOffset: offset + sliceLen,
58      index: index,
59      timStamp: timStamp,
60    })
61  }
62}
63