Created
February 24, 2016 15:48
-
-
Save conoro/2b0ffd347cdd1f55b950 to your computer and use it in GitHub Desktop.
Testing gkvlite Key Value Store in Go on Onion Omega
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/steveyen/gkvlite" | |
) | |
func main() { | |
f, err := os.Create("./gkvlite.kv") | |
if err != nil { | |
fmt.Println(err) | |
} | |
s, err := gkvlite.NewStore(f) | |
if err != nil { | |
fmt.Println(err) | |
} | |
c := s.SetCollection("cars", nil) | |
// You can also retrieve the collection, where c == cc. | |
//cc := s.GetCollection("cars") | |
//pp.Println(cc) | |
// Insert values. | |
c.Set([]byte("tesla"), []byte("$$$")) | |
c.Set([]byte("mercedes"), []byte("$$")) | |
c.Set([]byte("bmw"), []byte("$")) | |
// Replace values. | |
c.Set([]byte("tesla"), []byte("$$$$")) | |
// Retrieve values. | |
mercedesPrice, err := c.Get([]byte("mercedes")) | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println(string(mercedesPrice)) | |
// One of the most priceless vehicles is not in the collection. | |
//thisIsNil, err := c.Get([]byte("the-apollo-15-moon-buggy")) | |
//if err != nil { | |
// fmt.Println(err) | |
// } | |
// pp.Println(thisIsNil) | |
// Iterate through items. | |
c.VisitItemsAscend([]byte("ford"), true, func(i *gkvlite.Item) bool { | |
// This visitor callback will be invoked with every item | |
// with key "ford" and onwards, in key-sorted order. | |
// So: "mercedes", "tesla" are visited, in that ascending order, | |
// but not "bmw". | |
// If we want to stop visiting, return false; | |
// otherwise return true to keep visiting. | |
return true | |
}) | |
// Let's get a snapshot. | |
//snap := s.Snapshot() | |
//snapCars := snap.GetCollection("cars") | |
// The snapshot won't see modifications against the original Store. | |
//c.Delete([]byte("mercedes")) | |
//mercedesIsNil, err := c.Get([]byte("mercedes")) | |
//if err != nil { | |
// fmt.Println(err) | |
//} | |
//pp.Println(mercedesIsNil) | |
//mercedesPriceFromSnapshot, err := snapCars.Get([]byte("mercedes")) | |
//if err != nil { | |
// fmt.Println(err) | |
//} | |
//pp.Println(mercedesPriceFromSnapshot) | |
// Persist all the changes to disk. | |
s.Flush() | |
f.Sync() // Some applications may also want to fsync the underlying file. | |
// Now, other file readers can see the data, too. | |
// f2, err := os.Open("./gkvlite.kv") | |
// if err != nil { | |
// fmt.Println(err) | |
// } | |
// s2, err := gkvlite.NewStore(f2) | |
// if err != nil { | |
// fmt.Println(err) | |
// } | |
// pp.Println(s2) | |
// c2 := s.GetCollection("cars") | |
// | |
// bmwPrice, err := c2.Get([]byte("bmw")) | |
// if err != nil { | |
// fmt.Println(err) | |
// } | |
// pp.Println(bmwPrice) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment