http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableHashSet.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableHashSet.hpp b/clicache/src/CacheableHashSet.hpp new file mode 100644 index 0000000..581d4a8 --- /dev/null +++ b/clicache/src/CacheableHashSet.hpp @@ -0,0 +1,683 @@ +/* + * 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. + */ + + + +#pragma once + +#include "geode_defs.hpp" +#include "begin_native.hpp" +#include <geode/CacheableBuiltins.hpp> +#include "end_native.hpp" + +#include "Serializable.hpp" +#include "ExceptionTypes.hpp" +#include "impl/PdxInstanceImpl.hpp" +#include "native_shared_ptr.hpp" +#include "native_unique_ptr.hpp" + +using namespace System; +using namespace System::Collections::Generic; +#pragma managed + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + namespace Internal + { + /// <summary> + /// A mutable <c>ICacheableKey</c> hash set wrapper that can serve as + /// a distributable object for caching. + /// </summary> + template <System::UInt32 TYPEID, typename HSTYPE> + public ref class CacheableHashSetType + : public Serializable, public ICollection<Object^> + { + public: + + virtual void ToData(DataOutput^ output) override + { + output->WriteArrayLen(this->Count); + + auto set = static_cast<HSTYPE*>(m_nativeptr->get()); + for (const auto& iter : *set) { + auto key = Serializable::GetManagedValueGeneric<Object^>(iter); + output->WriteObject(key); + } + + GC::KeepAlive(this); + } + + virtual IGeodeSerializable^ FromData(DataInput^ input) override + { + int len = input->ReadArrayLen(); + if (len > 0) + { + for (int i = 0; i < len; i++) + { + Object^ key = (input->ReadObject()); + this->Add(key); + } + } + return this; + } + + virtual property System::UInt32 ObjectSize + { + virtual System::UInt32 get() override + { + System::UInt32 size = 0; + for each (Object^ key in this) { + if (key != nullptr) + //size += key->ObjectSize; + //TODO:: how should we do this now + size += 1; + } + return size; + } + } + + virtual int GetHashCode() override + { + IEnumerator<Object^>^ ie = GetEnumerator(); + + int h = 0; + while (ie->MoveNext() == true) + { + h = h + PdxInstanceImpl::deepArrayHashCode(ie->Current); + } + return h; + } + + virtual bool Equals(Object^ other)override + { + if (other == nullptr) + return false; + + CacheableHashSetType^ otherCHST = dynamic_cast<CacheableHashSetType^>(other); + + if (otherCHST == nullptr) + return false; + + if (Count != otherCHST->Count) + return false; + + IEnumerator<Object^>^ ie = GetEnumerator(); + + while (ie->MoveNext() == true) + { + if (otherCHST->Contains(ie->Current)) + return true; + else + return false; + } + + return true; + } + + /// <summary> + /// Enumerator for <c>CacheableHashSet</c> class. + /// </summary> + ref class Enumerator sealed + : public IEnumerator<Object^> + { + public: + // Region: IEnumerator<ICacheableKey^> Members + + /// <summary> + /// Gets the element in the collection at the current + /// position of the enumerator. + /// </summary> + /// <returns> + /// The element in the collection at the current position + /// of the enumerator. + /// </returns> + property Object^ Current + { + virtual Object^ get() = + IEnumerator<Object^>::Current::get + { + if (!m_started) { + throw gcnew System::InvalidOperationException( + "Call MoveNext first."); + } + auto ret = Serializable::GetManagedValueGeneric<Object^>(*(*(m_nativeptr->get()))); + GC::KeepAlive(this); + return ret; + } + } + + // End Region: IEnumerator<ICacheableKey^> Members + + // Region: IEnumerator Members + + /// <summary> + /// Advances the enumerator to the next element of the collection. + /// </summary> + /// <returns> + /// true if the enumerator was successfully advanced to the next + /// element; false if the enumerator has passed the end of + /// the collection. + /// </returns> + virtual bool MoveNext() + { + auto nptr = m_nativeptr->get(); + bool isEnd = static_cast<HSTYPE*>(m_set->m_nativeptr->get())->end() == *nptr; + if (!m_started) { + m_started = true; + } + else { + if (!isEnd) { + (*nptr)++; + isEnd = static_cast<HSTYPE*>(m_set->m_nativeptr->get())->end() == *nptr; + } + } + GC::KeepAlive(this); + return !isEnd; + } + + /// <summary> + /// Sets the enumerator to its initial position, which is before + /// the first element in the collection. + /// </summary> + virtual void Reset() + { + try + { + m_nativeptr = gcnew native_unique_ptr<typename HSTYPE::iterator>( + std::make_unique<typename HSTYPE::iterator>( + static_cast<HSTYPE*>(m_set->m_nativeptr->get())->begin())); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + m_started = false; + } + + !Enumerator() {} + ~Enumerator() {} + + // End Region: IEnumerator Members + + internal: + /// <summary> + /// Internal constructor to wrap a native object pointer + /// </summary> + /// <param name="nativeptr">The native object pointer</param> + inline Enumerator(CacheableHashSetType<TYPEID, HSTYPE>^ set) + : m_set(set) { + Reset(); + } + + private: + // Region: IEnumerator Members + + /// <summary> + /// Gets the current element in the collection. + /// </summary> + /// <returns> + /// The current element in the collection. + /// </returns> + /// <exception cref="System.InvalidOperationException"> + /// The enumerator is positioned before the first element of + /// the collection or after the last element. + /// </exception> + property Object^ ICurrent + { + virtual Object^ get() sealed = + System::Collections::IEnumerator::Current::get + { + return Current; + } + } + + // End Region: IEnumerator Members + + bool m_started; + + CacheableHashSetType<TYPEID, HSTYPE>^ m_set; + + native_unique_ptr<typename HSTYPE::iterator>^ m_nativeptr; + }; + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + virtual System::UInt32 get() override + { + return TYPEID; + } + } + + /// <summary> + /// Get the largest possible size of the <c>CacheableHashSet</c>. + /// </summary> + property System::Int32 MaxSize + { + inline System::Int32 get() + { + try + { + return static_cast<HSTYPE*>(m_nativeptr->get())->max_size(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + } + + /// <summary> + /// True if the <c>CacheableHashSet</c>'s size is 0. + /// </summary> + property bool IsEmpty + { + inline bool get() + { + try + { + return static_cast<HSTYPE*>(m_nativeptr->get())->empty(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + } + + /// <summary> + /// Get the number of buckets used by the HashSet. + /// </summary> + property System::Int32 BucketCount + { + inline System::Int32 get() + { + try + { + return static_cast<HSTYPE*>(m_nativeptr->get())->bucket_count(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + } + + /// <summary> + /// Increases the bucket count to at least <c>size</c> elements. + /// </summary> + /// <param name="size">The new size of the HashSet.</param> + virtual void Resize(System::Int32 size) sealed + { + try + { + static_cast<HSTYPE*>(m_nativeptr->get())->reserve(size); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + + /// <summary> + /// Swap the contents of this <c>CacheableHashSet</c> + /// with the given one. + /// </summary> + /// <param name="other"> + /// The other CacheableHashSet to use for swapping. + /// </param> + virtual void Swap(CacheableHashSetType<TYPEID, HSTYPE>^ other) sealed + { + try + { + if (other != nullptr) { + static_cast<HSTYPE*>(m_nativeptr->get())->swap( + *static_cast<HSTYPE*>(other->m_nativeptr->get())); + } + } + finally + { + GC::KeepAlive(m_nativeptr); + GC::KeepAlive(other->m_nativeptr); + } + } + + // Region: ICollection<ICacheableKey^> Members + + /// <summary> + /// Adds an item to the <c>CacheableHashSet</c>. + /// </summary> + /// <param name="item"> + /// The object to add to the collection. + /// </param> + virtual void Add(Object^ item) + { + _GF_MG_EXCEPTION_TRY2/* due to auto replace */ + + try + { + static_cast<HSTYPE*>(m_nativeptr->get())->insert(Serializable::GetUnmanagedValueGeneric(item, nullptr)); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + + _GF_MG_EXCEPTION_CATCH_ALL2/* due to auto replace */ + } + + /// <summary> + /// Removes all items from the <c>CacheableHashSet</c>. + /// </summary> + virtual void Clear() + { + try + { + static_cast<HSTYPE*>(m_nativeptr->get())->clear(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + + /// <summary> + /// Determines whether the <c>CacheableHashSet</c> contains + /// a specific value. + /// </summary> + /// <param name="item"> + /// The object to locate in the <c>CacheableHashSet</c>. + /// </param> + /// <returns> + /// true if item is found in the <c>CacheableHashSet</c>; + /// otherwise false. + /// </returns> + virtual bool Contains(Object^ item) + { + try + { + return static_cast<HSTYPE*>(m_nativeptr->get())->find(Serializable::GetUnmanagedValueGeneric(item, nullptr)) != static_cast<HSTYPE*>(m_nativeptr->get())->end(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + + /// <summary> + /// Copies the elements of the <c>CacheableHashSet</c> to an + /// <c>System.Array</c>, starting at a particular + /// <c>System.Array</c> index. + /// </summary> + /// <param name="array"> + /// The one-dimensional System.Array that is the destination of the + /// elements copied from <c>CacheableHashSet</c>. The + /// <c>System.Array</c> must have zero-based indexing. + /// </param> + /// <param name="arrayIndex"> + /// The zero-based index in array at which copying begins. + /// </param> + /// <exception cref="IllegalArgumentException"> + /// arrayIndex is less than 0 or array is null. + /// </exception> + /// <exception cref="OutOfRangeException"> + /// arrayIndex is equal to or greater than the length of array. + /// -or-The number of elements in the source <c>CacheableHashSet</c> + /// is greater than the available space from arrayIndex to the end + /// of the destination array. + /// </exception> + virtual void CopyTo(array<Object^>^ array, System::Int32 arrayIndex) + { + if (array == nullptr || arrayIndex < 0) { + throw gcnew IllegalArgumentException("CacheableHashSet.CopyTo():" + " array is null or array index is less than zero"); + } + + auto set = static_cast<HSTYPE*>(m_nativeptr->get()); + System::Int32 index = arrayIndex; + + if (arrayIndex >= array->Length || + array->Length < (arrayIndex + (System::Int32)set->size())) { + throw gcnew OutOfRangeException("CacheableHashSet.CopyTo():" + " array index is beyond the HashSet or length of given " + "array is less than that required to copy all the " + "elements from HashSet"); + } + for (const auto& iter : *set) { + array[index++] = Serializable::GetManagedValueGeneric<Object^>(iter); + } + + GC::KeepAlive(m_nativeptr); + } + + /// <summary> + /// Gets the number of elements contained in the + /// <c>CacheableHashSet</c>. + /// </summary> + virtual property System::Int32 Count + { + virtual System::Int32 get() + { + try + { + return static_cast<HSTYPE*>(m_nativeptr->get())->size(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + } + + /// <summary> + /// Removes the first occurrence of a specific object from the + /// <c>CacheableHashSet</c>. + /// </summary> + /// <param name="item"> + /// The object to remove from the <c>CacheableHashSet</c>. + /// </param> + /// <returns> + /// true if item was successfully removed from the + /// <c>CacheableHashSet</c>; otherwise, false. This method also + /// returns false if item is not found in the original + /// <c>CacheableHashSet</c>. + /// </returns> + virtual bool Remove(Object^ item) + { + try + { + return (static_cast<HSTYPE*>(m_nativeptr->get())->erase(Serializable::GetUnmanagedValueGeneric(item, nullptr)) > 0); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + + /// <summary> + /// Gets a value indicating whether the collection is read-only. + /// </summary> + /// <returns> + /// always false for <c>CacheableHashSet</c> + /// </returns> + virtual property bool IsReadOnly + { + virtual bool get() + { + return false; + } + } + + // End Region: ICollection<ICacheableKey^> Members + + // Region: IEnumerable<ICacheableKey^> Members + + /// <summary> + /// Returns an enumerator that iterates through the + /// <c>CacheableHashSet</c>. + /// </summary> + /// <returns> + /// A <c>System.Collections.Generic.IEnumerator</c> that + /// can be used to iterate through the <c>CacheableHashSet</c>. + /// </returns> + virtual IEnumerator<Object^>^ GetEnumerator() + { + return gcnew Enumerator(this); + } + + // End Region: IEnumerable<ICacheableKey^> Members + + internal: + /// <summary> + /// Factory function to register wrapper + /// </summary> + static IGeodeSerializable^ Create(apache::geode::client::Serializable* obj) + { + return (obj != NULL ? + gcnew CacheableHashSetType<TYPEID, HSTYPE>(obj) : nullptr); + } + + private: + // Region: IEnumerable Members + + /// <summary> + /// Returns an enumerator that iterates through a collection. + /// </summary> + /// <returns> + /// An <c>System.Collections.IEnumerator</c> object that can be used + /// to iterate through the collection. + /// </returns> + virtual System::Collections::IEnumerator^ GetIEnumerator() sealed = + System::Collections::IEnumerable::GetEnumerator + { + return GetEnumerator(); + } + + // End Region: IEnumerable Members + + protected: + /// <summary> + /// Private constructor to wrap a native object pointer + /// </summary> + /// <param name="nativeptr">The native object pointer</param> + inline CacheableHashSetType<TYPEID, HSTYPE>(apache::geode::client::SerializablePtr nativeptr) + : Serializable(nativeptr) { } + + /// <summary> + /// Allocates a new empty instance. + /// </summary> + inline CacheableHashSetType<TYPEID, HSTYPE>() + : Serializable(std::shared_ptr<HSTYPE>(static_cast<HSTYPE*>(HSTYPE::createDeserializable()))) + { } + + /// <summary> + /// Allocates a new empty instance with given initial size. + /// </summary> + /// <param name="size">The initial size of the HashSet.</param> + inline CacheableHashSetType<TYPEID, HSTYPE>(System::Int32 size) + : Serializable(HSTYPE::create(size)) + { } + }; + } + +#define _GFCLI_CACHEABLEHASHSET_DEF_GENERIC(m, HSTYPE) \ + public ref class m : public Internal::CacheableHashSetType<Apache::Geode::Client::GeodeClassIds::m, HSTYPE> \ + { \ + public: \ + /** <summary> + * Allocates a new empty instance. + * </summary> + */ \ + inline m() \ + : Internal::CacheableHashSetType<Apache::Geode::Client::GeodeClassIds::m, HSTYPE>() {} \ + \ + /** <summary> + * Allocates a new instance with the given size. + * </summary> + * <param name="size">the intial size of the new instance</param> + */ \ + inline m(System::Int32 size) \ + : Internal::CacheableHashSetType<Apache::Geode::Client::GeodeClassIds::m, HSTYPE>(size) {} \ + \ + /** <summary> + * Static function to create a new empty instance. + * </summary> + */ \ + inline static m^ Create() \ + { \ + return gcnew m(); \ + } \ + \ + /** <summary> + * Static function to create a new instance with the given size. + * </summary> + */ \ + inline static m^ Create(System::Int32 size) \ + { \ + return gcnew m(size); \ + } \ + \ + /* <summary> + * Factory function to register this class. + * </summary> + */ \ + static IGeodeSerializable^ CreateDeserializable() \ + { \ + return gcnew m(); \ + } \ + \ + internal: \ + static IGeodeSerializable^ Create(apache::geode::client::SerializablePtr obj) \ + { \ + return gcnew m(obj); \ + } \ + \ + private: \ + inline m(apache::geode::client::SerializablePtr nativeptr) \ + : Internal::CacheableHashSetType<Apache::Geode::Client::GeodeClassIds::m, HSTYPE>(nativeptr) { } \ + }; + + /// <summary> + /// A mutable <c>ICacheableKey</c> hash set wrapper that can serve as + /// a distributable object for caching. + /// </summary> + _GFCLI_CACHEABLEHASHSET_DEF_GENERIC(CacheableHashSet, + apache::geode::client::CacheableHashSet); + + /// <summary> + /// A mutable <c>ICacheableKey</c> hash set wrapper that can serve as + /// a distributable object for caching. This is provided for compability + /// with java side though is functionally identical to + /// <c>CacheableHashSet</c> i.e. does not provide the linked semantics of + /// java <c>LinkedHashSet</c>. + /// </summary> + _GFCLI_CACHEABLEHASHSET_DEF_GENERIC(CacheableLinkedHashSet, + apache::geode::client::CacheableLinkedHashSet); + } // namespace Client + } // namespace Geode +} // namespace Apache +
http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableHashTable.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableHashTable.hpp b/clicache/src/CacheableHashTable.hpp new file mode 100644 index 0000000..a2c6c7e --- /dev/null +++ b/clicache/src/CacheableHashTable.hpp @@ -0,0 +1,117 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "CacheableHashMap.hpp" +#include "DataInput.hpp" + + +using namespace System; +using namespace System::Collections::Generic; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// A mutable <c>ICacheableKey</c> to <c>IGeodeSerializable</c> hash table + /// that can serve as a distributable object for caching. This class + /// extends .NET generic <c>Dictionary</c> class. + /// </summary> + ref class CacheableHashTable + : public CacheableHashMap + { + public: + + /// <summary> + /// Allocates a new empty instance. + /// </summary> + inline CacheableHashTable() + : CacheableHashMap() + { } + + /// <summary> + /// Allocates a new instance copying from the given dictionary. + /// </summary> + /// <param name="dictionary"> + /// The dictionary whose elements are copied to this HashTable. + /// </param> + inline CacheableHashTable(Object^ dictionary) + : CacheableHashMap(dictionary) + { } + + /// <summary> + /// Allocates a new empty instance with given initial size. + /// </summary> + /// <param name="capacity"> + /// The initial capacity of the HashTable. + /// </param> + inline CacheableHashTable(System::Int32 capacity) + : CacheableHashMap(capacity) + { } + + + // Region: IGeodeSerializable Members + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + virtual System::UInt32 get() override + { + return GeodeClassIds::CacheableHashTable; + } + } + + // End Region: IGeodeSerializable Members + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableHashTable(); + } + + virtual IGeodeSerializable^ FromData(DataInput^ input) override + { + m_dictionary = input->ReadHashtable(); + return this; + } + internal: + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ Create(Object^ hashtable) + { + return gcnew CacheableHashTable(hashtable); + } + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableIdentityHashMap.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableIdentityHashMap.hpp b/clicache/src/CacheableIdentityHashMap.hpp new file mode 100644 index 0000000..6407c53 --- /dev/null +++ b/clicache/src/CacheableIdentityHashMap.hpp @@ -0,0 +1,128 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "CacheableHashMap.hpp" + + +using namespace System; +using namespace System::Collections::Generic; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// A mutable <c>ICacheableKey</c> to <c>IGeodeSerializable</c> hash map + /// that can serve as a distributable object for caching. This class + /// extends .NET generic <c>Dictionary</c> class. This class is meant + /// as a means to interoperate with java server side + /// <c>IdentityHashMap</c> class objects but is intentionally not + /// intended to provide <c>java.util.IdentityHashMap</c> semantics. + /// </summary> + ref class CacheableIdentityHashMap + : public CacheableHashMap + { + public: + /// <summary> + /// Allocates a new empty instance. + /// </summary> + inline CacheableIdentityHashMap() + : CacheableHashMap() + { } + + /// <summary> + /// Allocates a new instance copying from the given dictionary. + /// </summary> + /// <param name="dictionary"> + /// The dictionary whose elements are copied to this HashMap. + /// </param> + inline CacheableIdentityHashMap(Object^ dictionary) + : CacheableHashMap(dictionary) + { } + + /// <summary> + /// Allocates a new empty instance with given initial size. + /// </summary> + /// <param name="capacity"> + /// The initial capacity of the HashMap. + /// </param> + inline CacheableIdentityHashMap(System::Int32 capacity) + : CacheableHashMap(capacity) + { } + + /// <summary> + /// Static function to create a new empty instance. + /// </summary> + inline static CacheableIdentityHashMap^ Create() + { + return gcnew CacheableIdentityHashMap(); + } + + /// <summary> + /// Static function to create a new instance copying from the + /// given dictionary. + /// </summary> + inline static CacheableIdentityHashMap^ Create( + Object^ dictionary) + { + return gcnew CacheableIdentityHashMap(dictionary); + } + + /// <summary> + /// Static function to create a new instance with given initial size. + /// </summary> + inline static CacheableIdentityHashMap^ Create(System::Int32 capacity) + { + return gcnew CacheableIdentityHashMap(capacity); + } + + // Region: IGeodeSerializable Members + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + virtual System::UInt32 get() override + { + return GeodeClassIds::CacheableIdentityHashMap; + } + } + + // End Region: IGeodeSerializable Members + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableIdentityHashMap(); + } + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableKey.cpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableKey.cpp b/clicache/src/CacheableKey.cpp new file mode 100644 index 0000000..5fc03fa --- /dev/null +++ b/clicache/src/CacheableKey.cpp @@ -0,0 +1,127 @@ +/* + * 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 "geode_includes.hpp" +#include "CacheableKey.hpp" +#include "CacheableString.hpp" +#include "CacheableBuiltins.hpp" + +using namespace System; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + namespace native = apache::geode::client; + + // generic<class TKey> + System::Int32 CacheableKey::GetHashCode() + { + try + { + return static_cast<native::CacheableKey*>(m_nativeptr->get())->hashcode(); + } + finally + { + GC::KeepAlive(m_nativeptr); + } + } + + // generic<class TKey> + bool CacheableKey::Equals(Client::ICacheableKey^ other) + { + if (other == nullptr || other->ClassId != ClassId) { + return false; + } + try + { + return static_cast<native::CacheableKey*>(m_nativeptr->get())->operator==( + *static_cast<native::CacheableKey*>( + ((Client::CacheableKey^)other)->m_nativeptr->get())); + } + finally + { + GC::KeepAlive(m_nativeptr); + GC::KeepAlive(((Client::CacheableKey^)other)->m_nativeptr); + } + } + + //generic<class TKey> + bool CacheableKey::Equals(Object^ obj) + { + return Equals(dynamic_cast<CacheableKey^>(obj)); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (Byte value) + { + return (CacheableKey^) CacheableByte::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (bool value) + { + return (CacheableKey^) CacheableBoolean::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (Char value) + { + return (CacheableKey^) CacheableCharacter::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (Double value) + { + return (CacheableKey^) CacheableDouble::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (Single value) + { + return (CacheableKey^) CacheableFloat::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (System::Int16 value) + { + return (CacheableKey^) CacheableInt16::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey^ (System::Int32 value) + { + return (CacheableKey^) CacheableInt32::Create(value); + } + + // generic<class TKey> + CacheableKey::operator CacheableKey^ (System::Int64 value) + { + return (CacheableKey^) CacheableInt64::Create(value); + } + + //generic<class TKey> + CacheableKey::operator CacheableKey ^ (String^ value) + { + return dynamic_cast<CacheableKey^>(CacheableString::Create(value)); + } + } // namespace Client + } // namespace Geode +} // namespace Apache http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableKey.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableKey.hpp b/clicache/src/CacheableKey.hpp new file mode 100644 index 0000000..39bf3e5 --- /dev/null +++ b/clicache/src/CacheableKey.hpp @@ -0,0 +1,141 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "begin_native.hpp" +#include <geode/CacheableKey.hpp> +#include "end_native.hpp" + +#include "Serializable.hpp" +#include "ICacheableKey.hpp" + + +using namespace System; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// This class wraps the native C++ <c>apache::geode::client::Serializable</c> objects + /// as managed <see cref="../../IGeodeSerializable" /> objects. + /// </summary> + public ref class CacheableKey + : public Serializable, public ICacheableKey + { + public: + /// <summary> + /// Return the hashcode for this key. + /// It gets the hash code by calling the <c>hashcode()</c> function + /// of the underlying <c>apache::geode::client::CacheableKey</c> object. + /// </summary> + virtual System::Int32 GetHashCode() override; + + /// <summary> + /// Return true if this key matches other object. It invokes the '==' + /// operator of the underlying <c>apache::geode::client::CacheableKey</c> object. + /// </summary> + virtual bool Equals(ICacheableKey^ other); + + /// <summary> + /// Return true if this key matches other object. + /// It invokes the '==' operator if the underlying object is a + /// <c>apache::geode::client::CacheableKey</c>, else returns + /// <c>System.Object.Equals()</c> + /// </summary> + virtual bool Equals(Object^ obj) override; + + // Static conversion functions from primitive types. + + /// <summary> + /// Implicit conversion operator from a boolean + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (bool value); + + /// <summary> + /// Implicit conversion operator from a byte + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (Byte value); + + /// <summary> + /// Implicit conversion operator from a double + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (Double value); + + /// <summary> + /// Implicit conversion operator from a float + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (Single value); + + /// <summary> + /// Implicit conversion operator from a 16-bit integer + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (System::Int16 value); + + /// <summary> + /// Implicit conversion operator from a character + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (Char value); + + /// <summary> + /// Implicit conversion operator from a 32-bit integer + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (System::Int32 value); + + /// <summary> + /// Implicit conversion operator from a 64-bit integer + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (System::Int64 value); + + /// <summary> + /// Implicit conversion operator from a string + /// to a <c>CacheableKey</c>. + /// </summary> + static operator CacheableKey^ (String^ value); + + internal: + /// <summary> + /// Default constructor. + /// </summary> + inline CacheableKey() + : Client::Serializable() { } + + /// <summary> + /// Internal constructor to wrap a native object pointer + /// </summary> + /// <param name="nativeptr">The native object pointer</param> + inline CacheableKey(apache::geode::client::SerializablePtr nativeptr) + : Client::Serializable(nativeptr) { } + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + //namespace + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableLinkedList.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableLinkedList.hpp b/clicache/src/CacheableLinkedList.hpp new file mode 100644 index 0000000..0da310c --- /dev/null +++ b/clicache/src/CacheableLinkedList.hpp @@ -0,0 +1,152 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "CacheableVector.hpp" + + +using namespace System; +using namespace System::Collections::Generic; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// A mutable <c>IGeodeSerializable</c> vector wrapper that can serve as + /// a distributable object for caching. This class extends .NET generic + /// <c>List</c> class. + /// </summary> + ref class CacheableLinkedList + : public IGeodeSerializable + { + System::Collections::Generic::LinkedList<Object^>^ m_linkedList; + public: + /// <summary> + /// Allocates a new empty instance. + /// </summary> + inline CacheableLinkedList(System::Collections::Generic::LinkedList<Object^>^ list) + { + m_linkedList = list; + } + + + /// <summary> + /// Static function to create a new empty instance. + /// </summary> + inline static CacheableLinkedList^ Create() + { + return gcnew CacheableLinkedList(gcnew System::Collections::Generic::LinkedList<Object^>()); + } + + /// <summary> + /// Static function to create a new empty instance. + /// </summary> + inline static CacheableLinkedList^ Create(System::Collections::Generic::LinkedList<Object^>^ list) + { + return gcnew CacheableLinkedList(list); + } + + + // Region: IGeodeSerializable Members + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + virtual System::UInt32 get() + { + return GeodeClassIds::CacheableLinkedList; + } + } + + // Region: IGeodeSerializable Members + + virtual void ToData(DataOutput^ output) + { + if (m_linkedList != nullptr) + { + output->WriteArrayLen(m_linkedList->Count); + for each (Object^ obj in m_linkedList) { + //TODO::split + output->WriteObject(obj); + } + } + else + output->WriteByte(0xFF); + } + + virtual IGeodeSerializable^ FromData(DataInput^ input) + { + int len = input->ReadArrayLen(); + for (int i = 0; i < len; i++) + { + m_linkedList->AddLast(input->ReadObject()); + } + return this; + } + + /*System::UInt32 ObjectSize::get() + { + //TODO:: + System::UInt32 size = static_cast<System::UInt32> (sizeof(CacheableVector^)); + for each (IGeodeSerializable^ val in this) { + if (val != nullptr) { + size += val->ObjectSize; + } + } + return m_linkedList->Count; + }*/ + + virtual property System::UInt32 ObjectSize + { + virtual System::UInt32 get() + { + return m_linkedList->Count; + } + } + + virtual property System::Collections::Generic::LinkedList<Object^>^ Value + { + virtual System::Collections::Generic::LinkedList<Object^>^ get() + { + return m_linkedList; + } + } + // End Region: IGeodeSerializable Members + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableLinkedList(gcnew System::Collections::Generic::LinkedList<Object^>()); + } + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableObject.cpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableObject.cpp b/clicache/src/CacheableObject.cpp new file mode 100644 index 0000000..3bb18d2 --- /dev/null +++ b/clicache/src/CacheableObject.cpp @@ -0,0 +1,82 @@ +/* + * 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 "geode_includes.hpp" +#include "CacheableObject.hpp" +#include "DataInput.hpp" +#include "DataOutput.hpp" +#include "impl/GeodeNullStream.hpp" +#include "impl/GeodeDataInputStream.hpp" +#include "impl/GeodeDataOutputStream.hpp" + +using namespace System; +using namespace System::IO; +using namespace System::Runtime::Serialization::Formatters::Binary; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + void CacheableObject::ToData(DataOutput^ output) + { + if(m_obj != nullptr) + { + output->AdvanceCursor(4); // placeholder for object size bytes needed while reading back. + + GeodeDataOutputStream dos(output); + BinaryFormatter bf; + System::Int64 checkpoint = dos.Length; + bf.Serialize(%dos, m_obj); + m_objectSize = (System::UInt32) (dos.Length - checkpoint); + + output->RewindCursor(m_objectSize + 4); + output->WriteInt32(m_objectSize); + output->AdvanceCursor(m_objectSize); + } + } + + IGeodeSerializable^ CacheableObject::FromData(DataInput^ input) + { + int maxSize = input->ReadInt32(); + GeodeDataInputStream dis(input, maxSize); + System::UInt32 checkpoint = dis.BytesRead; + BinaryFormatter bf; + m_obj = bf.Deserialize(%dis); + m_objectSize = dis.BytesRead - checkpoint; + return this; + } + + System::UInt32 CacheableObject::ObjectSize::get() + { + if (m_objectSize == 0) { + GeodeNullStream ns; + BinaryFormatter bf; + bf.Serialize(%ns, m_obj); + + m_objectSize = (System::UInt32)sizeof(CacheableObject^) + (System::UInt32)ns.Length; + } + return m_objectSize; + } // namespace Client + } // namespace Geode +} // namespace Apache + + } //namespace + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableObject.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableObject.hpp b/clicache/src/CacheableObject.hpp new file mode 100644 index 0000000..3b9ff2f --- /dev/null +++ b/clicache/src/CacheableObject.hpp @@ -0,0 +1,156 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "IGeodeSerializable.hpp" +#include "GeodeClassIds.hpp" + +using namespace System; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// An mutable generic <see cref="System.Object" /> wrapper that can + /// serve as a distributable value for caching. + /// </summary> + /// <remarks> + /// <para> + /// This class can serialize any class which has either the + /// [Serializable] attribute set or implements + /// <see cref="System.Runtime.Serialization.ISerializable" /> interface. + /// However, for better efficiency the latter should be avoided and the + /// user should implement <see cref="../../IGeodeSerializable" /> instead. + /// </para><para> + /// The user must keep in mind that the rules that apply to runtime + /// serialization would be the rules that apply to this class. For + /// the serialization will be carried out by serializing all the + /// members (public/private/protected) of the class. Each of the + /// contained classes should also have either the [Serializable] + /// attribute set or implement <c>ISerializable</c> interface. + /// </para> + /// </remarks> + public ref class CacheableObject + : public IGeodeSerializable + { + public: + /// <summary> + /// Static function to create a new instance from the given object. + /// </summary> + /// <remarks> + /// If the given object is null then this method returns null. + /// </remarks> + inline static CacheableObject^ Create(Object^ value) + { + return (value != nullptr ? gcnew CacheableObject(value) : + nullptr); + } + + /// <summary> + /// Serializes this <see cref="System.Object" /> using + /// <see cref="System.Runtime.Serialization.Formatters.Binary.BinaryFormatter" /> class. + /// </summary> + /// <param name="output"> + /// the DataOutput object to use for serializing the object + /// </param> + virtual void ToData(DataOutput^ output); + + /// <summary> + /// Deserializes the <see cref="System.Object" /> using + /// <see cref="System.Runtime.Serialization.Formatters.Binary.BinaryFormatter" /> class. + /// </summary> + /// <param name="input"> + /// the DataInput stream to use for reading the object data + /// </param> + /// <returns>the deserialized object</returns> + virtual IGeodeSerializable^ FromData(DataInput^ input); + + /// <summary> + /// return the size of this object in bytes + /// </summary> + virtual property System::UInt32 ObjectSize + { + virtual System::UInt32 get(); + } + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + inline virtual System::UInt32 get() + { + return GeodeClassIds::CacheableManagedObject; + } + } + + /// <summary> + /// Gets the object value. + /// </summary> + /// <remarks> + /// The user can modify the object and the changes shall be reflected + /// immediately in the local cache. For this change to be propagate to + /// other members of the distributed system, the object needs to be + /// put into the cache. + /// </remarks> + property Object^ Value + { + inline Object^ get() + { + return m_obj; + } + } + + virtual String^ ToString() override + { + return (m_obj == nullptr ? nullptr : m_obj->ToString()); + } + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableObject(nullptr); + } + + internal: + /// <summary> + /// Allocates a new instance from the given object. + /// </summary> + inline CacheableObject(Object^ value) + : m_obj(value), m_objectSize(0) { } + + + + private: + Object^ m_obj; + System::UInt32 m_objectSize; + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableObjectArray.cpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableObjectArray.cpp b/clicache/src/CacheableObjectArray.cpp new file mode 100644 index 0000000..ac615fd --- /dev/null +++ b/clicache/src/CacheableObjectArray.cpp @@ -0,0 +1,108 @@ +/* + * 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 "begin_native.hpp" +#include <GeodeTypeIdsImpl.hpp> +#include "end_native.hpp" +#include "CacheableObjectArray.hpp" +#include "DataOutput.hpp" +#include "DataInput.hpp" +#include "ExceptionTypes.hpp" +#include "impl/SafeConvert.hpp" +#include "impl/PdxTypeRegistry.hpp" + +using namespace System; +using namespace System::Collections::Generic; + + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + // Region: IGeodeSerializable Members + + void CacheableObjectArray::ToData(DataOutput^ output) + { + output->WriteArrayLen((System::Int32)Count); + output->WriteByte((int8_t)apache::geode::client::GeodeTypeIdsImpl::Class); + output->WriteByte((int8_t)apache::geode::client::GeodeTypeIds::CacheableASCIIString); + output->WriteUTF("java.lang.Object"); + + for each (Object^ obj in this) { + //TODO::split + output->WriteObject(obj); + } + } + + IGeodeSerializable^ CacheableObjectArray::FromData(DataInput^ input) + { + int len = input->ReadArrayLen(); + if (len >= 0) { + //int8_t typeCode; + input->ReadByte(); // ignore CLASS typeid + input->ReadByte(); // ignore string typeid + unsigned short classLen; + classLen = input->ReadInt16(); + input->AdvanceCursor(classLen); + //nativeInput.readInt(&classLen); + //nativeInput.advanceCursor(classLen); + } + for (System::Int32 index = 0; index < len; ++index) { + Add(input->ReadObject()); + } + return this; + /*_GF_MG_EXCEPTION_TRY + + apache::geode::client::DataInput& nativeInput = *(input->_NativePtr); + System::Int32 len; + nativeInput.readArrayLen(&len); + if (len >= 0) { + int8_t typeCode; + nativeInput.read(&typeCode); // ignore CLASS typeid + nativeInput.read(&typeCode); // ignore string typeid + System::UInt16 classLen; + nativeInput.readInt(&classLen); + nativeInput.advanceCursor(classLen); + } + apache::geode::client::CacheablePtr value; + for (System::Int32 index = 0; index < len; ++index) { + nativeInput.readObject(value); + Add(SafeUMSerializableConvert(value.get())); + } + + _GF_MG_EXCEPTION_CATCH_ALL + return this;*/ + } + + System::UInt32 CacheableObjectArray::ObjectSize::get() + { + /* System::UInt32 size = static_cast<System::UInt32> (sizeof(CacheableObjectArray^)); + for each (IGeodeSerializable^ val in this) { + if (val != nullptr) { + size += val->ObjectSize; + } + }*/ + return Count; + } // namespace Client + } // namespace Geode +} // namespace Apache + + } //namespace http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableObjectArray.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableObjectArray.hpp b/clicache/src/CacheableObjectArray.hpp new file mode 100644 index 0000000..2a88330 --- /dev/null +++ b/clicache/src/CacheableObjectArray.hpp @@ -0,0 +1,153 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "IGeodeSerializable.hpp" +#include "GeodeClassIds.hpp" + + +using namespace System; +using namespace System::Collections::Generic; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// A mutable <c>IGeodeSerializable</c> object array wrapper that can serve + /// as a distributable object for caching. Though this class provides + /// compatibility with java Object[] serialization, it provides the + /// semantics of .NET generic <c>List</c> class. + /// </summary> + public ref class CacheableObjectArray + : public List<Object^>, public IGeodeSerializable + { + public: + /// <summary> + /// Allocates a new empty instance. + /// </summary> + inline CacheableObjectArray() + : List<Object^>() + { } + + /// <summary> + /// Allocates a new instance copying from the given collection. + /// </summary> + /// <param name="collection"> + /// The collection whose elements are copied to this list. + /// </param> + inline CacheableObjectArray(IEnumerable<Object^>^ collection) + : List<Object^>(collection) + { } + + /// <summary> + /// Allocates a new empty instance with given initial size. + /// </summary> + /// <param name="capacity"> + /// The initial capacity of the vector. + /// </param> + inline CacheableObjectArray(System::Int32 capacity) + : List<Object^>(capacity) + { } + + /// <summary> + /// Static function to create a new empty instance. + /// </summary> + inline static CacheableObjectArray^ Create() + { + return gcnew CacheableObjectArray(); + } + + /// <summary> + /// Static function to create a new instance copying from the + /// given collection. + /// </summary> + inline static CacheableObjectArray^ Create( + IEnumerable<Object^>^ collection) + { + return gcnew CacheableObjectArray(collection); + } + + /// <summary> + /// Static function to create a new instance with given initial size. + /// </summary> + inline static CacheableObjectArray^ Create(System::Int32 capacity) + { + return gcnew CacheableObjectArray(capacity); + } + + // Region: IGeodeSerializable Members + + /// <summary> + /// Serializes this object. + /// </summary> + /// <param name="output"> + /// the DataOutput object to use for serializing the object + /// </param> + virtual void ToData(DataOutput^ output); + + /// <summary> + /// Deserialize this object, typical implementation should return + /// the 'this' pointer. + /// </summary> + /// <param name="input"> + /// the DataInput stream to use for reading the object data + /// </param> + /// <returns>the deserialized object</returns> + virtual IGeodeSerializable^ FromData(DataInput^ input); + + /// <summary> + /// return the size of this object in bytes + /// </summary> + virtual property System::UInt32 ObjectSize + { + virtual System::UInt32 get(); + } + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + virtual System::UInt32 get() + { + return GeodeClassIds::CacheableObjectArray; + } + } + + // End Region: IGeodeSerializable Members + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableObjectArray(); + } + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableObjectXml.cpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableObjectXml.cpp b/clicache/src/CacheableObjectXml.cpp new file mode 100644 index 0000000..5899a20 --- /dev/null +++ b/clicache/src/CacheableObjectXml.cpp @@ -0,0 +1,111 @@ +/* + * 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 "geode_includes.hpp" +#include "CacheableObjectXml.hpp" +#include "DataInput.hpp" +#include "DataOutput.hpp" +#include "Log.hpp" +#include "impl/GeodeNullStream.hpp" +#include "impl/GeodeDataInputStream.hpp" +#include "impl/GeodeDataOutputStream.hpp" + +using namespace System; +using namespace System::IO; +using namespace System::Xml::Serialization; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + void CacheableObjectXml::ToData(DataOutput^ output) + { + if (m_obj == nullptr) { + output->WriteByte((Byte)1); + } + else + { + output->WriteByte((Byte)0); + Type^ objType = m_obj->GetType(); + + output->WriteUTF(objType->AssemblyQualifiedName); + + output->AdvanceCursor(4); // placeholder for object size bytes needed while reading back. + + XmlSerializer xs(objType); + GeodeDataOutputStream dos(output); + System::Int64 checkpoint = dos.Length; + xs.Serialize(%dos, m_obj); + m_objectSize = (System::UInt32) (dos.Length - checkpoint); + + output->RewindCursor(m_objectSize + 4); + output->WriteInt32(m_objectSize); + output->AdvanceCursor(m_objectSize); + } + } + + IGeodeSerializable^ CacheableObjectXml::FromData(DataInput^ input) + { + Byte isNull = input->ReadByte(); + if (isNull) { + m_obj = nullptr; + } + else { + String^ typeName = input->ReadUTF(); + Type^ objType = Type::GetType(typeName); + if (objType == nullptr) + { + Log::Error("CacheableObjectXml.FromData(): Cannot find type '" + + typeName + "'."); + m_obj = nullptr; + } + else { + int maxSize = input->ReadInt32(); + GeodeDataInputStream dis(input, maxSize); + XmlSerializer xs(objType); + System::UInt32 checkpoint = dis.BytesRead; + m_obj = xs.Deserialize(%dis); + m_objectSize = dis.BytesRead - checkpoint; + } + } + return this; + } + + System::UInt32 CacheableObjectXml::ObjectSize::get() + { + if (m_objectSize == 0) { + if (m_obj != nullptr) { + Type^ objType = m_obj->GetType(); + XmlSerializer xs(objType); + GeodeNullStream ns; + xs.Serialize(%ns, m_obj); + + m_objectSize = (System::UInt32)sizeof(CacheableObjectXml^) + (System::UInt32)ns.Length; + } + } + return m_objectSize; + } // namespace Client + } // namespace Geode +} // namespace Apache + + } //namespace + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableObjectXml.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableObjectXml.hpp b/clicache/src/CacheableObjectXml.hpp new file mode 100644 index 0000000..fdd1753 --- /dev/null +++ b/clicache/src/CacheableObjectXml.hpp @@ -0,0 +1,157 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "IGeodeSerializable.hpp" +#include "GeodeClassIds.hpp" + +using namespace System; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// A mutable generic <see cref="System.Object" /> wrapper that can + /// serve as a distributable value for caching. + /// </summary> + /// <remarks> + /// <para> + /// This class can contain any object and uses the + /// <see cref="System.Xml.Serialization.XmlSerializer" /> to + /// serialize and deserialize the object. So the user must use the + /// <c>XmlSerializer</c> attributes to control the serialization/deserialization + /// of the object (or implement the <see cref="System.Xml.Serialization.IXmlSerializable" />) + /// to change the serialization/deserialization. However, the latter should + /// be avoided for efficiency reasons and the user should implement + /// <see cref="../../IGeodeSerializable" /> instead. + /// </para><para> + /// The user must keep in mind that the rules that apply to <c>XmlSerializer</c> + /// would be the rules that apply to this class. For instance the user + /// cannot pass objects of class implementing or containing + /// <see cref="System.Collections.IDictionary" /> class, must use + /// <see cref="System.Xml.Serialization.XmlIncludeAttribute" /> to + /// mark user-defined types etc. + /// </para> + /// </remarks> + public ref class CacheableObjectXml + : public IGeodeSerializable + { + public: + /// <summary> + /// Static function to create a new instance from the given object. + /// </summary> + /// <remarks> + /// If the given object is null then this method returns null. + /// </remarks> + inline static CacheableObjectXml^ Create(Object^ value) + { + return (value != nullptr ? gcnew CacheableObjectXml(value) : + nullptr); + } + + /// <summary> + /// Serializes this <see cref="System.Object" /> using + /// <see cref="System.Xml.Serialization.XmlSerializer" /> class. + /// </summary> + /// <param name="output"> + /// the DataOutput object to use for serializing the object + /// </param> + virtual void ToData(DataOutput^ output); + + /// <summary> + /// Deserializes the <see cref="System.Object" /> using + /// <see cref="System.Xml.Serialization.XmlSerializer" /> class. + /// </summary> + /// <param name="input"> + /// the DataInput stream to use for reading the object data + /// </param> + /// <returns>the deserialized object</returns> + virtual IGeodeSerializable^ FromData(DataInput^ input); + + /// <summary> + /// return the size of this object in bytes + /// </summary> + virtual property System::UInt32 ObjectSize + { + virtual System::UInt32 get(); + } + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + inline virtual System::UInt32 get() + { + return GeodeClassIds::CacheableManagedObjectXml; + } + } + + /// <summary> + /// Gets the object value. + /// </summary> + /// <remarks> + /// The user can modify the object and the changes shall be reflected + /// immediately in the local cache. For this change to be propagate to + /// other members of the distributed system, the object needs to be + /// put into the cache. + /// </remarks> + property Object^ Value + { + inline Object^ get() + { + return m_obj; + } + } + + virtual String^ ToString() override + { + return (m_obj == nullptr ? nullptr : m_obj->ToString()); + } + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableObjectXml(nullptr); + } + + internal: + /// <summary> + /// Allocates a new instance from the given object. + /// </summary> + inline CacheableObjectXml(Object^ value) + : m_obj(value), m_objectSize(0) { } + + private: + Object^ m_obj; + System::UInt32 m_objectSize; + }; + } // namespace Client + } // namespace Geode +} // namespace Apache + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableStack.cpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableStack.cpp b/clicache/src/CacheableStack.cpp new file mode 100644 index 0000000..6b5d1c8 --- /dev/null +++ b/clicache/src/CacheableStack.cpp @@ -0,0 +1,91 @@ +/* + * 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 "CacheableStack.hpp" +#include "DataOutput.hpp" +#include "DataInput.hpp" +#include "begin_native.hpp" +#include <GeodeTypeIdsImpl.hpp> +#include "end_native.hpp" +#include "impl/SafeConvert.hpp" +#include "GeodeClassIds.hpp" + +using namespace System; +using namespace System::Collections::Generic; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + // Region: IGeodeSerializable Members + + void CacheableStack::ToData(DataOutput^ output) + { + if (m_stack != nullptr) + { + output->WriteArrayLen((System::Int32)m_stack->Count); + for each (Object^ obj in m_stack) { + output->WriteObject(obj); + } + } + else + { + output->WriteByte(0xFF); + } + } + + IGeodeSerializable^ CacheableStack::FromData(DataInput^ input) + { + int len = input->ReadArrayLen(); + if (len > 0) + { + System::Collections::Generic::Stack<Object^>^ stack = safe_cast<System::Collections::Generic::Stack<Object^>^>(m_stack); + for (int i = 0; i < len; i++) + { + (stack)->Push(input->ReadObject()); + // Push(input->ReadObject()); + } + } + return this; + } + + System::UInt32 CacheableStack::ClassId::get() + { + return GeodeClassIds::CacheableStack; + } + + System::UInt32 CacheableStack::ObjectSize::get() + { + //TODO: + /*System::UInt32 size = static_cast<System::UInt32> (sizeof(CacheableStack^)); + for each (IGeodeSerializable^ val in this) { + if (val != nullptr) { + size += val->ObjectSize; + } + }*/ + return m_stack->Count; + } // namespace Client + } // namespace Geode + } // namespace Apache + +} //namespace + http://git-wip-us.apache.org/repos/asf/geode-native/blob/6cbd424f/clicache/src/CacheableStack.hpp ---------------------------------------------------------------------- diff --git a/clicache/src/CacheableStack.hpp b/clicache/src/CacheableStack.hpp new file mode 100644 index 0000000..fbebf6a --- /dev/null +++ b/clicache/src/CacheableStack.hpp @@ -0,0 +1,130 @@ +/* + * 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. + */ + +#pragma once + +#include "geode_defs.hpp" +#include "IGeodeSerializable.hpp" + + +using namespace System; +using namespace System::Collections::Generic; + +namespace Apache +{ + namespace Geode + { + namespace Client + { + + /// <summary> + /// A mutable <c>IGeodeSerializable</c> vector wrapper that can serve as + /// a distributable object for caching. + /// </summary> + ref class CacheableStack + : public IGeodeSerializable + { + public: + /// <summary> + /// Allocates a new empty instance. + /// </summary> + inline CacheableStack(System::Collections::ICollection^ stack) + { + m_stack = stack; + } + + /// <summary> + /// Static function to create a new empty instance. + /// </summary> + inline static CacheableStack^ Create() + { + return gcnew CacheableStack(gcnew System::Collections::Generic::Stack<Object^>()); + } + + /// <summary> + /// Static function to create a new empty instance. + /// </summary> + inline static CacheableStack^ Create(System::Collections::ICollection^ stack) + { + return gcnew CacheableStack(stack); + } + + + + // Region: IGeodeSerializable Members + + /// <summary> + /// Serializes this object. + /// </summary> + /// <param name="output"> + /// the DataOutput object to use for serializing the object + /// </param> + virtual void ToData(DataOutput^ output); + + /// <summary> + /// Deserialize this object, typical implementation should return + /// the 'this' pointer. + /// </summary> + /// <param name="input"> + /// the DataInput stream to use for reading the object data + /// </param> + /// <returns>the deserialized object</returns> + virtual IGeodeSerializable^ FromData(DataInput^ input); + + /// <summary> + /// return the size of this object in bytes + /// </summary> + virtual property System::UInt32 ObjectSize + { + virtual System::UInt32 get(); + } + + /// <summary> + /// Returns the classId of the instance being serialized. + /// This is used by deserialization to determine what instance + /// type to create and deserialize into. + /// </summary> + /// <returns>the classId</returns> + virtual property System::UInt32 ClassId + { + virtual System::UInt32 get(); + } + + virtual property System::Collections::ICollection^ Value + { + virtual System::Collections::ICollection^ get() + { + return m_stack; + } + } + // End Region: IGeodeSerializable Members + + /// <summary> + /// Factory function to register this class. + /// </summary> + static IGeodeSerializable^ CreateDeserializable() + { + return gcnew CacheableStack(gcnew System::Collections::Generic::Stack<Object^>()); + } + + private: + System::Collections::ICollection^ m_stack; + }; + } // namespace Client + } // namespace Geode +} // namespace Apache +