From 813f1aaf1f951d8e89ec02b224b6728b839aba36 Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Sat, 18 Mar 2017 07:20:08 -0700 Subject: [PATCH] Separate SchemaChecker from Schema to reuse regex while parsing schema files Change-Id: I81735406af570af5902905f739c362fcf6ffdf28 Signed-off-by: Mu-Woong Lee --- src/server/Schema.cpp | 25 ++++++++++++++++ src/server/Schema.h | 48 ++++++++++++++++++++++++++++++ src/server/SchemaChecker.cpp | 61 ++++++++++++++++++++++++++++++++++++++ src/server/SchemaChecker.h | 41 ++++++++++++++++++++++++++ src/server/SchemaLoader.cpp | 70 ++++++++++++++++---------------------------- src/server/SchemaLoader.h | 25 ++++------------ 6 files changed, 205 insertions(+), 65 deletions(-) create mode 100644 src/server/Schema.cpp create mode 100644 src/server/Schema.h create mode 100644 src/server/SchemaChecker.cpp create mode 100644 src/server/SchemaChecker.h diff --git a/src/server/Schema.cpp b/src/server/Schema.cpp new file mode 100644 index 0000000..6e548e2 --- /dev/null +++ b/src/server/Schema.cpp @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2017 Samsung Electronics 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 "Schema.h" + +using namespace ctx; + +Schema::Schema() : + retention(0), + limit(0) +{ +} diff --git a/src/server/Schema.h b/src/server/Schema.h new file mode 100644 index 0000000..ac74b04 --- /dev/null +++ b/src/server/Schema.h @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2017 Samsung Electronics 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 __CONTEXT_STORE_SCHEMA_H__ +#define __CONTEXT_STORE_SCHEMA_H__ + +#include +#include +#include +#include + +namespace ctx { + + class Schema { + public: + enum class AttributeType { + UNDEFINED = 0, + INTEGER = 1, + REAL = 2, + TEXT = 3 + }; + + Schema(); + + std::string uri; + unsigned int retention; + unsigned int limit; + std::vector readPrivileges; + std::vector writePrivileges; + std::vector> attributes; + }; + +} + +#endif diff --git a/src/server/SchemaChecker.cpp b/src/server/SchemaChecker.cpp new file mode 100644 index 0000000..3a7a165 --- /dev/null +++ b/src/server/SchemaChecker.cpp @@ -0,0 +1,61 @@ +/* + * Copyright (c) 2017 Samsung Electronics 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 "Schema.h" +#include "SchemaChecker.h" + +#define COL_REGEX "^[A-Za-z]+\\w*$" + +using namespace ctx; + +SchemaChecker::SchemaChecker() : + __uriRegex(URI_REGEX("contextstore"), std::regex::optimize), + __privilegeRegex(URI_REGEX("privilege"), std::regex::optimize), + __columnNameRegex(COL_REGEX, std::regex::optimize) +{ +} + +bool SchemaChecker::validate(const Schema& schema) +{ + if (schema.retention > MAX_RETENTION || schema.limit > MAX_LIMIT) + return false; + + if (schema.retention == 0 && schema.limit == 0) + return false; + + if (schema.attributes.empty()) + return false; + + if (!std::regex_match(schema.uri, __uriRegex)) + return false; + + for (auto& privil : schema.readPrivileges) { + if (!std::regex_match(privil, __privilegeRegex)) + return false; + } + + for (auto& privil : schema.writePrivileges) { + if (!std::regex_match(privil, __privilegeRegex)) + return false; + } + + for (auto& attr: schema.attributes) { + if (!std::regex_match(attr.first, __columnNameRegex)) + return false; + } + + return true; +} diff --git a/src/server/SchemaChecker.h b/src/server/SchemaChecker.h new file mode 100644 index 0000000..c01ee03 --- /dev/null +++ b/src/server/SchemaChecker.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2017 Samsung Electronics 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 __CONTEXT_STORE_SCHEMA_CHECKER_H__ +#define __CONTEXT_STORE_SCHEMA_CHECKER_H__ + +#include +#include + +namespace ctx { + + class Schema; + + class SchemaChecker { + public: + SchemaChecker(); + + bool validate(const Schema& schema); + + private: + std::regex __uriRegex; + std::regex __privilegeRegex; + std::regex __columnNameRegex; + }; + +} + +#endif diff --git a/src/server/SchemaLoader.cpp b/src/server/SchemaLoader.cpp index b1b01bd..4343456 100644 --- a/src/server/SchemaLoader.cpp +++ b/src/server/SchemaLoader.cpp @@ -15,12 +15,15 @@ */ #include +#include #include #include #include #include #include #include "DatabaseManager.h" +#include "Schema.h" +#include "SchemaChecker.h" #include "SchemaLoader.h" /* Schema XML example: @@ -38,8 +41,6 @@ */ -#define COL_REGEX "^[A-Za-z]+\\w*$" - #define CHK_NAME(NODE, NAME) (!xmlStrcmp(NODE->name, (const xmlChar*)(NAME))) using namespace ctx; @@ -67,53 +68,14 @@ static std::string __getXmlContent(xmlNode* node) return out; } -Schema::Schema() : - retention(0), - limit(0) -{ -} - -bool Schema::valid() -{ - if (retention > MAX_RETENTION || limit > MAX_LIMIT) - return false; - - if (retention == 0 && limit == 0) - return false; - - if (attributes.empty()) - return false; - - std::regex uriRegex(URI_REGEX("contextstore"), std::regex::optimize); - if (!std::regex_match(uri, uriRegex)) - return false; - - std::regex privilegeRegex(URI_REGEX("privilege"), std::regex::optimize); - for (auto& privil : readPrivileges) { - if (!std::regex_match(privil, privilegeRegex)) - return false; - } - - for (auto& privil : writePrivileges) { - if (!std::regex_match(privil, privilegeRegex)) - return false; - } - - std::regex columnNameRegex(COL_REGEX, std::regex::optimize); - for (auto& attr: attributes) { - if (!std::regex_match(attr.first, columnNameRegex)) - return false; - } - - return true; -} - -SchemaLoader::SchemaLoader() +SchemaLoader::SchemaLoader() : + __schemaChecker(NULL) { } SchemaLoader::~SchemaLoader() { + delete __schemaChecker; } bool SchemaLoader::load() @@ -239,7 +201,10 @@ bool SchemaLoader::__parseSchema(xmlNode* node, Schema& schema) if (!limit.empty()) schema.limit = std::atoi(limit.c_str()); - return schema.valid(); + SchemaChecker* checker = __getSchemaChecker(); + IF_FAIL_RETURN(checker, false); + + return checker->validate(schema); } void SchemaLoader::__parsePrivilege(xmlNode* node, Schema& schema) @@ -268,6 +233,21 @@ void SchemaLoader::__parseAttribute(xmlNode* node, Schema& schema) schema.attributes.push_back(std::make_pair(name, Schema::AttributeType::TEXT)); } +SchemaChecker* SchemaLoader::__getSchemaChecker() +{ + if (__schemaChecker) + return __schemaChecker; + + try { + __schemaChecker = new SchemaChecker; + } catch (std::exception& e) { + _E("Exception: %s", e.what()); + return NULL; + } + + return __schemaChecker; +} + void SchemaLoader::__insertMetadata(Schema& schema) { char* updateSql = NULL; diff --git a/src/server/SchemaLoader.h b/src/server/SchemaLoader.h index 4b08934..59fdac0 100644 --- a/src/server/SchemaLoader.h +++ b/src/server/SchemaLoader.h @@ -17,7 +17,6 @@ #ifndef __CONTEXT_STORE_SCHEMA_LOADER_H__ #define __CONTEXT_STORE_SCHEMA_LOADER_H__ -#include #include #include #include @@ -26,25 +25,8 @@ namespace ctx { - class Schema { - public: - enum class AttributeType { - UNDEFINED = 0, - INTEGER = 1, - REAL = 2, - TEXT = 3 - }; - - Schema(); - bool valid(); - - std::string uri; - unsigned int retention; - unsigned int limit; - std::vector readPrivileges; - std::vector writePrivileges; - std::vector> attributes; - }; + class Schema; + class SchemaChecker; class SchemaLoader { public: @@ -61,10 +43,13 @@ namespace ctx { void __parseAttribute(xmlNode* node, Schema& schema); void __insertMetadata(Schema& schema); void __createStoreTable(Schema& schema); + SchemaChecker* __getSchemaChecker(); virtual std::string __getSchemaDir() = 0; virtual Database& __getDatabase() = 0; + SchemaChecker* __schemaChecker; + protected: SchemaLoader(); }; -- 2.7.4