GolangNote

Golang笔记

golang 用bolt 存储时间戳

Permalink

golang 用bolt 存储时间戳

Go: 用bolt 存储时间戳
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
}

输出:

plaintext: output
1
2
3
4
5
6
7
8
9
10
11
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 转摘请注明来源

Related articles

Golang 数据库 Bolt 碎片整理

Bolt 是一个优秀、纯 Go 实现、支持 ACID 事务的嵌入式 Key/Value 数据库。但在使用过程中会有很多空间碎片。一般数据库占用的空间是元数据空间的 1.5~4 倍。Bolt 没有内置的压缩功能,需要手动压缩。...

Write a Comment to "golang 用bolt 存储时间戳"

Submit Comment Login
Based on Golang + fastHTTP + sdb | go1.20 Processed in 1ms