Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache file id regardless of versioning setting. #14965

Merged
merged 2 commits into from
Aug 4, 2023
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 @@ -22,14 +22,12 @@
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathAttributes;
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.VersioningConfiguration;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.AttributesAdapter;
import ch.cyberduck.core.features.AttributesFinder;
import ch.cyberduck.core.features.Write;
import ch.cyberduck.core.io.Checksum;
import ch.cyberduck.core.preferences.HostPreferences;
import ch.cyberduck.core.transfer.TransferStatus;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -58,16 +56,10 @@ public class B2AttributesFinderFeature implements AttributesFinder, AttributesAd

private final B2Session session;
private final B2VersionIdProvider fileid;
private final VersioningConfiguration versioning;

public B2AttributesFinderFeature(final B2Session session, final B2VersionIdProvider fileid) {
this(session, fileid, new VersioningConfiguration(new HostPreferences(session.getHost()).getBoolean("b2.listing.versioning.enable")));
}

public B2AttributesFinderFeature(final B2Session session, final B2VersionIdProvider fileid, final VersioningConfiguration versioning) {
this.session = session;
this.fileid = fileid;
this.versioning = versioning;
}

@Override
Expand Down Expand Up @@ -163,9 +155,7 @@ protected PathAttributes toAttributes(final B2FileInfoResponse response) {
if(!response.getFileInfo().isEmpty()) {
attributes.setMetadata(new HashMap<>(response.getFileInfo()));
}
if(versioning.isEnabled()) {
attributes.setVersionId(response.getFileId());
}
attributes.setVersionId(response.getFileId());
final long timestamp = response.getUploadTimestamp();
if(response.getFileInfo().containsKey(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS)) {
final String value = response.getFileInfo().get(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS);
Expand Down Expand Up @@ -207,9 +197,7 @@ protected PathAttributes toAttributes(final B2FileResponse response) {
if(!response.getFileInfo().isEmpty()) {
attributes.setMetadata(new HashMap<>(response.getFileInfo()));
}
if(versioning.isEnabled()) {
attributes.setVersionId(response.getFileId());
}
attributes.setVersionId(response.getFileId());
final long timestamp = response.getUploadTimestamp();
if(response.getFileInfo().containsKey(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS)) {
final String value = response.getFileInfo().get(X_BZ_INFO_SRC_LAST_MODIFIED_MILLIS);
Expand Down Expand Up @@ -241,7 +229,7 @@ protected PathAttributes toAttributes(final B2FileResponse response) {

protected PathAttributes toAttributes(final B2BucketResponse response) {
final PathAttributes attributes = new PathAttributes();
attributes.setFileId(response.getBucketId());
attributes.setVersionId(response.getBucketId());
attributes.setRegion(response.getBucketType().name());
switch(response.getBucketType()) {
case allPublic:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import ch.cyberduck.core.PasswordCallback;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.VersioningConfiguration;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.Delete;
import ch.cyberduck.core.preferences.HostPreferences;
import ch.cyberduck.core.transfer.TransferStatus;

import org.apache.logging.log4j.LogManager;
Expand All @@ -40,10 +42,16 @@ public class B2DeleteFeature implements Delete {

private final B2Session session;
private final B2VersionIdProvider fileid;
private final VersioningConfiguration versioning;

public B2DeleteFeature(final B2Session session, final B2VersionIdProvider fileid) {
this(session, fileid, new VersioningConfiguration(new HostPreferences(session.getHost()).getBoolean("b2.listing.versioning.enable")));
}

public B2DeleteFeature(final B2Session session, final B2VersionIdProvider fileid, final VersioningConfiguration versioning) {
this.session = session;
this.fileid = fileid;
this.versioning = versioning;
}

@Override
Expand Down Expand Up @@ -79,7 +87,7 @@ public void delete(final Map<Path, TransferStatus> files, final PasswordCallback
}
else if(file.isFile()) {
try {
if(null == file.attributes().getVersionId()) {
if(!versioning.isEnabled() || null == file.attributes().getVersionId()) {
// Add hide marker
if(log.isDebugEnabled()) {
log.debug(String.format("Add hide marker %s of %s", file.attributes().getVersionId(), file));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ private String createPrefix(final Path directory) {

private Marker parse(final Path directory, final AttributedList<Path> objects,
final B2ListFilesResponse response, final Map<String, Long> revisions) {
final B2AttributesFinderFeature attr = new B2AttributesFinderFeature(session, fileid, versioning);
final B2AttributesFinderFeature attr = new B2AttributesFinderFeature(session, fileid);
for(B2FileInfoResponse info : response.getFiles()) {
if(StringUtils.equals(PathNormalizer.name(info.getFileName()), B2PathContainerService.PLACEHOLDER)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import ch.cyberduck.core.DefaultIOExceptionMappingService;
import ch.cyberduck.core.Path;
import ch.cyberduck.core.PathContainerService;
import ch.cyberduck.core.VersioningConfiguration;
import ch.cyberduck.core.exception.BackgroundException;
import ch.cyberduck.core.exception.NotfoundException;
import ch.cyberduck.core.features.VersionIdProvider;
import ch.cyberduck.core.preferences.HostPreferences;

import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.LogManager;
Expand All @@ -41,44 +39,35 @@ public class B2VersionIdProvider extends CachingVersionIdProvider implements Ver

private final PathContainerService containerService = new B2PathContainerService();
private final B2Session session;
private final VersioningConfiguration versioning;

public B2VersionIdProvider(final B2Session session) {
this(session, new VersioningConfiguration(new HostPreferences(session.getHost()).getBoolean("b2.listing.versioning.enable")));
}

public B2VersionIdProvider(final B2Session session, final VersioningConfiguration versioning) {
super(session.getCaseSensitivity());
this.session = session;
this.versioning = versioning;
}

@Override
public String getVersionId(final Path file) throws BackgroundException {
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
if(log.isDebugEnabled()) {
log.debug(String.format("Return version %s from attributes for file %s", file.attributes().getVersionId(), file));
}
return file.attributes().getVersionId();
}
final String cached = super.getVersionId(file);
if(cached != null) {
if(log.isDebugEnabled()) {
log.debug(String.format("Return cached versionid %s for file %s", cached, file));
}
return cached;
}
try {
if(containerService.isContainer(file)) {
if(StringUtils.isNotBlank(file.attributes().getFileId())) {
return file.attributes().getFileId();
}
final B2BucketResponse info = session.getClient().listBucket(file.getName());
if(null == info) {
throw new NotfoundException(file.getAbsolute());
}
// Cache in file attributes
return info.getBucketId();
}
if(StringUtils.isNotBlank(file.attributes().getVersionId())) {
if(log.isDebugEnabled()) {
log.debug(String.format("Return version %s from attributes for file %s", file.attributes().getVersionId(), file));
}
return file.attributes().getVersionId();
}
final String cached = super.getVersionId(file);
if(cached != null) {
if(log.isDebugEnabled()) {
log.debug(String.format("Return cached versionid %s for file %s", cached, file));
}
return cached;
return this.cache(file, info.getBucketId());
}
// Files that have been hidden will not be returned
final B2ListFilesResponse response = session.getClient().listFileNames(
Expand All @@ -98,12 +87,4 @@ public String getVersionId(final Path file) throws BackgroundException {
throw new DefaultIOExceptionMappingService().map(e);
}
}

@Override
public String cache(final Path file, final String id) {
if(versioning.isEnabled()) {
return super.cache(file, id);
}
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,16 @@ public class B2WriteFeature extends AbstractHttpWriteFeature<BaseB2Response> imp
private static final Logger log = LogManager.getLogger(B2WriteFeature.class);

private final PathContainerService containerService
= new B2PathContainerService();
= new B2PathContainerService();

private final B2Session session;
private final B2VersionIdProvider fileid;

private final ThreadLocal<B2GetUploadUrlResponse> urls
= new ThreadLocal<>();
= new ThreadLocal<>();

public B2WriteFeature(final B2Session session, final B2VersionIdProvider fileid) {
this(session, fileid, new B2AttributesFinderFeature(session, fileid));
}

public B2WriteFeature(final B2Session session, final B2VersionIdProvider fileid, final B2AttributesFinderFeature attributes) {
super(attributes);
super(new B2AttributesFinderFeature(session, fileid));
this.session = session;
this.fileid = fileid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,9 @@ public void testListRevisions() throws Exception {

@Test
public void testListRevisionsNoVersioning() throws Exception {
final B2VersionIdProvider fileid = new B2VersionIdProvider(session, VersioningConfiguration.empty());
final B2VersionIdProvider fileid = new B2VersionIdProvider(session);
final Path bucket = new B2DirectoryFeature(session, fileid,
new B2WriteFeature(session, fileid, new B2AttributesFinderFeature(session, fileid,
VersioningConfiguration.empty()))).mkdir(new Path(
new B2WriteFeature(session, fileid)).mkdir(new Path(
String.format("test-%s", new AsciiRandomStringService().random()), EnumSet.of(Path.Type.directory, Path.Type.volume)), new TransferStatus());
final String name = new AsciiRandomStringService().random();
final Path file = new Path(bucket, name, EnumSet.of(Path.Type.file));
Expand All @@ -260,18 +259,16 @@ public void testListRevisionsNoVersioning() throws Exception {
final TransferStatus status = new TransferStatus();
status.setLength(content.length);
status.setChecksum(new SHA1ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid, new B2AttributesFinderFeature(session, fileid,
VersioningConfiguration.empty())).write(file, status, new DisabledConnectionCallback());
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(file, status, new DisabledConnectionCallback());
IOUtils.write(content, out);
out.close();
final B2FileResponse response = (B2FileResponse) out.getStatus();
assertNotNull(response.getFileId());
assertNull(file.attributes().getVersionId());
assertEquals(response.getFileId(), file.attributes().getVersionId());
final AttributedList<Path> list = new B2ObjectListService(session, fileid, 10,
VersioningConfiguration.empty()).list(bucket, new DisabledListProgressListener());
assertTrue(list.contains(file));
assertNull(list.find(new SimplePathPredicate(file)).attributes().getRevision());
assertNull(list.find(new SimplePathPredicate(file)).attributes().getVersionId());
assertEquals(response.getFileId(), list.find(new SimplePathPredicate(file)).attributes().getVersionId());
assertEquals(content.length, list.find(new SimplePathPredicate(file)).attributes().getSize());
assertEquals(bucket, list.find(new SimplePathPredicate(file)).getParent());
}
Expand All @@ -281,23 +278,21 @@ public void testListRevisionsNoVersioning() throws Exception {
final TransferStatus status = new TransferStatus();
status.setLength(content.length);
status.setChecksum(new SHA1ChecksumCompute().compute(new ByteArrayInputStream(content), status));
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid, new B2AttributesFinderFeature(session, fileid,
VersioningConfiguration.empty())).write(file, status, new DisabledConnectionCallback());
final HttpResponseOutputStream<BaseB2Response> out = new B2WriteFeature(session, fileid).write(file, status, new DisabledConnectionCallback());
IOUtils.write(content, out);
out.close();
final B2FileResponse response = (B2FileResponse) out.getStatus();
assertNotNull(response.getFileId());
assertNull(file.attributes().getVersionId());
assertEquals(response.getFileId(), file.attributes().getVersionId());
final AttributedList<Path> list = new B2ObjectListService(session, fileid, 10,
VersioningConfiguration.empty()).list(bucket, new DisabledListProgressListener());
assertEquals(1, list.size());
assertTrue(list.contains(file));
assertEquals(bucket, list.get(file).getParent());
assertNull(list.get(file).attributes().getRevision());
assertNull(list.find(new SimplePathPredicate(file)).attributes().getVersionId());
assertEquals(response.getFileId(), list.find(new SimplePathPredicate(file)).attributes().getVersionId());
}
// Add hide marker
new B2DeleteFeature(session, fileid).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
new B2DeleteFeature(session, fileid, VersioningConfiguration.empty()).delete(Collections.singletonList(file), new DisabledLoginCallback(), new Delete.DisabledCallback());
assertTrue(new B2ObjectListService(session, fileid, 1, VersioningConfiguration.empty()).list(bucket, new DisabledListProgressListener()).isEmpty());
assertFalse(new B2FindFeature(session, fileid).find(file));
assertFalse(new DefaultFindFeature(session).find(file));
Expand Down