Skip to content

Commit

Permalink
moved implementation of Blob/Clob to the base class
Browse files Browse the repository at this point in the history
  • Loading branch information
alexradzin committed May 28, 2024
1 parent bf0548d commit e195749
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 180 deletions.
92 changes: 6 additions & 86 deletions src/main/java/com/firebolt/jdbc/type/lob/FireboltBlob.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,21 @@
package com.firebolt.jdbc.type.lob;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.sql.Blob;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class FireboltBlob extends FireboltLob implements Blob {
private byte[] buf;

public class FireboltBlob extends FireboltLob<byte[], Byte> implements Blob {
public FireboltBlob() {
this(new byte[0]);
}

public FireboltBlob(byte[] buf) {
this.buf = buf;
}

@Override
public long length() throws SQLException {
return buf.length;
super(buf, (a, i) -> a[i], Integer::byteValue, n -> (byte[])Array.newInstance(byte.class, n));
}

@Override
Expand All @@ -43,32 +35,8 @@ public InputStream getBinaryStream() throws SQLException {
}

@Override
@SuppressWarnings("StatementWithEmptyBody") // so what?
public long position(byte[] pattern, long start) throws SQLException {
isValid(buf);
if (start < 1 || start > buf.length || buf.length == 0) {
return -1;
}
if (pattern.length == 0) {
return 1;
}

int fromIndex = (int)(start - 1L);
int max = buf.length - pattern.length;
for (int i = fromIndex; i <= max; i++) {
if (buf[i] != pattern[0]) {
for (i++; i < max && buf[i] != pattern[0]; i++);
}
if (i <= max) {
int j = i + 1;
int end = j + pattern.length - 1;
for (int k = 1; j < end && buf[j] == pattern[k]; j++, k++);
if (j == end) {
return i + 1;
}
}
}
return -1;
return super.position(pattern, start);
}

@Override
Expand All @@ -83,54 +51,12 @@ public int setBytes(long pos, byte[] bytes) throws SQLException {

@Override
public int setBytes(long pos, byte[] bytes, int offset, int len) throws SQLException {
isValid(buf);
validateSetRange(pos, bytes.length, offset, len);
int index = (int)(pos - 1);
int newLength = Math.max(buf.length, index + len);
byte[] buffer = new byte[newLength];
System.arraycopy(buf, 0, buffer, 0, buf.length);
System.arraycopy(bytes, offset, buffer, index, len);
buf = buffer;
return len;
return setData(pos, bytes, offset, len);
}

@Override
public OutputStream setBinaryStream(long pos) throws SQLException {
isValid(buf);
return new OutputStream() {
private final List<Byte> bytes = new LinkedList<>();
private int from = (int)(pos - 1);
private volatile boolean closed = false;

@Override
public void write(int b) throws IOException {
if (closed) {
throw new IOException("Stream is closed");
}
bytes.add((byte)b);
}

@Override
public void flush() {
int length = bytes.size();
int newLength = Math.max(buf.length, length + from);
if (newLength > buf.length) {
byte[] newBuf = new byte[newLength];
System.arraycopy(buf, 0, newBuf, 0, buf.length);
buf = newBuf;
}
for (byte b : bytes) {
buf[from++] = b;
}
bytes.clear();
}

@Override
public void close() {
flush();
closed = true;
}
};
return setStream(pos, new LinkedList<>());
}

@Override
Expand All @@ -139,12 +65,6 @@ public void truncate(long length) throws SQLException {
buf = length == 0 ? new byte[0] : getBytes(1, (int)length);
}

@Override
public void free() throws SQLException {
isValid(buf);
buf = null;
}

@Override
public InputStream getBinaryStream(long pos, long length) throws SQLException {
return new ByteArrayInputStream(getBytes(pos, (int)length));
Expand Down
93 changes: 5 additions & 88 deletions src/main/java/com/firebolt/jdbc/type/lob/FireboltClob.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,26 @@
package com.firebolt.jdbc.type.lob;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.lang.reflect.Array;
import java.sql.Clob;
import java.sql.NClob;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class FireboltClob extends FireboltLob implements NClob {
private char[] buf;

public class FireboltClob extends FireboltLob<char[], Character> implements NClob {
public FireboltClob() {
this(new char[0]);
}

public FireboltClob(char[] buf) {
this.buf = buf;
}

@Override
public long length() throws SQLException {
isValid(buf);
return buf.length;
super(buf, (a, i) -> a[i], i -> (char)i.intValue(), n -> (char[])Array.newInstance(char.class, n));
}

@Override
Expand Down Expand Up @@ -57,33 +48,6 @@ public long position(String searchStr, long start) throws SQLException {
return position(searchStr.toCharArray(), start);
}

private long position(char[] pattern, long start) throws SQLException {
isValid(buf);
if (start < 1 || start > buf.length || buf.length == 0) {
return -1;
}
if (pattern.length == 0) {
return 1;
}

int fromIndex = (int)(start - 1L);
int max = buf.length - pattern.length;
for (int i = fromIndex; i <= max; i++) {
if (buf[i] != pattern[0]) {
for (i++; i < max && buf[i] != pattern[0]; i++);
}
if (i <= max) {
int j = i + 1;
int end = j + pattern.length - 1;
for (int k = 1; j < end && buf[j] == pattern[k]; j++, k++);
if (j == end) {
return i + 1;
}
}
}
return -1;
}

@Override
public long position(Clob searchStr, long start) throws SQLException {
return position(searchStr.getSubString(1, (int)searchStr.length()), start);
Expand All @@ -100,53 +64,12 @@ public int setString(long pos, String str, int offset, int len) throws SQLExcept
}

private int setChars(long pos, char[] chars, int offset, int len) throws SQLException {
isValid(buf);
validateSetRange(pos, chars.length, offset, len);
int index = (int)(pos - 1);
int newLength = Math.max(buf.length, index + len);
char[] buffer = new char[newLength];
System.arraycopy(buf, 0, buffer, 0, buf.length);
System.arraycopy(chars, offset, buffer, index, len);
buf = buffer;
return len;
return setData(pos, chars, offset, len);
}

@Override
public OutputStream setAsciiStream(long pos) throws SQLException {
isValid(buf);
return new OutputStream() {
private final List<Character> characters = new LinkedList<>();
private int from = (int)(pos - 1);
private volatile boolean closed = false;

@Override
public void write(int b) throws IOException {
if (closed) {
throw new IOException("Stream is closed");
}
characters.add((char)b);
}

@Override
public void flush() {
int length = characters.size();
int newLength = Math.max(buf.length, length + from);
if (newLength > buf.length) {
char[] newBuf = new char[newLength];
System.arraycopy(buf, 0, newBuf, 0, buf.length);
buf = newBuf;
}
for (char b : characters) {
buf[from++] = b;
}
characters.clear();
}

public void close() {
flush();
closed = true;
}
};
return setStream(pos, new LinkedList<>());
}

@Override
Expand All @@ -160,12 +83,6 @@ public void truncate(long length) throws SQLException {
buf = length == 0 ? new char[0] : getSubString(1, (int)length).toCharArray();
}

@Override
public void free() throws SQLException {
isValid(buf);
buf = null;
}

@Override
public Reader getCharacterStream(long pos, long length) throws SQLException {
return new StringReader(getSubString(pos, (int)length));
Expand Down
Loading

0 comments on commit e195749

Please sign in to comment.