• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 
16 #include "sqlite_global_config.h"
17 
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 
21 #include "logger.h"
22 #include "sqlite3sym.h"
23 
24 namespace OHOS {
25 namespace NativeRdb {
26 const int SqliteGlobalConfig::SOFT_HEAP_LIMIT = 8 * 1024 * 1024; /* 8MB */
27 const bool SqliteGlobalConfig::CALLBACK_LOG_SWITCH = true;       /* Sqlite callback log switch */
28 const int SqliteGlobalConfig::CONNECTION_POOL_SIZE = 4;
29 const std::string SqliteGlobalConfig::MEMORY_DB_PATH = ":memory:";
30 const int SqliteGlobalConfig::DB_PAGE_SIZE = 4096;
31 const std::string SqliteGlobalConfig::DEFAULT_JOURNAL_MODE = "WAL";
32 const std::string SqliteGlobalConfig::WAL_SYNC_MODE = "FULL";
33 const int SqliteGlobalConfig::JOURNAL_FILE_SIZE = 524288; /* 512KB */
34 const int SqliteGlobalConfig::WAL_AUTO_CHECKPOINT = 100;  /* 100 pages */
35 constexpr int APP_DEFAULT_UMASK = 0002;
36 
InitSqliteGlobalConfig()37 void SqliteGlobalConfig::InitSqliteGlobalConfig()
38 {
39     static SqliteGlobalConfig globalConfig;
40 }
41 
SqliteGlobalConfig()42 SqliteGlobalConfig::SqliteGlobalConfig()
43 {
44     umask(APP_DEFAULT_UMASK);
45 
46     sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
47 
48     sqlite3_config(SQLITE_CONFIG_LOG, &SqliteLogCallback, CALLBACK_LOG_SWITCH ? reinterpret_cast<void *>(1) : NULL);
49 
50     sqlite3_soft_heap_limit(SOFT_HEAP_LIMIT);
51 
52     sqlite3_initialize();
53 }
54 
~SqliteGlobalConfig()55 SqliteGlobalConfig::~SqliteGlobalConfig()
56 {
57 }
58 
SqliteLogCallback(const void * data,int err,const char * msg)59 void SqliteGlobalConfig::SqliteLogCallback(const void *data, int err, const char *msg)
60 {
61     bool verboseLog = (data != nullptr);
62     auto errType = static_cast<unsigned int>(err);
63     errType &= 0xFF;
64     if (errType == 0 || errType == SQLITE_CONSTRAINT || errType == SQLITE_SCHEMA || errType == SQLITE_NOTICE
65         || err == SQLITE_WARNING_AUTOINDEX) {
66         if (verboseLog) {
67             LOG_INFO("SQLite Error(%{public}d) %{private}s ", err, msg);
68         }
69     } else {
70         LOG_ERROR("SQLite Error(%{public}d) %{private}s", err, msg);
71     }
72 }
73 
GetReadConnectionCount()74 int SqliteGlobalConfig::GetReadConnectionCount()
75 {
76     return CONNECTION_POOL_SIZE - 1;
77 }
78 
GetMemoryDbPath()79 std::string SqliteGlobalConfig::GetMemoryDbPath()
80 {
81     return MEMORY_DB_PATH;
82 }
83 
GetPageSize()84 int SqliteGlobalConfig::GetPageSize()
85 {
86     return DB_PAGE_SIZE;
87 }
88 
GetWalSyncMode()89 std::string SqliteGlobalConfig::GetWalSyncMode()
90 {
91     return WAL_SYNC_MODE;
92 }
93 
GetJournalFileSize()94 int SqliteGlobalConfig::GetJournalFileSize()
95 {
96     return JOURNAL_FILE_SIZE;
97 }
98 
GetWalAutoCheckpoint()99 int SqliteGlobalConfig::GetWalAutoCheckpoint()
100 {
101     return WAL_AUTO_CHECKPOINT;
102 }
103 
GetDefaultJournalMode()104 std::string SqliteGlobalConfig::GetDefaultJournalMode()
105 {
106     return DEFAULT_JOURNAL_MODE;
107 }
108 } // namespace NativeRdb
109 } // namespace OHOS
110