• 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 #include "projection_tree.h"
16 
17 namespace DocumentDB {
18 constexpr int JSON_DEEP_MAX = 4;
19 
ParseTree(std::vector<std::vector<std::string>> & path)20 int ProjectionTree::ParseTree(std::vector<std::vector<std::string>> &path)
21 {
22     ProjectionNode *node = &node_;
23     if (node == nullptr) {
24         return E_OK;
25     }
26     for (auto singlePath : path) {
27         node = &node_;
28         for (size_t j = 0; j < singlePath.size(); j++) {
29             if (node->sonNode[singlePath[j]] != nullptr) {
30                 node = node->sonNode[singlePath[j]];
31                 if (j < singlePath.size() - 1 && node->isDeepest) {
32                     return -E_INVALID_ARGS;
33                 }
34                 if (j == singlePath.size() - 1 && !node->isDeepest) {
35                     return -E_INVALID_ARGS;
36                 }
37             } else {
38                 auto tempNode = new (std::nothrow) ProjectionNode;
39                 if (tempNode == nullptr) {
40                     GLOGE("Memory allocation failed!");
41                     return -E_FAILED_MEMORY_ALLOCATE;
42                 }
43                 tempNode->Deep = node->Deep + 1;
44                 if (tempNode->Deep > JSON_DEEP_MAX) {
45                     delete tempNode;
46                     return -E_INVALID_ARGS;
47                 }
48                 node->isDeepest = false;
49                 node->sonNode[singlePath[j]] = tempNode;
50                 node = node->sonNode[singlePath[j]];
51             }
52         }
53     }
54     return E_OK;
55 }
56 
SearchTree(std::vector<std::string> & singlePath,size_t & index)57 bool ProjectionTree::SearchTree(std::vector<std::string> &singlePath, size_t &index)
58 {
59     ProjectionNode *node = &node_;
60     for (size_t i = 0; i < singlePath.size(); i++) {
61         if (node->sonNode[singlePath[i]] != nullptr) {
62             node = node->sonNode[singlePath[i]];
63             if (node->isDeepest) {
64                 index = i + 1;
65             }
66         } else {
67             return false;
68         }
69     }
70     return true;
71 }
72 
DeleteProjectionNode()73 int ProjectionNode::DeleteProjectionNode()
74 {
75     for (auto item : sonNode) {
76         if (item.second != nullptr) {
77             delete item.second;
78             item.second = nullptr;
79         }
80     }
81     return E_OK;
82 }
83 } // namespace DocumentDB