This commit is contained in:
Stephen McQuay 2016-07-27 22:27:25 -07:00
commit 382c395900
No known key found for this signature in database
GPG Key ID: 1ABF428F71BAFC3D

72
main.go Normal file
View File

@ -0,0 +1,72 @@
package main
import (
"bufio"
"fmt"
"log"
"os"
"github.com/boltdb/bolt"
)
const usage = "bdb <(w)rite|(r)ead> <db>"
func main() {
log.SetFlags(log.Lshortfile)
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
switch os.Args[1] {
case "write", "read", "w", "r":
default:
fmt.Fprintf(os.Stderr, "%s\n", usage)
os.Exit(1)
}
db, err := bolt.Open(os.Args[2], 0600, nil)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("queue"))
if err != nil {
return err
}
return nil
})
if err != nil {
panic(err)
}
switch os.Args[1] {
case "write", "w":
err := db.Update(func(tx *bolt.Tx) error {
s := bufio.NewScanner(os.Stdin)
b := tx.Bucket([]byte("queue"))
for s.Scan() {
err := b.Put(s.Bytes(), s.Bytes())
if err != nil {
return err
}
}
return s.Err()
})
if err != nil {
panic(err)
}
case "read", "r":
db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("queue"))
c := b.Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
fmt.Printf("key=%s, value=%s\n", k, v)
}
return nil
})
}
}