NAME
ttypt/joint.h - Public API for the Interval Tree Library (libjoint).
SYNOPSIS
#include <time.h>
#include <sys/types.h>
#include <ttypt/corm.h>
Macros
Maximum length for ISO-8601 date string buffers.
Typedefs
Opaque iterator handle for interval tree traversal.
Functions
Initialize an interval tree database.
Close an interval tree database and free resources.
Start a new interval for an entity.
Stop an interval for an entity.
Begin iterating over intervals that intersect a time range.
Get the next interval and entity from an iterator.
Parse an ISO-8601 date string or Unix timestamp.
Format a Unix timestamp as an ISO-8601 date string.
Recall-kernel time-axis filler (see ttypt/rec.h).
Erase every interval an entity owns (Phase 2A inverse of start/stop).
Detailed Description
Public API for the Interval Tree Library (libjoint).
Provides an efficient interval tree implementation for tracking time-based intervals with persistence support. Built on libcorm for high-performance querying and sorted iteration.
Key features:
- Store and query time intervals with associated IDs
- File-based persistence via corm
- Efficient intersection queries
- Multiple secondary indexes (by max time, by ID)
- Sorted iteration support
ID-uniformity contract (see RECALL-KERNEL.md): entity ids are stored and compared internally as unsigned (32-bit) -- this is intentional, not a gap. The uniformity boundary other axis libraries (libsepal, liblemma) share is rec_ref_t (uint32_t, from rec.h), and it is enforced at the fill boundary only: rec_axis_fill_interval() widens every who to rec_ref_t before pushing it into the caller's rec_set_t. Internal storage width is each library's own choice (see libislet's identical convention for its per-cell values). Do not assume ids above UINT32_MAX round-trip through joint_start()/joint_stop()/joint_next().
See also
corm.h for underlying storage implementation
Macro Definition Documentation
#define DATE_MAX_LEN 20
Maximum length for ISO-8601 date string buffers. Used by printtime() for formatting timestamps.
Typedef Documentation
typedef void* joint_cur_t
Opaque iterator handle for interval tree traversal. Created by joint_iter() and used with joint_next() to traverse intervals that intersect a time range. Must not be freed directly by the user.
See also
joint_iter
joint_next
Function Documentation
void joint_close (unsigned jd)
Close an interval tree database and free resources. Closes all associated corm databases for the given handle, ensuring data is persisted to disk. After calling this function, the handle should not be used again.
Parameters
jd Database handle from joint_init().
Note
This function should be called before re-opening the same database file to ensure data persistence.
See also
joint_init
int joint_erase (unsigned jd, unsigned id)
Erase every interval an entity owns (Phase 2A inverse of start/stop). Walks the entity's id-index entries (corm_get_multi on the id secondary), recovers each struct ti, and deletes it from the primary ti map โ the associated max/id indexes are maintained automatically by corm_assoc. Cost O(intervals-of-id), never O(store); zero new stored state.
Parameters
jd Database handle from joint_init().
id Entity identifier (cannot be UINT32_MAX, reserved sentinel).
Returns
0 on success (even when the entity owns nothing โ idempotent). -1 on validation error (errno = EINVAL: entity ID is UINT32_MAX).
Note
Neighboring entities' intervals are unaffected. After erase the entity can joint_start() a fresh timeline.
unsigned joint_init (char * fname)
Initialize an interval tree database. Creates or opens a file-backed interval tree with three internal corm databases:
- "ti": Primary map (interval -> interval)
- "max": Secondary index sorted by interval max time
- "id": Secondary index sorted by entity ID
Parameters
fname Path to database file, or NULL for in-memory only. If provided, data persists across program runs.
Returns
Database handle for use with other joint_* functions. Handle is an integer ID that remains valid until process exit (no explicit close needed).
Note
File persistence uses corm's automatic save-on-exit. Multiple databases can share one file via different names.
See also
joint_start
joint_stop
joint_iter
Example:
// Create persistent interval tree
uint32_t jd = joint_init("events.corm");
// Add intervals
joint_start(jd, timestamp1, user_id);
joint_stop(jd, timestamp2, user_id);
// Query overlapping intervals
joint_cur_t cur = joint_iter(jd, start_time, end_time);
time_t min, max;
uint32_t count, who;
while (joint_next(&min, &max, &count, &who, &cur)) {
printf("Interval: %ld-%ld, ID: %u\n", min, max, who);
}
joint_cur_t joint_iter (unsigned jd, time_t start, time_t end)
Begin iterating over intervals that intersect a time range. Creates an iterator for all intervals that overlap with the specified time range [start, end). The iterator returns split intervals that show which entities were present during each sub-interval.
Parameters
jd Database handle from joint_init().
start Start of the query time range (inclusive).
end End of the query time range (exclusive).
Returns
Opaque iterator handle for use with joint_next(). The iterator remains valid until all results are consumed or the process exits. No explicit cleanup needed.
Note
The iterator computes split intervals that show periods where the set of present entities changes. Use joint_next() to retrieve each split interval and its associated entities.
See also
joint_next
joint_init
joint_start
joint_stop
int joint_next (time_t * min, time_t * max, unsigned * count, unsigned * who, joint_cur_t * c)
Get the next interval and entity from an iterator. Retrieves the next split interval from the iterator, along with one entity ID that was present during that interval. Call repeatedly to get all entities for each interval.
Parameters
min Start time of this interval segment.
max End time of this interval segment.
count Total number of entities present in this segment.
who Entity ID for this iteration.
c Iterator handle from joint_iter().
Returns
1 if an entity was retrieved (continue iteration). 0 if no more entities/intervals (iteration complete).
Note
To get all entities for each interval, keep calling joint_next() until count entities are retrieved for that time segment. The iterator automatically advances to the next interval when all entities are consumed.
See also
joint_iter
int joint_start (unsigned jd, time_t ts, unsigned id)
Start a new interval for an entity. Records that an entity (identified by id) began an activity at the given timestamp. The interval remains open (max = infinity) until closed with joint_stop().
Parameters
jd Database handle from joint_init().
ts Timestamp when the interval begins. Must be in range [LONG_MIN/2, LONG_MAX/2] to avoid conflicts with internal sentinel values.
id Entity identifier (e.g., user ID, session ID). Cannot be UINT32_MAX (reserved as internal sentinel).
Returns
0 on success (new interval started). 1 if entity already has an open interval at this time (duplicate start attempt). -1 on validation error (check errno):
- ERANGE: timestamp outside valid range
- EINVAL: entity ID is UINT32_MAX
Note
If an entity already has an open interval, this function returns 1 and does not create a duplicate.
See also
joint_stop
joint_init
int joint_stop (unsigned jd, time_t ts, unsigned id)
Stop an interval for an entity. Closes the most recent open interval for the given entity, setting its end time to the provided timestamp. If no open interval exists, creates a new interval ending at this time with start = -infinity.
Parameters
jd Database handle from joint_init().
ts Timestamp when the interval ends. Must be in range [LONG_MIN/2, LONG_MAX/2] to avoid conflicts with internal sentinel values.
id Entity identifier. Cannot be UINT32_MAX (reserved as internal sentinel).
Returns
0 on success (existing interval closed). 1 if no open interval existed (created backward interval). -1 on validation error (check errno):
- ERANGE: timestamp outside valid range
- EINVAL: entity ID is UINT32_MAX
Note
This function handles the case where stop is called before start by creating an interval from -infinity.
See also
joint_start
joint_init
void printtime (char buf[DATE_MAX_LEN], time_t ts)
Format a Unix timestamp as an ISO-8601 date string. Converts a time_t value to a human-readable date string. Special values -infinity and +infinity are formatted as "-inf" and "inf" respectively.
Output format depends on the time component:
- If time is 00:00:00: "YYYY-MM-DD" (date only)
- Otherwise: "YYYY-MM-DDTHH:MM:SS" (full ISO-8601)
Parameters
buf Buffer to store formatted string (must be at least DATE_MAX_LEN bytes).
ts Timestamp to format.
Note
The output uses the system's local timezone. Special handling for infinity values (min/max time_t).
See also
sscantime
DATE_MAX_LEN
int rec_axis_fill_interval (unsigned jd, time_t a, time_t b, rec_set_t * out)
Recall-kernel time-axis filler (see ttypt/rec.h). Every entity present at any point in [a, b) becomes one ref in out (rec_ref_t == the joint entity id, widened from the internal 32-bit unsigned -- see the ID-uniformity contract note at the top of this file). Refs are appended (additive) and out is sealed (duplicates across split segments are deduped by the seal). Compatible with rec_axis_t.fill via the "joint" axis registered by this library's constructor (ctx = the joint_init() handle, cast through a void*).
Parameters
jd Database handle from joint_init().
a Start of the range (inclusive).
b End of the range (exclusive).
out Recall-kernel set to fill.
Returns
0 on success, -1 if out is NULL.
time_t sscantime (char * buf)
Parse an ISO-8601 date string or Unix timestamp. Converts a date/time string to a Unix timestamp (time_t). Supports multiple formats:
- ISO-8601 date+time: "2024-12-25T14:30:00"
- ISO-8601 date only: "2024-12-25"
- Unix timestamp: "1735139400"
Parameters
buf String containing date/time to parse.
Returns
Parsed timestamp as time_t. On error, the behavior depends on the input format.
Note
For date-only format, time is assumed to be 00:00:00. The function uses the system's local timezone.
See also
printtime
Author
Generated automatically by Doxygen for joint from the source code.