.TH "ttypt/joint.h" 3 "joint" \" -*- nroff -*- .ad l .nh .SH NAME ttypt/joint.h \- Public API for the Interval Tree Library (libjoint)\&. .SH SYNOPSIS .br .PP \fR#include \fP .br \fR#include \fP .br \fR#include \fP .br .SS "Macros" .in +1c .ti -1c .RI "#define \fBDATE_MAX_LEN\fP 20" .br .RI "Maximum length for ISO-8601 date string buffers\&. " .in -1c .SS "Typedefs" .in +1c .ti -1c .RI "typedef void * \fBjoint_cur_t\fP" .br .RI "Opaque iterator handle for interval tree traversal\&. " .in -1c .SS "Functions" .in +1c .ti -1c .RI "unsigned \fBjoint_init\fP (char *fname)" .br .RI "Initialize an interval tree database\&. " .ti -1c .RI "void \fBjoint_close\fP (unsigned jd)" .br .RI "Close an interval tree database and free resources\&. " .ti -1c .RI "int \fBjoint_start\fP (unsigned jd, time_t ts, unsigned id)" .br .RI "Start a new interval for an entity\&. " .ti -1c .RI "int \fBjoint_stop\fP (unsigned jd, time_t ts, unsigned id)" .br .RI "Stop an interval for an entity\&. " .ti -1c .RI "\fBjoint_cur_t\fP \fBjoint_iter\fP (unsigned jd, time_t start, time_t end)" .br .RI "Begin iterating over intervals that intersect a time range\&. " .ti -1c .RI "int \fBjoint_next\fP (time_t *min, time_t *max, unsigned *count, unsigned *who, \fBjoint_cur_t\fP *c)" .br .RI "Get the next interval and entity from an iterator\&. " .ti -1c .RI "time_t \fBsscantime\fP (char *buf)" .br .RI "Parse an ISO-8601 date string or Unix timestamp\&. " .ti -1c .RI "void \fBprinttime\fP (char buf[\fBDATE_MAX_LEN\fP], time_t ts)" .br .RI "Format a Unix timestamp as an ISO-8601 date string\&. " .ti -1c .RI "int \fBrec_axis_fill_interval\fP (unsigned jd, time_t a, time_t b, rec_set_t *out)" .br .RI "Recall-kernel time-axis filler (see ttypt/rec\&.h)\&. " .ti -1c .RI "int \fBjoint_erase\fP (unsigned jd, unsigned id)" .br .RI "Erase every interval an entity owns (Phase 2A inverse of start/stop)\&. " .in -1c .SH "Detailed Description" .PP 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\&. .PP Key features: .IP "\(bu" 2 Store and query time intervals with associated IDs .IP "\(bu" 2 File-based persistence via corm .IP "\(bu" 2 Efficient intersection queries .IP "\(bu" 2 Multiple secondary indexes (by max time, by ID) .IP "\(bu" 2 Sorted iteration support .PP .PP ID-uniformity contract (see RECALL-KERNEL\&.md): entity ids are stored and compared internally as \fRunsigned\fP (32-bit) -- this is intentional, not a gap\&. The uniformity boundary other axis libraries (libsepal, liblemma) share is \fRrec_ref_t\fP (uint32_t, from rec\&.h), and it is enforced at the \fIfill\fP boundary only: \fBrec_axis_fill_interval()\fP widens every \fRwho\fP to \fRrec_ref_t\fP 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 \fBjoint_start()\fP/joint_stop()/joint_next()\&. .PP \fBSee also\fP .RS 4 corm\&.h for underlying storage implementation .RE .PP .SH "Macro Definition Documentation" .PP .SS "#define DATE_MAX_LEN 20" .PP Maximum length for ISO-8601 date string buffers\&. Used by \fBprinttime()\fP for formatting timestamps\&. .SH "Typedef Documentation" .PP .SS "typedef void* \fBjoint_cur_t\fP" .PP Opaque iterator handle for interval tree traversal\&. Created by \fBjoint_iter()\fP and used with \fBjoint_next()\fP to traverse intervals that intersect a time range\&. Must not be freed directly by the user\&. .PP \fBSee also\fP .RS 4 \fBjoint_iter\fP .PP \fBjoint_next\fP .RE .PP .SH "Function Documentation" .PP .SS "void joint_close (unsigned jd)" .PP 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\&. .PP \fBParameters\fP .RS 4 \fIjd\fP Database handle from \fBjoint_init()\fP\&. .RE .PP \fBNote\fP .RS 4 This function should be called before re-opening the same database file to ensure data persistence\&. .RE .PP \fBSee also\fP .RS 4 \fBjoint_init\fP .RE .PP .SS "int joint_erase (unsigned jd, unsigned id)" .PP Erase every interval an entity owns (Phase 2A inverse of start/stop)\&. Walks the entity's id-index entries (corm_get_multi on the \fRid\fP secondary), recovers each \fRstruct ti\fP, and deletes it from the primary \fRti\fP map — the associated \fRmax\fP/\fRid\fP indexes are maintained automatically by corm_assoc\&. Cost O(intervals-of-id), never O(store); zero new stored state\&. .PP \fBParameters\fP .RS 4 \fIjd\fP Database handle from \fBjoint_init()\fP\&. .br \fIid\fP Entity identifier (cannot be UINT32_MAX, reserved sentinel)\&. .RE .PP \fBReturns\fP .RS 4 0 on success (even when the entity owns nothing — idempotent)\&. -1 on validation error (errno = EINVAL: entity ID is UINT32_MAX)\&. .RE .PP \fBNote\fP .RS 4 Neighboring entities' intervals are unaffected\&. After erase the entity can \fBjoint_start()\fP a fresh timeline\&. .RE .PP .SS "unsigned joint_init (char * fname)" .PP Initialize an interval tree database\&. Creates or opens a file-backed interval tree with three internal corm databases: .IP "\(bu" 2 "ti": Primary map (interval -> interval) .IP "\(bu" 2 "max": Secondary index sorted by interval max time .IP "\(bu" 2 "id": Secondary index sorted by entity ID .PP .PP \fBParameters\fP .RS 4 \fIfname\fP Path to database file, or NULL for in-memory only\&. If provided, data persists across program runs\&. .RE .PP \fBReturns\fP .RS 4 Database handle for use with other joint_* functions\&. Handle is an integer ID that remains valid until process exit (no explicit close needed)\&. .RE .PP \fBNote\fP .RS 4 File persistence uses corm's automatic save-on-exit\&. Multiple databases can share one file via different names\&. .RE .PP \fBSee also\fP .RS 4 \fBjoint_start\fP .PP \fBjoint_stop\fP .PP \fBjoint_iter\fP .RE .PP Example: .PP .nf // 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); } .fi .PP .SS "\fBjoint_cur_t\fP joint_iter (unsigned jd, time_t start, time_t end)" .PP 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\&. .PP \fBParameters\fP .RS 4 \fIjd\fP Database handle from \fBjoint_init()\fP\&. .br \fIstart\fP Start of the query time range (inclusive)\&. .br \fIend\fP End of the query time range (exclusive)\&. .RE .PP \fBReturns\fP .RS 4 Opaque iterator handle for use with \fBjoint_next()\fP\&. The iterator remains valid until all results are consumed or the process exits\&. No explicit cleanup needed\&. .RE .PP \fBNote\fP .RS 4 The iterator computes split intervals that show periods where the set of present entities changes\&. Use \fBjoint_next()\fP to retrieve each split interval and its associated entities\&. .RE .PP \fBSee also\fP .RS 4 \fBjoint_next\fP .PP \fBjoint_init\fP .PP \fBjoint_start\fP .PP \fBjoint_stop\fP .RE .PP .SS "int joint_next (time_t * min, time_t * max, unsigned * count, unsigned * who, \fBjoint_cur_t\fP * c)" .PP 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\&. .PP \fBParameters\fP .RS 4 \fImin\fP Start time of this interval segment\&. .br \fImax\fP End time of this interval segment\&. .br \fIcount\fP Total number of entities present in this segment\&. .br \fIwho\fP Entity ID for this iteration\&. .br \fIc\fP Iterator handle from \fBjoint_iter()\fP\&. .RE .PP \fBReturns\fP .RS 4 1 if an entity was retrieved (continue iteration)\&. 0 if no more entities/intervals (iteration complete)\&. .RE .PP \fBNote\fP .RS 4 To get all entities for each interval, keep calling \fBjoint_next()\fP until count entities are retrieved for that time segment\&. The iterator automatically advances to the next interval when all entities are consumed\&. .RE .PP \fBSee also\fP .RS 4 \fBjoint_iter\fP .RE .PP .SS "int joint_start (unsigned jd, time_t ts, unsigned id)" .PP 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 \fBjoint_stop()\fP\&. .PP \fBParameters\fP .RS 4 \fIjd\fP Database handle from \fBjoint_init()\fP\&. .br \fIts\fP Timestamp when the interval begins\&. Must be in range [LONG_MIN/2, LONG_MAX/2] to avoid conflicts with internal sentinel values\&. .br \fIid\fP Entity identifier (e\&.g\&., user ID, session ID)\&. Cannot be UINT32_MAX (reserved as internal sentinel)\&. .RE .PP \fBReturns\fP .RS 4 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): .IP "\(bu" 2 ERANGE: timestamp outside valid range .IP "\(bu" 2 EINVAL: entity ID is UINT32_MAX .PP .RE .PP \fBNote\fP .RS 4 If an entity already has an open interval, this function returns 1 and does not create a duplicate\&. .RE .PP \fBSee also\fP .RS 4 \fBjoint_stop\fP .PP \fBjoint_init\fP .RE .PP .SS "int joint_stop (unsigned jd, time_t ts, unsigned id)" .PP 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\&. .PP \fBParameters\fP .RS 4 \fIjd\fP Database handle from \fBjoint_init()\fP\&. .br \fIts\fP Timestamp when the interval ends\&. Must be in range [LONG_MIN/2, LONG_MAX/2] to avoid conflicts with internal sentinel values\&. .br \fIid\fP Entity identifier\&. Cannot be UINT32_MAX (reserved as internal sentinel)\&. .RE .PP \fBReturns\fP .RS 4 0 on success (existing interval closed)\&. 1 if no open interval existed (created backward interval)\&. -1 on validation error (check errno): .IP "\(bu" 2 ERANGE: timestamp outside valid range .IP "\(bu" 2 EINVAL: entity ID is UINT32_MAX .PP .RE .PP \fBNote\fP .RS 4 This function handles the case where stop is called before start by creating an interval from -infinity\&. .RE .PP \fBSee also\fP .RS 4 \fBjoint_start\fP .PP \fBjoint_init\fP .RE .PP .SS "void printtime (char buf[DATE_MAX_LEN], time_t ts)" .PP 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\&. .PP Output format depends on the time component: .IP "\(bu" 2 If time is 00:00:00: "YYYY-MM-DD" (date only) .IP "\(bu" 2 Otherwise: "YYYY-MM-DDTHH:MM:SS" (full ISO-8601) .PP .PP \fBParameters\fP .RS 4 \fIbuf\fP Buffer to store formatted string (must be at least DATE_MAX_LEN bytes)\&. .br \fIts\fP Timestamp to format\&. .RE .PP \fBNote\fP .RS 4 The output uses the system's local timezone\&. Special handling for infinity values (min/max time_t)\&. .RE .PP \fBSee also\fP .RS 4 \fBsscantime\fP .PP \fBDATE_MAX_LEN\fP .RE .PP .SS "int rec_axis_fill_interval (unsigned jd, time_t a, time_t b, rec_set_t * out)" .PP Recall-kernel time-axis filler (see ttypt/rec\&.h)\&. Every entity present at any point in [a, b) becomes one ref in \fRout\fP (rec_ref_t == the joint entity id, widened from the internal 32-bit \fRunsigned\fP -- see the ID-uniformity contract note at the top of this file)\&. Refs are appended (additive) and \fRout\fP 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 \fBjoint_init()\fP handle, cast through a void*)\&. .PP \fBParameters\fP .RS 4 \fIjd\fP Database handle from \fBjoint_init()\fP\&. .br \fIa\fP Start of the range (inclusive)\&. .br \fIb\fP End of the range (exclusive)\&. .br \fIout\fP Recall-kernel set to fill\&. .RE .PP \fBReturns\fP .RS 4 0 on success, -1 if out is NULL\&. .RE .PP .SS "time_t sscantime (char * buf)" .PP Parse an ISO-8601 date string or Unix timestamp\&. Converts a date/time string to a Unix timestamp (time_t)\&. Supports multiple formats: .IP "\(bu" 2 ISO-8601 date+time: "2024-12-25T14:30:00" .IP "\(bu" 2 ISO-8601 date only: "2024-12-25" .IP "\(bu" 2 Unix timestamp: "1735139400" .PP .PP \fBParameters\fP .RS 4 \fIbuf\fP String containing date/time to parse\&. .RE .PP \fBReturns\fP .RS 4 Parsed timestamp as time_t\&. On error, the behavior depends on the input format\&. .RE .PP \fBNote\fP .RS 4 For date-only format, time is assumed to be 00:00:00\&. The function uses the system's local timezone\&. .RE .PP \fBSee also\fP .RS 4 \fBprinttime\fP .RE .PP .SH "Author" .PP Generated automatically by Doxygen for joint from the source code\&.