Views with Counters


#1

Hi,

How can I get the value of a counter inside a view ?
Is it doc.value ? Obviously doc is not JSON.

Thanks.


#2

Technically, it is JSON, but that was ambiguous until the last year or two. Search for views as base64. @avsej has a blog about how to access the value when not JSON.


#3

Thanks.

I found this and that, but they are a bit overkill.
All I want is to convert a counter into a number.
Using decodeBase64(doc) returns an array that converts 20 to [50,48] which is definitely playable.
Before I dive into converting the array to a number, is there a better way ?


#4

Actually, I may have spoken incorrectly. I think it actually is directly accessible since it’s a valid number. I may need to test this. Is it coming through as JSON or base64 in the meta data? Some things changed in 3.0, but a while back I had a similar problem and filed MB-12078, where it was determined the problem was the web UI, not the view engine itself.


#5

It is a counter, not JSON.and decodeBase64(doc) does return a value, albeit an array of bytes


#6

Ah, okay, Then it’s not being interpreted as JSON, though it is legal JSON. 50 is a “2” and 48 is a “0”. If you decode those bytes as UTF-8, you’ll have your number. The internal representation for counters is as a character string, owing to some memcached legacies.


#7

I see you saw it was the string… sorry about that.


#8

So, I’ll need to convert from byte array to int by myself, as I started ?
(x[0]-48)*10+(x[0]-48)*1 ?


#9

Ah, no. The bytes are the ascii characters. You can convert the bytes to a string as covered in Sergey’s blog.


#10

I don’t want the string.
I need to convert the integer value from the counter (written as a string) to an integer so that I can use/emit it inside the view.

This is what I wrote in the map function:

var aPoints = decodeBase64(doc);
    var iPoints = 0;
    var iLen = aPoints.length;
    for (var i in aPoints)
        iPoints+=Math.pow(10,iLen-i-1)*(aPoints[i]-48);