You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@etch.apache.org by ve...@apache.org on 2012/06/01 14:23:30 UTC

svn commit: r1345114 - in /incubator/etch/trunk/binding-cpp/runtime: include/common/ include/serialization/ src/main/ src/main/serialization/ src/test/ src/test/serialization/

Author: veithm
Date: Fri Jun  1 12:23:30 2012
New Revision: 1345114

URL: http://svn.apache.org/viewvc?rev=1345114&view=rev
Log:
ETCH-184 Implementation of EtchHashTableSerializer

Change-Id: I89d528ffedfd0937a93f636cd825f2df3cd38016

Added:
    incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchHashTableSerializer.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp
    incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchHashTableSerializerTest.cpp
Modified:
    incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h
    incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectHash.h
    incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
    incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt

Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h?rev=1345114&r1=1345113&r2=1345114&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchComparator.h Fri Jun  1 12:23:30 2012
@@ -40,4 +40,13 @@ public:
   }
 };
 
+template <class T>
+class EtchComparator <capu::SmartPointer<T> > {
+public:
+
+  inline capu::bool_t operator() (const capu::SmartPointer<EtchObject>& first, const capu::SmartPointer<EtchObject>& second) {
+    return first->equals(second.get());
+  }
+};
+
 #endif

Modified: incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectHash.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectHash.h?rev=1345114&r1=1345113&r2=1345114&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectHash.h (original)
+++ incubator/etch/trunk/binding-cpp/runtime/include/common/EtchObjectHash.h Fri Jun  1 12:23:30 2012
@@ -29,7 +29,11 @@ public:
   }
 
   static capu::uint64_t Digest(const EtchObject* key) {
-   return key->getHashCode();
+    return key->getHashCode();
+  }
+
+  static capu::uint64_t Digest(const capu::SmartPointer<EtchObject>& key) {
+    return key->getHashCode();
   }
 };
 #endif

Added: incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchHashTableSerializer.h
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchHashTableSerializer.h?rev=1345114&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchHashTableSerializer.h (added)
+++ incubator/etch/trunk/binding-cpp/runtime/include/serialization/EtchHashTableSerializer.h Fri Jun  1 12:23:30 2012
@@ -0,0 +1,71 @@
+/* $Id$
+ *
+ * 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.
+ */
+
+#ifndef __ETCHHASHTABLESERIALIZER_H__
+#define __ETCHHASHTABLESERIALIZER_H__
+
+#include "common/EtchHashTable.h"
+#include "common/EtchNativeArray.h"
+#include "serialization/EtchImportExportHelper.h"
+#include "serialization/EtchStructValue.h"
+#include "serialization/EtchField.h"
+#include "serialization/EtchType.h"
+#include "serialization/EtchClass2TypeMap.h"
+#include "serialization/EtchValidatorObject.h"
+#include "serialization/EtchValidatorObject.h"
+
+class EtchHashTableSerializer : public EtchImportExportHelper {
+public:
+
+  /**
+   * Constructor
+   */
+  EtchHashTableSerializer(EtchType* type, EtchField* field);
+
+  /**
+   * Destructor
+   */
+  virtual ~EtchHashTableSerializer();
+
+  /**
+   * @see EtchImportExportHelper
+   */
+  virtual status_t exportValue(EtchValueFactory* vf, capu::SmartPointer<EtchObject> value, EtchStructValue*& result);
+
+  /**
+   * @see EtchImportExportHelper
+   */
+  virtual status_t importValue(EtchStructValue* value, capu::SmartPointer<EtchObject> &result);
+
+  /**
+   * Defines custom fields in the value factory so that the importer can find them.
+   * @param type
+   * @param collection
+   */
+  static status_t Init(EtchType* type, EtchClass2TypeMap* class2type);
+
+private:
+
+  EtchField mField;
+  EtchType* mType;
+  const static EtchString FIELD_NAME;
+
+};
+
+#endif /* ETCHHASHTABLESERIALIZER_H */
+

Modified: incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt?rev=1345114&r1=1345113&r2=1345114&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/CMakeLists.txt Fri Jun  1 12:23:30 2012
@@ -90,6 +90,7 @@ SET(MAIN_INCLUDES
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchValidatorRuntimeException.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchRuntimeExceptionSerializer.h
     ${PROJECT_SOURCE_DIR}/include/serialization/EtchAuthenticationExceptionSerializer.h
+    ${PROJECT_SOURCE_DIR}/include/serialization/EtchHashTableSerializer.h
     ${PROJECT_SOURCE_DIR}/include/util/EtchUtil.h
     )
 
@@ -140,6 +141,7 @@ SET(MAIN_SOURCES
     serialization/EtchValidatorRuntimeException.cpp
     serialization/EtchRuntimeExceptionSerializer.cpp
     serialization/EtchAuthenticationExceptionSerializer.cpp
+    serialization/EtchHashTableSerializer.cpp
     util/EtchUtil.cpp
     )
 

Added: incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp?rev=1345114&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/main/serialization/EtchHashTableSerializer.cpp Fri Jun  1 12:23:30 2012
@@ -0,0 +1,112 @@
+/* $Id$
+ *
+ * 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 "serialization/EtchHashTableSerializer.h"
+
+
+const EtchString EtchHashTableSerializer::FIELD_NAME("keysAndValues");
+
+EtchHashTableSerializer::EtchHashTableSerializer(EtchType* type, EtchField* field)
+: mField(*field), mType(type) {
+
+}
+
+EtchHashTableSerializer::~EtchHashTableSerializer() {
+
+}
+
+status_t EtchHashTableSerializer::exportValue(EtchValueFactory* vf, capu::SmartPointer<EtchObject> value, EtchStructValue*& result) {
+  if ((value.get() == NULL) || (vf == NULL))
+    return ETCH_EINVAL;
+  if (value->getObjectType() != EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> >::TYPE())
+    return ETCH_EINVAL;
+
+  capu::SmartPointer<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > table = capu::smartpointer_cast<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > (value);
+
+  EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >::Iterator it = table->begin();
+  capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > keysAndValuesArray = new EtchNativeArray<capu::SmartPointer<EtchObject> > (1, table->count()*2);
+  capu::int32_t i = 0;
+  EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >::Pair ptr;
+
+  while (it.hasNext()) {
+    it.next(&ptr);
+    keysAndValuesArray->set(i, ptr.first);
+    i++;
+    keysAndValuesArray->set(i, ptr.second);
+    i++;
+  }
+
+  result = new EtchStructValue(mType, vf);
+  if (result->put(mField, keysAndValuesArray) != ETCH_OK) {
+    return ETCH_ERROR;
+  }
+
+  return ETCH_OK;
+}
+
+status_t EtchHashTableSerializer::importValue(EtchStructValue* value, capu::SmartPointer<EtchObject> &result) {
+  if (value == NULL) {
+    return ETCH_EINVAL;
+  } else if (!value->isType(mType)) {
+    return ETCH_EINVAL;
+  }
+  capu::SmartPointer<EtchObject> tmp;
+  if (value->get(mField, &tmp) != ETCH_OK)
+    return ETCH_ERROR;
+
+  EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >* table = new EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >();
+
+  capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > keysAndValuesArray = capu::smartpointer_cast<EtchNativeArray<capu::SmartPointer<EtchObject> > > (tmp);
+
+  capu::SmartPointer<EtchObject> key;
+  capu::SmartPointer<EtchObject> hashValue;
+  capu::int32_t i = 0;
+  while (i < keysAndValuesArray->getLength()) {
+    keysAndValuesArray->get(i, &key);
+    i++;
+    keysAndValuesArray->get(i, &hashValue);
+    i++;
+    status_t res = table->put(key, hashValue);
+    if (res != ETCH_OK)
+      return res;
+  }
+  result = (EtchObject*) table;
+  return ETCH_OK;
+}
+
+status_t EtchHashTableSerializer::Init(EtchType* type, EtchClass2TypeMap* class2type) {
+  status_t result;
+  EtchField field_ptr;
+  result = type->getField(FIELD_NAME, &field_ptr);
+  if (result != ETCH_OK)
+    return result;
+  class2type->put(EtchNativeArray<capu::SmartPointer<EtchObject> >::TYPE(), type);
+  type->setComponentType(EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> >::TYPE());
+  //set the import export helper
+  type->setImportExportHelper(new EtchHashTableSerializer(type, &field_ptr));
+  capu::SmartPointer<EtchValidator> tmp;
+  result = EtchValidatorObject::Get(1, tmp);
+  if (result != ETCH_OK)
+    return result;
+  result = type->putValidator(field_ptr, tmp);
+  if (result != ETCH_OK)
+    return result;
+  type->lock();
+  return ETCH_OK;
+}
\ No newline at end of file

Modified: incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt?rev=1345114&r1=1345113&r2=1345114&view=diff
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt (original)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/CMakeLists.txt Fri Jun  1 12:23:30 2012
@@ -71,6 +71,7 @@ add_executable (etch-cpp-test
     serialization/EtchValidatorRuntimeExceptionTest.cpp
     serialization/EtchRuntimeExceptionSerializerTest.cpp
     serialization/EtchAuthenticationExceptionSerializerTest.cpp
+    serialization/EtchHashTableSerializerTest.cpp
     util/EtchUtilTest.cpp
     ${GTEST}/src/gtest-all.cc
     ${GMOCK}/src/gmock-all.cc

Added: incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchHashTableSerializerTest.cpp
URL: http://svn.apache.org/viewvc/incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchHashTableSerializerTest.cpp?rev=1345114&view=auto
==============================================================================
--- incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchHashTableSerializerTest.cpp (added)
+++ incubator/etch/trunk/binding-cpp/runtime/src/test/serialization/EtchHashTableSerializerTest.cpp Fri Jun  1 12:23:30 2012
@@ -0,0 +1,183 @@
+/* $Id$
+ *
+ * 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 "gtest/gtest.h"
+#include "gmock/gmock.h"
+#include "serialization/EtchValueFactory.h"
+#include "serialization/EtchHashTableSerializer.h"
+#include "common/EtchNativeArray.h"
+
+class MockValueFactory4 : public virtual EtchValueFactory {
+public:
+
+  ~MockValueFactory4() {
+
+  }
+  MOCK_METHOD2(getType, status_t(capu::uint32_t id, EtchType*& result));
+
+  MOCK_METHOD2(getType, status_t(EtchString& name, EtchType*& result));
+
+  MOCK_METHOD1(addType, status_t(EtchType* type));
+
+  MOCK_METHOD0(lockDynamicTypes, status_t());
+
+  MOCK_METHOD0(unlockDynamicTypes, status_t());
+
+  MOCK_METHOD2(getMessageId, status_t(EtchMessage* msg, capu::int64_t &result));
+
+  MOCK_METHOD2(setMessageId, status_t(EtchMessage* msg, capu::int64_t msgid));
+
+  MOCK_METHOD0(get_mf__messageId, EtchField());
+
+  MOCK_METHOD2(getInReplyTo, status_t(EtchMessage* msg, capu::int64_t &result));
+
+  MOCK_METHOD2(setInReplyTo, status_t(EtchMessage* msg, capu::int64_t msgid));
+
+  MOCK_METHOD0(get_mf__inReplyTo, EtchField());
+
+  MOCK_METHOD2(importCustomValue, status_t(EtchStructValue* _struct, capu::SmartPointer<EtchObject> &result));
+
+  MOCK_METHOD2(exportCustomValue, status_t(capu::SmartPointer<EtchObject> value, EtchStructValue*& result));
+
+  MOCK_METHOD0(get_mt__exception, EtchType*());
+
+  MOCK_METHOD2(getCustomStructType, status_t(capu::int32_t c, EtchType *& type));
+
+  EtchLevel getLevel() {
+    return LEVEL_FULL;
+  }
+
+  MOCK_METHOD1(setLevel, EtchLevel(EtchLevel level));
+
+};
+
+TEST(EtchHashTableSerializerTest, initTest) {
+  EtchClass2TypeMap* c2type = new EtchClass2TypeMap();
+  EtchString typeName("type1");
+  EtchType* type = new EtchType(10, typeName);
+  EtchType* result;
+  EXPECT_TRUE(EtchHashTableSerializer::Init(type, c2type) == ETCH_OK);
+  c2type->get(EtchNativeArray<capu::SmartPointer<EtchObject> >::TYPE(), &result);
+
+  //check the added type to class to type matching
+  EXPECT_TRUE(type == result);
+  //check the initialized component type
+  EXPECT_TRUE(type->getComponentType() == (EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> >::TYPE()));
+
+  //to check the initialized validator
+  capu::SmartPointer<EtchValidator> validator;
+  EtchField field;
+  typeName = "keysAndValues";
+  type->getField(typeName, &field);
+  type->getValidator(field, validator);
+  capu::SmartPointer<EtchValidator> val;
+  EtchValidatorObject::Get(1, val);
+  EXPECT_TRUE(validator == val);
+  delete type;
+  delete c2type;
+}
+
+TEST(EtchHashTableSerializerTest, exportTest) {
+  EtchClass2TypeMap* c2type = new EtchClass2TypeMap();
+  EtchString typeName("type1");
+  EtchValueFactory* factory = new MockValueFactory4();
+  EtchType* type = new EtchType(10, typeName);
+  capu::SmartPointer<EtchObject> object = new EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> > ();
+  capu::SmartPointer<EtchObject> object2_key = new EtchInt32(90);
+  capu::SmartPointer<EtchObject> object2_value = new EtchInt32(91);
+  capu::SmartPointer<EtchObject> object3;
+  EtchStructValue* result;
+  //  //initialize the serializer
+  EXPECT_TRUE(EtchHashTableSerializer::Init(type, c2type) == ETCH_OK);
+  EtchImportExportHelper* test = type->getImportExportHelper();
+  //  //check with invalid values
+  EXPECT_TRUE(test->exportValue(NULL, NULL, result) == ETCH_EINVAL);
+  EXPECT_TRUE(test->exportValue(NULL, object, result) == ETCH_EINVAL);
+  EXPECT_TRUE(test->exportValue(factory, NULL, result) == ETCH_EINVAL);
+
+  //add some test entries
+  capu::SmartPointer<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > table = capu::smartpointer_cast<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > (object);
+  table->put(object2_key, object2_value);
+
+  //export values
+  EXPECT_TRUE(test->exportValue(factory, object, result) == ETCH_OK);
+
+  //check it has been correctly serialized or not
+  EtchString str("keysAndValues");
+  EtchField field;
+
+  //get the fields
+  EXPECT_TRUE(type->getField(str, &field) == ETCH_OK);
+  //use the field to get the serialized value
+  EXPECT_TRUE(result->get(field, &object3) == ETCH_OK);
+  capu::SmartPointer<EtchNativeArray<capu::SmartPointer<EtchObject> > > array = capu::smartpointer_cast<EtchNativeArray<capu::SmartPointer<EtchObject> > > (object3);
+  //check the assigned type
+  EXPECT_TRUE(type->getComponentType() == (EtchHashTable<capu::SmartPointer<EtchObject> , capu::SmartPointer<EtchObject> >::TYPE()));
+  //get the serialized value
+  capu::SmartPointer<EtchObject> object4;
+  array->get(0, &object4);
+  EtchInt32 *serialized_key = (EtchInt32 *) object4.get();
+  array->get(1, &object4);
+  EtchInt32 *serialized_value = (EtchInt32 *) object4.get();
+  //check the serialized value
+  EXPECT_TRUE(serialized_key->get() == 90);
+  EXPECT_TRUE(serialized_value->get() == 91);
+
+  delete c2type;
+  delete factory;
+  delete type;
+  delete result;
+}
+
+TEST(EtchHashTableSerializerTest, importTest) {
+  EtchClass2TypeMap* c2type = new EtchClass2TypeMap();
+  EtchString typeName("type1");
+  EtchValueFactory* factory = new MockValueFactory4();
+  EtchType* type = new EtchType(10, typeName);
+  capu::SmartPointer<EtchObject> object = new EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > ();
+  capu::SmartPointer<EtchObject> object2_key = new EtchInt32(90);
+  capu::SmartPointer<EtchObject> object2_value = new EtchInt32(91);
+  EtchStructValue* structValue;
+  //initialize the serializer
+  EXPECT_TRUE(EtchHashTableSerializer::Init(type, c2type) == ETCH_OK);
+  //get the serializer
+  EtchImportExportHelper* test = type->getImportExportHelper();
+  //add some test entries
+  capu::SmartPointer<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > table = capu::smartpointer_cast<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > (object);
+  table->put(object2_key, object2_value);
+  //export values
+  EXPECT_TRUE(test->exportValue(factory, object, structValue) == ETCH_OK);
+
+  //UPPER PART IS USED FOR GENERATING EXAMPLE INPUT VALUE FOR IMPORT VALUE
+  capu::SmartPointer<EtchObject> result;
+  //IMPORT TEST
+  EXPECT_TRUE(test->importValue(NULL, result) == ETCH_EINVAL);
+  EXPECT_TRUE(test->importValue(structValue, result) == ETCH_OK);
+
+  capu::SmartPointer<EtchObject> tmp;
+  //get the imported value
+
+  capu::SmartPointer<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > importedtable = capu::smartpointer_cast<EtchHashTable<capu::SmartPointer<EtchObject>, capu::SmartPointer<EtchObject> > > (result);
+  importedtable->get(object2_key, &tmp);
+  //check the imported value
+  EXPECT_TRUE(((EtchInt32*) tmp.get())->get() == 91);
+  delete c2type;
+  delete factory;
+  delete type;
+  delete structValue;
+}