-
Notifications
You must be signed in to change notification settings - Fork 4
/
usecase_test.go
666 lines (560 loc) · 16.8 KB
/
usecase_test.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
package smg
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"strconv"
"testing"
"time"
"github.com/favclip/smg/smgutils"
"github.com/mjibson/goon"
"google.golang.org/appengine/aetest"
"google.golang.org/appengine/datastore"
"google.golang.org/appengine/log"
"google.golang.org/appengine/search"
)
/// user defined
type Inventory struct {
ParentKey *datastore.Key `json:"-" datastore:"-" goon:"parent" search:"-"`
ID int64 `json:"-" datastore:"-" goon:"id" json:",string" search:",id"`
ProductName string
Description string `search:",ngram"`
Stock int `search:",rank"`
AdminNames []string `search:",json"`
Shops []*Shop `search:",json"`
CreatedAt time.Time `datastore:",noindex"`
UpdatedAt time.Time `datastore:",noindex" search:"-"`
}
type Shop struct {
Name string
Address string
}
func (doc *InventorySearch) DocID(c context.Context) (string, error) {
g := goon.FromContext(c)
id, err := strconv.ParseInt(doc.ID, 10, 0)
if err != nil {
return "", err
}
key, err := g.KeyError(&Inventory{ID: id})
if err != nil {
return "", err
}
return key.Encode(), nil
}
/// code example
func TestUsecaseSample(t *testing.T) {
c, closer, err := aetest.NewContext()
if err != nil {
t.Fatal(err.Error())
}
defer closer()
namedIndexName := "RiceCakes"
func() {
var inv *Inventory
index := NewInventorySearch()
inv = &Inventory{
ID: 111,
ProductName: "カップヌードル",
Description: "お湯を入れたらすぐヌードル!",
Stock: 3,
AdminNames: []string{"Mr.A"},
Shops: []*Shop{&Shop{Name: "0-24", Address: "Hongo street"}},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err = index.Put(c, inv)
if err != nil {
t.Fatal(err)
}
inv = &Inventory{
ID: 222,
ProductName: "おせんべい",
Description: "囓ってパリッ!",
Stock: 10,
AdminNames: []string{"Mr.B"},
Shops: []*Shop{&Shop{Name: "HighSon", Address: "Hongo Bldg"}},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
_, err = index.Put(c, inv)
if err != nil {
t.Fatal(err)
}
inv = &Inventory{
ID: 333,
ProductName: "お餅",
Description: "一年中食べたい!",
Stock: 1095,
AdminNames: []string{"Mr.C"},
Shops: []*Shop{&Shop{Name: "YushimaTenmangu", Address: "Yushima"}},
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
}
namedIndex := NewInventorySearchWithIndexName(namedIndexName)
_, err = namedIndex.Put(c, inv)
if err != nil {
t.Fatal(err)
}
}()
func() {
t.Logf("#1")
index := NewInventorySearch()
// (ProductName: カップヌードル AND Shop: Hongo AND Stock >= 1) OR (ProductName: カップヌードル AND Stock > 10) OR (ProductNameBigram: カッ AND ProductNameBigram: ップ AND ProductNameBigram: プヌ AND ProductNameBigram: ヌー AND ProductNameBigram: ード AND ProductNameBigram: ドル)
index.Group(func() {
index.ProductName.Match("カップヌードル").And().Shops.Match("Hongo").And().Stock.IntGreaterThanOrEqual(1)
})
index.Or().StartGroup().ProductName.Match("カップヌードル").And().Stock.IntGreaterThan(10).EndGroup()
index.Or().Description.NgramMatch("お湯を")
index.Stock.Desc()
index.Opts().Limit(10)
iter, err := index.Search(c)
if err != nil {
t.Fatal(err)
}
for {
docID, s, err := iter.Next(c)
if err == search.Done {
t.Log("#1 done!")
break
}
if err != nil {
t.Fatal(err.Error())
}
t.Logf("%s %#v", docID, s)
}
}()
func() {
t.Logf("#2")
index := NewInventorySearch()
index.Description.NgramMatch("囓って")
index.Stock.Desc()
index.Opts().Limit(10)
iter, err := index.Search(c)
if err != nil {
t.Log("#2 done!")
t.Fatal(err)
}
for {
docID, s, err := iter.Next(c)
if err == search.Done {
break
}
if err != nil {
t.Fatal(err.Error())
}
t.Logf("%s %#v", docID, s)
}
}()
func() {
t.Logf("#3")
index := NewInventorySearch()
index.ProductName.Match("お餅")
iter, err := index.Search(c)
if err != nil {
t.Fatal(err)
}
docID, s, err := iter.Next(c)
if err == nil {
t.Fatal(fmt.Errorf("find RiceCake in other inventory"), docID, s)
} else if err != search.Done {
t.Fatal(err.Error())
}
namedIndex := NewInventorySearchWithIndexName(namedIndexName)
namedIndex.ProductName.Match("お餅")
nIter, err := namedIndex.Search(c)
if err != nil {
t.Fatal(err)
}
cnt := 0
for {
docID, s, err := nIter.Next(c)
if err == search.Done {
break
}
if err != nil {
t.Fatal(err.Error())
}
t.Logf("%s %#v", docID, s)
cnt++
}
if cnt != 1 {
t.Fatal(fmt.Errorf("unexpected RiceCake cnt %d: expected 1", cnt), docID, s)
}
t.Log("#3 done!")
}()
}
/// generated code
type InventorySearch struct {
ID string
ProductName string
Description string
DescriptionUnigram string
DescriptionBigram string
Stock float64
AdminNames string
Shops string
CreatedAt time.Time
}
func (doc *InventorySearch) Load(fields []search.Field, metadata *search.DocumentMetadata) error {
return search.LoadStruct(doc, fields)
}
func (doc *InventorySearch) Save() ([]search.Field, *search.DocumentMetadata, error) {
fields, err := search.SaveStruct(doc)
if err != nil {
return nil, nil, err
}
metadata := &search.DocumentMetadata{Rank: int(doc.Stock)}
return fields, metadata, nil
}
func (doc *Inventory) Searchfy() (*InventorySearch, error) {
if doc == nil {
return nil, nil
}
dest := &InventorySearch{}
var err error
var b []byte
dest.ID = strconv.FormatInt(doc.ID, 10)
dest.ProductName = doc.ProductName
dest.Description = doc.Description
dest.DescriptionUnigram, err = smgutils.UnigramForSearch(doc.Description)
if err != nil {
return nil, err
}
dest.DescriptionBigram, err = smgutils.BigramForSearch(doc.Description)
if err != nil {
return nil, err
}
dest.Stock = float64(doc.Stock)
b, err = json.Marshal(dest.AdminNames)
if err != nil {
return nil, err
}
if str := string(b); str != "" && str != `""` {
dest.AdminNames = str
}
b, err = json.Marshal(dest.Shops)
if err != nil {
return nil, err
}
if str := string(b); str != "" && str != `""` {
dest.Shops = str
}
dest.CreatedAt = doc.CreatedAt
return dest, nil
}
func NewInventorySearch() *InventorySearchBuilder {
var _ search.FieldLoadSaver = &InventorySearch{}
op := &smgutils.Op{}
b := &InventorySearchBuilder{
rootOp: op,
currentOp: op,
opts: &search.SearchOptions{
Sort: &search.SortOptions{},
},
}
b.ProductName = &InventorySearchStringPropertyInfo{"ProductName", b}
b.Description = &InventorySearchNgramStringPropertyInfo{InventorySearchStringPropertyInfo{"Description", b}}
b.Stock = &InventorySearchNumberPropertyInfo{"Stock", b}
b.AdminNames = &InventorySearchStringPropertyInfo{"AdminNames", b}
b.Shops = &InventorySearchStringPropertyInfo{"Shops", b}
b.CreatedAt = &InventorySearchTimePropertyInfo{"CreatedAt", b}
return b
}
func NewInventorySearchWithIndexName(name string) *InventorySearchBuilder {
b := NewInventorySearch()
b.indexName = name
return b
}
var _ smgutils.SearchBuilder = &InventorySearchBuilder{}
type InventorySearchBuilder struct {
rootOp *smgutils.Op
currentOp *smgutils.Op // groupの扱い
opts *search.SearchOptions
query string
indexName string
index *search.Index
ProductName *InventorySearchStringPropertyInfo
Description *InventorySearchNgramStringPropertyInfo
Stock *InventorySearchNumberPropertyInfo
AdminNames *InventorySearchStringPropertyInfo
Shops *InventorySearchStringPropertyInfo
CreatedAt *InventorySearchTimePropertyInfo
}
func (b *InventorySearchBuilder) IndexName() string {
if b.indexName != "" {
return b.indexName
}
return "Inventory"
}
func (b *InventorySearchBuilder) QueryString() (string, error) {
buffer := &bytes.Buffer{}
err := b.rootOp.Query(buffer)
if err != nil {
return "", err
}
return buffer.String(), nil
}
func (b *InventorySearchBuilder) SearchOptions() *search.SearchOptions {
return b.opts
}
func (b *InventorySearchBuilder) And() *InventorySearchBuilder {
b.currentOp.Children = append(b.currentOp.Children, &smgutils.Op{Type: smgutils.And})
return b
}
func (b *InventorySearchBuilder) Or() *InventorySearchBuilder {
b.currentOp.Children = append(b.currentOp.Children, &smgutils.Op{Type: smgutils.Or})
return b
}
func (b *InventorySearchBuilder) Group(p func()) *InventorySearchBuilder {
b.StartGroup()
p()
b.EndGroup()
return b
}
func (b *InventorySearchBuilder) StartGroup() *InventorySearchBuilder {
op := &smgutils.Op{Type: smgutils.Group, Parent: b.currentOp}
b.currentOp.Children = append(b.currentOp.Children, op)
b.currentOp = op
return b
}
func (b *InventorySearchBuilder) EndGroup() *InventorySearchBuilder {
b.currentOp = b.currentOp.Parent
return b
}
func (b *InventorySearchBuilder) Put(c context.Context, src *Inventory) (string, error) {
doc, err := src.Searchfy()
if err != nil {
return "", err
}
return b.PutDocument(c, doc)
}
func (b *InventorySearchBuilder) PutMulti(c context.Context, srcs []*Inventory) ([]string, error) {
docs := make([]*InventorySearch, 0, len(srcs))
for _, src := range srcs {
doc, err := src.Searchfy()
if err != nil {
return nil, err
}
docs = append(docs, doc)
}
return b.PutDocumentMulti(c, docs)
}
func (b *InventorySearchBuilder) PutDocument(c context.Context, src *InventorySearch) (string, error) {
docIDs, err := b.PutDocumentMulti(c, []*InventorySearch{src})
if err != nil {
return "", err
}
return docIDs[0], nil
}
func (b *InventorySearchBuilder) PutDocumentMulti(c context.Context, srcs []*InventorySearch) ([]string, error) {
index, err := search.Open(b.IndexName())
if err != nil {
return nil, err
}
docIDs := make([]string, 0, len(srcs))
putSrcs := make([]interface{}, 0, len(srcs))
for _, src := range srcs {
docID := ""
if v, ok := interface{}(src).(smgutils.DocIDer); ok {
docID, err = v.DocID(c)
if err != nil {
return nil, err
}
src.ID = docID
}
docIDs = append(docIDs, docID)
putSrcs = append(putSrcs, src)
log.Debugf(c, "id: %#v, payload: %#v", docID, src)
}
docIDs, err = index.PutMulti(c, docIDs, putSrcs)
if err != nil {
return nil, err
}
for idx, docID := range docIDs {
srcs[idx].ID = docID
}
return docIDs, nil
}
func (b *InventorySearchBuilder) Delete(c context.Context, src *Inventory) error {
doc, err := src.Searchfy()
if err != nil {
return err
}
return b.DeleteDocument(c, doc)
}
func (b *InventorySearchBuilder) DeleteMulti(c context.Context, srcs []*Inventory) error {
docs := make([]*InventorySearch, 0, len(srcs))
for _, src := range srcs {
doc, err := src.Searchfy()
if err != nil {
return err
}
docs = append(docs, doc)
}
return b.DeleteDocumentMulti(c, docs)
}
func (b *InventorySearchBuilder) DeleteDocument(c context.Context, src *InventorySearch) error {
return b.DeleteDocumentMulti(c, []*InventorySearch{src})
}
func (b *InventorySearchBuilder) DeleteDocumentMulti(c context.Context, srcs []*InventorySearch) error {
docIDs := make([]string, 0, len(srcs))
for _, src := range srcs {
if v, ok := interface{}(src).(smgutils.DocIDer); ok {
docID, err := v.DocID(c)
if err != nil {
return err
}
docIDs = append(docIDs, docID)
continue
}
return errors.New("src is not implemented DocIDer interface")
}
return b.DeleteMultiByDocIDs(c, docIDs)
}
func (b *InventorySearchBuilder) DeleteByDocID(c context.Context, docID string) error {
return b.DeleteMultiByDocIDs(c, []string{docID})
}
func (b *InventorySearchBuilder) DeleteMultiByDocIDs(c context.Context, docIDs []string) error {
index, err := search.Open(b.IndexName())
if err != nil {
return err
}
return index.DeleteMulti(c, docIDs)
}
func (b *InventorySearchBuilder) Opts() *InventorySearchOptions {
return &InventorySearchOptions{b: b}
}
func (b *InventorySearchBuilder) Search(c context.Context) (*InventorySearchIterator, error) {
index, err := search.Open(b.IndexName())
if err != nil {
return nil, err
}
b.index = index
query, err := b.QueryString()
if err != nil {
return nil, err
}
b.query = query
log.Debugf(c, "query: `%s`, opts: %#v", b.query, b.opts)
iter := b.index.Search(c, b.query, b.opts)
return &InventorySearchIterator{b, iter}, nil
}
type InventorySearchOptions struct {
b *InventorySearchBuilder
}
func (b *InventorySearchOptions) Limit(value int) *InventorySearchOptions {
b.b.opts.Limit = value
return b
}
func (b *InventorySearchOptions) IDsOnly() *InventorySearchOptions {
b.b.opts.IDsOnly = true
return b
}
func (b *InventorySearchOptions) Cursor(cursor search.Cursor) *InventorySearchOptions {
b.b.opts.Cursor = cursor
return b
}
func (b *InventorySearchOptions) Offset(value int) *InventorySearchOptions {
b.b.opts.Offset = value
return b
}
type InventorySearchIterator struct {
b *InventorySearchBuilder
iter *search.Iterator
}
func (b *InventorySearchIterator) Next(c context.Context) (string, *InventorySearch, error) {
var s *InventorySearch
if b.b.opts.IDsOnly == false {
s = &InventorySearch{}
}
docID, err := b.iter.Next(s)
if err != nil {
return "", nil, err
}
if s != nil {
s.ID = docID
}
return docID, s, err
}
func (b *InventorySearchIterator) Cursor() search.Cursor {
return b.iter.Cursor()
}
type InventorySearchStringPropertyInfo struct {
Name string
b *InventorySearchBuilder
}
func (p *InventorySearchStringPropertyInfo) Match(value string) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Match, Value: value})
return p.b
}
type InventorySearchNgramStringPropertyInfo struct {
InventorySearchStringPropertyInfo
}
func (p *InventorySearchNgramStringPropertyInfo) NgramMatch(value string) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.NgramMatch, Value: value})
return p.b
}
type InventorySearchNumberPropertyInfo struct {
Name string
b *InventorySearchBuilder
}
func (p *InventorySearchNumberPropertyInfo) IntGreaterThanOrEqual(value int) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.GtEq, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) IntGreaterThan(value int) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Gt, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) IntLessThanOrEqual(value int) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.LtEq, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) IntLessThan(value int) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Lt, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) IntEqual(value int) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Eq, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Int64GreaterThanOrEqual(value int64) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.GtEq, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Int64GreaterThan(value int64) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Gt, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Int64LessThanOrEqual(value int64) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.LtEq, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Int64LessThan(value int64) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Lt, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Int64Equal(value int64) *InventorySearchBuilder {
p.b.currentOp.Children = append(p.b.currentOp.Children, &smgutils.Op{FieldName: p.Name, Type: smgutils.Eq, Value: value})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Asc() *InventorySearchBuilder {
p.b.opts.Sort.Expressions = append(p.b.opts.Sort.Expressions, search.SortExpression{
Expr: p.Name,
Reverse: true,
})
return p.b
}
func (p *InventorySearchNumberPropertyInfo) Desc() *InventorySearchBuilder {
p.b.opts.Sort.Expressions = append(p.b.opts.Sort.Expressions, search.SortExpression{
Expr: p.Name,
Reverse: false,
})
return p.b
}
type InventorySearchTimePropertyInfo struct {
Name string
b *InventorySearchBuilder
}