Exemple #1
0
func ExampleRangeIterator() {
	_ = fdb.APIVersion(100)
	db, _ := fdb.OpenDefault()
	tr, _ := db.CreateTransaction()

	// Clear and initialize data in this transaction. In examples we do not
	// commit transactions to avoid mutating a real database.
	tr.ClearRange(fdb.KeyRange{fdb.Key(""), fdb.Key{0xFF}})
	tr.Set(fdb.Key("apple"), []byte("foo"))
	tr.Set(fdb.Key("cherry"), []byte("baz"))
	tr.Set(fdb.Key("banana"), []byte("bar"))

	rr := tr.GetRange(fdb.KeyRange{fdb.Key(""), fdb.Key{0xFF}}, fdb.RangeOptions{})
	ri := rr.Iterator()

	// Advance() will return true until the iterator is exhausted
	for ri.Advance() {
		kv, _ := ri.GetNextWithError()
		fmt.Printf("%s is %s\n", kv.Key, kv.Value)
	}

	// Output:
	// apple is foo
	// banana is bar
	// cherry is baz
}
Exemple #2
0
func ExamplePrefixRange() {
	_ = fdb.APIVersion(100)
	db, _ := fdb.OpenDefault()
	tr, _ := db.CreateTransaction()

	// Clear and initialize data in this transaction. In examples we do not
	// commit transactions to avoid mutating a real database.
	tr.ClearRange(fdb.KeyRange{fdb.Key(""), fdb.Key{0xFF}})
	tr.Set(fdb.Key("alpha"), []byte("1"))
	tr.Set(fdb.Key("alphabetA"), []byte("2"))
	tr.Set(fdb.Key("alphabetB"), []byte("3"))
	tr.Set(fdb.Key("alphabetize"), []byte("4"))
	tr.Set(fdb.Key("beta"), []byte("5"))

	// Construct the range of all keys beginning with "alphabet"
	pr, _ := fdb.PrefixRange([]byte("alphabet"))

	// Read and process the range
	kvs, _ := tr.GetRange(pr, fdb.RangeOptions{}).GetSliceWithError()
	for _, kv := range kvs {
		fmt.Printf("%s: %s\n", string(kv.Key), string(kv.Value))
	}

	// Output:
	// alphabetA: 2
	// alphabetB: 3
	// alphabetize: 4
}
Exemple #3
0
func ExampleOpenDefault() {
	var e error

	e = fdb.APIVersion(100)
	if e != nil {
		log.Fatalf("Unable to set API version (%v)\n", e)
	}

	// OpenDefault opens the database described by the platform-specific default
	// cluster file and the database name []byte("DB").
	db, e := fdb.OpenDefault()
	if e != nil {
		log.Fatalf("Unable to open default database (%v)\n", e)
	}

	_ = db
}
Exemple #4
0
func ExampleOpenDefault() {
	var e error

	e = fdb.APIVersion(200)
	if e != nil {
		fmt.Printf("Unable to set API version: %v\n", e)
		return
	}

	// OpenDefault opens the database described by the platform-specific default
	// cluster file and the database name []byte("DB").
	db, e := fdb.OpenDefault()
	if e != nil {
		fmt.Printf("Unable to open default database: %v\n", e)
		return
	}

	_ = db
}
Exemple #5
0
func ExampleTransactor() {
	_ = fdb.APIVersion(100)
	db, _ := fdb.OpenDefault()

	fmt.Printf("Calling setOne with a database:\n")
	setOne(db, []byte("foo"), []byte("bar"))
	fmt.Printf("\nCalling setMany with a database:\n")
	setMany(db, []byte("bar"), []byte("foo1"), []byte("foo2"), []byte("foo3"))

	// Output:
	// Calling setOne with a database:
	// setOne got:  fdb.Database
	//
	// Calling setMany with a database:
	// setMany got: fdb.Database
	// setOne got:  fdb.Transaction
	// setOne got:  fdb.Transaction
	// setOne got:  fdb.Transaction
}