-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathbookstore.go
75 lines (68 loc) · 2.17 KB
/
bookstore.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
package main
import (
"net/url"
"strings"
"github.com/dgraph-io/badger"
"github.com/jirenius/go-res/store/badgerstore"
"github.com/rs/xid"
)
// BookStore contains the store and query stores for the books. BadgerDB is used
// for storage, but any other database can be used. What is needed is a wrapper
// that implements the Store and QueryStore interfaces found in package:
//
// github.com/jirenius/go-res/store
type BookStore struct {
*badgerstore.Store
BooksByTitle *badgerstore.QueryStore
}
// A badgerstore db index by book title (lower case).
var idxBookTitle = badgerstore.Index{
Name: "idxBook_title",
Key: func(v interface{}) []byte {
book := v.(Book)
return []byte(strings.ToLower(book.Title))
},
}
// NewBookStore creates a new BookStore.
func NewBookStore(db *badger.DB) *BookStore {
st := badgerstore.NewStore(db).
SetType(Book{}).
SetPrefix("book")
return &BookStore{
Store: st,
BooksByTitle: badgerstore.NewQueryStore(st, booksByTitleIndexQuery).
AddIndex(idxBookTitle),
}
}
// booksByTitleIndexQuery handles query requests. This method is badgerstore
// specific, and allows for simple index based queries towards the badgerDB
// store.
//
// Other database implementations for store.QueryStore would do it differently.
// A sql implementation might have you generate a proper WHERE statement, where
// as a mongoDB implementation would need a bson query document.
func booksByTitleIndexQuery(qs *badgerstore.QueryStore, q url.Values) (*badgerstore.IndexQuery, error) {
// All query parameters are ignored. Just query all books without limit.
return &badgerstore.IndexQuery{
Index: idxBookTitle,
Limit: -1,
}, nil
}
// Init seeds an empty store with some initial books. It panics on errors.
func (st *BookStore) Init() {
if err := st.Store.Init(func(add func(id string, v interface{})) error {
for _, book := range []Book{
{Title: "Animal Farm", Author: "George Orwell"},
{Title: "Brave New World", Author: "Aldous Huxley"},
{Title: "Coraline", Author: "Neil Gaiman"},
} {
book.ID = xid.New().String()
add(book.ID, book)
}
return nil
}); err != nil {
panic(err)
}
// Wait for the badgerDB index to be created
st.BooksByTitle.Flush()
}