Debian Codegraph @

src/main/org/h2/result/DefaultRow.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.result;
7
8 import org.h2.engine.Constants;
9 import org.h2.value.Value;
10 import org.h2.value.ValueBigint;
11
12 /**
13 * The default implementation of a row in a table.
14 */
15 public class DefaultRow extends Row {
16
17 /**
18 * The constant that means "memory usage is unknown and needs to be calculated first".
19 */
20 public static final int MEMORY_CALCULATE = -1;
21
22 /**
23 * The values of the row (one entry per column).
24 */
25 protected final Value[] data;
26
27 private int memory;
28
29 DefaultRow(int columnCount) {
30 this.data = new Value[columnCount];
31 this.memory = MEMORY_CALCULATE;
32 }
33
34 public DefaultRow(Value[] data) {
35 this.data = data;
36 this.memory = MEMORY_CALCULATE;
37 }
38
39 public DefaultRow(Value[] data, int memory) {
40 this.data = data;
41 this.memory = memory;
42 }
43
44 @Override
45 public Value getValue(int i) {
46 return i == ROWID_INDEX ? ValueBigint.get(key) : data[i];
47 }
48
49 @Override
50 public void setValue(int i, Value v) {
51 if (i == ROWID_INDEX) {
52 key = v.getLong();
53 } else {
54 data[i] = v;
55 }
56 }
57
58 @Override
59 public int getColumnCount() {
60 return data.length;
61 }
62
63 @Override
64 public int getMemory() {
65 if (memory != MEMORY_CALCULATE) {
66 return memory;
67 }
68 return memory = calculateMemory();
69 }
70
71 @Override
72 public String toString() {
73 StringBuilder builder = new StringBuilder("( /* key:").append(key).append(" */ ");
74 for (int i = 0, length = data.length; i < length; i++) {
75 if (i > 0) {
76 builder.append(", ");
77 }
78 Value v = data[i];
79 builder.append(v == null ? "null" : v.getTraceSQL());
80 }
81 return builder.append(')').toString();
82 }
83
84 /**
85 * Calculate the estimated memory used for this row, in bytes.
86 *
87 * @return the memory
88 */
89 protected int calculateMemory() {
90 int m = Constants.MEMORY_ROW + Constants.MEMORY_ARRAY + data.length * Constants.MEMORY_POINTER;
91 for (Value v : data) {
92 if (v != null) {
93 m += v.getMemory();
94 }
95 }
96 return m;
97 }
98
99 @Override
100 public Value[] getValueList() {
101 return data;
102 }
103
104 @Override
105 public boolean hasSharedData(Row other) {
106 return other instanceof DefaultRow && data == ((DefaultRow) other).data;
107 }
108
109 @Override
110 public void copyFrom(SearchRow source) {
111 setKey(source.getKey());
112 for (int i = 0; i < getColumnCount(); i++) {
113 setValue(i, source.getValue(i));
114 }
115 }
116 }