-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
161 lines (143 loc) · 4.27 KB
/
common.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
package es
import (
"context"
"encoding/json"
"fmt"
"github.com/volatiletech/sqlboiler/queries/qm"
"github.com/Bnei-Baruch/archive-backend/consts"
"github.com/Bnei-Baruch/archive-backend/mdb"
"github.com/Bnei-Baruch/archive-backend/mdb/models"
)
func uidToTypedUID(t string, uid string) string {
return fmt.Sprintf("%s:%s", t, uid)
}
func uidsToTypedUIDs(t string, uids []string) []string {
ret := make([]string, len(uids))
for i, uid := range uids {
ret[i] = uidToTypedUID(t, uid)
}
return ret
}
// Scopes - for detection of changes
func contentUnitsScopeByFile(fileUID string) ([]string, error) {
units, err := mdbmodels.ContentUnits(mdb.DB,
qm.InnerJoin("files AS f on f.content_unit_id = content_units.id"),
qm.Where("f.uid = ?", fileUID)).All()
if err != nil {
return nil, err
}
uids := make([]string, len(units))
for i, unit := range units {
uids[i] = unit.UID
}
return uids, nil
}
func collectionsScopeByFile(fileUID string) ([]string, error) {
collections, err := mdbmodels.Collections(mdb.DB,
qm.InnerJoin("collections_content_units AS ccu ON ccu.collection_id = collections.id"),
qm.InnerJoin("content_units AS cu ON ccu.content_unit_id = cu.id"),
qm.InnerJoin("files AS f on f.content_unit_id = cu.id"),
qm.Where("f.uid = ?", fileUID)).All()
if err != nil {
return nil, err
}
uids := make([]string, len(collections))
for i, collection := range collections {
uids[i] = collection.UID
}
return uids, nil
}
func contentUnitsScopeByCollection(cUID string) ([]string, error) {
units, err := mdbmodels.ContentUnits(mdb.DB,
qm.InnerJoin("collections_content_units AS ccu ON ccu.content_unit_id = content_units.id"),
qm.InnerJoin("collections AS c ON ccu.collection_id = c.id"),
qm.Where("c.uid = ?", cUID)).All()
if err != nil {
return nil, err
}
uids := make([]string, len(units))
for i, unit := range units {
uids[i] = unit.UID
}
return uids, nil
}
func collectionsScopeByContentUnit(cuUID string) ([]string, error) {
collections, err := mdbmodels.Collections(mdb.DB,
qm.InnerJoin("collections_content_units AS ccu ON ccu.collection_id = collections.id"),
qm.InnerJoin("content_units AS cu ON ccu.content_unit_id = cu.id"),
qm.Where("cu.uid = ?", cuUID)).All()
if err != nil {
return nil, err
}
uids := make([]string, len(collections))
for i, collection := range collections {
uids[i] = collection.UID
}
return uids, nil
}
// DEBUG FUNCTIONS
func dumpDB(title string) error {
fmt.Printf("\n\n ------------------- %s ------------------- \n\n", title)
units, err := mdbmodels.ContentUnits(mdb.DB).All()
if err != nil {
return err
}
fmt.Printf("\n\nCONTENT_UNITS\n-------------\n\n")
for i, unit := range units {
fmt.Printf("%d: %+v\n", i, unit)
}
i18ns, err := mdbmodels.ContentUnitI18ns(mdb.DB).All()
if err != nil {
return err
}
fmt.Printf("\n\nCONTENT_UNIT_I18N\n-------------\n\n")
for i, i18n := range i18ns {
fmt.Printf("%d: %+v\n", i, i18n)
}
collections, err := mdbmodels.Collections(mdb.DB).All()
if err != nil {
return err
}
fmt.Printf("\n\nCOLLECTIONS\n-----------\n\n")
for i, c := range collections {
fmt.Printf("%d: %+v\n", i, c)
}
ccus, err := mdbmodels.CollectionsContentUnits(mdb.DB).All()
if err != nil {
return err
}
fmt.Printf("\n\nCOLLECTIONS_CONTENT_UNITS\n-----------\n\n")
for i, ccu := range ccus {
fmt.Printf("%d: %+v\n", i, ccu)
}
files, err := mdbmodels.Files(mdb.DB).All()
if err != nil {
return err
}
fmt.Printf("\n\nFILES\n-------------\n\n")
for i, file := range files {
fmt.Printf("%d: %+v\n", i, file)
}
fmt.Printf("\n\n ------------------- END OF %s ------------------- \n\n", title)
return nil
}
func dumpIndexes(title string) error {
fmt.Printf("\n\n ------------------- %s ------------------- \n\n", title)
indexName := IndexName("test", consts.ES_UNITS_INDEX, consts.LANG_ENGLISH)
fmt.Printf("\n\n\nINDEX %s\n\n", indexName)
indexer := MakeIndexer("test", []string{consts.ES_UNITS_INDEX})
if err := indexer.RefreshAll(); err != nil {
return err
}
res, err := mdb.ESC.Search().Index(indexName).Do(context.TODO())
if err != nil {
return err
}
for i, hit := range res.Hits.Hits {
var cu ContentUnit
json.Unmarshal(*hit.Source, &cu)
fmt.Printf("%d: %+v\n", i, cu)
}
fmt.Printf("\n\n ------------------- END OF %s ------------------- \n\n", title)
return err
}