-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added experimental support for SQLite (sqlite-vec)
- Loading branch information
Showing
19 changed files
with
336 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
require "rails/generators" | ||
|
||
module Neighbor | ||
module Generators | ||
class SqliteGenerator < Rails::Generators::Base | ||
source_root File.join(__dir__, "templates") | ||
|
||
def copy_templates | ||
template "sqlite.rb", "config/initializers/neighbor.rb" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
# Load the sqlite-vec extension | ||
Neighbor::SQLite.initialize! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module Neighbor | ||
class Attribute < ActiveRecord::Type::Value | ||
delegate :type, :serialize, :deserialize, :cast, to: :new_cast_type | ||
|
||
def initialize(cast_type:, model:) | ||
@cast_type = cast_type | ||
@model = model | ||
end | ||
|
||
private | ||
|
||
def cast_value(...) | ||
new_cast_type.send(:cast_value, ...) | ||
end | ||
|
||
def new_cast_type | ||
@new_cast_type ||= begin | ||
if @cast_type.is_a?(ActiveModel::Type::Value) | ||
case @model.connection_db_config.adapter | ||
when /sqlite/i | ||
Type::SqliteVector.new | ||
else | ||
@cast_type | ||
end | ||
else | ||
@cast_type | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
module Neighbor | ||
module SQLite | ||
def self.initialize! | ||
require "sqlite_vec" | ||
require "active_record/connection_adapters/sqlite3_adapter" | ||
|
||
ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(InstanceMethods) | ||
end | ||
|
||
module InstanceMethods | ||
def configure_connection | ||
super | ||
db = ActiveRecord::VERSION::STRING.to_f >= 7.1 ? @raw_connection : @connection | ||
db.enable_load_extension(1) | ||
SqliteVec.load(db) | ||
db.enable_load_extension(0) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
module Neighbor | ||
module Type | ||
class SqliteVector < ActiveRecord::Type::Binary | ||
def serialize(value) | ||
if Utils.array?(value) | ||
value = value.to_a.pack("f*") | ||
end | ||
super(value) | ||
end | ||
|
||
def deserialize(value) | ||
value = super | ||
cast_value(value) unless value.nil? | ||
end | ||
|
||
private | ||
|
||
def cast_value(value) | ||
if value.is_a?(String) | ||
value.unpack("f*") | ||
elsif Utils.array?(value) | ||
value.to_a | ||
else | ||
raise "can't cast #{value.class.name} to vector" | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.