-
Notifications
You must be signed in to change notification settings - Fork 2
/
dsindex.go
175 lines (150 loc) · 4.32 KB
/
dsindex.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
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
package appwrap
import (
"context"
"fmt"
"reflect"
admin "cloud.google.com/go/datastore/admin/apiv1"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
dsadmin "google.golang.org/genproto/googleapis/datastore/admin/v1"
"gopkg.in/yaml.v2"
)
type Properties struct {
Name string
Direction string
}
type indexYaml struct {
Indexes []struct {
Kind string
Ancestor bool
Properties []Properties
}
}
type fieldIndex struct {
descending bool
index int
}
func (fi fieldIndex) String() string {
prefix := ""
if fi.descending {
prefix = "-"
}
return fmt.Sprintf("%s[%d]", prefix, fi.index)
}
type entityIndex struct {
ancestor bool
fields map[string]fieldIndex
}
func (ei entityIndex) String() string {
s := ""
for fieldName, field := range ei.fields {
if len(s) > 0 {
s += " "
}
s += field.String() + fieldName
}
return s
}
type DatastoreIndex map[string][]entityIndex
type datastoreAdminClient struct {
adapter datastoreAdminAdapter
}
type datastoreAdminAdapter interface {
withEachIndexFrom(*dsadmin.ListIndexesRequest, func(index *dsadmin.Index)) error
}
type datastoreAdminAdapterImpl struct {
client *admin.DatastoreAdminClient
ctx context.Context
}
func (d datastoreAdminAdapterImpl) withEachIndexFrom(request *dsadmin.ListIndexesRequest, f func(index *dsadmin.Index)) error {
it := d.client.ListIndexes(d.ctx, request)
for {
item, err := it.Next()
if err == iterator.Done {
break
} else if err != nil {
return fmt.Errorf("error listing indexes: %s", err.Error())
}
f(item)
}
return nil
}
func NewDatastoreAdminClient(ctx context.Context, opts ...option.ClientOption) datastoreAdminClient {
c, err := admin.NewDatastoreAdminClient(ctx, opts...)
if err != nil {
panic(fmt.Sprintf("Error creating DatastoreAdminClient: %s", err))
}
dac := datastoreAdminClient{
adapter: &datastoreAdminAdapterImpl{client: c, ctx: ctx},
}
return dac
}
func (c datastoreAdminClient) GetReadyDatastoreIndex(project string) (DatastoreIndex, error) {
req := &dsadmin.ListIndexesRequest{
ProjectId: project,
Filter: "state=READY",
}
index := make(DatastoreIndex, 0)
err := c.adapter.withEachIndexFrom(req, func(item *dsadmin.Index) {
entIndex := entityIndex{ancestor: item.Ancestor == dsadmin.Index_ALL_ANCESTORS, fields: make(map[string]fieldIndex, len(item.Properties))}
for i, prop := range item.Properties {
entIndex.fields[prop.Name] = fieldIndex{
descending: prop.Direction == dsadmin.Index_DESCENDING,
index: i,
}
}
index[item.Kind] = append(index[item.Kind], entIndex)
})
if err != nil {
return DatastoreIndex{}, err
}
return index, nil
}
func LoadIndexYaml(data []byte) (DatastoreIndex, error) {
var indexConfig indexYaml
if err := yaml.Unmarshal(data, &indexConfig); err != nil {
return nil, err
}
index := make(DatastoreIndex, len(indexConfig.Indexes))
for _, spec := range indexConfig.Indexes {
if spec.Kind == "" {
return nil, fmt.Errorf("missing entity kind")
} else if len(spec.Properties) < 2 && !spec.Ancestor {
if len(spec.Properties) == 1 && spec.Properties[0].Name == "__key__" && spec.Properties[0].Direction == "desc" {
// this is okay
} else {
return nil, fmt.Errorf("< 2 properties for index for kind " + spec.Kind)
}
}
entIndex := entityIndex{ancestor: spec.Ancestor, fields: make(map[string]fieldIndex, len(spec.Properties))}
for i, prop := range spec.Properties {
if prop.Name == "" {
return nil, fmt.Errorf("missing field name for kind " + spec.Kind)
} else if prop.Direction != "" && prop.Direction != "desc" && prop.Direction != "asc" {
return nil, fmt.Errorf(`unknown direction "%s" for kind %s`, prop.Direction, spec.Kind)
} else {
entIndex.fields[prop.Name] = fieldIndex{
descending: prop.Direction == "desc",
index: i,
}
}
}
index[spec.Kind] = append(index[spec.Kind], entIndex)
}
return index, nil
}
func IndexIntersection(d1 DatastoreIndex, d2 DatastoreIndex) DatastoreIndex {
intersection := make(DatastoreIndex, len(d1))
for d1Entity, d1Indexes := range d1 {
if d2Indexes, ok := d2[d1Entity]; ok {
for _, d1Index := range d1Indexes {
for _, d2Index := range d2Indexes {
if reflect.DeepEqual(d2Index, d1Index) {
intersection[d1Entity] = append(intersection[d1Entity], d1Index)
}
}
}
}
}
return intersection
}