• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2024 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 distributedKVStore from '@ohos.data.distributedKVStore';
17import relationalStore from '@ohos.data.relationalStore';
18import { BusinessError } from '@ohos.base';
19import startupManager from '@ohos.app.appstartup.startupManager';
20
21export class SingleKVStoreUtil {
22  static getKVStore(): Promise<distributedKVStore.SingleKVStore> {
23    // 返回单例对象
24    return new Promise((resolve, reject) => {
25      const options: distributedKVStore.Options = {
26        createIfMissing: true,
27        encrypt: false,
28        backup: false,
29        autoSync: false,
30        // kvStoreType不填时,默认创建多设备协同数据库
31        kvStoreType: distributedKVStore.KVStoreType.SINGLE_VERSION,
32        // 多设备协同数据库:kvStoreType: distributedKVStore.KVStoreType.DEVICE_COLLABORATION,
33        securityLevel: distributedKVStore.SecurityLevel.S1
34      };
35
36      let kvManagerUtilTask = startupManager.getStartupTaskResult('KvManagerUtilTask') as distributedKVStore.KVManager
37      kvManagerUtilTask.getKVStore<distributedKVStore.SingleKVStore>('storeId', options,
38        (err, store: distributedKVStore.SingleKVStore) => {
39          if (err) {
40            console.error(`Failed to get KVStore: Code:${err.code},message:${err.message}`);
41            reject(`Failed to get KVStore: Code:${err.code},message:${err.message}`)
42            return;
43          }
44          // 请确保获取到键值数据库实例后,再进行相关数据操作
45          if (store == undefined) {
46            reject('store == undefined');
47            return;
48          }
49          resolve(store);
50        });
51    })
52  }
53}
54
55export class KvManagerUtil {
56  private static kvManager: distributedKVStore.KVManager | undefined = undefined;
57
58  private constructor() {
59  }
60
61  public static getInstance(context) {
62    // 返回单例对象
63    if (this.kvManager == undefined) {
64      const kvManagerConfig: distributedKVStore.KVManagerConfig = {
65        context: context,
66        bundleName: 'com.samples.appstartup'
67      };
68      // 创建KVManager实例
69      this.kvManager = distributedKVStore.createKVManager(kvManagerConfig);
70    }
71    return this.kvManager;
72  }
73}
74
75export class RdbStoreUtil {
76  static getRdbStore(context): Promise<relationalStore.RdbStore> {
77    return new Promise((resolve, reject) => {
78      const STORE_CONFIG: relationalStore.StoreConfig = {
79        name: "RdbTest.db",
80        securityLevel: relationalStore.SecurityLevel.S1
81      };
82
83      relationalStore.getRdbStore(context, STORE_CONFIG, (err: BusinessError, rdbStore: relationalStore.RdbStore) => {
84        if (err) {
85          reject('rdbStore == undefined');
86          console.error(`Get RdbStore failed, code is ${err.code},message is ${err.message}`);
87          return;
88        }
89        resolve(rdbStore);
90        console.info('Get RdbStore successfully.');
91      })
92    });
93
94  }
95}
96
97export class TimeUtils {
98  static startTime: number = 0;
99  static endTime: number = 0;
100
101  static countTime(): number {
102    return this.endTime - this.startTime;
103  }
104}