/
rdf.go
144 lines (117 loc) · 3.19 KB
/
rdf.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
package bulk
import (
"context"
"fmt"
"net/http"
"github.com/delving/hub3/hub3/fragments"
"github.com/delving/hub3/hub3/models"
"github.com/delving/hub3/ikuzo/domain"
"github.com/delving/hub3/ikuzo/domain/domainpb"
"github.com/go-chi/render"
"github.com/gorilla/schema"
"github.com/rs/zerolog/log"
)
type rdfUploadForm struct {
Spec string `json:"spec"`
RecordType string `json:"recordType"`
TypePredicate string `json:"typePredicate"`
IDSplitter string `json:"idSplitter"`
}
func (ruf *rdfUploadForm) isValid() error {
if ruf.Spec == "" {
return fmt.Errorf("spec param is required")
}
if ruf.RecordType == "" {
return fmt.Errorf("recordType param is required")
}
if ruf.TypePredicate == "" {
return fmt.Errorf("typePredicate param is required")
}
if ruf.IDSplitter == "" {
return fmt.Errorf("idSplitter param is required")
}
return nil
}
var decoder = schema.NewDecoder()
func (s *Service) HandleRDF(w http.ResponseWriter, r *http.Request) {
orgID := domain.GetOrganizationID(r)
in, _, err := r.FormFile("turtle")
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
var form rdfUploadForm
err = decoder.Decode(&form, r.PostForm)
if err != nil {
log.Printf("Unable to decode form %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if err := form.isValid(); err != nil {
log.Printf("form is not valid; %s", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// todo handle when no form.Spec is given
ds, created, err := models.GetOrCreateDataSet(orgID.String(), form.Spec)
if err != nil {
log.Printf("Unable to get DataSet for %s\n", form.Spec)
render.PlainText(w, r, err.Error())
return
}
if created {
// err = fragments.SaveDataSet(orgID.String(), form.Spec, nil)
// if err != nil {
// log.Printf("Unable to Save DataSet Fragment for %s\n", form.Spec)
// if err != nil {
// render.PlainText(w, r, err.Error())
// return
// }
// }
}
ds, err = ds.IncrementRevision()
if err != nil {
render.PlainText(w, r, err.Error())
return
}
upl := fragments.NewRDFUploader(
orgID.String(),
form.Spec,
form.RecordType,
form.TypePredicate,
form.IDSplitter,
ds.Revision,
)
go func() {
defer in.Close()
log.Print("Start creating resource map")
_, err := upl.Parse(in)
if err != nil {
log.Printf("Can't read turtle file: %v", err)
return
}
log.Printf("Start saving fragments.")
processed, err := upl.IndexFragments(s.index)
if err != nil {
log.Printf("Can't save fragments: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Printf("Saved %d fragments for %s", processed, upl.Spec)
m := &domainpb.IndexMessage{
OrganisationID: orgID.String(),
DatasetID: form.Spec,
Revision: &domainpb.Revision{Number: upl.Revision},
IndexType: domainpb.IndexType_FRAGMENTS,
ActionType: domainpb.ActionType_DROP_ORPHANS,
}
if err := s.index.Publish(context.Background(), m); err != nil {
log.Printf("Can't dropped records: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}()
render.Status(r, http.StatusCreated)
render.PlainText(w, r, "ok")
return
}