foundationdb icon indicating copy to clipboard operation
foundationdb copied to clipboard

Need more examples on go api

Open sunface opened this issue 8 years ago • 8 comments

need some go examples

see the code comments below

package main

import (
	"fmt"
	"log"

	"github.com/apple/foundationdb/bindings/go/src/fdb"
	"github.com/apple/foundationdb/bindings/go/src/fdb/directory"
	"github.com/apple/foundationdb/bindings/go/src/fdb/tuple"
)

func main() {
	fdb.MustAPIVersion(510)
	db := fdb.MustOpenDefault()

	nmqDir, err := directory.CreateOrOpen(db, []string{"nmqtest"}, nil)
	if err != nil {
		log.Fatal(err)
	}

	testApp := nmqDir.Sub("testapp")
	testTopic := "testtopic"

	// store messages
	_, err = db.Transact(func(tr fdb.Transaction) (ret interface{}, err error) {
		for i := 1; i < 70; i++ {
			SCKey := testApp.Pack(tuple.Tuple{testTopic, fmt.Sprintf("%02d", i), fmt.Sprintf("%02d", i-1)})
			tr.Set(SCKey, []byte("11111"))
		}
		return
	})

	// query messages
	var keys []string
	_, err = db.Transact(func(tr fdb.Transaction) (ret interface{}, err error) {
                // I want to query 5 messages from the testTopic from a specify position
                // so, is there any solutions for this, may like the python code:
                // tr.get_range(begin, fdb.KeySelector.first_greater_than(begin) + 5)
		pr, _ := fdb.PrefixRange([]byte("testapp"))
		pr.Begin = testApp.Pack(tuple.Tuple{testTopic, fmt.Sprintf("%02d", 4)})
		pr.End = testApp.Pack(tuple.Tuple{testTopic, fmt.Sprintf("%02d", 69)})
		ir := tr.GetRange(pr, fdb.RangeOptions{Limit: 5}).Iterator()
		for ir.Advance() {
			k := ir.MustGet().Key
			keys = append(keys, string(k))
		}
		return
	})

	fmt.Println(keys)

       // clear all messages
	_, err = db.Transact(func(tr fdb.Transaction) (ret interface{}, err error) {
		tr.ClearRange(testApp.Sub(testTopic))
		return
	})
}

so, please add more examples of tuples range and offset ,thans very much : )

sunface avatar Apr 21 '18 07:04 sunface

+1

ezeev avatar Apr 25 '18 04:04 ezeev

+1

congim avatar Apr 27 '18 03:04 congim

I will try to contribute some worked examples of different use cases, having just spent a few hours going through the Go API this evening. No ETA though, so if someone else gets there first, then great! :-)

jen20 avatar Apr 27 '18 03:04 jen20

+1

GavinYang-LR avatar Apr 27 '18 04:04 GavinYang-LR

would appreciate a watch sample. I have a fairly nice one, but it only runs for few dozen watches and then bombs with FoundationDB error code 1009 (Request for future version).

rsrsps avatar Aug 09 '18 01:08 rsrsps

One thing that wasn't clear to me when starting out with FDB was that transaction timeouts are on a per-transaction basis. The docs don't set them and I think they should. Here's an example of checking whether or not a key exists w/ timeouts:

var exists bool 
keyToFind := "testing-key"

ret, err := db.Transact(func(tr fdb.Transaction) (interface{}, error) {
    tr.Options().SetTimeout(2000) // Set a 2 second timeout (you may want less)

    if tr.Get(fdb.Key(keyToFind)).MustGet() != nil {
         return true, nil
    }
    return false, nil
})

if err != nil {
    log.Printf("Error checking FDB for key: %v", err)
}

exists = ret.(bool)

dkoston avatar Feb 07 '19 16:02 dkoston

The docs don't set them and I think they should.

See also https://github.com/apple/foundationdb/issues/882

ajbeamon avatar Feb 07 '19 16:02 ajbeamon

The docs don't set them and I think they should.

See also #882

Thanks AJ. Added a pull request to add them to the docs (at least for python and golang)

dkoston avatar Feb 07 '19 16:02 dkoston