hazelcast map put operation not working - java

I am using hazelcast -2.5 in a cluster. I have a map (key: String, value: ArrayList of user defined objects). I am able to put/remove fine in most places but in one specific part of my code, the put operation fails silently (the key string used for the put operation is unique and the ArrayList is not empty either). No exceptions are thrown. In case there was a lock involved, I even tried tryPut and that call gave me a true return value. Right after the put operation, I tried printing out the keySet for the map but cannot see the key I just inserted - the size of the map has not changed either (yet the tryPut gave me a true return value and I'm reasonably sure the string I am using for the key is unique - and I am hoping the binary form for the key is unique as well). If the binary form for my key is not unique, I am assuming that the tryPut should return a false return value or at least replace the previously added key/value with the new key/value pair (unless I misinterpreted the docs).
boolean putVal = testMap.tryPut(this.testObj.UUID, testEntity, timeout, TimeUnit.MILLISECONDS); //timeout is 2000L or 2 seconds in this case
Any thoughts on troubleshooting this or figuring out if the binary form for my key is causing the issue will be appreciated.
Thanks

Try to do a get. And see if there is any value associated with that key. If not, the put should be successful.

Related

How to list the data using redis-cli console?

I am able to add & view the key value pairs through my restful API method invocations.
But after adding the key value pairs, when I try to list/ view them using redis-cli console, it is not listing any values.
As you can notice, in the console, it is listing some junk values for the **keys *** command (after adding new key/value via browser), but when I try to retrieve the key, it is showing up as empty.
What could be the reason for this?
How to list the values properly in the console?
also attaching the restful api method definitions:
The value you're seeing in the output of KEYS * is the java-serialized string user.
The first two bytes \xac\xed (hex: 0xACED) is the STREAM_MAGIC constant.
The next two bytes \x00\x05 (hex: 0x0005) is the STREAM_VERSION, version of the serialization protocol.
The next byte, t is 0x74 = TC_STRING meaning is a string object.
Finally \x00\x04 is the length of the string.
This protocol is described in the Object Serialization Stream Protocol, in 6.4.2 Terminal Symbols and Constants
You probably want to review your code as to why are the strings being java-serialized before reaching Redis. Probably it is because of the h: that shows in the screenshot.
On the meantime, you can do GET "\xac\xed\x00\x05t\x00\x04user" to inspect the value of your user key.

MongoDB (Java): efficient update of multiple documents to different(!) values

I have a MongoDB database and the program I'm writing is meant to change the values of a single field for all documents in a collection. Now if I want them all to change to a single value, like the string value "mask", then I know that updateMany does the trick and it's quite efficient.
However, what I want is an efficient solution for updating to different new values, in fact I want to pick the new value for the field in question for each document from a list, e.g. an ArrayList. But then something like this
collection.updateMany(new BasicDBObject(),
new BasicDBObject("$set",new BasicDBObject(fieldName,
listOfMasks.get(random.nextInt(size)))));
wouldn't work since updateMany doesn't recompute the value that the field should be set to, it just computes what the argument
listOfMasks.get(random.nextInt(size))
would be once and then it uses that for all the documents. So I don't think there's a solution to this problem that can actually employ updateMany since it's simply not versatile enough.
But I was wondering if anyone has any ideas for at least making it faster than simply iterating through all the documents and each time do updateOne where it updates to a new value from the ArrayList (in a random order but that's just a detail), like below?
// Loop until the MongoCursor is empty (until the search is complete)
try {
while (cursor.hasNext()) {
// Pick a random mask
String mask = listOfMasks.get(random.nextInt(size));
// Update this document
collection.updateOne(cursor.next(), Updates.set("test_field", mask));
}
} finally {
cursor.close();
}```
MongoDB provides the bulk write API to batch updates. This would be appropriate for your example of setting the value of a field to a random value (determined on the client) for each document.
Alternatively if there is a pattern to the changes needed you could potentially use find and modify operation with the available update operators.

how to know from Option[Map[String,Seq[String]]] contains key or not?

I want to know the request.body.asFormUrlEncoded contains deviceId or not.
val formValues=request.body.asFormUrlEncoded
val number = formValues.get("mobile").head
var deviceId ="deviceIdNotFound"
if(condtion) //thats the problem
deviceId= formValues.get("deviceId").head
is there any way of conatins or any other function for Option[Map[String,Seq[String]]]
I'd strongly encourage you not to use formValues.get("whatever"), in part because the syntax is highly confusing—it looks like you're calling get with a key argument (as for example on a map), when really you're calling get on the Option (which is an unsafe operation—you should stay away from get on Option basically always) and then apply on the resulting map (also unsafe). This muddle is Scala's fault, not yours, but you still want to avoid stepping in it.
Instead you can use exists on the Option together with contains on the map. Here's a slightly simplified example:
val containsKey = formValues.exists(_.contains(key))
This will return true only if the Option is non-empty and the map it contains has the key.
An even better approach is to avoid the if-statement like this:
val os: Option[Seq[String]] = for {
m <- formValues
v <- m.get(key)
} yield v
os.foreach { v => \\ do something with the value }
Here we end up with an Option that contains the value pointed to by key if the original Option is non-empty and the map contains that key.

Parsing and looking up a string with variable number of fields java

I have to read a file and store the values and then later do a lookup.
For e.g., the file will look as follows:
Gryffindor = 5
Gryffindor.Name.Harry = 10
Gryffindor.Name.Harry.Cloak.Black = 15
and so on...
I need to store these (I was thinking of a map). Later, I need to process every character and lookup this map to assign them points. Suppose I encounter Harry, I know that he's from Gryffindor and he's wearing a blue cloak. I will have to lookup this map (or whatever object I use) as
Gryffindor.Name.Harry.Cloak.Blue
which should return me nothing. I then need to fall back to just the name and lookup
Gryffindor.Name.Harry
that should return me a 10.
Similarly, if I lookup for Ron, (suppose he's wearing black),
Gryffindor.Name.Ron.Cloak.Black
should return nothing, fall back to
Gryffindor.Name.Ron
again nothing, fall back to
Gryffindor
which should return 5.
What will be an elegant way to store and read this data? I was thinking of using a map for storing the key value pairs and then a switch case to read them back. How would you do it?
Java has a built-in Properties class that implements Map and can read and write the data format you describe (see that class's load() and store() methods).
There's nothing in there to implement your "fall back to a higher-level key" feature, so you'll need to write a method that looks in the Properties instance for data under the desired key, and keeps trying successively shorter versions of the same key if it finds nothing.

SingleColumnValueFilter has no impact on result

hy,
this question is pretty similar to SingleColumnValueFilter not returning proper number of rows .
I use four SingleColumnValueFilter's w/ operator EQUAL and add them to a FilterList with Operator MUST_PASS_ONE. the number of results is the same as w/o setting the FilterList. The value to compare is a byte[] that should be correct as I just store the values from previous results. (it is an IP address that I convert to InetAddress, new InetAddress(value as byte[]), when retrieving the data, and for the query described I just call InetAddress.getAddress which returns a byte[])
Do you have any ideas what might be the problem? Am I using the Filter wrong?
EDIT:
I also used the original values retrieved by the query as value for SingleColumnValueFilter, and there was no difference in the results, thus the byte[] contents can't be the problem.
I think I can give the answer myself, sorry for not debugging and checking all the hbase code before.
I just checked the implementation of the compare algorithm (which is lexicographically), and thus i realized that the length is not taken into account, though I thought it would be filled up w/ zero's; unfortunately it is not.
The only reasonable option would be to create a custom comparator (eg see How do you use a custom comparator with SingleColumnValueFilter on HBase?)

Categories

Resources