# int64 support for counters

**URL:** <https://discuss.aerospike.com/t/int64-support-for-counters/1128>\
**Category:** Go Client\
**Created:** [April 2, 2015, 7:35am UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128 "2015-04-02T07:35:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![lming](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lming](https://discuss.aerospike.com/u/lming)\
**Post date:** [April 2, 2015, 7:35am UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/1 "2015-04-02T07:35:14Z")

</div>

I’d like to implement an atomic counter. Ideally I can use Client.Add(), but the method does not return the new value of the bin. So I resort to a UDF that increases a number typed bin and return it. However the interface{} returned from Client.Execute() is of type int. How can I make the returned type become int64? Or are there any better way to get int64 counters?

Here is my UDF in case it’s useful:

```
function add(topRec, intBin, intVal)
        if type(intVal) ~= "number" then
                error("input value is not a number")
        end
        if not aerospike:exists(topRec) then
                aerospike:create(topRec)
        end
        local v = topRec[intBin]
        if v == nil then
                v = intVal
        else
                if type(v) ~= "number" then
                        error("value in the bin is not a number")
                end
                v = v + intVal
        end
        topRec[intBin] = v
        aerospike:update(topRec)
        return v
end

```

---

<div class="post-metadata">

**Author:** ![Khosrow\_Afroozeh](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khosrow\_Afroozeh](https://discuss.aerospike.com/u/Khosrow_Afroozeh)\
**Post date:** [April 2, 2015, 7:53am UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/2 "2015-04-02T07:53:59Z")

</div>

There’s a much better way. You can use `Operate` command:

```nohighlight
  bin := NewBin('atomicCounter', 1) // value can be any integer
  ops := []*Operation{
    GetOp(),
    AddOp(bin),
  }

  rec, err := client.Operate(nil, key, ops...)

```

`Operate` command is atomic, and all ops are done in a single transaction.

`GetOp()` always happens after other operations regardless of the order.

As to your other question:

```nohighlight
counter := int64(rec.Bins['atomicCounter].(int))

```

`int` is an alias for `int64` on 64bit platforms.

---

<div class="post-metadata">

**Author:** ![lming](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lming](https://discuss.aerospike.com/u/lming)\
**Post date:** [April 2, 2015, 8:23am UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/3 "2015-04-02T08:23:15Z")

</div>

That’s neat. Thanks!

---

<div class="post-metadata">

**Author:** ![lming](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lming](https://discuss.aerospike.com/u/lming)\
**Post date:** [April 2, 2015, 8:31am UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/4 "2015-04-02T08:31:13Z")

</div>

Out of curious, how to get int64 from db on 32 bit platform?

---

<div class="post-metadata">

**Author:** ![Khosrow\_Afroozeh](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khosrow\_Afroozeh](https://discuss.aerospike.com/u/Khosrow_Afroozeh)\
**Post date:** [April 2, 2015, 9:01am UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/5 "2015-04-02T09:01:09Z")

</div>

The client is not supported on 32bit platforms.

---

<div class="post-metadata">

**Author:** ![lming](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lming](https://discuss.aerospike.com/u/lming)\
**Post date:** [April 2, 2015, 6:39pm UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/6 "2015-04-02T18:39:00Z")

</div>

It seems better to use int64 as strconv.ParseInt() does.

Also looks right now the db (or Go client) does not support storing/fetching uint64? I actually faced this problem and have to encode uint64 into a customized base 64 string. Or I can actually use uint64 as LDT item’s key directly?

Thanks.

---

<div class="post-metadata">

**Author:** ![Khosrow\_Afroozeh](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khosrow\_Afroozeh](https://discuss.aerospike.com/u/Khosrow_Afroozeh)\
**Post date:** [April 2, 2015, 7:55pm UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/7 "2015-04-02T19:55:48Z")

</div>

You can cast your `uint64` into an `int64`. Values bigger than `MaxInt64` will become negative. On retrieval, you can cast them back to `uint64`.

```nohighlight
  u64 := uint64(math.MaxUint64)
  i64 := int64(u64)

  key, err := NewKey(namespace, set, i64)
  client.PutBins(nil, key, NewBin("bin", i64))

  rec, err := client.Get(nil, key)
  fmt.Println(uint64(rec.Bins["bin"].(int)))

```

---

<div class="post-metadata">

**Author:** ![lming](https://avatars.discourse-cdn.com/v4/letter/l/e19adc/32.png) [@lming](https://discuss.aerospike.com/u/lming)\
**Post date:** [April 2, 2015, 8:42pm UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/8 "2015-04-02T20:42:40Z")

</div>

I see. That’s a workaround for storage purpose. But it does not work for llist item key which is used for ordering, right?

---

<div class="post-metadata">

**Author:** ![Khosrow\_Afroozeh](https://avatars.discourse-cdn.com/v4/letter/k/c6cbf5/32.png) [@Khosrow\_Afroozeh](https://discuss.aerospike.com/u/Khosrow_Afroozeh)\
**Post date:** [April 3, 2015, 6:56pm UTC](https://discuss.aerospike.com/t/int64-support-for-counters/1128/9 "2015-04-03T18:56:49Z")

</div>

Haven’t checked, but I’d assume not.
