### Setup and Query Kuzu DB in Browser with JavaScript Source: https://github.com/kineviz/bighorn/blob/master/tools/wasm/examples/browser_persistent/public/index.html This JavaScript code initializes a Kuzu database in the browser using WebAssembly and the IDBFS filesystem. It handles both the first run (database creation and data loading) and subsequent runs (querying the persistent database). It relies on the 'kuzu' library and browser's localStorage for persistence. ```javascript import kuzu from './index.js'; window.kuzu = kuzu; const appendOutput = (text) => { const output = document.getElementById("output"); output.innerText += text + "\n"; }; (async () => { const isFirstRun = window.localStorage.getItem("isFirstRun") !== "false"; if (isFirstRun) { appendOutput("First run detected. Setting up the database..."); // Write the data into WASM filesystem const userCSV = `Adam,30 Karissa,40 Zhang,50 Noura,25`; const cityCSV = `Waterloo,150000 Kitchener,200000 Guelph,75000`; const followsCSV = `Adam,Karissa,2020 Adam,Zhang,2020 Karissa,Zhang,2021 Zhang,Noura,2022`; const livesInCSV = `Adam,Waterloo Karissa,Waterloo Zhang,Kitchener Noura,Guelph`; await kuzu.FS.writeFile("/user.csv", userCSV); await kuzu.FS.writeFile("/city.csv", cityCSV); await kuzu.FS.writeFile("/follows.csv", followsCSV); await kuzu.FS.writeFile("/lives-in.csv", livesInCSV); // Make a persistent IDBFS directory await kuzu.FS.mkdir("/database"); await kuzu.FS.mountIdbfs("/database"); appendOutput("IDBFS mounted."); // Create an empty database and connect to it const db = new kuzu.Database("/database"); const conn = new kuzu.Connection(db); const queries = [ // Create the tables "CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name))", "CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY (name))", "CREATE REL TABLE Follows(FROM User TO User, since INT64)", "CREATE REL TABLE LivesIn(FROM User TO City)", // Load the data "COPY User FROM 'user.csv'", "COPY City FROM 'city.csv'", "COPY Follows FROM 'follows.csv'", "COPY LivesIn FROM 'lives-in.csv'", ]; appendOutput("Setting up the database..."); // Execute the queries to setup the database for (const query of queries) { appendOutput("Executing query: " + query); const queryResult = await conn.query(query); const outputString = await queryResult.toString(); appendOutput(outputString); await queryResult.close(); } appendOutput("Database setup complete."); // Close the connection await conn.close(); appendOutput("Connection closed."); // Close the database await db.close(); appendOutput("Database closed."); // Sync the filesystem await kuzu.FS.syncfs(false); appendOutput("Database persisted to IDBFS."); await kuzu.FS.unmount("/database"); appendOutput("IDBFS unmounted."); window.localStorage.setItem("isFirstRun", "false"); } else { appendOutput("Second or later run detected. Querying the database..."); // Make a persistent IDBFS directory await kuzu.FS.mkdir("/database"); await kuzu.FS.mountIdbfs("/database"); appendOutput("IDBFS mounted."); // Sync the filesystem await kuzu.FS.syncfs(true); appendOutput("Database loaded from IDBFS."); // Create a new database and connect to it const db = new kuzu.Database("/database"); const conn = new kuzu.Connection(db); // Query the database let queryString = "MATCH (u:User) -[f:Follows]-> (v:User) RETURN u.name, f.since, v.name"; appendOutput("Executing query: " + queryString); let queryResult = await conn.query(queryString); let rows = await queryResult.getAllObjects(); appendOutput("Query result:"); for (const row of rows) { appendOutput(`User ${row['u.name']} follows ${row['v.name']} since ${row['f.since']}`); } await queryResult.close(); appendOutput(""); queryString = "MATCH (u:User) -[l:LivesIn]-> (c:City) RETURN u.name, c.name"; appendOutput("Executing query: " + queryString); queryResult = await conn.query(queryString); rows = await queryResult.getAllObjects(); appendOutput("Query result:"); // Print the rows for (const row of rows) { appendOutput(`User ${row['u.name']} lives in ${row['c.name']}`); } await queryResult.close(); appendOutput(""); appendOutput("All queries executed successfully."); // Close the connection await conn.close(); appendOutput("Connection closed."); // Close the database await db.close(); appendOutput("Database closed."); // Unmount the IDBFS await kuzu.FS.unmount("/database"); appendOutput("IDBFS unmounted."); } })(); ``` -------------------------------- ### Quick Start: Kuzu Database Operations (ES Modules) Source: https://github.com/kineviz/bighorn/blob/master/tools/nodejs_api/README.md Demonstrates initializing a Kuzu database, creating tables, loading data from CSV files, and executing a query to retrieve user data. This example uses ES Modules for importing Kuzu functionalities. ```javascript // Import the Kuzu module (ESM) import { Database, Connection } from "kuzu"; const main = async () => { // Initialize database and connection const db = new Database("./test"); const conn = new Connection(db); // Define schema await conn.query(` CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name)); `); await conn.query(` CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY (name)); `); await conn.query(` CREATE REL TABLE Follows(FROM User TO User, since INT64); `); await conn.query(` CREATE REL TABLE LivesIn(FROM User TO City); `); // Load data from CSV files await conn.query(`COPY User FROM "user.csv"`); await conn.query(`COPY City FROM "city.csv"`); await conn.query(`COPY Follows FROM "follows.csv"`); await conn.query(`COPY LivesIn FROM "lives-in.csv"`); // Run a query const result = await conn.query("MATCH (u:User) RETURN u.name, u.age;"); // Fetch all results const rows = await result.getAll(); // Output results for (const row of rows) { console.log(row); } }; main().catch(console.error); ``` -------------------------------- ### Install Kuzu Extension from Local Server Source: https://github.com/kineviz/bighorn/blob/master/README.md This Cypher query shows how to install an extension from a locally hosted extension server. The `FROM` clause specifies the server's URL, which is `http://localhost:8080/` in this example. Replace `` with the desired extension. ```cypher INSTALL FROM 'http://localhost:8080/'; ``` -------------------------------- ### Build and Install Brotli using CMake Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/README.md Steps to build and install the Brotli compression algorithm using CMake. This process involves creating an output directory, configuring the build with CMake specifying the build type and installation prefix, and then building and installing the target. Ensure CMake is installed on your system. ```shell mkdir out && cd out cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=./installed .. cmake --build . --config Release --target install ``` -------------------------------- ### Kuzu WebAssembly Database Setup and Querying in JavaScript Source: https://github.com/kineviz/bighorn/blob/master/tools/wasm/examples/browser_in_memory/public/index.html This JavaScript code snippet demonstrates the complete workflow of using Kuzu WebAssembly in a web browser. It initializes the Kuzu database, defines a social network schema, loads data from CSV strings, and executes various queries. The output of each step and query is appended to a designated HTML element with the ID 'output'. ```javascript import kuzu from './index.js'; window.kuzu = kuzu; const appendOutput = (text) => { const output = document.getElementById("output"); output.innerText += text + "\n"; }; (async () => { // Write the data into WASM filesystem const userCSV = `Adam,30 Karissa,40 Zhang,50 Noura,25`; const cityCSV = `Waterloo,150000 Kitchener,200000 Guelph,75000`; const followsCSV = `Adam,Karissa,2020 Adam,Zhang,2020 Karissa,Zhang,2021 Zhang,Noura,2022`; const livesInCSV = `Adam,Waterloo Karissa,Waterloo Zhang,Kitchener Noura,Guelph`; await kuzu.FS.writeFile("/user.csv", userCSV); await kuzu.FS.writeFile("/city.csv", cityCSV); await kuzu.FS.writeFile("/follows.csv", followsCSV); await kuzu.FS.writeFile("/lives-in.csv", livesInCSV); // Create an empty database and connect to it const db = new kuzu.Database(""); const conn = new kuzu.Connection(db); const queries = [ // Create the tables "CREATE NODE TABLE User(name STRING, age INT64, PRIMARY KEY (name))", "CREATE NODE TABLE City(name STRING, population INT64, PRIMARY KEY (name))", "CREATE REL TABLE Follows(FROM User TO User, since INT64)", "CREATE REL TABLE LivesIn(FROM User TO City)", // Load the data "COPY User FROM 'user.csv'", "COPY City FROM 'city.csv'", "COPY Follows FROM 'follows.csv'", "COPY LivesIn FROM 'lives-in.csv'", ]; appendOutput("Setting up the database..."); // Execute the queries to setup the database for (const query of queries) { appendOutput("Executing query: " + query); const queryResult = await conn.query(query); const outputString = await queryResult.toString(); appendOutput(outputString); await queryResult.close(); } appendOutput("Database setup complete."); appendOutput(""); // Query the database let queryString = "MATCH (u:User) -[f:Follows]-> (v:User) RETURN u.name, f.since, v.name"; appendOutput("Executing query: " + queryString); let queryResult = await conn.query(queryString); let rows = await queryResult.getAllObjects(); appendOutput("Query result:"); for (const row of rows) { appendOutput(`User ${row['u.name']} follows ${row['v.name']} since ${row['f.since']}`); } await queryResult.close(); appendOutput(""); queryString = "MATCH (u:User) -[l:LivesIn]-> (c:City) RETURN u.name, c.name"; appendOutput("Executing query: " + queryString); queryResult = await conn.query(queryString); rows = await queryResult.getAllObjects(); appendOutput("Query result:"); // Print the rows for (const row of rows) { appendOutput(`User ${row['u.name']} lives in ${row['c.name']}`); } await queryResult.close(); appendOutput(""); appendOutput("All queries executed successfully."); // Close the connection await conn.close(); appendOutput("Connection closed."); // Close the database await db.close(); appendOutput("Database closed."); })(); ``` -------------------------------- ### Install Brotli Python Module using pip Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/README.md Instructions for installing the Brotli compression module for Python. You can install the latest release or the development version directly from GitHub using pip. The `--upgrade` flag ensures you get the most recent version from the repository. Refer to the Python README for detailed installation and development information. ```shell pip install brotli pip install --upgrade git+https://github.com/google/brotli ``` -------------------------------- ### Install Brotli using vcpkg Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/README.md Instructions to install the Brotli compression library using the vcpkg dependency manager. This involves cloning the vcpkg repository, bootstrapping it, integrating it with your system, and then installing the brotli package. Ensure your vcpkg repository is up-to-date for the latest version. ```shell git clone https://github.com/Microsoft/vcpkg.git cd vcpkg ./bootstrap-vcpkg.sh ./vcpkg integrate install ./vcpkg install brotli ``` -------------------------------- ### Python Kuzu Extension Loading Source: https://context7.com/kineviz/bighorn/llms.txt Illustrates how to install and load extensions dynamically at runtime using Python with the Kuzu database. It mentions that common extensions are pre-installed in newer versions, and provides a commented-out example for installing extensions from a local server for older versions. ```python import kuzu db = kuzu.Database("graph_analytics.db") conn = kuzu.Connection(db) # Note: For Kuzu v0.11.3+, common extensions (algo, fts, json, vector) are pre-installed # For earlier versions or other extensions, install from local extension server: # conn.execute("INSTALL extension_name FROM 'http://localhost:8080/'") ``` -------------------------------- ### Install Dev Dependencies Source: https://github.com/kineviz/bighorn/blob/master/tools/nodejs_api/README.md Installs development dependencies required for contributing to the Kuzu Node.js project. Includes all necessary tools for local development and testing. ```bash npm install --include=dev ``` -------------------------------- ### Install Dependencies (Bash) Source: https://github.com/kineviz/bighorn/blob/master/tools/wasm/README.md Command to install project dependencies using npm. This is a standard step in many Node.js projects to fetch necessary packages listed in `package.json`. ```bash npm i ``` -------------------------------- ### Install Kuzu Node.js Package Source: https://github.com/kineviz/bighorn/blob/master/tools/nodejs_api/README.md Installs the Kuzu Node.js package using npm. This is the primary step to begin using the Kuzu database in a Node.js environment. ```bash npm install kuzu ``` -------------------------------- ### Install Target in CMake Source: https://github.com/kineviz/bighorn/blob/master/tools/shell/CMakeLists.txt Specifies targets to be installed. This command defines which executables, libraries, or other files should be installed when the 'install' build target is invoked. ```cmake install(TARGETS kuzu_shell) ``` -------------------------------- ### Install pkg-config Files (CMake) Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/CMakeLists.txt This CMake code installs the generated `.pc` files for `libbrotlicommon`, `libbrotlidec`, and `libbrotlienc` to the `pkgconfig` subdirectory of the installation's library path. The installation is conditional on `BROTLI_BUNDLED_MODE` being false. ```cmake transform_pc_file("scripts/libbrotlicommon.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libbrotlicommon.pc" "${BROTLI_VERSION}") transform_pc_file("scripts/libbrotlidec.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libbrotlidec.pc" "${BROTLI_VERSION}") transform_pc_file("scripts/libbrotlienc.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/libbrotlienc.pc" "${BROTLI_VERSION}") if(NOT BROTLI_BUNDLED_MODE) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libbrotlicommon.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libbrotlidec.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libbrotlienc.pc" DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig") endif() # BROTLI_BUNDLED_MODE ``` -------------------------------- ### CMake Installation Rules Source: https://github.com/kineviz/bighorn/blob/master/third_party/cppjieba/deps/limonp/CMakeLists.txt Configures the installation of the project, including the main target library, export configuration files, and header directories. It uses GNUInstallDirs for standard installation paths. ```cmake include(GNUInstallDirs) install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}) install(EXPORT ${PROJECT_NAME} DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/${PROJECT_NAME}/ NAMESPACE ${PROJECT_NAME}:: FILE ${PROJECT_NAME}-config.cmake) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) ``` -------------------------------- ### Install Man Page (CMake) Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/CMakeLists.txt This CMake code conditionally installs the `brotli.1` man page to the `man1` directory. The installation is enabled if the `BROTLI_BUILD_TOOLS` option is true. ```cmake if (BROTLI_BUILD_TOOLS) install(FILES "docs/brotli.1" DESTINATION "${CMAKE_INSTALL_FULL_MANDIR}/man1") endif() ``` -------------------------------- ### CMake: Install Brotli Components Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/CMakeLists.txt This snippet defines the installation rules for the Brotli components. It specifies where the runtime executables, libraries, and include directories should be placed on the system. Installation is conditional based on the BROTLI_BUNDLED_MODE and BROTLI_BUILD_TOOLS variables. ```cmake if(NOT BROTLI_BUNDLED_MODE) if (BROTLI_BUILD_TOOLS) install( TARGETS brotli RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ) endif() install( TARGETS ${BROTLI_LIBRARIES_CORE} ARCHIVE DESTINATION "${CMAKE_INSTALL_LIBDIR}" LIBRARY DESTINATION "${CMAKE_INSTALL_LIBDIR}" RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}" ) install( DIRECTORY ${BROTLI_INCLUDE_DIRS}/brotli DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}" ) endif() # BROTLI_BUNDLED_MODE ``` -------------------------------- ### CMake Interface Library and Installation Rules Source: https://github.com/kineviz/bighorn/blob/master/third_party/cppjieba/CMakeLists.txt This snippet defines an interface library for 'cppjieba' and specifies installation rules for include directories and dictionaries. It also includes logic to enable testing when it's the top-level project. ```cmake # Define a variable to check if this is the top-level project if(NOT DEFINED CPPJIEBA_TOP_LEVEL_PROJECT) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(CPPJIEBA_TOP_LEVEL_PROJECT ON) else() set(CPPJIEBA_TOP_LEVEL_PROJECT OFF) endif() endif() if(NOT TARGET cppjieba) add_library(cppjieba INTERFACE) target_include_directories(cppjieba INTERFACE ${PROJECT_SOURCE_DIR}/include ${PROJECT_SOURCE_DIR}/deps/limonp/include ) endif() include(GNUInstallDirs) install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) install(DIRECTORY dict/ DESTINATION ${CMAKE_INSTALL_DATADIR}/cppjieba/dict) if(CPPJIEBA_TOP_LEVEL_PROJECT) ENABLE_TESTING() message(STATUS "MSVC value: ${MSVC}") ADD_SUBDIRECTORY(test) ADD_TEST(NAME ./test/test.run COMMAND ./test/test.run) ADD_TEST(NAME ./load_test COMMAND ./load_test) endif() ``` -------------------------------- ### Include Directories Setup (CMake) Source: https://github.com/kineviz/bighorn/blob/master/extension/duckdb/CMakeLists.txt Sets up the necessary include directories for the project. This includes project-specific headers, generated headers, headers for the httpfs extension, and DuckDB's include directories. ```cmake include_directories( ${PROJECT_SOURCE_DIR}/src/include ${CMAKE_BINARY_DIR}/src/include ${PROJECT_SOURCE_DIR}/extension/httpfs/src/include # For S3 configuration src/include ${DuckDB_INCLUDE_DIRS}) ``` -------------------------------- ### Install Pybind11 Components and Config Files Source: https://github.com/kineviz/bighorn/blob/master/third_party/pybind11/CMakeLists.txt Installs pybind11 headers, config files (Config.cmake, ConfigVersion.cmake), and various helper scripts. It also sets up pkg-config support and an uninstall target if PYBIND11_MASTER_PROJECT is defined. This snippet is conditional on PYBIND11_INSTALL. ```cmake if(PYBIND11_INSTALL) install(DIRECTORY ${pybind11_INCLUDE_DIR}/pybind11 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) set(PYBIND11_CMAKECONFIG_INSTALL_DIR "${CMAKE_INSTALL_DATAROOTDIR}/cmake/${PROJECT_NAME}" CACHE STRING "install path for pybind11Config.cmake") if(IS_ABSOLUTE "${CMAKE_INSTALL_INCLUDEDIR}") set(pybind11_INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}") else() set(pybind11_INCLUDEDIR "\${\}\nPACKAGE_PREFIX_DIR\}/${CMAKE_INSTALL_INCLUDEDIR}") endif() configure_package_config_file( tools/${PROJECT_NAME}Config.cmake.in "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) if(CMAKE_VERSION VERSION_LESS 3.14) # Remove CMAKE_SIZEOF_VOID_P from ConfigVersion.cmake since the library does # not depend on architecture specific settings or libraries. set(_PYBIND11_CMAKE_SIZEOF_VOID_P ${CMAKE_SIZEOF_VOID_P}) unset(CMAKE_SIZEOF_VOID_P) write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion) set(CMAKE_SIZEOF_VOID_P ${_PYBIND11_CMAKE_SIZEOF_VOID_P}) else() # CMake 3.14+ natively supports header-only libraries write_basic_package_version_file( ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion ARCH_INDEPENDENT) endif() install( FILES ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}Config.cmake ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake tools/FindPythonLibsNew.cmake tools/pybind11Common.cmake tools/pybind11Tools.cmake tools/pybind11NewTools.cmake DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) if(NOT PYBIND11_EXPORT_NAME) set(PYBIND11_EXPORT_NAME "${PROJECT_NAME}Targets") endif() install(TARGETS pybind11_headers EXPORT "${PYBIND11_EXPORT_NAME}") install( EXPORT "${PYBIND11_EXPORT_NAME}" NAMESPACE "pybind11::" DESTINATION ${PYBIND11_CMAKECONFIG_INSTALL_DIR}) # pkg-config support if(NOT prefix_for_pc_file) set(prefix_for_pc_file "${CMAKE_INSTALL_PREFIX}") endif() join_paths(includedir_for_pc_file "\${\}\nprefix" "${CMAKE_INSTALL_INCLUDEDIR}") configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/pybind11.pc.in" "${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc" @ONLY) install(FILES "${CMAKE_CURRENT_BINARY_DIR}/pybind11.pc" DESTINATION "${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig/") # Uninstall target if(PYBIND11_MASTER_PROJECT) configure_file("${CMAKE_CURRENT_SOURCE_DIR}/tools/cmake_uninstall.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake" IMMEDIATE @ONLY) add_custom_target(uninstall COMMAND ${CMAKE_COMMAND} -P ${CMAKE_CURRENT_BINARY_DIR}/cmake_uninstall.cmake) endif() endif() ``` -------------------------------- ### CMake: Build Options Configuration Source: https://github.com/kineviz/bighorn/blob/master/third_party/pybind11/CMakeLists.txt Configures various build options for pybind11, including installation, testing, Python support, GIL management, and header directories. These options allow users to customize the build behavior based on their needs. ```cmake # Options option(PYBIND11_INSTALL "Install pybind11 header files?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_TEST "Build pybind11 test suite?" ${PYBIND11_MASTER_PROJECT}) option(PYBIND11_NOPYTHON "Disable search for Python" OFF) option(PYBIND11_SIMPLE_GIL_MANAGEMENT "Use simpler GIL management logic that does not support disassociation" OFF) set(PYBIND11_INTERNALS_VERSION "" CACHE STRING "Override the ABI version, may be used to enable the unstable ABI.") if(PYBIND11_SIMPLE_GIL_MANAGEMENT) add_compile_definitions(PYBIND11_SIMPLE_GIL_MANAGEMENT) endif() cmake_dependent_option( USE_PYTHON_INCLUDE_DIR "Install pybind11 headers in Python include directory instead of default installation prefix" OFF "PYBIND11_INSTALL" OFF) cmake_dependent_option(PYBIND11_FINDPYTHON "Force new FindPython" OFF "NOT CMAKE_VERSION VERSION_LESS 3.12" OFF) ``` -------------------------------- ### CMake: Installation Directory Configuration Source: https://github.com/kineviz/bighorn/blob/master/CMakeLists.txt Defines and sets cache variables for installation directories (lib, bin, include, CMake files). These paths can be overridden by the user during the CMake configuration step. ```cmake set(INSTALL_LIB_DIR lib CACHE PATH "Installation directory for libraries") set(INSTALL_BIN_DIR bin CACHE PATH "Installation directory for executables") set(INSTALL_INCLUDE_DIR include CACHE PATH "Installation directory for header files") set(INSTALL_CMAKE_DIR ${DEF_INSTALL_CMAKE_DIR} CACHE PATH "Installation directory for CMake files") ``` -------------------------------- ### CMake Project Setup and Basic Configuration Source: https://github.com/kineviz/bighorn/blob/master/CMakeLists.txt Initializes the CMake build system for the Kuzu project, specifying the minimum required CMake version, project name, version, and supported languages (CXX, C). It also sets core C++ standards and visibility settings. ```cmake cmake_minimum_required(VERSION 3.15) project(Kuzu VERSION 0.11.2.2 LANGUAGES CXX C) set(CMAKE_CXX_STANDARD 20) set(CMAKE_CXX_STANDARD_REQUIRED TRUE) set(CMAKE_CXX_VISIBILITY_PRESET hidden) set(CMAKE_C_VISIBILITY_PRESET hidden) set(CMAKE_EXPORT_COMPILE_COMMANDS TRUE) set(CMAKE_FIND_PACKAGE_RESOLVE_SYMLINKS TRUE) set(CMAKE_POSITION_INDEPENDENT_CODE ON) set(CMAKE_VISIBILITY_INLINES_HIDDEN ON) ``` -------------------------------- ### CMake: Minimum Version and Policy Setup Source: https://github.com/kineviz/bighorn/blob/master/third_party/pybind11/CMakeLists.txt Sets the minimum required CMake version and applies a policy version. This ensures compatibility with different CMake versions, especially for older Visual Studio versions, by providing a workaround for versioning issues. ```cmake cmake_minimum_required(VERSION 3.15) # The `cmake_minimum_required(VERSION 3.4...3.22)` syntax does not work with # some versions of VS that have a patched CMake 3.11. This forces us to emulate # the behavior using the following workaround: if(${CMAKE_VERSION} VERSION_LESS 3.22) cmake_policy(VERSION ${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}) else() cmake_policy(VERSION 3.22) endif() ``` -------------------------------- ### CMake Project Setup and Options Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/CMakeLists.txt Initializes the CMake project, sets the minimum required version, and defines project-wide options such as building shared libraries and CLI tools. It also enforces a build type if none is specified. ```cmake cmake_minimum_required(VERSION 3.15) cmake_policy(SET CMP0048 NEW) project(brotli C) option(BUILD_SHARED_LIBS "Build shared libraries" OFF) set(BROTLI_BUILD_TOOLS OFF CACHE BOOL "Build/install CLI tools") if(NOT CMAKE_BUILD_TYPE AND NOT CMAKE_CONFIGURATION_TYPES) message(STATUS "Setting build type to Release as none was specified.") set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build" FORCE) else() message(STATUS "Build type is '${CMAKE_BUILD_TYPE}'") endif() ``` -------------------------------- ### Host Kuzu Extension Server with Docker Source: https://github.com/kineviz/bighorn/blob/master/README.md This snippet demonstrates how to pull and run the Kuzu extension server Docker image. The server listens on port 8080, allowing extensions to be installed from a local environment. Ensure Docker is installed and running. ```bash docker pull ghcr.io/kuzudb/extension-repo:latest docker run -d -p 8080:80 ghcr.io/kuzudb/extension-repo:latest ``` -------------------------------- ### Include Directories Setup (CMake) Source: https://github.com/kineviz/bighorn/blob/master/extension/unity_catalog/CMakeLists.txt This CMake code configures the include paths for the project. It adds directories from the source, binary, and extension-specific locations, ensuring that headers for the main project, DuckDB, and other extensions like httpfs (for S3) are accessible during compilation. ```cmake include_directories( ${PROJECT_SOURCE_DIR}/src/include ${CMAKE_BINARY_DIR}/src/include src/include ${PROJECT_SOURCE_DIR}/extension/duckdb/src/include ${PROJECT_SOURCE_DIR}/extension/httpfs/src/include # For S3 configuration ${DuckDB_INCLUDE_DIRS}) ``` -------------------------------- ### Define Shell Flags using args Library (C++) Source: https://github.com/kineviz/bighorn/blob/master/tools/shell/shell_development_guide.md This C++ code snippet demonstrates how to define various flags for the KuzuDB shell using the 'args' library. It includes positional, help, value, and simple flags. Ensure flag strings are lowercase and follow naming conventions. The 'args::get()' function is used to retrieve values from flags. ```c++ args::ArgumentParser parser("KuzuDB Shell"); args::Positional inputDirFlag(parser, "databasePath", "Path to the database. If not given or set to \":memory:\", the database will be opened " "under in-memory mode."); args::HelpFlag help(parser, "help", "Display this help menu", {'h', "help"}); args::ValueFlag bpSizeInMBFlag(parser, "", "Size of buffer pool for default and large page sizes in megabytes", {'d', "default_bp_size", "defaultbpsize"}, -1u); args::Flag disableCompression(parser, "no_compression", "Disable compression", {"no_compression", "nocompression"}); ``` -------------------------------- ### CMake: Generating and Installing Single-File Header Source: https://github.com/kineviz/bighorn/blob/master/src/CMakeLists.txt This snippet shows how to conditionally generate a single-file header ('kuzu.hpp') for the Kuzu project. It utilizes Python to collect header content and creates a custom target that depends on this generated file, ensuring it's built when needed. It also handles the installation of both C API and the generated single-file header. ```cmake if(${BUILD_SINGLE_FILE_HEADER}) # Create a command to generate kuzu.hpp, and then create a target that is # always built that depends on it. This allows our generator to detect when # exactly to build kuzu.hpp, while still building the target by default. find_package(Python3 3.9...4 REQUIRED) add_custom_command( OUTPUT kuzu.hpp COMMAND ${Python3_EXECUTABLE} ${PROJECT_SOURCE_DIR}/scripts/collect-single-file-header.py ${CMAKE_CURRENT_BINARY_DIR}/.. DEPENDS ${PROJECT_SOURCE_DIR}/scripts/collect-single-file-header.py kuzu_shared) add_custom_target(single_file_header ALL DEPENDS kuzu.hpp) endif() install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/c_api/kuzu.h TYPE INCLUDE) if(${BUILD_SINGLE_FILE_HEADER}) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/kuzu.hpp TYPE INCLUDE) endif() ``` -------------------------------- ### Add Project Subdirectories (CMake) Source: https://github.com/kineviz/bighorn/blob/master/extension/unity_catalog/CMakeLists.txt This CMake command recursively includes build definitions from specified subdirectories. It adds the 'installer', 'main', 'connector', 'storage', and 'options' components to the build system, allowing for modular project structure and compilation. ```cmake add_subdirectory(src/installer) add_subdirectory(src/main) add_subdirectory(src/connector) add_subdirectory(src/storage) add_subdirectory(src/options) ``` -------------------------------- ### Configure Pybind11 Include Directories Source: https://github.com/kineviz/bighorn/blob/master/third_party/pybind11/CMakeLists.txt Sets the CMAKE_INSTALL_INCLUDEDIR variable based on whether Python include directories are defined and if USE_PYTHON_INCLUDE_DIR is enabled. This ensures correct installation paths for header files. ```cmake if(USE_PYTHON_INCLUDE_DIR AND DEFINED Python_INCLUDE_DIRS) file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${Python_INCLUDE_DIRS}) elseif(USE_PYTHON_INCLUDE_DIR AND DEFINED PYTHON_INCLUDE_DIR) file(RELATIVE_PATH CMAKE_INSTALL_INCLUDEDIR ${CMAKE_INSTALL_PREFIX} ${PYTHON_INCLUDE_DIRS}) endif() ``` -------------------------------- ### CMake: pybind11 Header List Source: https://github.com/kineviz/bighorn/blob/master/third_party/pybind11/CMakeLists.txt Defines the list of header files that are part of the pybind11 library. This list is used for building and potentially installing the header files as part of the project. ```cmake # NB: when adding a header don't forget to also add it to setup.py set(PYBIND11_HEADERS include/pybind11/detail/class.h include/pybind11/detail/common.h include/pybind11/detail/descr.h include/pybind11/detail/init.h include/pybind11/detail/internals.h include/pybind11/detail/type_caster_base.h include/pybind11/detail/typeid.h include/pybind11/attr.h include/pybind11/buffer_info.h include/pybind11/cast.h include/pybind11/chrono.h include/pybind11/common.h include/pybind11/complex.h include/pybind11/options.h include/pybind11/eigen.h include/pybind11/eigen/matrix.h include/pybind11/eigen/tensor.h include/pybind11/embed.h include/pybind11/eval.h include/pybind11/gil.h include/pybind11/iostream.h include/pybind11/functional.h include/pybind11/numpy.h include/pybind11/operators.h include/pybind11/pybind11.h include/pybind11/pytypes.h include/pybind11/stl.h include/pybind11/stl_bind.h include/pybind11/stl/filesystem.h) ``` -------------------------------- ### CMake Project Options Configuration Source: https://github.com/kineviz/bighorn/blob/master/CMakeLists.txt Defines various build options for the Kineviz Bighorn project using CMake's 'option' command. These options control features like building benchmarks, extensions, APIs, examples, tests, and enabling specific functionalities like LTO and backtraces. ```cmake option(AUTO_UPDATE_GRAMMAR "Automatically regenerate C++ grammar files on change." TRUE) option(BUILD_BENCHMARK "Build benchmarks." FALSE) option(BUILD_EXTENSIONS "Semicolon-separated list of extensions to build." "") option(BUILD_EXAMPLES "Build examples." FALSE) option(BUILD_JAVA "Build Java API." FALSE) option(BUILD_NODEJS "Build NodeJS API." FALSE) option(BUILD_PYTHON "Build Python API." FALSE) option(BUILD_SHELL "Build Interactive Shell" TRUE) option(BUILD_SINGLE_FILE_HEADER "Build single file header. Requires Python >= 3.9." TRUE) option(BUILD_TESTS "Build C++ tests." FALSE) option(BUILD_EXTENSION_TESTS "Build C++ extension tests." FALSE) option(BUILD_KUZU "Build Kuzu." TRUE) option(ENABLE_BACKTRACES "Enable backtrace printing for exceptions and segfaults" FALSE) option(USE_STD_FORMAT "Use std::format instead of a custom formatter." FALSE) option(PREFER_SYSTEM_DEPS "Only download certain deps if not found on the system" TRUE) option(BUILD_LCOV "Build coverage report." FALSE) ``` -------------------------------- ### Shell Configuration Struct (C++) Source: https://github.com/kineviz/bighorn/blob/master/tools/shell/shell_development_guide.md Defines the `ShellConfig` struct used to configure the KuzuDB shell. It holds settings such as the history file path, maximum row size, print width, printer implementation, and statistics display. Ensure new settings have default values. ```c++ struct ShellConfig { const char* path_to_history = ""; uint64_t maxRowSize = defaultMaxRows; uint32_t maxPrintWidth = 0; std::unique_ptr printer = std::make_unique(); bool stats = true; }; ``` -------------------------------- ### Transform pkg-config Template File (CMake) Source: https://github.com/kineviz/bighorn/blob/master/third_party/brotli/CMakeLists.txt The `transform_pc_file` function reads a `.pc.in` template file, replaces placeholder variables with actual installation paths and version information, and writes the resulting `.pc` file. It uses `generate_pkg_config_path` to resolve library and include directories. ```cmake function(transform_pc_file INPUT_FILE OUTPUT_FILE VERSION) file(READ ${INPUT_FILE} TEXT) set(PREFIX "${CMAKE_INSTALL_PREFIX}") string(REGEX REPLACE "@prefix@" "${PREFIX}" TEXT ${TEXT}) string(REGEX REPLACE "@exec_prefix@" "${PREFIX}" TEXT ${TEXT}) generate_pkg_config_path(LIBDIR "${CMAKE_INSTALL_FULL_LIBDIR}" prefix "${PREFIX}") string(REGEX REPLACE "@libdir@" "${LIBDIR}" TEXT ${TEXT}) generate_pkg_config_path(INCLUDEDIR "${CMAKE_INSTALL_FULL_INCLUDEDIR}" prefix "${PREFIX}") string(REGEX REPLACE "@includedir@" "${INCLUDEDIR}" TEXT ${TEXT}) string(REGEX REPLACE "@PACKAGE_VERSION@" "${VERSION}" TEXT ${TEXT}) file(WRITE ${OUTPUT_FILE} ${TEXT}) endfunction() ``` -------------------------------- ### Initialize Kuzu Database Instance (C++) Source: https://context7.com/kineviz/bighorn/llms.txt Demonstrates how to create and configure a Kuzu database instance. Supports both in-memory databases and persistent databases with customizable system settings like buffer pool size, threading, compression, and transaction behavior. Requires the kuzu.hpp header. ```cpp #include "kuzu.hpp" using namespace kuzu::main; // Create in-memory database auto db = std::make_unique(":memory:"); // Create persistent database with custom configuration SystemConfig config; config.bufferPoolSize = 1024 * 1024 * 1024; // 1GB buffer pool config.maxNumThreads = 4; // Use 4 threads for query execution config.enableCompression = true; // Enable on-disk compression config.readOnly = false; // Allow write operations config.autoCheckpoint = true; // Auto-checkpoint on WAL threshold config.checkpointThreshold = 16777216; // 16MB WAL size triggers checkpoint auto db_persistent = std::make_unique("/path/to/database", config); ``` -------------------------------- ### Package Prebuilt Binaries Source: https://github.com/kineviz/bighorn/blob/master/tools/nodejs_api/README.md Runs the packaging script to bundle prebuilt binaries into the npm package. This process is essential for distributing the Kuzu Node.js module with precompiled binaries for various platforms. ```bash node package ``` -------------------------------- ### Prepare and Execute Parameterized Queries in C++ Source: https://context7.com/kineviz/bighorn/llms.txt Shows how to prepare a Cypher query with parameters using the Kuzu C++ API, check for preparation errors, and then execute the prepared statement multiple times with different parameter values. This improves performance and security for repeated queries. Dependencies include the Kuzu main and common libraries. ```cpp #include "kuzu.hpp" using namespace kuzu::main; using namespace kuzu::common; auto database = std::make_unique("social_network.db"); auto connection = std::make_unique(database.get()); // Prepare a parameterized query auto preparedStmt = connection->prepare( "MATCH (p:Person) " "WHERE p.age >= $minAge AND p.isStudent = $studentStatus " "RETURN p.name, p.age, p.email"); // Check if preparation succeeded if (!preparedStmt->isSuccess()) { std::cerr << "Preparation failed: " << preparedStmt->getErrorMessage() << std::endl; return 1; } // Execute with different parameters - find adult students std::unordered_map> params1; params1["minAge"] = std::make_unique((int64_t)18); params1["studentStatus"] = std::make_unique(true); auto result1 = connection->executeWithParams(preparedStmt.get(), std::move(params1)); std::cout << "Adult students:\n" << result1->toString() << std::endl; // Execute with different parameters - find working adults std::unordered_map> params2; params2["minAge"] = std::make_unique((int64_t)25); params2["studentStatus"] = std::make_unique(false); auto result2 = connection->executeWithParams(preparedStmt.get(), std::move(params2)); std::cout << "Working adults:\n" << result2->toString() << std::endl; ``` -------------------------------- ### CMake: Setting up Kuzu Libraries and Dependencies Source: https://github.com/kineviz/bighorn/blob/master/src/CMakeLists.txt This snippet demonstrates how to define static and shared libraries for Kuzu, specify required compile definitions, and link against a comprehensive list of external libraries. It also conditionally links against specific libraries based on the operating system and build configurations. ```cmake include_directories(${CMAKE_CURRENT_BINARY_DIR}) # Have to pass this down to every subdirectory, which actually adds the files. # This doesn't affect parent directories. add_compile_definitions(KUZU_EXPORTS) add_compile_definitions(ANTLR4CPP_STATIC) add_subdirectory(binder) add_subdirectory(c_api) add_subdirectory(catalog) add_subdirectory(common) add_subdirectory(expression_evaluator) add_subdirectory(function) add_subdirectory(graph) add_subdirectory(main) add_subdirectory(optimizer) add_subdirectory(parser) add_subdirectory(planner) add_subdirectory(processor) add_subdirectory(storage) add_subdirectory(transaction) add_subdirectory(extension) add_library(kuzu STATIC ${ALL_OBJECT_FILES}) add_library(kuzu_shared SHARED ${ALL_OBJECT_FILES}) set(KUZU_LIBRARIES antlr4_cypher antlr4_runtime brotlidec brotlicommon fast_float utf8proc re2 fastpfor parquet snappy thrift yyjson zstd miniz mbedtls lz4 roaring_bitmap simsimd) if (NOT __SINGLE_THREADED__) set(KUZU_LIBRARIES ${KUZU_LIBRARIES} Threads::Threads) endif() if(NOT WIN32) set(KUZU_LIBRARIES dl ${KUZU_LIBRARIES}) endif() # Seems to be needed for clang on linux only # for compiling std::atomic::compare_exchange_weak if ((NOT APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL "Clang") AND NOT __WASM__ AND NOT __SINGLE_THREADED__) set(KUZU_LIBRARIES atomic ${KUZU_LIBRARIES}) endif() if (ENABLE_BACKTRACES) set(KUZU_LIBRARIES ${KUZU_LIBRARIES} cpptrace::cpptrace) endif() target_link_libraries(kuzu PUBLIC ${KUZU_LIBRARIES}) target_link_libraries(kuzu_shared PUBLIC ${KUZU_LIBRARIES}) unset(KUZU_LIBRARIES) set(KUZU_INCLUDES $ $ ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/include/c_api) target_include_directories(kuzu PUBLIC ${KUZU_INCLUDES}) target_include_directories(kuzu_shared PUBLIC ${KUZU_INCLUDES}) unset(KUZU_INCLUDES) if(WIN32) # Anything linking against the static library must not use dllimport. target_compile_definitions(kuzu INTERFACE KUZU_STATIC_DEFINE) endif() if(NOT WIN32) set_target_properties(kuzu_shared PROPERTIES OUTPUT_NAME kuzu) endif() install(TARGETS kuzu kuzu_shared) ``` -------------------------------- ### Kuzu C API: Initialize, Schema, Insert, Query, and Cleanup Source: https://context7.com/kineviz/bighorn/llms.txt This C code snippet demonstrates how to use the Kuzu C API to initialize a database, create a table schema, insert nodes, execute a query, and process the results. It also includes error handling and cleanup procedures for database resources. This API is suitable for integration with languages supporting C FFI. ```c #include #include #include #include "kuzu.h" int main() { kuzu_database db; kuzu_connection conn; kuzu_query_result result; // Initialize database with default configuration kuzu_system_config config = kuzu_default_system_config(); kuzu_database_init("./graph.db", config, &db); kuzu_connection_init(&db, &conn); // Create schema kuzu_connection_query(&conn, "CREATE NODE TABLE Person(name STRING, age INT64, PRIMARY KEY(name));", &result); if (!kuzu_query_result_is_success(&result)) { char* error_msg = kuzu_query_result_get_error_message(&result); printf("Schema creation failed: %s\n", error_msg); kuzu_destroy_string(error_msg); goto cleanup_result; } kuzu_query_result_destroy(&result); // Insert nodes kuzu_connection_query(&conn, "CREATE (:Person {name: 'Alice', age: 25});", &result); kuzu_query_result_destroy(&result); kuzu_connection_query(&conn, "CREATE (:Person {name: 'Bob', age: 30});", &result); kuzu_query_result_destroy(&result); // Execute query and iterate results kuzu_connection_query(&conn, "MATCH (a:Person) RETURN a.name AS NAME, a.age AS AGE ORDER BY a.age;", &result); if (!kuzu_query_result_is_success(&result)) { char* error_msg = kuzu_query_result_get_error_message(&result); printf("Query failed: %s\n", error_msg); kuzu_destroy_string(error_msg); goto cleanup_result; } // Get result metadata uint64_t num_columns = kuzu_query_result_get_num_columns(&result); printf("Number of columns: %" PRIu64 "\n", num_columns); // Iterate through results kuzu_flat_tuple tuple; kuzu_value value; while (kuzu_query_result_has_next(&result)) { kuzu_query_result_get_next(&result, &tuple); // Extract name (string at index 0) kuzu_flat_tuple_get_value(&tuple, 0, &value); char* name; kuzu_value_get_string(&value, &name); // Extract age (int64 at index 1) kuzu_flat_tuple_get_value(&tuple, 1, &value); int64_t age; kuzu_value_get_int64(&value, &age); printf("Name: %s, Age: %" PRIi64 "\n", name, age); kuzu_destroy_string(name); } // Print formatted result char* result_string = kuzu_query_result_to_string(&result); printf("\nFormatted output:\n%s", result_string); kuzu_destroy_string(result_string); kuzu_value_destroy(&value); kuzu_flat_tuple_destroy(&tuple); cleanup_result: kuzu_query_result_destroy(&result); kuzu_connection_destroy(&conn); kuzu_database_destroy(&db); return 0; } ```