Skip to content

Commit 0b21f08

Browse files
ankanedavecramer
andcommitted
Added support for binary representation - closes #5
Co-authored-by: Dave Cramer <davecramer@gmail.com>
1 parent 9e809b4 commit 0b21f08

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.3 (unreleased)
2+
3+
- Added support for binary representation
4+
15
## 0.1.2 (2023-05-08)
26

37
- Added support for Java 11 and 17

src/main/java/com/pgvector/PGvector.java

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
import java.sql.SQLException;
66
import java.util.Arrays;
77
import org.postgresql.PGConnection;
8+
import org.postgresql.util.ByteConverter;
9+
import org.postgresql.util.PGBinaryObject;
810
import org.postgresql.util.PGobject;
911

1012
/**
1113
* PGvector class
1214
*/
13-
public class PGvector extends PGobject implements Serializable, Cloneable {
15+
public class PGvector extends PGobject implements PGBinaryObject, Serializable, Cloneable {
1416
private float[] vec;
1517

1618
/**
@@ -62,6 +64,45 @@ public String getValue() {
6264
}
6365
}
6466

67+
/**
68+
* Returns the number of bytes for the binary representation
69+
*/
70+
public int lengthInBytes() {
71+
return vec == null ? 0 : 4 + vec.length * 4;
72+
}
73+
74+
/**
75+
* Sets the value from a binary representation of a vector
76+
*/
77+
public void setByteValue(byte[] value, int offset) throws SQLException {
78+
int dim = ByteConverter.int2(value, offset);
79+
80+
int unused = ByteConverter.int2(value, offset + 2);
81+
if (unused != 0) {
82+
throw new SQLException("expected unused to be 0");
83+
}
84+
85+
vec = new float[dim];
86+
for (int i = 0; i < dim; i++) {
87+
vec[i] = ByteConverter.float4(value, offset + 4 + i * 4);
88+
}
89+
}
90+
91+
/**
92+
* Writes the binary representation of a vector
93+
*/
94+
public void toBytes(byte[] bytes, int offset) {
95+
if (vec == null) {
96+
return;
97+
}
98+
99+
ByteConverter.int2(bytes, offset, vec.length);
100+
ByteConverter.int2(bytes, offset + 2, 0);
101+
for (int i = 0; i < vec.length; i++) {
102+
ByteConverter.float4(bytes, offset + 4 + i * 4, vec[i]);
103+
}
104+
}
105+
65106
/**
66107
* Returns an array
67108
*/

src/test/java/com/pgvector/JDBCJava.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
import java.sql.*;
44
import com.pgvector.PGvector;
5+
import org.postgresql.PGConnection;
56

67
public class JDBCJava {
7-
public static void example() throws SQLException {
8+
public static void example(boolean read_binary) throws SQLException {
89
Connection conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/pgvector_java_test");
10+
if (read_binary) {
11+
conn.unwrap(PGConnection.class).setPrepareThreshold(-1);
12+
}
913

1014
Statement setupStmt = conn.createStatement();
1115
setupStmt.executeUpdate("CREATE EXTENSION IF NOT EXISTS vector");

src/test/scala/com/pgvector/MainSpec.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ package com.pgvector
22

33
class HelloSpec extends munit.FunSuite {
44
test("JDBCJava") {
5-
JDBCJava.example()
5+
JDBCJava.example(false)
6+
}
7+
8+
test("JDBCJava read binary") {
9+
JDBCJava.example(true)
610
}
711

812
test("SpringJDBC") {

0 commit comments

Comments
 (0)