This repository has been archived by the owner on Jul 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
tracekv.go
112 lines (93 loc) · 2.73 KB
/
tracekv.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
110
111
112
// Copyright 2014-2015 The Dename 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 tracekv implements a tracing wrapper for kv.DB
package tracekv
import (
"fmt"
"github.com/yahoo/coname/keyserver/kv"
)
type tracekv struct {
kv.DB
traceUpdate func(Update)
traceBatch func([]Update)
}
// WithTracing returns a kv.DB that calls traceUpdate on every Put or Delete
// and traceBatch on every Write.
func WithTracing(db kv.DB, traceUpdate func(Update), traceBatch func([]Update)) kv.DB {
return tracekv{db, traceUpdate, traceBatch}
}
func mapUpdate(f func(Update)) func([]Update) {
return func(ps []Update) {
for _, p := range ps {
f(p)
}
}
}
// WithSimpleTracing returns a kv.DB that calls traceUpdate on every Put
// (regardless of whether it is issued by itself or during a Batch Write).
func WithSimpleTracing(db kv.DB, traceUpdate func(Update)) kv.DB {
return WithTracing(db, traceUpdate, mapUpdate(traceUpdate))
}
func (db tracekv) Get(key []byte) ([]byte, error) {
return db.DB.Get(key)
}
func (db tracekv) Put(key, value []byte) error {
err := db.DB.Put(key, value)
db.traceUpdate(Update{key, value, false})
return err
}
func (db tracekv) Delete(key []byte) error {
err := db.DB.Delete(key)
db.traceUpdate(Update{key, nil, true})
return err
}
func (db tracekv) NewBatch() kv.Batch {
return &traceBatch{nil, db.DB.NewBatch()}
}
func (db tracekv) Write(b kv.Batch) error {
wb, ok := b.(*traceBatch)
if !ok {
return fmt.Errorf("tracekv.Write: expected *tracekv.traceBatch, got %T", b)
}
err := db.DB.Write(wb.b)
db.traceBatch(wb.operations)
return err
}
func (db tracekv) NewIterator(rg *kv.Range) kv.Iterator {
return db.DB.NewIterator(rg)
}
func (db tracekv) ErrNotFound() error {
return db.DB.ErrNotFound()
}
type traceBatch struct {
operations []Update
b kv.Batch
}
// Update represents a single change to a kv.DB.
type Update struct {
Key, Value []byte
IsDeletion bool
}
func (lb *traceBatch) Reset() {
lb.operations = nil
lb.b.Reset()
}
func (lb *traceBatch) Put(key, value []byte) {
lb.operations = append(lb.operations, Update{key, value, false})
lb.b.Put(key, value)
}
func (lb *traceBatch) Delete(key []byte) {
lb.operations = append(lb.operations, Update{key, nil, true})
lb.b.Delete(key)
}