Googler | 74d894a | 2017-12-07 10:38:45 -0800 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import com.google.common.collect.ImmutableSortedSet; |
| 17 | import com.google.devtools.build.lib.cmdline.Label; |
Googler | ab5f233 | 2017-12-11 15:32:29 -0800 | [diff] [blame^] | 18 | import com.google.devtools.build.lib.cmdline.LabelCodec; |
Googler | 74d894a | 2017-12-07 10:38:45 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.skyframe.TestSuiteExpansionValue.TestSuiteExpansionKey; |
Googler | 74d894a | 2017-12-07 10:38:45 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; |
| 21 | import com.google.devtools.build.lib.skyframe.serialization.SerializationException; |
| 22 | import com.google.protobuf.CodedInputStream; |
| 23 | import com.google.protobuf.CodedOutputStream; |
| 24 | import java.io.IOException; |
| 25 | import java.util.Comparator; |
| 26 | |
| 27 | /** Custom serialization for {@link TestSuiteExpansionKey}. */ |
| 28 | class TestSuiteExpansionKeyCodec implements ObjectCodec<TestSuiteExpansionKey> { |
| 29 | public static final TestSuiteExpansionKeyCodec INSTANCE = new TestSuiteExpansionKeyCodec(); |
| 30 | |
| 31 | private TestSuiteExpansionKeyCodec() {} |
| 32 | |
| 33 | @Override |
| 34 | public Class<TestSuiteExpansionKey> getEncodedClass() { |
| 35 | return TestSuiteExpansionKey.class; |
| 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public void serialize(TestSuiteExpansionKey key, CodedOutputStream codedOut) |
| 40 | throws IOException, SerializationException { |
| 41 | // Writes the target count to the stream so deserialization knows when to stop. |
| 42 | codedOut.writeInt32NoTag(key.getTargets().size()); |
| 43 | for (Label label : key.getTargets()) { |
| 44 | LabelCodec.INSTANCE.serialize(label, codedOut); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | @Override |
| 49 | public TestSuiteExpansionKey deserialize(CodedInputStream codedIn) |
| 50 | throws SerializationException, IOException { |
| 51 | int size = codedIn.readInt32(); |
| 52 | ImmutableSortedSet.Builder<Label> builder = |
| 53 | new ImmutableSortedSet.Builder<>(Comparator.naturalOrder()); |
| 54 | for (int i = 0; i < size; ++i) { |
| 55 | builder.add(LabelCodec.INSTANCE.deserialize(codedIn)); |
| 56 | } |
| 57 | return new TestSuiteExpansionKey(builder.build()); |
| 58 | } |
| 59 | } |