blob: a08064c8ce59a2bbf395b6c31c0f06903b54458a (
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.http.client.core;
import java.io.IOException;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.ThreadLocalRandom;
/**
* A document operation
*
* @author Einar M R Rosenvinge
*/
final public class Document {
private final String documentId;
private final ByteBuffer data;
private final long createTimeMillis = System.currentTimeMillis();
// This is initialized lazily to reduce work on calling thread (which is the thread calling the API).
private String operationId = null;
private final Object context;
private long queueInsertTimestampMillis;
public Document(String documentId, byte[] data, Object context) {
this.documentId = documentId;
this.context = context;
this.data = ByteBuffer.wrap(data);
}
public Document(String documentId, String operationId, CharSequence data, Object context) {
this.documentId = documentId;
this.operationId = operationId;
this.context = context;
try {
this.data = StandardCharsets.UTF_8.newEncoder().encode(CharBuffer.wrap(data));
} catch (CharacterCodingException e) {
throw new RuntimeException("Error encoding document data into UTF8 " + documentId, e);
}
}
public void resetQueueTime() {
queueInsertTimestampMillis = System.currentTimeMillis();
}
public long timeInQueueMillis() {
return System.currentTimeMillis() - queueInsertTimestampMillis;
}
public CharSequence getDataAsString() {
return StandardCharsets.UTF_8.decode(data.asReadOnlyBuffer());
}
public Object getContext() {
return context;
}
public static class DocumentException extends IOException {
private static final long serialVersionUID = 29832833292L;
public DocumentException(String message)
{
super(message);
}
}
public String getDocumentId() {
return documentId;
}
public ByteBuffer getData() {
return data.asReadOnlyBuffer();
}
public int size() {
return data.remaining();
}
public long createTimeMillis() {
return createTimeMillis;
}
public String getOperationId() {
if (operationId == null) {
operationId = new BigInteger(64, ThreadLocalRandom.current()).toString(32);
}
return operationId;
}
@Override
public String toString() { return "document '" + documentId + "'"; }
}
|