blob: ae8413f226ed2b5a29bc5c00ec7e0b98a1856466 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.restapi;
import com.yahoo.document.Document;
import com.yahoo.document.DocumentId;
import com.yahoo.document.json.JsonWriter;
import com.yahoo.documentapi.DumpVisitorDataHandler;
import org.apache.commons.lang3.exception.ExceptionUtils;
import java.nio.charset.StandardCharsets;
/**
* Handling data from visit.
*
* @author dybis
*/
class LocalDataVisitorHandler extends DumpVisitorDataHandler {
StringBuilder commaSeparatedJsonDocuments = new StringBuilder();
final StringBuilder errors = new StringBuilder();
private boolean isFirst = true;
private final Object monitor = new Object();
String getErrors() {
return errors.toString();
}
String getCommaSeparatedJsonDocuments() {
return commaSeparatedJsonDocuments.toString();
}
@Override
public void onDocument(Document document, long l) {
try {
final String docJson = new String(JsonWriter.toByteArray(document), StandardCharsets.UTF_8.name());
synchronized (monitor) {
if (!isFirst) {
commaSeparatedJsonDocuments.append(",");
}
isFirst = false;
commaSeparatedJsonDocuments.append(docJson);
}
} catch (Exception e) {
synchronized (monitor) {
errors.append(ExceptionUtils.getStackTrace(e)).append("\n");
}
}
}
// TODO: Not sure if we should support removal or not. Do nothing here maybe?
@Override
public void onRemove(DocumentId documentId) {
try {
final String removeJson = new String(JsonWriter.documentRemove(documentId), StandardCharsets.UTF_8.name());
synchronized (monitor) {
if (!isFirst) {
commaSeparatedJsonDocuments.append(",");
}
isFirst = false;
commaSeparatedJsonDocuments.append(removeJson);
}
} catch (Exception e) {
synchronized (monitor) {
errors.append(ExceptionUtils.getStackTrace(e)).append("\n");
}
}
}
}
|