Retrieve Object using ObjectID

#1
Hi, how can we retrieve the data from Store using ObjectID. We are using Byte Array as the Key name while inserting but when we want to retrieve a specific Key we are finding it difficult. But we do not want to loop through the entire Keys and identify the correct one. Can you please help if this is possible.
 

admin

Administrator
#1
Hi Chakriau,
Yes, you can use a byte array as object's key for storing and retrieving objects. Please let us know what ScaleOut product and its version you are using and post the code snippet that would illustrate the issue. We will be glad to help you finding the problem.
 
#1
Hi, below is the version information.
Console Version: 5.0.18.218
Library Version: 5.0.23.223
Code:
private static NamedCache _cache;
_cache = CacheFactory.GetCache("CacheNamespace");
cacheObject = _cache.Get("54657374696E67000000000000000000");
The Actual ObjectID displayed in the ObjectBrowser is "0x54657374696E67000000000000000000".
 

admin

Administrator
#1
The NamedCache API supports byte arrays and string keys (and GUIDs) as object keys. Please make sure that if your program code is expecting to work with byte arrays, you're calling the appropriate NamedCache overload. Please see the Named Cache API documentation for more information on available method overloads.

In the code example above, you've used the string overload, which uses a string key. String keys and byte arrays representing the same readable value in your above sample are not equivalent keys (due to encoding - i.e., "0" (string) is not equivalent to 0 (byte)). See the below code sample for an illustration:

Code:
Code:
NamedCache cache = CacheFactory.GetCache("CacheNamespace");
string stringObject = "stored with string key";
string byteArrayObject = "stored with byte array";

// store the objects with different key methods
string stringKey = "54657374696E67000000000000000000";
byte[] byteArrayKey = Encoding.UTF8.GetBytes(stringKey);

// store using string key
// object ID: 54657374696E67000000000000000000
cache.Add(stringKey, stringObject);   

// store using byte array representation of the same string value
// object ID: 0x3534363537333734363936453637303030303030303030303030303030303030
cache.Add(byteArrayKey, byteArrayObject);

// get the object stored with string key
string stringObjectRet = (string)cache.Get(stringKey);
Console.WriteLine("Object value retrieved with string key: {0}", stringObjectRet);

// get the object stored with a byte array
string byteArrayObjectRet = (string)cache.Get(byteArrayKey);
Console.WriteLine("Object value retrieved with string key: {0}", byteArrayObjectRet);
Output:
Object value retrieved with string key: stored with string key
Object value retrieved with byte array key: stored with byte array
 
#1
Hi Team, Thanks for the reply. The above code works fine as long as we know the KeyName that we have used to Insert the object. But the issue we are facing is little different. Below is the complete scenario to give better picture.
  • Key is generated based on multiple values related to the item and generated dynamically.
  • It is converted to Byte Array using Encoding.ASCII.GetBytes.
  • While retrieving we are able to retrieve in the Application which is fine.
  • But if we need to verify a single object by taking it from Object Browser, we are able to do that since we do not have the exact key that is generated to create this key.
  • since the inserted data is encrypted, we are not able to view the data as well.
Below is the screenshot of one of the object from ObjectBrowser. We are trying to retrieve the value based on "Object ID" i.e. 0x54657374696E67000000000000000000.
/storage/temp/31-soss.png
 

admin

Administrator
#1
SOSS object keys are stored as 128-bit keys (16 bytes), which is what you're seeing in the Object Browser.

To retrieve an object with the key 0x54657374696E67000000000000000000 from outside your application's logic, you can construct the byte array manually:

Code:
byte[] byteKey = {0x54, 0x65, 0x73, 0x74, 0x69, 0x6E, 0x67, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
object myObject = cache.Get(byteKey);
Depending on your requirements, you can use any of the .NET methods in Encoding, BitConverter, etc. to help automate this process.
Please be aware that if you continue this approach, you will be limited to encoded key names no greater than 16 characters (assuming an 8-bit encoding). You may wish to use the automatic "string key" functionality available with the Named Cache to enable you to use arbitrary strings (which we hash internally) for you keys; this latter approach would also enable a 1:1 readable lookup in the Object Browser.
 
Top