oximedia-clips 0.1.8

Professional clip management and logging system for OxiMedia
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
//! `SQLite` database storage for clips.

use crate::clip::{Clip, ClipId};
use crate::error::{ClipError, ClipResult};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePool};
use sqlx::Row;
use std::str::FromStr;

/// `SQLite` database for clip storage.
#[derive(Debug, Clone)]
pub struct ClipDatabase {
    pool: SqlitePool,
}

impl ClipDatabase {
    /// Creates a new clip database.
    ///
    /// # Errors
    ///
    /// Returns an error if the database cannot be opened or migrated.
    pub async fn new(database_url: impl AsRef<str>) -> ClipResult<Self> {
        let options =
            SqliteConnectOptions::from_str(database_url.as_ref())?.create_if_missing(true);

        let pool = SqlitePool::connect_with(options).await?;

        let db = Self { pool };
        db.migrate().await?;

        Ok(db)
    }

    async fn migrate(&self) -> ClipResult<()> {
        sqlx::query(
            r"
            CREATE TABLE IF NOT EXISTS clips (
                id TEXT PRIMARY KEY,
                file_path TEXT NOT NULL,
                name TEXT NOT NULL,
                description TEXT,
                duration INTEGER,
                frame_rate_num INTEGER,
                frame_rate_den INTEGER,
                in_point INTEGER,
                out_point INTEGER,
                rating INTEGER NOT NULL DEFAULT 0,
                is_favorite INTEGER NOT NULL DEFAULT 0,
                is_rejected INTEGER NOT NULL DEFAULT 0,
                keywords TEXT,
                created_at TEXT NOT NULL,
                modified_at TEXT NOT NULL,
                custom_metadata TEXT
            )
            ",
        )
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    /// Saves a clip to the database.
    ///
    /// # Errors
    ///
    /// Returns an error if the clip cannot be saved.
    #[allow(clippy::too_many_arguments)]
    pub async fn save_clip(&self, clip: &Clip) -> ClipResult<()> {
        let keywords_json = serde_json::to_string(&clip.keywords)
            .map_err(|e| ClipError::Serialization(e.to_string()))?;

        let (frame_rate_num, frame_rate_den) = clip
            .frame_rate
            .map_or((None, None), |fr| (Some(fr.num), Some(fr.den)));

        sqlx::query(
            r"
            INSERT INTO clips (
                id, file_path, name, description, duration,
                frame_rate_num, frame_rate_den, in_point, out_point,
                rating, is_favorite, is_rejected, keywords,
                created_at, modified_at, custom_metadata
            ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
            ON CONFLICT(id) DO UPDATE SET
                file_path = excluded.file_path,
                name = excluded.name,
                description = excluded.description,
                duration = excluded.duration,
                frame_rate_num = excluded.frame_rate_num,
                frame_rate_den = excluded.frame_rate_den,
                in_point = excluded.in_point,
                out_point = excluded.out_point,
                rating = excluded.rating,
                is_favorite = excluded.is_favorite,
                is_rejected = excluded.is_rejected,
                keywords = excluded.keywords,
                modified_at = excluded.modified_at,
                custom_metadata = excluded.custom_metadata
            ",
        )
        .bind(clip.id.to_string())
        .bind(clip.file_path.to_string_lossy().to_string())
        .bind(&clip.name)
        .bind(&clip.description)
        .bind(clip.duration)
        .bind(frame_rate_num)
        .bind(frame_rate_den)
        .bind(clip.in_point)
        .bind(clip.out_point)
        .bind(i64::from(clip.rating.to_value()))
        .bind(i64::from(clip.is_favorite))
        .bind(i64::from(clip.is_rejected))
        .bind(keywords_json)
        .bind(clip.created_at.to_rfc3339())
        .bind(clip.modified_at.to_rfc3339())
        .bind(&clip.custom_metadata)
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    /// Gets a clip by ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the clip is not found or cannot be loaded.
    pub async fn get_clip(&self, clip_id: &ClipId) -> ClipResult<Clip> {
        let row = sqlx::query("SELECT * FROM clips WHERE id = ?")
            .bind(clip_id.to_string())
            .fetch_optional(&self.pool)
            .await?
            .ok_or_else(|| ClipError::ClipNotFound(clip_id.to_string()))?;

        Self::row_to_clip(&row)
    }

    /// Gets all clips.
    ///
    /// # Errors
    ///
    /// Returns an error if clips cannot be loaded.
    pub async fn get_all_clips(&self) -> ClipResult<Vec<Clip>> {
        let rows = sqlx::query("SELECT * FROM clips ORDER BY created_at DESC")
            .fetch_all(&self.pool)
            .await?;

        rows.iter().map(Self::row_to_clip).collect()
    }

    /// Deletes a clip by ID.
    ///
    /// # Errors
    ///
    /// Returns an error if the clip cannot be deleted.
    pub async fn delete_clip(&self, clip_id: &ClipId) -> ClipResult<()> {
        sqlx::query("DELETE FROM clips WHERE id = ?")
            .bind(clip_id.to_string())
            .execute(&self.pool)
            .await?;

        Ok(())
    }

    /// Searches clips by name or keywords.
    ///
    /// # Errors
    ///
    /// Returns an error if the search fails.
    pub async fn search_clips(&self, query: &str) -> ClipResult<Vec<Clip>> {
        let search_pattern = format!("%{query}%");

        let rows = sqlx::query(
            "SELECT * FROM clips WHERE name LIKE ? OR keywords LIKE ? ORDER BY created_at DESC",
        )
        .bind(&search_pattern)
        .bind(&search_pattern)
        .fetch_all(&self.pool)
        .await?;

        rows.iter().map(Self::row_to_clip).collect()
    }

    #[allow(dead_code)]
    fn row_to_clip(row: &sqlx::sqlite::SqliteRow) -> ClipResult<Clip> {
        use chrono::DateTime;
        use oximedia_core::types::Rational;
        use std::path::PathBuf;

        let id_str: String = row.try_get("id")?;
        let id = id_str
            .parse()
            .map_err(|e: uuid::Error| ClipError::Serialization(e.to_string()))?;

        let keywords_json: String = row.try_get("keywords")?;
        let keywords: Vec<String> = serde_json::from_str(&keywords_json)
            .map_err(|e| ClipError::Serialization(e.to_string()))?;

        let created_at_str: String = row.try_get("created_at")?;
        let created_at = DateTime::parse_from_rfc3339(&created_at_str)
            .map_err(|e| ClipError::Serialization(e.to_string()))?
            .with_timezone(&chrono::Utc);

        let modified_at_str: String = row.try_get("modified_at")?;
        let modified_at = DateTime::parse_from_rfc3339(&modified_at_str)
            .map_err(|e| ClipError::Serialization(e.to_string()))?
            .with_timezone(&chrono::Utc);

        let rating_val: i64 = row.try_get("rating")?;
        #[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)]
        let rating = crate::logging::Rating::from_value(rating_val as u8)
            .unwrap_or(crate::logging::Rating::Unrated);

        let frame_rate = match (
            row.try_get::<Option<i64>, _>("frame_rate_num")?,
            row.try_get::<Option<i64>, _>("frame_rate_den")?,
        ) {
            (Some(num), Some(den)) => Some(Rational::new(num, den)),
            _ => None,
        };

        Ok(Clip {
            id,
            file_path: PathBuf::from(row.try_get::<String, _>("file_path")?),
            name: row.try_get("name")?,
            description: row.try_get("description")?,
            duration: row.try_get("duration")?,
            frame_rate,
            in_point: row.try_get("in_point")?,
            out_point: row.try_get("out_point")?,
            rating,
            is_favorite: row.try_get::<i64, _>("is_favorite")? != 0,
            is_rejected: row.try_get::<i64, _>("is_rejected")? != 0,
            keywords,
            markers: Vec::new(), // Would need separate table
            created_at,
            modified_at,
            custom_metadata: row.try_get("custom_metadata")?,
            camera: None,
        })
    }

    /// Returns the number of clips in the database.
    ///
    /// # Errors
    ///
    /// Returns an error if the count query fails.
    pub async fn count_clips(&self) -> ClipResult<i64> {
        let row = sqlx::query("SELECT COUNT(*) as count FROM clips")
            .fetch_one(&self.pool)
            .await?;

        Ok(row.try_get("count")?)
    }

    /// Saves multiple clips in a single database transaction.
    ///
    /// This is significantly faster than calling `save_clip()` in a loop for
    /// large batches because SQLite commits per-transaction rather than
    /// per-statement.
    ///
    /// # Errors
    ///
    /// Returns an error if the transaction cannot be started or any clip fails
    /// to save.
    pub async fn batch_save_clips(&self, clips: &[Clip]) -> ClipResult<()> {
        let mut tx = self.pool.begin().await?;

        for clip in clips {
            let keywords_json = serde_json::to_string(&clip.keywords)
                .map_err(|e| ClipError::Serialization(e.to_string()))?;

            let (frame_rate_num, frame_rate_den) = clip
                .frame_rate
                .map_or((None, None), |fr| (Some(fr.num), Some(fr.den)));

            sqlx::query(
                r"
                INSERT INTO clips (
                    id, file_path, name, description, duration,
                    frame_rate_num, frame_rate_den, in_point, out_point,
                    rating, is_favorite, is_rejected, keywords,
                    created_at, modified_at, custom_metadata
                ) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
                ON CONFLICT(id) DO UPDATE SET
                    file_path = excluded.file_path,
                    name = excluded.name,
                    description = excluded.description,
                    duration = excluded.duration,
                    frame_rate_num = excluded.frame_rate_num,
                    frame_rate_den = excluded.frame_rate_den,
                    in_point = excluded.in_point,
                    out_point = excluded.out_point,
                    rating = excluded.rating,
                    is_favorite = excluded.is_favorite,
                    is_rejected = excluded.is_rejected,
                    keywords = excluded.keywords,
                    modified_at = excluded.modified_at,
                    custom_metadata = excluded.custom_metadata
                ",
            )
            .bind(clip.id.to_string())
            .bind(clip.file_path.to_string_lossy().to_string())
            .bind(&clip.name)
            .bind(&clip.description)
            .bind(clip.duration)
            .bind(frame_rate_num)
            .bind(frame_rate_den)
            .bind(clip.in_point)
            .bind(clip.out_point)
            .bind(i64::from(clip.rating.to_value()))
            .bind(i64::from(clip.is_favorite))
            .bind(i64::from(clip.is_rejected))
            .bind(keywords_json)
            .bind(clip.created_at.to_rfc3339())
            .bind(clip.modified_at.to_rfc3339())
            .bind(&clip.custom_metadata)
            .execute(&mut *tx)
            .await?;
        }

        tx.commit().await?;
        Ok(())
    }

    /// Returns clips with pagination support.
    ///
    /// `page` is 0-indexed; `page_size` is the number of clips per page.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    pub async fn get_clips_page(&self, page: i64, page_size: i64) -> ClipResult<Vec<Clip>> {
        let offset = page * page_size;
        let rows = sqlx::query("SELECT * FROM clips ORDER BY created_at DESC LIMIT ? OFFSET ?")
            .bind(page_size)
            .bind(offset)
            .fetch_all(&self.pool)
            .await?;

        rows.iter().map(Self::row_to_clip).collect()
    }

    /// Searches clips with pagination support.
    ///
    /// # Errors
    ///
    /// Returns an error if the query fails.
    pub async fn search_clips_page(
        &self,
        query: &str,
        page: i64,
        page_size: i64,
    ) -> ClipResult<Vec<Clip>> {
        let search_pattern = format!("%{query}%");
        let offset = page * page_size;

        let rows = sqlx::query(
            "SELECT * FROM clips WHERE name LIKE ? OR keywords LIKE ? \
             ORDER BY created_at DESC LIMIT ? OFFSET ?",
        )
        .bind(&search_pattern)
        .bind(&search_pattern)
        .bind(page_size)
        .bind(offset)
        .fetch_all(&self.pool)
        .await?;

        rows.iter().map(Self::row_to_clip).collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::PathBuf;

    #[tokio::test]
    async fn test_database_creation() {
        let db = ClipDatabase::new(":memory:")
            .await
            .expect("new should succeed");
        let count = db.count_clips().await.expect("count_clips should succeed");
        assert_eq!(count, 0);
    }

    #[tokio::test]
    async fn test_save_and_get_clip() {
        let db = ClipDatabase::new(":memory:")
            .await
            .expect("new should succeed");

        let mut clip = Clip::new(PathBuf::from("/test.mov"));
        clip.set_name("Test Clip");
        clip.add_keyword("test");

        db.save_clip(&clip).await.expect("operation should succeed");

        let loaded = db
            .get_clip(&clip.id)
            .await
            .expect("get_clip should succeed");
        assert_eq!(loaded.name, "Test Clip");
        assert_eq!(loaded.keywords.len(), 1);
    }

    #[tokio::test]
    async fn test_search_clips() {
        let db = ClipDatabase::new(":memory:")
            .await
            .expect("new should succeed");

        let mut clip1 = Clip::new(PathBuf::from("/test1.mov"));
        clip1.set_name("Interview");

        let mut clip2 = Clip::new(PathBuf::from("/test2.mov"));
        clip2.set_name("Action Scene");

        db.save_clip(&clip1)
            .await
            .expect("operation should succeed");
        db.save_clip(&clip2)
            .await
            .expect("operation should succeed");

        let results = db
            .search_clips("interview")
            .await
            .expect("search_clips should succeed");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].name, "Interview");
    }
}