Skip to content

Commit

Permalink
S3 Storage Patch (#66)
Browse files Browse the repository at this point in the history
* Patch to make the S3 associated document fetch calls recognize the fetchType argument difference between META and FULL
  • Loading branch information
millmanw authored May 10, 2022
1 parent 0dec36d commit 0955a51
Showing 1 changed file with 36 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -123,26 +123,35 @@ public void storeAssociatedDocument(AssociatedDocument doc) throws Exception {
public List<AssociatedDocument> getAssociatedDocuments(String uniqueId, FetchType fetchType) throws Exception {
if (FetchType.NONE.equals(fetchType))
return Collections.emptyList();
FindIterable<Document> found = client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + DOCUMENT_UNIQUE_ID_KEY, uniqueId));

FindIterable<Document> found = client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + DOCUMENT_UNIQUE_ID_KEY, uniqueId));
List<AssociatedDocument> docs = new ArrayList<>();
for (Document doc : found) {
docs.add(parseTOC(doc));

//Have to do it this way because the FindIterable does not implement the streams API.
if (FetchType.META.equals(fetchType)) {
for (Document doc : found) {
docs.add(buildMetadataDocument(doc));
}
} else if (FetchType.FULL.equals(fetchType)) {
for (Document doc : found) {
docs.add(buildFullDocument(doc));
}
} else {
return Collections.emptyList();
}

return docs;
}

@Override
public AssociatedDocument getAssociatedDocument(String uniqueId, String filename, FetchType fetchType) throws Exception {
if (!FetchType.NONE.equals(fetchType)) {
String uid = String.join("-", uniqueId, filename);
FindIterable<Document> found = client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + FILE_UNIQUE_ID_KEY, uid));
Document doc = found.first();
if (null != doc) {
return parseTOC(doc);
}
}
return null;
String uid = String.join("-", uniqueId, filename);

return switch (fetchType) {
case NONE, UNRECOGNIZED -> null;
case META -> buildMetadataDocument(client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + FILE_UNIQUE_ID_KEY, uid)).first());
case FULL -> buildFullDocument(client.getDatabase(dbName).getCollection(COLLECTION).find(Filters.eq("metadata." + FILE_UNIQUE_ID_KEY, uid)).first());
};
}

@Override
Expand Down Expand Up @@ -316,7 +325,7 @@ private Document parseAssociated(AssociatedDocument doc, Long length) {
return TOC;
}

private AssociatedDocument parseTOC(Document doc) throws IOException {
private AssociatedDocument.Builder parseMongo(Document doc) {
AssociatedDocument.Builder aBuilder = AssociatedDocument.newBuilder();
aBuilder.setFilename(doc.getString(FILENAME));

Expand All @@ -329,6 +338,10 @@ private AssociatedDocument parseTOC(Document doc) throws IOException {
meta.remove(FILE_UNIQUE_ID_KEY);
aBuilder.setMetadata(ZuliaUtil.mongoDocumentToByteString(meta));

return aBuilder;
}

private void addFileContents(AssociatedDocument.Builder aBuilder, Document doc) throws IOException {
Document s3Info = doc.get("s3", Document.class);
GetObjectRequest gor = GetObjectRequest.builder()
.bucket(s3Info.getString("bucket"))
Expand All @@ -339,7 +352,16 @@ private AssociatedDocument parseTOC(Document doc) throws IOException {
try (compression) {
aBuilder.setDocument(ByteString.readFrom(compression));
}
}

private AssociatedDocument buildMetadataDocument(Document doc) {
AssociatedDocument.Builder builder = parseMongo(doc);
return builder.build();
}

return aBuilder.build();
private AssociatedDocument buildFullDocument(Document doc) throws IOException {
AssociatedDocument.Builder builder = parseMongo(doc);
addFileContents(builder, doc);
return builder.build();
}
}

0 comments on commit 0955a51

Please sign in to comment.