Can I check if part of an element exists in an Array of Strings, or check if multiple elements exist in one query? So:
1) Does an element start with 'aaa:' in the array ['aaa:1', 'bab:0', 'aab:1']
2) Does the element 'aaa:1' OR 'aaa:0' exist in the array ['aaa:1', 'bab:0','aab:1']
If so, do not execute the API operation.
Is this possible? The Documentation isn't clear if UPDATE_ITEM is this robust or not.
The short answer is: NO for both
The long answer is:
Point 1) will never be possible
Point 2) can be done using the CONTAINS filter of the scan operation but... only for a single match. No "OR" stuff. However, scan is both slow and expensive and thus heavily discouraged.
UPDATE_ITEM conditions will only allow exact matches.
Related
I have a std::map like so: std::map<UINT32,USER_DEFINED_X> with a variable number N of elements. This map is part of an overall application that runs on a real time framework. The map contains elements such that it includes times for when certain activities are supposed to occur. During each frame, the map is scanned to see if any of those times match up with current time. There is one condition that needs to be checked though before processing the activities. I need to check to see if the element that is going to be processed is the first one in the list that is being processed. I am not sure how to do that. One approach I thought about using was to create another temporary map/array where I would store the element that has been processed in order, then get the order from that temporary array/map?
Does anybody know of a better way I can conduct this operation?
We have lists with fixed sizes that will get populated concurrently by different processes. Is there a way to perform this without using TRANSACTIONS?
For example, is there an atomic operation where you add an item to a list ONLY if the size of the list is smaller than X?
There is no single command, which will add an item to a list only if the list contains less than n items. You would need to wrap them into a transaction in order to make them "atomic".
The only way to implement an atomic call without transactions would be via a LUA script. Something along the lines (pseudo code):
local len = redis.call("LLEN", KEYS[1])
if len >= ARGV[1] then
return nil
end
redis.call("LPUSH", KEYS[1], ARGV[2])
return ARGV[2]
You would call this LUA script with the key-name of the list (KEYS[1]), the maximum length of the list (ARGV[1]) and the item that's supposed to be put on the list (ARGV[2]). Only if the length of the list is less than the maximum will the item be added and returned. If the list length is greater-equal to the maximum "nil" will be returned.
I'll begin my answer with a question: why are you so reluctant to use transactions to ensure atomicity? Note that Redis' design is such that it provides you with flexible building blocks that you can put together to implement all kinds of patterns. If an atomic command doesn't exist, you're encouraged to use MULTI/EXEC blocks or Lua scripts to achieve the same effect using existing primitives.
So the answer's no, Redis doesn't have an atomic command that can add an item and keep a fixed-sized list. This pattern is popularly implemented with a LTRIM or LRANGE, depending on the exact type of behavior you're looking for (e.g. what happens what trying to add an item to a "full" list, is an empty/smaller list possible and how are items always pushed before popping). While in some cases transactionality can be eschewed, most times you'll want to ensure that there are no race conditions in the list's management.
So, I have this text file (generated with Aspell) with 200 000 words in it. It's going to be used for a crabble game, to check if the word is legit. That means that, most likely, there's going to be quite a lot of checks where the word is not in there, and I was wondering what the most efficient way would be.
Checking the textfile line per line is going to take 200 000 iterations per check, so that'd be my last choice.
Getting all the words in a QList, and use the Qlist::contain() function (or QList::indexOf() since I think i'm using Qt4.8). I don't know about the efficiency of that though, and there's going to be quite a lot of memory used.
Using a hashtable. I honestly am not sure how that works, so if anyone could tell wether there are Qt data types provided, I can do some research.
Are there any other, efficient methods? Currently leaning to the QList method, seems easiest to implement :)
You can use std::unordered_set, it performs lookups via hash table.
Qt has it's own implementation of it QSet
Do not use QList or the first file traversal method, as both are orders of magnitude slower than one hashtable lookup.
Assuming the hash is good, using a hashtable will definitely be the fastest method (as it's a simple computation of the hash - since the string is probably not very long, that shouldn't take much time - typical English words are around 5 characters long).
There is an example in the QHash section of this page for how to hash a string: http://doc.qt.digia.com/qq/qq19-containers.html
Sort the list -- a one time operation: save it sorted, or sort it when starting your program -- and use a binary search. Looking up any word in 200,000 items is going to take on average 17.6 lookups, with about the four first operations only having to check a single character.
We are reading a book and we have to store every character of this book with it's count.
like : "This is to test" out should be: T4 H1 I2 S3 O1 E1
What will be the most appropriate data structure here and why? and what would be the logic here.
An array of ints would do fine here. Create an array, each index is a letter of the alphabet (you'll probably want to store upper and lower case separately for perf reasons when scanning the book). As you scan, increment the int at the array location for that letter. When you're done, print them all out.
Based on your description, you only need a simple hash of characters to their count. This is because you only have a limited set of characters that can occur in a book (even counting punctuation, accents and special characters). So a hash with a few hundred entries will suffice.
The appropriate data structure would be a std::vector (or, if you want something built-in, an array). If you're using C++0x, std::array would work nicely as well.
The logic is pretty simple -- read a character, (apparently convert to upper case), and increment the count for that item in the array/vector.
The choice of selecting a data structure not only depends on what kind of data you want to store inside the data structure but more importantly on what kind of operations you want to perform on the data.
Have a look at this excellent chart which helps to decide when to use which STL container.
Ofcourse, In your case an std::array(C+0x) or std::vector, seems to be a good choice.
I need to build a generic method in coldfusion to compare two query result sets... Any Ideas???
If you are looking to simply decide whether two queries are exactly alike, then you can do this:
if(serializeJSON(query1) eq serializeJSON(query2)) ...
This will convert both queries to strings and compare the strings.
If you're looking for more nuance, I believe Sergii's approach (convert to struct, compare keys) is probably the right approach. You could "guard" it by adding in simple checks first.... do the column lists match? Is the recordcount the same? That way, if either of those checks fail, you know that the queries can't possibly be equivalent and so it's safe to return false, thereby avoiding the performance hit of a full compare.
If I understand you correctly, you have two result sets with same structure but different datasets (like selecting with different clauses).
If this is correct, I believe that better (more efficient) way is to try to solve this task on the database level. Maybe with temporarily/cumulative tables and/or stored procedure.
Using CF it is almost definitely will need a ton of loops, which can be inappropriate for the large datasets. Though I did something like this for the small datasets using intermediate storage: converted one result set into the structure, and looped over the second query with checking the structure keys.