### Start CubDB Database Source: https://github.com/lucaong/cubdb/blob/master/README.md Starts the CubDB database process, specifying a directory for its data files. The directory will be created if it does not exist. Ensure only one CubDB process uses a given data directory. ```elixir {:ok, db} = CubDB.start_link(data_dir: "my/data/directory") ``` -------------------------------- ### Basic CubDB Operations (Put, Get, Delete) Source: https://github.com/lucaong/cubdb/blob/master/README.md Demonstrates the fundamental put, get, and delete operations for key-value pairs in CubDB. 'get' returns nil if the key does not exist. ```elixir CubDB.put(db, :foo, "some value") #=> :ok CubDB.get(db, :foo) #=> "some value" CubDB.delete(db, :foo) #=> :ok CubDB.get(db, :foo) #=> nil ``` -------------------------------- ### Add CubDB Dependency to mix.exs Source: https://github.com/lucaong/cubdb/blob/master/README.md Add the CubDB dependency to your project's mix.exs file to install the library. ```elixir def deps do [ {:cubdb, "~> 2.0.2"} ] end ``` -------------------------------- ### Atomic Transaction in CubDB Source: https://github.com/lucaong/cubdb/blob/master/README.md Performs multiple operations atomically using the `transaction` function and `CubDB.Tx` module. This example shows swapping the values of two keys. ```elixir # Swapping :a and :b atomically: CubDB.transaction(db, fn tx -> a = CubDB.Tx.get(tx, :a) b = CubDB.Tx.get(tx, :b) tx = CubDB.Tx.put(tx, :a, b) tx = CubDB.Tx.put(tx, :b, a) {:commit, tx, :ok} end) #=> :ok ``` -------------------------------- ### Process Select Results with Stream Functions Source: https://github.com/lucaong/cubdb/blob/master/README.md Demonstrates processing a lazy stream from `CubDB.select` using `Stream` functions to filter, transform, and aggregate data. This example calculates the sum of the last 3 even values. ```elixir # select entries in reverse order CubDB.select(db, reverse: true) |> Stream.map(fn {_key, value} -> value end) # discard the key and keep only the value |> Stream.filter(fn value -> is_integer(value) && Integer.is_even(value) end) # filter only even integers |> Stream.take(3) # take the first 3 values |> Enum.sum() # sum the values #=> 18 ``` -------------------------------- ### Storing Collections with Composite Keys Source: https://github.com/lucaong/cubdb/blob/master/HOWTO.md Use composite keys, typically tuples, to store and differentiate between multiple collections within a single CubDB database. This example shows adding people and articles. ```elixir # Add a few people: :ok = CubDB.put(db, {:people, 1}, %{first_name: "Margaret", last_name: "Hamilton"}) :ok = CubDB.put(db, {:people, 2}, %{first_name: "Alan", last_name: "Turing"}) # Add a few articles: :ok = CubDB.put(db, {:articles, 1}, %{title: "Spaceship Guidance made easy", text: "..."}) :ok = CubDB.put(db, {:articles, 2}, %{title: "Morphogenesis for the uninitiated", text: "..."}) ``` -------------------------------- ### Backing Up and Restoring CubDB Database Source: https://github.com/lucaong/cubdb/blob/master/HOWTO.md Create a backup of the current database state to a specified directory and then open that backup as a new CubDB process. This is useful for data migration or recovery. ```elixir # Backup the current state of the database :ok = CubDB.back_up(db, "some/target/path") # Open the backup as another CubDB process {:ok, copy} = CubDB.start_link(data_dir: "some/target/path") ``` -------------------------------- ### v1 Select with Pipe and Reduce vs. v2 Stream-based Select Source: https://github.com/lucaong/cubdb/blob/master/UPGRADING.md Compares the v1 syntax for `CubDB.select` using `:pipe` and `:reduce` options with the equivalent v2 implementation that utilizes `Stream` and `Enum` modules for lazy processing. This demonstrates how to achieve similar data transformation and aggregation in v2. ```elixir {:ok, product} = CubDB.select(db, min_key: :foo, max_key: :bar, pipe: [ map: fn {_, val} -> val end, filter: fn val -> val > 0 end ], reduce: fn val, acc -> val * acc end ) ``` ```elixir product = CubDB.select(db, min_key: :foo, max_key: :bar) |> Stream.map(fn {_, val} -> val end) |> Stream.filter(fn val -> val > 0 end) |> Enum.reduce(fn val, acc -> val * acc end) ``` -------------------------------- ### Selecting All Items in a Collection Source: https://github.com/lucaong/cubdb/blob/master/HOWTO.md Select all records belonging to a specific collection by using a range query with composite keys. This leverages tuple comparison to define the boundaries of the collection. ```elixir # Select all people {:ok, people_wth_keys} = CubDB.select(db, min_key: {:people, 0}, max_key: {:people, nil}) # Select all articles {:ok, articles_with_keys} = CubDB.select(db, min_key: {:articles, 0}, max_key: {:articles, nil}) ``` -------------------------------- ### Retrieving Specific Items by Composite Key Source: https://github.com/lucaong/cubdb/blob/master/HOWTO.md Retrieve individual records from a collection by specifying their composite key. This is useful for accessing a single person or article by its unique ID. ```elixir person = CubDB.get(db, {:people, 1}) article = CubDB.get(db, {:articles, 2}) ``` -------------------------------- ### Select Entries by Key Range in CubDB Source: https://github.com/lucaong/cubdb/blob/master/README.md Retrieves a range of entries sorted by key using the `select` function. The result is a lazy stream that can be processed with `Enum` and `Stream` modules. ```elixir CubDB.select(db, min_key: :b, max_key: :e) |> Enum.to_list() #=> [b: 2, c: 3, d: 4, e: 5] ``` -------------------------------- ### Batch Put Operation in CubDB Source: https://github.com/lucaong/cubdb/blob/master/README.md Uses `put_multi` to atomically insert multiple key-value pairs into CubDB. This is more efficient than individual `put` operations for multiple entries. ```elixir CubDB.put_multi(db, [a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8]) #=> :ok ``` -------------------------------- ### Read-Only Snapshot Operations in CubDB Source: https://github.com/lucaong/cubdb/blob/master/README.md Uses `with_snapshot` to perform read operations that are isolated from concurrent writes without blocking them. This is preferable to transactions when no writes are needed. ```elixir # the key of y depends on the value of x, so we ensure consistency by getting # both entries from the same snapshot, isolating from the effects of concurrent # writes {x, y} = CubDB.with_snapshot(db, fn snap -> x = CubDB.Snapshot.get(snap, :x) y = CubDB.Snapshot.get(snap, x) {x, y} end) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.