summaryrefslogtreecommitdiff
path: root/plugins/kv/rpc.go
blob: 3f7ba97cc43070861ab52f7445acc787bbdbbcf9 (plain)
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package kv

import (
	"github.com/spiral/errors"
	"github.com/spiral/roadrunner/v2/plugins/logger"
	kvv1 "github.com/spiral/roadrunner/v2/proto/kv/v1beta"
)

// Wrapper for the plugin
type rpc struct {
	// all available storages
	storages map[string]Storage
	// svc is a plugin implementing Storage interface
	srv *Plugin
	// Logger
	log logger.Logger
}

// Has accept []*kvv1.Payload proto payload with Storage and Item
func (r *rpc) Has(in *kvv1.Request, out *kvv1.Response) error {
	const op = errors.Op("rpc_has")

	if in.GetStorage() == "" {
		return errors.E(op, errors.Str("no storage provided"))
	}

	keys := make([]string, 0, len(in.GetItems()))

	for i := 0; i < len(in.GetItems()); i++ {
		keys = append(keys, in.Items[i].Key)
	}

	if st, ok := r.storages[in.GetStorage()]; ok {
		ret, err := st.Has(keys...)
		if err != nil {
			return errors.E(op, err)
		}

		// update the value in the pointer
		// save the result
		out.Items = make([]*kvv1.Item, 0, len(ret))
		for k := range ret {
			out.Items = append(out.Items, &kvv1.Item{
				Key: k,
			})
		}
		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}

// Set accept proto payload with Storage and Item
func (r *rpc) Set(in *kvv1.Request, _ *kvv1.Response) error {
	const op = errors.Op("rpc_set")

	if st, exists := r.storages[in.GetStorage()]; exists {
		err := st.Set(in.GetItems()...)
		if err != nil {
			return errors.E(op, err)
		}

		// save the result
		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}

// MGet accept proto payload with Storage and Item
func (r *rpc) MGet(in *kvv1.Request, out *kvv1.Response) error {
	const op = errors.Op("rpc_mget")

	keys := make([]string, 0, len(in.GetItems()))

	for i := 0; i < len(in.GetItems()); i++ {
		keys = append(keys, in.Items[i].Key)
	}

	if st, exists := r.storages[in.GetStorage()]; exists {
		ret, err := st.MGet(keys...)
		if err != nil {
			return errors.E(op, err)
		}

		out.Items = make([]*kvv1.Item, 0, len(ret))
		for k := range ret {
			out.Items = append(out.Items, &kvv1.Item{
				Key:   k,
				Value: ret[k],
			})
		}
		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}

// MExpire accept proto payload with Storage and Item
func (r *rpc) MExpire(in *kvv1.Request, _ *kvv1.Response) error {
	const op = errors.Op("rpc_mexpire")

	if st, exists := r.storages[in.GetStorage()]; exists {
		err := st.MExpire(in.GetItems()...)
		if err != nil {
			return errors.E(op, err)
		}

		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}

// TTL accept proto payload with Storage and Item
func (r *rpc) TTL(in *kvv1.Request, out *kvv1.Response) error {
	const op = errors.Op("rpc_ttl")
	keys := make([]string, 0, len(in.GetItems()))

	for i := 0; i < len(in.GetItems()); i++ {
		keys = append(keys, in.Items[i].Key)
	}

	if st, exists := r.storages[in.GetStorage()]; exists {
		ret, err := st.TTL(keys...)
		if err != nil {
			return errors.E(op, err)
		}

		out.Items = make([]*kvv1.Item, 0, len(ret))
		for k := range ret {
			out.Items = append(out.Items, &kvv1.Item{
				Key:     k,
				Timeout: ret[k],
			})
		}

		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}

// Delete accept proto payload with Storage and Item
func (r *rpc) Delete(in *kvv1.Request, _ *kvv1.Response) error {
	const op = errors.Op("rcp_delete")

	keys := make([]string, 0, len(in.GetItems()))

	for i := 0; i < len(in.GetItems()); i++ {
		keys = append(keys, in.Items[i].Key)
	}
	if st, exists := r.storages[in.GetStorage()]; exists {
		err := st.Delete(keys...)
		if err != nil {
			return errors.E(op, err)
		}

		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}

// Clear clean the storage
func (r *rpc) Clear(in *kvv1.Request, _ *kvv1.Response) error {
	const op = errors.Op("rcp_delete")

	if st, exists := r.storages[in.GetStorage()]; exists {
		err := st.Clear()
		if err != nil {
			return errors.E(op, err)
		}

		return nil
	}

	return errors.E(op, errors.Errorf("no such storage: %s", in.GetStorage()))
}