/
docstore.go
138 lines (94 loc) · 3.65 KB
/
docstore.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
package resolver
/*
$> cd /usr/local/data/dynamodb
$> java -Djava.library.path=./DynamoDBLocal_lib -jar DynamoDBLocal.jar -sharedDb
$> cd /usr/local/whosonfirst/go-whosonfirst-findingaid
$> go run -mod vendor cmd/create-dynamodb-tables/main.go -dynamodb-uri 'awsdynamodb://findingaid?region=us-west-2&endpoint=http://localhost:8000&credentials=static:local:local:local' -refresh
$> make cli && ./bin/populate -producer-uri 'awsdynamodb://findingaid?region=us-west-2&endpoint=http://localhost:8000&credentials=static:local:local:local&partition_key=id' /usr/local/data/sfomuseum-data-maps/
$> cd /usr/local/whosonfirst/go-reader-findingaid
$> make cli && ./bin/read -reader-uri 'findingaid://awsdynamodb/findingaid?region=us-west-2&endpoint=http://localhost:8000&credentials=static:local:local:local&partition_key=id&template=https://raw.githubusercontent.com/sfomuseum-data/{repo}/main/data/' 1360391327 | jq '.["properties"]["wof:name"]'
"SFO (1988)"
*/
import (
"context"
"fmt"
"github.com/aaronland/go-aws-dynamodb"
"gocloud.dev/docstore"
gc_dynamodb "gocloud.dev/docstore/awsdynamodb"
_ "log"
"net/url"
"strings"
)
// type DocstoreResolver implements the `Resolver` interface for data stored in a gocloud.dev/docstore compatible collection.
type DocstoreResolver struct {
Resolver
// A Docstore `sql.DB` instance containing Who's On First finding aid data.
collection *docstore.Collection
}
func init() {
ctx := context.Background()
RegisterResolver(ctx, "awsdynamodb", NewDocstoreResolver)
for _, scheme := range docstore.DefaultURLMux().CollectionSchemes() {
err := RegisterResolver(ctx, scheme, NewDocstoreResolver)
if err != nil {
panic(err)
}
}
}
// NewDocstoreResolver will return a new `Resolver` instance for resolving repository names
// and IDs stored in a gocloud.dev/docstore Collection.
func NewDocstoreResolver(ctx context.Context, uri string) (Resolver, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, fmt.Errorf("Failed to parse URL, %w", err)
}
// START OF put me in a package function or something
var collection *docstore.Collection
if u.Scheme == "awsdynamodb" {
// Connect local dynamodb using Golang
// https://gist.github.com/Tamal/02776c3e2db7eec73c001225ff52e827
// https://gocloud.dev/howto/docstore/#dynamodb-ctor
client, err := dynamodb.NewClientWithURI(ctx, uri)
if err != nil {
return nil, fmt.Errorf("Failed to create client, %v", err)
}
u, _ := url.Parse(uri)
table_name := u.Host
table_name = strings.TrimLeft(table_name, "/")
q := u.Query()
partition_key := q.Get("partition_key")
col, err := gc_dynamodb.OpenCollection(client, table_name, partition_key, "", nil)
if err != nil {
return nil, fmt.Errorf("Failed to open collection, %w", err)
}
collection = col
} else {
col, err := docstore.OpenCollection(ctx, uri)
if err != nil {
return nil, fmt.Errorf("Failed to create database for '%s', %w", uri, err)
}
collection = col
}
// END OF put me in a package function or something
if err != nil {
return nil, fmt.Errorf("Failed to open collection, %w", err)
}
f := &DocstoreResolver{
collection: collection,
}
return f, nil
}
// GetRepo returns the name of the repository associated with this ID in a Who's On First finding aid.
func (r *DocstoreResolver) GetRepo(ctx context.Context, id int64) (string, error) {
// TBD: Import whosonfirst/go-whosonfirst-findingaid/producer/docstore CatalogRecord?
doc := map[string]interface{}{
"id": id,
"repo_name": "",
}
err := r.collection.Get(ctx, doc)
if err != nil {
return "", fmt.Errorf("Failed to get record for %d, %w", id, err)
}
repo := doc["repo_name"].(string)
return repo, nil
}