Repository navigation
Conversation
eppleton
left a comment
There was a problem hiding this comment.
Making toPath work for every FileObject sounds useful, and the shared PathTestHid suite is a good base. Some observations from reading the code and checking the JDK and bootstrap sources; I didn't build or run the PR.
Most of the points below share one root: every PathTestHid test derives all paths from a single toPath() call, and on the fallback (non-disk) side the suite currently only runs on JarFileSystem, which is read-only. So equality across separate toPath() calls, writable virtual filesystems (MemoryFileSystem, MultiFileSystem/layers) and watch events are not exercised yet. Your "other filesystems" checklist item would catch several of these.
On the API side: toPath on master is only used from tests, so the new "never null" contract shouldn't break anything in the repo, but platform applications may use null as "not on disk". An apichanges.xml entry and a spec version bump would make that visible (I see it's on your checklist).
FileUtil.toPath to never return null
| */ | ||
| public static Path toPath(FileObject fo) { | ||
| Parameters.notNull("fo", fo); | ||
| File f = toFile(fo); |
There was a problem hiding this comment.
Maybe check for (Path) fo.getAttribute("java.nio.Path") first, and make trivial change to masterfs to return directly? Could allow for better optimization from file system implementations in future and/or option to switch in nio2 based memory and zip filesystems.
Is conversion via attribute documented?
There was a problem hiding this comment.
toFile(fo)check was put here by @lkishalmi and I am trying to not modify it.- it works fine for
LocalFileSystemand masterfs to provide the underlayingPathimplementations - it works less well for
MultiFileSystemwhich can also have underlaying impl- there I will have to disable the delegation
- but that can be done locally inside the module now ...
- ... without any visible API
Is conversion via attribute documented?
- probably it is not
- it should be listed in this table
check for (Path) fo.getAttribute("java.nio.Path")
- not in this PR (unless necessary), but for the future I'd like to do something bigger:
- let us add
FileObject implements Path! - then any
FileObjectwill immediately be usable with any JDK path oriented API
There was a problem hiding this comment.
it should be listed in this table
Yes, I checked there before commenting. Would have thought there and/or in the Javadoc for the method?
...for the future I'd like to do something bigger:
let us addFileObject implements Path!
Every FileObject having a Path sounds great. Every FileObject being a Path might not be so good an idea. Paths from the same provider are special cased (eg. in Files::copy and the providers expect to be able to cast to implementation (eg. UnixPath). That means that a FileObject from masterfs would bypass the optimal same-provider pathways.
|
Thanks! Good changes in the right direction. We need to look at replacing uses of |
… virtual-path issues they exposed.
…ots and selection
eppleton
left a comment
There was a problem hiding this comment.
Thanks for the new tests and the fixes to relative paths. getRoot/getParent returning null for relative paths, normalize keeping leading .., and resolve(String) honouring absolute arguments all match the Path contract now. The WatchService close handling also looks right to me: the close marker is passed on so every blocked take() wakes up.
A few notes on the new FileChannel emulation, from reading the code (I haven't run it); details inline. One earlier point is still open: toUri() still calls fileObject.toURI() without a null check (see my reply in the thread on NioPathForFileObject).
| return count; | ||
| } | ||
|
|
||
| private void update(long at, ByteBuffer src, long truncate) throws IOException { |
There was a problem hiding this comment.
Both directions are quadratic in the file size.
- Writes: every
writecallsfile.asBytes(), copies the whole content and writes the whole file back throughgetOutputStream. Writing a 10 MB file in 8 KB chunks rewrites the file about 1,250 times, and each rewrite fires afileChangedevent to every listener, including the newWatchService. - Reads:
readImpl(line 192) opens a newInputStreamfor every read andskipNBytesto the position, reading at most 8 KB. For an entry in a JAR filesystem, that means inflating the entry again from the start for each chunk.
The Javadoc already says the virtual channel holds the FileObject lock until close. Keeping the content in memory while the channel is open and writing it once on close (and on force) would match that model. Keeping an open stream for sequential reads would avoid re-skipping.
| } | ||
|
|
||
|
|
||
| private record State(long position, Thread operationOwner) { |
There was a problem hiding this comment.
All state transitions synchronize and wait/notifyAll on State.class, which is one monitor shared by every NioFileObjectChannel in the JVM. Unrelated channels on different files therefore contend on the same lock, and every finished operation wakes all waiters of all channels. A monitor per channel (e.g. the channel instance or a private lock object) would keep the same semantics without the global coupling. It would probably also allow a simpler scheme than the snapshot/retry logic in completeRead, which fits the "less moving pieces" direction of the recent commits.
| @Override | ||
| public void force(boolean metaData) throws IOException { | ||
| checkOpen(); | ||
| throw new UnsupportedOperationException("Forcing virtual files to storage is not supported"); |
There was a problem hiding this comment.
FileChannel.force only declares IOException, and callers that write and then force(true) (a common pattern for safe saves, e.g. in Lucene) don't expect UnsupportedOperationException. Since every write is already handed to the FileObject immediately, force can simply return after checkOpen(). If writes get buffered as suggested above, force would be the place to flush them.
| public java.nio.channels.FileLock tryLock(long position, long size, boolean shared) throws IOException { | ||
| checkOpen(); | ||
| if (fileLock == null) { | ||
| throw new NonWritableChannelException(); |
There was a problem hiding this comment.
For a read-only channel this throws NonWritableChannelException even when shared == true. The JDK only requires a writable channel for exclusive locks; a shared lock on a readable channel is allowed. The new Javadoc in FileUtil only describes the writable case, so either return a (no-op) shared lock here or document that read-only virtual channels cannot be locked.
| throw new NoSuchFileException(path.toString()); | ||
| } | ||
| } | ||
| return new WritableFileObjectByteChannel(fo); |
There was a problem hiding this comment.
With newFileChannel there are now two independent write implementations with different semantics. The writable newByteChannel path (WritableFileObjectByteChannel):
- ignores
TRUNCATE_EXISTING: the old content is always loaded, so writing a shorter text leaves the old tail in place; - ignores
APPEND: the position starts at 0; - ignores
CREATE_NEW: there is noFileAlreadyExistsException; - takes no
FileObjectlock, unlike what the newFileUtilJavadoc describes.
Files.newByteChannel(path, WRITE, ...) and FileChannel.open(path, WRITE, ...) therefore behave differently on the same virtual path. Since FileChannel is a SeekableByteChannel, newByteChannel could simply return the new channel for all option sets, leaving one implementation to maintain and test.
078b1cd to
dc6a31a
Compare
FileUtil.toPathLocalFileSystemFileObjectnullLocalFileSystemTestand make it pass - done in 1e50301Path<->FileObjectinteractions