From ea0a9cdd55eda5b0602d7e2e77db49d47d9039ad Mon Sep 17 00:00:00 2001 From: xubo245 Date: Wed, 31 Oct 2018 14:41:21 +0800 Subject: [PATCH] [CARBONDATA-3063] Support set and get carbon property in C++ SDK --- .../core/util/CarbonProperties.java | 12 ++- store/CSDK/CMakeLists.txt | 4 +- store/CSDK/src/CarbonProperties.cpp | 97 +++++++++++++++++++ store/CSDK/src/CarbonProperties.h | 71 ++++++++++++++ store/CSDK/src/CarbonRow.h | 1 - store/CSDK/test/main.cpp | 16 ++- 6 files changed, 192 insertions(+), 9 deletions(-) create mode 100644 store/CSDK/src/CarbonProperties.cpp create mode 100644 store/CSDK/src/CarbonProperties.h diff --git a/core/src/main/java/org/apache/carbondata/core/util/CarbonProperties.java b/core/src/main/java/org/apache/carbondata/core/util/CarbonProperties.java index 7ec22be4f7e..d117b4d7ea4 100644 --- a/core/src/main/java/org/apache/carbondata/core/util/CarbonProperties.java +++ b/core/src/main/java/org/apache/carbondata/core/util/CarbonProperties.java @@ -755,7 +755,7 @@ public static String getStorePath() { /** * This method will be used to get the properties value * - * @param key + * @param key property key * @return properties value */ public String getProperty(String key) { @@ -789,9 +789,10 @@ private String getSessionPropertyValue(String key) { /** * This method will be used to get the properties value if property is not - * present then it will return tghe default value + * present then it will return the default value * - * @param key + * @param key property key + * @param defaultValue properties default value * @return properties value */ public String getProperty(String key, String defaultValue) { @@ -805,8 +806,9 @@ public String getProperty(String key, String defaultValue) { /** * This method will be used to add a new property * - * @param key - * @return properties value + * @param key property key + * @param value properties value + * @return CarbonProperties object */ public CarbonProperties addProperty(String key, String value) { carbonProperties.setProperty(key, value); diff --git a/store/CSDK/CMakeLists.txt b/store/CSDK/CMakeLists.txt index 4da47cfacb3..2d31c75b2d7 100644 --- a/store/CSDK/CMakeLists.txt +++ b/store/CSDK/CMakeLists.txt @@ -8,7 +8,9 @@ find_package(JNI REQUIRED) include_directories(${JNI_INCLUDE_DIRS}) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") -set(SOURCE_FILES src/CarbonReader.cpp src/CarbonReader.h test/main.cpp src/CarbonRow.h src/CarbonRow.cpp src/CarbonWriter.h src/CarbonWriter.cpp src/CarbonSchemaReader.h src/CarbonSchemaReader.cpp src/Schema.h src/Schema.cpp) +set(SOURCE_FILES src/CarbonReader.cpp src/CarbonReader.h test/main.cpp src/CarbonRow.h + src/CarbonRow.cpp src/CarbonWriter.h src/CarbonWriter.cpp src/CarbonSchemaReader.h + src/CarbonSchemaReader.cpp src/Schema.h src/Schema.cpp src/CarbonProperties.cpp src/CarbonProperties.h) add_executable(CJDK ${SOURCE_FILES}) get_filename_component(JAVA_JVM_LIBRARY_DIR ${JAVA_JVM_LIBRARY} DIRECTORY) diff --git a/store/CSDK/src/CarbonProperties.cpp b/store/CSDK/src/CarbonProperties.cpp new file mode 100644 index 00000000000..a24f969db25 --- /dev/null +++ b/store/CSDK/src/CarbonProperties.cpp @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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 "CarbonProperties.h" + +CarbonProperties::CarbonProperties(JNIEnv *env) { + if (env == NULL) { + throw std::runtime_error("JNIEnv parameter can't be NULL."); + } + this->jniEnv = env; + this->carbonPropertiesClass = jniEnv->FindClass("org/apache/carbondata/core/util/CarbonProperties"); + if (carbonPropertiesClass == NULL) { + throw std::runtime_error("Can't find the class in java: org/apache/carbondata/core/util/CarbonProperties"); + } + jmethodID id = jniEnv->GetStaticMethodID(carbonPropertiesClass, "getInstance", + "()Lorg/apache/carbondata/core/util/CarbonProperties;"); + if (id == NULL) { + throw std::runtime_error("Can't find the method in java: getInstance"); + } + this->carbonPropertiesObject = jniEnv->CallStaticObjectMethod(carbonPropertiesClass, id); +} + + +jobject CarbonProperties::addProperty(char *key, char *value) { + if (key == NULL) { + throw std::runtime_error("key parameter can't be NULL."); + } + if (value == NULL) { + throw std::runtime_error("value parameter can't be NULL."); + } + jmethodID id = jniEnv->GetMethodID(carbonPropertiesClass, "addProperty", + "(Ljava/lang/String;Ljava/lang/String;)Lorg/apache/carbondata/core/util/CarbonProperties;"); + if (id == NULL) { + throw std::runtime_error("Can't find the method in java: addProperty"); + } + jvalue args[2]; + args[0].l = jniEnv->NewStringUTF(key);; + args[1].l = jniEnv->NewStringUTF(value);; + this->carbonPropertiesObject = jniEnv->CallObjectMethodA(carbonPropertiesObject, id, args); + return carbonPropertiesObject; +} + +char *CarbonProperties::getProperty(char *key) { + if (key == NULL) { + throw std::runtime_error("key parameter can't be NULL."); + } + jmethodID id = jniEnv->GetMethodID(carbonPropertiesClass, "getProperty", + "(Ljava/lang/String;)Ljava/lang/String;"); + if (id == NULL) { + throw std::runtime_error("Can't find the method in java: getProperty"); + } + jvalue args[1]; + args[0].l = jniEnv->NewStringUTF(key); + jobject value = jniEnv->CallObjectMethodA(carbonPropertiesObject, id, args); + if (value == NULL) { + return NULL; + } + char *str = (char *) jniEnv->GetStringUTFChars((jstring) value, JNI_FALSE); + jniEnv->DeleteLocalRef(value); + return str; +} + +char *CarbonProperties::getProperty(char *key, char *defaultValue) { + if (key == NULL) { + throw std::runtime_error("key parameter can't be NULL."); + } + if (defaultValue == NULL) { + throw std::runtime_error("defaultValue parameter can't be NULL."); + } + jmethodID id = jniEnv->GetMethodID(carbonPropertiesClass, "getProperty", + "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); + if (id == NULL) { + throw std::runtime_error("Can't find the method in java: getProperty"); + } + jvalue args[2]; + args[0].l = jniEnv->NewStringUTF(key); + args[1].l = jniEnv->NewStringUTF(defaultValue); + jobject value = jniEnv->CallObjectMethodA(carbonPropertiesObject, id, args); + char *str = (char *) jniEnv->GetStringUTFChars((jstring) value, JNI_FALSE); + jniEnv->DeleteLocalRef(value); + return str; +} diff --git a/store/CSDK/src/CarbonProperties.h b/store/CSDK/src/CarbonProperties.h new file mode 100644 index 00000000000..a5d391b03b5 --- /dev/null +++ b/store/CSDK/src/CarbonProperties.h @@ -0,0 +1,71 @@ +#include + +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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. + */ + +class CarbonProperties { +private: + /** + * carbonProperties Class + */ + jclass carbonPropertiesClass; + + /** + * carbonProperties Object + */ + jobject carbonPropertiesObject; +public: + /** + * jni env + */ + JNIEnv *jniEnv; + + /** + * Constructor of CarbonProperties + * + * @param env JNI env + */ + CarbonProperties(JNIEnv *env); + + /** + * This method will be used to add a new property + * + * @param key property key + * @param value property value + * @return CarbonProperties object + */ + jobject addProperty(char *key, char *value); + + /** + * This method will be used to get the properties value + * + * @param key property key + * @return property value + */ + char *getProperty(char *key); + + /** + * This method will be used to get the properties value + * if property is not present then it will return the default value + * + * @param key property key + * @param defaultValue property default Value + * @return + */ + char *getProperty(char *key, char *defaultValue); + +}; diff --git a/store/CSDK/src/CarbonRow.h b/store/CSDK/src/CarbonRow.h index 007f8a91b64..2395fa6e318 100644 --- a/store/CSDK/src/CarbonRow.h +++ b/store/CSDK/src/CarbonRow.h @@ -58,7 +58,6 @@ class CarbonRow { * Constructor and express the carbon row result * * @param env JNI env - * @param jo carbon Row object */ CarbonRow(JNIEnv *env); diff --git a/store/CSDK/test/main.cpp b/store/CSDK/test/main.cpp index 876e2e137f1..a3ee97c27d1 100644 --- a/store/CSDK/test/main.cpp +++ b/store/CSDK/test/main.cpp @@ -26,6 +26,7 @@ #include "../src/CarbonWriter.h" #include "../src/CarbonSchemaReader.h" #include "../src/Schema.h" +#include "../src/CarbonProperties.h" using namespace std; @@ -274,6 +275,16 @@ bool tryCatchException(JNIEnv *env) { printf("\nfinished handle exception\n"); } +void testCarbonProperties(JNIEnv *env) { + printf("%s", "test Carbon Properties:"); + CarbonProperties carbonProperties(env); + char *key = "carbon.unsafe.working.memory.in.mb"; + printf("%s\t", carbonProperties.getProperty(key)); + printf("%s\t", carbonProperties.getProperty(key, "512")); + carbonProperties.addProperty(key, "1024"); + printf("%s\t", carbonProperties.getProperty(key)); +} + /** * test write data to local disk * @@ -452,11 +463,12 @@ int main(int argc, char *argv[]) { tryCatchException(env); char *indexFilePath = argv[1]; char *dataFilePath = argv[2]; - readSchemaInIndexFile(env, indexFilePath); - readSchemaInDataFile(env, dataFilePath); + testCarbonProperties(env); readFromLocalWithoutProjection(env); testWriteData(env, "./data", 1, argv); readFromLocal(env); + readSchemaInIndexFile(env, indexFilePath); + readSchemaInDataFile(env, dataFilePath); } (jvm)->DestroyJavaVM();