-
Notifications
You must be signed in to change notification settings - Fork 1
/
transaction_dml.go
98 lines (83 loc) · 2.96 KB
/
transaction_dml.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
// Copyright (C) 2022 PuzzleDB Contributors.
//
// 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 store
import (
"bytes"
"github.com/cybergarage/puzzledb-go/puzzledb/context"
"github.com/cybergarage/puzzledb-go/puzzledb/document"
"github.com/cybergarage/puzzledb-go/puzzledb/store"
"github.com/cybergarage/puzzledb-go/puzzledb/store/kv"
)
func (txn *transaction) createSchemaKey(schema string) store.Key {
colKey := document.NewKeyWith(txn.Database().Name(), schema)
return kv.NewKeyWith(kv.CollectionKeyHeader, colKey)
}
func (txn *transaction) setCollection(ctx context.Context, col store.Collection) error {
kvSchemaKey := txn.createSchemaKey(col.Name())
var encSchema bytes.Buffer
err := txn.EncodeDocument(&encSchema, col.Data())
if err != nil {
return err
}
kvObj := kv.Object{
Key: kvSchemaKey,
Value: encSchema.Bytes(),
}
return txn.kv.Set(&kvObj)
}
// CreateCollection creates a new schema.
func (txn *transaction) CreateCollection(ctx context.Context, col store.Collection) error {
ctx.StartSpan("CreateCollection")
defer ctx.FinishSpan()
return txn.setCollection(ctx, col)
}
// UpdateCollection updates the specified collection.
func (txn *transaction) UpdateCollection(ctx context.Context, col store.Collection) error {
ctx.StartSpan("UpdateCollection")
defer ctx.FinishSpan()
return txn.setCollection(ctx, col)
}
// GetCollection returns the specified schema.
func (txn *transaction) GetCollection(ctx context.Context, name string) (store.Collection, error) {
ctx.StartSpan("GetCollection")
defer ctx.FinishSpan()
kvSchemaKey := txn.createSchemaKey(name)
kvRs, err := txn.kv.GetRange(kvSchemaKey)
if err != nil {
return nil, err
}
if !kvRs.Next() {
return nil, store.NewErrSchemaNotExist(name)
}
kvObj := kvRs.Object()
obj, err := txn.DecodeDocument(bytes.NewReader(kvObj.Value))
if err != nil {
return nil, err
}
return document.NewCollectionWith(obj)
}
// RemoveCollection removes the specified schema.
func (txn *transaction) RemoveCollection(ctx context.Context, name string) error {
ctx.StartSpan("RemoveCollection")
defer ctx.FinishSpan()
kvSchemaKey := txn.createSchemaKey(name)
return txn.kv.Remove(kvSchemaKey)
}
// TruncateCollections removes all schemas.
func (txn *transaction) TruncateCollections(ctx context.Context) error {
ctx.StartSpan("TruncateCollections")
defer ctx.FinishSpan()
kvSchemaKey := kv.NewKeyWith(kv.CollectionKeyHeader, document.NewKeyWith(txn.Database().Name()))
return txn.kv.RemoveRange(kvSchemaKey)
}