-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
plugindb.go
156 lines (138 loc) · 3.84 KB
/
plugindb.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*
Copyright 2023 Avi Zimmerman <avi.zimmerman@gmail.com>
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 plugindb contains a SQL driver for running data queries over a Plugin
// Query stream.
package plugindb
import (
"context"
"errors"
"io"
"sync"
"github.com/google/uuid"
v1 "github.com/webmeshproj/api/v1"
"github.com/webmeshproj/webmesh/pkg/storage"
)
// Open opens a new database connection to a plugin query stream.
func Open(srv v1.Plugin_InjectQuerierServer) storage.Storage {
return &pluginDB{srv: srv}
}
type pluginDB struct {
srv v1.Plugin_InjectQuerierServer
// TODO: Add a multiplexer to allow multiple queries at once?
mu sync.Mutex
}
func (p *pluginDB) Get(ctx context.Context, key string) (string, error) {
p.mu.Lock()
defer p.mu.Unlock()
id, err := uuid.NewRandom()
if err != nil {
return "", err
}
req := &v1.PluginQuery{
Id: id.String(),
Command: v1.PluginQuery_GET,
Query: key,
}
if err := p.srv.Send(req); err != nil {
return "", err
}
resp, err := p.srv.Recv()
if err != nil {
return "", err
}
if len(resp.GetValue()) == 0 {
// This should never happen, but just in case.
return "", storage.ErrKeyNotFound
}
return resp.GetValue()[0], nil
}
// Put sets the value of a key.
func (p *pluginDB) Put(ctx context.Context, key, value string) error {
return errors.New("put not implemented")
}
// Delete removes a key.
func (p *pluginDB) Delete(ctx context.Context, key string) error {
return errors.New("delete not implemented")
}
// List returns all keys with a given prefix.
func (p *pluginDB) List(ctx context.Context, prefix string) ([]string, error) {
p.mu.Lock()
defer p.mu.Unlock()
id, err := uuid.NewRandom()
if err != nil {
return nil, err
}
req := &v1.PluginQuery{
Id: id.String(),
Command: v1.PluginQuery_LIST,
Query: prefix,
}
if err := p.srv.Send(req); err != nil {
return nil, err
}
resp, err := p.srv.Recv()
if err != nil {
return nil, err
}
return resp.GetValue(), nil
}
// IterPrefix iterates over all keys with a given prefix.
func (p *pluginDB) IterPrefix(ctx context.Context, prefix string, fn storage.PrefixIterator) error {
p.mu.Lock()
defer p.mu.Unlock()
id, err := uuid.NewRandom()
if err != nil {
return err
}
req := &v1.PluginQuery{
Id: id.String(),
Command: v1.PluginQuery_ITER,
Query: prefix,
}
if err := p.srv.Send(req); err != nil {
return err
}
for {
resp, err := p.srv.Recv()
if err != nil {
return err
}
if resp.GetError() == "EOF" {
return nil
}
if len(resp.GetValue()) == 0 {
// Should never happen, silently continue.
continue
}
if err := fn(resp.GetKey(), resp.GetValue()[0]); err != nil {
return err
}
}
}
// Snapshot returns a snapshot of the storage.
func (p *pluginDB) Snapshot(ctx context.Context) (io.Reader, error) {
return nil, errors.New("snapshot not implemented")
}
// Restore restores a snapshot of the storage.
func (p *pluginDB) Restore(ctx context.Context, r io.Reader) error {
return errors.New("restore not implemented")
}
// Subscribe will call the given function whenever a key with the given prefix is changed.
// The returned function can be called to unsubscribe.
func (p *pluginDB) Subscribe(ctx context.Context, prefix string, fn storage.SubscribeFunc) (func(), error) {
return nil, errors.New("subscribe not implemented")
}
// Close closes the storage.
func (p *pluginDB) Close() error {
return nil
}