apple / foundationdb

FoundationDB - the open source, distributed, transactional key-value store
https://apple.github.io/foundationdb/
Apache License 2.0
14.59k stars 1.32k forks source link

Need more examples on go api #216

Open sunface opened 6 years ago

sunface commented 6 years ago

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 : )

ezeev commented 6 years ago

+1

congim commented 6 years ago

+1

jen20 commented 6 years ago

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! :-)

GavinYang-LR commented 6 years ago

+1

rsrsps commented 6 years ago

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).

dkoston commented 5 years ago

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)
ajbeamon commented 5 years ago

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

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

dkoston commented 5 years ago

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)