Debian Codegraph @

src/main/org/h2/mvstore/tx/Record.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.mvstore.tx;
7
8 import java.nio.ByteBuffer;
9 import org.h2.engine.Constants;
10 import org.h2.mvstore.DataUtils;
11 import org.h2.mvstore.MVMap;
12 import org.h2.mvstore.WriteBuffer;
13 import org.h2.mvstore.type.BasicDataType;
14 import org.h2.value.VersionedValue;
15
16 /**
17 * Class Record is a value for undoLog.
18 * It contains information about a single change of some map.
19 *
20 * @author <a href='mailto:[email protected]'>Andrei Tokar</a>
21 */
22 final class Record<K,V> {
23
24 // -1 is a bogus map id
25 static final Record<?,?> COMMIT_MARKER = new Record<>(-1, null, null);
26
27 /**
28 * Map id for this change is related to
29 */
30 final int mapId;
31
32 /**
33 * Key of the changed map entry key
34 */
35 final K key;
36
37 /**
38 * Value of the entry before change.
39 * It is null if entry did not exist before the change (addition).
40 */
41 final VersionedValue<V> oldValue;
42
43 Record(int mapId, K key, VersionedValue<V> oldValue) {
44 this.mapId = mapId;
45 this.key = key;
46 this.oldValue = oldValue;
47 }
48
49 @Override
50 public String toString() {
51 return "mapId=" + mapId + ", key=" + key + ", value=" + oldValue;
52 }
53
54 /**
55 * A data type for undo log values
56 */
57 static final class Type<K,V> extends BasicDataType<Record<K,V>> {
58 private final TransactionStore transactionStore;
59
60 Type(TransactionStore transactionStore) {
61 this.transactionStore = transactionStore;
62 }
63
64 @Override
65 public int getMemory(Record<K,V> record) {
66 int result = Constants.MEMORY_OBJECT + 4 + 3 * Constants.MEMORY_POINTER;
67 if (record.mapId >= 0) {
68 MVMap<K,VersionedValue<V>> map = transactionStore.getMap(record.mapId);
69 result += map.getKeyType().getMemory(record.key) +
70 map.getValueType().getMemory(record.oldValue);
71 }
72 return result;
73 }
74
75 @Override
76 public int compare(Record<K,V> aObj, Record<K,V> bObj) {
77 throw new UnsupportedOperationException();
78 }
79
80 @Override
81 public void write(WriteBuffer buff, Record<K,V> record) {
82 buff.putVarInt(record.mapId);
83 if (record.mapId >= 0) {
84 MVMap<K, VersionedValue<V>> map = transactionStore.getMap(record.mapId);
85 map.getKeyType().write(buff, record.key);
86 VersionedValue<V> oldValue = record.oldValue;
87 if (oldValue == null) {
88 buff.put((byte) 0);
89 } else {
90 buff.put((byte) 1);
91 map.getValueType().write(buff, oldValue);
92 }
93 }
94 }
95
96 @SuppressWarnings("unchecked")
97 @Override
98 public Record<K,V> read(ByteBuffer buff) {
99 int mapId = DataUtils.readVarInt(buff);
100 if (mapId < 0) {
101 return (Record<K,V>)COMMIT_MARKER;
102 }
103 MVMap<K, VersionedValue<V>> map = transactionStore.getMap(mapId);
104 K key = map.getKeyType().read(buff);
105 VersionedValue<V> oldValue = null;
106 if (buff.get() == 1) {
107 oldValue = map.getValueType().read(buff);
108 }
109 return new Record<>(mapId, key, oldValue);
110 }
111
112 @SuppressWarnings("unchecked")
113 @Override
114 public Record<K,V>[] createStorage(int size) {
115 return new Record[size];
116 }
117 }
118 }