|
| 1 | +package com.pgvector; |
| 2 | + |
| 3 | +import java.io.Serializable; |
| 4 | +import java.sql.Connection; |
| 5 | +import java.sql.SQLException; |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.List; |
| 8 | +import java.util.Objects; |
| 9 | +import org.postgresql.PGConnection; |
| 10 | +import org.postgresql.util.ByteConverter; |
| 11 | +import org.postgresql.util.PGBinaryObject; |
| 12 | +import org.postgresql.util.PGobject; |
| 13 | + |
| 14 | +/** |
| 15 | + * PGsparsevec class |
| 16 | + */ |
| 17 | +public class PGsparsevec extends PGobject implements PGBinaryObject, Serializable, Cloneable { |
| 18 | + private int dimensions; |
| 19 | + private int[] indices; |
| 20 | + private float[] values; |
| 21 | + |
| 22 | + /** |
| 23 | + * Constructor |
| 24 | + */ |
| 25 | + public PGsparsevec() { |
| 26 | + type = "sparsevec"; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * Constructor |
| 31 | + * |
| 32 | + * @param v float array |
| 33 | + */ |
| 34 | + public PGsparsevec(float[] v) { |
| 35 | + this(); |
| 36 | + |
| 37 | + int nnz = 0; |
| 38 | + for (int i = 0; i < v.length; i++) { |
| 39 | + if (v[i] != 0) { |
| 40 | + nnz++; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + dimensions = v.length; |
| 45 | + indices = new int[nnz]; |
| 46 | + values = new float[nnz]; |
| 47 | + |
| 48 | + int j = 0; |
| 49 | + for (int i = 0; i < v.length; i++) { |
| 50 | + if (v[i] != 0) { |
| 51 | + indices[j] = i; |
| 52 | + values[j] = v[i]; |
| 53 | + j++; |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Constructor |
| 60 | + * |
| 61 | + * @param <T> number |
| 62 | + * @param v list of numbers |
| 63 | + */ |
| 64 | + public <T extends Number> PGsparsevec(List<T> v) { |
| 65 | + this(); |
| 66 | + if (Objects.isNull(v)) { |
| 67 | + indices = null; |
| 68 | + } else { |
| 69 | + int nnz = 0; |
| 70 | + for (T f : v) { |
| 71 | + if (f.floatValue() != 0) { |
| 72 | + nnz++; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + dimensions = v.size(); |
| 77 | + indices = new int[nnz]; |
| 78 | + values = new float[nnz]; |
| 79 | + |
| 80 | + int i = 0; |
| 81 | + int j = 0; |
| 82 | + for (T f : v) { |
| 83 | + float fv = f.floatValue(); |
| 84 | + if (fv != 0) { |
| 85 | + indices[j] = i; |
| 86 | + values[j] = fv; |
| 87 | + j++; |
| 88 | + } |
| 89 | + i++; |
| 90 | + } |
| 91 | + |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Constructor |
| 97 | + * |
| 98 | + * @param s text representation of a sparse vector |
| 99 | + * @throws SQLException exception |
| 100 | + */ |
| 101 | + public PGsparsevec(String s) throws SQLException { |
| 102 | + this(); |
| 103 | + setValue(s); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * Sets the value from a text representation of a sparse vector |
| 108 | + */ |
| 109 | + public void setValue(String s) throws SQLException { |
| 110 | + if (s == null) { |
| 111 | + indices = null; |
| 112 | + } else { |
| 113 | + String[] sp = s.split("/", 2); |
| 114 | + String[] elements = sp[0].substring(1, sp[0].length() - 1).split(","); |
| 115 | + |
| 116 | + dimensions = Integer.parseInt(sp[1]); |
| 117 | + indices = new int[elements.length]; |
| 118 | + values = new float[elements.length]; |
| 119 | + |
| 120 | + for (int i = 0; i < elements.length; i++) |
| 121 | + { |
| 122 | + String[] ep = elements[i].split(":", 2); |
| 123 | + indices[i] = Integer.parseInt(ep[0]) - 1; |
| 124 | + values[i] = Float.parseFloat(ep[1]); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Returns the text representation of a sparse vector |
| 131 | + */ |
| 132 | + public String getValue() { |
| 133 | + if (indices == null) { |
| 134 | + return null; |
| 135 | + } else { |
| 136 | + StringBuilder sb = new StringBuilder(13 + 27 * indices.length); |
| 137 | + sb.append('{'); |
| 138 | + |
| 139 | + for (int i = 0; i < indices.length; i++) { |
| 140 | + if (i > 0) { |
| 141 | + sb.append(','); |
| 142 | + } |
| 143 | + sb.append(indices[i] + 1); |
| 144 | + sb.append(':'); |
| 145 | + sb.append(values[i]); |
| 146 | + } |
| 147 | + |
| 148 | + sb.append('}'); |
| 149 | + sb.append('/'); |
| 150 | + sb.append(dimensions); |
| 151 | + return sb.toString(); |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns the number of bytes for the binary representation |
| 157 | + */ |
| 158 | + public int lengthInBytes() { |
| 159 | + return indices == null ? 0 : 12 + indices.length * 4 + values.length * 4; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * Sets the value from a binary representation of a sparse vector |
| 164 | + */ |
| 165 | + public void setByteValue(byte[] value, int offset) throws SQLException { |
| 166 | + dimensions = ByteConverter.int4(value, offset); |
| 167 | + int nnz = ByteConverter.int4(value, offset + 4); |
| 168 | + |
| 169 | + int unused = ByteConverter.int4(value, offset + 8); |
| 170 | + if (unused != 0) { |
| 171 | + throw new SQLException("expected unused to be 0"); |
| 172 | + } |
| 173 | + |
| 174 | + indices = new int[nnz]; |
| 175 | + for (int i = 0; i < nnz; i++) { |
| 176 | + indices[i] = ByteConverter.int4(value, offset + 12 + i * 4); |
| 177 | + } |
| 178 | + |
| 179 | + values = new float[nnz]; |
| 180 | + for (int i = 0; i < nnz; i++) { |
| 181 | + values[i] = ByteConverter.float4(value, offset + 12 + nnz * 4 + i * 4); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Writes the binary representation of a sparse vector |
| 187 | + */ |
| 188 | + public void toBytes(byte[] bytes, int offset) { |
| 189 | + if (indices == null) { |
| 190 | + return; |
| 191 | + } |
| 192 | + |
| 193 | + // server will error on overflow due to unconsumed buffer |
| 194 | + // could set to Integer.MAX_VALUE for friendlier error message |
| 195 | + ByteConverter.int4(bytes, offset, dimensions); |
| 196 | + ByteConverter.int4(bytes, offset + 4, indices.length); |
| 197 | + ByteConverter.int4(bytes, offset + 8, 0); |
| 198 | + for (int i = 0; i < indices.length; i++) { |
| 199 | + ByteConverter.int4(bytes, offset + 12 + i * 4, indices[i]); |
| 200 | + } |
| 201 | + for (int i = 0; i < values.length; i++) { |
| 202 | + ByteConverter.float4(bytes, offset + 12 + indices.length * 4 + i * 4, values[i]); |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * Returns an array |
| 208 | + * |
| 209 | + * @return an array |
| 210 | + */ |
| 211 | + public float[] toArray() { |
| 212 | + if (indices == null) { |
| 213 | + return null; |
| 214 | + } |
| 215 | + |
| 216 | + float[] vec = new float[dimensions]; |
| 217 | + for (int i = 0; i < indices.length; i++) { |
| 218 | + vec[indices[i]] = values[i]; |
| 219 | + } |
| 220 | + return vec; |
| 221 | + } |
| 222 | + |
| 223 | + /** |
| 224 | + * Registers the sparsevec type |
| 225 | + * |
| 226 | + * @param conn connection |
| 227 | + * @throws SQLException exception |
| 228 | + */ |
| 229 | + public static void addSparsevecType(Connection conn) throws SQLException { |
| 230 | + conn.unwrap(PGConnection.class).addDataType("sparsevec", PGsparsevec.class); |
| 231 | + } |
| 232 | +} |
0 commit comments