io.datakernel.remotefs.TransformFsClient Maven / Gradle / Ivy
/*
* Copyright (C) 2015-2019 SoftIndex LLC.
*
* 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 io.datakernel.remotefs;
import io.datakernel.bytebuf.ByteBuf;
import io.datakernel.csp.ChannelConsumer;
import io.datakernel.csp.ChannelSupplier;
import io.datakernel.promise.Promise;
import org.jetbrains.annotations.NotNull;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import static java.util.stream.Collectors.toList;
final class TransformFsClient implements FsClient {
private final FsClient parent;
private final Function> into;
private final Function> from;
private final Function> globInto;
TransformFsClient(FsClient parent, Function> into, Function> from, Function> globInto) {
this.parent = parent;
this.into = into;
this.from = from;
this.globInto = globInto;
}
@Override
public Promise> upload(@NotNull String name, long offset, long revision) {
Optional transformed = into.apply(name);
if (!transformed.isPresent()) {
return Promise.ofException(BAD_PATH);
}
return parent.upload(transformed.get(), offset, revision);
}
@Override
public Promise> download(@NotNull String name, long offset, long length) {
Optional transformed = into.apply(name);
if (!transformed.isPresent()) {
return Promise.ofException(FILE_NOT_FOUND);
}
return parent.download(transformed.get(), offset, length);
}
@Override
public Promise move(@NotNull String name, @NotNull String target, long targetRevision, long tombstoneRevision) {
return renamingOp(name, target, () -> parent.move(name, target, targetRevision, tombstoneRevision));
}
@Override
public Promise copy(@NotNull String name, @NotNull String target, long targetRevision) {
return renamingOp(name, target, () -> parent.copy(name, target, targetRevision));
}
private Promise renamingOp(String filename, String newFilename, Supplier> original) {
Optional transformed = into.apply(filename);
Optional transformedNew = into.apply(newFilename);
if (!transformed.isPresent() || !transformedNew.isPresent()) {
return Promise.ofException(BAD_PATH);
}
return original.get();
}
@Override
public Promise> listEntities(@NotNull String glob) {
return parent.listEntities(globInto.apply(glob).orElse("**"))
.map(transformList(glob));
}
@Override
public Promise> list(@NotNull String glob) {
return parent.list(globInto.apply(glob).orElse("**"))
.map(transformList(glob));
}
private Function, List> transformList(String glob) {
Predicate pred = RemoteFsUtils.getGlobStringPredicate(glob);
return list -> list.stream()
.map(meta ->
from.apply(meta.getName())
.map(name -> FileMetadata.of(name, meta.getSize(), meta.getTimestamp(), meta.getRevision())))
.filter(meta -> meta.isPresent() && pred.test(meta.get().getName()))
.map(Optional::get)
.collect(toList());
}
@Override
public Promise getMetadata(@NotNull String name) {
Optional transformed = into.apply(name);
return transformed.map(s ->
parent.getMetadata(s)
.map(meta -> {
if (meta == null) {
return null;
}
return from.apply(meta.getName())
.map(meta::withName)
.orElse(null);
})).orElse(Promise.of(null));
}
@Override
public Promise ping() {
return parent.ping();
}
@Override
public Promise delete(@NotNull String name, long revision) {
Optional transformed = into.apply(name);
if (!transformed.isPresent()) {
return Promise.complete();
}
return parent.delete(transformed.get(), revision);
}
@Override
public FsClient transform(@NotNull Function> into, @NotNull Function> from, @NotNull Function> globInto) {
if (into == this.from && from == this.into) { // huh
return parent;
}
return new TransformFsClient(parent,
name -> into.apply(name).flatMap(this.into),
name -> this.from.apply(name).flatMap(from),
name -> globInto.apply(name).flatMap(this.globInto)
);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy