@@ -17,7 +17,15 @@ const MAX_IPFS_MAP_FILE_SIZE_ENV_VAR: &str = "GRAPH_MAX_IPFS_MAP_FILE_SIZE";
17
17
// 256MB
18
18
const MAX_IPFS_MAP_FILE_SIZE : u64 = 256 * 1024 * 1024 ;
19
19
20
- type ValueStream = Box < Stream < Item = Value , Error = failure:: Error > + Send + ' static > ;
20
+ /// The values that `json_stream` returns. The struct contains the deserialized
21
+ /// JSON value from the input stream, together with the line number from which
22
+ /// the value was read.
23
+ pub struct StreamValue {
24
+ pub value : Value ,
25
+ pub line : usize ,
26
+ }
27
+
28
+ type ValueStream = Box < Stream < Item = StreamValue , Error = failure:: Error > + Send + ' static > ;
21
29
22
30
/// Resolves links to subgraph manifests and resources referenced by them.
23
31
pub trait LinkResolver : Send + Sync + ' static {
@@ -90,7 +98,7 @@ impl LinkResolver for ipfs_api::IpfsClient {
90
98
/// Supports links of the form `/ipfs/ipfs_hash` or just `ipfs_hash`.
91
99
fn cat ( & self , link : & Link ) -> Box < Future < Item = Vec < u8 > , Error = failure:: Error > + Send > {
92
100
// Grab env vars.
93
- let max_file_bytes = read_u64_env_var ( MAX_IPFS_FILE_BYTES_ENV_VAR ) ;
101
+ let max_file_bytes = read_u64_from_env ( MAX_IPFS_FILE_BYTES_ENV_VAR ) ;
94
102
95
103
// Discard the `/ipfs/` prefix (if present) to get the hash.
96
104
let path = link. link . trim_start_matches ( "/ipfs/" ) . to_owned ( ) ;
@@ -120,16 +128,19 @@ impl LinkResolver for ipfs_api::IpfsClient {
120
128
// to the line number in the overall file
121
129
let mut count = 0 ;
122
130
123
- let stream: ValueStream =
124
- Box :: new ( poll_fn ( move || -> Poll < Option < Value > , failure:: Error > {
131
+ let stream: ValueStream = Box :: new ( poll_fn (
132
+ move || -> Poll < Option < StreamValue > , failure:: Error > {
125
133
loop {
126
134
if let Some ( offset) = buf. iter ( ) . position ( |b| * b == b'\n' ) {
127
135
let line_bytes = buf. split_to ( offset + 1 ) ;
128
136
count += 1 ;
129
137
if line_bytes. len ( ) > 1 {
130
138
let line = std:: str:: from_utf8 ( & line_bytes) ?;
131
139
let res = match serde_json:: from_str :: < Value > ( line) {
132
- Ok ( v) => Ok ( Async :: Ready ( Some ( v) ) ) ,
140
+ Ok ( v) => Ok ( Async :: Ready ( Some ( StreamValue {
141
+ value : v,
142
+ line : count,
143
+ } ) ) ) ,
133
144
Err ( e) => {
134
145
// Adjust the line number in the serde error. This
135
146
// is fun because we can only get at the full error
@@ -162,7 +173,8 @@ impl LinkResolver for ipfs_api::IpfsClient {
162
173
}
163
174
}
164
175
}
165
- } ) ) ;
176
+ } ,
177
+ ) ) ;
166
178
// Check the size of the file
167
179
let max_file_bytes =
168
180
read_u64_from_env ( MAX_IPFS_MAP_FILE_SIZE_ENV_VAR ) . unwrap_or ( MAX_IPFS_MAP_FILE_SIZE ) ;
@@ -209,7 +221,7 @@ mod tests {
209
221
let link = runtime. block_on ( client. add ( text. as_bytes ( ) ) ) . unwrap ( ) . hash ;
210
222
runtime. block_on (
211
223
LinkResolver :: json_stream ( & client, & Link { link : link. clone ( ) } )
212
- . and_then ( |stream| stream. collect ( ) ) ,
224
+ . and_then ( |stream| stream. map ( |sv| sv . value ) . collect ( ) ) ,
213
225
)
214
226
}
215
227
0 commit comments