-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathsqlite_vec.lua
More file actions
116 lines (100 loc) · 3.18 KB
/
sqlite_vec.lua
File metadata and controls
116 lines (100 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
-- sqlite_vec.lua Lua 5.1 compatible version with JSON fallback
local sqlite3 = require("lsqlite3")
local M = {}
-- Function to load extension
function M.load(db)
local possible_paths = {
"../../sqlite-vec.so", -- Linux
"../../sqlite-vec.dll", -- Windows
"../../sqlite-vec.dylib", -- macOS
"./sqlite-vec.so",
"./sqlite-vec.dll",
"./sqlite-vec.dylib",
"../sqlite-vec.so",
"../sqlite-vec.dll",
"../sqlite-vec.dylib",
"sqlite-vec",
}
local entry_point = "sqlite3_vec_init"
if db.enable_load_extension then
db:enable_load_extension(true)
for _, path in ipairs(possible_paths) do
local ok, result = pcall(function()
return db:load_extension(path, entry_point)
end)
if ok then
db:enable_load_extension(false)
return result
end
end
db:enable_load_extension(false)
error("Failed to load extension from all paths")
else
for _, path in ipairs(possible_paths) do
local ok, result = pcall(function()
return db:load_extension(path, entry_point)
end)
if ok then
return result
else
local ok2, result2 = pcall(function()
return db:load_extension(path)
end)
if ok2 then
return result2
end
end
end
error("Failed to load extension from all paths")
end
end
-- Lua 5.1 compatible float to binary conversion function
local function float_to_bytes(f)
if f == 0 then
return string.char(0, 0, 0, 0)
end
local sign = 0
if f < 0 then
sign = 1
f = -f
end
local mantissa, exponent = math.frexp(f)
exponent = exponent - 1
if exponent < -126 then
mantissa = mantissa * 2^(exponent + 126)
exponent = -127
else
mantissa = (mantissa - 0.5) * 2
end
exponent = exponent + 127
mantissa = math.floor(mantissa * 2^23 + 0.5)
local bytes = {}
bytes[1] = mantissa % 256; mantissa = math.floor(mantissa / 256)
bytes[2] = mantissa % 256; mantissa = math.floor(mantissa / 256)
bytes[3] = mantissa % 256 + (exponent % 2) * 128; exponent = math.floor(exponent / 2)
bytes[4] = exponent % 128 + sign * 128
return string.char(bytes[1], bytes[2], bytes[3], bytes[4])
end
-- Helper function: serialize float vector to binary format (Lua 5.1 compatible)
function M.serialize_f32(vector)
local buffer = {}
if string.pack then
for _, v in ipairs(vector) do
table.insert(buffer, string.pack("f", v))
end
else
for _, v in ipairs(vector) do
table.insert(buffer, float_to_bytes(v))
end
end
return table.concat(buffer)
end
-- New: JSON format vector serialization (more reliable fallback)
function M.serialize_json(vector)
local values = {}
for _, v in ipairs(vector) do
table.insert(values, tostring(v))
end
return "[" .. table.concat(values, ",") .. "]"
end
return M