dcapwell commented on code in PR #2299: URL: https://github.com/apache/cassandra/pull/2299#discussion_r1186516814
########## test/unit/org/apache/cassandra/io/filesystem/ListenableFileSystem.java: ########## @@ -0,0 +1,541 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.cassandra.io.filesystem; + +import java.io.EOFException; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.UncheckedIOException; +import java.net.URI; +import java.nio.ByteBuffer; +import java.nio.MappedByteBuffer; +import java.nio.channels.AsynchronousFileChannel; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; +import java.nio.channels.SeekableByteChannel; +import java.nio.channels.WritableByteChannel; +import java.nio.file.FileSystem; +import java.nio.file.FileSystems; +import java.nio.file.OpenOption; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.nio.file.attribute.FileAttribute; +import java.nio.file.spi.FileSystemProvider; +import java.util.Arrays; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.memory.MemoryUtil; + +public class ListenableFileSystem extends ForwardingFileSystem +{ + public interface Listener + { + } + + public interface OnOpen extends Listener + { + default void preOpen(Path path, Set<? extends OpenOption> options, FileAttribute<?>[] attrs) throws IOException + { + } + + void postOpen(Path path, Set<? extends OpenOption> options, FileAttribute<?>[] attrs, FileChannel channel) throws IOException; + } + + public interface OnRead extends Listener + { + default void preRead(Path path, FileChannel channel, long position, ByteBuffer dst) throws IOException + { + } + + void postRead(Path path, FileChannel channel, long position, ByteBuffer dst, int read) throws IOException; + } + + public interface OnTransferTo extends Listener + { + default void preTransferTo(Path path, FileChannel channel, long position, long count, WritableByteChannel target) throws IOException + { + } + + void postTransferTo(Path path, FileChannel channel, long position, long count, WritableByteChannel target, long transfered) throws IOException; + } + + public interface OnTransferFrom extends Listener + { + default void preTransferFrom(Path path, FileChannel channel, ReadableByteChannel src, long position, long count) throws IOException + { + } + + void postTransferFrom(Path path, FileChannel channel, ReadableByteChannel src, long position, long count, long transfered) throws IOException; + } + + public interface OnWrite extends Listener + { + void preWrite(Path path, FileChannel channel, long position, ByteBuffer src) throws IOException; + + void postWrite(Path path, FileChannel channel, long position, ByteBuffer src, int wrote) throws IOException; + } + + public interface OnChannelMeta extends Listener + { + default void prePosition(Path path, FileChannel channel, long position, long newPosition) throws IOException + { + } + + default void postPosition(Path path, FileChannel channel, long position, long newPosition) throws IOException + { + } + + default void preTruncate(Path path, FileChannel channel, long size, long targetSize) throws IOException + { + } + + default void postTruncate(Path path, FileChannel channel, long size, long targetSize, long newSize) throws IOException + { + } + + default void preForce(Path path, FileChannel channel, boolean metaData) throws IOException + { + } + + default void postForce(Path path, FileChannel channel, boolean metaData) throws IOException + { + } + } + + public interface Unsubscribable extends AutoCloseable + { + @Override + void close(); + } + + private final List<OnOpen> onOpen = new CopyOnWriteArrayList<>(); + private final List<OnTransferTo> onTransferTo = new CopyOnWriteArrayList<>(); + private final List<OnRead> onRead = new CopyOnWriteArrayList<>(); + private final List<OnWrite> onWrite = new CopyOnWriteArrayList<>(); + private final List<OnTransferFrom> onTransferFrom = new CopyOnWriteArrayList<>(); + private final List<OnChannelMeta> onChannelMeta = new CopyOnWriteArrayList<>(); + private final List<List<? extends Listener>> lists = Arrays.asList(onOpen, onRead, onTransferTo, onWrite, onTransferFrom, onChannelMeta); + private final ListenableFileSystemProvider provider; + + public ListenableFileSystem(FileSystem delegate) + { + super(delegate); + this.provider = new ListenableFileSystemProvider(super.provider()); + } + + public Unsubscribable listen(Listener listener) + { + if (listener instanceof OnOpen) + onOpen.add((OnOpen) listener); + if (listener instanceof OnRead) + onRead.add((OnRead) listener); + if (listener instanceof OnTransferTo) + onTransferTo.add((OnTransferTo) listener); + if (listener instanceof OnWrite) + onWrite.add((OnWrite) listener); + if (listener instanceof OnTransferFrom) + onTransferFrom.add((OnTransferFrom) listener); + if (listener instanceof OnChannelMeta) + onChannelMeta.add((OnChannelMeta) listener); + return () -> remove(listener); + } + + public void remove(Listener listener) + { + lists.forEach(l -> l.remove(listener)); + } + + public void clearListeners() + { + lists.forEach(List::clear); + } + + private interface ListenerAction<T> + { + void accept(T value) throws IOException; + } + + private <T> void notifyListeners(List<T> listeners, ListenerAction<T> fn) throws IOException + { + for (T listener : listeners) + fn.accept(listener); + } + + @Override + protected Path wrap(Path p) + { + return p instanceof ListenablePath ? p : new ListenablePath(p); + } + + @Override + protected Path unwrap(Path p) + { + return p instanceof ListenablePath ? ((ListenablePath) p).delegate : p; + } + + @Override + public ListenableFileSystemProvider provider() + { + return provider; + } + + + private class ListenableFileSystemProvider extends ForwardingFileSystemProvider + { + ListenableFileSystemProvider(FileSystemProvider delegate) + { + super(delegate); + } + + @Override + protected Path wrap(Path a) + { + return ListenableFileSystem.this.wrap(a); + } + + @Override + protected Path unwrap(Path p) + { + return ListenableFileSystem.this.unwrap(p); + } + + @Override + public OutputStream newOutputStream(Path path, OpenOption... options) throws IOException + { + int len = options.length; + Set<OpenOption> opts = new HashSet<>(len + 3); + if (len == 0) + { + opts.add(StandardOpenOption.CREATE); + opts.add(StandardOpenOption.TRUNCATE_EXISTING); + } + else + { + for (OpenOption opt : options) + { + if (opt == StandardOpenOption.READ) + throw new IllegalArgumentException("READ not allowed"); + opts.add(opt); + } + } + opts.add(StandardOpenOption.WRITE); + return Channels.newOutputStream(newFileChannel(path, opts)); + } + + @Override + public InputStream newInputStream(Path path, OpenOption... options) throws IOException + { + if (options.length > 0) Review Comment: was copy/paste from java... prob to avoid iterators? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]

