Skip to content

Instantly share code, notes, and snippets.

@stupidbodo
Forked from khaf/gist:50809b45ba5645f9f80c
Last active August 29, 2015 14:14
Show Gist options
  • Save stupidbodo/9f887e3ec2c009a38782 to your computer and use it in GitHub Desktop.
Save stupidbodo/9f887e3ec2c009a38782 to your computer and use it in GitHub Desktop.
Aerospike Golang Scan
package main
import (
"log"
"math"
"runtime"
"time"
as "github.com/aerospike/aerospike-client-go"
)
const (
HOSTNAME = "127.0.0.1"
PORT = 3000
NAMESPACE = "test"
SET = ""
)
func panicOnError(err error) {
if err != nil {
log.Fatalln(err)
}
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
client, err := as.NewClient(HOSTNAME, PORT)
panicOnError(err)
count := 0
for i := 1; i < math.MaxInt16; i *= 2 {
for j := count; j < i; j++ {
key, _ := as.NewKey(NAMESPACE, SET, j)
err := client.PutBins(nil, key, as.NewBin("bin", j))
panicOnError(err)
count++
}
log.Printf("There are now %d records in the database...\n", i)
for j := 0; j < 3; j++ {
t := time.Now()
scan(client, i)
log.Printf("Scanning %d records took: %v\n", i, time.Now().Sub(t))
}
}
}
// scans the database and checks if the number of returned records
// is the same as the expected number
func scan(client *as.Client, expectedRecordsCount int) {
recordset, err := client.ScanAll(nil, NAMESPACE, SET)
panicOnError(err)
count := 0
L:
for {
select {
case rec := <-recordset.Records:
if rec == nil {
break L
}
count++
case err := <-recordset.Errors:
panicOnError(err)
}
}
log.Println("Number of records returned by ScanAll:", count)
if expectedRecordsCount != count {
log.Fatalf("Expected ScanAll to return %d records, but it returned %d.\n", expectedRecordsCount, count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment