Couchbase MapReduce performance - couchbase

We have been doing development on a new project for several months on Couchbase and while we've done a number of views, we just recently introduced our first reduce.
The reduce function is operating on an index that is less than 100 rows, yet whenever I add a new document that matches the map, the reduce data is completely destroyed and returns zeros for all values. It stays like this for quite some time, 5 - 10 minutes, sometimes longer and then eventually starts outputting expected values.
Since the index is so small, I wonder what I might have done wrong. I could make it work with the data being stale - but going to 0 makes it unworkable. I'm hoping someone can look at my functions and tell me if I've written them in a way that won't perform.
The map:
function (doc, meta) {
if(meta.type == "json") {
if(doc.object == "product") {
emit([doc.account_id, doc.test], {active: doc.active, deleted: doc.deleted} );
}
}
}
The reduce:
function (keys, values) {
var out = {active: 0, inactive: 0, deleted: 0};
for(v in values) {
if (values[v].active == true && values[v].deleted == false) {
out.active++
}
if (values[v].active == false && values[v].deleted == false) {
out.inactive++
}
if (values[v].deleted == true) {
out.deleted++
}
}
return out;
}
We are running the latest version of Community edition - 2.2.0 (build-837). It's running in a dev environment with lots of hardware resources and very little usage. As a side note, other than this little snag, Couchbase has been great.

This may not be the root cause of your problem, but you haven't handled the re-reduce case in your reduce() function - which can cause incorrect reduce results. See the Couchbase Developer Guide section on Understanding Custom Reduces and Re-reduce
I'd also suggest temporarily removing the reduce() function to verify that the index operates as expected when when just using a map() function.

Related

Immutable.js, how to quit search on first find

currently I am doing
var adnetCustomerModel = customersList.find((adnetCustomerModel) => {
return adnetCustomerModel.getId() == customerId;
})
but wasting CPU cycles as I have to continue and traverse the entire list (or so I am assuming that's what happens).
I'd like to quit on first find.
Now I know I can do a filter().first() (which I believe will have same waste of CPU cycles) but is there a better way?
If it was a normal for loop I would just break...
will the return achieve the same effect in immutable.js?
tx for reading,
Sean
Immutable’s find() already returns only the first value for which the predicate returns true. It actually just wraps around the findEntry() method that’s implemented like this:
findEntry(predicate, context, notSetValue) {
var found = notSetValue;
this.__iterate((v, k, c) => {
if (predicate.call(context, v, k, c)) {
found = [k, v];
return false;
}
});
return found;
}
So, you’re not wasting any cycles. :)
Vanilla JavaScript Array.prototype.find() also returns the value of the first element to match the predicate.

Apache2 Perl vHosts Error

I just worked through this tutorial and modified the table by adding another column. I want to check the value before adding the template script. It didn't work and the script includes the template-ssl every time. It is important that this script works with MySQL, mass vhosts is not possible.
$My::dir = #row[3];
$My::encrypted = #row[4];
if ($My::encrypted == 'ssl') {
$s->add_config(["Include /etc/apache2/sites-available/template-ssl"]);
}
else {
$s->add_config(["Include /etc/apache2/sites-available/template-def"]);
}
I think the variables doesn't work but if(#row[4] == "ssl") also fire as true every time. Even when the DataRow contains "def".
Ok, it was too simple. The error was that you compare stings with "xx" eq "yy" and numbers with 1 == 2.

[cc creator]Comparison not working

I have an array of Nodes 'flags', and I want to set my object's position at the first object in that array, it works and the object actually gets positioned as intended, but when I make the comparison it fails and logs 'NO'.
The line of code that sets the position works, but the comparison fails, what's wrong here?!
start: function () {
this.node.position = this.flags[0].position;
this.movement();
},
movement: function() {
if (this.node.position == this.flags[0].position) { // Problem
console.log("YES");
}
else {
console.log("No");
Update:
When I do it like this it works:
if (this.node.position.x == this.flags[0].position.x) // or position.y
Well if you write javascript here (and it looks like you do) there're two things you should know:
You can't compare objects with == out of the box
({"a":1} == {"a":1})
Will return false (you may try it yourself in your browser.
As a workaround you could do something like:
function posCompare(p1, p2){
return p1.x === p2.x && p1.y === p2.y;
}
Then use it instead of == for positions
See how I use === instead of ==? Second thing to know is Use only ===. You can learn the difference Which equals operator (== vs ===) should be used in JavaScript comparisons? but I'd keep away from == anywhere. It's slower, it may cause strange errors here and there - just don't use it at all

How to suppress the warning "Assignment within conditional. Did you mean == instead of =?"

With the new ASC 2.0 compiler I get warnings when I code like below:
// (_achievementsFromServer is an Array)
while(item=_achievementsFromServer.pop())
{
// do something with item here
}
The warning reads: "Assignment within conditional. Did you mean == instead of =?"
While in general I appreciate all warnings from the compiler, I'd like to suppress this one in this case because I did not mean == here. I want to pop all items in the array and do something with it until the array is empty.
while( (item=_achievementsFromServer.pop())==true )
seems to work but looks a bit confusing. Any other ideas?
This may seem better.
while(_achievementsFromServer.length > 0) {
var item:Object = _achievementsFromServer.pop();
}
Just like removeChild
var d:DisplayObjectContainer;
while(d.numChildren > 0) {
d.removeChildAt(0);
}
While I was hoping for some other way, I think #AmyBlankenship improved my own suggestion:
while((item=_achievementsFromServer.pop())!=null)
{
//....
}
It's clear and understandable what's going on, and doesn't rely on checking the length of the Array on every iteration.
Googling some more I found a compiler option -compiler.warn-assignment-within-conditional that could be set to false but then you won't be warned anywhere in your project anymore. And I'm not so confident that I never accidently type = instead of ==, so that's not a good solution I think.

Custom sorting function bottleneck

I am trying to sort big array using actionscript 3.
The problem is that i have to use custom sorting function which is painfully slow and leads to flash plugin crash.
Below is a sample code for custom function used to sort array by length of its members:
private function sortByLength():int {
var x:int = arguments[0].length;
var y:int = arguments[1].length;
if (x > y){
return 1;
}else if (x < y){
return -1;
}else{
return 0;
}
}
Which is called like this:
var txt:Array = ["abcde","ab","abc","a"];
txt.sort(sortByLength);
Please advise me how can this be done faster ?
How to change application logic to avoid Flash plugin crashes during sorting ?
try to use strong typing whenever possible, here tell your function that you are waiting two strings.
you could rewrite your function in two way one fastest than the other if you know that all your element are not null:
function sortByLength(a:String, b:String):int {
return a.length-b.length // fastest way not comparison
}
and if you can have null check for it (this one will put null in front of all element):
function sortByLengthWithNull(a:String, b:String):int {
if (a==null) return -1
if (b==null) return 1
return a.length-b.length
}
If you need super-fast sorting, then it might be worthwhile not using an array at all and instead using a linked-list. There are different advantages to each. Primarily, with a linked-list, index-access is slow, while iterating through the list is fast, and linked-lists are not native to AS3 so you'll have to roll your own.
On the upside, you may well be able to use some of Polygonal Labs' code: http://lab.polygonal.de/as3ds/.
Sorting is very, very fast for nearly-sorted data with a linked list, as this article discusses: http://lab.polygonal.de/2007/11/26/data-structures-more-on-linked-lists/.
This solution gives you lots more work, but will eventually give you lots more sort-speed too.
Hope this helps.
-- additional --
I noticed your question in the comments of another answer about "One question however is unanswered - how to perform greedy computations in Flash without hanging it?"
For this, essentially the answer is to break your computation over multiple frames, something like this:
public function sort():void
{
addEventListener(Event.ENTER_FRAME, iterateSort);
}
private function iterateSort():void
{
var time:int = getTimer() + TARGET_MILLISECONDS_PER_FRAME;
var isFinished:Boolean = false;
while (!isFinished && getTimer() < time)
isFinished = continueSort();
if (isFinished)
removeEventListener(Event.ENTER_FRAME, iterateSort);
}
function continueSort():Boolean
{
... implement an 'atom of sort' here, whatever that means ...
}
sortByLength should have two parameters, shouldn't it? I guess that's what you mean by the arguments array...
This looks fine to me, unless arguments is not a local variable, but instead a member variable, and you're just looking at its [0] and [1] elements on each function call. That would at least produce undesired results.