• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright (c) 2023 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 "collection_option.h"
17 
18 #include <algorithm>
19 #include <cstring>
20 
21 #include "doc_errno.h"
22 #include "json_object.h"
23 #include "log_print.h"
24 
25 namespace DocumentDB {
26 namespace {
27 constexpr const char *OPT_MAX_DOC = "maxdoc";
28 
CheckConfigValid(const JsonObject & config)29 int CheckConfigValid(const JsonObject &config)
30 {
31     JsonObject child = config.GetChild();
32     while (!child.IsNull()) {
33         std::string fieldName = child.GetItemField();
34         if (strcmp(OPT_MAX_DOC, fieldName.c_str()) != 0) {
35             GLOGE("Invalid collection config.");
36             return -E_INVALID_CONFIG_VALUE;
37         }
38         child = child.GetNext();
39     }
40     return E_OK;
41 }
42 } // namespace
43 
ReadOption(const std::string & optStr,int & errCode)44 CollectionOption CollectionOption::ReadOption(const std::string &optStr, int &errCode)
45 {
46     if (optStr.empty()) {
47         return {};
48     }
49 
50     std::string lowerCaseOptStr = optStr;
51     std::transform(lowerCaseOptStr.begin(), lowerCaseOptStr.end(), lowerCaseOptStr.begin(), [](unsigned char c) {
52         return std::tolower(c);
53     });
54 
55     JsonObject collOpt = JsonObject::Parse(lowerCaseOptStr, errCode);
56     if (errCode != E_OK) {
57         GLOGE("Read collection option failed from str. %d", errCode);
58         return {};
59     }
60 
61     errCode = CheckConfigValid(collOpt);
62     if (errCode != E_OK) {
63         GLOGE("Check collection option, not support config item. %d", errCode);
64         return {};
65     }
66 
67     static const JsonFieldPath maxDocField = { OPT_MAX_DOC };
68     if (!collOpt.IsFieldExists(maxDocField)) {
69         return {};
70     }
71 
72     ValueObject maxDocValue = collOpt.GetObjectByPath(maxDocField, errCode);
73     if (errCode != E_OK) {
74         GLOGE("Read collection option failed. %d", errCode);
75         return {};
76     }
77 
78     if (maxDocValue.GetValueType() != ValueObject::ValueType::VALUE_NUMBER) {
79         GLOGE("Check collection option failed, the field type of maxDoc is not NUMBER.");
80         errCode = -E_INVALID_CONFIG_VALUE;
81         return {};
82     }
83 
84     if (maxDocValue.GetIntValue() <= 0 || static_cast<uint64_t>(maxDocValue.GetIntValue()) > UINT32_MAX) {
85         GLOGE("Check collection option failed, invalid maxDoc value.");
86         errCode = -E_INVALID_CONFIG_VALUE;
87         return {};
88     }
89 
90     CollectionOption option;
91     option.maxDoc_ = static_cast<uint32_t>(maxDocValue.GetIntValue());
92     option.option_ = optStr;
93     return option;
94 }
95 } // namespace DocumentDB