Skip to content

Commit ca26380

Browse files
committed
Improved example [skip ci]
1 parent dfe208f commit ca26380

File tree

1 file changed

+13
-5
lines changed

1 file changed

+13
-5
lines changed

examples/loading/src/main/java/com/example/Example.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.postgresql.copy.CopyManager;
1111
import org.postgresql.core.BaseConnection;
1212
import org.postgresql.util.ByteConverter;
13+
import org.postgresql.util.PGBinaryObject;
1314

1415
public class Example {
1516
public static void main(String[] args) throws SQLException {
@@ -50,13 +51,20 @@ public static void main(String[] args) throws SQLException {
5051
copyIn.writeToCopy(buffer, 0, 19);
5152

5253
for (int i = 0; i < rows; i++) {
53-
PGvector embedding = new PGvector(embeddings.get(i));
54+
PGBinaryObject[] values = {new PGvector(embeddings.get(i))};
5455

5556
// write row
56-
ByteConverter.int2(buffer, 0, 1);
57-
ByteConverter.int4(buffer, 2, embedding.lengthInBytes());
58-
embedding.toBytes(buffer, 6);
59-
copyIn.writeToCopy(buffer, 0, 6 + embedding.lengthInBytes());
57+
ByteConverter.int2(buffer, 0, values.length);
58+
int pos = 2;
59+
for (int j = 0; j < values.length; j++) {
60+
PGBinaryObject value = values[j];
61+
int len = value.lengthInBytes();
62+
ByteConverter.int4(buffer, pos, len);
63+
pos += 4;
64+
value.toBytes(buffer, pos);
65+
pos += len;
66+
}
67+
copyIn.writeToCopy(buffer, 0, pos);
6068

6169
// show progress
6270
if (i % 10000 == 0) {

0 commit comments

Comments
 (0)