added sqlite-vec extension

This commit is contained in:
Fabian Freund
2025-01-17 14:29:46 +01:00
parent 8c14b1f91e
commit ff15c460db
57 changed files with 1758 additions and 0 deletions
+134
View File
@@ -0,0 +1,134 @@
cmake_minimum_required(VERSION 3.10)
# Project-level configuration.
set(PROJECT_NAME "sqlite3_vec")
project(${PROJECT_NAME} LANGUAGES CXX C)
set(PLUGIN_NAME "sqlite3_vec_plugin")
# Plugin sources
list(APPEND PLUGIN_SOURCES
"sqlite3_vec_plugin.cc"
)
# Fetch SQLite and Vec libraries
include(FetchContent)
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.24.0")
FetchContent_Declare(
sqlite3
URL https://sqlite.org/2024/sqlite-autoconf-3470200.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP NEW
)
FetchContent_Declare(
vec0
URL https://github.com/asg017/sqlite-vec/archive/refs/tags/v0.1.7-alpha.2.tar.gz
DOWNLOAD_EXTRACT_TIMESTAMP NEW
)
else()
FetchContent_Declare(
sqlite3
URL https://sqlite.org/2024/sqlite-autoconf-3470200.tar.gz
)
FetchContent_Declare(
vec0
URL https://github.com/asg017/sqlite-vec/archive/refs/tags/v0.1.7-alpha.2.tar.gz
)
endif()
FetchContent_MakeAvailable(sqlite3)
FetchContent_MakeAvailable(vec0)
# Generate Vec header
add_custom_command(
OUTPUT ${vec0_SOURCE_DIR}/sqlite-vec.h
COMMAND make sqlite-vec.h
WORKING_DIRECTORY ${vec0_SOURCE_DIR}
COMMENT "Building sqlite-vec.h with make"
)
add_custom_target(generate_vec_header
DEPENDS ${vec0_SOURCE_DIR}/sqlite-vec.h
)
# Build Vec library
add_library(vec0
SHARED
${vec0_SOURCE_DIR}/sqlite-vec.c
)
add_dependencies(vec0 generate_vec_header)
target_include_directories(vec0
PRIVATE
${vec0_SOURCE_DIR}
${sqlite3_SOURCE_DIR}
)
target_compile_options(vec0
PRIVATE
-Wall
-Wextra
-O3
)
# Plugin library
add_library(${PLUGIN_NAME} SHARED
${PLUGIN_SOURCES}
)
apply_standard_settings(${PLUGIN_NAME})
set_target_properties(${PLUGIN_NAME} PROPERTIES
CXX_VISIBILITY_PRESET hidden)
target_compile_definitions(${PLUGIN_NAME} PRIVATE FLUTTER_PLUGIN_IMPL)
target_include_directories(${PLUGIN_NAME} INTERFACE
"${CMAKE_CURRENT_SOURCE_DIR}/include")
target_link_libraries(${PLUGIN_NAME} PRIVATE flutter)
target_link_libraries(${PLUGIN_NAME} PRIVATE PkgConfig::GTK)
target_link_libraries(${PLUGIN_NAME} PRIVATE vec0)
# Bundle the vec0 library
set(sqlite3_vec_bundled_libraries
"$<TARGET_FILE:vec0>"
PARENT_SCOPE
)
# Tests section remains unchanged
if (${include_${PROJECT_NAME}_tests})
if(${CMAKE_VERSION} VERSION_LESS "3.11.0")
message("Unit tests require CMake 3.11.0 or later")
else()
set(TEST_RUNNER "${PROJECT_NAME}_test")
enable_testing()
# Add the Google Test dependency.
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/release-1.11.0.zip
)
# Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
# Disable install commands for gtest so it doesn't end up in the bundle.
set(INSTALL_GTEST OFF CACHE BOOL "Disable installation of googletest" FORCE)
FetchContent_MakeAvailable(googletest)
# The plugin's exported API is not very useful for unit testing, so build the
# sources directly into the test binary rather than using the shared library.
add_executable(${TEST_RUNNER}
test/sqlite3_vec_plugin_test.cc
${PLUGIN_SOURCES}
)
apply_standard_settings(${TEST_RUNNER})
target_include_directories(${TEST_RUNNER} PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}")
target_link_libraries(${TEST_RUNNER} PRIVATE flutter)
target_link_libraries(${TEST_RUNNER} PRIVATE PkgConfig::GTK)
target_link_libraries(${TEST_RUNNER} PRIVATE gtest_main gmock)
# Enable automatic test discovery.
include(GoogleTest)
gtest_discover_tests(${TEST_RUNNER})
endif() # CMake version check
endif() # include_${PROJECT_NAME}_tests
@@ -0,0 +1,26 @@
#ifndef FLUTTER_PLUGIN_SQLITE3_VEC_PLUGIN_H_
#define FLUTTER_PLUGIN_SQLITE3_VEC_PLUGIN_H_
#include <flutter_linux/flutter_linux.h>
G_BEGIN_DECLS
#ifdef FLUTTER_PLUGIN_IMPL
#define FLUTTER_PLUGIN_EXPORT __attribute__((visibility("default")))
#else
#define FLUTTER_PLUGIN_EXPORT
#endif
typedef struct _Sqlite3VecPlugin Sqlite3VecPlugin;
typedef struct {
GObjectClass parent_class;
} Sqlite3VecPluginClass;
FLUTTER_PLUGIN_EXPORT GType sqlite3_vec_plugin_get_type();
FLUTTER_PLUGIN_EXPORT void sqlite3_vec_plugin_register_with_registrar(
FlPluginRegistrar* registrar);
G_END_DECLS
#endif // FLUTTER_PLUGIN_SQLITE3_VEC_PLUGIN_H_
@@ -0,0 +1,76 @@
#include "include/sqlite3_vec/sqlite3_vec_plugin.h"
#include <flutter_linux/flutter_linux.h>
#include <gtk/gtk.h>
#include <sys/utsname.h>
#include <cstring>
#include "sqlite3_vec_plugin_private.h"
#define SQLITE3_VEC_PLUGIN(obj) \
(G_TYPE_CHECK_INSTANCE_CAST((obj), sqlite3_vec_plugin_get_type(), \
Sqlite3VecPlugin))
struct _Sqlite3VecPlugin {
GObject parent_instance;
};
G_DEFINE_TYPE(Sqlite3VecPlugin, sqlite3_vec_plugin, g_object_get_type())
// Called when a method call is received from Flutter.
static void sqlite3_vec_plugin_handle_method_call(
Sqlite3VecPlugin* self,
FlMethodCall* method_call) {
g_autoptr(FlMethodResponse) response = nullptr;
const gchar* method = fl_method_call_get_name(method_call);
if (strcmp(method, "getPlatformVersion") == 0) {
response = get_platform_version();
} else {
response = FL_METHOD_RESPONSE(fl_method_not_implemented_response_new());
}
fl_method_call_respond(method_call, response, nullptr);
}
FlMethodResponse* get_platform_version() {
struct utsname uname_data = {};
uname(&uname_data);
g_autofree gchar *version = g_strdup_printf("Linux %s", uname_data.version);
g_autoptr(FlValue) result = fl_value_new_string(version);
return FL_METHOD_RESPONSE(fl_method_success_response_new(result));
}
static void sqlite3_vec_plugin_dispose(GObject* object) {
G_OBJECT_CLASS(sqlite3_vec_plugin_parent_class)->dispose(object);
}
static void sqlite3_vec_plugin_class_init(Sqlite3VecPluginClass* klass) {
G_OBJECT_CLASS(klass)->dispose = sqlite3_vec_plugin_dispose;
}
static void sqlite3_vec_plugin_init(Sqlite3VecPlugin* self) {}
static void method_call_cb(FlMethodChannel* channel, FlMethodCall* method_call,
gpointer user_data) {
Sqlite3VecPlugin* plugin = SQLITE3_VEC_PLUGIN(user_data);
sqlite3_vec_plugin_handle_method_call(plugin, method_call);
}
void sqlite3_vec_plugin_register_with_registrar(FlPluginRegistrar* registrar) {
Sqlite3VecPlugin* plugin = SQLITE3_VEC_PLUGIN(
g_object_new(sqlite3_vec_plugin_get_type(), nullptr));
g_autoptr(FlStandardMethodCodec) codec = fl_standard_method_codec_new();
g_autoptr(FlMethodChannel) channel =
fl_method_channel_new(fl_plugin_registrar_get_messenger(registrar),
"sqlite3_vec",
FL_METHOD_CODEC(codec));
fl_method_channel_set_method_call_handler(channel, method_call_cb,
g_object_ref(plugin),
g_object_unref);
g_object_unref(plugin);
}
@@ -0,0 +1,10 @@
#include <flutter_linux/flutter_linux.h>
#include "include/sqlite3_vec/sqlite3_vec_plugin.h"
// This file exposes some plugin internals for unit testing. See
// https://github.com/flutter/flutter/issues/88724 for current limitations
// in the unit-testable API.
// Handles the getPlatformVersion method call.
FlMethodResponse *get_platform_version();
@@ -0,0 +1,31 @@
#include <flutter_linux/flutter_linux.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include "include/sqlite3_vec/sqlite3_vec_plugin.h"
#include "sqlite3_vec_plugin_private.h"
// This demonstrates a simple unit test of the C portion of this plugin's
// implementation.
//
// Once you have built the plugin's example app, you can run these tests
// from the command line. For instance, for a plugin called my_plugin
// built for x64 debug, run:
// $ build/linux/x64/debug/plugins/my_plugin/my_plugin_test
namespace sqlite3_vec {
namespace test {
TEST(Sqlite3VecPlugin, GetPlatformVersion) {
g_autoptr(FlMethodResponse) response = get_platform_version();
ASSERT_NE(response, nullptr);
ASSERT_TRUE(FL_IS_METHOD_SUCCESS_RESPONSE(response));
FlValue* result = fl_method_success_response_get_result(
FL_METHOD_SUCCESS_RESPONSE(response));
ASSERT_EQ(fl_value_get_type(result), FL_VALUE_TYPE_STRING);
// The full string varies, so just validate that it has the right format.
EXPECT_THAT(fl_value_get_string(result), testing::StartsWith("Linux "));
}
} // namespace test
} // namespace sqlite3_vec