Skip to content

Commit 2d266ce

Browse files
author
Denver
authored
Merge pull request #291 from rpmoore/folder_filter
Creating a map and filter that will take a list of Contents, Filter t…
2 parents bf1711c + 7acca47 commit 2d266ce

File tree

6 files changed

+204
-5
lines changed

6 files changed

+204
-5
lines changed

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpers.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import com.spectralogic.ds3client.models.Contents;
2222
import com.spectralogic.ds3client.models.bulk.Ds3Object;
2323
import com.spectralogic.ds3client.serializer.XmlProcessingException;
24+
import com.spectralogic.ds3client.utils.Predicate;
2425

2526
import java.io.IOException;
2627
import java.nio.channels.SeekableByteChannel;
2728
import java.nio.file.Path;
2829
import java.security.SignatureException;
30+
import java.util.Iterator;
2931
import java.util.Map;
3032
import java.util.UUID;
3133

@@ -277,6 +279,14 @@ public abstract Iterable<Contents> listObjects(final String bucket, final String
277279
*/
278280
public abstract Iterable<Ds3Object> removePrefixFromDs3ObjectsList(final Iterable<Ds3Object> objectsList, final String prefix);
279281

282+
/**
283+
* Filter out folders from the object list. Use this method when piping the list of objects from listObjects to a bulk job.
284+
* @param objects
285+
* @return
286+
*/
287+
public abstract Iterable<Ds3Object> toDs3Iterable(final Iterable<Contents> objects);
288+
289+
public abstract Iterable<Ds3Object> toDs3Iterable(final Iterable<Contents> objects, final Predicate<Contents> filter);
280290
/**
281291
* Strip prefix from the beginning of objectName. If objectName does not start with prefix, return objectName unmodified.
282292
* @param objectName

ds3-sdk/src/main/java/com/spectralogic/ds3client/helpers/Ds3ClientHelpersImpl.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
* ******************************************************************************
3-
* Copyright 2014-2015 Spectra Logic Corporation. All Rights Reserved.
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
44
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
55
* this file except in compliance with the License. A copy of the License is located at
66
*
@@ -15,6 +15,8 @@
1515

1616
package com.spectralogic.ds3client.helpers;
1717

18+
import com.google.common.base.Function;
19+
import com.google.common.collect.FluentIterable;
1820
import com.google.common.collect.ImmutableMultimap;
1921
import com.google.common.collect.Lists;
2022
import com.spectralogic.ds3client.Ds3Client;
@@ -29,9 +31,11 @@
2931
import com.spectralogic.ds3client.models.common.Range;
3032
import com.spectralogic.ds3client.networking.FailedRequestException;
3133
import com.spectralogic.ds3client.serializer.XmlProcessingException;
34+
import com.spectralogic.ds3client.utils.Predicate;
3235
import org.slf4j.Logger;
3336
import org.slf4j.LoggerFactory;
3437

38+
import javax.annotation.Nullable;
3539
import java.io.IOException;
3640
import java.nio.file.FileVisitResult;
3741
import java.nio.file.Files;
@@ -135,10 +139,7 @@ private Ds3ClientHelpers.Job innerStartReadAllJob(final String bucket, final Rea
135139
throws SignatureException, IOException, XmlProcessingException {
136140
final Iterable<Contents> contentsList = this.listObjects(bucket);
137141

138-
final List<Ds3Object> ds3Objects = new ArrayList<>();
139-
for (final Contents objectApiBean : contentsList) {
140-
ds3Objects.add(new Ds3Object(objectApiBean.getKey()));
141-
}
142+
final Iterable<Ds3Object> ds3Objects = this.toDs3Iterable(contentsList, FolderNameFilter.filter());
142143

143144
return this.startReadJob(bucket, ds3Objects, options);
144145
}
@@ -268,4 +269,34 @@ public Iterable<Ds3Object> removePrefixFromDs3ObjectsList(final Iterable<Ds3Obje
268269
}
269270
return newObjectsList;
270271
}
272+
273+
@Override
274+
public Iterable<Ds3Object> toDs3Iterable(final Iterable<Contents> objects) {
275+
return toDs3Iterable(objects, null);
276+
}
277+
278+
@Override
279+
public Iterable<Ds3Object> toDs3Iterable(final Iterable<Contents> objects, final Predicate<Contents> filter) {
280+
return FluentIterable.from(objects).filter(new com.google.common.base.Predicate<Contents>() {
281+
@Override
282+
public boolean apply(@Nullable final Contents input) {
283+
return input == null;
284+
}
285+
}).filter(new com.google.common.base.Predicate<Contents>() {
286+
@Override
287+
public boolean apply(@Nullable final Contents input) {
288+
if (filter != null) {
289+
return filter.test(input);
290+
} else {
291+
return false; // do not filter anything if filter is null
292+
}
293+
}
294+
}).transform(new Function<Contents, Ds3Object>() {
295+
@Nullable
296+
@Override
297+
public Ds3Object apply(@Nullable final Contents input) {
298+
return new Ds3Object(input.getKey(), input.getSize());
299+
}
300+
});
301+
}
271302
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.helpers;
17+
18+
import com.spectralogic.ds3client.models.Contents;
19+
import com.spectralogic.ds3client.utils.Predicate;
20+
21+
public class FolderNameFilter implements Predicate<Contents> {
22+
23+
@Override
24+
public boolean test(final Contents contents) {
25+
return contents.getKey().endsWith("/");
26+
}
27+
28+
public static Predicate<Contents> filter() {
29+
return new FolderNameFilter();
30+
}
31+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.utils;
17+
18+
import java.io.IOException;
19+
import java.nio.ByteBuffer;
20+
import java.nio.channels.SeekableByteChannel;
21+
22+
public class EmptySeekableByteChannel implements SeekableByteChannel {
23+
private boolean open = true;
24+
@Override
25+
public int read(final ByteBuffer dst) throws IOException {
26+
return 0;
27+
}
28+
29+
@Override
30+
public int write(final ByteBuffer src) throws IOException {
31+
return 0;
32+
}
33+
34+
@Override
35+
public long position() throws IOException {
36+
return 0;
37+
}
38+
39+
@Override
40+
public SeekableByteChannel position(final long newPosition) throws IOException {
41+
return this;
42+
}
43+
44+
@Override
45+
public long size() throws IOException {
46+
return 0;
47+
}
48+
49+
@Override
50+
public SeekableByteChannel truncate(final long size) throws IOException {
51+
return this;
52+
}
53+
54+
@Override
55+
public boolean isOpen() {
56+
return open;
57+
}
58+
59+
@Override
60+
public void close() throws IOException {
61+
open = false;
62+
}
63+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.utils;
17+
18+
public interface Predicate<T> {
19+
boolean test(T t);
20+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* ******************************************************************************
3+
* Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved.
4+
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use
5+
* this file except in compliance with the License. A copy of the License is located at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* or in the "license" file accompanying this file.
10+
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
11+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
12+
* specific language governing permissions and limitations under the License.
13+
* ****************************************************************************
14+
*/
15+
16+
package com.spectralogic.ds3client.helpers;
17+
18+
import com.spectralogic.ds3client.models.Contents;
19+
import org.junit.Test;
20+
21+
import static org.junit.Assert.assertFalse;
22+
import static org.junit.Assert.assertTrue;
23+
24+
public class FolderNameFilter_Test {
25+
@Test
26+
public void filterTest() {
27+
final FolderNameFilter filter = new FolderNameFilter();
28+
29+
final Contents contents = new Contents();
30+
contents.setKey("name/");
31+
32+
assertTrue(filter.test(contents));
33+
}
34+
35+
@Test
36+
public void noFilterTest() {
37+
final FolderNameFilter filter = new FolderNameFilter();
38+
39+
final Contents contents = new Contents();
40+
contents.setKey("name");
41+
42+
assertFalse(filter.test(contents));
43+
}
44+
}

0 commit comments

Comments
 (0)