Skip to content

Commit 92498d4

Browse files
committed
Use Location in file system APIs
1 parent 12a6f81 commit 92498d4

File tree

132 files changed

+924
-847
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

132 files changed

+924
-847
lines changed

lib/trino-filesystem/src/main/java/io/trino/filesystem/FileEntry.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import static java.util.Comparator.comparing;
2727
import static java.util.Objects.requireNonNull;
2828

29-
public record FileEntry(String location, long length, Instant lastModified, Optional<List<Block>> blocks)
29+
public record FileEntry(Location location, long length, Instant lastModified, Optional<List<Block>> blocks)
3030
{
3131
public FileEntry
3232
{

lib/trino-filesystem/src/main/java/io/trino/filesystem/TrinoFileSystem.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public interface TrinoFileSystem
5454
*
5555
* @throws IllegalArgumentException if location is not valid for this file system
5656
*/
57-
TrinoInputFile newInputFile(String location);
57+
TrinoInputFile newInputFile(Location location);
5858

5959
/**
6060
* Creates a TrinoInputFile with a predeclared length which can be used to read the file data.
@@ -64,15 +64,15 @@ public interface TrinoFileSystem
6464
*
6565
* @throws IllegalArgumentException if location is not valid for this file system
6666
*/
67-
TrinoInputFile newInputFile(String location, long length);
67+
TrinoInputFile newInputFile(Location location, long length);
6868

6969
/**
7070
* Creates a TrinoOutputFile which can be used to create or overwrite the file. The file
7171
* location path cannot be empty, and must not end with a slash or whitespace.
7272
*
7373
* @throws IllegalArgumentException if location is not valid for this file system
7474
*/
75-
TrinoOutputFile newOutputFile(String location);
75+
TrinoOutputFile newOutputFile(Location location);
7676

7777
/**
7878
* Deletes the specified file. The file location path cannot be empty, and must not end with
@@ -81,7 +81,7 @@ public interface TrinoFileSystem
8181
* @throws IllegalArgumentException if location is not valid for this file system
8282
* @throws IOException if the file does not exist or was not deleted.
8383
*/
84-
void deleteFile(String location)
84+
void deleteFile(Location location)
8585
throws IOException;
8686

8787
/**
@@ -92,10 +92,10 @@ void deleteFile(String location)
9292
* @throws IllegalArgumentException if location is not valid for this file system
9393
* @throws IOException if a file does not exist or was not deleted.
9494
*/
95-
default void deleteFiles(Collection<String> locations)
95+
default void deleteFiles(Collection<Location> locations)
9696
throws IOException
9797
{
98-
for (String location : locations) {
98+
for (var location : locations) {
9999
deleteFile(location);
100100
}
101101
}
@@ -117,7 +117,7 @@ default void deleteFiles(Collection<String> locations)
117117
* @param location the directory to delete
118118
* @throws IllegalArgumentException if location is not valid for this file system
119119
*/
120-
void deleteDirectory(String location)
120+
void deleteDirectory(Location location)
121121
throws IOException;
122122

123123
/**
@@ -128,7 +128,7 @@ void deleteDirectory(String location)
128128
*
129129
* @throws IllegalArgumentException if either location is not valid for this file system
130130
*/
131-
void renameFile(String source, String target)
131+
void renameFile(Location source, Location target)
132132
throws IOException;
133133

134134
/**
@@ -148,6 +148,6 @@ void renameFile(String source, String target)
148148
* @param location the directory to list
149149
* @throws IllegalArgumentException if location is not valid for this file system
150150
*/
151-
FileIterator listFiles(String location)
151+
FileIterator listFiles(Location location)
152152
throws IOException;
153153
}

lib/trino-filesystem/src/main/java/io/trino/filesystem/TrinoInputFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ Instant lastModified()
3333
boolean exists()
3434
throws IOException;
3535

36-
String location();
36+
Location location();
3737
}

lib/trino-filesystem/src/main/java/io/trino/filesystem/TrinoOutputFile.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ OutputStream create(AggregatedMemoryContext memoryContext)
4141
OutputStream createOrOverwrite(AggregatedMemoryContext memoryContext)
4242
throws IOException;
4343

44-
String location();
44+
Location location();
4545
}

lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalFileIterator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import io.trino.filesystem.FileEntry;
1717
import io.trino.filesystem.FileIterator;
18+
import io.trino.filesystem.Location;
1819

1920
import java.io.IOException;
2021
import java.nio.file.Files;
@@ -34,7 +35,7 @@ class LocalFileIterator
3435
private final Path rootPath;
3536
private final Iterator<Path> iterator;
3637

37-
public LocalFileIterator(String location, Path rootPath, Path path)
38+
public LocalFileIterator(Location location, Path rootPath, Path path)
3839
throws IOException
3940
{
4041
this.rootPath = requireNonNull(rootPath, "rootPath is null");
@@ -75,7 +76,7 @@ public FileEntry next()
7576
}
7677

7778
return new FileEntry(
78-
"local:///" + rootPath.relativize(path),
79+
Location.of("local:///" + rootPath.relativize(path)),
7980
Files.size(path),
8081
Files.getLastModifiedTime(path).toInstant(),
8182
Optional.empty());

lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalFileSystem.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,25 @@ public LocalFileSystem(Path rootPath)
4545
}
4646

4747
@Override
48-
public TrinoInputFile newInputFile(String location)
48+
public TrinoInputFile newInputFile(Location location)
4949
{
5050
return new LocalInputFile(location, toFilePath(location));
5151
}
5252

5353
@Override
54-
public TrinoInputFile newInputFile(String location, long length)
54+
public TrinoInputFile newInputFile(Location location, long length)
5555
{
5656
return new LocalInputFile(location, toFilePath(location), length);
5757
}
5858

5959
@Override
60-
public TrinoOutputFile newOutputFile(String location)
60+
public TrinoOutputFile newOutputFile(Location location)
6161
{
6262
return new LocalOutputFile(location, toFilePath(location));
6363
}
6464

6565
@Override
66-
public void deleteFile(String location)
66+
public void deleteFile(Location location)
6767
throws IOException
6868
{
6969
Path filePath = toFilePath(location);
@@ -76,7 +76,7 @@ public void deleteFile(String location)
7676
}
7777

7878
@Override
79-
public void deleteDirectory(String location)
79+
public void deleteDirectory(Location location)
8080
throws IOException
8181
{
8282
Path directoryPath = toDirectoryPath(location);
@@ -121,7 +121,7 @@ public FileVisitResult postVisitDirectory(Path directory, IOException exception)
121121
}
122122

123123
@Override
124-
public void renameFile(String source, String target)
124+
public void renameFile(Location source, Location target)
125125
throws IOException
126126
{
127127
Path sourcePath = toFilePath(source);
@@ -145,45 +145,42 @@ public void renameFile(String source, String target)
145145
}
146146

147147
@Override
148-
public FileIterator listFiles(String location)
148+
public FileIterator listFiles(Location location)
149149
throws IOException
150150
{
151151
return new LocalFileIterator(location, rootPath, toDirectoryPath(location));
152152
}
153153

154-
private Path toFilePath(String fileLocation)
154+
private Path toFilePath(Location location)
155155
{
156-
Location location = parseLocalLocation(fileLocation);
156+
validateLocalLocation(location);
157157
location.verifyValidFileLocation();
158158

159-
Path localPath = toPath(fileLocation, location);
159+
Path localPath = toPath(location);
160160

161161
// local file path can not be empty as this would create a file for the root entry
162-
checkArgument(!localPath.equals(rootPath), "Local file location must contain a path: %s", fileLocation);
162+
checkArgument(!localPath.equals(rootPath), "Local file location must contain a path: %s", localPath);
163163
return localPath;
164164
}
165165

166-
private Path toDirectoryPath(String directoryLocation)
166+
private Path toDirectoryPath(Location location)
167167
{
168-
Location location = parseLocalLocation(directoryLocation);
169-
Path localPath = toPath(directoryLocation, location);
170-
return localPath;
168+
validateLocalLocation(location);
169+
return toPath(location);
171170
}
172171

173-
private static Location parseLocalLocation(String locationString)
172+
private static void validateLocalLocation(Location location)
174173
{
175-
Location location = Location.of(locationString);
176-
checkArgument(location.scheme().equals(Optional.of("local")), "Only 'local' scheme is supported: %s", locationString);
177-
checkArgument(location.userInfo().isEmpty(), "Local location cannot contain user info: %s", locationString);
178-
checkArgument(location.host().isEmpty(), "Local location cannot contain a host: %s", locationString);
179-
return location;
174+
checkArgument(location.scheme().equals(Optional.of("local")), "Only 'local' scheme is supported: %s", location);
175+
checkArgument(location.userInfo().isEmpty(), "Local location cannot contain user info: %s", location);
176+
checkArgument(location.host().isEmpty(), "Local location cannot contain a host: %s", location);
180177
}
181178

182-
private Path toPath(String locationString, Location location)
179+
private Path toPath(Location location)
183180
{
184181
// ensure path isn't something like '../../data'
185182
Path localPath = rootPath.resolve(location.path()).normalize();
186-
checkArgument(localPath.startsWith(rootPath), "Location references data outside of the root: %s", locationString);
183+
checkArgument(localPath.startsWith(rootPath), "Location references data outside of the root: %s", location);
187184
return localPath;
188185
}
189186
}

lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInput.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.filesystem.local;
1515

16+
import io.trino.filesystem.Location;
1617
import io.trino.filesystem.TrinoInput;
1718

1819
import java.io.EOFException;
@@ -28,12 +29,12 @@
2829
class LocalInput
2930
implements TrinoInput
3031
{
31-
private final String location;
32+
private final Location location;
3233
private final File file;
3334
private final RandomAccessFile input;
3435
private boolean closed;
3536

36-
public LocalInput(String location, File file)
37+
public LocalInput(Location location, File file)
3738
throws IOException
3839
{
3940
this.location = requireNonNull(location, "location is null");

lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInputFile.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
*/
1414
package io.trino.filesystem.local;
1515

16+
import io.trino.filesystem.Location;
1617
import io.trino.filesystem.TrinoInput;
1718
import io.trino.filesystem.TrinoInputFile;
1819
import io.trino.filesystem.TrinoInputStream;
@@ -33,18 +34,18 @@
3334
public class LocalInputFile
3435
implements TrinoInputFile
3536
{
36-
private final String location;
37+
private final Location location;
3738
private final Path path;
3839
private OptionalLong length = OptionalLong.empty();
3940
private Optional<Instant> lastModified = Optional.empty();
4041

41-
public LocalInputFile(String location, Path path)
42+
public LocalInputFile(Location location, Path path)
4243
{
4344
this.location = requireNonNull(location, "location is null");
4445
this.path = requireNonNull(path, "path is null");
4546
}
4647

47-
public LocalInputFile(String location, Path path, long length)
48+
public LocalInputFile(Location location, Path path, long length)
4849
{
4950
this.location = requireNonNull(location, "location is null");
5051
this.path = requireNonNull(path, "path is null");
@@ -54,7 +55,7 @@ public LocalInputFile(String location, Path path, long length)
5455

5556
public LocalInputFile(File file)
5657
{
57-
this(file.getPath(), file.toPath());
58+
this(Location.of(file.toURI().toString()), file.toPath());
5859
}
5960

6061
@Override
@@ -65,7 +66,7 @@ public TrinoInput newInput()
6566
return new LocalInput(location, path.toFile());
6667
}
6768
catch (IOException e) {
68-
throw new FileNotFoundException(location);
69+
throw new FileNotFoundException(location.toString());
6970
}
7071
}
7172

@@ -77,7 +78,7 @@ public TrinoInputStream newStream()
7778
return new LocalInputStream(location, path.toFile());
7879
}
7980
catch (FileNotFoundException e) {
80-
throw new FileNotFoundException(location);
81+
throw new FileNotFoundException(location.toString());
8182
}
8283
}
8384

@@ -119,14 +120,14 @@ public boolean exists()
119120
}
120121

121122
@Override
122-
public String location()
123+
public Location location()
123124
{
124125
return location;
125126
}
126127

127128
@Override
128129
public String toString()
129130
{
130-
return location();
131+
return location.toString();
131132
}
132133
}

lib/trino-filesystem/src/main/java/io/trino/filesystem/local/LocalInputStream.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import com.google.common.primitives.Ints;
1717
import com.google.common.primitives.Longs;
18+
import io.trino.filesystem.Location;
1819
import io.trino.filesystem.TrinoInputStream;
1920

2021
import java.io.BufferedInputStream;
@@ -30,15 +31,15 @@
3031
class LocalInputStream
3132
extends TrinoInputStream
3233
{
33-
private final String location;
34+
private final Location location;
3435
private final File file;
3536
private final long fileLength;
3637

3738
private InputStream input;
3839
private long position;
3940
private boolean closed;
4041

41-
public LocalInputStream(String location, File file)
42+
public LocalInputStream(Location location, File file)
4243
throws FileNotFoundException
4344
{
4445
this.location = requireNonNull(location, "location is null");

0 commit comments

Comments
 (0)