Skip to content

Commit

Permalink
Added fromBinary constructor to SparseVector
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Sep 10, 2024
1 parent 1339791 commit 381afd5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
25 changes: 25 additions & 0 deletions lib/sparsevec.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

class SparseVector {
final int dimensions;
final List<int> indices;
Expand All @@ -18,6 +20,29 @@ class SparseVector {
return SparseVector._(dimensions, indices, values);
}

factory SparseVector.fromBinary(Uint8List bytes) {
var bdata = new ByteData.view(bytes.buffer, bytes.offsetInBytes);
var dimensions = bdata.getInt32(0);
var nnz = bdata.getInt32(4);

var unused = bdata.getInt32(8);
if (unused != 0) {
throw FormatException('expected unused to be 0');
}

var indices = <int>[];
for (var i = 0; i < nnz; i++) {
indices.add(bdata.getInt32(12 + i * 4));
}

var values = <double>[];
for (var i = 0; i < nnz; i++) {
values.add(bdata.getFloat32(12 + 4 * nnz + i * 4));
}

return SparseVector._(dimensions, indices, values);
}

@override
String toString() {
var elements = [
Expand Down
3 changes: 2 additions & 1 deletion test/postgres_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ void main() {

List<List<dynamic>> results = await connection.execute(
Sql.named(
"SELECT id, embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5"),
"SELECT id, embedding, sparse_embedding FROM items ORDER BY embedding <-> @embedding LIMIT 5"),
parameters: {
"embedding": Vector([1, 1, 1]).toString()
});
for (final row in results) {
print(row[0]);
print(Vector.fromBinary(row[1].bytes));
print(SparseVector.fromBinary(row[2].bytes));
}

await connection
Expand Down

0 comments on commit 381afd5

Please sign in to comment.