MCPcopy
hub / github.com/cayleygraph/cayley / ServeRead

Method ServeRead

server/http/api_v2.go:316–373  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

314}
315
316func (api *APIv2) ServeRead(w http.ResponseWriter, r *http.Request) {
317 format := getFormat(r, "format", hdrAccept)
318 if format == nil || format.Writer == nil {
319 jsonResponse(w, http.StatusBadRequest, fmt.Errorf("format is not supported for reading data"))
320 return
321 }
322 h, err := api.handleForRequest(r)
323 if err != nil {
324 jsonResponse(w, http.StatusBadRequest, err)
325 return
326 }
327 values := shape.FilterQuads(
328 valuesFromString(r.FormValue("sub")),
329 valuesFromString(r.FormValue("pred")),
330 valuesFromString(r.FormValue("obj")),
331 valuesFromString(r.FormValue("label")),
332 )
333 it := values.BuildIterator(h.QuadStore)
334 qr := graph.NewResultReader(h.QuadStore, it)
335
336 defer qr.Close()
337
338 wr := writerFrom(w, r, hdrAcceptEncoding)
339 defer wr.Close()
340
341 cw := &checkWriter{w: wr}
342 qwc := format.Writer(cw)
343 defer qwc.Close()
344 var qw quad.Writer = qwc
345 if len(format.Mime) != 0 {
346 w.Header().Set(hdrContentType, format.Mime[0])
347 }
348 if irif := r.FormValue("iri"); irif != "" {
349 opts := quad.IRIOptions{
350 Format: quad.IRIDefault,
351 }
352 switch irif {
353 case "short":
354 opts.Format = quad.IRIShort
355 case "full":
356 opts.Format = quad.IRIFull
357 }
358 qw = quad.IRIWriter(qw, opts)
359 }
360 if bw, ok := qw.(quad.BatchWriter); ok {
361 _, err = quad.CopyBatch(bw, qr, api.batch)
362 } else {
363 _, err = quad.Copy(qw, qr)
364 }
365 if err != nil && !cw.written {
366 jsonResponse(w, http.StatusInternalServerError, err)
367 return
368 } else if err != nil {
369 // can do nothing here, since first byte (and header) was written
370 // TODO: check if client just gone away
371 clog.Errorf("read quads error: %v", err)
372 }
373}

Callers

nothing calls this directly

Calls 12

handleForRequestMethod · 0.95
FilterQuadsFunction · 0.92
NewResultReaderFunction · 0.92
ErrorfFunction · 0.92
getFormatFunction · 0.85
writerFromFunction · 0.85
jsonResponseFunction · 0.70
valuesFromStringFunction · 0.70
ErrorfMethod · 0.65
BuildIteratorMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected