forked from pgjdbc/r2dbc-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresqlRow.java
233 lines (185 loc) · 7.34 KB
/
PostgresqlRow.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* Copyright 2017-2019 the original author or authors.
*
* 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
*
* https://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.r2dbc.postgresql;
import io.netty.buffer.ByteBuf;
import io.r2dbc.postgresql.codec.Codecs;
import io.r2dbc.postgresql.message.Format;
import io.r2dbc.postgresql.message.backend.DataRow;
import io.r2dbc.postgresql.message.backend.RowDescription;
import io.r2dbc.postgresql.util.Assert;
import io.r2dbc.spi.Row;
import reactor.util.annotation.Nullable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
/**
* An implementation of {@link Row} for a PostgreSQL database.
*/
public final class PostgresqlRow implements Row {
private final Codecs codecs;
private final List<Column> columns;
private final AtomicBoolean isReleased = new AtomicBoolean(false);
private final Map<String, Column> nameKeyedColumns;
PostgresqlRow(Codecs codecs, List<Column> columns) {
this.codecs = Assert.requireNonNull(codecs, "codecs must not be null");
this.columns = Assert.requireNonNull(columns, "columns must not be null");
this.nameKeyedColumns = getNameKeyedColumns(this.columns);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
PostgresqlRow that = (PostgresqlRow) o;
return Objects.equals(this.columns, that.columns);
}
@Nullable
@Override
public <T> T get(Object identifier, Class<T> type) {
Assert.requireNonNull(identifier, "identifier must not be null");
Assert.requireNonNull(type, "type must not be null");
requireNotReleased();
Column column;
if (identifier instanceof Integer) {
column = getColumn((Integer) identifier);
} else if (identifier instanceof String) {
column = getColumn((String) identifier);
} else {
throw new IllegalArgumentException(String.format("Identifier '%s' is not a valid identifier. Should either be an Integer index or a String column name.", identifier));
}
return this.codecs.decode(column.getByteBuf(), column.getDataType(), column.getFormat(), type);
}
@Override
public int hashCode() {
return Objects.hash(this.columns);
}
@Override
public String toString() {
return "PostgresqlRow{" +
"codecs=" + this.codecs +
", columns=" + this.columns +
", isReleased=" + this.isReleased +
", nameKeyedColumns=" + this.nameKeyedColumns +
'}';
}
static PostgresqlRow toRow(Codecs codecs, DataRow dataRow, RowDescription rowDescription) {
Assert.requireNonNull(codecs, "codecs must not be null");
Assert.requireNonNull(dataRow, "dataRow must not be null");
Assert.requireNonNull(rowDescription, "rowDescription must not be null");
List<Column> columns = getColumns(dataRow.getColumns(), rowDescription.getFields());
dataRow.release();
return new PostgresqlRow(codecs, columns);
}
void release() {
this.columns.forEach(Column::release);
this.isReleased.set(true);
}
private static List<Column> getColumns(List<ByteBuf> byteBufs, List<RowDescription.Field> fields) {
List<Column> columns = new ArrayList<>(byteBufs.size());
for (int i = 0; i < byteBufs.size(); i++) {
ByteBuf byteBuf = byteBufs.get(i);
RowDescription.Field field = fields.get(i);
columns.add(new Column(byteBuf, field.getDataType(), field.getFormat(), field.getName()));
}
return columns;
}
private Column getColumn(String name) {
if (!this.nameKeyedColumns.containsKey(name)) {
throw new IllegalArgumentException(String.format("Column name '%s' does not exist in column names %s", name, this.nameKeyedColumns.keySet()));
}
return this.nameKeyedColumns.get(name);
}
private Column getColumn(Integer index) {
if (index >= this.columns.size()) {
throw new IllegalArgumentException(String.format("Column index %d is larger than the number of columns %d", index, this.columns.size()));
}
return this.columns.get(index);
}
private Map<String, Column> getNameKeyedColumns(List<Column> columns) {
Map<String, Column> nameKeyedColumns = new HashMap<>(columns.size());
for (Column column : columns) {
nameKeyedColumns.put(column.getName(), column);
}
return nameKeyedColumns;
}
private void requireNotReleased() {
if (this.isReleased.get()) {
throw new IllegalStateException("Value cannot be retrieved after row has been released");
}
}
static final class Column {
private final ByteBuf byteBuf;
private final Integer dataType;
private final Format format;
private final String name;
Column(@Nullable ByteBuf byteBuf, Integer dataType, Format format, String name) {
this.byteBuf = byteBuf == null ? null : byteBuf.retain();
this.dataType = Assert.requireNonNull(dataType, "dataType must not be null");
this.format = Assert.requireNonNull(format, "format must not be null");
this.name = Assert.requireNonNull(name, "name must not be null");
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Column that = (Column) o;
return Objects.equals(this.byteBuf, that.byteBuf) &&
Objects.equals(this.dataType, that.dataType) &&
this.format == that.format &&
Objects.equals(this.name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(this.byteBuf, this.dataType, this.format, this.name);
}
@Override
public String toString() {
return "Column{" +
"byteBuf=" + this.byteBuf +
", dataType=" + this.dataType +
", format=" + this.format +
", name='" + this.name + '\'' +
'}';
}
private ByteBuf getByteBuf() {
return this.byteBuf;
}
private Integer getDataType() {
return this.dataType;
}
private Format getFormat() {
return this.format;
}
private String getName() {
return this.name;
}
private void release() {
if (this.byteBuf != null) {
this.byteBuf.release();
}
}
}
}