Is updating a part of a value possible in Redis? - redis

I have to use Redis and Jedis in a project, in order to access quickly to some data from generated keys. The value stored will be some complex Java objects.
Is it possible to update only a part of this value, without getting it before ?
For exemple, if I serialize an object of a classe like that
public class MyObject {
private MySubObject1 myObj1;
private MySubObject2 myObj2;
private MySubObject3 myObj3;
}
If MyObject is associated with a key in Redis, can I from that key only update the myObj2 field ? Or have I to get the value first, update the field and put it again in Redis ?

If you want to update like this. You can use a hash instead of flat key value store.
Here is the snippet
hset("MyObject","myObj1",(serialized value of myobj1))
hset("MyObject","myObj2",(serialized value of myobj2)) ...
If you want to change myObj2 alone
do hset("MyObject","myObj2",(new value))

Related

Accessing properties of a Kotlin entity

I'm new to Kotlin, so apologies if I'm not articulating concepts correctly. I have an instance of an Entity
[TestEntity(applicationId=1, timestamp=2018-01-24T18:40:30, issueState=MA, product=test, status=sold, paymentMode=VISA, premium=null)]
I am writing a service that is going to take these keys and use them to create the headers of a report. The keys may change depending on the type of report the user is trying to generate, which will have an impact on the Entity that will be instantiated.
I want to be able to iterate over this Entity so that I can create an array to use for the headers. Any thoughts on how I do this?
I think the cleanest solution is storing values in a map and delegating properties to it.
Don't think you can otherwise iterate over class fields without some verbose getter chain or ugly reflection shenanigans.
For example here you can access map fields as if they were class fields, but can also easily iterate over map.
data class TestEntity(val map : Map<String, Any>){
val appId : Int by map
val timeStamp : Long by map
(... more fields)
}

Json.net not reading back primary key

I'm using Newsoft JSON lib in a vb .net project and I found this error I can't solve.
I use CLSA objects of type Formulario and add some to a list. When I serialize that list the Primary Keys value are fine. In this example the first object of the list has a value of 3 in the ID property but after deserialize the object that value returns 0.
This happend with every ID. I check CSLA class and try to tweak some values like Int32 to Integer or change the readonly property to a read/write property but no avail.
Am I missing something? Every other property has the correct value but the IDs.
Thanks!

How to remove an item from Redis set using a property as determination?

When we remove items from a Redis Set using servicestack typed client
redisset = typedclient.Sets["setkey"];
redisset.remove(object1);
It usually will check every properties of object1, how do we define class of object1 which property to check for equality?
For example, object1 has those properties
session_key:"somekey"
session_name:"a name"
author:"Harry"
...
When we try to remove it from a set, we only want it to check to see if the session_key is matching and ignore other properties.
I thought I saw it once some where but I couldn't find it anymore.
Complex types in Sets are blobbed as JSON. None of Redis Operations work on blobbed values which are opaque to Redis. You would need to go through each item in the Set on the client, i.e. deserialize it back into an object, check the session key then remove the matching entry.
The only option to do this on the server is to use a custom server-side lua script to parse the json into a Lua object and check the property that way.

Map implementaion using arraylist as value

I am using Maps in my code for the first time, hence require some inputs from you experts.
My requirement is I have to check two different tables from database. Value from First table will be used as Key and Value for second table will be used as Value for the key.
Each key will have multiple values, so I will be storing all values against each key in a arraylist i.e. my Map will be like MAP.
Now, my issue is following:
I don't know the total no. of keys, so I can't create arraylist objects in advance. How to manage this?
How can I check if key exists in map such that if it exists then I have to updated the arraylist corresponding to it only. And if it doesn't exist then create new key, create arraylist corresponding to it, populate arraylist with the value.
Finally I have to iterate whole map and use the key and values.
How can it be implemented? Am I following the right approach? if not what is a better approach?
Thanks
With lists in Java you do not need to know the size up front. That is a requirement for Arrays. Therefore just create your Map
Map> myMap = new HashMap<>();
This should work
if (myMap.containsKey(someKey)) {
myMap.get(someKey).add(someValue); // adds a value to the list that already is in the map
} else {
myMap.put(someKey, Arrays.asList(someValue)); // which inserts a new key/value
}
If you need to iterate over all values in the list then you need a nested for loop
for (Map.Entry> entry : myMap.entrySet()) {
// your key = entry.getKey()
for (ValueType value : entry.getValue()) {
// use your value
}
}
http://docs.oracle.com/javase/7/docs/api/java/util/Map.html
Hope that helps.

NHibernate autoincrement string column

I want to have a column of type string that will be composed of static prefix and dynamic auto incremented integer. Number should autoincrement on every insert. This column is not PK but it should act like id. Also i would like to filter on that column.
Something like
PREFIX_0000
PREFIX_0001
PREFIX_0002
PREFIX_0003
I don't want to store a counter in some table.
Is there a possibility to do that on NHibernate mapping level? Or any other ideas?
Thanks.
in you DTO you can make a getter which returns a prefix_######ID:
public string MyAutoIncrement
{
get { return string.Format("{0}_{1:D8}",this.Prefix,this.Id); }
}