Skip to content

Commit cb278da

Browse files
committed
[chore] introdued named methods
... for better navigability
1 parent 14eee43 commit cb278da

File tree

1 file changed

+147
-141
lines changed

1 file changed

+147
-141
lines changed

src/main/java/com/launchableinc/ingest/commits/CommitGraphCollector.java

Lines changed: 147 additions & 141 deletions
Original file line numberDiff line numberDiff line change
@@ -157,150 +157,70 @@ public void transfer(URL service, Authenticator authenticator, boolean enableTim
157157
// every time a new stream is needed, supply ByteArrayOutputStream, and when the data is all
158158
// written, turn around and ship that over
159159
transfer(
160-
advertised,
161-
(ContentProducer commits) -> {
162-
try {
163-
URL url = new URL(service, "collect");
164-
HttpPost request = new HttpPost(url.toExternalForm());
165-
request.setHeader("Content-Type", "application/json");
166-
request.setHeader("Content-Encoding", "gzip");
167-
request.setEntity(new EntityTemplate(os -> commits.writeTo(new GZIPOutputStream(os))));
168-
169-
if (outputAuditLog()) {
170-
System.err.printf(
171-
"AUDIT:launchable:%ssend request method:post path:%s headers:%s"
172-
+ " args:",
173-
dryRunPrefix(), url, dumpHeaderAsJson(request.getAllHeaders()));
174-
commits.writeTo(System.err);
175-
System.err.println();
176-
}
177-
if (dryRun) {
178-
return;
179-
}
180-
handleError(url, client.execute(request)).close();
181-
} catch (IOException e) {
182-
throw new UncheckedIOException(e);
183-
}
184-
},
185-
new TreeReceiver() {
186-
private final List<VirtualFile> files = new ArrayList<>();
187-
188-
private void writeJsonTo(OutputStream os) throws IOException {
189-
try (JsonGenerator w = new JsonFactory().createGenerator(os)) {
190-
w.setCodec(objectMapper);
191-
w.writeStartObject();
192-
w.writeArrayFieldStart("tree");
193-
194-
for (VirtualFile commit : files) {
195-
w.writeStartObject();
196-
w.writeFieldName("path");
197-
w.writeString(commit.path());
198-
w.writeFieldName("blob");
199-
w.writeString(commit.blob().name());
200-
w.writeEndObject();
201-
}
202-
203-
w.writeEndArray();
204-
w.writeEndObject();
205-
}
206-
}
207-
@Override
208-
public Collection<VirtualFile> response() {
209-
try {
210-
URL url = new URL(service, "collect/tree");
211-
HttpPost request = new HttpPost(url.toExternalForm());
212-
request.setHeader("Content-Type", "application/json");
213-
request.setHeader("Content-Encoding", "gzip");
214-
request.setEntity(new EntityTemplate(raw -> {
215-
try (OutputStream os = new GZIPOutputStream(raw)) {
216-
writeJsonTo(os);
217-
}
218-
}));
219-
220-
if (outputAuditLog()) {
221-
System.err.printf(
222-
"AUDIT:launchable:%ssend request method:post path:%s headers:%s args:",
223-
dryRunPrefix(), url, dumpHeaderAsJson(request.getAllHeaders()));
224-
writeJsonTo(System.err);
225-
System.err.println();
226-
}
227-
228-
// even in dry run, this method needs to execute in order to show what files we'll be collecting
229-
try (CloseableHttpResponse response = handleError(url, client.execute(request));
230-
JsonParser parser = new JsonFactory().createParser(response.getEntity().getContent())) {
231-
return select(objectMapper.readValue(parser, String[].class));
232-
}
233-
} catch (IOException e) {
234-
throw new UncheckedIOException(e);
235-
} finally {
236-
files.clear();
237-
}
238-
}
239-
240-
private List<VirtualFile> select(String[] response) {
241-
Map<String,VirtualFile> filesByPath = new HashMap<>();
242-
for (VirtualFile f : files) {
243-
filesByPath.put(f.path(), f);
244-
}
245-
246-
List<VirtualFile> selected = new ArrayList<>();
247-
for (String path : response) {
248-
VirtualFile f = filesByPath.get(path);
249-
if (f!=null) {
250-
selected.add(f);
251-
}
252-
}
160+
advertised,
161+
(ContentProducer commits) -> sendCommits(service, client, commits),
162+
new TreeReceiverImpl(service, client),
163+
(ContentProducer files) -> sendFiles(service, client, files),
164+
256);
165+
}
166+
}
253167

254-
return selected;
255-
}
168+
private void sendCommits(URL service, CloseableHttpClient client, ContentProducer commits) throws IOException {
169+
URL url = new URL(service, "collect");
170+
HttpPost request = new HttpPost(url.toExternalForm());
171+
request.setHeader("Content-Type", "application/json");
172+
request.setHeader("Content-Encoding", "gzip");
173+
request.setEntity(new EntityTemplate(os -> commits.writeTo(new GZIPOutputStream(os))));
174+
175+
if (outputAuditLog()) {
176+
System.err.printf(
177+
"AUDIT:launchable:%ssend request method:post path:%s headers:%s"
178+
+ " args:",
179+
dryRunPrefix(), url, dumpHeaderAsJson(request.getAllHeaders()));
180+
commits.writeTo(System.err);
181+
System.err.println();
182+
}
183+
if (dryRun) {
184+
return;
185+
}
186+
handleError(url, client.execute(request)).close();
187+
}
256188

257-
@Override
258-
public void accept(VirtualFile f) {
259-
files.add(f);
260-
}
261-
},
262-
(ContentProducer files) -> {
263-
try {
264-
URL url = new URL(service, "collect/files");
265-
HttpPost request = new HttpPost(url.toExternalForm());
266-
request.setHeader("Content-Type", "application/octet-stream");
267-
// no content encoding, since .tar.gz is considered content
268-
request.setEntity(new EntityTemplate(os -> files.writeTo(new GZIPOutputStream(os))));
269-
270-
if (outputAuditLog()) {
271-
System.err.printf(
272-
"AUDIT:launchable:%ssend request method:post path:%s headers:%s args:",
273-
dryRunPrefix(), url, dumpHeaderAsJson(request.getAllHeaders()));
274-
275-
// TODO: inefficient to buffer everything in memory just to read it back
276-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
277-
files.writeTo(baos);
278-
TarArchiveInputStream tar =
279-
new TarArchiveInputStream(
280-
new ByteArrayInputStream(baos.toByteArray()),
281-
"UTF-8");
282-
TarArchiveEntry entry;
283-
boolean first = true;
284-
while ((entry = tar.getNextTarEntry()) != null) {
285-
System.err.printf(entry.getName());
286-
if (first) {
287-
first = false;
288-
} else {
289-
System.err.print(", ");
290-
}
291-
}
292-
System.err.println();
293-
}
294-
if (dryRun) {
295-
return;
296-
}
297-
handleError(url, client.execute(request)).close();
298-
} catch (IOException e) {
299-
throw new UncheckedIOException(e);
300-
}
301-
},
302-
256);
189+
private void sendFiles(URL service, CloseableHttpClient client, ContentProducer files) throws IOException {
190+
URL url = new URL(service, "collect/files");
191+
HttpPost request = new HttpPost(url.toExternalForm());
192+
request.setHeader("Content-Type", "application/octet-stream");
193+
// no content encoding, since .tar.gz is considered content
194+
request.setEntity(new EntityTemplate(os -> files.writeTo(new GZIPOutputStream(os))));
195+
196+
if (outputAuditLog()) {
197+
System.err.printf(
198+
"AUDIT:launchable:%ssend request method:post path:%s headers:%s args:",
199+
dryRunPrefix(), url, dumpHeaderAsJson(request.getAllHeaders()));
200+
201+
// TODO: inefficient to buffer everything in memory just to read it back
202+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
203+
files.writeTo(baos);
204+
TarArchiveInputStream tar =
205+
new TarArchiveInputStream(
206+
new ByteArrayInputStream(baos.toByteArray()),
207+
"UTF-8");
208+
TarArchiveEntry entry;
209+
boolean first = true;
210+
while ((entry = tar.getNextTarEntry()) != null) {
211+
System.err.printf(entry.getName());
212+
if (first) {
213+
first = false;
214+
} else {
215+
System.err.print(", ");
216+
}
217+
}
218+
System.err.println();
219+
}
220+
if (dryRun) {
221+
return;
303222
}
223+
handleError(url, client.execute(request)).close();
304224
}
305225

306226
private void honorControlHeaders(HttpResponse response) {
@@ -628,4 +548,90 @@ public void close() {
628548
objectReader.close();
629549
}
630550
}
551+
552+
private class TreeReceiverImpl implements TreeReceiver {
553+
private final List<VirtualFile> files = new ArrayList<>();
554+
private final URL service;
555+
private final CloseableHttpClient client;
556+
557+
public TreeReceiverImpl(URL service, CloseableHttpClient client) {
558+
this.service = service;
559+
this.client = client;
560+
}
561+
562+
private void writeJsonTo(OutputStream os) throws IOException {
563+
try (JsonGenerator w = new JsonFactory().createGenerator(os)) {
564+
w.setCodec(objectMapper);
565+
w.writeStartObject();
566+
w.writeArrayFieldStart("tree");
567+
568+
for (VirtualFile commit : files) {
569+
w.writeStartObject();
570+
w.writeFieldName("path");
571+
w.writeString(commit.path());
572+
w.writeFieldName("blob");
573+
w.writeString(commit.blob().name());
574+
w.writeEndObject();
575+
}
576+
577+
w.writeEndArray();
578+
w.writeEndObject();
579+
}
580+
}
581+
582+
@Override
583+
public Collection<VirtualFile> response() {
584+
try {
585+
URL url = new URL(service, "collect/tree");
586+
HttpPost request = new HttpPost(url.toExternalForm());
587+
request.setHeader("Content-Type", "application/json");
588+
request.setHeader("Content-Encoding", "gzip");
589+
request.setEntity(new EntityTemplate(raw -> {
590+
try (OutputStream os = new GZIPOutputStream(raw)) {
591+
writeJsonTo(os);
592+
}
593+
}));
594+
595+
if (outputAuditLog()) {
596+
System.err.printf(
597+
"AUDIT:launchable:%ssend request method:post path:%s headers:%s args:",
598+
dryRunPrefix(), url, dumpHeaderAsJson(request.getAllHeaders()));
599+
writeJsonTo(System.err);
600+
System.err.println();
601+
}
602+
603+
// even in dry run, this method needs to execute in order to show what files we'll be collecting
604+
try (CloseableHttpResponse response = handleError(url, client.execute(request));
605+
JsonParser parser = new JsonFactory().createParser(response.getEntity().getContent())) {
606+
return select(objectMapper.readValue(parser, String[].class));
607+
}
608+
} catch (IOException e) {
609+
throw new UncheckedIOException(e);
610+
} finally {
611+
files.clear();
612+
}
613+
}
614+
615+
private List<VirtualFile> select(String[] response) {
616+
Map<String,VirtualFile> filesByPath = new HashMap<>();
617+
for (VirtualFile f : files) {
618+
filesByPath.put(f.path(), f);
619+
}
620+
621+
List<VirtualFile> selected = new ArrayList<>();
622+
for (String path : response) {
623+
VirtualFile f = filesByPath.get(path);
624+
if (f!=null) {
625+
selected.add(f);
626+
}
627+
}
628+
629+
return selected;
630+
}
631+
632+
@Override
633+
public void accept(VirtualFile f) {
634+
files.add(f);
635+
}
636+
}
631637
}

0 commit comments

Comments
 (0)