Separate SchemaChecker from Schema to reuse regex while parsing schema files 46/119646/2
authorMu-Woong Lee <muwoong.lee@samsung.com>
Sat, 18 Mar 2017 14:20:08 +0000 (07:20 -0700)
committerMu-Woong Lee <muwoong.lee@samsung.com>
Fri, 24 Mar 2017 10:32:01 +0000 (03:32 -0700)
Change-Id: I81735406af570af5902905f739c362fcf6ffdf28
Signed-off-by: Mu-Woong Lee <muwoong.lee@samsung.com>
src/server/Schema.cpp [new file with mode: 0644]
src/server/Schema.h [new file with mode: 0644]
src/server/SchemaChecker.cpp [new file with mode: 0644]
src/server/SchemaChecker.h [new file with mode: 0644]
src/server/SchemaLoader.cpp
src/server/SchemaLoader.h

diff --git a/src/server/Schema.cpp b/src/server/Schema.cpp
new file mode 100644 (file)
index 0000000..6e548e2
--- /dev/null
@@ -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 (file)
index 0000000..ac74b04
--- /dev/null
@@ -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 <utility>
+#include <string>
+#include <vector>
+#include <ContextStoreTypesPrivate.h>
+
+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<std::string> readPrivileges;
+               std::vector<std::string> writePrivileges;
+               std::vector<std::pair<std::string, AttributeType>> attributes;
+       };
+
+}
+
+#endif
diff --git a/src/server/SchemaChecker.cpp b/src/server/SchemaChecker.cpp
new file mode 100644 (file)
index 0000000..3a7a165
--- /dev/null
@@ -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 (file)
index 0000000..c01ee03
--- /dev/null
@@ -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 <regex>
+#include <ContextStoreTypesPrivate.h>
+
+namespace ctx {
+
+       class Schema;
+
+       class SchemaChecker {
+       public:
+               SchemaChecker();
+
+               bool validate(const Schema& schema);
+
+       private:
+               std::regex __uriRegex;
+               std::regex __privilegeRegex;
+               std::regex __columnNameRegex;
+       };
+
+}
+
+#endif
index b1b01bd..4343456 100644 (file)
  */
 
 #include <cstdio>
+#include <utility>
 #include <fstream>
 #include <sstream>
 #include <regex>
 #include <dirent.h>
 #include <PathUtil.h>
 #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;
index 4b08934..59fdac0 100644 (file)
@@ -17,7 +17,6 @@
 #ifndef __CONTEXT_STORE_SCHEMA_LOADER_H__
 #define __CONTEXT_STORE_SCHEMA_LOADER_H__
 
-#include <utility>
 #include <string>
 #include <vector>
 #include <libxml/tree.h>
 
 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<std::string> readPrivileges;
-               std::vector<std::string> writePrivileges;
-               std::vector<std::pair<std::string, AttributeType>> 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();
        };