org.projectnessie.objectstoragemock.InterceptingBucket Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of nessie-object-storage-mock Show documentation
Show all versions of nessie-object-storage-mock Show documentation
Rudimentary S3, ADLS-Gen2, GCS endpoint delegating to functions to serve content.
/*
* Copyright (C) 2024 Dremio
*
* 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 org.projectnessie.objectstoragemock;
import java.util.Optional;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Stream;
public final class InterceptingBucket extends Bucket {
private final Bucket bucket;
private volatile Function> retriever;
private volatile Function> deleter;
private volatile BiFunction> updater;
private volatile BiFunction>> lister;
public InterceptingBucket(Bucket bucket) {
this.bucket = bucket;
reset();
}
public void reset() {
retriever = key -> Optional.empty();
deleter = key -> Optional.empty();
updater = (key, mode) -> Optional.empty();
lister = (prefix, offset) -> Optional.empty();
}
public void setRetriever(Function> retriever) {
this.retriever = retriever;
}
public void setDeleter(Function> deleter) {
this.deleter = deleter;
}
public void setUpdater(BiFunction> updater) {
this.updater = updater;
}
public void setLister(BiFunction>> lister) {
this.lister = lister;
}
@Override
public String creationDate() {
return bucket.creationDate();
}
@Override
public ObjectRetriever object() {
return key -> retriever.apply(key).orElseGet(() -> bucket.object().retrieve(key));
}
@Override
public Deleter deleter() {
return key -> deleter.apply(key).orElseGet(() -> bucket.deleter().delete(key));
}
@Override
public Updater updater() {
return (key, mode) ->
updater.apply(key, mode).orElseGet(() -> bucket.updater().update(key, mode));
}
@Override
public Lister lister() {
return (prefix, offset) ->
lister.apply(prefix, offset).orElseGet(() -> bucket.lister().list(prefix, offset));
}
}