/
descendants.go
285 lines (203 loc) · 6.85 KB
/
descendants.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
package sql
// Dealing with descendants means querying the `ancestors` table or joining on
// the `ancestors` table and the `spr` table. Originally querying for descendants
// was done using SQLite's "instr" function but a) that probably wouldn't have
// worked for MySQL, etc. and b) it (the function) triggered timeouts and errors
// when querying using a remote VFS-enabled SQLite database, or at least that is
// the current theory.
import (
"context"
db_sql "database/sql"
"fmt"
// "log/slog"
"strings"
"github.com/aaronland/go-pagination"
"github.com/aaronland/go-pagination/countable"
"github.com/whosonfirst/go-whosonfirst-spelunker"
wof_spr "github.com/whosonfirst/go-whosonfirst-spr/v2"
"github.com/whosonfirst/go-whosonfirst-sql/tables"
"github.com/whosonfirst/go-whosonfirst-sqlite-spr"
)
func (s *SQLSpelunker) GetDescendants(ctx context.Context, pg_opts pagination.Options, id int64, filters []spelunker.Filter) (wof_spr.StandardPlacesResults, pagination.Results, error) {
q_where, q_args, err := s.descendantsQueryWhere(ctx, id, filters)
if err != nil {
return nil, nil, fmt.Errorf("Failed to derive query where statement, %w", err)
}
q_cols := s.descendantsQueryColumnsAll(ctx)
q := s.descendantsQueryStatement(ctx, q_cols, q_where)
if pg_opts != nil {
limit, offset := s.deriveLimitOffset(pg_opts)
q = fmt.Sprintf("%s LIMIT %d OFFSET %d", q, limit, offset)
}
pg_ch := make(chan pagination.Results)
results_ch := make(chan wof_spr.StandardPlacesResults)
done_ch := make(chan bool)
err_ch := make(chan error)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
go func() {
defer func() {
done_ch <- true
}()
count_q := s.descendantsQueryCountStatement(ctx, q_where)
count, err := s.queryCount(ctx, fmt.Sprintf("%s.id", tables.SPR_TABLE_NAME), count_q, q_args...)
if err != nil {
err_ch <- fmt.Errorf("Failed to derive query count, %w", err)
return
}
var pg_results pagination.Results
var pg_err error
if pg_opts != nil {
pg_results, pg_err = countable.NewResultsFromCountWithOptions(pg_opts, count)
} else {
pg_results, pg_err = countable.NewResultsFromCount(count)
}
if pg_err != nil {
err_ch <- fmt.Errorf("Failed to derive pagination results, %w", pg_err)
return
}
pg_ch <- pg_results
}()
go func() {
defer func() {
done_ch <- true
}()
rows, err := s.db.QueryContext(ctx, q, q_args...)
if err != nil {
err_ch <- fmt.Errorf("Failed to query where '%s', %w", q, err)
return
}
results := make([]wof_spr.StandardPlacesResult, 0)
for rows.Next() {
select {
case <-ctx.Done():
break
default:
// pass
}
spr_row, err := spr.RetrieveSPRWithRows(ctx, rows)
if err != nil {
err_ch <- fmt.Errorf("Failed to derive SPR from row, %w", err)
return
}
results = append(results, spr_row)
}
err = rows.Close()
if err != nil {
err_ch <- fmt.Errorf("Failed to close results rows for descendants, %w", err)
return
}
spr_results := &spr.SQLiteResults{
Places: results,
}
results_ch <- spr_results
}()
var pg_results pagination.Results
var spr_results wof_spr.StandardPlacesResults
remaining := 2
for remaining > 0 {
select {
case <-done_ch:
remaining -= 1
case r := <-pg_ch:
pg_results = r
case r := <-results_ch:
spr_results = r
case err := <-err_ch:
return nil, nil, err
}
}
return spr_results, pg_results, nil
}
func (s *SQLSpelunker) GetDescendantsFaceted(ctx context.Context, id int64, filters []spelunker.Filter, facets []*spelunker.Facet) ([]*spelunker.Faceting, error) {
q_where, q_args, err := s.descendantsQueryWhere(ctx, id, filters)
if err != nil {
return nil, fmt.Errorf("Failed to derive query where statement, %w", err)
}
results := make([]*spelunker.Faceting, len(facets))
// START OF do this in go routines
for idx, f := range facets {
q := s.descendantsQueryFacetStatement(ctx, f, q_where)
// slog.Info("FACET", "q", q, "args", q_args)
counts, err := s.facetWithQuery(ctx, q, q_args...)
if err != nil {
return nil, fmt.Errorf("Failed to facet columns, %w", err)
}
fc := &spelunker.Faceting{
Facet: f,
Results: counts,
}
results[idx] = fc
}
// END OF do this in go routines
return results, nil
}
func (s *SQLSpelunker) CountDescendants(ctx context.Context, id int64) (int64, error) {
var count int64
q := fmt.Sprintf("SELECT COUNT(id) FROM %s WHERE ancestor_id = ?", tables.ANCESTORS_TABLE_NAME)
row := s.db.QueryRowContext(ctx, q, id)
err := row.Scan(&count)
switch {
case err == db_sql.ErrNoRows:
return 0, spelunker.ErrNotFound
case err != nil:
return 0, fmt.Errorf("Failed to execute count descendants query for %d, %w", id, err)
default:
return count, nil
}
}
func (s *SQLSpelunker) descendantsQueryWhere(ctx context.Context, id int64, filters []spelunker.Filter) ([]string, []interface{}, error) {
where := []string{
fmt.Sprintf("%s.ancestor_id = ?", tables.ANCESTORS_TABLE_NAME),
}
args := []interface{}{
id,
}
where, args, err := s.assignFilters(where, args, filters)
if err != nil {
return nil, nil, err
}
return where, args, nil
}
func (s *SQLSpelunker) descendantsQueryColumnsAll(ctx context.Context) []string {
// START OF put me in a function
str_cols := `id, parent_id, name, placetype,
inception, cessation,
country, repo,
latitude, longitude,
min_latitude, min_longitude,
max_latitude, max_longitude,
is_current, is_deprecated, is_ceased,is_superseded, is_superseding,
supersedes, superseded_by, belongsto,
is_alt, alt_label,
lastmodified`
cols := strings.Split(str_cols, ",")
// END OF put me in a function
count_cols := len(cols)
fq_cols := make([]string, count_cols)
for idx, c := range cols {
c = strings.TrimSpace(c)
fq_cols[idx] = fmt.Sprintf("%s.%s AS %s", tables.SPR_TABLE_NAME, c, c)
}
return fq_cols
}
func (s *SQLSpelunker) descendantsQueryStatement(ctx context.Context, cols []string, where []string) string {
str_cols := strings.Join(cols, ",")
str_where := strings.Join(where, " AND ")
return fmt.Sprintf("SELECT %s FROM %s JOIN %s ON %s.id = %s.id AND %s", str_cols, tables.SPR_TABLE_NAME, tables.ANCESTORS_TABLE_NAME, tables.SPR_TABLE_NAME, tables.ANCESTORS_TABLE_NAME, str_where)
}
func (s *SQLSpelunker) descendantsQueryCountStatement(ctx context.Context, where []string) string {
cols := []string{
fmt.Sprintf("%s.id AS id", tables.SPR_TABLE_NAME),
}
return s.descendantsQueryStatement(ctx, cols, where)
}
func (s *SQLSpelunker) descendantsQueryFacetStatement(ctx context.Context, facet *spelunker.Facet, where []string) string {
facet_label := s.facetLabel(facet)
cols := []string{
fmt.Sprintf("%s.%s AS %s", tables.SPR_TABLE_NAME, facet_label, facet),
fmt.Sprintf("COUNT(%s.id) AS count", tables.SPR_TABLE_NAME),
}
q := s.descendantsQueryStatement(ctx, cols, where)
return fmt.Sprintf("%s GROUP BY %s.%s ORDER BY count DESC", q, tables.SPR_TABLE_NAME, facet_label)
}