Golang笔记

golang 用bolt 存储时间戳

golang 用bolt 存储时间戳

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, nil)
	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
}

输出:

writing 3 key/value pairs to 'bob'
writing 1 key/value pairs to 'susan'
committed data

reading data for 'bob':
• 2000-01-01T00:00:00Z » aaa
• 2000-02-01T00:00:00Z » bbb
• 2000-03-01T00:00:00Z » ccc

reading data for 'susan':
• 2000-01-01T00:00:00Z » xxx
本文网址: https://golangnote.com/topic/144.html (转载注明出处)
关于GolangNote:记录在工作中使用golang 遇到、面临的相关问题及解决方法。如果你在这里获得一些知识或信息,解决你的编程问题,请考虑捐赠给不幸的人或者你喜欢的慈善机构,除捐赠外,种植树木、志愿服务或减少排碳的行为也很有益处。如果你有任何问题可以在下面 留言
Be the first to comment!
Captcha image
Relative Articles