Skip to content

Instantly share code, notes, and snippets.

@ego008
Forked from benbjohnson/main.go
Created February 28, 2017 06:51
Show Gist options
  • Save ego008/233b75bfe1570797ff11d1b3e6cec864 to your computer and use it in GitHub Desktop.
Save ego008/233b75bfe1570797ff11d1b3e6cec864 to your computer and use it in GitHub Desktop.
Bolt for timestamp data
package main
import (
"encoding/binary"
"log"
"time"
"github.com/boltdb/bolt"
)
// This example opens a Bolt database and creates multiple buckets. Each bucket
// represents a set of time series data where the key is the big endian encoded
// Unix time in seconds and the value is just some string.
func main() {
log.SetFlags(0)
// Open the database.
var db, err = bolt.Open("db", 0600)
if err != nil {
log.Fatal(err)
}
// Start a read/write transaction.
err = db.Update(func(tx *bolt.Tx) error {
log.Println("writing 3 key/value pairs to 'bob'")
// Insert values for "bob" for jan 1, mar 1, feb 1.
if err := put(tx, "bob", "2000-01-01T00:00:00Z", "aaa"); err != nil {
return err
}
if err := put(tx, "bob", "2000-03-01T00:00:00Z", "ccc"); err != nil {
return err
}
if err := put(tx, "bob", "2000-02-01T00:00:00Z", "bbb"); err != nil {
return err
}
log.Println("writing 1 key/value pairs to 'susan'")
// Insert values for "susan" for only jan 1.
if err := put(tx, "susan", "2000-01-01T00:00:00Z", "xxx"); err != nil {
return err
}
return nil
})
if err != nil {
log.Fatal("update: ", err)
}
log.Println("committed data")
log.Println("")
// Start a read transaction.
db.View(func(tx *bolt.Tx) error {
// Use a cursor to loop over bob's key/values in order.
log.Println("reading data for 'bob':")
c := tx.Bucket([]byte("bob")).Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// Extract the Unix seconds from the key and convert to time.Time.
t := time.Unix(int64(binary.BigEndian.Uint64(k)), 0).UTC()
// Print out the key/value.
log.Printf("• %s » %s", t.Format(time.RFC3339), v)
}
log.Println("")
// Use a cursor to loop over susan's key/values in order.
log.Println("reading data for 'susan':")
c = tx.Bucket([]byte("susan")).Cursor()
for k, v := c.First(); k != nil; k, v = c.Next() {
// Extract the Unix seconds from the key and convert to time.Time.
t := time.Unix(int64(binary.BigEndian.Uint64(k)), 0).UTC()
// Print out the key/value.
log.Printf("• %s » %s", t.Format(time.RFC3339), v)
}
log.Println("")
return nil
})
}
// put inserts a value at a given time for a given bucket.
func put(tx *bolt.Tx, name, timestamp, value string) error {
// Parse timestamp.
var t, err = time.Parse(time.RFC3339, timestamp)
if err != nil {
return err
}
// Encode as big endian time.
var k = make([]byte, 8)
binary.BigEndian.PutUint64(k, uint64(t.Unix()))
// Create bucket (if it doesn't exist).
b, err := tx.CreateBucketIfNotExists([]byte(name))
if err != nil {
return err
}
// Insert k/v. If another value already exists at the same time then
// it'll be overwritten.
if err := b.Put(k, []byte(value)); err != nil {
return err
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment