Skip to content

Commit 659efa6

Browse files
committed
C++ examples and docs
1 parent d03c88d commit 659efa6

6 files changed

Lines changed: 159 additions & 77 deletions

File tree

.github/workflows/cpp-examples.yml

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
name: C++ examples verification
2+
3+
# Builds the C++ examples with CMake and smoke-tests the simplest one against a
4+
# fixture SQLite database. Mirrors python-examples.yml, but for the header-only
5+
# C++ API. The fixture DB is built with the `rcdb` CLI (the canonical way to
6+
# initialize an RCDB database) so there is no checked-in binary to drift.
7+
8+
on:
9+
push:
10+
branches: ['*']
11+
pull_request:
12+
branches: [main]
13+
14+
env:
15+
# 4-slash form: SQLAlchemy (used to seed) and the C++ SqLiteProvider parser
16+
# (strips the leading "sqlite:///") both resolve this to the same absolute path.
17+
RCDB_TEST_CONNECTION: "sqlite:////${{ github.workspace }}/cpp_examples.sqlite"
18+
19+
jobs:
20+
cpp-examples:
21+
runs-on: ubuntu-latest
22+
23+
steps:
24+
- uses: actions/checkout@v6
25+
26+
- name: Install build dependencies
27+
run: |
28+
sudo apt-get update
29+
sudo apt-get install -y cmake g++ libsqlite3-dev
30+
31+
- name: Set up Python
32+
uses: actions/setup-python@v6
33+
with:
34+
python-version: "3.12"
35+
36+
- name: Install rcdb (provides the `rcdb db init` test fixture)
37+
run: |
38+
python -m pip install --upgrade pip
39+
pip install --editable $GITHUB_WORKSPACE/python
40+
41+
# The C++ examples read from a real RCDB database. We seed a SQLite one
42+
# with the cpp test fixture, then add an `event_count` value for run 1 so
43+
# the `simple` example (which looks up event_count) has data to read.
44+
- name: Create the example SQLite database
45+
run: |
46+
rcdb -c "$RCDB_TEST_CONNECTION" db init --add-cpp-tests --confirm
47+
rcdb -c "$RCDB_TEST_CONNECTION" add condition 1 event_count 12345
48+
49+
- name: Configure and build C++ examples (SQLite only)
50+
run: |
51+
cmake -S $GITHUB_WORKSPACE/cpp -B $GITHUB_WORKSPACE/cpp/build -DWITH_SQLITE=ON -DWITH_MYSQL=OFF
52+
cmake --build $GITHUB_WORKSPACE/cpp/build -j
53+
54+
# ── Smoke-test the examples ───────────────────────────────────
55+
# Each example that can run against the fixture DB gets its own step so
56+
# failures are visible per-binary.
57+
58+
- name: "Example: simple (read event_count)"
59+
run: |
60+
OUT=$("$GITHUB_WORKSPACE/cpp/build/examples_simple" "$RCDB_TEST_CONNECTION" 1)
61+
echo "$OUT"
62+
echo "$OUT" | grep -q "event_count is: 12345"

cpp/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# RCDB_TEST_CONNECTION="sqlite:///<path-to>.sqlite" ./build/test_rcdb_cpp
88
# Create the test database with: rcdb -c sqlite:///<path-to>.sqlite db init --add-cpp-tests --confirm
99

10-
cmake_minimum_required(VERSION 3.4)
10+
cmake_minimum_required(VERSION 3.5)
1111
project(rcdb_cpp)
1212

1313
option(WITH_MYSQL "Compile with MySQL support" OFF)
@@ -96,6 +96,7 @@ if(WITH_SQLITE)
9696
endif()
9797

9898
add_executable(test_rcdb_cpp ${TEST_SOURCE_FILES})
99+
add_executable(examples_simple examples/simple.cpp)
99100
add_executable(examples_trigger_params examples/get_trigger_params.cpp)
100101
add_executable(examples_fadc_masks examples/get_fadc_masks.cpp)
101102
add_executable(examples_write_conditions examples/write_conditions.cpp)
@@ -104,6 +105,7 @@ add_executable(examples_write_objects_to_json examples/write_objects_to_json.cpp
104105

105106
target_link_libraries(test_rcdb_cpp ${DB_REQIRED_LIBRARIES} dl pthread sqlite3)
106107
target_link_libraries(examples_trigger_params ${DB_REQIRED_LIBRARIES} dl pthread)
108+
target_link_libraries(examples_simple ${DB_REQIRED_LIBRARIES} dl pthread)
107109
target_link_libraries(examples_fadc_masks ${DB_REQIRED_LIBRARIES} dl pthread)
108110
target_link_libraries(examples_write_conditions ${DB_REQIRED_LIBRARIES} dl pthread)
109111
target_link_libraries(examples_write_array_to_json ${DB_REQIRED_LIBRARIES} dl pthread)

docs/Cpp.md

Lines changed: 41 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -13,76 +13,64 @@ C++ API code is located in [$RCDB_HOME/cpp](https://github.com/JeffersonLab/rcdb
1313

1414
## Installation
1515

16-
TL; DR; version:
16+
RCDB C++ API is **header-only** (since v0.03) — there is no separate library build step.
17+
The CMake project in `$RCDB_HOME/cpp` builds the unit tests and examples, and handles
18+
all compiler flags and library linkage for you.
1719

18-
**Just include headers and:**
19-
20-
* define ```RCDB_MYSQL``` for MySQL, ```RCDB_SQLITE``` for SQLite
21-
* Ensure libs and headers are included.
20+
### Dependencies
2221

23-
Compile and run the simplest example for SQLite
22+
#### Ubuntu / Debian
2423

25-
```bash
26-
> gcc $RCDB_HOME/cpp/examples/simple.cpp -o simple -I$RCDB_HOME/cpp/include/ -std=c++11 -lstdc++ -lsqlite3 -DRCDB_SQLITE
24+
```bash
25+
# SQLite
26+
sudo apt-get install libsqlite3-dev
2727

28-
> ./simple sqlite:////path/to/db/rcdb.sqlite 10452
28+
# MySQL (either one)
29+
sudo apt-get install libmysqlclient-dev
30+
# or
31+
sudo apt-get install libmariadbclient-dev
2932
```
3033

31-
with MySQL support:
32-
```
33-
> gcc $RCDB_HOME/cpp/examples/simple.cpp -o simple -I$RCDB_HOME/cpp/include/ -std=c++11 -lstdc++ -DRCDB_MYSQL `mysql_config --libs --cflags --include`
34+
#### CentOS / Fedora
3435

35-
> ./simple mysql://rcdb@hallddb.jlab.org/rcdb 10452
36+
```bash
37+
sudo dnf install sqlite-devel mysql-devel
3638
```
3739

38-
Combine both to have MySQL and SQLite working together
39-
40-
---
41-
42-
RCDB C++ API is a header only since 0.03. Which means there is no more librcdb and separate step for RCDB.
43-
That also means that MySQL and SQLite libraries should be linked to the application which includes RCDB headers.
44-
45-
In order for your code to build ensure flags/configuration:
46-
47-
* There is at least C++11 support enabled and stdc++ library linked. This means that probably minimum GCC version to be used is 4.8:
48-
49-
```-std=c++11 -lstdc++```
50-
51-
* For MySQL:
52-
53-
* Define ```RCDB_MYSQL```
54-
* Add mysql-connector includes and libs. There is useful ```mysql_config``` script:
55-
56-
``` -DRCDB_MYSQL `mysql_config --libs --cflags --include` ```
57-
58-
* For SQLite:
59-
60-
* Define ```RCDB_SQLITE```:
61-
* Link libsqlite3
62-
63-
``` -DRCDB_SQLITE -lsqlite3 ```
64-
65-
66-
** Defining RCDB_MYSQL or RCDB_SQLITE **
40+
### SQLite only
6741

42+
```bash
43+
cd $RCDB_HOME/cpp
44+
cmake -S . -B build -DWITH_SQLITE=ON -DWITH_MYSQL=OFF
45+
cmake --build build --target examples_simple
6846

69-
* Code ```#define RCDB_MYSQL```
70-
* Compiler arguments ```-DRCDB_MYSQL```
71-
* CMake ```-DWITH_MYSQL=ON``` (which does ```add_definitions(-DRCDB_MYSQL)```)
47+
./build/examples_simple sqlite:////path/to/db/rcdb.sqlite 10452
48+
```
7249

50+
### MySQL only
7351

74-
### Dependencies
52+
```bash
53+
cd $RCDB_HOME/cpp
54+
cmake -S . -B build -DWITH_SQLITE=OFF -DWITH_MYSQL=ON
55+
cmake --build build --target examples_simple
7556

76-
#### Ubuntu
57+
./build/examples_simple mysql://rcdb@hallddb.jlab.org/rcdb 10452
58+
```
7759

78-
* MySQL ```libmysqlclient-dev``` or ```libmariadbclient-dev```
79-
* SQLite ```libsqlite3-dev```
60+
### MySQL + SQLite (both)
8061

81-
```sudo apt-get install libmariadbclient-dev libsqlite3-dev -y```
62+
```bash
63+
cd $RCDB_HOME/cpp
64+
cmake -S . -B build -DWITH_SQLITE=ON -DWITH_MYSQL=ON
65+
cmake --build build --target examples_simple
8266

83-
#### CentOS/Fedora
67+
# use either connection string:
68+
./build/examples_simple sqlite:////path/to/db/rcdb.sqlite 10452
69+
./build/examples_simple mysql://rcdb@hallddb.jlab.org/rcdb 10452
70+
```
8471

85-
... please add, somebody ...
72+
> CMake sets the required compile definitions (`-DRCDB_SQLITE`, `-DRCDB_MYSQL`) and links
73+
> the correct libraries automatically based on the `-DWITH_*` options.
8674
8775
<br/>
8876
<br/>
@@ -151,7 +139,7 @@ targets (e.g. `examples_trigger_params`). There is no separate install step or c
151139

152140
**List of examples:**
153141

154-
* [simple.cpp](https://github.com/JeffersonLab/rcdb/blob/main/cpp/examples/simple.cpp) - Simple condition readout. Note: this example has no CMake target and is not built by `cmake --build build`; compile it manually with `gcc` as shown in the Installation section.
142+
* [simple.cpp](https://github.com/JeffersonLab/rcdb/blob/main/cpp/examples/simple.cpp) (target `examples_simple`) - Simple condition readout. Used as the introductory build example in the Installation section above.
155143
* [get_trigger_params.cpp](https://github.com/JeffersonLab/rcdb/blob/main/cpp/examples/get_trigger_params.cpp) (target `examples_trigger_params`) - Versatile data readout example. It includes:
156144
* Reading conditions
157145
* Working with JSON serialized objects

docs/get-started/installation.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ or setup data updating scripts. Reconstruction frameworks in C++ or JAVA would l
55
RCDB API and use it. Developers and system administrators - install RCDB development database,
66
install website on hosting etc. We can split installation into chapters:
77

8-
1. ***Python (library, command line interface, user GUI (local website))***
9-
2. C++ library compilation - see C++ section
10-
3. Java library compilation - see Java section
11-
4. Development install (git clone, MySQL setup, server management) - see Development section
8+
1. [**Python** (library, command line interface, user GUI (local website))](./python)
9+
2. [**C++ library** compilation](../Cpp)
10+
3. [**Java library** compilation](../Java)
11+
4. [**Development install** (git clone, MySQL setup, server management)](../development/development)
1212

1313

1414
## Python

docs/get-started/select-values.md

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
- [TL;DR; aka Too long didn't read](#tl-dr)
2+
- [CLI: rcdb select](#cli-rcdb-select)
23
- [Select and filter](#select-and-filter)
34
- [All options](#all-options)
45
- [Result details](#result-details)
56
- [Performance](#performance)
6-
- [From shell](#from-shell)
7+
- [Shell one-liner](#shell-one-liner)
78

89
## TLDR
910

10-
Fastest way to select values in 3 lines (using HallD rcdb as example):
11+
The two ways to select run values from RCDB:
12+
13+
- **Python API**`db.select_values(...)` — best for scripts and analysis workflows (see below)
14+
- **CLI**`rcdb select` — great for quick interactive queries and shell scripts (see [CLI: rcdb select](#cli-rcdb-select))
15+
16+
Fastest way to select values in 3 lines using the Python API (using HallD rcdb as example):
1117

1218
```python
1319
# import RCDB
@@ -30,6 +36,24 @@ table = db.select_values(['polarization_angle','beam_current'], "@is_production"
3036

3137
<br>
3238

39+
## CLI: rcdb select
40+
41+
The `rcdb select` command lets you query runs and view condition values directly from the terminal without writing a Python script:
42+
43+
```bash
44+
rcdb select "@is_production and event_count>1000" 30000-31000
45+
```
46+
47+
The first argument is a [selection query](query-syntax), the optional second argument is a run range (`30000-31000`). You can also specify which condition columns to display, use `--dump`/`-d` for an export-friendly output without table borders, and `--desc`/`--asc` to control the sort order:
48+
49+
```bash
50+
rcdb select "@is_production" 30000-31000 "event_count beam_current" --dump --desc
51+
```
52+
53+
See [`rcdb-cli`](../rcdb-cli) for the full reference.
54+
55+
<br>
56+
3357
## Select and filter
3458
3559
The fastest designed way to get values from RCDB is by using ```select_values``` function.
@@ -139,26 +163,12 @@ More info about old select runs && get values API: [Select runs & get values DEP
139163
140164
<br>
141165
142-
## From shell
143-
(Shell one liner)
144-
145-
Suppose one wants to select values in a bash script but doesn't want to create a separate python script.
146-
It is possible to call ```python -c "semicolon;separated;commands"```
166+
## Shell one-liner
147167
148-
Combining everything in such one-liner:
168+
For embedding a quick value fetch in a bash script without a separate file, you can also use the Python one-liner form:
149169
150170
```bash
151171
python -c "import rcdb.provider;t=rcdb.provider.RCDBProvider('mysql://rcdb@hallddb.jlab.org/rcdb2').select_values(['polarization_angle','polarization_direction'], run_min=30000, run_max=31000);print('\n'.join([' '.join(map(str, r)) for r in t]))"
152172
```
153173
154-
Shouldn't be there an easier way? There is the ```rcdb select``` command doing it:
155-
156-
```bash
157-
rcdb select "@is_production and event_count>1000" 30000-31000
158-
```
159-
160-
The first argument is a selection query, and the optional second argument is a run range (e.g. ```30000-31000```). You can also pass view columns to display, use ```--dump```/```-d``` for an export-friendly output without table borders, and ```--desc```/```--asc``` to control the run number sort order:
161-
162-
```bash
163-
rcdb select "@is_production" 30000-31000 "event_count beam_current" --dump --desc
164-
```
174+
For most shell use cases `rcdb select` (see [above](#cli-rcdb-select)) is simpler.

docs/python.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,26 @@
22

33
Below is a high-level overview of RCDB’s Python API
44

5+
```python
6+
from rcdb import RCDBProvider
7+
8+
# Connect (MySQL or SQLite)
9+
db = RCDBProvider("mysql://rcdb@hallddb.jlab.org/rcdb2")
10+
11+
# Select run values with a query
12+
table = db.select_values(["beam_current", "event_count"],
13+
"@is_production and beam_current > 100",
14+
run_min=30000, run_max=31000)
15+
16+
for row in table:
17+
run_number, beam_current, event_count = row
18+
print(run_number, beam_current, event_count)
19+
```
20+
21+
See [Select Values](get-started/select-values) for more on querying, or read on for a full API reference.
22+
23+
---
24+
525
## Core Aspects
626

727
1. **SQLAlchemy ORM**

0 commit comments

Comments
 (0)