Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: introduce tuple abstraction (rebased) (#1701)
* refactor: introduce tuple abstraction This replaces the use of byte[][] in a number of places with a Tuple class that wraps it. This is a necessary change to support doing anything other than blindly reading incoming data into a heap- allocated byte array. * docs: fix up copyright and add some javadoc * Extract remaining tuples in new code Co-authored-by: Tom Dunstan <tomdcc@users.noreply.github.com>
- Loading branch information
Showing
15 changed files
with
240 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Copyright (c) 2020, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package org.postgresql.core; | ||
|
||
/** | ||
* Class representing a row in a {@link java.sql.ResultSet}. | ||
*/ | ||
public class Tuple { | ||
private final boolean forUpdate; | ||
final byte[][] data; | ||
|
||
/** | ||
* Construct an empty tuple. Used in updatable result sets. | ||
* @param length the number of fields in the tuple. | ||
*/ | ||
public Tuple(int length) { | ||
this(new byte[length][], true); | ||
} | ||
|
||
/** | ||
* Construct a populated tuple. Used when returning results. | ||
* @param data the tuple data | ||
*/ | ||
public Tuple(byte[][] data) { | ||
this(data, false); | ||
} | ||
|
||
private Tuple(byte[][] data, boolean forUpdate) { | ||
this.data = data; | ||
this.forUpdate = forUpdate; | ||
} | ||
|
||
/** | ||
* Number of fields in the tuple | ||
* @return number of fields | ||
*/ | ||
public int fieldCount() { | ||
return data.length; | ||
} | ||
|
||
/** | ||
* Total length in bytes of the tuple data. | ||
* @return the number of bytes in this tuple | ||
*/ | ||
public int length() { | ||
int length = 0; | ||
for (byte[] field : data) { | ||
if (field != null) { | ||
length += field.length; | ||
} | ||
} | ||
return length; | ||
} | ||
|
||
/** | ||
* Get the data for the given field | ||
* @param index 0-based field position in the tuple | ||
* @return byte array of the data | ||
*/ | ||
public byte[] get(int index) { | ||
return data[index]; | ||
} | ||
|
||
/** | ||
* Create a copy of the tuple for updating. | ||
* @return a copy of the tuple that allows updates | ||
*/ | ||
public Tuple updateableCopy() { | ||
return copy(true); | ||
} | ||
|
||
/** | ||
* Create a read-only copy of the tuple | ||
* @return a copy of the tuple that does not allow updates | ||
*/ | ||
public Tuple readOnlyCopy() { | ||
return copy(false); | ||
} | ||
|
||
private Tuple copy(boolean forUpdate) { | ||
byte[][] dataCopy = new byte[data.length][]; | ||
System.arraycopy(data, 0, dataCopy, 0, data.length); | ||
return new Tuple(dataCopy, forUpdate); | ||
} | ||
|
||
/** | ||
* Set the given field to the given data. | ||
* @param index 0-based field position | ||
* @param fieldData the data to set | ||
*/ | ||
public void set(int index, byte[] fieldData) { | ||
if (!forUpdate) { | ||
throw new IllegalArgumentException("Attempted to write to readonly tuple"); | ||
} | ||
data[index] = fieldData; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.