mirror of https://github.com/tidwall/buntdb.git
Make indentation consistent
This commit is contained in:
parent
271124b361
commit
e8211f89e6
198
README.md
198
README.md
|
@ -59,20 +59,20 @@ database, use the `buntdb.Open()` function:
|
|||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"log"
|
||||
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Open the data.db file. It will be created if it doesn't exist.
|
||||
db, err := buntdb.Open("data.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
// Open the data.db file. It will be created if it doesn't exist.
|
||||
db, err := buntdb.Open("data.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
...
|
||||
...
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -94,8 +94,8 @@ A read-only transaction should be used when you don't need to make changes to th
|
|||
|
||||
```go
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
...
|
||||
return nil
|
||||
...
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -104,8 +104,8 @@ A read/write transaction is used when you need to make changes to your data. The
|
|||
|
||||
```go
|
||||
err := db.Update(func(tx *buntdb.Tx) error {
|
||||
...
|
||||
return nil
|
||||
...
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -115,8 +115,8 @@ To set a value you must open a read/write transaction:
|
|||
|
||||
```go
|
||||
err := db.Update(func(tx *buntdb.Tx) error {
|
||||
_, _, err := tx.Set("mykey", "myvalue", nil)
|
||||
return err
|
||||
_, _, err := tx.Set("mykey", "myvalue", nil)
|
||||
return err
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -125,12 +125,12 @@ To get the value:
|
|||
|
||||
```go
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
val, err := tx.Get("mykey")
|
||||
if err != nil{
|
||||
return err
|
||||
}
|
||||
fmt.Printf("value is %s\n", val)
|
||||
return nil
|
||||
val, err := tx.Get("mykey")
|
||||
if err != nil{
|
||||
return err
|
||||
}
|
||||
fmt.Printf("value is %s\n", val)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -141,10 +141,10 @@ All keys/value pairs are ordered in the database by the key. To iterate over the
|
|||
|
||||
```go
|
||||
err := db.View(func(tx *buntdb.Tx) error {
|
||||
err := tx.Ascend("", func(key, value string) bool {
|
||||
fmt.Printf("key: %s, value: %s\n", key, value)
|
||||
})
|
||||
return err
|
||||
err := tx.Ascend("", func(key, value string) bool {
|
||||
fmt.Printf("key: %s, value: %s\n", key, value)
|
||||
})
|
||||
return err
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -171,13 +171,13 @@ Now you can add various names:
|
|||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("user:0:name", "tom", nil)
|
||||
tx.Set("user:1:name", "Randi", nil)
|
||||
tx.Set("user:2:name", "jane", nil)
|
||||
tx.Set("user:4:name", "Janet", nil)
|
||||
tx.Set("user:5:name", "Paula", nil)
|
||||
tx.Set("user:6:name", "peter", nil)
|
||||
tx.Set("user:7:name", "Terri", nil)
|
||||
return nil
|
||||
tx.Set("user:1:name", "Randi", nil)
|
||||
tx.Set("user:2:name", "jane", nil)
|
||||
tx.Set("user:4:name", "Janet", nil)
|
||||
tx.Set("user:5:name", "Paula", nil)
|
||||
tx.Set("user:6:name", "peter", nil)
|
||||
tx.Set("user:7:name", "Terri", nil)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -185,10 +185,10 @@ Finally you can iterate over the index:
|
|||
|
||||
```go
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Ascend("names", func(key, val string) bool {
|
||||
fmt.Printf(buf, "%s %s\n", key, val)
|
||||
return true
|
||||
})
|
||||
tx.Ascend("names", func(key, val string) bool {
|
||||
fmt.Printf(buf, "%s %s\n", key, val)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
@ -226,23 +226,23 @@ And then add values:
|
|||
|
||||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("user:0:age", "35", nil)
|
||||
tx.Set("user:1:age", "49", nil)
|
||||
tx.Set("user:2:age", "13", nil)
|
||||
tx.Set("user:4:age", "63", nil)
|
||||
tx.Set("user:5:age", "8", nil)
|
||||
tx.Set("user:6:age", "3", nil)
|
||||
tx.Set("user:7:age", "16", nil)
|
||||
return nil
|
||||
tx.Set("user:0:age", "35", nil)
|
||||
tx.Set("user:1:age", "49", nil)
|
||||
tx.Set("user:2:age", "13", nil)
|
||||
tx.Set("user:4:age", "63", nil)
|
||||
tx.Set("user:5:age", "8", nil)
|
||||
tx.Set("user:6:age", "3", nil)
|
||||
tx.Set("user:7:age", "16", nil)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
```go
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Ascend("ages", func(key, val string) bool {
|
||||
fmt.Printf(buf, "%s %s\n", key, val)
|
||||
return true
|
||||
})
|
||||
tx.Ascend("ages", func(key, val string) bool {
|
||||
fmt.Printf(buf, "%s %s\n", key, val)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
@ -273,10 +273,10 @@ To add some lon,lat points to the `fleet` index:
|
|||
|
||||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("fleet:0:pos", "[-115.567 33.532]", nil)
|
||||
tx.Set("fleet:1:pos", "[-116.671 35.735]", nil)
|
||||
tx.Set("fleet:2:pos", "[-113.902 31.234]", nil)
|
||||
return nil
|
||||
tx.Set("fleet:0:pos", "[-115.567 33.532]", nil)
|
||||
tx.Set("fleet:1:pos", "[-116.671 35.735]", nil)
|
||||
tx.Set("fleet:2:pos", "[-113.902 31.234]", nil)
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -285,8 +285,8 @@ And then you can run the `Intersects` function on the index:
|
|||
```go
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Intersects("fleet", "[-117 30],[-112 36]", func(key, val string) bool {
|
||||
...
|
||||
return true
|
||||
...
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
|
@ -352,40 +352,40 @@ For example:
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"fmt"
|
||||
|
||||
"github.com/tidwall/buntdb"
|
||||
"github.com/tidwall/buntdb"
|
||||
)
|
||||
|
||||
func main() {
|
||||
db, _ := buntdb.Open(":memory:")
|
||||
db.CreateIndex("last_name", "*", buntdb.IndexJSON("name.last"))
|
||||
db.CreateIndex("age", "*", buntdb.IndexJSON("age"))
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
|
||||
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
|
||||
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
|
||||
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
|
||||
return nil
|
||||
})
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
fmt.Println("Order by last name")
|
||||
tx.Ascend("last_name", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
fmt.Println("Order by age")
|
||||
tx.Ascend("age", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
fmt.Println("Order by age range 30-50")
|
||||
tx.AscendRange("age", `{"age":30}`, `{"age":50}`, func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
db, _ := buntdb.Open(":memory:")
|
||||
db.CreateIndex("last_name", "*", buntdb.IndexJSON("name.last"))
|
||||
db.CreateIndex("age", "*", buntdb.IndexJSON("age"))
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
|
||||
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
|
||||
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
|
||||
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
|
||||
return nil
|
||||
})
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
fmt.Println("Order by last name")
|
||||
tx.Ascend("last_name", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
fmt.Println("Order by age")
|
||||
tx.Ascend("age", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
fmt.Println("Order by age range 30-50")
|
||||
tx.AscendRange("age", `{"age":30}`, `{"age":50}`, func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
}
|
||||
```
|
||||
|
||||
|
@ -419,20 +419,20 @@ In this example we are creating a multi value index on "name.last" and "age":
|
|||
db, _ := buntdb.Open(":memory:")
|
||||
db.CreateIndex("last_name_age", "*", buntdb.IndexJSON("name.last"), buntdb.IndexJSON("age"))
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
|
||||
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
|
||||
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
|
||||
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
|
||||
tx.Set("5", `{"name":{"first":"Sam","last":"Anderson"},"age":51}`, nil)
|
||||
tx.Set("6", `{"name":{"first":"Melinda","last":"Prichard"},"age":44}`, nil)
|
||||
return nil
|
||||
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, nil)
|
||||
tx.Set("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
|
||||
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, nil)
|
||||
tx.Set("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
|
||||
tx.Set("5", `{"name":{"first":"Sam","last":"Anderson"},"age":51}`, nil)
|
||||
tx.Set("6", `{"name":{"first":"Melinda","last":"Prichard"},"age":44}`, nil)
|
||||
return nil
|
||||
})
|
||||
db.View(func(tx *buntdb.Tx) error {
|
||||
tx.Ascend("last_name_age", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
tx.Ascend("last_name_age", func(key, value string) bool {
|
||||
fmt.Printf("%s: %s\n", key, value)
|
||||
return true
|
||||
})
|
||||
return nil
|
||||
})
|
||||
|
||||
// Output:
|
||||
|
@ -449,8 +449,8 @@ Any index can be put in descending order by wrapping it's less function with `bu
|
|||
|
||||
```go
|
||||
db.CreateIndex("last_name_age", "*",
|
||||
buntdb.IndexJSON("name.last"),
|
||||
buntdb.Desc(buntdb.IndexJSON("age")))
|
||||
buntdb.IndexJSON("name.last"),
|
||||
buntdb.Desc(buntdb.IndexJSON("age")))
|
||||
```
|
||||
|
||||
This will create a multi value index where the last name is ascending and the age is descending.
|
||||
|
@ -493,7 +493,7 @@ Items can be automatically evicted by using the `SetOptions` object in the `Set`
|
|||
```go
|
||||
db.Update(func(tx *buntdb.Tx) error {
|
||||
tx.Set("mykey", "myval", &buntdb.SetOptions{Expires:true, TTL:time.Second})
|
||||
return nil
|
||||
return nil
|
||||
})
|
||||
```
|
||||
|
||||
|
|
Loading…
Reference in New Issue