forked from cayleygraph/cayley
/
load.go
82 lines (73 loc) · 1.96 KB
/
load.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
package internal
import (
"fmt"
"io"
client "net/http"
"net/url"
"os"
"path/filepath"
"github.com/google/cayley/graph"
"github.com/google/cayley/internal/config"
"github.com/google/cayley/internal/db"
"github.com/google/cayley/quad"
"github.com/google/cayley/quad/cquads"
"github.com/google/cayley/quad/nquads"
)
// Load loads a graph from the given path and write it to qw. See
// DecompressAndLoad for more information.
func Load(qw graph.QuadWriter, cfg *config.Config, path, typ string) error {
return DecompressAndLoad(qw, cfg, path, typ, db.Load)
}
// DecompressAndLoad will load or fetch a graph from the given path, decompress
// it, and then call the given load function to process the decompressed graph.
// If no loadFn is provided, db.Load is called.
func DecompressAndLoad(qw graph.QuadWriter, cfg *config.Config, path, typ string, loadFn func(graph.QuadWriter, *config.Config, quad.Unmarshaler) error) error {
var r io.Reader
if path == "" {
path = cfg.DatabasePath
}
if path == "" {
return nil
}
u, err := url.Parse(path)
if err != nil || u.Scheme == "file" || u.Scheme == "" {
// Don't alter relative URL path or non-URL path parameter.
if u.Scheme != "" && err == nil {
// Recovery heuristic for mistyping "file://path/to/file".
path = filepath.Join(u.Host, u.Path)
}
f, err := os.Open(path)
if err != nil {
return fmt.Errorf("could not open file %q: %v", path, err)
}
defer f.Close()
r = f
} else {
res, err := client.Get(path)
if err != nil {
return fmt.Errorf("could not get resource <%s>: %v", u, err)
}
defer res.Body.Close()
r = res.Body
}
r, err = Decompressor(r)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
var dec quad.Unmarshaler
switch typ {
case "cquad":
dec = cquads.NewDecoder(r)
case "nquad":
dec = nquads.NewDecoder(r)
default:
return fmt.Errorf("unknown quad format %q", typ)
}
if loadFn != nil {
return loadFn(qw, cfg, dec)
}
return db.Load(qw, cfg, dec)
}