func ReadTriplesFromFile(c chan *graph.Triple, tripleFile string) { f, err := os.Open(tripleFile) if err != nil { glog.Fatalln("Couldn't open file", tripleFile) } defer func() { if err := f.Close(); err != nil { glog.Fatalln(err) } }() nquads.ReadNQuadsFromReader(c, f) }
func (api *Api) ServeV1WriteNQuad(w http.ResponseWriter, r *http.Request, params httprouter.Params) int { if api.config.ReadOnly { return FormatJson400(w, "Database is read-only.") } formFile, _, err := r.FormFile("NQuadFile") if err != nil { glog.Errorln(err) return FormatJsonError(w, 500, "Couldn't read file: "+err.Error()) } defer formFile.Close() blockSize, blockErr := strconv.ParseInt(r.URL.Query().Get("block_size"), 10, 64) if blockErr != nil { blockSize = int64(api.config.LoadSize) } tChan := make(chan *graph.Triple) go nquads.ReadNQuadsFromReader(tChan, formFile) tripleblock := make([]*graph.Triple, blockSize) nTriples := 0 i := int64(0) for t := range tChan { tripleblock[i] = t i++ nTriples++ if i == blockSize { api.ts.AddTripleSet(tripleblock) i = 0 } } api.ts.AddTripleSet(tripleblock[0:i]) fmt.Fprintf(w, "{\"result\": \"Successfully wrote %d triples.\"}", nTriples) return 200 }