Receiving reply from Redis ZRANGE - node.js

I currently have two sorted sets and I am trying to get all scores and members from one set and use it to remove members from the other set. The module that I am using is node_redis.
Right now I am trying to get the members and scores by calling client.zrange() and storing the reply in an array.
Am I correct in assuming the reply would be in array form? I realize the redis api says it returns a "Multi-bulk reply" but what does that mean exactly and how would I go about using it if it isn't an array?
I also have another question and that is can I use an array when using zadd()?
An example would be like this.
client.zadd(historyKey, scores, members, function(err, reply){});
Where scores and members are arrays.
EDIT:
I am working with receiving and parsing SNMP Traps. Basically I receive a trap and check its alarm type. The useful information in these traps are the alarm type and the full trap name. I check to see if the alarm is a 0,1, or 2.
If it's a 1, then I store it in my Current sorted set at the unix time I received it. If it's a 0 or 2 I know that type of alarm is done and that I need to remove all traps like it from the Current set and put them into the History set along with the one I just received.
In order to remove the traps from the Current and put them into the History I had to create a separate set for each individual trap in order to keep track of where they would be located in the Current set.
That is if I receive the trap "RGB Gamut Error( ----Bb )" at time 1346276537 and store it in Current, I also store the exact score and member in a separate set with key "IPAddress:RGB Gamut Error".
That way when I receive alarm type 0 or 2 with the name "RGB Gamut Error" I can just append the IP Address to the front of it, go do zrange on that set, then add to History and remove from Current. And lastly delete the "IPAddress:RGB Gamut Error" set so I can start fresh.
Sidenote: My members actually have two numbers added to the end in order to make each member unique and not overwrite each other. This is really there only purpose.
Ex: IPAdress::RGB Gamut Error( Rr--Bb ):5:46

Am I correct in assuming the reply would be in array form?
Yes, node_redis will give you the reply from a zrange as an array.
I also have another question and that is can I use an array when using zadd()? An example would be like this.
No. Before redis 2.4, you could only send one parameter at a time (so zadd key score member). Since redis 2.4, zadd (and many other commands) are variadic, i.e. they accept any number of parameters -- but not as an array. You still have to call it like this:
client.zadd(key, score1, member1, score2, member2, ..., function(err, reply){});
You could do some .apply trickery, but you would have to zip the scores and members arrays into one array first.
Update:
If you already have scores and members arrays, you could merge them into one array like this:
var scores = [1, 2, 3],
members = ['a', 'b', 'c'];
function merge (other) {
return function (result, current, index) {
result.push(current, other[index]);
return result;
}
}
var merged = scores.reduce(merge(members), []);
// Now merged = [1, 'a', 2, 'b', 3, 'c'];
var args = [key].concat(merged).concat(function(err, reply){});
client.zadd.apply(client, args);

Related

Building a std::map and issue with using std::emplace

Code:
std::map<CString, S_DISCUSSION_HIST_ITEM> mapHistory;
// History list is in ascending date order
for (auto& sHistItem : m_listDiscussionItemHist)
{
if (m_bFullHistoryMode)
mapHistory.emplace(sHistItem.strName, sHistItem);
else if (sHistItem.eSchool == m_eActiveSchool)
mapHistory.emplace(sHistItem.strName, sHistItem);
}
// The map is sorted by Name (so reset by date later)
// The map has the latest assignment info for each Name now
Observation:
I now understand that std::emplace behaves like this:
The insertion only takes place if no other element in the container has a key equivalent to the one being emplaced (keys in a map container are unique).
Therefore my code is flawed. What I was hoping to acheive (in pseudo code) is:
For Each History Item
Is the name in the map?
No, so add to map with sHitItem
Yes, so replace the sHistItem with this one
End Loop
By the end of this loop iteration I want to have the most recent sHitItem, for each person. But as it is, it is only adding an entry into the map if the name does not exist.
What is the simplest way to get around this?
Use insert_or_assign method if the item is assignable. It will be assigned if it already exists. Or use [] operator followed by assignment, it will default-construct item if it does not exist.
For non-assignable types I'm afraid there's no convenient way.

Creating Node.js enum in code to match list of values in database

I have a list of valid values that I am storing in a data store. This list is about 20 items long now and will likely grow to around 100, maybe more.
I feel there are a variety of reasons it makes sense to store this in a data store rather than just storing in code. I want to be able to maintain the list and its metadata and make it accessible to other services, so it seems like a micro-service data store.
But in code, we want to make sure only values from the list are passed, and they can typically be hardcoded. So we would like to create an enum that can be used in code to ensure that valid values are passed.
I have created a simple node.js that can generate a JS file with the enum right from the data store. This could be regenerated anytime the file changes or maybe on a schedule. But sharing the enum file with any node.js applications that use it would not be trivial.
Has anyone done anything like this? Any reason why this would be a bad approach? Any feedback is welcome.
Piggy-backing off of this answer, which describes a way of creating an "enum" in JavaScript: you can grab the list of constants from your server (via an HTTP call) and then generate the enum in code, without the need for creating and loading a JavaScript source file.
Given that you have loaded your enumConstants from the back-end (here I hard-coded them):
const enumConstants = [
'FIRST',
'SECOND',
'THIRD'
];
const temp = {};
for (const constant of enumConstants) {
temp[constant] = constant;
}
const PlaceEnum = Object.freeze(temp);
console.log(PlaceEnum.FIRST);
// Or, in one line
const PlaceEnum2 = Object.freeze(enumConstants.reduce((o, c) => { o[c] = c; return o; }, {}));
console.log(PlaceEnum2.FIRST);
It is not ideal for code analysis or when using a smart editor, because the object is not explicitly defined and the editor will complain, but it will work.
Another approach is just to use an array and look for its members.
const members = ['first', 'second', 'third'...]
// then test for the members
members.indexOf('first') // 0
members.indexOf('third') // 2
members.indexOf('zero') // -1
members.indexOf('your_variable_to_test') // does it exist in the "enum"?
Any value that is >=0 will be a member of the list. -1 will not be a member. This doesn't "lock" the object like freeze (above) but I find it suffices for most of my similar scenarios.

Why does this Context.Sync not work?

Why does this code snippet not write the values back to Excel unless I un-comment the range.values=range.values line?
$('#run').click(function() {
invokeRun()
.catch(OfficeHelpers.logError);
});
function invokeRun() {
return Excel.run(function(context) {
var range = context.workbook.worksheets.getItem("Sheet1").getRange("A1:B3");
range.load('values');
return context.sync()
.then(function() {
range.values[1][1]=99;
console.log(JSON.stringify(range.values));
//range.values=range.values
return context.sync();
});
});
}
Array properties are special. I have added a page on my website to describe the topic: Reading and writing array properties.
Summarizing from there, the way that the proxy-object model works, whenever you set a property on an object, the Office.js runtime has a hook into the setter and getter, which is used to intercept the call and add the command to the queue.
Let's take an example of a regular property first. Per the above, whenever you set something like range.format.fill.color = "red", the setter for the color property intercepts the request and internally adds a command into the queue to set the range fill color to red (to be dispatched with the next context.sync)
On the other hand, if all you had was var color = range.format.fill.color
(after a load and a sync, of course), the getter would fire instead of the setter, and the color variable would get the range's current fill color.
Now, that was regular properties. Whenever you set an element of the array, you are effectively accessing the array value as a getter. From a runtime perspective, this line is no different from a slightly more verbose version:
var array = range.values;
array[r][c] = '-';
Because the getter for range.values returns a perfectly plain JS array object, accessing it and then setting its value does nothing to propagate it back to the original Range object.
If you want the values to get reflected back, the best thing is to get a reference to the array right after the sync (i.e., var array = range.values, just as above), then set the values on the array as needed, and then finally set it back to the object: range.values = array.
It means you could also modify the values array in place, and then assign the values property back to itself at the completion of the loop (range.values = range.values). However, this looks awkward, as if it’s a no-op, whereas in reality it is not. So personally, I prefer to retrieve the array at the beginning and assign it to its own variable, then do any necessary modifications, and finally set the full array back.
UPDATE to clarify the above:
To be very clear, the arrays returned by accessing the .values, .formulas, etc., ARE pure vanilla JS arrays. That's actually the crux of the problem: that in order for Office.js to return pure objects, it means that those pure objects can't be "spiked" with the ability to reflect changes.
For what it's worth, we actually have an upcoming feature that should be rolling out in a month or two, where we will be introducing an object.set syntax, as in:
range.set({
values: [[1, 2], [3, 4]],
format: {
fill: {
color: "purple"
}
}
}
This will make it more convenient to set multiple properties on the same object, but it might also make the array properties easier to deal with.

MongoDB with Node, set starting point and select finite reverse number of records

I'm using mongo as my data store for a list of messages sent. Each message has an id and what I would like to be able to do (as efficiently as possible) is return n number of results starting before a supplied id going in reverse.
So, for example, with a function call like:
getHistory(start, count)
I could supply:
getHistory("a123", 10)
Which would return 10 records prior to the record with 'id="a123"`. Trick is the ID's are GUID and so I can't just increment backward based on that.
This is what I have so far and it's not starting in the correct position:
var cursor = collection.find({id: id}).sort({timestamp: -1}).limit(10)
Not tested, but something along the following lines should work (in mongoose):
collection.find({_id: {$lt: start}}).sort({_id: -1}).limit(10);
This should find all elements before the start id, reverse the order and get the first 10. In other words the last 10 before start.

What is in the reduce function arguments in CouchDB?

I understand that the reduce function is supposed to somewhat combine the results of the map function but what exactly is passed to the reduce function?
function(keys, values){
// what's in keys?
// what's in values?
}
I tried to explore this in the Futon temporary view builder but all I got were reduce_overflow_errors. So I can't even print the keys or values arguments to try to understand what they look like.
Thanks for your help.
Edit:
My problem is the following. I'm using the temporary view builder of Futon.
I have a set of document representing text files (it's for a script I want to use to make translation of documents easier).
text_file:
id // the id of the text file is its path on the file system
I also have some documents that represent text fragments appearing in the said files, and their position in each file.
text_fragment:
id
file_id // correspond to a text_file document
position
I'd like to get for each text_file, a list of the text fragments that appear in the said file.
Update
Note on JavaScript API change: Prior to Tue, 20 May 2008 (Subversion revision r658405) the function to emit a row to the map index, was named "map". It has now been changed to "emit".
That's the reason why there is mapused instead of emitit was renamed. Sorry I corrected my code to be valid in the recent version of CouchDB.
Edit
I think what you are looking for is a has-many relationship or a join in sql db language. Here is a blog article by Christopher Lenz that describes exactly what your options are for this kind of scenario in CouchDB.
In the last part there is a technique described that you can use for the list you want.
You need a map function of the following format
function(doc) {
if (doc.type == "text_file") {
emit([doc._id, 0], doc);
} else if (doc.type == "text_fragment") {
emit([doc.file_id, 1], doc);
}
}
Now you can query the view in the following way:
my_view?startkey=["text_file_id"]&endkey;=["text_file_id", 2]
This gives you a list of the form
text_file
text_fragement_1
text_fragement_2
..
Old Answer
Directly from the CouchDB Wiki
function (key, values, rereduce) {
return sum(values);
}
Reduce functions are passed three arguments in the order key, values and rereduce
Reduce functions must handle two cases:
When rereduce is false:
key will be an array whose elements are arrays of the form [key,id], where key is a key emitted by the map function and id is that of the document from which the key was generated.
values will be an array of the values emitted for the respective elements in keys
i.e. reduce([ [key1,id1], [key2,id2], [key3,id3] ], [value1,value2,value3], false)
When rereduce is true:
key will be null
values will be an array of values returned by previous calls to the reduce function
i.e. reduce(null, [intermediate1,intermediate2,intermediate3], true)
Reduce functions should return a single value, suitable for both the value field of the final view and as a member of the values array passed to the reduce function.

Resources