/
Entry.java
112 lines (82 loc) · 2.66 KB
/
Entry.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
Copyright 2022 CodeNotary, Inc. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.codenotary.immudb4j;
import io.codenotary.immudb.ImmudbProto;
public class Entry {
private long tx;
private byte[] key;
private byte[] value;
private KVMetadata metadata;
private Reference referencedBy;
private long revision;
private Entry() {}
public Entry(byte[] key, byte[] value) {
this.key = key;
this.value = value;
}
public static Entry valueOf(ImmudbProto.Entry e) {
final Entry entry = new Entry();
entry.tx = e.getTx();
entry.key = e.getKey().toByteArray();
entry.value = e.getValue().toByteArray();
if (e.hasMetadata()) {
entry.metadata = KVMetadata.valueOf(e.getMetadata());
}
if (e.hasReferencedBy()) {
entry.referencedBy = Reference.valueOf(e.getReferencedBy());
}
entry.revision = e.getRevision();
return entry;
}
public long getTx() {
return tx;
}
public byte[] getKey() {
return key;
}
public byte[] getValue() {
return value;
}
public KVMetadata getMetadata() {
return metadata;
}
public Reference getReferenceBy() {
return referencedBy;
}
public long getRevision() {
return revision;
}
public byte[] getEncodedKey() {
if (referencedBy == null) {
return Utils.wrapWithPrefix(key, Consts.SET_KEY_PREFIX);
}
return Utils.wrapWithPrefix(referencedBy.getKey(), Consts.SET_KEY_PREFIX);
}
public byte[] digestFor(int version) {
final KV kv;
if (referencedBy == null) {
kv = new KV(
getEncodedKey(),
metadata,
Utils.wrapWithPrefix(value, Consts.PLAIN_VALUE_PREFIX)
);
} else {
kv = new KV(
getEncodedKey(),
referencedBy.getMetadata(),
Utils.wrapReferenceValueAt(Utils.wrapWithPrefix(key, Consts.SET_KEY_PREFIX), referencedBy.getAtTx())
);
}
return kv.digestFor(version);
}
}