From 5c913095a9a93a569889e58fb0b6a6d0f127da31 Mon Sep 17 00:00:00 2001 From: mazhao Date: Wed, 15 Mar 2023 08:40:51 +0000 Subject: [PATCH 1/9] Packaging of cjson library Signed-off-by: mazhao --- .../src/oh_adapter/include/jsonInfo.h | 36 +++ .../src/oh_adapter/include/jsonOperator.h | 55 ++++ .../src/oh_adapter/src/jsonInfo.cpp | 87 ++++++ .../src/oh_adapter/src/jsonOperator.cpp | 262 ++++++++++++++++++ .../unittest/json_Adator/jsonInfo_test.cpp | 93 +++++++ 5 files changed, 533 insertions(+) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/json_Adator/jsonInfo_test.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h new file mode 100644 index 00000000..1dcc8669 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h @@ -0,0 +1,36 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef JSONINFO_H +#define JSONINFO_H + +#include +#include + +#include "cJSON.h" + +class JsonInfo { +public: + JsonInfo() = default; + + ~JsonInfo() = default; + + std::vector> ParsePath(const std::string &path); + + int GetJsonDeep(const std::string &jsonstr); + +private: + int Parsepot(const std::string &mixfield_name, std::vector> &parsePath, int index); +}; +#endif \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h new file mode 100644 index 00000000..b1237cac --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h @@ -0,0 +1,55 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#ifndef JSONOPERATOR_H +#define JSONOPERATOR_H + +#include +#include + +#include "cJSON.h" + +class JsonOperator { +public: + JsonOperator() = default; + ~JsonOperator() = default; + + std::string GetValue(std::string &jsonstr, std::vector path); + + int GetValueType(std::string &jsonstr, std::vector path); + + int AddValueToJson(std::string &jsonstr, const std::string &field, const int value); + + int AddValueToJson(std::string &jsonstr, const std::string &field, const std::string &value); + + int AddItemToJson(std::string &jsonstr, const std::string &field, const std::string &Item); + + int AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value); + + int AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value); + + int AddItemToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &Item); + + int DeleteJsonData(std::string &jsonstr, std::vector path); + + std::vector GetBrotherField(std::string &jsonstr, std::vector path); + + const int JsonOperator_int = 8; + const int JsonOperator_string = 16; + const int JsonOperator_array = 32; + const int JsonOperator_object = 64; +private: + std::pair Finditem(std::string &jsonstr, const std::vector json_path); +}; +#endif \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp new file mode 100644 index 00000000..c27da029 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp @@ -0,0 +1,87 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include +#include "jsonInfo.h" + +int JsonInfo::Parsepot(const std::string &mixfield_name, std::vector> &parsePath , int index) +{ + std::string tempparse_name; + std::vector parsed_mixfiled_name; + for (int j = 0; j < mixfield_name.size(); j++) { + if (mixfield_name[j] != '.') { + tempparse_name = tempparse_name + mixfield_name[j]; + } + if (mixfield_name[j] == '.' || j == mixfield_name.size() - 1) { + parsed_mixfiled_name.emplace_back(tempparse_name); + tempparse_name.clear(); + } + } + parsePath[index].insert(parsePath[index].end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + return 0; +} + +std::vector> JsonInfo::ParsePath(const std::string &path) +{ + std::vector> parsePath; + cJSON *root = cJSON_Parse(path.c_str()); + cJSON *projection_json = root->child; + if (projection_json == nullptr) { + std::cout << "projection_json is null"; + } + for (int i = 0; i < cJSON_GetArraySize(root); i++) { + std::string firstfield_name = ""; + std::string projection_str = projection_json->string; + for (int j = 0; j < projection_str.size(); j++) { + if (projection_str[j] != '.') { + firstfield_name = firstfield_name + projection_str[j]; + } + if (projection_str[j] == '.' || j == projection_str.size() - 1) { + break; + } + } + if (parsePath.size() < i + 1) { + parsePath.push_back(std::vector(0)); + } + Parsepot(projection_json->string, parsePath, i); + cJSON *projection_json_root = projection_json; + while (projection_json->child != nullptr) { + projection_json = projection_json->child; + Parsepot(projection_json->string, parsePath, i); + } + projection_json = projection_json_root; + if (projection_json->next != NULL) { + projection_json = projection_json->next; + } + } + return parsePath; +} + +int JsonInfo::GetJsonDeep(const std::string &jsonstr) +{ + auto cJsondata = cJSON_Parse(jsonstr.c_str()); + std::string printed_str = cJSON_Print(cJsondata); + int lens = 0; + int deep = 0; + for (int i = 0; i < printed_str.size(); i++) { + if (printed_str[i] == '[' || printed_str[i] == '{') { + lens++; + } + else if (printed_str[i] == ']' || printed_str[i] == '}') { + deep = std::max(deep, lens); + lens--; + } + } + return deep; +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp new file mode 100644 index 00000000..0c95677a --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp @@ -0,0 +1,262 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#include +#include +#include "jsonOperator.h" + +std::pair JsonOperator::Finditem(std::string &jsonstr, const std::vector json_path) +{ + cJSON *cJsondata_item = cJSON_Parse(jsonstr.c_str()); + cJSON *cJsondata_temp; + cJSON *cJsondata_root; + cJsondata_root = cJsondata_item; + if (json_path.size() != 0 && json_path[0] == "") { + return std::pair(cJsondata_item, cJsondata_item); + } + for (int i = 0; i < json_path.size(); i++) { + if (cJsondata_item->type == cJSON_Object) { + if (cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()) == nullptr) { + return std::pair(nullptr, nullptr); + } + cJsondata_temp = cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()); + cJsondata_item = cJsondata_temp; + } + if (cJsondata_item->type == cJSON_Array) { + int nums = 0; + for (int j = 0; j < json_path[i].size(); j++) { + if (json_path[i][j]-'0' > 9 || json_path[i][j] - '0' < 0 || json_path[i][j] - '0' > cJSON_GetArraySize(cJsondata_item)) { + break; + } + auto GetArrayret = cJSON_GetArrayItem(cJsondata_item, json_path[i][j]-'0'); + if (typeid(GetArrayret) == typeid(cJSON*)) { + cJsondata_item = GetArrayret; + } + else { + if ( i != json_path.size() - 1) { + std::cout << "Patharg wrong"; + } + else { + return std::pair(nullptr, nullptr); + } + } + } + } + + } + return std::pair(cJsondata_root, cJsondata_item); +} + +std::string JsonOperator::GetValue(std::string &jsonstr, std::vector path) +{ + auto cjson_pair = Finditem(jsonstr, path); + auto cjsondata = cjson_pair.second; + if (cjsondata == nullptr) { + std::cout << "arg is wrong"; + return ""; + } + std::string cjsondata_str = cJSON_Print(cjsondata); + cJSON_Delete(cjsondata); + return cjsondata_str; +} + +int JsonOperator::GetValueType(std::string &jsonstr, std::vector path) +{ + int type; + auto cjson_pair = Finditem(jsonstr, path); + auto cjsondata = cjson_pair.second; + if (cjsondata == nullptr) { + std::cout << "arg is wrong"; + return 0; + } + type = cjsondata->type; + cJSON_Delete(cjsondata); + return type; +} + +int JsonOperator::AddValueToJson(std::string &jsonstr, const std::string &field, const int value) +{ + cJSON *cJsondata = cJSON_Parse(jsonstr.c_str()); + cJSON_AddNumberToObject(cJsondata,field.c_str(),value); + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddValueToJson(std::string &jsonstr, const std::string &field, const std::string &value) +{ + cJSON *cJsondata = cJSON_Parse(jsonstr.c_str()); + cJSON_AddStringToObject(cJsondata,field.c_str(),value.c_str()); + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddItemToJson(std::string &jsonstr, const std::string &field, const std::string &Item) +{ + cJSON *cJsondata = cJSON_Parse(jsonstr.c_str()); + cJSON* node = cJSON_Parse(Item.c_str()); + if (node == nullptr) { + std::cout << "creat cjson false"; + return 0; + } + cJSON_AddItemToObject(cJsondata, field.c_str(), node); + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value) +{ + auto cjson_pair = Finditem(jsonstr, path); + cJSON* node = cjson_pair.second; + cJSON* cJsondata = cjson_pair.first; + if (cJsondata == nullptr) { + std::cout << "arg is wrong"; + return 0; + } + if (node->type == cJSON_Array) { + cJSON* temp = cJSON_CreateObject(); + cJSON_AddNumberToObject(temp,field.c_str(),value); + cJSON_AddItemToArray(node, temp); + } + else { + cJSON_AddNumberToObject(node, field.c_str(), value); + } + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value) +{ + auto cjson_pair = Finditem(jsonstr, path); + cJSON* node = cjson_pair.second; + cJSON* cJsondata = cjson_pair.first; + if (cJsondata == nullptr) { + std::cout << "arg is wrong"; + return 0; + } + if (node->type == cJSON_Array) { + cJSON* temp = cJSON_CreateObject(); + cJSON_AddStringToObject(temp,field.c_str(),value.c_str()); + cJSON_AddItemToArray(node, temp); + } + else { + cJSON_AddStringToObject(node, field.c_str(), value.c_str()); + } + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddItemToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &Item) +{ + auto cjson_pair = Finditem(jsonstr, path); + cJSON* node = cjson_pair.second; + cJSON* cJsondata = cjson_pair.first; + if (cJsondata == nullptr) { + std::cout << "arg is wrong"; + return 0; + } + if (node->type == cJSON_Array) { + cJSON* temp = cJSON_CreateObject(); + cJSON_AddItemToObject(temp,field.c_str(), cJSON_Parse(Item.c_str())); + cJSON_AddItemToArray(node, temp); + } + else { + cJSON_AddItemToObject(node, field.c_str(), cJSON_Parse(Item.c_str())); + } + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::DeleteJsonData(std::string &jsonstr, std::vector path) +{ + cJSON *node_father; + cJSON *cJsondata_root; + std::string lastString; + if (path.size()>0) { + lastString = path.back(); + path.pop_back(); + if (path.size() == 0) { + std::vector emptyPath; + auto cjson_pair = Finditem(jsonstr, emptyPath); + node_father = cjson_pair.second; + cJsondata_root = cjson_pair.first; + path.emplace_back(lastString); + } + else { + auto cjson_pair = Finditem(jsonstr, path); + node_father = cjson_pair.second; + cJsondata_root = cjson_pair.first; + path.emplace_back(lastString); + } + } + if (node_father->type == cJSON_Object) { + if (cJSON_GetObjectItem(node_father, lastString.c_str()) != nullptr) { + cJSON_DeleteItemFromObject(node_father, lastString.c_str()); + } + else { + std::cout <<"no item that can be deleted"; + } + } + if (node_father->type == cJSON_Array) { + if (cJSON_GetArrayItem(node_father, lastString[0] - '0') != nullptr) { + cJSON_DeleteItemFromArray(node_father, lastString[0] - '0'); + } + else { + std::cout <<"no item that can be deleted"; + } + } + jsonstr = cJSON_Print(cJsondata_root); + cJSON_Delete(cJsondata_root); + return 0; +} + +std::vector JsonOperator::GetBrotherField(std::string &jsonstr, std::vector path) +{ + std::vector brotherField; + cJSON *cJsondata; + auto cjson_pair = Finditem(jsonstr, path); + if (cjson_pair.second == nullptr) { + return brotherField; + } + if (cjson_pair.second->type == cJSON_Object) { + if (cjson_pair.second->child != nullptr) { + cJsondata = cjson_pair.second->child; + } + while (cJsondata->prev != nullptr) { + cJsondata = cJsondata->prev; + } + while (cJsondata != nullptr) { + brotherField.emplace_back(cJsondata->string); + cJsondata = cJsondata->next; + } + } + if (cjson_pair.second->type == cJSON_Array) { + cJsondata = cjson_pair.second; + int array_lens = cJSON_GetArraySize(cJsondata); + for (int i = 0; i < array_lens; i++) { + if (cJSON_GetArrayItem(cJsondata, i)->type != cJSON_Object) { + brotherField.emplace_back(""); + } + else { + brotherField.emplace_back(std::to_string(i)); + } + } + } + return brotherField; +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/json_Adator/jsonInfo_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/json_Adator/jsonInfo_test.cpp new file mode 100644 index 00000000..8aa6304f --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/json_Adator/jsonInfo_test.cpp @@ -0,0 +1,93 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +#include "grd_base/grd_db_api.h" +#include "jsonInfo.h" + +using namespace testing::ext; + +namespace { + const int MAX_DEPTH_FOR_TEST = 10; + const int STRING1_DEPTH = 12; + const int STRING3_DEPTH = 6; + + // nest depth = 12 and valid. + const string JSON_STRING1 = "{\"#14\":[[{\"#11\":{\"#8\":[{\"#5\":[[{\"#2\":[{\"#0\":\"value_\"},\"value_\"]," + "\"#3\":\"value_\"},\"value_\"],\"value_\"],\"#6\":\"value_\"},\"value_\"],\"#9\":\"value_\"}," + "\"#12\":\"value_\"},\"value_\"],\"value_\"],\"#15\":{\"#18\":{\"#16\":\"value_\"},\"#19\":\"value_\"}}"; + + // nest depth = 12 and invalid happens in nest depth = 2. + const string JSON_STRING2 = "{\"#17\":[\"just for mistake pls.[{\"#14\":[[{\"#11\":{\"#8\":{\"#5\":[{\"#2\":" + "{\"#0\":\"value_\"},\"#3\":\"value_\"},\"value_\"],\"#6\":\"value_\"},\"#9\":\"value_\"}," + "\"#12\":\"value_\"},\"value_\"],\"value_\"],\"#15\":\"value_\"},\"value_\"],\"value_\"]," + "\"#18\":{\"#21\":{\"#19\":\"value_\"},\"#22\":\"value_\"}}"; + + // nest depth = 6 and valid. + const string JSON_STRING3 = "{\"#5\":[{\"#2\":[[{\"#0\":\"value_\"},\"value_\"],\"value_\"],\"#3\":\"value_\"}," + "\"value_\"],\"#6\":{\"#7\":\"value_\",\"#8\":\"value_\"}}"; + + // nest depth = 6 and invalid happens in nest depth = 3. + const string JSON_STRING4 = "{\"#6\":[{\"#3\":\"just for mistake pls.[{\"#0\":[\"value_\"],\"#1\":\"value_\"}," + "\"value_\"],\"#4\":\"value_\"},\"value_\"],\"#7\":{\"#8\":\"value_\",\"#9\":\"value_\"}}"; + + // nest depth = 15 and invalid happens in nest depth = 11. + const string JSON_STRING5 = "{\"#35\":[{\"#29\":{\"#23\":{\"#17\":{\"#11\":{\"#8\":[{\"#5\":[{\"#2\":" + "\"just for mistake pls.[[[{\"#0\":\"value_\"},\"value_\"],\"value_\"],\"value_\"],\"#3\":\"value_\"}," + "\"value_\"],\"#6\":\"value_\"},\"value_\"],\"#9\":\"value_\"},\"#12\":{\"#13\":\"value_\"," + "\"#14\":\"value_\"}},\"#18\":{\"#19\":\"value_\",\"#20\":\"value_\"}},\"#24\":{\"#25\":\"value_\"," + "\"#26\":\"value_\"}},\"#30\":{\"#31\":\"value_\",\"#32\":\"value_\"}},\"value_\"],\"#36\":" + "{\"#37\":[\"value_\"],\"#38\":\"value_\"}}"; + + uint32_t g_oriMaxNestDepth = 0; +} + +class jsonInfoTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void jsonInfoTest::SetUpTestCase(void) +{ +} + +void jsonInfoTest::TearDownTestCase(void) +{ +} + +void jsonInfoTest::SetUp(void) +{ +} + +void jsonInfoTest::TearDown(void) +{ +} + + +/** + * @tc.name: GetjsonDeepTest + * @tc.desc: Test get jsons deep lens. + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ +HWTEST_F(jsonInfoTest, GetjsonDeepTest001, TestSize.Level1) +{ + +} \ No newline at end of file -- Gitee From bcc14468f6ad479914cd09d52a83898fc814d6a2 Mon Sep 17 00:00:00 2001 From: mazhao Date: Wed, 15 Mar 2023 08:45:59 +0000 Subject: [PATCH 2/9] delete json_operator that is a null document Signed-off-by: mazhao --- .../data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/json_operator.h deleted file mode 100644 index e69de29b..00000000 -- Gitee From a8cb1787e8b007b22de04747f67287dfab6be816 Mon Sep 17 00:00:00 2001 From: mazhao Date: Fri, 17 Mar 2023 03:22:28 +0000 Subject: [PATCH 3/9] Add the function of pathParse, which can now parse the paths of all json documents, and support the decomposition of paths with dots. Signed-off-by: mazhao --- .../src/oh_adapter/include/jsonInfo.h | 7 ++- .../src/oh_adapter/src/jsonInfo.cpp | 45 ++++++++----------- 2 files changed, 21 insertions(+), 31 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h index 1dcc8669..4d6cf3eb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h @@ -12,14 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef JSONINFO_H #define JSONINFO_H +#include "cJSON.h" #include #include - -#include "cJSON.h" - class JsonInfo { public: JsonInfo() = default; @@ -31,6 +30,6 @@ public: int GetJsonDeep(const std::string &jsonstr); private: - int Parsepot(const std::string &mixfield_name, std::vector> &parsePath, int index); + int ParseNode(cJSON *Node, std::vector onePath, std::vector> &parsePath); }; #endif \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp index c27da029..8dfde194 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp @@ -12,13 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include "jsonInfo.h" -int JsonInfo::Parsepot(const std::string &mixfield_name, std::vector> &parsePath , int index) +int JsonInfo::ParseNode(cJSON *Node, std::vector onePath, std::vector> &parsePath) { std::string tempparse_name; std::vector parsed_mixfiled_name; + std::string mixfield_name = Node->string; for (int j = 0; j < mixfield_name.size(); j++) { if (mixfield_name[j] != '.') { tempparse_name = tempparse_name + mixfield_name[j]; @@ -28,7 +30,18 @@ int JsonInfo::Parsepot(const std::string &mixfield_name, std::vector forePath; + forePath = onePath; + onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); + if (Node->child != nullptr && Node->child->string != nullptr) { + ParseNode(Node->child, onePath, parsePath); + } + else { + parsePath.emplace_back(onePath); + } + if (Node->next != nullptr) { + ParseNode(Node->next, forePath, parsePath); + } return 0; } @@ -40,31 +53,9 @@ std::vector> JsonInfo::ParsePath(const std::string &pat if (projection_json == nullptr) { std::cout << "projection_json is null"; } - for (int i = 0; i < cJSON_GetArraySize(root); i++) { - std::string firstfield_name = ""; - std::string projection_str = projection_json->string; - for (int j = 0; j < projection_str.size(); j++) { - if (projection_str[j] != '.') { - firstfield_name = firstfield_name + projection_str[j]; - } - if (projection_str[j] == '.' || j == projection_str.size() - 1) { - break; - } - } - if (parsePath.size() < i + 1) { - parsePath.push_back(std::vector(0)); - } - Parsepot(projection_json->string, parsePath, i); - cJSON *projection_json_root = projection_json; - while (projection_json->child != nullptr) { - projection_json = projection_json->child; - Parsepot(projection_json->string, parsePath, i); - } - projection_json = projection_json_root; - if (projection_json->next != NULL) { - projection_json = projection_json->next; - } - } + std::vector onePath; + parsePath.emplace_back(onePath); + ParseNode(projection_json, onePath, parsePath); return parsePath; } -- Gitee From a0b6da9fc0dd8fa72053ca35ba913dcc24d65383 Mon Sep 17 00:00:00 2001 From: mazhao Date: Fri, 17 Mar 2023 07:56:36 +0000 Subject: [PATCH 4/9] Enhance the function of the addvalue series to create a new branch of the path even if it does not exist, providing the ability to add true, false, and null to the json document Signed-off-by: mazhao --- .../src/oh_adapter/include/jsonOperator.h | 11 +- .../src/oh_adapter/src/jsonInfo.cpp | 1 - .../src/oh_adapter/src/jsonOperator.cpp | 148 +++++++++++++++++- 3 files changed, 150 insertions(+), 10 deletions(-) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h index b1237cac..d66a583d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h @@ -12,14 +12,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #ifndef JSONOPERATOR_H #define JSONOPERATOR_H +#include "cJSON.h" #include #include - -#include "cJSON.h" - class JsonOperator { public: JsonOperator() = default; @@ -35,6 +34,12 @@ public: int AddItemToJson(std::string &jsonstr, const std::string &field, const std::string &Item); + int AddNullToJson(std::string &jsonstr, std::vector path, const std::string &field); + + int AddTrueToJson(std::string &jsonstr, std::vector path, const std::string &field); + + int AddFalseToJson(std::string &jsonstr, std::vector path, const std::string &field); + int AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value); int AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value); diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp index 8dfde194..c3b572db 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp @@ -54,7 +54,6 @@ std::vector> JsonInfo::ParsePath(const std::string &pat std::cout << "projection_json is null"; } std::vector onePath; - parsePath.emplace_back(onePath); ParseNode(projection_json, onePath, parsePath); return parsePath; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp index 0c95677a..2260aad5 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + #include #include #include "jsonOperator.h" @@ -117,9 +118,15 @@ int JsonOperator::AddItemToJson(std::string &jsonstr, const std::string &field, return 0; } -int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value) +int JsonOperator::AddNullToJson(std::string &jsonstr, std::vector path, const std::string &field) { auto cjson_pair = Finditem(jsonstr, path); + std::vector unexistFiled; + while (cjson_pair.first == nullptr) { + unexistFiled.emplace_back(path.back()); + path.pop_back(); + cjson_pair = Finditem(jsonstr, path); + } cJSON* node = cjson_pair.second; cJSON* cJsondata = cjson_pair.first; if (cJsondata == nullptr) { @@ -128,20 +135,32 @@ int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector } if (node->type == cJSON_Array) { cJSON* temp = cJSON_CreateObject(); - cJSON_AddNumberToObject(temp,field.c_str(),value); + cJSON_AddNullToObject(temp,field.c_str()); cJSON_AddItemToArray(node, temp); } else { - cJSON_AddNumberToObject(node, field.c_str(), value); + if (unexistFiled.size() != 0) { + for (int i = unexistFiled.size() - 1; i >= 0; i--) { + cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); + node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); + } + } + cJSON_AddNullToObject(node, field.c_str()); } jsonstr = cJSON_Print(cJsondata); cJSON_Delete(cJsondata); return 0; } -int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value) +int JsonOperator::AddTrueToJson(std::string &jsonstr, std::vector path, const std::string &field) { auto cjson_pair = Finditem(jsonstr, path); + std::vector unexistFiled; + while (cjson_pair.first == nullptr) { + unexistFiled.emplace_back(path.back()); + path.pop_back(); + cjson_pair = Finditem(jsonstr, path); + } cJSON* node = cjson_pair.second; cJSON* cJsondata = cjson_pair.first; if (cJsondata == nullptr) { @@ -150,20 +169,131 @@ int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector } if (node->type == cJSON_Array) { cJSON* temp = cJSON_CreateObject(); - cJSON_AddStringToObject(temp,field.c_str(),value.c_str()); + cJSON_AddTrueToObject(temp,field.c_str()); + cJSON_AddItemToArray(node, temp); + } + else { + if (unexistFiled.size() != 0) { + for (int i = unexistFiled.size() - 1; i >= 0; i--) { + cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); + node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); + } + } + cJSON_AddTrueToObject(node, field.c_str()); + } + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddFalseToJson(std::string &jsonstr, std::vector path, const std::string &field) +{ + auto cjson_pair = Finditem(jsonstr, path); + std::vector unexistFiled; + while (cjson_pair.first == nullptr) { + unexistFiled.emplace_back(path.back()); + path.pop_back(); + cjson_pair = Finditem(jsonstr, path); + } + cJSON* node = cjson_pair.second; + cJSON* cJsondata = cjson_pair.first; + if (cJsondata == nullptr) { + std::cout << "arg is wrong"; + return 0; + } + if (node->type == cJSON_Array) { + cJSON* temp = cJSON_CreateObject(); + cJSON_AddFalseToObject(temp,field.c_str()); + cJSON_AddItemToArray(node, temp); + } + else { + if (unexistFiled.size() != 0) { + for (int i = unexistFiled.size() - 1; i >= 0; i--) { + cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); + node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); + } + } + cJSON_AddFalseToObject(node, field.c_str()); + } + jsonstr = cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + +int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value) +{ + auto cjson_pair = Finditem(jsonstr, path); + std::vector unexistFiled; + while (cjson_pair.first == nullptr) { + unexistFiled.emplace_back(path.back()); + path.pop_back(); + cjson_pair = Finditem(jsonstr, path); + } + cJSON* node = cjson_pair.second; + cJSON* cJsondata = cjson_pair.first; + if (cJsondata == nullptr) { + std::cout << "arg is wrong"; + return 0; + } + if (node->type == cJSON_Array) { + cJSON* temp = cJSON_CreateObject(); + cJSON_AddNumberToObject(temp,field.c_str(),value); cJSON_AddItemToArray(node, temp); } else { - cJSON_AddStringToObject(node, field.c_str(), value.c_str()); + if (unexistFiled.size() != 0) { + for (int i = unexistFiled.size() - 1; i >= 0; i--) { + cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); + node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); + } + } + cJSON_AddNumberToObject(node, field.c_str(), value); } jsonstr = cJSON_Print(cJsondata); cJSON_Delete(cJsondata); return 0; } +int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value) +{ + auto cjson_pair = Finditem(jsonstr, path); + std::vector unexistFiled; + while (cjson_pair.first == nullptr) { + unexistFiled.emplace_back(path.back()); + path.pop_back(); + cjson_pair = Finditem(jsonstr, path); + } + cJSON* node = cjson_pair.second; + cJSON* cJsondata = cjson_pair.first; + if (node->type == cJSON_Array) { + cJSON* temp = cJSON_CreateObject(); + cJSON_AddStringToObject(temp,field.c_str(),value.c_str()); + cJSON_AddItemToArray(node, temp); + } + else { + if (unexistFiled.size() != 0) { + for (int i = unexistFiled.size() - 1; i >= 0; i--) { + cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); + node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); + } + } + cJSON_AddStringToObject(node, field.c_str(), value.c_str()); + + } + std :: cout << cJSON_Print(cJsondata); + cJSON_Delete(cJsondata); + return 0; +} + int JsonOperator::AddItemToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &Item) { auto cjson_pair = Finditem(jsonstr, path); + std::vector unexistFiled; + while (cjson_pair.first == nullptr) { + unexistFiled.emplace_back(path.back()); + path.pop_back(); + cjson_pair = Finditem(jsonstr, path); + } cJSON* node = cjson_pair.second; cJSON* cJsondata = cjson_pair.first; if (cJsondata == nullptr) { @@ -176,6 +306,12 @@ int JsonOperator::AddItemToJson(std::string &jsonstr, std::vector p cJSON_AddItemToArray(node, temp); } else { + if (unexistFiled.size() != 0) { + for (int i = unexistFiled.size() - 1; i >= 0; i--) { + cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); + node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); + } + } cJSON_AddItemToObject(node, field.c_str(), cJSON_Parse(Item.c_str())); } jsonstr = cJSON_Print(cJsondata); -- Gitee From 8083b89041a199c40a2231a03dab2f3996c7bff2 Mon Sep 17 00:00:00 2001 From: mazhao Date: Sat, 18 Mar 2023 07:50:42 +0000 Subject: [PATCH 5/9] ProjectionTree apply Signed-off-by: mazhao --- .../include/grd_document/projectionTree.h | 50 +++++++++++++++ .../src/executor/document/projectionTree.cpp | 62 +++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/projectionTree.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/projectionTree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/projectionTree.h new file mode 100644 index 00000000..fde095f0 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/projectionTree.h @@ -0,0 +1,50 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef PROJECTION_TREE_H +#define PROJECTION_TREE_H + +#include +#include +#include + +struct ProjectionNode { + std::unordered_map SonNode; + bool isDeepest; + int Deep; + ProjectionNode() { + Deep = 1; + isDeepest = true; + } +}; + +class ProjectionTree { +public: + ProjectionTree() = default; + + ~ProjectionTree() = default; + + void Init() { + root_ = new ProjectionNode(); + } + + int ParseTree(std::vector> &path); + + bool SerchTree(std::vector &onePath); + +private: + ProjectionNode *root_; +}; +#endif \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp new file mode 100644 index 00000000..cb8c5d68 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp @@ -0,0 +1,62 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include +#include "projectionTree.h" + +int ProjectionTree::ParseTree(std::vector> &path) { + ProjectionNode *node = root_; + if (node == NULL) { + std::cout<<"root_ is null"; + return 0; + } + for (int i = 0; i < path.size(); i++) { + node = root_; + for (int j = 0; j < path[i].size(); j++) { + if (node->SonNode[path[i][j]] != nullptr) { + node = node->SonNode[path[i][j]]; + if (j < path[i].size() - 1 && node->isDeepest == true) { + std::cout<<"projection format is wrong"; + return 0; + } + if (j == path[i].size() - 1 && node->isDeepest == false) { + std::cout<<"projection format is wrong"; + return 0; + } + } + else { + auto tempNode = new ProjectionNode; + tempNode->Deep = node->Deep + 1; + node->isDeepest = false; + node->SonNode[path[i][j]] = tempNode; + node = node->SonNode[path[i][j]]; + } + } + } + return 0; +} + +bool ProjectionTree::SerchTree(std::vector &onePath) { + ProjectionNode *node = root_; + for (int i = 0; i < onePath.size(); i++) { + if (node->SonNode[onePath[i]]) { + node = node->SonNode[onePath[i]]; + } + else { + return false; + } + } + return true; +} \ No newline at end of file -- Gitee From 13da51ae203b8f6536d21eb30281bb7f87df1f7f Mon Sep 17 00:00:00 2001 From: mazhao Date: Sat, 18 Mar 2023 08:46:31 +0000 Subject: [PATCH 6/9] check documentFormat function apply Signed-off-by: mazhao --- .../src/oh_adapter/include/jsonInfo.h | 4 +++ .../src/oh_adapter/src/jsonInfo.cpp | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h index 4d6cf3eb..a06ab80a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h @@ -29,7 +29,11 @@ public: int GetJsonDeep(const std::string &jsonstr); + int CheckJsonField(const std::string &jsonstr); + private: int ParseNode(cJSON *Node, std::vector onePath, std::vector> &parsePath); + + int CheckNode(cJSON *Node); }; #endif \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp index c3b572db..81b96346 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp @@ -74,4 +74,37 @@ int JsonInfo::GetJsonDeep(const std::string &jsonstr) } } return deep; +} + +int JsonInfo::CheckNode(cJSON *Node) +{ + if (Node->type == cJSON_Object) { + std::string field_str; + if (Node->child != nullptr) { + field_str = Node->child->string; + } + for (int i = 0; i < field_str.size(); i++) { + if (!(('a'<=field_str[i] && field_str[i]<='z')|| ('A'<=field_str[i] && field_str[i]<='Z') || ('0'<=field_str[i] && field_str[i]<='9') || '_' == field_str[i])) { + std::cout << "wrong"; + return 1; + } + } + } + if (Node->child != nullptr) { + return CheckNode(Node->child); + } + if (Node->next != nullptr) { + return CheckNode(Node->next); + } + return 0; +} + +int JsonInfo::CheckJsonField(const std::string &jsonstr) +{ + auto cJsondata = cJSON_Parse(jsonstr.c_str()); + if (cJsondata == nullptr) { + std::cout << "wrong"; + return 0; + } + return CheckNode(cJsondata); } \ No newline at end of file -- Gitee From a84d1b378267c273bec38a1e517c20c4ed996266 Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 21 Mar 2023 02:56:36 +0000 Subject: [PATCH 7/9] supply insert demo and its ut Signed-off-by: mazhao --- .../gaussdb_rd_Simple/CMakeLists.txt | 1 + .../include/grd_document/grd_document_api.h | 4 +- .../executor/document/grd_document_api.cpp | 56 ++++++++++++++++ .../executor/include}/projectionTree.h | 0 .../src/interface/include/document_store.h | 1 + .../src/interface/src/document_store.cpp | 5 ++ .../src/oh_adapter/include/kv_store_manager.h | 1 + .../src/oh_adapter/src/kv_store_manager.cpp | 1 - .../src/sqlite_store_executor_impl.cpp | 31 +++++++++ .../unittest/api/documentdb_insert_test.cpp | 66 +++++++++++++++++++ .../unittest/api/doucumentdb_api_test.cpp | 1 + 11 files changed, 164 insertions(+), 3 deletions(-) rename services/distributeddataservice/service/data_share/gaussdb_rd_Simple/{include/grd_document => src/executor/include}/projectionTree.h (100%) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_insert_test.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt index 7686fe99..4d7754bd 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/CMakeLists.txt @@ -160,6 +160,7 @@ include_directories( ${PROJECT_SOURCE_DIR}/src/interface ${PROJECT_SOURCE_DIR}/src/interface/include ${PROJECT_SOURCE_DIR}/src/interface/src + ${PROJECT_SOURCE_DIR}/src/executor/include ) set(DISTRIBUTEDDB_PATH ${PROJECT_SOURCE_DIR}/third_party/distributeddatamgr_kv_store/frameworks/libs/distributeddb/) diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h index 09f1719c..fc062be4 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/grd_document_api.h @@ -16,8 +16,8 @@ #ifndef GRD_DOCUMENT_API_H #define GRD_DOCUMENT_API_H -#include "grd_type_export.h" -#include "grd_resultset_api.h" +#include "grd_base/grd_type_export.h" +#include "grd_base/grd_resultset_api.h" #ifdef __cplusplus extern "C" { diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp index e69de29b..e2702e76 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/grd_document_api.cpp @@ -0,0 +1,56 @@ + +#include "grd_base/grd_db_api.h" +#include "grd_base/grd_error.h" +#include "document_store_manager.h" +#include "document_store.h" +#include "doc_errno.h" +#include "grd_document/grd_document_api.h" + +using namespace DocumentDB; + +typedef struct GRD_DB { + DocumentStore *store_ = nullptr; +} GRD_DB; + +int GRD_CreateCollection(GRD_DB *db, const char *collectionName, const char *optionStr, unsigned int flags) +{ + return GRD_OK; +} + +int GRD_DropCollection(GRD_DB *db, const char *collectionName, unsigned int flags) +{ + return GRD_OK; +} + +int GRD_InsertDoc(GRD_DB *db, const char *collectionName, const char *document, unsigned int flags) +{ + auto store = db->store_; + std::string str1 = collectionName; + std::string str2 = document; + Key key(str1.c_str(), str1.c_str() + str1.size()); + Value value(str2.c_str(), str2.c_str() + str2.size()); + if (store->PutData(key, value) != E_OK) { + return GRD_FIELD_NOT_FOUND; + } + return GRD_OK;; +} + +int GRD_FindDoc(GRD_DB *db, const char *collectionName, Query query, unsigned int flags, GRD_ResultSet **resultSet) +{ + return GRD_OK; +} + +int GRD_UpdateDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *update, unsigned int flags) +{ + return GRD_OK; +} + +int GRD_UpSertDoc(GRD_DB *db, const char *collectionName, const char *filter, const char *document, unsigned int flags) +{ + return GRD_OK; +} + +int GRD_DeleteDoc(GRD_DB *db, const char *collectionName, const char *filter, unsigned int flags) +{ + return GRD_OK; +} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/projectionTree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/projectionTree.h similarity index 100% rename from services/distributeddataservice/service/data_share/gaussdb_rd_Simple/include/grd_document/projectionTree.h rename to services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/projectionTree.h diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h index 52bb3c29..d9ba9679 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/include/document_store.h @@ -23,6 +23,7 @@ class DocumentStore { public: DocumentStore(KvStoreExecutor *); ~DocumentStore(); + int PutData(const Key &key, const Value &value); private: KvStoreExecutor *executor_ = nullptr; }; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp index ac9e5ad4..c5b1ff9a 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/interface/src/document_store.cpp @@ -24,4 +24,9 @@ DocumentStore::~DocumentStore() { delete executor_; } + +int DocumentStore::PutData(const Key &key, const Value &value) +{ + executor_->PutData(key, value); +} } // DocumentDB \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h index 6c82e983..a2a8baeb 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/kv_store_manager.h @@ -24,6 +24,7 @@ namespace DocumentDB { class KvStoreManager { public: static int GetKvStore(const std::string &path, KvStoreExecutor *&executor); + static int InsertKvStore(); }; } // DocumentDB #endif // KV_STORE_MANAGER_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp index d4c91bcd..dbc8e278 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/kv_store_manager.cpp @@ -41,7 +41,6 @@ int KvStoreManager::GetKvStore(const std::string &path, KvStoreExecutor *&execut if (db == nullptr) { return -E_ERROR; } - executor = new (std::nothrow) SqliteStoreExecutor(db); return E_OK; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp index 78f76313..68580312 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/sqlite_store_executor_impl.cpp @@ -12,7 +12,9 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include "securec.h" #include "doc_errno.h" +#include "log_print.h" #include "sqlite_store_executor_impl.h" namespace DocumentDB { @@ -28,6 +30,35 @@ SqliteStoreExecutor::~SqliteStoreExecutor() int SqliteStoreExecutor::PutData(const Key &key, const Value &value) { + sqlite3 *db = dbHandle_; + int rc; + const std::string sql_creat = "CREATE TABLE JSON(" \ + "id varchar(255)," \ + "json varchar(255) );"; + char *errMsg = nullptr; + rc = sqlite3_exec(db, sql_creat.c_str(), 0, 0, &errMsg); + if( rc != SQLITE_OK ){ + return E_ERROR; + } + int rc_2; + std::string sql_key(key.begin(), key.end()); + sql_key.insert(0, "\'"); + sql_key.push_back('\''); + std::string sql_value(value.begin(), value.end()); + sql_value.insert(0, "\'"); + sql_value.push_back('\''); + const std::string sql_insert_head = "INSERT INTO JSON VALUES ("; + const std::string sql_insert_middle_key = sql_key; + const std::string sql_insert_middle_dot = ","; + const std::string sql_insert_middle_value = sql_value; + const std::string sql_insert_tail = ");"; + const std::string sql_insert = sql_insert_head + sql_insert_middle_key + sql_insert_middle_dot + sql_insert_middle_value + sql_insert_tail; + char *errMsg_2 = nullptr; + rc_2 = sqlite3_exec(db, sql_insert.c_str(), 0, 0, &errMsg_2); + if( rc_2 != SQLITE_OK ){ + return E_ERROR; + } + sqlite3_close(db); return E_OK; } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_insert_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_insert_test.cpp new file mode 100644 index 00000000..6c864a72 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/documentdb_insert_test.cpp @@ -0,0 +1,66 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#include + +#include "grd_base/grd_db_api.h" +#include "grd_document/grd_document_api.h" +#include "grd_base/grd_error.h" +using namespace testing::ext; + +std::string path = "./document.db"; +GRD_DB *g_db = nullptr; + +class DocumentInsertApiTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); +}; + +void DocumentInsertApiTest::SetUpTestCase(void) +{ + GRD_DBOpen(path.c_str(), nullptr, 0, &g_db); + EXPECT_NE(g_db, nullptr); +} + +void DocumentInsertApiTest::TearDownTestCase(void) +{ +} + +void DocumentInsertApiTest::SetUp(void) +{ +} + +void DocumentInsertApiTest::TearDown(void) +{ +} + + +/** + * @tc.name: OpenDBTest001 + * @tc.desc: Test Insert document db + * @tc.type: FUNC + * @tc.require: + * @tc.author: mazhao + */ + +HWTEST_F(DocumentInsertApiTest, InsertDBTest001, TestSize.Level1) +{ + const char *key = "id"; + const char *document = "{\"id\":\"1\"}"; + EXPECT_EQ(GRD_InsertDoc(g_db, key, document, 0), GRD_OK); +} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp index ea2b198f..d3ba2596 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/test/unittest/api/doucumentdb_api_test.cpp @@ -18,6 +18,7 @@ #include "log_print.h" #include "grd_base/grd_db_api.h" #include "grd_base/grd_error.h" +#include "grd_document/grd_document_api.h" using namespace DocumentDB; using namespace testing::ext; -- Gitee From 161acc4f7e854765d91dc835559b52282133b051 Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 21 Mar 2023 08:22:26 +0000 Subject: [PATCH 8/9] json abillty supply Signed-off-by: mazhao --- .../src/common/include/doc_common.h | 23 + .../src/common/include/json_common.h | 39 ++ .../src/common/src/json_common.cpp | 0 .../src/oh_adapter/include/jsonOperator.h | 60 --- .../src/oh_adapter/include/json_object.h | 45 ++ .../jsonInfo.h => src/cjson_object.h} | 38 +- .../src/oh_adapter/src/jsonInfo.cpp | 110 ----- .../src/oh_adapter/src/jsonOperator.cpp | 398 ------------------ .../src/oh_adapter/src/json_object.cpp | 0 9 files changed, 130 insertions(+), 583 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h rename services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/{include/jsonInfo.h => src/cjson_object.h} (55%) delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp delete mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h index a4919b67..4d6d7f44 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h @@ -18,9 +18,32 @@ #include #include +#include "json_obect.h" + +struct ResultValue { + enum class ValueType { + VALUE_INT, + VALUE_TRUE, + VALUE_FALSE, + VALUE_STRING, + VALUE_OBJECT, + VALUE_ARRAY + }; + int ResultValue_int; + std::string ResultValue_string; + ValueType ResultValue_type; + JsonObject *jsonObject; +} namespace DocumentDB { using Key = std::vector; using Value = std::vector; + +int GetJsonDeep(const std::string &jsonStr, int &jsonDeep); +int CheckColletionNameFormat(JsonObject *collectionName); +int CheckDBFormat(GRD_DB *DB); +int CheckfilterFormat(JsonObject *filter); + + } // DocumentDB #endif // DOC_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h new file mode 100644 index 00000000..c454160b --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -0,0 +1,39 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef json_COMMON_H +#define json_COMMON_H + +#include +#include +#include "json_obect.h" + + +namespace DocumentDB { + JsonOperator() = default; + ~JsonOperator() = default; + + std::string GetValue(JsonObect *jsonObect, std::vector path); + + int DeleteJsonData(JsonObect *jsonObect, std::vector path); + + std::vector GetJsonBrotherField(JsonObect *jsonObect, std::vector path); + + std::pair FindItem (JsonObect *jsonObect, std::vector path);//pair.first is JsonObect_root + + std::vector> ParsePath(JsonObect *jsonObect); + +} // DocumentDB +#endif // json_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/src/json_common.cpp new file mode 100644 index 00000000..e69de29b diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h deleted file mode 100644 index d66a583d..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonOperator.h +++ /dev/null @@ -1,60 +0,0 @@ -/* -* Copyright (c) 2023 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#ifndef JSONOPERATOR_H -#define JSONOPERATOR_H - -#include "cJSON.h" -#include -#include -class JsonOperator { -public: - JsonOperator() = default; - ~JsonOperator() = default; - - std::string GetValue(std::string &jsonstr, std::vector path); - - int GetValueType(std::string &jsonstr, std::vector path); - - int AddValueToJson(std::string &jsonstr, const std::string &field, const int value); - - int AddValueToJson(std::string &jsonstr, const std::string &field, const std::string &value); - - int AddItemToJson(std::string &jsonstr, const std::string &field, const std::string &Item); - - int AddNullToJson(std::string &jsonstr, std::vector path, const std::string &field); - - int AddTrueToJson(std::string &jsonstr, std::vector path, const std::string &field); - - int AddFalseToJson(std::string &jsonstr, std::vector path, const std::string &field); - - int AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value); - - int AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value); - - int AddItemToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &Item); - - int DeleteJsonData(std::string &jsonstr, std::vector path); - - std::vector GetBrotherField(std::string &jsonstr, std::vector path); - - const int JsonOperator_int = 8; - const int JsonOperator_string = 16; - const int JsonOperator_array = 32; - const int JsonOperator_object = 64; -private: - std::pair Finditem(std::string &jsonstr, const std::vector json_path); -}; -#endif \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h new file mode 100644 index 00000000..3d3163e5 --- /dev/null +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/json_object.h @@ -0,0 +1,45 @@ +/* +* Copyright (c) 2023 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +#ifndef JSON_OBJECT_H +#define JSON_OBJECT_H + +#include "doc_common.h" + +namespace DocumentDB { +class JsonObect { +public: + JsonObect () = default; + virtual ~JsonObect() = default; + + virtual int Init() = 0; + + virtual int Print() = 0; + + virtual int GetItemObject() = 0; + + virtual int GetItemArray() = 0; + + virtual int GetArraySize() = 0; + + virtual int GetItemValue() = 0; + + virtual int DelectItemFromObect() = 0; + + JsonObect *next = nullptr; + JsonObect *child = nullptr; +}; +} // DocumentDB +#endif // JSON_OBJECT_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h similarity index 55% rename from services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h rename to services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h index a06ab80a..f7b678cf 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/include/jsonInfo.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h @@ -13,27 +13,35 @@ * limitations under the License. */ -#ifndef JSONINFO_H -#define JSONINFO_H +#ifndef CJSON_OBJECT_H +#define CJSON_OBJECT_H +#include "json_object.h" #include "cJSON.h" -#include -#include -class JsonInfo { +#include "doc_common.h" + +namespace DocumentDB { +class cJsonObect : public JsonObect { public: - JsonInfo() = default; + cJsonObect(); + virtual ~cJsonObect() override; + + int Parse() override; + + int Print() override; + + int GetItemObject() override; + + int GetItemArray() override; - ~JsonInfo() = default; + int GetArraySize() override; - std::vector> ParsePath(const std::string &path); - - int GetJsonDeep(const std::string &jsonstr); + int GetItemValue() override; - int CheckJsonField(const std::string &jsonstr); + int DelectItemFromObect() override; private: - int ParseNode(cJSON *Node, std::vector onePath, std::vector> &parsePath); - - int CheckNode(cJSON *Node); + cJSON *cjson_; }; -#endif \ No newline at end of file +} // DocumentDB +#endif // SQLITE_STORE_EXECUTOR_IMPL_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp deleted file mode 100644 index 81b96346..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonInfo.cpp +++ /dev/null @@ -1,110 +0,0 @@ -/* -* Copyright (c) 2023 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include -#include "jsonInfo.h" - -int JsonInfo::ParseNode(cJSON *Node, std::vector onePath, std::vector> &parsePath) -{ - std::string tempparse_name; - std::vector parsed_mixfiled_name; - std::string mixfield_name = Node->string; - for (int j = 0; j < mixfield_name.size(); j++) { - if (mixfield_name[j] != '.') { - tempparse_name = tempparse_name + mixfield_name[j]; - } - if (mixfield_name[j] == '.' || j == mixfield_name.size() - 1) { - parsed_mixfiled_name.emplace_back(tempparse_name); - tempparse_name.clear(); - } - } - std::vector forePath; - forePath = onePath; - onePath.insert(onePath.end(), parsed_mixfiled_name.begin(), parsed_mixfiled_name.end()); - if (Node->child != nullptr && Node->child->string != nullptr) { - ParseNode(Node->child, onePath, parsePath); - } - else { - parsePath.emplace_back(onePath); - } - if (Node->next != nullptr) { - ParseNode(Node->next, forePath, parsePath); - } - return 0; -} - -std::vector> JsonInfo::ParsePath(const std::string &path) -{ - std::vector> parsePath; - cJSON *root = cJSON_Parse(path.c_str()); - cJSON *projection_json = root->child; - if (projection_json == nullptr) { - std::cout << "projection_json is null"; - } - std::vector onePath; - ParseNode(projection_json, onePath, parsePath); - return parsePath; -} - -int JsonInfo::GetJsonDeep(const std::string &jsonstr) -{ - auto cJsondata = cJSON_Parse(jsonstr.c_str()); - std::string printed_str = cJSON_Print(cJsondata); - int lens = 0; - int deep = 0; - for (int i = 0; i < printed_str.size(); i++) { - if (printed_str[i] == '[' || printed_str[i] == '{') { - lens++; - } - else if (printed_str[i] == ']' || printed_str[i] == '}') { - deep = std::max(deep, lens); - lens--; - } - } - return deep; -} - -int JsonInfo::CheckNode(cJSON *Node) -{ - if (Node->type == cJSON_Object) { - std::string field_str; - if (Node->child != nullptr) { - field_str = Node->child->string; - } - for (int i = 0; i < field_str.size(); i++) { - if (!(('a'<=field_str[i] && field_str[i]<='z')|| ('A'<=field_str[i] && field_str[i]<='Z') || ('0'<=field_str[i] && field_str[i]<='9') || '_' == field_str[i])) { - std::cout << "wrong"; - return 1; - } - } - } - if (Node->child != nullptr) { - return CheckNode(Node->child); - } - if (Node->next != nullptr) { - return CheckNode(Node->next); - } - return 0; -} - -int JsonInfo::CheckJsonField(const std::string &jsonstr) -{ - auto cJsondata = cJSON_Parse(jsonstr.c_str()); - if (cJsondata == nullptr) { - std::cout << "wrong"; - return 0; - } - return CheckNode(cJsondata); -} \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp deleted file mode 100644 index 2260aad5..00000000 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/jsonOperator.cpp +++ /dev/null @@ -1,398 +0,0 @@ -/* -* Copyright (c) 2023 Huawei Device Co., Ltd. -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ - -#include -#include -#include "jsonOperator.h" - -std::pair JsonOperator::Finditem(std::string &jsonstr, const std::vector json_path) -{ - cJSON *cJsondata_item = cJSON_Parse(jsonstr.c_str()); - cJSON *cJsondata_temp; - cJSON *cJsondata_root; - cJsondata_root = cJsondata_item; - if (json_path.size() != 0 && json_path[0] == "") { - return std::pair(cJsondata_item, cJsondata_item); - } - for (int i = 0; i < json_path.size(); i++) { - if (cJsondata_item->type == cJSON_Object) { - if (cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()) == nullptr) { - return std::pair(nullptr, nullptr); - } - cJsondata_temp = cJSON_GetObjectItem(cJsondata_item,json_path[i].c_str()); - cJsondata_item = cJsondata_temp; - } - if (cJsondata_item->type == cJSON_Array) { - int nums = 0; - for (int j = 0; j < json_path[i].size(); j++) { - if (json_path[i][j]-'0' > 9 || json_path[i][j] - '0' < 0 || json_path[i][j] - '0' > cJSON_GetArraySize(cJsondata_item)) { - break; - } - auto GetArrayret = cJSON_GetArrayItem(cJsondata_item, json_path[i][j]-'0'); - if (typeid(GetArrayret) == typeid(cJSON*)) { - cJsondata_item = GetArrayret; - } - else { - if ( i != json_path.size() - 1) { - std::cout << "Patharg wrong"; - } - else { - return std::pair(nullptr, nullptr); - } - } - } - } - - } - return std::pair(cJsondata_root, cJsondata_item); -} - -std::string JsonOperator::GetValue(std::string &jsonstr, std::vector path) -{ - auto cjson_pair = Finditem(jsonstr, path); - auto cjsondata = cjson_pair.second; - if (cjsondata == nullptr) { - std::cout << "arg is wrong"; - return ""; - } - std::string cjsondata_str = cJSON_Print(cjsondata); - cJSON_Delete(cjsondata); - return cjsondata_str; -} - -int JsonOperator::GetValueType(std::string &jsonstr, std::vector path) -{ - int type; - auto cjson_pair = Finditem(jsonstr, path); - auto cjsondata = cjson_pair.second; - if (cjsondata == nullptr) { - std::cout << "arg is wrong"; - return 0; - } - type = cjsondata->type; - cJSON_Delete(cjsondata); - return type; -} - -int JsonOperator::AddValueToJson(std::string &jsonstr, const std::string &field, const int value) -{ - cJSON *cJsondata = cJSON_Parse(jsonstr.c_str()); - cJSON_AddNumberToObject(cJsondata,field.c_str(),value); - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddValueToJson(std::string &jsonstr, const std::string &field, const std::string &value) -{ - cJSON *cJsondata = cJSON_Parse(jsonstr.c_str()); - cJSON_AddStringToObject(cJsondata,field.c_str(),value.c_str()); - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddItemToJson(std::string &jsonstr, const std::string &field, const std::string &Item) -{ - cJSON *cJsondata = cJSON_Parse(jsonstr.c_str()); - cJSON* node = cJSON_Parse(Item.c_str()); - if (node == nullptr) { - std::cout << "creat cjson false"; - return 0; - } - cJSON_AddItemToObject(cJsondata, field.c_str(), node); - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddNullToJson(std::string &jsonstr, std::vector path, const std::string &field) -{ - auto cjson_pair = Finditem(jsonstr, path); - std::vector unexistFiled; - while (cjson_pair.first == nullptr) { - unexistFiled.emplace_back(path.back()); - path.pop_back(); - cjson_pair = Finditem(jsonstr, path); - } - cJSON* node = cjson_pair.second; - cJSON* cJsondata = cjson_pair.first; - if (cJsondata == nullptr) { - std::cout << "arg is wrong"; - return 0; - } - if (node->type == cJSON_Array) { - cJSON* temp = cJSON_CreateObject(); - cJSON_AddNullToObject(temp,field.c_str()); - cJSON_AddItemToArray(node, temp); - } - else { - if (unexistFiled.size() != 0) { - for (int i = unexistFiled.size() - 1; i >= 0; i--) { - cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); - node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); - } - } - cJSON_AddNullToObject(node, field.c_str()); - } - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddTrueToJson(std::string &jsonstr, std::vector path, const std::string &field) -{ - auto cjson_pair = Finditem(jsonstr, path); - std::vector unexistFiled; - while (cjson_pair.first == nullptr) { - unexistFiled.emplace_back(path.back()); - path.pop_back(); - cjson_pair = Finditem(jsonstr, path); - } - cJSON* node = cjson_pair.second; - cJSON* cJsondata = cjson_pair.first; - if (cJsondata == nullptr) { - std::cout << "arg is wrong"; - return 0; - } - if (node->type == cJSON_Array) { - cJSON* temp = cJSON_CreateObject(); - cJSON_AddTrueToObject(temp,field.c_str()); - cJSON_AddItemToArray(node, temp); - } - else { - if (unexistFiled.size() != 0) { - for (int i = unexistFiled.size() - 1; i >= 0; i--) { - cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); - node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); - } - } - cJSON_AddTrueToObject(node, field.c_str()); - } - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddFalseToJson(std::string &jsonstr, std::vector path, const std::string &field) -{ - auto cjson_pair = Finditem(jsonstr, path); - std::vector unexistFiled; - while (cjson_pair.first == nullptr) { - unexistFiled.emplace_back(path.back()); - path.pop_back(); - cjson_pair = Finditem(jsonstr, path); - } - cJSON* node = cjson_pair.second; - cJSON* cJsondata = cjson_pair.first; - if (cJsondata == nullptr) { - std::cout << "arg is wrong"; - return 0; - } - if (node->type == cJSON_Array) { - cJSON* temp = cJSON_CreateObject(); - cJSON_AddFalseToObject(temp,field.c_str()); - cJSON_AddItemToArray(node, temp); - } - else { - if (unexistFiled.size() != 0) { - for (int i = unexistFiled.size() - 1; i >= 0; i--) { - cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); - node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); - } - } - cJSON_AddFalseToObject(node, field.c_str()); - } - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const int value) -{ - auto cjson_pair = Finditem(jsonstr, path); - std::vector unexistFiled; - while (cjson_pair.first == nullptr) { - unexistFiled.emplace_back(path.back()); - path.pop_back(); - cjson_pair = Finditem(jsonstr, path); - } - cJSON* node = cjson_pair.second; - cJSON* cJsondata = cjson_pair.first; - if (cJsondata == nullptr) { - std::cout << "arg is wrong"; - return 0; - } - if (node->type == cJSON_Array) { - cJSON* temp = cJSON_CreateObject(); - cJSON_AddNumberToObject(temp,field.c_str(),value); - cJSON_AddItemToArray(node, temp); - } - else { - if (unexistFiled.size() != 0) { - for (int i = unexistFiled.size() - 1; i >= 0; i--) { - cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); - node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); - } - } - cJSON_AddNumberToObject(node, field.c_str(), value); - } - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddValueToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &value) -{ - auto cjson_pair = Finditem(jsonstr, path); - std::vector unexistFiled; - while (cjson_pair.first == nullptr) { - unexistFiled.emplace_back(path.back()); - path.pop_back(); - cjson_pair = Finditem(jsonstr, path); - } - cJSON* node = cjson_pair.second; - cJSON* cJsondata = cjson_pair.first; - if (node->type == cJSON_Array) { - cJSON* temp = cJSON_CreateObject(); - cJSON_AddStringToObject(temp,field.c_str(),value.c_str()); - cJSON_AddItemToArray(node, temp); - } - else { - if (unexistFiled.size() != 0) { - for (int i = unexistFiled.size() - 1; i >= 0; i--) { - cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); - node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); - } - } - cJSON_AddStringToObject(node, field.c_str(), value.c_str()); - - } - std :: cout << cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::AddItemToJson(std::string &jsonstr, std::vector path, const std::string &field, const std::string &Item) -{ - auto cjson_pair = Finditem(jsonstr, path); - std::vector unexistFiled; - while (cjson_pair.first == nullptr) { - unexistFiled.emplace_back(path.back()); - path.pop_back(); - cjson_pair = Finditem(jsonstr, path); - } - cJSON* node = cjson_pair.second; - cJSON* cJsondata = cjson_pair.first; - if (cJsondata == nullptr) { - std::cout << "arg is wrong"; - return 0; - } - if (node->type == cJSON_Array) { - cJSON* temp = cJSON_CreateObject(); - cJSON_AddItemToObject(temp,field.c_str(), cJSON_Parse(Item.c_str())); - cJSON_AddItemToArray(node, temp); - } - else { - if (unexistFiled.size() != 0) { - for (int i = unexistFiled.size() - 1; i >= 0; i--) { - cJSON_AddItemToObject(node, unexistFiled[i].c_str(), cJSON_CreateObject()); - node = cJSON_GetObjectItem(node, unexistFiled[i].c_str()); - } - } - cJSON_AddItemToObject(node, field.c_str(), cJSON_Parse(Item.c_str())); - } - jsonstr = cJSON_Print(cJsondata); - cJSON_Delete(cJsondata); - return 0; -} - -int JsonOperator::DeleteJsonData(std::string &jsonstr, std::vector path) -{ - cJSON *node_father; - cJSON *cJsondata_root; - std::string lastString; - if (path.size()>0) { - lastString = path.back(); - path.pop_back(); - if (path.size() == 0) { - std::vector emptyPath; - auto cjson_pair = Finditem(jsonstr, emptyPath); - node_father = cjson_pair.second; - cJsondata_root = cjson_pair.first; - path.emplace_back(lastString); - } - else { - auto cjson_pair = Finditem(jsonstr, path); - node_father = cjson_pair.second; - cJsondata_root = cjson_pair.first; - path.emplace_back(lastString); - } - } - if (node_father->type == cJSON_Object) { - if (cJSON_GetObjectItem(node_father, lastString.c_str()) != nullptr) { - cJSON_DeleteItemFromObject(node_father, lastString.c_str()); - } - else { - std::cout <<"no item that can be deleted"; - } - } - if (node_father->type == cJSON_Array) { - if (cJSON_GetArrayItem(node_father, lastString[0] - '0') != nullptr) { - cJSON_DeleteItemFromArray(node_father, lastString[0] - '0'); - } - else { - std::cout <<"no item that can be deleted"; - } - } - jsonstr = cJSON_Print(cJsondata_root); - cJSON_Delete(cJsondata_root); - return 0; -} - -std::vector JsonOperator::GetBrotherField(std::string &jsonstr, std::vector path) -{ - std::vector brotherField; - cJSON *cJsondata; - auto cjson_pair = Finditem(jsonstr, path); - if (cjson_pair.second == nullptr) { - return brotherField; - } - if (cjson_pair.second->type == cJSON_Object) { - if (cjson_pair.second->child != nullptr) { - cJsondata = cjson_pair.second->child; - } - while (cJsondata->prev != nullptr) { - cJsondata = cJsondata->prev; - } - while (cJsondata != nullptr) { - brotherField.emplace_back(cJsondata->string); - cJsondata = cJsondata->next; - } - } - if (cjson_pair.second->type == cJSON_Array) { - cJsondata = cjson_pair.second; - int array_lens = cJSON_GetArraySize(cJsondata); - for (int i = 0; i < array_lens; i++) { - if (cJSON_GetArrayItem(cJsondata, i)->type != cJSON_Object) { - brotherField.emplace_back(""); - } - else { - brotherField.emplace_back(std::to_string(i)); - } - } - } - return brotherField; -} diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/json_object.cpp new file mode 100644 index 00000000..e69de29b -- Gitee From 3f49799da071beb40b312c6b736d77670397a092 Mon Sep 17 00:00:00 2001 From: mazhao Date: Tue, 21 Mar 2023 08:46:34 +0000 Subject: [PATCH 9/9] json abillty supply modify Signed-off-by: mazhao --- .../gaussdb_rd_Simple/src/common/include/doc_common.h | 3 +-- .../gaussdb_rd_Simple/src/common/include/json_common.h | 7 ++++--- .../src/executor/document/projectionTree.cpp | 9 ++++++--- .../src/executor/include/projectionTree.h | 4 +++- .../src/oh_adapter/src/cjson_object.cpp | 0 .../gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h | 6 +++--- 6 files changed, 17 insertions(+), 12 deletions(-) create mode 100644 services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h index 4d6d7f44..f6c0f55c 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/doc_common.h @@ -20,6 +20,7 @@ #include #include "json_obect.h" +namespace DocumentDB { struct ResultValue { enum class ValueType { VALUE_INT, @@ -34,8 +35,6 @@ struct ResultValue { ValueType ResultValue_type; JsonObject *jsonObject; } - -namespace DocumentDB { using Key = std::vector; using Value = std::vector; diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h index c454160b..90cb580d 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/common/include/json_common.h @@ -22,8 +22,10 @@ namespace DocumentDB { - JsonOperator() = default; - ~JsonOperator() = default; +class JsonCommon { +public: + JsonCommon() = default; + ~JsonCommon() = default; std::string GetValue(JsonObect *jsonObect, std::vector path); @@ -34,6 +36,5 @@ namespace DocumentDB { std::pair FindItem (JsonObect *jsonObect, std::vector path);//pair.first is JsonObect_root std::vector> ParsePath(JsonObect *jsonObect); - } // DocumentDB #endif // json_COMMON_H \ No newline at end of file diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp index cb8c5d68..2653bb6b 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/document/projectionTree.cpp @@ -15,11 +15,14 @@ #include #include "projectionTree.h" +#include "log_print.h" + +using namespace DocumentDB; int ProjectionTree::ParseTree(std::vector> &path) { ProjectionNode *node = root_; if (node == NULL) { - std::cout<<"root_ is null"; + GLOGX("root_ is null"); return 0; } for (int i = 0; i < path.size(); i++) { @@ -28,11 +31,11 @@ int ProjectionTree::ParseTree(std::vector> &path) { if (node->SonNode[path[i][j]] != nullptr) { node = node->SonNode[path[i][j]]; if (j < path[i].size() - 1 && node->isDeepest == true) { - std::cout<<"projection format is wrong"; + GLOGX("projection format is wrong"); return 0; } if (j == path[i].size() - 1 && node->isDeepest == false) { - std::cout<<"projection format is wrong"; + GLOGX("projection format is wrong"); return 0; } } diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/projectionTree.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/projectionTree.h index fde095f0..4b891b44 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/projectionTree.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/executor/include/projectionTree.h @@ -20,6 +20,7 @@ #include #include +namespace DocumentDB { struct ProjectionNode { std::unordered_map SonNode; bool isDeepest; @@ -47,4 +48,5 @@ public: private: ProjectionNode *root_; }; -#endif \ No newline at end of file +} // DocumentDB +#endif // PROJECTION_TREE_H diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.cpp new file mode 100644 index 00000000..e69de29b diff --git a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h index f7b678cf..8064c400 100644 --- a/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h +++ b/services/distributeddataservice/service/data_share/gaussdb_rd_Simple/src/oh_adapter/src/cjson_object.h @@ -21,10 +21,10 @@ #include "doc_common.h" namespace DocumentDB { -class cJsonObect : public JsonObect { +class cJsonObject : public cJsonObject { public: - cJsonObect(); - virtual ~cJsonObect() override; + cJsonObject(); + virtual ~cJsonObject() override; int Parse() override; -- Gitee