-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotodb.go
More file actions
193 lines (150 loc) · 4.98 KB
/
protodb.go
File metadata and controls
193 lines (150 loc) · 4.98 KB
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
// Copyright 2021 Linka Cloud All rights reserved.
//
// 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 protodb
import (
"context"
"go.linka.cloud/protofilters/filters"
"go.linka.cloud/protodb/internal/badgerd/replication"
"go.linka.cloud/protodb/internal/client"
"go.linka.cloud/protodb/internal/db"
"go.linka.cloud/protodb/internal/protodb"
"go.linka.cloud/protodb/internal/server"
"go.linka.cloud/protodb/protodb/v1alpha1"
)
var (
Where = filters.Where
)
type DB = protodb.DB
type Tx = protodb.Tx
type Reader = protodb.Reader
type Watcher = protodb.Watcher
type Writer = protodb.Writer
type TxProvider = protodb.TxProvider
type Committer = protodb.Committer
type Registerer = protodb.Registerer
type Leader = protodb.Leader
type Locker = protodb.Locker
type Resolver = protodb.Resolver
// Deprecated: use Resolver.
type Resolverer = protodb.Resolverer
type EventType = v1alpha1.WatchEventType
const (
EventTypeEnter = v1alpha1.WatchEventEnter
EventTypeLeave = v1alpha1.WatchEventLeave
EventTypeUpdate = v1alpha1.WatchEventUpdate
)
type Event = protodb.Event
var DefaultPath = db.DefaultPath
var Open = db.Open
type Option = db.Option
var (
WithPath = db.WithPath
WithInMemory = db.WithInMemory
WithBadgerOptionsFunc = db.WithBadgerOptionsFunc
WithLogger = db.WithLogger
WithApplyDefaults = db.WithApplyDefaults
WithIgnoreProtoRegisterErrors = db.WithIgnoreProtoRegisterErrors
WithProtoRegisterErrHandler = db.WithProtoRegisterErrHandler
WithWireBreakingCheck = db.WithWireBreakingCheck
WithOnClose = db.WithOnClose
WithReplication = db.WithReplication
WithIndexCompaction = db.WithIndexCompaction
)
type GetOption = protodb.GetOption
var (
WithPaging = protodb.WithPaging
WithFilter = protodb.WithFilter
WithReadFieldMaskPaths = protodb.WithReadFieldMaskPaths
WithReadFieldMask = protodb.WithReadFieldMask
WithReverse = protodb.WithReverse
WithOne = protodb.WithOne
WithOrderBy = protodb.WithOrderBy
WithOrderByAsc = protodb.WithOrderByAsc
WithOrderByDesc = protodb.WithOrderByDesc
)
type TxOption = protodb.TxOption
var WithReadOnly = protodb.WithReadOnly
type SetOption = protodb.SetOption
var (
WithTTL = protodb.WithTTL
WithWriteFieldMaskPaths = protodb.WithWriteFieldMaskPaths
WithWriteFieldMask = protodb.WithWriteFieldMask
)
type ReplicationMode = replication.Mode
var (
ReplicationModeNone = replication.ModeNone
ReplicationModeAsync = replication.ModeAsync
ReplicationModeSync = replication.ModeSync
)
type ReplicationOption = replication.Option
var (
WithMode = replication.WithMode
WithName = replication.WithName
WithAddrs = replication.WithAddrs
WithGossipPort = replication.WithGossipPort
WithGRPCPort = replication.WithGRPCPort
WithTick = replication.WithTick
// WithEncryptionKey set a key to encrypt gossip messages
WithEncryptionKey = replication.WithEncryptionKey
WithServerCert = replication.WithServerCert
WithServerKey = replication.WithServerKey
WithClientCert = replication.WithClientCert
WithClientKey = replication.WithClientKey
WithClientCA = replication.WithClientCA
WithTLSConfig = replication.WithTLSConfig
)
type (
Paging = v1alpha1.Paging
PagingInfo = v1alpha1.PagingInfo
FilterExpr = filters.Expression
Filter = filters.FieldFilterer
OrderBy = v1alpha1.OrderBy
)
type OrderDirection = v1alpha1.OrderDirection
const (
OrderDirectionUnspecified = v1alpha1.OrderDirectionUnspecified
OrderDirectionAsc = v1alpha1.OrderDirectionAsc
OrderDirectionDesc = v1alpha1.OrderDirectionDesc
)
type Client = client.Client
var NewClient = client.NewClient
type Server = server.Server
var NewServer = server.NewServer
func WithTx(ctx context.Context, db TxProvider, fn func(ctx context.Context, tx Tx) error, opts ...TxOption) error {
tx, err := db.Tx(ctx, opts...)
if err != nil {
return err
}
defer tx.Close()
if err := fn(ctx, tx); err != nil {
return err
}
return tx.Commit(ctx)
}
func WithTx2[R any](ctx context.Context, db TxProvider, fn func(ctx context.Context, tx Tx) (R, error), opts ...TxOption) (R, error) {
var o R
tx, err := db.Tx(ctx, opts...)
if err != nil {
return o, err
}
defer tx.Close()
o, err = fn(ctx, tx)
if err != nil {
return o, err
}
if err := tx.Commit(ctx); err != nil {
return o, err
}
return o, nil
}