|
1
|
|
|
package storage |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"context" |
|
5
|
|
|
|
|
6
|
|
|
"github.com/Permify/permify/pkg/database" |
|
7
|
|
|
base "github.com/Permify/permify/pkg/pb/base/v1" |
|
8
|
|
|
"github.com/Permify/permify/pkg/token" |
|
9
|
|
|
) |
|
10
|
|
|
|
|
11
|
|
|
// DataReader - Interface for reading Data from the storage. |
|
12
|
|
|
type DataReader interface { |
|
13
|
|
|
// QueryRelationships reads relation tuples from the storage based on the given filter. |
|
14
|
|
|
// It returns an iterator to iterate over the tuples and any error encountered. |
|
15
|
|
|
QueryRelationships(ctx context.Context, tenantID string, filter *base.TupleFilter, snap string, pagination database.CursorPagination) (iterator *database.TupleIterator, err error) |
|
16
|
|
|
|
|
17
|
|
|
// ReadRelationships reads relation tuples from the storage based on the given filter and pagination. |
|
18
|
|
|
// It returns a collection of tuples, a continuous token indicating the position in the data set, and any error encountered. |
|
19
|
|
|
ReadRelationships(ctx context.Context, tenantID string, filter *base.TupleFilter, snap string, pagination database.Pagination) (collection *database.TupleCollection, ct database.EncodedContinuousToken, err error) |
|
20
|
|
|
|
|
21
|
|
|
// QuerySingleAttribute retrieves a single attribute from the storage based on the given filter. |
|
22
|
|
|
// It returns the retrieved attribute and any error encountered. |
|
23
|
|
|
QuerySingleAttribute(ctx context.Context, tenantID string, filter *base.AttributeFilter, snap string) (attribute *base.Attribute, err error) |
|
24
|
|
|
|
|
25
|
|
|
// QueryAttributes reads multiple attributes from the storage based on the given filter. |
|
26
|
|
|
// It returns an iterator to iterate over the attributes and any error encountered. |
|
27
|
|
|
QueryAttributes(ctx context.Context, tenantID string, filter *base.AttributeFilter, snap string, pagination database.CursorPagination) (iterator *database.AttributeIterator, err error) |
|
28
|
|
|
|
|
29
|
|
|
// ReadAttributes reads multiple attributes from the storage based on the given filter and pagination. |
|
30
|
|
|
// It returns a collection of attributes, a continuous token indicating the position in the data set, and any error encountered. |
|
31
|
|
|
ReadAttributes(ctx context.Context, tenantID string, filter *base.AttributeFilter, snap string, pagination database.Pagination) (collection *database.AttributeCollection, ct database.EncodedContinuousToken, err error) |
|
32
|
|
|
|
|
33
|
|
|
// QueryUniqueSubjectReferences reads unique subject references from the storage based on the given filter and pagination. |
|
34
|
|
|
// It returns a slice of subject reference IDs, a continuous token indicating the position in the data set, and any error encountered. |
|
35
|
|
|
QueryUniqueSubjectReferences(ctx context.Context, tenantID string, subjectReference *base.RelationReference, excluded []string, snap string, pagination database.Pagination) (ids []string, ct database.EncodedContinuousToken, err error) |
|
36
|
|
|
|
|
37
|
|
|
// HeadSnapshot reads the latest version of the snapshot from the storage for a specific tenant. |
|
38
|
|
|
// It returns the snapshot token representing the version of the snapshot and any error encountered. |
|
39
|
|
|
HeadSnapshot(ctx context.Context, tenantID string) (token.SnapToken, error) |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
type NoopDataReader struct{} |
|
43
|
|
|
|
|
44
|
|
|
func NewNoopRelationshipReader() DataReader { |
|
45
|
|
|
return &NoopDataReader{} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
func (f *NoopDataReader) QueryRelationships(_ context.Context, _ string, _ *base.TupleFilter, _ string, _ database.CursorPagination) (*database.TupleIterator, error) { |
|
49
|
|
|
return database.NewTupleIterator(), nil |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
func (f *NoopDataReader) ReadRelationships(_ context.Context, _ string, _ *base.TupleFilter, _ string, _ database.Pagination) (*database.TupleCollection, database.EncodedContinuousToken, error) { |
|
53
|
|
|
return database.NewTupleCollection(), database.NewNoopContinuousToken().Encode(), nil |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
func (f *NoopDataReader) QuerySingleAttribute(_ context.Context, _ string, _ *base.AttributeFilter, _ string) (*base.Attribute, error) { |
|
57
|
|
|
return &base.Attribute{}, nil |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
func (f *NoopDataReader) QueryAttributes(_ context.Context, _ string, _ *base.AttributeFilter, _ string, _ database.CursorPagination) (*database.AttributeIterator, error) { |
|
61
|
|
|
return database.NewAttributeIterator(), nil |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
func (f *NoopDataReader) ReadAttributes(_ context.Context, _ string, _ *base.AttributeFilter, _ string, _ database.Pagination) (*database.AttributeCollection, database.EncodedContinuousToken, error) { |
|
65
|
|
|
return database.NewAttributeCollection(), database.NewNoopContinuousToken().Encode(), nil |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
func (f *NoopDataReader) QueryUniqueSubjectReferences(_ context.Context, _ string, _ *base.RelationReference, _ []string, _ string, _ database.Pagination) ([]string, database.EncodedContinuousToken, error) { |
|
69
|
|
|
return []string{}, database.NewNoopContinuousToken().Encode(), nil |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
func (f *NoopDataReader) HeadSnapshot(_ context.Context, _ string) (token.SnapToken, error) { |
|
73
|
|
|
return token.NewNoopToken(), nil |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
type DataWriter interface { |
|
77
|
|
|
// Write inserts a new TupleCollection and AttributeCollection into the database for a specified tenant. |
|
78
|
|
|
// Returns an encoded snapshot token representing the state of the database after the write operation and any error encountered. |
|
79
|
|
|
Write(ctx context.Context, tenantID string, tupleCollection *database.TupleCollection, attributesCollection *database.AttributeCollection) (token token.EncodedSnapToken, err error) |
|
80
|
|
|
|
|
81
|
|
|
// Delete removes data from the database based on the provided tuple and attribute filters for a specified tenant. |
|
82
|
|
|
// Returns an encoded snapshot token representing the state of the database after the delete operation and any error encountered. |
|
83
|
|
|
Delete(ctx context.Context, tenantID string, tupleFilter *base.TupleFilter, attributeFilter *base.AttributeFilter) (token token.EncodedSnapToken, err error) |
|
84
|
|
|
|
|
85
|
|
|
// RunBundle executes a specified data bundle for a given tenant. |
|
86
|
|
|
// Returns an encoded snapshot token representing the state of the database after running the bundle and any error encountered. |
|
87
|
|
|
RunBundle(ctx context.Context, tenantID string, arguments map[string]string, bundle *base.DataBundle) (token token.EncodedSnapToken, err error) |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
type NoopDataWriter struct{} |
|
91
|
|
|
|
|
92
|
|
|
func NewNoopDataWriter() DataWriter { |
|
93
|
|
|
return &NoopDataWriter{} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
func (n *NoopDataWriter) Write(_ context.Context, _ string, _ *database.TupleCollection, _ *database.AttributeCollection) (token.EncodedSnapToken, error) { |
|
97
|
|
|
return token.NewNoopToken().Encode(), nil |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
func (n *NoopDataWriter) Delete(_ context.Context, _ string, _ *base.TupleFilter, _ *base.AttributeFilter) (token.EncodedSnapToken, error) { |
|
101
|
|
|
return token.NewNoopToken().Encode(), nil |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
func (n *NoopDataWriter) RunBundle(_ context.Context, _ string, _ map[string]string, _ *base.DataBundle) (token.EncodedSnapToken, error) { |
|
105
|
|
|
return nil, nil |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
// SchemaReader - Reads schema definitions from the storage. |
|
109
|
|
|
type SchemaReader interface { |
|
110
|
|
|
// ReadSchema returns the schema definition for a specific tenant and version as a structured object. |
|
111
|
|
|
ReadSchema(ctx context.Context, tenantID, version string) (schema *base.SchemaDefinition, err error) |
|
112
|
|
|
// ReadSchemaString returns the schema definition for a specific tenant and version as a string. |
|
113
|
|
|
ReadSchemaString(ctx context.Context, tenantID, version string) (definitions []string, err error) |
|
114
|
|
|
// ReadEntityDefinition reads entity config from the storage. |
|
115
|
|
|
ReadEntityDefinition(ctx context.Context, tenantID, entityName, version string) (definition *base.EntityDefinition, v string, err error) |
|
116
|
|
|
// ReadRuleDefinition reads rule config from the storage. |
|
117
|
|
|
ReadRuleDefinition(ctx context.Context, tenantID, ruleName, version string) (definition *base.RuleDefinition, v string, err error) |
|
118
|
|
|
// HeadVersion reads the latest version of the schema from the storage. |
|
119
|
|
|
HeadVersion(ctx context.Context, tenantID string) (version string, err error) |
|
120
|
|
|
// ListSchemas lists all schemas from the storage |
|
121
|
|
|
ListSchemas(ctx context.Context, tenantID string, pagination database.Pagination) (schemas []*base.SchemaList, ct database.EncodedContinuousToken, err error) |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
type NoopSchemaReader struct{} |
|
125
|
|
|
|
|
126
|
|
|
func NewNoopSchemaReader() SchemaReader { |
|
127
|
|
|
return &NoopSchemaReader{} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
func (n *NoopSchemaReader) ReadSchema(_ context.Context, _, _ string) (*base.SchemaDefinition, error) { |
|
131
|
|
|
return &base.SchemaDefinition{}, nil |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
func (n *NoopSchemaReader) ReadSchemaString(_ context.Context, _, _ string) ([]string, error) { |
|
135
|
|
|
return []string{}, nil |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
func (n *NoopSchemaReader) ReadEntityDefinition(_ context.Context, _, _, _ string) (*base.EntityDefinition, string, error) { |
|
139
|
|
|
return &base.EntityDefinition{}, "", nil |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
func (n *NoopSchemaReader) ReadRuleDefinition(_ context.Context, _, _, _ string) (*base.RuleDefinition, string, error) { |
|
143
|
|
|
return &base.RuleDefinition{}, "", nil |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
func (n *NoopSchemaReader) HeadVersion(_ context.Context, _ string) (string, error) { |
|
147
|
|
|
return "", nil |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
func (n *NoopSchemaReader) ListSchemas(_ context.Context, _ string, _ database.Pagination) (tenants []*base.SchemaList, ct database.EncodedContinuousToken, err error) { |
|
151
|
|
|
return nil, nil, nil |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
// SchemaWriter - Writes schema definitions to the storage. |
|
155
|
|
|
type SchemaWriter interface { |
|
156
|
|
|
// WriteSchema writes schema to the storage. |
|
157
|
|
|
WriteSchema(ctx context.Context, definitions []SchemaDefinition) (err error) |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
type NoopSchemaWriter struct{} |
|
161
|
|
|
|
|
162
|
|
|
func NewNoopSchemaWriter() SchemaWriter { |
|
163
|
|
|
return &NoopSchemaWriter{} |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
func (n *NoopSchemaWriter) WriteSchema(_ context.Context, _ []SchemaDefinition) error { |
|
167
|
|
|
return nil |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
// BundleReader - Reads data bundles from storage. |
|
171
|
|
|
type BundleReader interface { |
|
172
|
|
|
// Read retrieves a data bundle based on tenant ID and name. |
|
173
|
|
|
Read(ctx context.Context, tenantID, name string) (bundle *base.DataBundle, err error) |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
type NoopBundleReader struct{} |
|
177
|
|
|
|
|
178
|
|
|
func NewNoopBundleReader() BundleReader { |
|
179
|
|
|
return &NoopBundleReader{} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
func (n *NoopBundleReader) Read(_ context.Context, _, _ string) (*base.DataBundle, error) { |
|
183
|
|
|
return nil, nil |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// BundleWriter - Manages writing and deletion of data bundles. |
|
187
|
|
|
type BundleWriter interface { |
|
188
|
|
|
// Write stores bundles in storage for a tenant. |
|
189
|
|
|
Write(ctx context.Context, bundles []Bundle) (names []string, err error) |
|
190
|
|
|
|
|
191
|
|
|
// Delete removes a bundle from storage for a tenant. |
|
192
|
|
|
Delete(ctx context.Context, tenantID, name string) (err error) |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
type NoopBundleWriter struct{} |
|
196
|
|
|
|
|
197
|
|
|
func NewNoopBundleWriter() BundleWriter { |
|
198
|
|
|
return &NoopBundleWriter{} |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
func (n *NoopBundleWriter) Write(_ context.Context, _ []Bundle) (names []string, err error) { |
|
202
|
|
|
return nil, nil |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
func (n *NoopBundleWriter) Delete(_ context.Context, _, _ string) error { |
|
206
|
|
|
return nil |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
// Watcher - Watches relation tuple changes from the storage. |
|
210
|
|
|
type Watcher interface { |
|
211
|
|
|
// Watch watches relation tuple changes from the storage. |
|
212
|
|
|
Watch(ctx context.Context, tenantID, snap string) (<-chan *base.DataChanges, <-chan error) |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
type NoopWatcher struct{} |
|
216
|
|
|
|
|
217
|
|
|
func NewNoopWatcher() Watcher { |
|
218
|
|
|
return &NoopWatcher{} |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
func (n *NoopWatcher) Watch(_ context.Context, _, _ string) (<-chan *base.DataChanges, <-chan error) { |
|
222
|
|
|
// Create empty channels |
|
223
|
|
|
aclChanges := make(chan *base.DataChanges) |
|
224
|
|
|
errs := make(chan error) |
|
225
|
|
|
|
|
226
|
|
|
// Close the channels immediately |
|
227
|
|
|
close(aclChanges) |
|
228
|
|
|
close(errs) |
|
229
|
|
|
|
|
230
|
|
|
return aclChanges, errs |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
// TenantReader - Reads tenants from the storage. |
|
234
|
|
|
type TenantReader interface { |
|
235
|
|
|
// ListTenants reads tenants from the storage. |
|
236
|
|
|
ListTenants(ctx context.Context, pagination database.Pagination) (tenants []*base.Tenant, ct database.EncodedContinuousToken, err error) |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
type NoopTenantReader struct{} |
|
240
|
|
|
|
|
241
|
|
|
func NewNoopTenantReader() TenantReader { |
|
242
|
|
|
return &NoopTenantReader{} |
|
243
|
|
|
} |
|
244
|
|
|
|
|
245
|
|
|
func (n *NoopTenantReader) ListTenants(_ context.Context, _ database.Pagination) ([]*base.Tenant, database.EncodedContinuousToken, error) { |
|
246
|
|
|
return []*base.Tenant{}, database.NewNoopContinuousToken().Encode(), nil |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
// TenantWriter - Writes tenants to the storage. |
|
250
|
|
|
type TenantWriter interface { |
|
251
|
|
|
// CreateTenant writes tenant to the storage. |
|
252
|
|
|
CreateTenant(ctx context.Context, id, name string) (tenant *base.Tenant, err error) |
|
253
|
|
|
// DeleteTenant deletes tenant from the storage. |
|
254
|
|
|
DeleteTenant(ctx context.Context, tenantID string) (tenant *base.Tenant, err error) |
|
255
|
|
|
} |
|
256
|
|
|
|
|
257
|
|
|
type NoopTenantWriter struct{} |
|
258
|
|
|
|
|
259
|
|
|
func NewNoopTenantWriter() TenantWriter { |
|
260
|
|
|
return &NoopTenantWriter{} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
func (n *NoopTenantWriter) CreateTenant(_ context.Context, _, _ string) (*base.Tenant, error) { |
|
264
|
|
|
return &base.Tenant{}, nil |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
func (n *NoopTenantWriter) DeleteTenant(_ context.Context, _ string) (*base.Tenant, error) { |
|
268
|
|
|
return &base.Tenant{}, nil |
|
269
|
|
|
} |
|
270
|
|
|
|