aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/vfs
diff options
context:
space:
mode:
authorGravatar shahan <shahan@google.com>2018-02-13 10:08:47 -0800
committerGravatar Copybara-Service <copybara-piper@google.com>2018-02-13 10:10:14 -0800
commitfae34b9ee464186c628c313eebced9e89d8146f6 (patch)
tree55d6392acc16aa3c87ada027bf0d92c366c551cc /src/main/java/com/google/devtools/build/lib/vfs
parenta3339c8ca2e824757afe698f591770f4232af530 (diff)
Replaces InjectingObjectCodec with dependencies threaded through (Des|S)erializationContext.
PiperOrigin-RevId: 185547740
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/vfs')
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/FileSystemProvider.java21
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/Path.java19
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/Root.java19
-rw-r--r--src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java47
4 files changed, 25 insertions, 81 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemProvider.java b/src/main/java/com/google/devtools/build/lib/vfs/FileSystemProvider.java
deleted file mode 100644
index 160bb2ecf9..0000000000
--- a/src/main/java/com/google/devtools/build/lib/vfs/FileSystemProvider.java
+++ /dev/null
@@ -1,21 +0,0 @@
-// Copyright 2018 The Bazel Authors. All rights reserved.
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.google.devtools.build.lib.vfs;
-
-/** Abstraction for injecting FileSystem dependencies. */
-public interface FileSystemProvider {
-
- FileSystem getFileSystem();
-}
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/Path.java b/src/main/java/com/google/devtools/build/lib/vfs/Path.java
index 649eea9256..b4542ecd83 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/Path.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/Path.java
@@ -16,7 +16,6 @@ package com.google.devtools.build.lib.vfs;
import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
@@ -62,8 +61,7 @@ import javax.annotation.Nullable;
@ThreadSafe
public class Path
implements Comparable<Path>, Serializable, SkylarkPrintable, FileType.HasFileType {
- public static final InjectingObjectCodec<Path, FileSystemProvider> CODEC =
- new PathCodecWithInjectedFileSystem();
+ public static final ObjectCodec<Path> CODEC = new Codec();
private static FileSystem fileSystemForSerialization;
@@ -907,8 +905,7 @@ public class Path
driveStrLength = OS.getDriveStrLength(path);
}
- private static class PathCodecWithInjectedFileSystem
- implements InjectingObjectCodec<Path, FileSystemProvider> {
+ private static class Codec implements ObjectCodec<Path> {
private final ObjectCodec<String> stringCodec = StringCodecs.asciiOptimized();
@Override
@@ -918,21 +915,23 @@ public class Path
@Override
public void serialize(
- FileSystemProvider fsProvider,
SerializationContext context,
Path path,
CodedOutputStream codedOut)
throws IOException, SerializationException {
- Preconditions.checkArgument(path.getFileSystem() == fsProvider.getFileSystem());
+ Preconditions.checkArgument(
+ path.getFileSystem() == context.getDependency(FileSystem.class),
+ "%s != %s",
+ path.getFileSystem(),
+ context.getDependency(FileSystem.class));
stringCodec.serialize(context, path.getPathString(), codedOut);
}
@Override
- public Path deserialize(
- FileSystemProvider fsProvider, DeserializationContext context, CodedInputStream codedIn)
+ public Path deserialize(DeserializationContext context, CodedInputStream codedIn)
throws IOException, SerializationException {
return Path.createAlreadyNormalized(
- stringCodec.deserialize(context, codedIn), fsProvider.getFileSystem());
+ stringCodec.deserialize(context, codedIn), context.getDependency(FileSystem.class));
}
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/Root.java b/src/main/java/com/google/devtools/build/lib/vfs/Root.java
index 73ff687b56..6f090e41a7 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/Root.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/Root.java
@@ -16,7 +16,7 @@ package com.google.devtools.build.lib.vfs;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodec;
+import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
import com.google.protobuf.CodedInputStream;
@@ -35,7 +35,7 @@ import javax.annotation.Nullable;
*/
public interface Root extends Comparable<Root>, Serializable {
- InjectingObjectCodec<Root, FileSystemProvider> CODEC = new RootCodec();
+ ObjectCodec<Root> CODEC = new RootCodec();
/** Constructs a root from a path. */
static Root fromPath(Path path) {
@@ -258,7 +258,7 @@ public interface Root extends Comparable<Root>, Serializable {
}
/** Codec to serialize {@link Root}s. */
- class RootCodec implements InjectingObjectCodec<Root, FileSystemProvider> {
+ class RootCodec implements ObjectCodec<Root> {
@Override
public Class<Root> getEncodedClass() {
return Root.class;
@@ -266,16 +266,16 @@ public interface Root extends Comparable<Root>, Serializable {
@Override
public void serialize(
- FileSystemProvider dependency,
SerializationContext context,
Root obj,
CodedOutputStream codedOut)
throws SerializationException, IOException {
if (obj instanceof PathRoot) {
codedOut.writeBoolNoTag(false);
- Path.CODEC.serialize(dependency, context, ((PathRoot) obj).path, codedOut);
+ Path.CODEC.serialize(context, ((PathRoot) obj).path, codedOut);
} else if (obj instanceof AbsoluteRoot) {
- Preconditions.checkArgument(((AbsoluteRoot) obj).fileSystem == dependency.getFileSystem());
+ Preconditions.checkArgument(
+ ((AbsoluteRoot) obj).fileSystem == context.getDependency(FileSystem.class));
codedOut.writeBoolNoTag(true);
} else {
throw new AssertionError("Unknown Root subclass: " + obj.getClass().getName());
@@ -283,14 +283,13 @@ public interface Root extends Comparable<Root>, Serializable {
}
@Override
- public Root deserialize(
- FileSystemProvider dependency, DeserializationContext context, CodedInputStream codedIn)
+ public Root deserialize(DeserializationContext context, CodedInputStream codedIn)
throws SerializationException, IOException {
boolean isAbsolute = codedIn.readBool();
if (isAbsolute) {
- return dependency.getFileSystem().getAbsoluteRoot();
+ return context.getDependency(FileSystem.class).getAbsoluteRoot();
} else {
- Path path = Path.CODEC.deserialize(dependency, context, codedIn);
+ Path path = Path.CODEC.deserialize(context, codedIn);
return new PathRoot(path);
}
}
diff --git a/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java b/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
index aa3153463d..bbc89a96d9 100644
--- a/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
+++ b/src/main/java/com/google/devtools/build/lib/vfs/RootedPath.java
@@ -14,14 +14,8 @@
package com.google.devtools.build.lib.vfs;
import com.google.common.base.Preconditions;
-import com.google.devtools.build.lib.skyframe.serialization.DeserializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.InjectingObjectCodecAdapter;
import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
-import com.google.devtools.build.lib.skyframe.serialization.SerializationContext;
-import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
-import com.google.protobuf.CodedInputStream;
-import com.google.protobuf.CodedOutputStream;
-import java.io.IOException;
+import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import java.io.Serializable;
import java.util.Objects;
@@ -35,13 +29,18 @@ import java.util.Objects;
* <p>TODO(bazel-team): use an opaque root representation so as to not expose the absolute path to
* clients via #asPath or #getRoot.
*/
+@AutoCodec
public class RootedPath implements Serializable {
+ public static final ObjectCodec<RootedPath> CODEC = new RootedPath_AutoCodec();
+
private final Root root;
private final PathFragment rootRelativePath;
/** Constructs a {@link RootedPath} from a {@link Root} and path fragment relative to the root. */
- private RootedPath(Root root, PathFragment rootRelativePath) {
+ @AutoCodec.Instantiator
+ @AutoCodec.VisibleForSerialization
+ RootedPath(Root root, PathFragment rootRelativePath) {
Preconditions.checkState(
rootRelativePath.isAbsolute() == root.isAbsolute(),
"rootRelativePath: %s root: %s",
@@ -123,36 +122,4 @@ public class RootedPath implements Serializable {
public String toString() {
return "[" + root + "]/[" + rootRelativePath + "]";
}
-
- /** Custom serialization for {@link RootedPath}s. */
- public static class RootedPathCodec implements ObjectCodec<RootedPath> {
-
- private final ObjectCodec<Root> rootCodec;
-
- /** Create an instance which will deserialize RootedPaths on {@code fileSystem}. */
- public RootedPathCodec(FileSystem fileSystem) {
- this.rootCodec = new InjectingObjectCodecAdapter<>(Root.CODEC, () -> fileSystem);
- }
-
- @Override
- public Class<RootedPath> getEncodedClass() {
- return RootedPath.class;
- }
-
- @Override
- public void serialize(
- SerializationContext context, RootedPath rootedPath, CodedOutputStream codedOut)
- throws IOException, SerializationException {
- rootCodec.serialize(context, rootedPath.getRoot(), codedOut);
- PathFragment.CODEC.serialize(context, rootedPath.getRootRelativePath(), codedOut);
- }
-
- @Override
- public RootedPath deserialize(DeserializationContext context, CodedInputStream codedIn)
- throws IOException, SerializationException {
- Root root = rootCodec.deserialize(context, codedIn);
- PathFragment rootRelativePath = PathFragment.CODEC.deserialize(context, codedIn);
- return toRootedPath(root, rootRelativePath);
- }
- }
}