forked from cayleygraph/cayley
-
Notifications
You must be signed in to change notification settings - Fork 0
/
load.go
158 lines (143 loc) · 3.52 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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package internal
import (
"fmt"
"io"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/cayleygraph/cayley/clog"
"github.com/cayleygraph/cayley/graph"
"github.com/cayleygraph/cayley/internal/decompressor"
"github.com/cayleygraph/cayley/quad"
"github.com/cayleygraph/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, batch int, path, typ string) error {
return DecompressAndLoad(qw, batch, path, typ, nil)
}
type readCloser struct {
quad.ReadCloser
close func() error
}
func (r readCloser) Close() error {
err := r.ReadCloser.Close()
if r.close != nil {
r.close()
}
return err
}
type nopCloser struct {
quad.Reader
}
func (r nopCloser) Close() error { return nil }
func QuadReaderFor(path, typ string) (quad.ReadCloser, error) {
var (
r io.Reader
c io.Closer
)
if path == "-" {
r = os.Stdin
} else if u, err := url.Parse(path); 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 os.IsNotExist(err) {
return nil, err
} else if err != nil {
return nil, fmt.Errorf("could not open file %q: %v", path, err)
}
r, c = f, f
} else {
res, err := http.Get(path)
if err != nil {
return nil, fmt.Errorf("could not get resource <%s>: %v", u, err)
}
// TODO(dennwc): save content type for format auto-detection
r, c = res.Body, res.Body
}
r, err := decompressor.New(r)
if err != nil {
if c != nil {
c.Close()
}
if err == io.EOF {
return nopCloser{quad.NewReader(nil)}, nil
}
return nil, err
}
var qr quad.ReadCloser
switch typ {
case "cquad", "nquad": // legacy
qr = nquads.NewReader(r, false)
default:
var format *quad.Format
if typ == "" {
name := filepath.Base(path)
name = strings.TrimSuffix(name, ".gz")
name = strings.TrimSuffix(name, ".bz2")
format = quad.FormatByExt(filepath.Ext(name))
if format == nil {
typ = "nquads"
}
}
if format == nil {
format = quad.FormatByName(typ)
}
if format == nil {
err = fmt.Errorf("unknown quad format %q", typ)
} else if format.Reader == nil {
err = fmt.Errorf("decoding of %q is not supported", typ)
}
if err != nil {
if c != nil {
c.Close()
}
return nil, err
}
qr = format.Reader(r)
}
if c != nil {
return readCloser{ReadCloser: qr, close: c.Close}, nil
}
return qr, nil
}
// 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, batch int, path, typ string, writerFunc func(graph.QuadWriter) graph.BatchWriter) error {
if path == "" {
return nil
}
qr, err := QuadReaderFor(path, typ)
if err != nil {
return err
}
defer qr.Close()
if writerFunc == nil {
writerFunc = graph.NewWriter
}
dest := writerFunc(qw)
_, err = quad.CopyBatch(&batchLogger{BatchWriter: dest}, qr, batch)
if err != nil {
return fmt.Errorf("db: failed to load data: %v", err)
}
return dest.Close()
}
type batchLogger struct {
cnt int
quad.BatchWriter
}
func (w *batchLogger) WriteQuads(quads []quad.Quad) (int, error) {
n, err := w.BatchWriter.WriteQuads(quads)
if clog.V(2) {
w.cnt += n
clog.Infof("Wrote %d quads.", w.cnt)
}
return n, err
}