Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
jni-thrift-util.h
Go to the documentation of this file.
1 // Copyright 2015 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef IMPALA_RPC_JNI_THRIFT_UTIL_H
16 #define IMPALA_RPC_JNI_THRIFT_UTIL_H
17 
18 #include "rpc/thrift-util.h"
19 #include "util/jni-util.h"
20 
23 
24 namespace impala {
25 
26 template <class T>
27 Status SerializeThriftMsg(JNIEnv* env, T* msg, jbyteArray* serialized_msg) {
28  int buffer_size = 100 * 1024; // start out with 100KB
29  ThriftSerializer serializer(false, buffer_size);
30 
31  uint8_t* buffer = NULL;
32  uint32_t size = 0;
33  RETURN_IF_ERROR(serializer.Serialize<T>(msg, &size, &buffer));
34 
36  *serialized_msg = env->NewByteArray(size);
38  if (*serialized_msg == NULL) return Status("couldn't construct jbyteArray");
39  env->SetByteArrayRegion(*serialized_msg, 0, size, reinterpret_cast<jbyte*>(buffer));
41  return Status::OK;
42 }
43 
44 template <class T>
45 Status DeserializeThriftMsg(JNIEnv* env, jbyteArray serialized_msg, T* deserialized_msg) {
46  jboolean is_copy = false;
47  uint32_t buf_size = env->GetArrayLength(serialized_msg);
48  jbyte* buf = env->GetByteArrayElements(serialized_msg, &is_copy);
49 
51  reinterpret_cast<uint8_t*>(buf), &buf_size, false, deserialized_msg));
52 
55  env->ReleaseByteArrayElements(serialized_msg, buf, JNI_ABORT);
56  return Status::OK;
57 }
58 
59 }
60 
61 #endif
#define RETURN_IF_ERROR(stmt)
some generally useful macros
Definition: status.h:242
Status Serialize(T *obj, std::vector< uint8_t > *result)
Serializes obj into result. Result will contain a copy of the memory.
Definition: thrift-util.h:48
#define RETURN_ERROR_IF_EXC(env)
Definition: jni-util.h:99
static const Status OK
Definition: status.h:87
Status DeserializeThriftMsg(JNIEnv *env, jbyteArray serialized_msg, T *deserialized_msg)
Status SerializeThriftMsg(JNIEnv *env, T *msg, jbyteArray *serialized_msg)