descending indexes

This commit is contained in:
Josh Baker 2016-08-19 11:56:43 -07:00
parent 18e872e092
commit 066fbb7901
3 changed files with 70 additions and 24 deletions

View File

@ -419,34 +419,44 @@ This is similar to a [multi column index](http://dev.mysql.com/doc/refman/5.7/en
In this example we are creating a multi value index on "name.last" and "age": In this example we are creating a multi value index on "name.last" and "age":
```go ```go
db, _ := buntdb.Open(":memory:") db, _ := buntdb.Open(":memory:")
db.CreateIndex("last_name_age", "*", buntdb.IndexJSON("name.last"), buntdb.IndexJSON("age")) db.CreateIndex("last_name_age", "*", buntdb.IndexJSON("name.last"), buntdb.IndexJSON("age"))
db.Update(func(tx *buntdb.Tx) error { db.Update(func(tx *buntdb.Tx) error {
tx.Set("1", `{"name":{"first":"Tom","last":"Johnson"},"age":38}`, 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("2", `{"name":{"first":"Janet","last":"Prichard"},"age":47}`, nil)
tx.Set("3", `{"name":{"first":"Carol","last":"Anderson"},"age":52}`, 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("4", `{"name":{"first":"Alan","last":"Cooper"},"age":28}`, nil)
tx.Set("5", `{"name":{"first":"Sam","last":"Anderson"},"age":51}`, nil) tx.Set("5", `{"name":{"first":"Sam","last":"Anderson"},"age":51}`, nil)
tx.Set("6", `{"name":{"first":"Melinda","last":"Prichard"},"age":44}`, nil) tx.Set("6", `{"name":{"first":"Melinda","last":"Prichard"},"age":44}`, nil)
return nil return nil
}) })
db.View(func(tx *buntdb.Tx) error { db.View(func(tx *buntdb.Tx) error {
tx.Ascend("last_name_age", func(key, value string) bool { tx.Ascend("last_name_age", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value) fmt.Printf("%s: %s\n", key, value)
return true return true
})
return nil
}) })
return nil
})
// Output: // Output:
// 5: {"name":{"first":"Sam","last":"Anderson"},"age":51} // 5: {"name":{"first":"Sam","last":"Anderson"},"age":51}
// 3: {"name":{"first":"Carol","last":"Anderson"},"age":52} // 3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
// 4: {"name":{"first":"Alan","last":"Cooper"},"age":28} // 4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
// 1: {"name":{"first":"Tom","last":"Johnson"},"age":38} // 1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
// 6: {"name":{"first":"Melinda","last":"Prichard"},"age":44} // 6: {"name":{"first":"Melinda","last":"Prichard"},"age":44}
// 2: {"name":{"first":"Janet","last":"Prichard"},"age":47} // 2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
``` ```
## Descending Ordered Index
Any index can be put in descending order by wrapping it's less function with `buntdb.Desc`.
```go
db.CreateIndex("last_name_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.
## Data Expiration ## Data Expiration
Items can be automatically evicted by using the `SetOptions` object in the `Set` function to set a `TTL`. Items can be automatically evicted by using the `SetOptions` object in the `Set` function to set a `TTL`.

View File

@ -1612,3 +1612,10 @@ func IndexJSONCaseSensitive(path string) func(a, b string) bool {
return gjson.Get(a, path).Less(gjson.Get(b, path), true) return gjson.Get(a, path).Less(gjson.Get(b, path), true)
} }
} }
// Desc is a helper function that changes the order of an index.
func Desc(less func(a, b string) bool) func(a, b string) bool {
return func(a, b string) bool {
return less(b, a)
}
}

View File

@ -413,6 +413,35 @@ func TestVariousTx(t *testing.T) {
t.Fatalf("should not be able to perform transactionso on a closed database.") t.Fatalf("should not be able to perform transactionso on a closed database.")
} }
} }
func ExampleDesc() {
db, _ := Open(":memory:")
db.CreateIndex("last_name_age", "*", IndexJSON("name.last"), Desc(IndexJSON("age")))
db.Update(func(tx *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
})
db.View(func(tx *Tx) error {
tx.Ascend("last_name_age", func(key, value string) bool {
fmt.Printf("%s: %s\n", key, value)
return true
})
return nil
})
// Output:
//3: {"name":{"first":"Carol","last":"Anderson"},"age":52}
//5: {"name":{"first":"Sam","last":"Anderson"},"age":51}
//4: {"name":{"first":"Alan","last":"Cooper"},"age":28}
//1: {"name":{"first":"Tom","last":"Johnson"},"age":38}
//2: {"name":{"first":"Janet","last":"Prichard"},"age":47}
//6: {"name":{"first":"Melinda","last":"Prichard"},"age":44}
}
func ExampleDB_CreateIndex_jSON() { func ExampleDB_CreateIndex_jSON() {
db, _ := Open(":memory:") db, _ := Open(":memory:")
db.CreateIndex("last_name", "*", IndexJSON("name.last")) db.CreateIndex("last_name", "*", IndexJSON("name.last"))