Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 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. | |
| 4 | |
| 5 library operation.queue; | |
| 6 | |
| 7 import 'dart:collection'; | |
| 8 | |
| 9 import 'package:analysis_server/src/analysis_server.dart'; | |
| 10 import 'package:analysis_server/src/operation/operation.dart'; | |
| 11 | |
| 12 | |
| 13 /** | |
| 14 * A queue of operations in an [AnalysisServer]. | |
| 15 */ | |
| 16 class ServerOperationQueue { | |
|
Brian Wilkerson
2014/06/02 14:32:18
The implementation of this class makes the assumpt
scheglov
2014/06/02 17:56:59
Done.
Now we do two passes - during the first we l
| |
| 17 final List<Queue<ServerOperation>> queues = <Queue<ServerOperation>>[]; | |
| 18 | |
| 19 ServerOperationQueue() { | |
| 20 for (int i = 0; i < ServerOperationPriority.COUNT; i++) { | |
| 21 var queue = new DoubleLinkedQueue<ServerOperation>(); | |
| 22 queues.add(queue); | |
| 23 } | |
| 24 } | |
| 25 | |
| 26 /** | |
| 27 * Adds the given operation to this queue. The exact position in the queue | |
| 28 * depends on the priority of the given operation relative to the priorities | |
| 29 * of the other operations in the queue. If there is already an operation with | |
| 30 * which the given one can be merge, it will be merged. | |
| 31 */ | |
| 32 void add(ServerOperation operation) { | |
| 33 int queueIndex = operation.priority.ordinal; | |
| 34 Queue<ServerOperation> queue = queues[queueIndex]; | |
| 35 // check if can be merged | |
| 36 if (operation is MergeableOperation) { | |
| 37 for (ServerOperation existingOperation in queue) { | |
| 38 if (existingOperation is MergeableOperation) { | |
| 39 bool merged = existingOperation.mergeWith(operation); | |
| 40 if (merged) { | |
| 41 return; | |
| 42 } | |
| 43 } | |
| 44 } | |
| 45 } | |
| 46 // add to the end | |
| 47 queue.addLast(operation); | |
| 48 } | |
| 49 | |
| 50 /** | |
| 51 * Returns `true` if there are no queued [ServerOperation]s. | |
| 52 */ | |
| 53 bool get isEmpty { | |
| 54 return queues.every((queue) => queue.isEmpty); | |
| 55 } | |
| 56 | |
| 57 /** | |
| 58 * Returns the next operation to perform or `null` if empty. | |
| 59 */ | |
| 60 ServerOperation take() { | |
| 61 for (Queue<ServerOperation> queue in queues) { | |
| 62 if (!queue.isEmpty) { | |
| 63 return queue.removeFirst(); | |
| 64 } | |
| 65 } | |
| 66 return null; | |
| 67 } | |
| 68 } | |
| 69 | |
| OLD | NEW |