janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 1 | // Copyright 2017 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 | |
Googler | ab5f233 | 2017-12-11 15:32:29 -0800 | [diff] [blame] | 15 | package com.google.devtools.build.lib.vfs; |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 16 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 17 | import com.google.common.base.Preconditions; |
michajlo | e45c055 | 2018-02-11 09:23:55 -0800 | [diff] [blame] | 18 | import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext; |
Googler | ab5f233 | 2017-12-11 15:32:29 -0800 | [diff] [blame] | 19 | import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec; |
michajlo | e45c055 | 2018-02-11 09:23:55 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.skyframe.serialization.SerializationContext; |
Googler | ab5f233 | 2017-12-11 15:32:29 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.skyframe.serialization.SerializationException; |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 22 | import com.google.devtools.build.lib.skyframe.serialization.strings.StringCodecs; |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 23 | import com.google.protobuf.CodedInputStream; |
| 24 | import com.google.protobuf.CodedOutputStream; |
| 25 | import java.io.IOException; |
| 26 | |
| 27 | /** Custom serialization for {@link Path}s. */ |
| 28 | public class PathCodec implements ObjectCodec<Path> { |
| 29 | |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 30 | private final ObjectCodec<String> stringCodec = StringCodecs.asciiOptimized(); |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 31 | private final FileSystem fileSystem; |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 32 | |
| 33 | /** Create an instance for serializing and deserializing {@link Path}s on {@code fileSystem}. */ |
Googler | ab5f233 | 2017-12-11 15:32:29 -0800 | [diff] [blame] | 34 | public PathCodec(FileSystem fileSystem) { |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 35 | this.fileSystem = fileSystem; |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | @Override |
| 39 | public Class<Path> getEncodedClass() { |
| 40 | return Path.class; |
| 41 | } |
| 42 | |
| 43 | @Override |
michajlo | e45c055 | 2018-02-11 09:23:55 -0800 | [diff] [blame] | 44 | public void serialize(SerializationContext context, Path path, CodedOutputStream codedOut) |
michajlo | b45d5f5 | 2017-10-07 19:44:43 +0200 | [diff] [blame] | 45 | throws IOException, SerializationException { |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 46 | Preconditions.checkState( |
| 47 | path.getFileSystem() == fileSystem, |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 48 | "Path's FileSystem (%s) did not match the configured FileSystem (%s) for path (%s)", |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 49 | path.getFileSystem(), |
tomlu | a729b9b | 2018-02-08 15:32:00 -0800 | [diff] [blame] | 50 | fileSystem, |
| 51 | path); |
michajlo | e45c055 | 2018-02-11 09:23:55 -0800 | [diff] [blame] | 52 | stringCodec.serialize(context, path.getPathString(), codedOut); |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | @Override |
michajlo | e45c055 | 2018-02-11 09:23:55 -0800 | [diff] [blame] | 56 | public Path deserialize(DeserializationContext context, CodedInputStream codedIn) |
| 57 | throws IOException, SerializationException { |
| 58 | return fileSystem.getPath(stringCodec.deserialize(context, codedIn)); |
janakr | 23e152c | 2017-09-14 02:58:31 +0200 | [diff] [blame] | 59 | } |
| 60 | } |