|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## What this is |
| 6 | + |
| 7 | +`mgconsole` is a C++20 command-line client for the [Memgraph](https://memgraph.com) graph |
| 8 | +database. It talks the Bolt protocol via the bundled `mgclient` library, reads Cypher from |
| 9 | +either an interactive prompt (replxx) or stdin, and prints results as tabular / CSV / cypherl. |
| 10 | + |
| 11 | +## Build |
| 12 | + |
| 13 | +Dependencies are fetched and built from source via CMake `ExternalProject` (`gflags` pinned to |
| 14 | +`70c01a6`, `mgclient` pinned to `v1.5.0`); `OpenSSL` and a C++20 compiler must be present. The |
| 15 | +first configure/build is slow because of these external builds. |
| 16 | + |
| 17 | +```bash |
| 18 | +cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release . # macOS: add -DOPENSSL_ROOT_DIR="$(brew --prefix openssl)" |
| 19 | +cmake --build build |
| 20 | +cmake --install build # installs to /usr (Linux) or /usr/local (macOS) by default |
| 21 | +``` |
| 22 | + |
| 23 | +The binary lands at `build/src/mgconsole`. `compile_commands.json` is emitted into `build/`. |
| 24 | +`-Wall -Wextra -pedantic -Werror` is enabled — warnings break the build. |
| 25 | + |
| 26 | +**Static / release build** (matches what ships): `./build-generic-linux.sh` builds inside the |
| 27 | +`memgraph/mgbuild` Docker image with the Memgraph toolchain and `-DMGCONSOLE_STATIC_SSL=ON`, |
| 28 | +producing `build/generic/mgconsole`. |
| 29 | + |
| 30 | +## Test |
| 31 | + |
| 32 | +Tests are wired into CTest and require a Memgraph instance (binary or Docker image) to run |
| 33 | +against — they spin one up, exercise the client, then tear it down. |
| 34 | + |
| 35 | +```bash |
| 36 | +# Configure tests against a Docker Memgraph (no local binary needed): |
| 37 | +cmake -B build -G Ninja -DMEMGRAPH_USE_DOCKER=ON -DMEMGRAPH_DOCKER_IMAGE=memgraph/memgraph:latest |
| 38 | +cmake --build build |
| 39 | +ctest --verbose --test-dir build # runs all tests |
| 40 | + |
| 41 | +ctest --test-dir build -R parameters-unit-test # single unit test (no DB needed) |
| 42 | +ctest --test-dir build -R mgconsole-test # end-to-end I/O tests (plaintext) |
| 43 | +ctest --test-dir build -R mgconsole-secure-test # same, over SSL |
| 44 | +``` |
| 45 | + |
| 46 | +To point at a local Memgraph binary instead of Docker, configure with |
| 47 | +`-DMEMGRAPH_PATH=/path/to/memgraph` and leave `MEMGRAPH_USE_DOCKER=OFF` (default). |
| 48 | + |
| 49 | +Two test kinds: |
| 50 | +- **Unit** (`tests/unit/`): `parameters_test.cpp` links the standalone `params` library and runs |
| 51 | + without a database. |
| 52 | +- **End-to-end** (`tests/input_output/`): driven by `run-tests.sh`. For every file in `input/`, |
| 53 | + it runs the client once per output format and diffs stdout against the matching golden file in |
| 54 | + `output_tabular/`, `output_csv/`, etc. **When you change output formatting or add an input |
| 55 | + case, regenerate/add the corresponding golden file in every `output_*` directory** — the test |
| 56 | + matrix is the cross-product of inputs × formats. |
| 57 | + |
| 58 | +## Architecture |
| 59 | + |
| 60 | +`main.cpp` parses gflags, sets up signal handlers, and dispatches to exactly one of four "modes" |
| 61 | +based on whether stdin is a TTY and the `--import-mode` flag. Each mode lives in its own |
| 62 | +translation unit under the `mode::` namespace and implements a `Run(...)` entry point: |
| 63 | + |
| 64 | +- **`mode::interactive`** (`interactive.cpp`) — chosen when stdin is a TTY. The replxx REPL loop: |
| 65 | + reads a query, handles `:param`/`:params`/`:help`/`:quit` commands, executes via `mgclient`, |
| 66 | + prints results, manages history, and reconnects (3 retries) on fatal connection errors. |
| 67 | +- **`mode::serial_import`** (`serial_import.cpp`) — default non-interactive (piped) path. Reads |
| 68 | + queries one at a time and executes them in order. This is the `DUMP DATABASE | mgconsole` path. |
| 69 | +- **`mode::batch_import`** (`batch_import.cpp`) — `--import-mode=batched-parallel`. EXPERIMENTAL. |
| 70 | + Classifies each query (via `QueryInfo`) as pre/vertex/edge/post, groups vertex and edge queries |
| 71 | + into batches, and executes batches concurrently across a thread pool of `--workers-number` |
| 72 | + Bolt sessions, with exponential backoff + retry on failure. Vertices are flushed before edges |
| 73 | + because edges depend on existing vertices. Query classification is heuristic — that's why this |
| 74 | + mode is experimental. |
| 75 | +- **`mode::parsing`** (`parsing.cpp`) — `--import-mode=parser`. Parses queries and prints |
| 76 | + `QueryInfo` stats without touching the database. |
| 77 | + |
| 78 | +All four modes funnel through shared primitives in `src/utils/`, organized by namespace within |
| 79 | +`utils.hpp`/`utils.cpp` (a large ~1300-line file): |
| 80 | + |
| 81 | +- **`query::`** — `GetQuery()` reads and accumulates a complete (`;`-terminated, possibly |
| 82 | + multi-line) query from the input source, optionally producing `QueryInfo` (the has_create / |
| 83 | + has_match / has_merge / ... flags that drive batch classification). `ExecuteQuery()` / |
| 84 | + `ExecuteBatch()` run against an `mg_session`. `QueryResult` carries records, header, timing, |
| 85 | + notifications, and execution stats. |
| 86 | +- **`console::`** — TTY detection, line reading, `Echo*` output helpers (failure/info/stats). |
| 87 | +- **`format::`** — `CsvOptions` and `OutputOptions`; `Output()` renders a result set as tabular, |
| 88 | + CSV, or cypherl. |
| 89 | +- **`utils::bolt`** (`bolt.hpp`/`bolt.cpp`) — `Config` struct and `MakeBoltSession()`, the single |
| 90 | + place sessions are created (direct or routing connection). |
| 91 | + |
| 92 | +`src/parameters.{hpp,cpp}` is deliberately a **separate static library (`params`)** depending |
| 93 | +only on `mgclient`, so the `:param` parsing/storage logic can be unit-tested in isolation. Don't |
| 94 | +add heavier dependencies to it. |
| 95 | + |
| 96 | +Concurrency support for batch mode is custom and lives in `utils/`: `thread_pool`, `future` |
| 97 | +(promise/future with notification hooks), `notifier`, and `synchronized`. `mg_memory.hpp` wraps |
| 98 | +raw `mgclient` C pointers in RAII unique-ptr types (`MgSessionPtr`, `MgValuePtr`, etc.) — use |
| 99 | +these rather than managing `mg_*` lifetimes by hand. |
| 100 | + |
| 101 | +## Conventions |
| 102 | + |
| 103 | +- Every source file carries the GPLv3 license header — copy it onto new files. |
| 104 | +- Formatting is enforced by `.clang-format` (Google base, 120 col). Run `clang-format` before |
| 105 | + committing. |
| 106 | +- `MG_ASSERT` / `MG_FAIL` (`utils/assert.hpp`) are the assertion/abort macros. |
| 107 | +- `date.hpp` is a large vendored third-party header (Howard Hinnant's date lib) — don't edit it. |
0 commit comments