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

S3 Storage Patch #66

Merged
merged 7 commits into from
May 10, 2022
Merged
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 @@ -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();
}
}