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 没有内置的压缩功能,需要手动压缩。...

Golang phantomjs 动态代理实现

phantomjs 是个很优秀的软件,虽然现在被chrome headless 抢了风头,但在某些特定场合,使用phantomjs 还是很方便,这里是介绍使用Go 实现动态代理。...

Golang WebAssembly 了解一下

Go 1.11 起开始支持 WebAssembly ,也就是说以后可以使用任何语言作为“前端语言”来进行 Web 开发。...

bolt 数据库的事务

Bolt 类似于 LMDB,这个被认为是在现代 kye/value 存储中最好的。但是又不同于 LevelDB,BoltDB 支持完全可序列化的ACID事务...

Golang http client 处理重定向网页

假设一个网址有多个重定向,A-B-C-D,使用 http.Client.Get 最后取得的内容是网址D的内容,我们该手动处理包含重定向的网址。...

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

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