I am using redis to store a list of items in a set. I add a very similar list to the set periodically, and obviously, the sorted set only adds new items if they don't already exist. Is there a way to get that list of new items that were just added? The ones that didn't already exist in the set?
Danke schoen.
ZADD command, if called with one score/member pair, returns 1 if member was new and 0 if it already existed. You can use this. Add elements one by one and check return values.
In addition to iterating over your new list as described by Sergio, you could store the keys from your sorted set in a set, store your new list in a set and do an sdiff or sdiffstore on them to get the difference.
Which route will be better is a judgement call for you based in your code and data set. If the new list is short if probably go with simple iteration using the zadd command like Sergio describes. Otherwise I'd test to see which is better for my use case.
Related
I am using multiple redis sets, hashes and Sorted sets for my use-case.
Suppose I am having a HASH set which stores ID and its corresponding object. (Project ID and its Content)
I have sets which contain list of ID's (List of ProjectIDs)
I have sorted sets which will sortBy DateTime fields and other Integer scores.
(Sort by DeadLine, Created etc and also by Project Name).
I also created a Sorted Set as my use-case needs Sort By Name (say Project Name). I created Project Name Sorted Set (ProjectName:ID as value and 0 as score).
So my requirement is that I need to sort my set (which contain ID's) based on Project Name in DESC or ASC.
How to achieve this??
Read the documentation about SORT - it should be something like SORT nameofzset BY nosort GET nameofhash->*. Even better, learn how to write Lua scripts and execute them with EVAL.
I use sorted set data type in Redis.
I add data with command zadd. Adding data is JSON format.
How I can change value in this sorted set by score?
I need get JSON value and change one field and after update this sorted set.
I tried to add againg data with the same score, but I get dublicates
It's simple!
ZREM key data;
ZADD key score newdata;
You simply cannot UPDATE an element in SET structure. It's not possible by definition! Like I cannot EAT a cup of water, I can only DRINK it ^_^
Reply if you have any further problems.
By the way, I don't know your application need, but I have a strong feeling that SORTED SET is unsuitable for your application senario.
One does not update a set's (sorted or not) members. You'll have to remove the old member and add the new (updated JSON) in its place with the relevant score. You could wrap this in Lua or a WATCH/MULTI/EXEC block for atomicity.
You could remove first by score if you're using the list as a key (score) => value store.
ZREMRANGEBYSCORE key score score
ZADD key score data
Is it possible to get a field's history (if it exists) for a field in an array or something of that sort in selenium? For example, user id field, I can see all IDs that have been used so far.
The purpose I'd like to use this is quickly create new IDs that haven't been used before. For example testID45 is already taken, so I'll use testID46 to create a new one. It's a lazy way to fill out a form without keeping track of the taken IDs.
I don't fully understand why you want to create IDs using Selenium. If you would post more info on what problem you are trying to solve, I could try to provide a better answer.
If you want to pull the IDs from existing elements you could do something like this. This finds all INPUT elements that have an ID specified and writes out the IDs. You could parse the IDs and then determine which ID to use next. I wouldn't recommend this because it would be faster to just generate a new ID that will be unique but maybe you need this for some reason.
List<WebElement> ids = driver.findElements(By.cssSelector("input[id]"));
for (WebElement id : ids)
{
System.out.println(id.getAttribute("id"));
}
I would recommend generating a new ID of your own format that would be unique on the page. This should be good enough for your purposes.
Random rnd = new Random();
String id = Long.toHexString(rnd.nextLong());
System.out.println("testID-" + id); // e.g. testID-cb8e7bac29ec7c7a
There are many other methods of generating strings in this post that you can reference also.
I want to generically access the old attributes that have changed in a model - that is, I want to get a hash of the old attributes values. My code is interested in all attributes that have changed, which may be a different set each time it's run.
I know you can get an array of changed attribute names with
model.changed
and I know you can do
model.attribute_was
to get the old value of an attribute if you know the name, but I can't find a way to programatically combine the two or to otherwise get the set of old values
I'm using it to create news stories about objects, eg
User 'Bob' changed x from a to b
You can use the attributes hash to generate this array:
old = model.changed.map{|attr| model.send("#{attr}_was".to_sym) }
Under one UnitOfWork (session) I may call CreateCriteria twice. My first call is to populate a grid for data editing. Let's say the data has been edited and flushed (saved) to the database and the grid is still open. After the data is edited, I may call CreateCriteria a second time to retrieve a list of objects that are validated and found in error.
Lets say ObjectA was retrieved by both calls to session.CreateCriteria. It was edited in the grid but found in error within the second list.
The first question would be: Considering first level cache, is ObjectA--that was retrieved from the second call to CreateCriteri--represent the one retrieved from the first call? or, better yet, did NHibernate "detect and reuse" ObjectA from the first call assuming the keys did not change?
To my final point in question: I want to edit ObjectA which was found in error, and let's say it was brought up in a ListBox. Therefore, I want to highlight that object, call session.Get()(key) in order to retrieve it from cache, then bring up a change form to change ObjectA's properties. Which object am I changing? The one from the first call to CreateCriteria or the second call? Are they the same?
Thank you in advance.
Second level cache
Take a look at http://ayende.com/Blog/archive/2006/07/24/DeepDivingIntoNHibernateTheSecondLevelCache.aspx and http://www.javalobby.org/java/forums/t48846.html
From the former:
The second level cache does not hold
entities, but collections of values
So, with caching setup properly, NHibernate will be able to recreate your object without having to get the actual values from the database. In other words, the object will be created the same as when it wasn't in cache, except that since the values are cached, NHibernate won't actually query the database since it already knows what's in there.
I'm not quite sure what you mean by "validation" and "found in error". Are you validating before insert? Typically my entities are validated before the insert/update and won't actually be inserted/updated if invalid.
Validation aside, what I think you're asking is that if you:
save something
do a flush
retrieve an item (from a new session) with the same key as the one saved in step 1
will you be retrieving the same reference to the object you saved in step 1(?). And the answer is no since NHibernate does not cache the OBJECT but rather the values so it can create a new entity populated with the cached values (instead of actually performing a DB query).
However, does that really matter? If you overload Equals such that equality of 2 entities are based on their ID, then finding the same (not reference equal, but same) item in a grid (or a hash of any kind) should be a snap.
First level cache
I didn't realize you were talking about 1st level cache. 1st level cache works as an identity map and does cache the instance of the object. Therefore, if you do 2 selects from the db based on the same ID, you will retrieve the same instance of the object.