Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import static java.util.Comparator.comparing;
import static java.util.Objects.requireNonNull;

public record FileEntry(String location, long length, Instant lastModified, Optional<List<Block>> blocks)
public record FileEntry(Location location, long length, Instant lastModified, Optional<List<Block>> blocks)
{
public FileEntry
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public final class Location
private final OptionalInt port;
private final String path;

public static Location parse(String location)
public static Location of(String location)
{
requireNonNull(location, "location is null");
checkArgument(!location.isEmpty(), "location is empty");
Expand Down Expand Up @@ -121,11 +121,6 @@ private Location withPath(String location, String path)
return new Location(location, scheme, userInfo, host, port, path);
}

public String location()
{
return location;
}

/**
* Returns the scheme of the location, if present.
* If the scheme is present, the value will not be an empty string.
Expand Down Expand Up @@ -287,6 +282,9 @@ public int hashCode()
return location.hashCode();
}

/**
* Return the original location string.
*/
@Override
public String toString()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public interface TrinoFileSystem
*
* @throws IllegalArgumentException if location is not valid for this file system
*/
TrinoInputFile newInputFile(String location);
TrinoInputFile newInputFile(Location location);

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

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

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

/**
Expand All @@ -92,10 +92,10 @@ void deleteFile(String location)
* @throws IllegalArgumentException if location is not valid for this file system
* @throws IOException if a file does not exist or was not deleted.
*/
default void deleteFiles(Collection<String> locations)
default void deleteFiles(Collection<Location> locations)
throws IOException
{
for (String location : locations) {
for (var location : locations) {
deleteFile(location);
}
}
Expand All @@ -117,7 +117,7 @@ default void deleteFiles(Collection<String> locations)
* @param location the directory to delete
* @throws IllegalArgumentException if location is not valid for this file system
*/
void deleteDirectory(String location)
void deleteDirectory(Location location)
throws IOException;

/**
Expand All @@ -128,7 +128,7 @@ void deleteDirectory(String location)
*
* @throws IllegalArgumentException if either location is not valid for this file system
*/
void renameFile(String source, String target)
void renameFile(Location source, Location target)
throws IOException;

/**
Expand All @@ -148,6 +148,6 @@ void renameFile(String source, String target)
* @param location the directory to list
* @throws IllegalArgumentException if location is not valid for this file system
*/
FileIterator listFiles(String location)
FileIterator listFiles(Location location)
throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ Instant lastModified()
boolean exists()
throws IOException;

String location();
Location location();
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,5 @@ OutputStream create(AggregatedMemoryContext memoryContext)
OutputStream createOrOverwrite(AggregatedMemoryContext memoryContext)
throws IOException;

String location();
Location location();
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import io.trino.filesystem.FileEntry;
import io.trino.filesystem.FileIterator;
import io.trino.filesystem.Location;

import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -34,7 +35,7 @@ class LocalFileIterator
private final Path rootPath;
private final Iterator<Path> iterator;

public LocalFileIterator(String location, Path rootPath, Path path)
public LocalFileIterator(Location location, Path rootPath, Path path)
throws IOException
{
this.rootPath = requireNonNull(rootPath, "rootPath is null");
Expand Down Expand Up @@ -75,7 +76,7 @@ public FileEntry next()
}

return new FileEntry(
"local:///" + rootPath.relativize(path),
Location.of("local:///" + rootPath.relativize(path)),
Files.size(path),
Files.getLastModifiedTime(path).toInstant(),
Optional.empty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static io.trino.filesystem.Location.parse;
import static io.trino.filesystem.local.LocalUtils.handleException;

/**
Expand All @@ -46,25 +45,25 @@ public LocalFileSystem(Path rootPath)
}

@Override
public TrinoInputFile newInputFile(String location)
public TrinoInputFile newInputFile(Location location)
{
return new LocalInputFile(location, toFilePath(location));
}

@Override
public TrinoInputFile newInputFile(String location, long length)
public TrinoInputFile newInputFile(Location location, long length)
{
return new LocalInputFile(location, toFilePath(location), length);
}

@Override
public TrinoOutputFile newOutputFile(String location)
public TrinoOutputFile newOutputFile(Location location)
{
return new LocalOutputFile(location, toFilePath(location));
}

@Override
public void deleteFile(String location)
public void deleteFile(Location location)
throws IOException
{
Path filePath = toFilePath(location);
Expand All @@ -77,7 +76,7 @@ public void deleteFile(String location)
}

@Override
public void deleteDirectory(String location)
public void deleteDirectory(Location location)
throws IOException
{
Path directoryPath = toDirectoryPath(location);
Expand Down Expand Up @@ -122,7 +121,7 @@ public FileVisitResult postVisitDirectory(Path directory, IOException exception)
}

@Override
public void renameFile(String source, String target)
public void renameFile(Location source, Location target)
throws IOException
{
Path sourcePath = toFilePath(source);
Expand All @@ -146,45 +145,42 @@ public void renameFile(String source, String target)
}

@Override
public FileIterator listFiles(String location)
public FileIterator listFiles(Location location)
throws IOException
{
return new LocalFileIterator(location, rootPath, toDirectoryPath(location));
}

private Path toFilePath(String fileLocation)
private Path toFilePath(Location location)
{
Location location = parseLocalLocation(fileLocation);
validateLocalLocation(location);
location.verifyValidFileLocation();

Path localPath = toPath(fileLocation, location);
Path localPath = toPath(location);

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

private Path toDirectoryPath(String directoryLocation)
private Path toDirectoryPath(Location location)
{
Location location = parseLocalLocation(directoryLocation);
Path localPath = toPath(directoryLocation, location);
return localPath;
validateLocalLocation(location);
return toPath(location);
}

private static Location parseLocalLocation(String locationString)
private static void validateLocalLocation(Location location)
{
Location location = parse(locationString);
checkArgument(location.scheme().equals(Optional.of("local")), "Only 'local' scheme is supported: %s", locationString);
checkArgument(location.userInfo().isEmpty(), "Local location cannot contain user info: %s", locationString);
checkArgument(location.host().isEmpty(), "Local location cannot contain a host: %s", locationString);
return location;
checkArgument(location.scheme().equals(Optional.of("local")), "Only 'local' scheme is supported: %s", location);
checkArgument(location.userInfo().isEmpty(), "Local location cannot contain user info: %s", location);
checkArgument(location.host().isEmpty(), "Local location cannot contain a host: %s", location);
}

private Path toPath(String locationString, Location location)
private Path toPath(Location location)
{
// ensure path isn't something like '../../data'
Path localPath = rootPath.resolve(location.path()).normalize();
checkArgument(localPath.startsWith(rootPath), "Location references data outside of the root: %s", locationString);
checkArgument(localPath.startsWith(rootPath), "Location references data outside of the root: %s", location);
return localPath;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.filesystem.local;

import io.trino.filesystem.Location;
import io.trino.filesystem.TrinoInput;

import java.io.EOFException;
Expand All @@ -28,12 +29,12 @@
class LocalInput
implements TrinoInput
{
private final String location;
private final Location location;
private final File file;
private final RandomAccessFile input;
private boolean closed;

public LocalInput(String location, File file)
public LocalInput(Location location, File file)
throws IOException
{
this.location = requireNonNull(location, "location is null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
package io.trino.filesystem.local;

import io.trino.filesystem.Location;
import io.trino.filesystem.TrinoInput;
import io.trino.filesystem.TrinoInputFile;
import io.trino.filesystem.TrinoInputStream;
Expand All @@ -33,18 +34,18 @@
public class LocalInputFile
implements TrinoInputFile
{
private final String location;
private final Location location;
private final Path path;
private OptionalLong length = OptionalLong.empty();
private Optional<Instant> lastModified = Optional.empty();

public LocalInputFile(String location, Path path)
public LocalInputFile(Location location, Path path)
{
this.location = requireNonNull(location, "location is null");
this.path = requireNonNull(path, "path is null");
}

public LocalInputFile(String location, Path path, long length)
public LocalInputFile(Location location, Path path, long length)
{
this.location = requireNonNull(location, "location is null");
this.path = requireNonNull(path, "path is null");
Expand All @@ -54,7 +55,7 @@ public LocalInputFile(String location, Path path, long length)

public LocalInputFile(File file)
{
this(file.getPath(), file.toPath());
this(Location.of(file.toURI().toString()), file.toPath());
}

@Override
Expand All @@ -65,7 +66,7 @@ public TrinoInput newInput()
return new LocalInput(location, path.toFile());
}
catch (IOException e) {
throw new FileNotFoundException(location);
throw new FileNotFoundException(location.toString());
}
}

Expand All @@ -77,7 +78,7 @@ public TrinoInputStream newStream()
return new LocalInputStream(location, path.toFile());
}
catch (FileNotFoundException e) {
throw new FileNotFoundException(location);
throw new FileNotFoundException(location.toString());
}
}

Expand Down Expand Up @@ -119,14 +120,14 @@ public boolean exists()
}

@Override
public String location()
public Location location()
{
return location;
}

@Override
public String toString()
{
return location();
return location.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import io.trino.filesystem.Location;
import io.trino.filesystem.TrinoInputStream;

import java.io.BufferedInputStream;
Expand All @@ -30,15 +31,15 @@
class LocalInputStream
extends TrinoInputStream
{
private final String location;
private final Location location;
private final File file;
private final long fileLength;

private InputStream input;
private long position;
private boolean closed;

public LocalInputStream(String location, File file)
public LocalInputStream(Location location, File file)
throws FileNotFoundException
{
this.location = requireNonNull(location, "location is null");
Expand Down
Loading