/
client.go
109 lines (91 loc) · 2.87 KB
/
client.go
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
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package filestorage // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/storage/filestorage"
import (
"context"
"errors"
"time"
"go.etcd.io/bbolt"
"go.opentelemetry.io/collector/extension/experimental/storage"
)
var defaultBucket = []byte(`default`)
type fileStorageClient struct {
db *bbolt.DB
}
func newClient(filePath string, timeout time.Duration) (*fileStorageClient, error) {
options := &bbolt.Options{
Timeout: timeout,
NoSync: true,
}
db, err := bbolt.Open(filePath, 0600, options)
if err != nil {
return nil, err
}
initBucket := func(tx *bbolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(defaultBucket)
return err
}
if err := db.Update(initBucket); err != nil {
return nil, err
}
return &fileStorageClient{db}, nil
}
// Get will retrieve data from storage that corresponds to the specified key
func (c *fileStorageClient) Get(ctx context.Context, key string) ([]byte, error) {
op := storage.GetOperation(key)
err := c.Batch(ctx, op)
if err != nil {
return nil, err
}
return op.Value, nil
}
// Set will store data. The data can be retrieved using the same key
func (c *fileStorageClient) Set(ctx context.Context, key string, value []byte) error {
return c.Batch(ctx, storage.SetOperation(key, value))
}
// Delete will delete data associated with the specified key
func (c *fileStorageClient) Delete(ctx context.Context, key string) error {
return c.Batch(ctx, storage.DeleteOperation(key))
}
// Batch executes the specified operations in order. Get operation results are updated in place
func (c *fileStorageClient) Batch(_ context.Context, ops ...storage.Operation) error {
batch := func(tx *bbolt.Tx) error {
bucket := tx.Bucket(defaultBucket)
if bucket == nil {
return errors.New("storage not initialized")
}
var err error
for _, op := range ops {
switch op.Type {
case storage.Get:
op.Value = bucket.Get([]byte(op.Key))
case storage.Set:
err = bucket.Put([]byte(op.Key), op.Value)
case storage.Delete:
err = bucket.Delete([]byte(op.Key))
default:
return errors.New("wrong operation type")
}
if err != nil {
return err
}
}
return nil
}
return c.db.Update(batch)
}
// Close will close the database
func (c *fileStorageClient) Close(_ context.Context) error {
return c.db.Close()
}