Skip to content

Commit

Permalink
#2 guava for better filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Dec 6, 2013
1 parent 0963a5f commit a1fa921
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 34 deletions.
4 changes: 4 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@
<artifactId>aws-java-sdk</artifactId>
<version>1.6.3</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/jcabi/s3/AwsBucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.amazonaws.AmazonServiceException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.DeleteObjectRequest;
import com.amazonaws.services.s3.model.ListObjectsRequest;
import com.amazonaws.services.s3.model.ObjectListing;
import com.amazonaws.services.s3.model.S3ObjectSummary;
import com.jcabi.aspects.Immutable;
Expand Down Expand Up @@ -101,7 +102,7 @@ public void remove(@NotNull(message = "key can't be NULL")
aws.deleteObject(new DeleteObjectRequest(this.bkt, key));
Logger.info(
this,
"ocket %s removed in bucket %s",
"ocket '%s' removed in bucket '%s'",
key, this.bkt
);
} catch (AmazonServiceException ex) {
Expand All @@ -122,14 +123,18 @@ public Iterable<String> list(@NotNull(message = "prefix can't be NULL")
final String pfx) throws IOException {
try {
final AmazonS3 aws = this.regn.aws();
final ObjectListing listing = aws.listObjects(this.bkt, pfx);
final ObjectListing listing = aws.listObjects(
new ListObjectsRequest()
.withBucketName(this.bkt)
.withPrefix(pfx)
);
final Collection<String> list = new LinkedList<String>();
for (final S3ObjectSummary sum : listing.getObjectSummaries()) {
list.add(sum.getKey());
}
Logger.info(
this,
"listed %d ocket(s) with prefix '%s' in bucket %s",
"listed %d ocket(s) with prefix '%s' in bucket '%s'",
listing.getObjectSummaries().size(), pfx, this.bkt
);
return list;
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/com/jcabi/s3/AwsOcket.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public ObjectMetadata meta() throws IOException {
);
Logger.info(
this,
"metadata loaded for ocket %s in bucket %s, etag=%s",
"metadata loaded for ocket '%s' in bucket '%s', etag=%s",
this.name, this.bkt.name(), meta.getETag()
);
return meta;
Expand All @@ -123,7 +123,7 @@ public void read(@NotNull(message = "output stream can't be NULL")
input.close();
Logger.info(
this,
"loaded %d byte(s) from ocket %s in bucket %s, etag=%s",
"loaded %d byte(s) from ocket '%s' in bucket '%s', etag=%s",
bytes, this.name, this.bkt.name(),
obj.getObjectMetadata().getETag()
);
Expand Down Expand Up @@ -152,7 +152,7 @@ public void write(
);
Logger.info(
this,
"saved %d byte(s) to ocket %s in bucket %s, etag=%s",
"saved %d byte(s) to ocket '%s' in bucket '%s', etag=%s",
cnt.getByteCount(), this.name, this.bkt.name(),
result.getETag()
);
Expand Down
42 changes: 17 additions & 25 deletions src/main/java/com/jcabi/s3/Bucket.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
*/
package com.jcabi.s3;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.jcabi.aspects.Immutable;
import com.jcabi.aspects.Loggable;
import java.io.IOException;
import java.util.Iterator;
import javax.validation.constraints.NotNull;
import lombok.EqualsAndHashCode;
import lombok.ToString;
Expand Down Expand Up @@ -132,35 +134,25 @@ public void remove(final String key) throws IOException {
}
@Override
public Iterable<String> list(final String pfx) throws IOException {
// @checkstyle AnonInnerLength (50 lines)
return new Iterable<String>() {
@Override
public Iterator<String> iterator() {
final Iterator<String> list;
try {
list = Bucket.Prefixed.this.origin
.list(Bucket.Prefixed.this.extend(pfx)).iterator();
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
return new Iterator<String>() {
@Override
public boolean hasNext() {
return list.hasNext();
}
return Iterables.filter(
Iterables.transform(
this.origin.list(this.extend(pfx)),
new Function<String, String>() {
@Override
public String next() {
return list.next().substring(
public String apply(final String input) {
return input.substring(
Bucket.Prefixed.this.prefix.length()
);
}
@Override
public void remove() {
list.remove();
}
};
}
),
new Predicate<String>() {
@Override
public boolean apply(final String input) {
return !input.isEmpty();
}
}
};
);
}
/**
* Extend name with a prefix.
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/com/jcabi/s3/AwsBucketITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,17 +127,17 @@ public void listsObjectsInPrefixedBucket() throws Exception {
public void listsInPrefixedBucketWithouCollisions() throws Exception {
final Bucket bucket = this.rule.bucket();
// @checkstyle MultipleStringLiterals (1 line)
final String[] names = {"alpha/alpha.xml", "alpha.xml"};
final String[] names = {"alpha/", "alpha/beta.xml"};
for (final String name : names) {
new Ocket.Text(bucket.ocket(name)).write("whatsup");
new Ocket.Text(bucket.ocket(name)).write("");
}
final Bucket bkt = new Bucket.Prefixed(bucket, "alpha/");
try {
MatcherAssert.assertThat(
bkt.list(""),
Matchers.allOf(
Matchers.<String>iterableWithSize(1),
Matchers.hasItem("alpha.xml")
Matchers.hasItem("beta.xml")
)
);
} finally {
Expand Down

0 comments on commit a1fa921

Please sign in to comment.