OLD | NEW |
---|---|
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 /** | 5 /** |
6 * An isolate-compatible object registry and lookup service. | 6 * An isolate-compatible object registry and lookup service. |
7 */ | 7 */ |
8 library dart.pkg.isolate.registry; | 8 library dart.pkg.isolate.registry; |
9 | 9 |
10 import "dart:async" show Future, Completer, TimeoutException; | 10 import "dart:async" show Future, Completer, TimeoutException; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
67 * If a registry is used between isolates created using [Isolate.spawnUri], | 67 * If a registry is used between isolates created using [Isolate.spawnUri], |
68 * the `Registry` object can't be sent between the isolates directly. | 68 * the `Registry` object can't be sent between the isolates directly. |
69 * Instead the [RegistryManager.commandPort] port can be sent and a | 69 * Instead the [RegistryManager.commandPort] port can be sent and a |
70 * `Registry` created from the command port using this constructor. | 70 * `Registry` created from the command port using this constructor. |
71 * | 71 * |
72 * The optional [timeout] parameter can be set to the duration | 72 * The optional [timeout] parameter can be set to the duration |
73 * this registry should wait before assuming that an operation | 73 * this registry should wait before assuming that an operation |
74 * has failed. | 74 * has failed. |
75 */ | 75 */ |
76 Registry.fromPort(SendPort commandPort, | 76 Registry.fromPort(SendPort commandPort, |
77 {Duration timeout: const Duration(seconds: 5)}) | 77 {Duration timeout: const Duration(seconds: 5)}) |
Lasse Reichstein Nielsen
2015/02/26 10:59:14
Indent '{' to after '('.
| |
78 : _commandPort = commandPort, | 78 : _commandPort = commandPort, |
79 _timeout = timeout; | 79 _timeout = timeout; |
80 | 80 |
81 _RegistryCache get _cache { | 81 _RegistryCache get _cache { |
82 _RegistryCache cache = _caches[this]; | 82 _RegistryCache cache = _caches[this]; |
83 if (cache != null) return cache; | 83 if (cache != null) return cache; |
84 cache = new _RegistryCache(); | 84 cache = new _RegistryCache(); |
85 _caches[this] = cache; | 85 _caches[this] = cache; |
86 return cache; | 86 return cache; |
87 } | 87 } |
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 | 252 |
253 final Map<int, Object> id2object = new HashMap(); | 253 final Map<int, Object> id2object = new HashMap(); |
254 final Map<Object, int> object2id = new HashMap.identity(); | 254 final Map<Object, int> object2id = new HashMap.identity(); |
255 | 255 |
256 int id(Object object) { | 256 int id(Object object) { |
257 int result = object2id[object]; | 257 int result = object2id[object]; |
258 if (result == _BEING_ADDED) return null; | 258 if (result == _BEING_ADDED) return null; |
259 return result; | 259 return result; |
260 } | 260 } |
261 | 261 |
262 Object operator[](int id) => id2object[id]; | 262 Object operator [](int id) => id2object[id]; |
263 | 263 |
264 // Register a pair of id/object in the cache. | 264 // Register a pair of id/object in the cache. |
265 // if the id is already in the cache, just return the existing | 265 // if the id is already in the cache, just return the existing |
266 // object. | 266 // object. |
267 Object register(int id, Object object) { | 267 Object register(int id, Object object) { |
268 object = id2object.putIfAbsent(id, () { | 268 object = id2object.putIfAbsent(id, () { |
269 object2id[object] = id; | 269 object2id[object] = id; |
270 return object; | 270 return object; |
271 }); | 271 }); |
272 return object; | 272 return object; |
273 } | 273 } |
274 | 274 |
275 bool isAdding(element) => object2id[element] == _BEING_ADDED; | 275 bool isAdding(element) => object2id[element] == _BEING_ADDED; |
276 | 276 |
277 void setAdding(element) { | 277 void setAdding(element) { |
278 assert(!contains(element)); | 278 assert(!contains(element)); |
279 object2id[element] = _BEING_ADDED; | 279 object2id[element] = _BEING_ADDED; |
280 } | 280 } |
281 | 281 |
282 void stopAdding(element) { | 282 void stopAdding(element) { |
283 assert(object2id[element] == _BEING_ADDED); | 283 assert(object2id[element] == _BEING_ADDED); |
284 object2id.remove(element); | 284 object2id.remove(element); |
285 } | 285 } |
286 | 286 |
287 void remove(int id) { | 287 void remove(int id) { |
(...skipping 24 matching lines...) Expand all Loading... | |
312 /** | 312 /** |
313 * Create a new registry managed by the created [RegistryManager]. | 313 * Create a new registry managed by the created [RegistryManager]. |
314 * | 314 * |
315 * The optional [timeout] parameter can be set to the duration | 315 * The optional [timeout] parameter can be set to the duration |
316 * registry objects should wait before assuming that an operation | 316 * registry objects should wait before assuming that an operation |
317 * has failed. | 317 * has failed. |
318 */ | 318 */ |
319 RegistryManager({timeout: const Duration(seconds: 5)}) | 319 RegistryManager({timeout: const Duration(seconds: 5)}) |
320 : _timeout = timeout, | 320 : _timeout = timeout, |
321 _commandPort = new RawReceivePort() { | 321 _commandPort = new RawReceivePort() { |
322 _commandPort.handler = _handleCommand; | 322 _commandPort.handler = _handleCommand; |
323 } | 323 } |
324 | 324 |
325 /** | 325 /** |
326 * The command port receiving commands for the registry manager. | 326 * The command port receiving commands for the registry manager. |
327 * | 327 * |
328 * Use this port with [Registry.fromPort] to link a registry to the | 328 * Use this port with [Registry.fromPort] to link a registry to the |
329 * manager in isolates where you can't send a [Registry] object directly. | 329 * manager in isolates where you can't send a [Registry] object directly. |
330 */ | 330 */ |
331 SendPort get commandPort => _commandPort.sendPort; | 331 SendPort get commandPort => _commandPort.sendPort; |
332 | 332 |
333 /** | 333 /** |
334 * Get a registry backed by this manager. | 334 * Get a registry backed by this manager. |
335 * | 335 * |
336 * This registry can be sent to other isolates created using | 336 * This registry can be sent to other isolates created using |
337 * [Isolate.spawn]. | 337 * [Isolate.spawn]. |
338 */ | 338 */ |
339 Registry get registry => new Registry.fromPort(_commandPort.sendPort, | 339 Registry get registry => |
340 timeout: _timeout); | 340 new Registry.fromPort(_commandPort.sendPort, timeout: _timeout); |
341 | 341 |
342 // Used as argument to putIfAbsent. | 342 // Used as argument to putIfAbsent. |
343 static Set _createSet() => new HashSet(); | 343 static Set _createSet() => new HashSet(); |
344 | 344 |
345 void _handleCommand(List command) { | 345 void _handleCommand(List command) { |
346 switch(command[0]) { | 346 switch (command[0]) { |
347 case _ADD: | 347 case _ADD: |
348 _add(command[1], command[2], command[3]); | 348 _add(command[1], command[2], command[3]); |
349 return; | 349 return; |
350 case _REMOVE: | 350 case _REMOVE: |
351 _remove(command[1], command[2], command[3]); | 351 _remove(command[1], command[2], command[3]); |
352 return; | 352 return; |
353 case _ADD_TAGS: | 353 case _ADD_TAGS: |
354 _addTags(command[1], command[2], command[3]); | 354 _addTags(command[1], command[2], command[3]); |
355 return; | 355 return; |
356 case _REMOVE_TAGS: | 356 case _REMOVE_TAGS: |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
391 _tag2id[tag].remove(id); | 391 _tag2id[tag].remove(id); |
392 } | 392 } |
393 replyPort.send(true); | 393 replyPort.send(true); |
394 } | 394 } |
395 | 395 |
396 void _addTags(List<int> ids, List tags, SendPort replyPort) { | 396 void _addTags(List<int> ids, List tags, SendPort replyPort) { |
397 assert(tags != null); | 397 assert(tags != null); |
398 assert(tags.isNotEmpty); | 398 assert(tags.isNotEmpty); |
399 for (int id in ids) { | 399 for (int id in ids) { |
400 _RegistryEntry entry = _entries[id]; | 400 _RegistryEntry entry = _entries[id]; |
401 if (entry == null) continue; // Entry was removed. | 401 if (entry == null) continue; // Entry was removed. |
Lasse Reichstein Nielsen
2015/02/26 10:59:14
Two spaces before '//'.
| |
402 entry.tags.addAll(tags); | 402 entry.tags.addAll(tags); |
403 for (var tag in tags) { | 403 for (var tag in tags) { |
404 Set ids = _tag2id.putIfAbsent(tag, _createSet); | 404 Set ids = _tag2id.putIfAbsent(tag, _createSet); |
405 ids.add(id); | 405 ids.add(id); |
406 } | 406 } |
407 } | 407 } |
408 replyPort.send(null); | 408 replyPort.send(null); |
409 } | 409 } |
410 | 410 |
411 void _removeTags(List<int> ids, List tags, SendPort replyPort) { | 411 void _removeTags(List<int> ids, List tags, SendPort replyPort) { |
412 assert(tags != null); | 412 assert(tags != null); |
413 assert(tags.isNotEmpty); | 413 assert(tags.isNotEmpty); |
414 for (int id in ids) { | 414 for (int id in ids) { |
415 _RegistryEntry entry = _entries[id]; | 415 _RegistryEntry entry = _entries[id]; |
416 if (entry == null) continue; // Object was removed. | 416 if (entry == null) continue; // Object was removed. |
Lasse Reichstein Nielsen
2015/02/26 10:59:15
Ditto.
| |
417 entry.tags.removeAll(tags); | 417 entry.tags.removeAll(tags); |
418 } | 418 } |
419 for (var tag in tags) { | 419 for (var tag in tags) { |
420 Set tagIds = _tag2id[tag]; | 420 Set tagIds = _tag2id[tag]; |
421 if (tagIds == null) continue; | 421 if (tagIds == null) continue; |
422 tagIds.removeAll(ids); | 422 tagIds.removeAll(ids); |
423 } | 423 } |
424 replyPort.send(null); | 424 replyPort.send(null); |
425 } | 425 } |
426 | 426 |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
460 var entries = _entries.values; | 460 var entries = _entries.values; |
461 if (max != null) entries = entries.take(max); | 461 if (max != null) entries = entries.take(max); |
462 for (_RegistryEntry entry in entries) { | 462 for (_RegistryEntry entry in entries) { |
463 result.add(entry.id); | 463 result.add(entry.id); |
464 result.add(entry.element); | 464 result.add(entry.element); |
465 } | 465 } |
466 replyPort.send(result); | 466 replyPort.send(result); |
467 return; | 467 return; |
468 } | 468 } |
469 var matchingIds = _findTaggedIds(tags); | 469 var matchingIds = _findTaggedIds(tags); |
470 if (max == null) max = matchingIds.length; // All results. | 470 if (max == null) max = matchingIds.length; // All results. |
Lasse Reichstein Nielsen
2015/02/26 10:59:15
And ditto on the spaces.
| |
471 for (var id in matchingIds) { | 471 for (var id in matchingIds) { |
472 result.add(id); | 472 result.add(id); |
473 result.add(_entries[id].element); | 473 result.add(_entries[id].element); |
474 max--; | 474 max--; |
475 if (max == 0) break; | 475 if (max == 0) break; |
476 } | 476 } |
477 replyPort.send(result); | 477 replyPort.send(result); |
478 } | 478 } |
479 | 479 |
480 /** | 480 /** |
481 * Shut down the registry service. | 481 * Shut down the registry service. |
482 * | 482 * |
483 * After this, all [Registry] operations will time out. | 483 * After this, all [Registry] operations will time out. |
484 */ | 484 */ |
485 void close() { | 485 void close() { |
486 _commandPort.close(); | 486 _commandPort.close(); |
487 } | 487 } |
488 } | 488 } |
489 | 489 |
490 /** Entry in [RegistryManager]. */ | 490 /** Entry in [RegistryManager]. */ |
491 class _RegistryEntry { | 491 class _RegistryEntry { |
492 final int id; | 492 final int id; |
493 final Object element; | 493 final Object element; |
494 final Set tags = new HashSet(); | 494 final Set tags = new HashSet(); |
495 final Capability removeCapability = new Capability(); | 495 final Capability removeCapability = new Capability(); |
496 _RegistryEntry(this.id, this.element); | 496 _RegistryEntry(this.id, this.element); |
497 } | 497 } |
OLD | NEW |