drasi-bootstrap-scriptfile 0.1.10

ScriptFile bootstrap plugin for Drasi
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
# ScriptFile Bootstrap Provider

A bootstrap provider for Drasi that loads initial graph data from JSONL (JSON Lines) script files.

## Overview

The ScriptFile bootstrap provider enables loading graph data (nodes and relationships) from one or more JSONL files during query initialization. This is useful for:

- **Testing and Development**: Pre-populate queries with test data without external dependencies
- **Static Data Loading**: Bootstrap queries with reference data, lookup tables, or configuration
- **Data Migration**: Import existing graph data from file-based exports
- **Reproducible Scenarios**: Create repeatable test scenarios with known data sets
- **Multi-file Datasets**: Load large datasets split across multiple JSONL files

The provider reads JSONL files sequentially, filters data based on requested labels from queries, and sends matching nodes and relationships as bootstrap events.

## Key Features

- **Label-based Filtering**: Automatically filters nodes and relationships based on query label requirements
- **Multi-file Support**: Read from multiple JSONL files in sequence
- **Comment Support**: Allows inline documentation within script files
- **Checkpoint Support**: Label records for marking positions in the script timeline
- **Automatic Sequencing**: Maintains order across multiple files
- **Robust Error Handling**: Validates file format and record structure
- **Auto-finish Generation**: Automatically adds finish record if not present in script

## Configuration

### Builder Pattern (Recommended)

The builder pattern provides the most flexible and readable configuration approach:

```rust
use drasi_bootstrap_scriptfile::ScriptFileBootstrapProvider;

// Single file
let provider = ScriptFileBootstrapProvider::builder()
    .with_file("/path/to/data.jsonl")
    .build();

// Multiple files (processed in order)
let provider = ScriptFileBootstrapProvider::builder()
    .with_file("/data/nodes.jsonl")
    .with_file("/data/relationships.jsonl")
    .with_file("/data/reference.jsonl")
    .build();

// Bulk file paths
let paths = vec![
    "/data/nodes.jsonl".to_string(),
    "/data/relations.jsonl".to_string(),
];
let provider = ScriptFileBootstrapProvider::builder()
    .with_file_paths(paths)
    .build();
```

### Configuration Struct

Use the configuration struct when loading from serialized configuration:

```rust
use drasi_bootstrap_scriptfile::ScriptFileBootstrapProvider;
use drasi_lib::bootstrap::ScriptFileBootstrapConfig;

let config = ScriptFileBootstrapConfig {
    file_paths: vec![
        "/path/to/data.jsonl".to_string(),
        "/path/to/more_data.jsonl".to_string(),
    ],
};

let provider = ScriptFileBootstrapProvider::new(config);
```

### Direct Paths

For simple cases with known file paths:

```rust
use drasi_bootstrap_scriptfile::ScriptFileBootstrapProvider;

let provider = ScriptFileBootstrapProvider::with_paths(vec![
    "/data/bootstrap.jsonl".to_string(),
]);
```

### Default Provider

Create an empty provider (no files):

```rust
use drasi_bootstrap_scriptfile::ScriptFileBootstrapProvider;

let provider = ScriptFileBootstrapProvider::default();
```

## Configuration Options

| Name | Description | Data Type | Valid Values | Default |
|------|-------------|-----------|--------------|---------|
| `file_paths` | List of JSONL file paths to read in order | `Vec<String>` | Valid file system paths ending in `.jsonl` | `[]` (empty) |

**Notes:**
- Files are processed in the order they appear in the `file_paths` list
- All files must have the `.jsonl` extension
- First file must begin with a Header record
- Relative or absolute paths are supported

## Input Schema

Bootstrap script files use JSONL (JSON Lines) format - one JSON object per line. Each record has a `kind` field that identifies the record type.

### Header Record

**Required** as the first record in the first file. Provides metadata about the script.

```json
{
  "kind": "Header",
  "start_time": "2024-01-01T00:00:00+00:00",
  "description": "Initial bootstrap data"
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | String | Yes | Must be `"Header"` |
| `start_time` | ISO 8601 DateTime | Yes | Script reference timestamp |
| `description` | String | No | Human-readable script description |

### Node Record

Represents a graph node/entity with properties and labels.

```json
{
  "kind": "Node",
  "id": "person-1",
  "labels": ["Person", "Employee"],
  "properties": {
    "name": "Alice Smith",
    "age": 30,
    "email": "alice@example.com"
  }
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | String | Yes | Must be `"Node"` |
| `id` | String | Yes | Unique identifier for the node |
| `labels` | Array of Strings | Yes | Node type labels (can be empty array) |
| `properties` | JSON Object | No | Node properties (defaults to null/empty) |

**Property Types:** Properties support all JSON types: strings, numbers, booleans, objects, arrays, and null.

### Relation Record

Represents a graph relationship/edge between two nodes.

```json
{
  "kind": "Relation",
  "id": "knows-1",
  "labels": ["KNOWS"],
  "start_id": "person-1",
  "start_label": "Person",
  "end_id": "person-2",
  "end_label": "Person",
  "properties": {
    "since": 2020,
    "strength": 0.85
  }
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | String | Yes | Must be `"Relation"` |
| `id` | String | Yes | Unique identifier for the relationship |
| `labels` | Array of Strings | Yes | Relationship type labels |
| `start_id` | String | Yes | ID of the source/start node |
| `start_label` | String | No | Optional label of start node |
| `end_id` | String | Yes | ID of the target/end node |
| `end_label` | String | No | Optional label of end node |
| `properties` | JSON Object | No | Relationship properties (defaults to null/empty) |

**Direction:** Relationships are directed from `start_id` to `end_id`. In Cypher: `(start)-[relation]->(end)`

### Comment Record

Documentation and notes within the script. Automatically filtered out during processing.

```json
{
  "kind": "Comment",
  "comment": "This section contains employee data"
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | String | Yes | Must be `"Comment"` |
| `comment` | String | Yes | Comment text (arbitrary length) |

### Label Record

Checkpoint marker for script navigation (currently informational only).

```json
{
  "kind": "Label",
  "offset_ns": 1000000000,
  "label": "checkpoint_1",
  "description": "After employee nodes loaded"
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | String | Yes | Must be `"Label"` |
| `offset_ns` | Number (u64) | No | Nanoseconds offset from script start time |
| `label` | String | Yes | Checkpoint label/name |
| `description` | String | No | Checkpoint description |

### Finish Record

Marks the end of the script. If not present, one is automatically generated.

```json
{
  "kind": "Finish",
  "description": "Bootstrap complete"
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `kind` | String | Yes | Must be `"Finish"` |
| `description` | String | No | Completion message |

## Usage Examples

### Example 1: Basic Bootstrap Script

Create a simple bootstrap file with people and relationships:

**people.jsonl:**
```jsonl
{"kind":"Header","start_time":"2024-01-01T00:00:00Z","description":"People and relationships"}
{"kind":"Node","id":"alice","labels":["Person"],"properties":{"name":"Alice","age":30}}
{"kind":"Node","id":"bob","labels":["Person"],"properties":{"name":"Bob","age":35}}
{"kind":"Relation","id":"r1","labels":["KNOWS"],"start_id":"alice","end_id":"bob","properties":{"since":2020}}
{"kind":"Finish","description":"Done"}
```

**Rust code:**
```rust
use drasi_bootstrap_scriptfile::ScriptFileBootstrapProvider;

let provider = ScriptFileBootstrapProvider::builder()
    .with_file("/data/people.jsonl")
    .build();
```

### Example 2: Multi-file Dataset

Split large datasets across multiple files for better organization:

**nodes.jsonl:**
```jsonl
{"kind":"Header","start_time":"2024-01-01T00:00:00Z","description":"Employee database"}
{"kind":"Comment","comment":"Employee nodes"}
{"kind":"Node","id":"e1","labels":["Employee"],"properties":{"name":"Alice","dept":"Engineering"}}
{"kind":"Node","id":"e2","labels":["Employee"],"properties":{"name":"Bob","dept":"Sales"}}
```

**relationships.jsonl:**
```jsonl
{"kind":"Comment","comment":"Reporting relationships"}
{"kind":"Relation","id":"r1","labels":["REPORTS_TO"],"start_id":"e2","end_id":"e1","properties":{}}
{"kind":"Finish","description":"All data loaded"}
```

**Rust code:**
```rust
let provider = ScriptFileBootstrapProvider::builder()
    .with_file("/data/nodes.jsonl")
    .with_file("/data/relationships.jsonl")
    .build();
```

### Example 3: Label-based Filtering

Only nodes/relationships matching query labels are loaded:

**mixed_data.jsonl:**
```jsonl
{"kind":"Header","start_time":"2024-01-01T00:00:00Z","description":"Mixed entity types"}
{"kind":"Node","id":"p1","labels":["Person"],"properties":{"name":"Alice"}}
{"kind":"Node","id":"c1","labels":["Company"],"properties":{"name":"Acme Corp"}}
{"kind":"Node","id":"p2","labels":["Person","Employee"],"properties":{"name":"Bob"}}
{"kind":"Relation","id":"r1","labels":["WORKS_AT"],"start_id":"p2","end_id":"c1","properties":{}}
{"kind":"Finish"}
```

If a query requests only `Person` nodes, both `p1` and `p2` are loaded (even though `p2` has multiple labels). The `Company` node is filtered out.

**Query example:**
```cypher
MATCH (p:Person)
WHERE p.name = 'Alice'
RETURN p
```

Only nodes with the `Person` label would be loaded from the bootstrap file.

### Example 4: Complex Properties

Properties can contain nested objects, arrays, and various data types:

```jsonl
{"kind":"Header","start_time":"2024-01-01T00:00:00Z","description":"Complex data"}
{"kind":"Node","id":"user1","labels":["User"],"properties":{
  "name":"Alice",
  "age":30,
  "active":true,
  "score":95.5,
  "roles":["admin","developer"],
  "metadata":{"created":"2023-01-01","source":"import"},
  "nullable_field":null
}}
{"kind":"Finish"}
```

All standard JSON types are supported: strings, numbers, booleans, arrays, objects, and null.

### Example 5: Programmatic Usage

Using the provider in a Drasi application:

```rust
use drasi_lib::DrasiLib;
use drasi_lib::Query;
use drasi_bootstrap_scriptfile::ScriptFileBootstrapProvider;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Create bootstrap provider
    let bootstrap = ScriptFileBootstrapProvider::builder()
        .with_file("/data/initial_data.jsonl")
        .build();

    // Create Drasi instance with bootstrap provider
    let drasi = DrasiLib::builder()
        .with_bootstrap_provider("script-bootstrap", bootstrap)
        .build()
        .await?;

    // Add a query that uses the bootstrap provider
    let query = Query::cypher("my-query")
        .query("MATCH (p:Person) WHERE p.age > 25 RETURN p")
        .with_bootstrap("script-bootstrap")
        .build();

    drasi.add_query(query).await?;
    drasi.start_query("my-query").await?;

    // Bootstrap data is now loaded and query is running

    Ok(())
}
```

## Implementation Details

### Label Filtering Logic

The provider automatically filters records based on the labels requested by queries:

1. **Empty label request**: If a query doesn't specify labels, all records are included
2. **Label matching**: A record is included if ANY of its labels match ANY requested label
3. **Multiple labels**: Records can have multiple labels; any match is sufficient

Example:
- Query requests: `["Person"]`
- Node has labels: `["Person", "Employee"]`
- Result: Node is included (matched on "Person")

### Multi-file Processing

When multiple files are provided:

1. Files are read sequentially in the order specified
2. The Header record must appear in the first file only
3. Subsequent files continue from where the previous file ended
4. Sequence numbers are maintained across all files
5. First Finish record encountered terminates processing

### Sequencing

Each record is assigned a sequence number:

- Header record: sequence 0
- Subsequent records: incremented for each non-comment record
- Comment records: increment sequence but are not returned
- Sequence continues across file boundaries

### Automatic Finish Record

If no Finish record exists in the script files:

- One is automatically generated after the last record
- Description: "Auto generated at end of script."
- Once reached, iteration stops

### Error Handling

The provider validates:

- File extension must be `.jsonl`
- First record must be a Header
- Each line must be valid JSON
- Record structure must match expected schema
- Properties must be JSON objects or null

Errors are returned as `anyhow::Result` with descriptive messages.

## Thread Safety

The `ScriptFileBootstrapProvider` is safe to use across threads and can be shared using `Arc`. The `bootstrap` method is async and can handle concurrent bootstrap requests from multiple queries.

## Performance Considerations

- **File I/O**: Files are read synchronously using buffered readers
- **Memory**: Records are processed one at a time (streaming), not loaded entirely into memory
- **Filtering**: Label filtering is done in-memory during iteration
- **Large Files**: Suitable for files with millions of records due to streaming approach

## Testing

The module includes comprehensive tests for:

- Builder pattern variations
- Multi-file reading
- Label filtering logic
- Comment filtering
- Automatic finish record generation
- Error cases (invalid files, missing headers, malformed JSON)
- Sequence numbering across files

Run tests:
```bash
cargo test -p drasi-bootstrap-scriptfile
```

## Dependencies

- `drasi-lib`: Core Drasi library for bootstrap provider trait
- `drasi-core`: Core models (Element, SourceChange, etc.)
- `anyhow`: Error handling
- `async-trait`: Async trait support
- `serde`/`serde_json`: JSON serialization/deserialization
- `chrono`: DateTime handling
- `log`: Logging support

## Plugin Packaging

This bootstrap provider is compiled as a dynamic plugin (cdylib) that can be loaded by drasi-server at runtime.

**Key files:**
- `Cargo.toml` — includes `crate-type = ["lib", "cdylib"]`
- `src/descriptor.rs` — implements `BootstrapPluginDescriptor` with kind `"scriptfile"`, configuration DTO, and OpenAPI schema generation
- `src/lib.rs` — invokes `drasi_plugin_sdk::export_plugin!` to export the plugin entry point

**Building:**
```bash
cargo build -p drasi-bootstrap-scriptfile
```

The compiled `.so` (Linux) / `.dylib` (macOS) / `.dll` (Windows) is placed in `target/debug/` and can be copied to the server's `plugins/` directory.

For more details on the plugin descriptor pattern and configuration DTOs, see the [Bootstrap Provider Developer Guide](../README.md#packaging-as-a-dynamic-plugin).

## License

Licensed under the Apache License, Version 2.0. See LICENSE file for details.