You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by mm...@apache.org on 2021/09/23 04:38:58 UTC

[geode-native] 03/03: Support for 32-bit keys/values - includes signed and unsigned

This is an automated email from the ASF dual-hosted git repository.

mmartell pushed a commit to branch wip-bytearray-for-values
in repository https://gitbox.apache.org/repos/asf/geode-native.git

commit 198ac47d176d70bab097aee984d264d62f17a224
Author: Mike Martell <mm...@pivotal.io>
AuthorDate: Wed Sep 22 21:38:46 2021 -0700

    Support for 32-bit keys/values
    - includes signed and unsigned
---
 c-bindings/include/geode/region.h         |  7 ++++
 c-bindings/src/region.cpp                 | 42 +++++++++++++++++++
 c-bindings/src/region.hpp                 |  4 ++
 netcore/NetCore.Test/RegionFactoryTest.cs |  8 ++++
 netcore/NetCore/Region.cs                 | 67 +++++++++++++++++++++++++++++--
 5 files changed, 125 insertions(+), 3 deletions(-)

diff --git a/c-bindings/include/geode/region.h b/c-bindings/include/geode/region.h
index 7355d04..6c99232 100644
--- a/c-bindings/include/geode/region.h
+++ b/c-bindings/include/geode/region.h
@@ -42,12 +42,19 @@ APACHE_GEODE_C_EXPORT void apache_geode_Region_PutString(
 APACHE_GEODE_C_EXPORT void apache_geode_Region_PutByteArray(
     apache_geode_region_t* region, const char* key, const char* value, size_t size);
 
+APACHE_GEODE_C_EXPORT void apache_geode_Region_PutByteArrayForInt32Key(
+    apache_geode_region_t* region, int32_t key, const char* value,
+    size_t size);
+
 APACHE_GEODE_C_EXPORT const char* apache_geode_Region_GetString(
     apache_geode_region_t* region, const char* key);
 
 APACHE_GEODE_C_EXPORT void apache_geode_Region_GetByteArray(
     apache_geode_region_t* region, const char* key, char** value, size_t* size);
 
+APACHE_GEODE_C_EXPORT void apache_geode_Region_GetByteArrayForInt32Key(
+    apache_geode_region_t* region, const int32_t key, char** value, size_t* size);
+
 APACHE_GEODE_C_EXPORT void apache_geode_Region_Remove(
     apache_geode_region_t* region, const char* key);
 
diff --git a/c-bindings/src/region.cpp b/c-bindings/src/region.cpp
index 500506b..707ed4f 100644
--- a/c-bindings/src/region.cpp
+++ b/c-bindings/src/region.cpp
@@ -55,6 +55,12 @@ void RegionWrapper::PutByteArray(const std::string& key, const char* value,
   region_->put(key, apache::geode::client::CacheableBytes::create(val));
 }
 
+void RegionWrapper::PutByteArray(const int32_t key, const char* value,
+                                 size_t size) {
+  std::vector<int8_t> val(value, value + size);
+  region_->put(key, apache::geode::client::CacheableBytes::create(val));
+}
+
 const char* RegionWrapper::GetString(const std::string& key) {
   auto value = region_->get(key);
   lastValue_ =
@@ -84,6 +90,27 @@ void RegionWrapper::GetByteArray(const std::string& key, char** value,
   }
 }
 
+void RegionWrapper::GetByteArray(const int32_t key, char** value,
+                                 size_t* size) {
+  std::shared_ptr<apache::geode::client::Serializable> val = region_->get(key);
+
+  if (val.get() == nullptr) return;
+
+  auto bytes =
+      std::dynamic_pointer_cast<apache::geode::client::CacheableBytes>(val);
+  int valSize = val->objectSize();
+#if defined(_WIN32)
+  int8_t* byteArray = static_cast<int8_t*>(CoTaskMemAlloc(valSize));
+#else
+  int8_t* byteArray = static_cast<int8_t*>(malloc(valSize));
+#endif
+  if (bytes) {
+    memcpy(byteArray, bytes->value().data(), valSize);
+    *value = reinterpret_cast<char*>(byteArray);
+    *size = valSize;
+  }
+}
+
 void RegionWrapper::Remove(const std::string& key) { region_->remove(key); }
 
 bool RegionWrapper::ContainsValueForKey(const std::string& key) {
@@ -108,6 +135,14 @@ void apache_geode_Region_PutByteArray(apache_geode_region_t* region,
   regionWrapper->PutByteArray(key, value, size);
 }
 
+void apache_geode_Region_PutByteArrayForInt32Key(apache_geode_region_t* region,
+                                                 const int32_t key,
+                                                 const char* value,
+                                                 size_t size) {
+  RegionWrapper* regionWrapper = reinterpret_cast<RegionWrapper*>(region);
+  regionWrapper->PutByteArray(key, value, size);
+}
+
 const char* apache_geode_Region_GetString(apache_geode_region_t* region,
                                           const char* key) {
   RegionWrapper* regionWrapper = reinterpret_cast<RegionWrapper*>(region);
@@ -121,6 +156,13 @@ void apache_geode_Region_GetByteArray(apache_geode_region_t* region,
   return regionWrapper->GetByteArray(key, value, size);
 }
 
+void apache_geode_Region_GetByteArrayForInt32Key(apache_geode_region_t* region,
+                                                 const int32_t key,
+                                                 char** value, size_t* size) {
+  RegionWrapper* regionWrapper = reinterpret_cast<RegionWrapper*>(region);
+  return regionWrapper->GetByteArray(key, value, size);
+}
+
 void apache_geode_Region_Remove(apache_geode_region_t* region,
                                 const char* key) {
   RegionWrapper* regionWrapper = reinterpret_cast<RegionWrapper*>(region);
diff --git a/c-bindings/src/region.hpp b/c-bindings/src/region.hpp
index 603a2c7..b2e1392 100644
--- a/c-bindings/src/region.hpp
+++ b/c-bindings/src/region.hpp
@@ -37,10 +37,14 @@ class RegionWrapper : public ClientKeeper {
 
   void PutByteArray(const std::string& key, const char* value, size_t size);
 
+  void PutByteArray(const int32_t key, const char* value, size_t size);
+
   const char* GetString(const std::string& key);
 
   void GetByteArray(const std::string& key, char** value, size_t* size);
 
+  void GetByteArray(const int32_t key, char** value, size_t* size);
+
   void Remove(const std::string& key);
 
   bool ContainsValueForKey(const std::string& key);
diff --git a/netcore/NetCore.Test/RegionFactoryTest.cs b/netcore/NetCore.Test/RegionFactoryTest.cs
index 4076349..9941f30 100644
--- a/netcore/NetCore.Test/RegionFactoryTest.cs
+++ b/netcore/NetCore.Test/RegionFactoryTest.cs
@@ -57,12 +57,20 @@ namespace Apache.Geode.Client {
       region.Put(Username3,779);
       var value3 = region.Get(Username3);
 
+      region.Put(666, 66666);
+      var value4 = region.Get(666);
+
+      region.Put((uint)333, 33333);
+      var value5 = region.Get(333);
+
       var user1 = region.GetString(Username1);
       var user2 = region.GetString(Username2);
 
       Assert.Equal(user1, fullname1);
       Assert.Equal(user2, fullname2);
       Assert.Equal(779, value3);
+      Assert.Equal(66666, value4);
+      Assert.Equal(33333, value5);
     }
 
     private void DoRemoves(Region region) {
diff --git a/netcore/NetCore/Region.cs b/netcore/NetCore/Region.cs
index 7b6e400..53a4a9e 100644
--- a/netcore/NetCore/Region.cs
+++ b/netcore/NetCore/Region.cs
@@ -32,6 +32,11 @@ namespace Apache.Geode.Client {
                                                                 IntPtr value, int length);
 
     [DllImport(Constants.libPath, CharSet = CharSet.Auto)]
+    private static extern bool apache_geode_Region_PutByteArrayForInt32Key(IntPtr region,
+          [param: MarshalAs(UnmanagedType.I4)] Int32 key,
+          IntPtr value, int length);
+
+    [DllImport(Constants.libPath, CharSet = CharSet.Auto)]
     private static extern IntPtr apache_geode_Region_GetString(IntPtr region, IntPtr key);
 
     [DllImport(Constants.libPath, CharSet = CharSet.Auto)]
@@ -39,6 +44,11 @@ namespace Apache.Geode.Client {
                                                                 ref IntPtr value, ref int size);
 
     [DllImport(Constants.libPath, CharSet = CharSet.Auto)]
+    private static extern void apache_geode_Region_GetByteArrayForInt32Key(IntPtr region,
+          [param: MarshalAs(UnmanagedType.I4)] Int32 key,
+          ref IntPtr value, ref int size);
+
+    [DllImport(Constants.libPath, CharSet = CharSet.Auto)]
     private static extern void apache_geode_Region_Remove(IntPtr region, IntPtr key);
 
     [DllImport(Constants.libPath, CharSet = CharSet.Auto)]
@@ -71,6 +81,15 @@ namespace Apache.Geode.Client {
       Marshal.FreeCoTaskMem(valuePtr);
     }
 
+    public void PutByteArray(Int32 key, byte[] value)
+    {
+      //var keyPtr = Marshal.AllocCoTaskMem(4);
+      var valuePtr = Marshal.AllocCoTaskMem(value.Length);
+      Marshal.Copy(value, 0, valuePtr, value.Length);
+      apache_geode_Region_PutByteArrayForInt32Key(_containedObject, key, valuePtr, value.Length);
+      Marshal.FreeCoTaskMem(valuePtr);
+    }
+
     public void Put<TKey, TValue>(TKey key, TValue value)
     {
       // This is a generic, so can't do any marshaling directly.
@@ -85,13 +104,32 @@ namespace Apache.Geode.Client {
         //  _formatter.Serialize(_fs, value);
         //  PutByteArray(key.ToString(), _fs.ToArray());
         //}
-        if (value.GetType() == typeof(int)) {
+        if (value.GetType() == typeof(int) || value.GetType() == typeof(uint)) {
+    
           //PutByteArray(key.ToString(), BitConverter.GetBytes(Convert.ToInt32(value)));
           var byteArray = new byte[4];
           Array.Copy(BitConverter.GetBytes(Convert.ToInt32(value)), byteArray, 4);
           PutByteArray(key.ToString(), byteArray);
         }
-
+      }
+      else if (key.GetType() == typeof(int) || key.GetType() == typeof(uint))
+      {
+        var type = value.GetType();
+        if (!type.IsSerializable)
+          throw new Exception("Error: Object is not Serializable.");
+        //using (var _fs = new System.IO.MemoryStream())
+        //{
+        //  var _formatter = new BinaryFormatter();
+        //  _formatter.Serialize(_fs, value);
+        //  PutByteArray(key.ToString(), _fs.ToArray());
+        //}
+        if (value.GetType() == typeof(int) || value.GetType() == typeof(uint))
+        {
+          //PutByteArray(key.ToString(), BitConverter.GetBytes(Convert.ToInt32(value)));
+          var byteArray = new byte[4];
+          Array.Copy(BitConverter.GetBytes(Convert.ToInt32(value)), byteArray, 4);
+          PutByteArray(Convert.ToInt32(key), byteArray);
+        }
       }
       else {
         throw new NotImplementedException();
@@ -101,11 +139,18 @@ namespace Apache.Geode.Client {
     public int Get<TKey>(TKey key)
     {
       // This is a generic, so can't do any marshaling directly.
-      //if (key.GetType() == typeof(string))
+      if (key.GetType() == typeof(string))
       {
         var value = GetByteArray(key.ToString());
         return BitConverter.ToInt32(value);
       }
+      if (key.GetType() == typeof(int))
+      {
+        var value = GetByteArray(Convert.ToInt32(key));
+        return BitConverter.ToInt32(value);
+      }
+      else
+        throw new NotImplementedException();
     }
 
     public string GetString(string key) {
@@ -130,6 +175,22 @@ namespace Apache.Geode.Client {
         return null;
     }
 
+    public byte[] GetByteArray(int key)
+    {
+      var valPtr = (IntPtr)0;
+      int size = 0;
+      apache_geode_Region_GetByteArrayForInt32Key(_containedObject, key, ref valPtr, ref size);
+      if (size > 0)
+      {
+        Byte[] byteArray = new Byte[size];
+        Marshal.Copy(valPtr, byteArray, 0, size);
+        Marshal.FreeCoTaskMem(valPtr);
+        return byteArray;
+      }
+      else
+        return null;
+    }
+
     public void Remove(string key) {
       var keyPtr = Marshal.StringToCoTaskMemUTF8(key);
       apache_geode_Region_Remove(_containedObject, keyPtr);