An efficient collection of resource quantities. All values are guaranteed to be positive and finite. E.g. [("cpus", 4.5), ("gpus", 0.1), ("ports", 1000)] Absent resource entries imply there is no (zero) such resources. Notes on handling negativity and arithmetic operations: values are guaranteed to be positive. This is achieved by construction validation and no public mutation interfaces. Durin
| 61 | // model and arithmetic implementation have to operate |
| 62 | // on broader invariants. |
| 63 | class ResourceQuantities |
| 64 | { |
| 65 | public: |
| 66 | // Parse an input string of semicolon separated "name:number" pairs. |
| 67 | // Duplicate names are allowed in the input and will be merged into one entry. |
| 68 | // Entries with zero values will be silently dropped. |
| 69 | // |
| 70 | // Example: "cpus:10;mem:1024;ports:3" |
| 71 | // "cpus:10; mem:1024; ports:3" |
| 72 | // NOTE: we will trim the whitespace around the pair and in the number. |
| 73 | // However, whitespace in "c p us:10" are preserved and will be parsed to |
| 74 | // {"c p us", 10}. This is consistent with `Resources::fromSimpleString()`. |
| 75 | // |
| 76 | // Numbers must be non-negative and finite, otherwise an `Error` |
| 77 | // will be returned. |
| 78 | static Try<ResourceQuantities> fromString(const std::string& text); |
| 79 | |
| 80 | // Take scalar `Resources` and combine them into `ResourceQuantities`. |
| 81 | // Only the resource name and its scalar value are used and the rest of the |
| 82 | // meta-data is ignored. It is caller's responsibility to ensure all |
| 83 | // `Resource` entries are of scalar type. Otherwise a `CHECK` error will |
| 84 | // be triggered. |
| 85 | static ResourceQuantities fromScalarResources(const Resources& resources); |
| 86 | |
| 87 | // Same as above, but takes a single Resource that must be valid |
| 88 | // and of scalar type. |
| 89 | static ResourceQuantities fromScalarResource(const Resource& resource); |
| 90 | |
| 91 | // Take `Resources` and combine them into `ResourceQuantities`. This function |
| 92 | // assumes that the provided resources have already been validated; for |
| 93 | // example, it assumes that ranges do not overlap and that sets do not contain |
| 94 | // duplicate items. |
| 95 | static ResourceQuantities fromResources(const Resources& resources); |
| 96 | |
| 97 | // Returns the summed up `ResourceQuantities` given a |
| 98 | // `hashmap<Key, ResourceQuantities>`. |
| 99 | template <typename Key> |
| 100 | static ResourceQuantities sum(const hashmap<Key, ResourceQuantities>& map) |
| 101 | { |
| 102 | ResourceQuantities result; |
| 103 | |
| 104 | foreachvalue (const ResourceQuantities& quantities, map) { |
| 105 | result += quantities; |
| 106 | } |
| 107 | |
| 108 | return result; |
| 109 | } |
| 110 | |
| 111 | ResourceQuantities(); |
| 112 | |
| 113 | explicit ResourceQuantities( |
| 114 | const google::protobuf::Map<std::string, Value::Scalar>& map); |
| 115 | |
| 116 | ResourceQuantities(const ResourceQuantities& that) = default; |
| 117 | ResourceQuantities(ResourceQuantities&& that) = default; |
| 118 | |
| 119 | ResourceQuantities& operator=(const ResourceQuantities& that) = default; |
| 120 | ResourceQuantities& operator=(ResourceQuantities&& that) = default; |