### Build Example with GLib2 Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds an example executable that links against Hiredis and GLib2. Requires GLib2 development files to be installed. ```cmake INCLUDE(FindPkgConfig) # Check for GLib PKG_CHECK_MODULES(GLIB2 glib-2.0) if (GLIB2_FOUND) INCLUDE_DIRECTORIES(${GLIB2_INCLUDE_DIRS}) LINK_DIRECTORIES(${GLIB2_LIBRARY_DIRS}) ADD_EXECUTABLE(example-glib example-glib.c) TARGET_LINK_LIBRARIES(example-glib hiredis ${GLIB2_LIBRARIES}) ENDIF(GLIB2_FOUND) ``` -------------------------------- ### Configure Installation Prefix Source: https://github.com/snapchat/keydb/blob/main/deps/jemalloc/INSTALL.md Set the base directory for jemalloc installation. This example installs files into /usr/local. ```bash ./configure --prefix=/usr/local ``` -------------------------------- ### Build Basic Hiredis Example Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds a basic example executable that links only against Hiredis. ```cmake ADD_EXECUTABLE(example example.c) TARGET_LINK_LIBRARIES(example hiredis) ``` -------------------------------- ### Build Example with libuv Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds an example executable that links against Hiredis and libuv. Requires libuv development files. ```cmake FIND_PATH(LIBUV uv.h) IF (LIBUV) ADD_EXECUTABLE(example-libuv example-libuv.c) TARGET_LINK_LIBRARIES(example-libuv hiredis uv) ENDIF() ``` -------------------------------- ### Build Example with libev Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds an example executable that links against Hiredis and libev. Requires libev development files. ```cmake FIND_PATH(LIBEV ev.h HINTS /usr/local /usr/opt/local ENV LIBEV_INCLUDE_DIR) if (LIBEV) # Just compile and link with libev ADD_EXECUTABLE(example-libev example-libev.c) TARGET_LINK_LIBRARIES(example-libev hiredis ev) ENDIF() ``` -------------------------------- ### Installation and Package Configuration Source: https://github.com/snapchat/keydb/blob/main/deps/cpp-statsd-client/CMakeLists.txt Configures installation rules for the library targets, package version file, and CMake configuration files. It ensures the library, headers, and configuration are installed to the correct locations. ```cmake # The installation and pkg-config-like cmake config install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}_Targets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY SameMajorVersion) configure_package_config_file( "${PROJECT_SOURCE_DIR}/cmake/${PROJECT_NAME}Config.cmake.in" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) install(EXPORT ${PROJECT_NAME}_Targets FILE ${PROJECT_NAME}Targets.cmake NAMESPACE ${PROJECT_NAME}:: DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) install(DIRECTORY ${PROJECT_SOURCE_DIR}/include DESTINATION include) ``` -------------------------------- ### Build and Install Memkind Source: https://github.com/snapchat/keydb/blob/main/deps/memkind/src/README.md Builds and installs the memkind library using the provided build script. ```bash ./build.sh make install ``` -------------------------------- ### Build Hiredis Push Example Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds an example executable for Hiredis push functionality, linking against Hiredis. ```cmake ADD_EXECUTABLE(example-push example-push.c) TARGET_LINK_LIBRARIES(example-push hiredis) ``` -------------------------------- ### Build Example for macOS with CoreFoundation Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds an example executable for macOS that links against Hiredis and the CoreFoundation framework. ```cmake IF (APPLE) FIND_LIBRARY(CF CoreFoundation) ADD_EXECUTABLE(example-macosx example-macosx.c) TARGET_LINK_LIBRARIES(example-macosx hiredis ${CF}) ENDIF() ``` -------------------------------- ### Build Example with libevent Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/examples/CMakeLists.txt Configures and builds an example executable that links against Hiredis and libevent. Requires libevent development files. ```cmake FIND_PATH(LIBEVENT event.h) if (LIBEVENT) ADD_EXECUTABLE(example-libevent example-libevent) TARGET_LINK_LIBRARIES(example-libevent hiredis event) ENDIF() ``` -------------------------------- ### Install Additional Build Dependencies Source: https://github.com/snapchat/keydb/blob/main/README.md Install optional dependencies that may be needed for certain KeyDB build configurations. ```bash % sudo apt-get install autoconf autotools-dev libnuma-dev libtool ``` -------------------------------- ### Build and Install Developer Sources Source: https://github.com/snapchat/keydb/blob/main/deps/jemalloc/INSTALL.md Steps for building and installing from unpackaged developer sources. This sequence may be necessary when working with the latest development code. ```bash ./autogen.sh make dist make make install ``` -------------------------------- ### Install KeyDB Binaries Source: https://github.com/snapchat/keydb/blob/main/README.md Install KeyDB binaries to the default /usr/local/bin directory. You can specify a different destination using the PREFIX variable. ```bash % make install ``` ```bash make PREFIX=/some/other/directory install ``` -------------------------------- ### Configure with Installation Suffix Source: https://github.com/snapchat/keydb/blob/main/deps/memkind/src/jemalloc/INSTALL.md Append a suffix to installed file names using --with-install-suffix to allow multiple versions. ```bash --with-install-suffix= ``` -------------------------------- ### Install Hiredis SSL Library and Headers Source: https://github.com/snapchat/keydb/blob/main/deps/hiredis/CMakeLists.txt Installs the hiredis_ssl library, headers, and pkg-config file. This is done only if ENABLE_SSL is set to ON. ```cmake INSTALL(TARGETS hiredis_ssl EXPORT hiredis_ssl-targets RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) INSTALL(FILES hiredis_ssl.h DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/hiredis) INSTALL(FILES ${CMAKE_CURRENT_BINARY_DIR}/hiredis_ssl.pc DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig) ``` -------------------------------- ### Install C++ StatsD Client using Makefile Source: https://github.com/snapchat/keydb/blob/main/deps/cpp-statsd-client/README.md Use this command to install the header files for the C++ StatsD client. ```shell make install ``` -------------------------------- ### Build and Install Packaged Release Source: https://github.com/snapchat/keydb/blob/main/deps/jemalloc/INSTALL.md Standard procedure for installing a packaged release of jemalloc. Ensure you are in the root directory of the source tree. ```bash ./configure make make install ``` -------------------------------- ### Jemalloc Installation with Make Variables Source: https://github.com/snapchat/keydb/blob/main/deps/jemalloc/INSTALL.md Optionally define make variables when invoking make for custom installation paths and build configurations. ```makefile INCLUDEDIR="?" LIBDIR="?" MANDIR="?" DESTDIR="?" CC="?" CFLAGS="?" CPPFLAGS="?" LDFLAGS="?" PATH="?" ``` -------------------------------- ### Lua Table Constructor Equivalent Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Shows the equivalent imperative code for the complex table constructor example. This clarifies how fields are assigned and indexed. ```lua do local t = {} t[f(1)] = g t[1] = "x" -- 1st exp t[2] = "y" -- 2nd exp t.x = 1 -- t["x"] = 1 t[3] = f(x) -- 3rd exp t[30] = 23 t[4] = 45 -- 4th exp a = t end ``` -------------------------------- ### Lua Logical Operators Examples Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Demonstrates the short-circuiting behavior and return values of Lua's 'and' and 'or' logical operators. ```lua 10 or 20 --> 10 10 or error() --> 10 nil or "a" --> "a" nil and 10 --> nil false and error() --> false false and nil --> false false or nil --> nil 10 and 20 --> 20 ``` -------------------------------- ### Lua Coroutine Example Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Demonstrates the creation and sequential resumption of a Lua coroutine, showing the flow of control and data between the main thread and the coroutine body. ```lua function foo (a) print("foo", a) return coroutine.yield(2*a) end co = coroutine.create(function (a,b) print("co-body", a, b) local r = foo(a+1) print("co-body", r) local r, s = coroutine.yield(a+b, a-b) print("co-body", r, s) return b, "end" end) print("main", coroutine.resume(co, 1, 10)) print("main", coroutine.resume(co, "r")) print("main", coroutine.resume(co, "x", "y")) print("main", coroutine.resume(co, "x", "y")) ``` -------------------------------- ### Lua Closures and Upvalues Example Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Shows how closures created within a loop capture distinct local variables (upvalues) and share outer local variables. ```lua a = {} local x = 20 for i=1,10 do local y = 0 a[i] = function () y=y+1; return x+y end end ``` -------------------------------- ### Lua Multiple Assignment Example Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Demonstrates multiple variable assignment and value adjustment. Excess values are discarded, and fewer values are padded with nil. ```lua i = 3 i, a[i] = i+1, 20 ``` ```lua x, y = y, x ``` ```lua x, y, z = y, z, x ``` -------------------------------- ### Example C Function for Lua Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html An example C function that calculates the average and sum of numerical arguments passed from Lua. It demonstrates stack manipulation, type checking, and returning multiple values. ```c static int foo (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ lua_Number sum = 0; int i; for (i = 1; i <= n; i++) { if (!lua_isnumber(L, i)) { lua_pushstring(L, "incorrect argument"); lua_error(L); } sum += lua_tonumber(L, i); } lua_pushnumber(L, sum/n); /* first result */ lua_pushnumber(L, sum); /* second result */ return 2; /* number of results */ } ``` -------------------------------- ### Lua Lexical Scoping Example Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Demonstrates lexical scoping in Lua, showing how local variables are declared within blocks and how inner scopes can access outer variables. ```lua x = 10 -- global variable do -- new block local x = x -- new 'x', with value 10 print(x) --> 10 x = x+1 do -- another block local x = x+1 -- another 'x' print(x) --> 12 end print(x) --> 11 end print(x) --> 10 (the global one) ``` -------------------------------- ### Lua Table Constructor Initialization Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Demonstrates initializing a Lua table with various field types, including computed keys, string keys, sequential elements, and explicit numerical keys. Fields are added sequentially starting from 1 unless explicitly keyed. ```lua a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 } ``` -------------------------------- ### Lua Non-Tail Call Example Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html These examples are not tail calls because they involve adjustments to return values, additional expressions, or discarding results, preventing stack frame reuse. ```lua return 2 * f(x) ``` ```lua return x, f(x) ``` ```lua f(x); return ``` ```lua return x or f(x) ``` -------------------------------- ### Creating a Coroutine Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Use `coroutine.create` to create a new coroutine. It takes a function as an argument, which serves as the coroutine's main function. This function only creates the coroutine and returns a handle; it does not start execution. ```Lua coroutine.create(function() ... end) ``` -------------------------------- ### Lua Arithmetic Operators Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Provides examples of Lua's standard arithmetic operators, including binary operators (+, -, *, /, %, ^) and the unary negation operator (-). Operations are performed on numbers or strings convertible to numbers. ```lua x^(-0.5) computes the inverse of the square root of x ``` -------------------------------- ### Lua Tail Call Example Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html This is NOT a tail call. Lua requires a specific syntax for tail calls where the 'return' statement directly precedes a single function call to reuse the stack frame. ```lua return (f(x)) ``` -------------------------------- ### Accessing Global Variables in Lua C API Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Demonstrates how to get the value of a global variable from Lua's environment table using lua_getfield. ```c lua_getfield(L, LUA_GLOBALSINDEX, varname); ``` -------------------------------- ### Resuming a Coroutine Source: https://github.com/snapchat/keydb/blob/main/deps/lua/doc/manual.html Call `coroutine.resume` to start or resume a coroutine. Pass the coroutine's handle as the first argument. Any additional arguments are passed to the coroutine's main function or the point where it yielded. Returns `true` on success, `false` on error. ```Lua coroutine.resume(thread, arg1, arg2) ``` -------------------------------- ### Basic Project Setup Source: https://github.com/snapchat/keydb/blob/main/deps/cpp-statsd-client/CMakeLists.txt Initializes the CMake build system, sets the project name, version, language, description, and homepage URL. ```cmake cmake_minimum_required(VERSION 3.5) project(cpp-statsd-client VERSION 1.0.2 LANGUAGES CXX DESCRIPTION "A header-only StatsD client implemented in C++" HOMEPAGE_URL "https://github.com/vthiery/cpp-statsd-client") ``` -------------------------------- ### Enable Active-Active Replication Source: https://github.com/snapchat/keydb/blob/main/README.md Enable active-active replication, allowing multiple instances to accept reads and writes while staying synced. Refer to KeyDB documentation for detailed setup and examples. ```config active-replica yes ```