-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathcollection_keys.go
61 lines (54 loc) · 1.75 KB
/
collection_keys.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
// Copyright 2022 Democratized Data Foundation
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package base
import (
"fmt"
"github.com/sourcenetwork/defradb/client"
"github.com/sourcenetwork/defradb/core"
"github.com/sourcenetwork/defradb/errors"
)
// MakeIndexPrefix generates a key prefix for the given collection/index descriptions
func MakeCollectionKey(col client.CollectionDescription) core.DataStoreKey {
return core.DataStoreKey{
CollectionId: col.IDString(),
}
}
// MakeIndexKey generates a key for the target dockey, using the collection/index description
func MakeDocKey(col client.CollectionDescription, docKey string) core.DataStoreKey {
return core.DataStoreKey{
CollectionId: col.IDString(),
DocKey: docKey,
}
}
func MakePrimaryIndexKeyForCRDT(
c client.CollectionDescription,
ctype client.CType,
key core.DataStoreKey,
fieldName string,
) (core.DataStoreKey, error) {
switch ctype {
case client.COMPOSITE:
return MakeCollectionKey(c).WithInstanceInfo(key).WithFieldId(core.COMPOSITE_NAMESPACE), nil
case client.LWW_REGISTER:
fieldKey := getFieldKey(c, key, fieldName)
return MakeCollectionKey(c).WithInstanceInfo(fieldKey), nil
}
return core.DataStoreKey{}, errors.New("invalid CRDT type")
}
func getFieldKey(
c client.CollectionDescription,
key core.DataStoreKey,
fieldName string,
) core.DataStoreKey {
if !c.Schema.IsEmpty() {
return key.WithFieldId(fmt.Sprint(c.Schema.GetFieldKey(fieldName)))
}
return key.WithFieldId(fieldName)
}