This repository has been archived by the owner on Aug 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
store.go
103 lines (79 loc) · 2.61 KB
/
store.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
/*
Copyright SecureKey Technologies Inc. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package metrics
import (
"time"
"github.com/hyperledger/aries-framework-go/spi/storage"
"github.com/trustbloc/kms/pkg/metrics"
)
// StoreWrapper wrap aries store.
type StoreWrapper struct {
s storage.Store
m metricsProvider
dbType string
}
type metricsProvider interface {
DBPutTime(dbType string, duration time.Duration)
DBGetTime(dbType string, duration time.Duration)
DBGetTagsTime(dbType string, duration time.Duration)
DBGetBulkTime(dbType string, duration time.Duration)
DBQueryTime(dbType string, duration time.Duration)
DBDeleteTime(dbType string, duration time.Duration)
DBBatchTime(dbType string, duration time.Duration)
}
// NewStore return new store metrics.
func NewStore(s storage.Store, dbType string) *StoreWrapper {
return &StoreWrapper{s: s, m: metrics.Get(), dbType: dbType}
}
// Put data.
func (store *StoreWrapper) Put(key string, value []byte, tags ...storage.Tag) error {
start := time.Now()
defer func() { store.m.DBPutTime(store.dbType, time.Since(start)) }()
return store.s.Put(key, value, tags...)
}
// Get data.
func (store *StoreWrapper) Get(key string) ([]byte, error) {
start := time.Now()
defer func() { store.m.DBGetTime(store.dbType, time.Since(start)) }()
return store.s.Get(key)
}
// GetTags get tags.
func (store *StoreWrapper) GetTags(key string) ([]storage.Tag, error) {
start := time.Now()
defer func() { store.m.DBGetTagsTime(store.dbType, time.Since(start)) }()
return store.s.GetTags(key)
}
// GetBulk get bulk.
func (store *StoreWrapper) GetBulk(keys ...string) ([][]byte, error) {
start := time.Now()
defer func() { store.m.DBGetBulkTime(store.dbType, time.Since(start)) }()
return store.s.GetBulk(keys...)
}
// Query from db.
func (store *StoreWrapper) Query(expression string, options ...storage.QueryOption) (storage.Iterator, error) {
start := time.Now()
defer func() { store.m.DBQueryTime(store.dbType, time.Since(start)) }()
return store.s.Query(expression, options...)
}
// Delete data.
func (store *StoreWrapper) Delete(key string) error {
start := time.Now()
defer func() { store.m.DBDeleteTime(store.dbType, time.Since(start)) }()
return store.s.Delete(key)
}
// Batch data.
func (store *StoreWrapper) Batch(operations []storage.Operation) error {
start := time.Now()
defer func() { store.m.DBBatchTime(store.dbType, time.Since(start)) }()
return store.s.Batch(operations)
}
// Flush data.
func (store *StoreWrapper) Flush() error {
return store.s.Flush()
}
// Close store.
func (store *StoreWrapper) Close() error {
return store.s.Close()
}