• 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 #define LOG_TAG "RdbStoreConfig"
16 #include "rdb_store_config.h"
17 
18 #include <sstream>
19 #include <mutex>
20 
21 #include "logger.h"
22 #include "rdb_errno.h"
23 #include "rdb_security_manager.h"
24 #include "string_utils.h"
25 #include "sqlite_global_config.h"
26 #include "sqlite_utils.h"
27 #include "rdb_fault_hiview_reporter.h"
28 
29 namespace OHOS::NativeRdb {
30 using namespace OHOS::Rdb;
31 // this is modifications to be made in the future.
32 static std::mutex g_mutex;
33 
RdbStoreConfig(const std::string & name,StorageMode storageMode,bool isReadOnly,const std::vector<uint8_t> & encryptKey,const std::string & journalMode,const std::string & syncMode,const std::string & databaseFileType,SecurityLevel securityLevel,bool isCreateNecessary,bool autoCheck,int journalSize,int pageSize)34 RdbStoreConfig::RdbStoreConfig(const std::string &name, StorageMode storageMode, bool isReadOnly,
35     const std::vector<uint8_t> &encryptKey, const std::string &journalMode, const std::string &syncMode,
36     const std::string &databaseFileType, SecurityLevel securityLevel, bool isCreateNecessary, bool autoCheck,
37     int journalSize, int pageSize)
38     : readOnly_(isReadOnly), isCreateNecessary_(isCreateNecessary), autoCheck_(autoCheck), journalSize_(journalSize),
39       pageSize_(pageSize), securityLevel_(securityLevel), storageMode_(storageMode), path_(name),
40       journalMode_(journalMode), syncMode_(syncMode), databaseFileType(databaseFileType)
41 {
42     name_ = StringUtils::ExtractFileName(name);
43     cryptoParam_.encryptKey_ = encryptKey;
44     walLimitSize_ = GlobalExpr::DB_WAL_DEFAULT_SIZE;
45     checkpointSize_ = GlobalExpr::DB_WAL_WARNING_SIZE;
46     startCheckpointSize_ = GlobalExpr::DB_WAL_SIZE_LIMIT_MIN;
47     clearMemorySize_ = GlobalExpr::CLEAR_MEMORY_SIZE;
48 }
49 
~RdbStoreConfig()50 RdbStoreConfig::~RdbStoreConfig()
51 {
52     ClearEncryptKey();
53 }
54 
55 /**
56  * Obtains the database name.
57  */
GetName() const58 std::string RdbStoreConfig::GetName() const
59 {
60     return name_;
61 }
62 
63 /**
64  * Obtains the database path.
65  */
GetPath() const66 std::string RdbStoreConfig::GetPath() const
67 {
68     return path_;
69 }
70 
71 /**
72  * Obtains the storage mode.
73  */
GetStorageMode() const74 StorageMode RdbStoreConfig::GetStorageMode() const
75 {
76     return storageMode_;
77 }
78 
79 /**
80  * Obtains the journal mode in this {@code StoreConfig} object.
81  */
GetJournalMode() const82 std::string RdbStoreConfig::GetJournalMode() const
83 {
84     return journalMode_;
85 }
86 
87 /**
88  * Obtains the synchronization mode in this {@code StoreConfig} object.
89  */
GetSyncMode() const90 std::string RdbStoreConfig::GetSyncMode() const
91 {
92     return syncMode_;
93 }
94 
95 /**
96  * Checks whether the database is read-only.
97  */
IsReadOnly() const98 bool RdbStoreConfig::IsReadOnly() const
99 {
100     return readOnly_;
101 }
102 
103 /**
104  * Checks whether the database is memory.
105  */
IsMemoryRdb() const106 bool RdbStoreConfig::IsMemoryRdb() const
107 {
108     return GetStorageMode() == StorageMode::MODE_MEMORY;
109 }
110 
111 /**
112  * Obtains the database file type in this {@code StoreConfig} object.
113  */
GetDatabaseFileType() const114 std::string RdbStoreConfig::GetDatabaseFileType() const
115 {
116     return databaseFileType;
117 }
118 
SetName(std::string name)119 void RdbStoreConfig::SetName(std::string name)
120 {
121     this->name_ = std::move(name);
122 }
123 
124 /**
125  * Sets the journal mode  for the object.
126  */
SetJournalMode(JournalMode journalMode)127 void RdbStoreConfig::SetJournalMode(JournalMode journalMode)
128 {
129     this->journalMode_ = GetJournalModeValue(journalMode);
130 }
131 
SetJournalMode(const std::string & journalMode)132 void RdbStoreConfig::SetJournalMode(const std::string &journalMode)
133 {
134     this->journalMode_ = journalMode;
135 }
136 
SetDatabaseFileType(DatabaseFileType type)137 void RdbStoreConfig::SetDatabaseFileType(DatabaseFileType type)
138 {
139     this->databaseFileType = GetDatabaseFileTypeValue(type);
140 }
141 
142 /**
143  * Sets the path  for the object.
144  */
SetPath(std::string path)145 void RdbStoreConfig::SetPath(std::string path)
146 {
147     this->path_ = std::move(path);
148 }
149 
SetStorageMode(StorageMode storageMode)150 void RdbStoreConfig::SetStorageMode(StorageMode storageMode)
151 {
152     this->storageMode_ = storageMode;
153 }
154 
IsAutoCheck() const155 bool RdbStoreConfig::IsAutoCheck() const
156 {
157     return autoCheck_;
158 }
SetAutoCheck(bool autoCheck)159 void RdbStoreConfig::SetAutoCheck(bool autoCheck)
160 {
161     this->autoCheck_ = autoCheck;
162 }
GetJournalSize() const163 int RdbStoreConfig::GetJournalSize() const
164 {
165     return journalSize_;
166 }
SetJournalSize(int journalSize)167 void RdbStoreConfig::SetJournalSize(int journalSize)
168 {
169     this->journalSize_ = journalSize;
170 }
GetPageSize() const171 int RdbStoreConfig::GetPageSize() const
172 {
173     return pageSize_;
174 }
SetPageSize(int pageSize)175 void RdbStoreConfig::SetPageSize(int pageSize)
176 {
177     this->pageSize_ = pageSize;
178 }
GetEncryptAlgo() const179 EncryptAlgo RdbStoreConfig::GetEncryptAlgo() const
180 {
181     return static_cast<EncryptAlgo>(cryptoParam_.encryptAlgo);
182 }
SetEncryptAlgo(EncryptAlgo encryptAlgo)183 void RdbStoreConfig::SetEncryptAlgo(EncryptAlgo encryptAlgo)
184 {
185     this->cryptoParam_.encryptAlgo = static_cast<int32_t>(encryptAlgo);
186 }
187 
SetReadOnly(bool readOnly)188 void RdbStoreConfig::SetReadOnly(bool readOnly)
189 {
190     this->readOnly_ = readOnly;
191 }
192 
SetDistributedType(DistributedType type)193 int RdbStoreConfig::SetDistributedType(DistributedType type)
194 {
195     if (type != DistributedType::RDB_DEVICE_COLLABORATION) {
196         LOG_ERROR("type is invalid.");
197         return E_ERROR;
198     }
199     distributedType_ = type;
200     return E_OK;
201 }
202 
GetDistributedType() const203 DistributedType RdbStoreConfig::GetDistributedType() const
204 {
205     return distributedType_;
206 }
207 
SetBundleName(const std::string & bundleName)208 int RdbStoreConfig::SetBundleName(const std::string &bundleName)
209 {
210     if (bundleName.empty()) {
211         LOG_ERROR("bundleName is empty.");
212         return E_ERROR;
213     }
214     bundleName_ = bundleName;
215     return E_OK;
216 }
217 
GetBundleName() const218 std::string RdbStoreConfig::GetBundleName() const
219 {
220     return bundleName_;
221 }
222 
SetModuleName(const std::string & moduleName)223 void RdbStoreConfig::SetModuleName(const std::string &moduleName)
224 {
225     moduleName_ = moduleName;
226 }
227 
GetModuleName() const228 std::string RdbStoreConfig::GetModuleName() const
229 {
230     return moduleName_;
231 }
232 
SetServiceName(const std::string & serviceName)233 void RdbStoreConfig::SetServiceName(const std::string &serviceName)
234 {
235     SetBundleName(serviceName);
236 }
237 
SetArea(int32_t area)238 void RdbStoreConfig::SetArea(int32_t area)
239 {
240     area_ = area + 1;
241 }
242 
GetArea() const243 int32_t RdbStoreConfig::GetArea() const
244 {
245     return area_;
246 }
247 
GetJournalModeValue(JournalMode journalMode)248 std::string RdbStoreConfig::GetJournalModeValue(JournalMode journalMode)
249 {
250     std::string value = "";
251 
252     switch (journalMode) {
253         case JournalMode::MODE_DELETE:
254             return "DELETE";
255         case JournalMode::MODE_TRUNCATE:
256             return "TRUNCATE";
257         case JournalMode::MODE_PERSIST:
258             return "PERSIST";
259         case JournalMode::MODE_MEMORY:
260             return "MEMORY";
261         case JournalMode::MODE_WAL:
262             return "WAL";
263         case JournalMode::MODE_OFF:
264             return "OFF";
265         default:
266             break;
267     }
268     return value;
269 }
270 
GetSyncModeValue(SyncMode syncMode)271 std::string RdbStoreConfig::GetSyncModeValue(SyncMode syncMode)
272 {
273     std::string value = "";
274     switch (syncMode) {
275         case SyncMode::MODE_OFF:
276             return "MODE_OFF";
277         case SyncMode::MODE_NORMAL:
278             return "MODE_NORMAL";
279         case SyncMode::MODE_FULL:
280             return "MODE_FULL";
281         case SyncMode::MODE_EXTRA:
282             return "MODE_EXTRA";
283         default:
284             break;
285     }
286 
287     return value;
288 }
289 
GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)290 std::string RdbStoreConfig::GetDatabaseFileTypeValue(DatabaseFileType databaseFileType)
291 {
292     std::string value = "";
293     switch (databaseFileType) {
294         case DatabaseFileType::NORMAL:
295             return "db";
296         case DatabaseFileType::BACKUP:
297             return "backup";
298         case DatabaseFileType::CORRUPT:
299             return "corrupt";
300         default:
301             break;
302     }
303 
304     return value;
305 }
306 
SetSecurityLevel(SecurityLevel sl)307 void RdbStoreConfig::SetSecurityLevel(SecurityLevel sl)
308 {
309     securityLevel_ = sl;
310 }
311 
GetSecurityLevel() const312 SecurityLevel RdbStoreConfig::GetSecurityLevel() const
313 {
314     return securityLevel_;
315 }
316 
SetEncryptStatus(const bool status)317 void RdbStoreConfig::SetEncryptStatus(const bool status)
318 {
319     this->isEncrypt_ = status;
320 }
321 
IsEncrypt() const322 bool RdbStoreConfig::IsEncrypt() const
323 {
324     return isEncrypt_ || !cryptoParam_.encryptKey_.empty();
325 }
326 
IsCreateNecessary() const327 bool RdbStoreConfig::IsCreateNecessary() const
328 {
329     return isCreateNecessary_;
330 }
331 
SetCreateNecessary(bool isCreateNecessary)332 void RdbStoreConfig::SetCreateNecessary(bool isCreateNecessary)
333 {
334     isCreateNecessary_ = isCreateNecessary;
335 }
336 
GetReadConSize() const337 int RdbStoreConfig::GetReadConSize() const
338 {
339     return readConSize_;
340 }
341 
SetReadConSize(int readConSize)342 void RdbStoreConfig::SetReadConSize(int readConSize)
343 {
344     readConSize_ = readConSize;
345 }
346 
SetEncryptKey(const std::vector<uint8_t> & encryptKey)347 void RdbStoreConfig::SetEncryptKey(const std::vector<uint8_t> &encryptKey)
348 {
349     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
350     cryptoParam_.encryptKey_ = encryptKey;
351 }
352 
RestoreEncryptKey(const std::vector<uint8_t> & encryptKey) const353 void RdbStoreConfig::RestoreEncryptKey(const std::vector<uint8_t> &encryptKey) const
354 {
355     RdbSecurityManager::GetInstance().RestoreKeyFile(GetPath(), encryptKey);
356     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
357     newEncryptKey_.assign(newEncryptKey_.size(), 0);
358     cryptoParam_.encryptKey_ = encryptKey;
359 }
360 
GetEncryptKey() const361 std::vector<uint8_t> RdbStoreConfig::GetEncryptKey() const
362 {
363     return cryptoParam_.encryptKey_;
364 }
365 
ResetEncryptKey(const std::vector<uint8_t> & encryptKey) const366 void RdbStoreConfig::ResetEncryptKey(const std::vector<uint8_t> &encryptKey) const
367 {
368     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
369     cryptoParam_.encryptKey_ = encryptKey;
370 }
371 
ChangeEncryptKey() const372 void RdbStoreConfig::ChangeEncryptKey() const
373 {
374     RdbSecurityManager::GetInstance().ChangeKeyFile(GetPath());
375     if (newEncryptKey_.empty()) {
376         return;
377     }
378     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
379     cryptoParam_.encryptKey_.assign(newEncryptKey_.data(), newEncryptKey_.data() + newEncryptKey_.size());
380     newEncryptKey_.assign(newEncryptKey_.size(), 0);
381     newEncryptKey_.resize(0);
382 }
383 
GetNewEncryptKey() const384 std::vector<uint8_t> RdbStoreConfig::GetNewEncryptKey() const
385 {
386     return newEncryptKey_;
387 }
388 
Initialize() const389 int32_t RdbStoreConfig::Initialize() const
390 {
391     return GenerateEncryptedKey();
392 }
393 
GenerateEncryptedKey() const394 int32_t RdbStoreConfig::GenerateEncryptedKey() const
395 {
396     if (!isEncrypt_ || !cryptoParam_.encryptKey_.empty()) {
397         return E_OK;
398     }
399 
400     auto name = bundleName_;
401     if (name.empty()) {
402         name = std::string(path_).substr(0, path_.rfind("/") + 1);
403     }
404     using KeyFileType = RdbSecurityManager::KeyFileType;
405     std::lock_guard<std::mutex> lock(g_mutex);
406     auto errCode = RdbSecurityManager::GetInstance().Init(name);
407     if (errCode != E_OK) {
408         RdbFaultHiViewReporter::ReportFault(RdbFaultDbFileEvent(FT_OPEN, E_ROOT_KEY_FAULT, *this,
409             "gen root key fail ret=" + std::to_string(errCode)));
410         LOG_ERROR("generate root encrypt key failed, bundleName_:%{public}s", bundleName_.c_str());
411         return errCode;
412     }
413     auto rdbPwd = RdbSecurityManager::GetInstance().GetRdbPassword(path_, KeyFileType::PUB_KEY_FILE);
414     if (rdbPwd.IsValid()) {
415         cryptoParam_.encryptKey_ = std::vector<uint8_t>(rdbPwd.GetData(), rdbPwd.GetData() + rdbPwd.GetSize());
416     }
417     rdbPwd.Clear();
418     if ((rdbPwd.isKeyExpired && autoRekey_) ||
419         RdbSecurityManager::GetInstance().IsKeyFileExists(path_, KeyFileType::PUB_KEY_FILE_NEW_KEY)) {
420         auto rdbNewPwd = RdbSecurityManager::GetInstance().GetRdbPassword(path_, KeyFileType::PUB_KEY_FILE_NEW_KEY);
421         if (rdbNewPwd.IsValid()) {
422             newEncryptKey_ = std::vector<uint8_t>(rdbNewPwd.GetData(), rdbNewPwd.GetData() + rdbNewPwd.GetSize());
423         }
424         rdbPwd.Clear();
425     }
426     if (cryptoParam_.encryptKey_.empty() && newEncryptKey_.empty()) {
427         LOG_WARN("key is inValid, bundleName_:%{public}s", bundleName_.c_str());
428     }
429     return E_OK;
430 }
431 
ClearEncryptKey()432 void RdbStoreConfig::ClearEncryptKey()
433 {
434     cryptoParam_.encryptKey_.assign(cryptoParam_.encryptKey_.size(), 0);
435     newEncryptKey_.assign(newEncryptKey_.size(), 0);
436 }
437 
SetScalarFunction(const std::string & functionName,int argc,ScalarFunction function)438 void RdbStoreConfig::SetScalarFunction(const std::string &functionName, int argc, ScalarFunction function)
439 {
440     customScalarFunctions_.try_emplace(functionName, ScalarFunctionInfo(function, argc));
441 }
442 
SetScalarFunctions(const std::map<std::string,ScalarFunctionInfo> functions)443 void RdbStoreConfig::SetScalarFunctions(const std::map<std::string, ScalarFunctionInfo> functions)
444 {
445     customScalarFunctions_ = functions;
446 }
447 
GetScalarFunctions() const448 std::map<std::string, ScalarFunctionInfo> RdbStoreConfig::GetScalarFunctions() const
449 {
450     return customScalarFunctions_;
451 }
452 
SetDataGroupId(const std::string & DataGroupId)453 void RdbStoreConfig::SetDataGroupId(const std::string &DataGroupId)
454 {
455     dataGroupId_ = DataGroupId;
456 }
457 
GetDataGroupId() const458 std::string RdbStoreConfig::GetDataGroupId() const
459 {
460     return dataGroupId_;
461 }
462 
SetAutoClean(bool isAutoClean)463 void RdbStoreConfig::SetAutoClean(bool isAutoClean)
464 {
465     isAutoClean_ = isAutoClean;
466 }
467 
GetAutoClean() const468 bool RdbStoreConfig::GetAutoClean() const
469 {
470     return isAutoClean_;
471 }
472 
SetIsVector(bool isVector)473 void RdbStoreConfig::SetIsVector(bool isVector)
474 {
475     isVector_ = isVector;
476     isVector ? SetDBType(DB_VECTOR) : SetDBType(DB_SQLITE);
477 }
478 
IsVector() const479 bool RdbStoreConfig::IsVector() const
480 {
481     return isVector_;
482 }
483 
SetCustomDir(const std::string & customDir)484 void RdbStoreConfig::SetCustomDir(const std::string &customDir)
485 {
486     customDir_ = customDir;
487 }
488 
GetCustomDir() const489 std::string RdbStoreConfig::GetCustomDir() const
490 {
491     return customDir_;
492 }
493 
SetVisitorDir(const std::string & visitorDir)494 void RdbStoreConfig::SetVisitorDir(const std::string &visitorDir)
495 {
496     visitorDir_ = visitorDir;
497 }
498 
GetVisitorDir() const499 std::string RdbStoreConfig::GetVisitorDir() const
500 {
501     return visitorDir_;
502 }
503 
IsSearchable() const504 bool RdbStoreConfig::IsSearchable() const
505 {
506     return isSearchable_;
507 }
508 
SetSearchable(bool isSearchable)509 void RdbStoreConfig::SetSearchable(bool isSearchable)
510 {
511     isSearchable_ = isSearchable;
512 }
513 
GetWriteTime() const514 int RdbStoreConfig::GetWriteTime() const
515 {
516     return writeTimeout_;
517 }
518 
SetWriteTime(int timeout)519 void RdbStoreConfig::SetWriteTime(int timeout)
520 {
521     writeTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
522 }
523 
IsCustomEncryptParam() const524 bool RdbStoreConfig::IsCustomEncryptParam() const
525 {
526     return customEncryptParam_;
527 }
528 
GetReadTime() const529 int RdbStoreConfig::GetReadTime() const
530 {
531     return readTimeout_;
532 }
533 
SetReadTime(int timeout)534 void RdbStoreConfig::SetReadTime(int timeout)
535 {
536     readTimeout_ = std::max(MIN_TIMEOUT, std::min(MAX_TIMEOUT, timeout));
537 }
538 
SetRoleType(RoleType role)539 void RdbStoreConfig::SetRoleType(RoleType role)
540 {
541     role_ = role;
542 }
543 
GetRoleType() const544 uint32_t RdbStoreConfig::GetRoleType() const
545 {
546     return role_;
547 }
548 
SetDBType(int32_t dbType)549 void RdbStoreConfig::SetDBType(int32_t dbType)
550 {
551     dbType_ = dbType;
552 }
553 
GetDBType() const554 int32_t RdbStoreConfig::GetDBType() const
555 {
556     return dbType_;
557 }
558 
SetAllowRebuild(bool allowRebuild)559 void RdbStoreConfig::SetAllowRebuild(bool allowRebuild)
560 {
561     this->allowRebuilt_ = allowRebuild;
562 }
563 
GetAllowRebuild() const564 bool RdbStoreConfig::GetAllowRebuild() const
565 {
566     return allowRebuilt_;
567 }
568 
SetIntegrityCheck(IntegrityCheck checkType)569 void RdbStoreConfig::SetIntegrityCheck(IntegrityCheck checkType)
570 {
571     checkType_ = checkType;
572 }
573 
GetIntegrityCheck() const574 IntegrityCheck RdbStoreConfig::GetIntegrityCheck() const
575 {
576     return checkType_;
577 }
578 
GetIter() const579 int32_t RdbStoreConfig::GetIter() const
580 {
581     return cryptoParam_.iterNum;
582 }
583 
SetIter(int32_t iter) const584 void RdbStoreConfig::SetIter(int32_t iter) const
585 {
586     cryptoParam_.iterNum = iter;
587 }
588 
SetPluginLibs(const std::vector<std::string> & pluginLibs)589 void RdbStoreConfig::SetPluginLibs(const std::vector<std::string> &pluginLibs)
590 {
591     pluginLibs_ = pluginLibs;
592 }
593 
GetPluginLibs() const594 std::vector<std::string> RdbStoreConfig::GetPluginLibs() const
595 {
596     return pluginLibs_;
597 }
598 
GetHaMode() const599 int32_t RdbStoreConfig::GetHaMode() const
600 {
601     return haMode_;
602 }
603 
SetHaMode(int32_t haMode)604 void RdbStoreConfig::SetHaMode(int32_t haMode)
605 {
606     haMode_ = haMode;
607 }
608 
GetPromiseInfo() const609 PromiseInfo RdbStoreConfig::GetPromiseInfo() const
610 {
611     return promiseInfo_;
612 }
613 
SetPromiseInfo(PromiseInfo promiseInfo)614 void RdbStoreConfig::SetPromiseInfo(PromiseInfo promiseInfo)
615 {
616     promiseInfo_ = promiseInfo;
617 }
618 
GetTokenizer() const619 Tokenizer RdbStoreConfig::GetTokenizer() const
620 {
621     return tokenizer_;
622 }
623 
SetTokenizer(Tokenizer tokenizer)624 void RdbStoreConfig::SetTokenizer(Tokenizer tokenizer)
625 {
626     tokenizer_ = tokenizer;
627 }
628 
GetWalLimitSize() const629 ssize_t RdbStoreConfig::GetWalLimitSize() const
630 {
631     return walLimitSize_;
632 }
633 
SetEnableSemanticIndex(bool enableSemanticIndex)634 void RdbStoreConfig::SetEnableSemanticIndex(bool enableSemanticIndex)
635 {
636     enableSemanticIndex_ = enableSemanticIndex;
637 }
638 
GetEnableSemanticIndex() const639 bool RdbStoreConfig::GetEnableSemanticIndex() const
640 {
641     return enableSemanticIndex_;
642 }
643 
SetWalLimitSize(ssize_t size)644 void RdbStoreConfig::SetWalLimitSize(ssize_t size)
645 {
646     if (size < GlobalExpr::DB_WAL_DEFAULT_SIZE) {
647         size = GlobalExpr::DB_WAL_DEFAULT_SIZE;
648     }
649     if (size > GlobalExpr::DB_WAL_SIZE_LIMIT_MAX) {
650         size = GlobalExpr::DB_WAL_SIZE_LIMIT_MAX;
651     }
652     walLimitSize_ = size;
653     // '(size >> 1) + (size >> 2)' Size of the WAL file that does not checkpoint within 5 minutes when sqlite_busy
654     checkpointSize_ = (size >> 1) + (size >> 2);
655     // '(size >> 5) + (size >> 7)' Size of the WAL file for starting checkpoint.
656     startCheckpointSize_ = (size >> 5) + (size >> 7);
657 }
658 
GetClearMemorySize() const659 int32_t RdbStoreConfig::GetClearMemorySize() const
660 {
661     return clearMemorySize_;
662 }
663 
SetClearMemorySize(int32_t size)664 void RdbStoreConfig::SetClearMemorySize(int32_t size)
665 {
666     if (size < 0 || size > GlobalExpr::CLEAR_MEMORY_SIZE) {
667         LOG_WARN("size is inValid, size:%{public}d", size);
668         size = GlobalExpr::CLEAR_MEMORY_SIZE;
669     }
670     clearMemorySize_ = size;
671 }
672 
GetCollatorLocales() const673 std::string RdbStoreConfig::GetCollatorLocales() const
674 {
675     return collatorLocales_.Get();
676 }
677 
SetCollatorLocales(const std::string & locales) const678 void RdbStoreConfig::SetCollatorLocales(const std::string &locales) const
679 {
680     collatorLocales_.Set(locales);
681 }
682 
GetCheckpointSize() const683 ssize_t RdbStoreConfig::GetCheckpointSize() const
684 {
685     return checkpointSize_;
686 }
687 
GetStartCheckpointSize() const688 ssize_t RdbStoreConfig::GetStartCheckpointSize() const
689 {
690     return startCheckpointSize_;
691 }
692 
GetSubUser() const693 int32_t RdbStoreConfig::GetSubUser() const
694 {
695     return subUser_;
696 }
697 
SetSubUser(int32_t subUser)698 void RdbStoreConfig::SetSubUser(int32_t subUser)
699 {
700     subUser_ = subUser;
701 }
702 
EnableRekey(bool enable)703 void RdbStoreConfig::EnableRekey(bool enable)
704 {
705     autoRekey_ = enable;
706 }
707 
SetCryptoParam(RdbStoreConfig::CryptoParam cryptoParam)708 void RdbStoreConfig::SetCryptoParam(RdbStoreConfig::CryptoParam cryptoParam)
709 {
710     cryptoParam_ = cryptoParam;
711     if (!(cryptoParam_.encryptKey_.empty())) {
712         customEncryptParam_ = true;
713     }
714 }
715 
GetCryptoParam() const716 RdbStoreConfig::CryptoParam RdbStoreConfig::GetCryptoParam() const
717 {
718     return cryptoParam_;
719 }
720 
721 RdbStoreConfig::CryptoParam::CryptoParam() = default;
722 
~CryptoParam()723 RdbStoreConfig::CryptoParam::~CryptoParam()
724 {
725     encryptKey_.assign(encryptKey_.size(), 0);
726 }
727 
IsValid() const728 bool RdbStoreConfig::CryptoParam::IsValid() const
729 {
730     int32_t count = iterNum;
731     if (count < 0) {
732         return false;
733     }
734 
735     if (encryptAlgo != AES_256_CBC && encryptAlgo != AES_256_GCM) {
736         return false;
737     }
738 
739     if (hmacAlgo < SHA1 || hmacAlgo > SHA512) {
740         return false;
741     }
742 
743     if (kdfAlgo < KDF_SHA1 || kdfAlgo > KDF_SHA512) {
744         return false;
745     }
746 
747     return (cryptoPageSize != 0) && ((cryptoPageSize & DB_INVALID_CRYPTO_PAGE_SIZE_MASK) == 0) &&
748            (cryptoPageSize & (cryptoPageSize - 1)) == 0;
749 }
750 
ToString() const751 std::string RdbStoreConfig::ToString() const
752 {
753     std::stringstream oss;
754     oss << " bundleName:" << bundleName_ << ",";
755     oss << " moduleName:" << moduleName_ << ",";
756     oss << " dataGroupId:" << dataGroupId_ << ",";
757     oss << " path:" << SqliteUtils::Anonymous(path_) << ",";
758     oss << " storageMode:" << static_cast<int32_t>(storageMode_) << ",";
759     oss << " journalMode:" << journalMode_ << ",";
760     oss << " syncMode:" << syncMode_ << ",";
761     oss << " databaseFileType:" << databaseFileType << ",";
762     oss << " isEncrypt:" << IsEncrypt() << ",";
763     oss << " isSearchable:" << IsSearchable() << ",";
764     oss << " readOnly_:" << readOnly_ << ",";
765     oss << " securityLevel:" << static_cast<int32_t>(securityLevel_) << ",";
766     oss << " journalSize:" << journalSize_ << ",";
767     oss << " pageSize:" << pageSize_ << ",";
768     oss << " dbType:" << dbType_ << ",";
769     oss << " customDir:" << SqliteUtils::Anonymous(customDir_) << ",";
770     oss << " haMode:" << haMode_ << ",";
771     oss << " pluginLibs size:" << pluginLibs_.size() << ",";
772     oss << " area:" << area_ << ",";
773     return oss.str();
774 }
775 
FormatCfg(const RdbStoreConfig & first,const RdbStoreConfig & second)776 std::string RdbStoreConfig::FormatCfg(const RdbStoreConfig &first, const RdbStoreConfig &second)
777 {
778     std::stringstream oss;
779     oss << " storageMode:" << static_cast<int32_t>(first.storageMode_) << "->"
780         << static_cast<int32_t>(second.storageMode_) << ",";
781     oss << " journalMode:" << first.journalMode_ << "->" << second.journalMode_ << ",";
782     oss << " syncMode:" << first.syncMode_ << "->" << second.syncMode_ << ",";
783     oss << " databaseFileType:" << first.databaseFileType << "->" << second.databaseFileType << ",";
784     oss << " isEncrypt:" << first.IsEncrypt() << "->" << second.IsEncrypt() << ",";
785     oss << " isSearchable:" << first.IsSearchable() << "->" << second.IsSearchable() << ",";
786     oss << " readOnly_:" << first.readOnly_ << "->" << second.readOnly_ << ",";
787     oss << " securityLevel:" << static_cast<int32_t>(first.securityLevel_) << "->"
788         << static_cast<int32_t>(second.securityLevel_) << ",";
789     oss << " journalSize:" << first.journalSize_ << "->" << second.journalSize_ << ",";
790     oss << " pageSize:" << first.pageSize_ << "->" << second.pageSize_ << ",";
791     oss << " dbType:" << first.dbType_ << "->" << second.dbType_ << ",";
792     oss << " customDir:" << SqliteUtils::Anonymous(first.customDir_) << "->"
793         << SqliteUtils::Anonymous(second.customDir_) << ",";
794     oss << " haMode:" << first.haMode_ << "->" << second.haMode_ << ",";
795     oss << " pluginLibs size:" << first.pluginLibs_.size() << "->" << second.pluginLibs_.size() << ",";
796     oss << " area:" << first.area_ << "->" << second.area_ << ",";
797     return oss.str();
798 }
799 
SetRegisterInfo(RegisterType type,bool state) const800 void RdbStoreConfig::SetRegisterInfo(RegisterType type, bool state) const
801 {
802     registerInfo_.Set(type, state);
803 }
804 
GetRegisterInfo(RegisterType type) const805 bool RdbStoreConfig::GetRegisterInfo(RegisterType type) const
806 {
807     return registerInfo_.Get(type);
808 }
809 
IsEqualRegisterInfo(const RdbStoreConfig & config) const810 bool RdbStoreConfig::IsEqualRegisterInfo(const RdbStoreConfig& config) const
811 {
812     return registerInfo_ == config.registerInfo_;
813 }
814 } // namespace OHOS::NativeRdb
815