Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(966)

Side by Side Diff: pkg/barback/lib/src/internal_asset.dart

Issue 55983005: Serialize barback assets more efficiently. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add a couple docstrings Created 7 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library barback.asset; 5 library barback.internal_asset;
6 6
7 import 'dart:async'; 7 import 'dart:async';
8 import 'dart:convert'; 8 import 'dart:convert';
9 import 'dart:io'; 9 import 'dart:io';
10 10
11 import 'asset.dart';
11 import 'asset_id.dart'; 12 import 'asset_id.dart';
12 import 'file_pool.dart'; 13 import 'file_pool.dart';
14 import 'serialize.dart';
13 import 'stream_replayer.dart'; 15 import 'stream_replayer.dart';
14 import 'utils.dart'; 16 import 'utils.dart';
15 17
16 /// A blob of content. 18 /// The internal base class of barback assets.
17 /// 19 ///
18 /// Assets may come from the file system, or as the output of a [Transformer]. 20 /// This exposes serialization infrastructure so that pub can use it without
19 /// They are identified by [AssetId]. 21 /// making it part of the public barback API.
20 abstract class Asset { 22 abstract class InternalAsset implements Asset {
21 /// The ID for this asset.
22 final AssetId id; 23 final AssetId id;
23 24
24 Asset(this.id); 25 InternalAsset(this.id);
25 26
26 factory Asset.fromBytes(AssetId id, List<int> bytes) => 27 /// Deserialize an asset that's been serialized using [serialize].
27 new _BinaryAsset(id, bytes); 28 factory InternalAsset.deserialize(Map asset) {
29 switch (asset['type']) {
30 case 'stream': return new StreamAsset.deserialize(asset);
31 case 'binary': return new BinaryAsset.deserialize(asset);
32 case 'file': return new FileAsset.deserialize(asset);
33 case 'string': return new StringAsset.deserialize(asset);
34 default:
35 throw new FormatException('Unknown asset type "${asset['type']}".');
36 }
37 }
Bob Nystrom 2013/11/06 21:29:08 How about just moving this to Asset and getting ri
nweiz 2013/11/07 00:44:48 I really didn't want to commit to a publicly-visib
Bob Nystrom 2013/11/07 18:04:56 We could put it in a separate library (lib/seriali
nweiz 2013/11/07 22:36:08 "lib/serialization.dart" would still be part of th
28 38
29 factory Asset.fromFile(AssetId id, File file) => 39 /// Serialize this asset to a map that can be sent between isolates.
30 new _FileAsset(id, file); 40 Map serialize();
31
32 factory Asset.fromString(AssetId id, String content) =>
33 new _StringAsset(id, content);
34
35 factory Asset.fromPath(AssetId id, String path) =>
36 new _FileAsset(id, new File(path));
37
38 /// Creates an asset from a stream.
39 ///
40 /// This immediately starts draining [stream].
41 factory Asset.fromStream(AssetId id, Stream<List<int>> stream) =>
42 new _StreamAsset(id, stream);
43
44 /// Returns the contents of the asset as a string.
45 ///
46 /// If the asset was created from a [String] the original string is always
47 /// returned and [encoding] is ignored. Otherwise, the binary data of the
48 /// asset is decoded using [encoding], which defaults to [UTF8].
49 Future<String> readAsString({Encoding encoding});
50
51 /// Streams the binary contents of the asset.
52 ///
53 /// If the asset was created from a [String], this returns its UTF-8 encoding.
54 Stream<List<int>> read();
55 } 41 }
56 42
57 /// An asset whose data is stored in a list of bytes. 43 /// An asset whose data is stored in a list of bytes.
58 class _BinaryAsset extends Asset { 44 class BinaryAsset extends InternalAsset {
59 final List<int> _contents; 45 final List<int> _contents;
60 46
61 _BinaryAsset(AssetId id, this._contents) 47 BinaryAsset(AssetId id, this._contents)
62 : super(id); 48 : super(id);
63 49
50 BinaryAsset.deserialize(Map asset)
51 : this(deserializeId(asset['id']), asset['contents']);
52
64 Future<String> readAsString({Encoding encoding}) { 53 Future<String> readAsString({Encoding encoding}) {
65 if (encoding == null) encoding = UTF8; 54 if (encoding == null) encoding = UTF8;
66 55
67 return new Future.value(encoding.decode(_contents)); 56 return new Future.value(encoding.decode(_contents));
68 } 57 }
69 58
70 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream(); 59 Stream<List<int>> read() => new Future<List<int>>.value(_contents).asStream();
71 60
72 String toString() { 61 String toString() {
73 var buffer = new StringBuffer(); 62 var buffer = new StringBuffer();
(...skipping 15 matching lines...) Expand all
89 } else { 78 } else {
90 for (var i = 0; i < _contents.length; i++) { 79 for (var i = 0; i < _contents.length; i++) {
91 if (i > 0) buffer.write(" "); 80 if (i > 0) buffer.write(" ");
92 buffer.write(byteToHex(_contents[i])); 81 buffer.write(byteToHex(_contents[i]));
93 } 82 }
94 } 83 }
95 84
96 buffer.write("]"); 85 buffer.write("]");
97 return buffer.toString(); 86 return buffer.toString();
98 } 87 }
88
89 Map serialize() {
90 // TODO(nweiz): Send a typed array if that works after issue 14703 is fixed.
91 return {
92 'type': 'binary',
93 'id': serializeId(id),
94 'contents': _contents.toList()
Bob Nystrom 2013/11/06 21:29:08 Why copy the list? It's immutable (or, at least, i
nweiz 2013/11/07 00:44:48 The user could pass anything that implements List<
Bob Nystrom 2013/11/07 18:04:56 Ah, makes sense. Leave a comment to that effect.
nweiz 2013/11/07 22:36:08 Done.
95 };
96 }
99 } 97 }
100 98
101 /// An asset backed by a file on the local file system. 99 /// An asset backed by a file on the local file system.
102 class _FileAsset extends Asset { 100 class FileAsset extends InternalAsset {
103 /// Use a [FilePool] to handle reads so we can try to cope with running out 101 /// Use a [FilePool] to handle reads so we can try to cope with running out
104 /// of file descriptors more gracefully. 102 /// of file descriptors more gracefully.
105 static final _pool = new FilePool(); 103 static final _pool = new FilePool();
106 104
107 final File _file; 105 final String _path;
108 _FileAsset(AssetId id, this._file) 106 FileAsset(AssetId id, this._path)
109 : super(id); 107 : super(id);
110 108
109 FileAsset.deserialize(Map asset)
110 : this(deserializeId(asset['id']), asset['path']);
111
111 Future<String> readAsString({Encoding encoding}) { 112 Future<String> readAsString({Encoding encoding}) {
112 if (encoding == null) encoding = UTF8; 113 if (encoding == null) encoding = UTF8;
113 return _pool.readAsString(_file, encoding); 114 return _pool.readAsString(_path, encoding);
114 } 115 }
115 116
116 Stream<List<int>> read() => _pool.openRead(_file); 117 Stream<List<int>> read() => _pool.openRead(_path);
117 118
118 String toString() => 'File "${_file.path}"'; 119 String toString() => 'File "${_path}"';
120
121 Map serialize() {
122 return {
123 'type': 'file',
124 'id': serializeId(id),
125 'path': _path
126 };
127 }
119 } 128 }
120 129
121 /// An asset whose data is stored in a string. 130 /// An asset whose data is stored in a string.
122 class _StringAsset extends Asset { 131 class StringAsset extends InternalAsset {
123 final String _contents; 132 final String _contents;
124 133
125 _StringAsset(AssetId id, this._contents) 134 StringAsset(AssetId id, this._contents)
126 : super(id); 135 : super(id);
127 136
137 StringAsset.deserialize(Map asset)
138 : this(deserializeId(asset['id']), asset['contents']);
139
128 Future<String> readAsString({Encoding encoding}) => 140 Future<String> readAsString({Encoding encoding}) =>
129 new Future.value(_contents); 141 new Future.value(_contents);
130 142
131 Stream<List<int>> read() => 143 Stream<List<int>> read() =>
132 new Future<List<int>>.value(UTF8.encode(_contents)).asStream(); 144 new Future<List<int>>.value(UTF8.encode(_contents)).asStream();
133 145
134 String toString() { 146 String toString() {
135 // Don't show the whole string if it's long. 147 // Don't show the whole string if it's long.
136 var contents = _contents; 148 var contents = _contents;
137 if (contents.length > 40) { 149 if (contents.length > 40) {
138 contents = contents.substring(0, 20) + " ... " + 150 contents = contents.substring(0, 20) + " ... " +
139 contents.substring(contents.length - 20); 151 contents.substring(contents.length - 20);
140 } 152 }
141 153
142 contents = _escape(contents); 154 contents = _escape(contents);
143 return 'String "$contents"'; 155 return 'String "$contents"';
144 } 156 }
145 157
146 String _escape(String string) { 158 String _escape(String string) {
147 return string 159 return string
148 .replaceAll("\"", r'\"') 160 .replaceAll("\"", r'\"')
149 .replaceAll("\n", r"\n") 161 .replaceAll("\n", r"\n")
150 .replaceAll("\r", r"\r") 162 .replaceAll("\r", r"\r")
151 .replaceAll("\t", r"\t"); 163 .replaceAll("\t", r"\t");
152 } 164 }
165
166 Map serialize() {
167 return {
168 'type': 'string',
169 'id': serializeId(id),
170 'contents': _contents
171 };
172 }
153 } 173 }
154 174
155 /// An asset whose data is available from a stream. 175 /// An asset whose data is available from a stream.
156 class _StreamAsset extends Asset { 176 class StreamAsset extends InternalAsset {
157 /// A stream replayer that records and replays the contents of the input 177 /// A stream replayer that records and replays the contents of the input
158 /// stream. 178 /// stream.
159 final StreamReplayer<List<int>> _replayer; 179 final StreamReplayer<List<int>> _replayer;
160 180
161 _StreamAsset(AssetId id, Stream<List<int>> stream) 181 StreamAsset(AssetId id, Stream<List<int>> stream)
162 : _replayer = new StreamReplayer(stream), 182 : _replayer = new StreamReplayer(stream),
163 super(id); 183 super(id);
164 184
185 StreamAsset.deserialize(Map asset)
186 : this(deserializeId(asset['id']), deserializeStream(asset['stream']));
187
165 Future<String> readAsString({Encoding encoding}) { 188 Future<String> readAsString({Encoding encoding}) {
166 if (encoding == null) encoding = UTF8; 189 if (encoding == null) encoding = UTF8;
167 return _replayer.getReplay().toList() 190 return _replayer.getReplay().toList()
168 .then((chunks) => encoding.decode(flatten(chunks))); 191 .then((chunks) => encoding.decode(flatten(chunks)));
169 } 192 }
170 193
171 Stream<List<int>> read() => _replayer.getReplay(); 194 Stream<List<int>> read() => _replayer.getReplay();
172 195
173 String toString() => "Stream"; 196 String toString() => "Stream";
197
198 Map serialize() {
199 return {
200 'type': 'stream',
201 'id': serializeId(id),
202 'stream': serializeStream(_replayer.getReplay())
203 };
204 }
174 } 205 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698