docbox-core 0.13.2

Docbox core business logic and functionality
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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
use std::time::Duration;

use docbox_core::files::index_file::create_file_index;
use docbox_database::{
    DbPool,
    migrations::apply_tenant_migrations,
    models::{
        document_box::DocumentBox,
        file::{CreateFile, File},
        folder::{CreateFolder, Folder},
        tenant::{CreateTenant, Tenant},
    },
};
use docbox_processing::ProcessingIndexMetadata;
use docbox_search::{
    DatabaseSearchIndex, TenantSearchIndex,
    models::{DocumentPage, FileSearchRequest, SearchRequest},
};
use itertools::Itertools;
use testcontainers::{ContainerAsync, GenericImage};
use testcontainers_modules::postgres::Postgres;
use tokio::time::Instant;
use uuid::{Uuid, uuid};

use crate::common::{
    database::{test_database, test_database_container, test_root_db},
    typesense::{test_search_factory, test_typesense_container},
};
use fake::{Fake, faker::lorem::zh_tw::Words};
use futures::{StreamExt, stream::FuturesUnordered};

mod common;

const SEARCH_TEXT_CONTENT: &str = include_str!("./samples/search_text_content_target.txt");
const SEARCH_TEXT_PHRASE: &str = "Within this content there is a very specific sentence we are searching for, this is that sentence.";
const SEED_FILE_ID: Uuid = uuid!("278397ec-7f78-4a90-a4be-f4d7df6841a7");

const SEARCH_TEXT_CONTENT_2: &str = include_str!("./samples/search_text_content_target_2.txt");
const SEARCH_TEXT_PHRASE_2: &str = "Here is another piece of content I would like to match";
const SEED_FILE_ID_2: Uuid = uuid!("ceaaa68f-df24-4038-b424-db7c0fc6d98a");

async fn seed_index_data(
    db: &DbPool,
    search: &TenantSearchIndex,
    scope: &str,
    samples: usize,
) -> (DocumentBox, Folder) {
    let mut files = Vec::new();
    let mut search_data = Vec::new();

    let scope = scope.to_string();
    let document_box = DocumentBox::create(db, scope.clone()).await.unwrap();
    let root = Folder::create(
        db,
        CreateFolder {
            name: "Root".to_string(),
            document_box: scope.clone(),
            folder_id: None,
            ..Default::default()
        },
    )
    .await
    .unwrap();

    for _ in 0..samples {
        let id = Uuid::new_v4();
        let create_file = CreateFile {
            id,
            name: id.to_string(),
            folder_id: root.id,
            ..Default::default()
        };

        let mut pages = Vec::with_capacity(50);

        for i in 0..2 {
            pages.push(DocumentPage {
                page: i,
                content: Words(50..5000).fake::<Vec<String>>().join(" "),
            });
        }

        let index_metadata = ProcessingIndexMetadata { pages: Some(pages) };

        let data = create_file_index(&create_file, &scope, Some(index_metadata));
        files.push(create_file);
        search_data.push(data);
    }

    println!("generated data for seeding");

    // Insert search data in batches of 5k
    let chunks = search_data.into_iter().chunks(1000);

    let chunks = chunks.into_iter();
    let mut stream = chunks
        .map(|chunk| {
            let chunk = chunk.collect::<Vec<_>>();

            async move {
                search.add_data(chunk).await.unwrap();
                println!("stored chunk of search data");
            }
        })
        .collect::<FuturesUnordered<_>>();

    while let Some(_item) = stream.next().await {}

    println!("stored search data");

    // Create files in batches of 100
    for create in files.chunks(100) {
        let mut futures = create
            .iter()
            .map(|file| File::create(db, file.clone()))
            .collect::<FuturesUnordered<_>>();

        while let Some(result) = futures.next().await {
            result.unwrap();
        }
    }

    (document_box, root)
}

#[allow(unused)]
struct TestTenant {
    tenant: Tenant,
    //
    root_db: DbPool,
    root_db_container: ContainerAsync<Postgres>,
    //
    tenant_db: DbPool,
    tenant_db_container: ContainerAsync<Postgres>,
}

async fn create_test_tenant() -> TestTenant {
    // Initialize root database
    let (root_db, root_db_container) = test_root_db().await;

    // Initialize test tenant
    let tenant_id = Uuid::new_v4();
    let tenant = Tenant::create(
        &root_db,
        CreateTenant {
            id: tenant_id,
            name: "test".to_string(),
            db_name: "test".to_string(),
            db_secret_name: Some("test".to_string()),
            db_iam_user_name: None,
            s3_name: "test".to_string(),
            os_index_name: "test".to_string(),
            env: "Development".to_string(),
            event_queue_url: None,
        },
    )
    .await
    .unwrap();

    // Create tenant database
    let tenant_db_container = test_database_container().await;
    let tenant_db = test_database(&tenant_db_container).await;

    // Apply tenant migrations properly
    {
        let mut root_trans = root_db.begin().await.unwrap();
        let mut trans = tenant_db.begin().await.unwrap();
        apply_tenant_migrations(&mut root_trans, &mut trans, &tenant, None)
            .await
            .unwrap();
        trans.commit().await.unwrap();
        root_trans.commit().await.unwrap();
    }

    TestTenant {
        tenant,
        root_db,
        root_db_container,
        tenant_db,
        tenant_db_container,
    }
}

async fn create_search_index_typesense(
    test_tenant: &TestTenant,
) -> (TenantSearchIndex, ContainerAsync<GenericImage>) {
    let container = test_typesense_container().await;
    let search = test_search_factory(&container).await;
    let index = search.create_search_index(&test_tenant.tenant);

    index.create_index().await.unwrap();

    (index, container)
}

async fn create_search_index_database(test_tenant: &TestTenant) -> TenantSearchIndex {
    // Create search index
    let search = TenantSearchIndex::Database(DatabaseSearchIndex::from_pool(
        test_tenant.tenant_db.clone(),
    ));
    search.create_index().await.unwrap();

    // Apply search migrations
    {
        let mut root_trans = test_tenant.root_db.begin().await.unwrap();
        let mut trans = test_tenant.tenant_db.begin().await.unwrap();

        search
            .apply_migrations(&test_tenant.tenant, &mut root_trans, &mut trans, None)
            .await
            .unwrap();

        trans.commit().await.unwrap();
        root_trans.commit().await.unwrap();
    }

    search
}

#[tokio::test]
async fn test_search() {
    let test_tenant = create_test_tenant().await;
    let search = create_search_index_database(&test_tenant).await;

    let db = &test_tenant.tenant_db;

    let scope = "test".to_string();
    let _document_box = DocumentBox::create(db, scope.clone()).await.unwrap();
    let root = Folder::create(
        db,
        CreateFolder {
            name: "Root".to_string(),
            document_box: scope.clone(),
            folder_id: None,
            ..Default::default()
        },
    )
    .await
    .unwrap();

    // Seed our special search content
    let create_file = CreateFile {
        id: SEED_FILE_ID,
        name: SEED_FILE_ID.to_string(),
        folder_id: root.id,
        ..Default::default()
    };

    let data = create_file_index(
        &create_file,
        &scope,
        Some(ProcessingIndexMetadata {
            pages: Some(vec![DocumentPage {
                page: 0,
                content: SEARCH_TEXT_CONTENT.to_string(),
            }]),
        }),
    );
    File::create(db, create_file).await.unwrap();
    search.add_data(vec![data]).await.unwrap();

    let results = search
        .search_index(
            &["test".to_string()],
            SearchRequest {
                query: Some(SEARCH_TEXT_PHRASE.to_string()),
                include_content: true,
                ..Default::default()
            },
            None,
        )
        .await
        .unwrap();

    assert_eq!(results.total_hits, 1);
    let results = search
        .search_index_file(
            &"test".to_string(),
            SEED_FILE_ID,
            FileSearchRequest {
                query: Some(SEARCH_TEXT_PHRASE.to_string()),
                ..Default::default()
            },
        )
        .await
        .unwrap();

    assert_eq!(results.total_hits, 1);
    // dbg!(&results);
}

#[tokio::test]
#[ignore = "Benchmarking test for search performance"]
async fn test_search_database() {
    let test_tenant = create_test_tenant().await;
    let search = create_search_index_database(&test_tenant).await;

    println!("finished initialize");

    let (document_box, root) = seed_index_data(&test_tenant.tenant_db, &search, "test", 100).await;

    // Seed our special search content
    let create_file = CreateFile {
        id: SEED_FILE_ID,
        name: SEED_FILE_ID.to_string(),
        folder_id: root.id,
        ..Default::default()
    };

    let data = create_file_index(
        &create_file,
        &document_box.scope,
        Some(ProcessingIndexMetadata {
            pages: Some(vec![DocumentPage {
                page: 0,
                content: SEARCH_TEXT_CONTENT.to_string(),
            }]),
        }),
    );
    File::create(&test_tenant.tenant_db, create_file)
        .await
        .unwrap();
    search.add_data(vec![data]).await.unwrap();

    // Seed our special search content
    let create_file = CreateFile {
        id: SEED_FILE_ID_2,
        name: SEED_FILE_ID_2.to_string(),
        folder_id: root.id,
        ..Default::default()
    };

    let data = create_file_index(
        &create_file,
        &document_box.scope,
        Some(ProcessingIndexMetadata {
            pages: Some(vec![DocumentPage {
                page: 0,
                content: SEARCH_TEXT_CONTENT_2.to_string(),
            }]),
        }),
    );
    File::create(&test_tenant.tenant_db, create_file)
        .await
        .unwrap();
    search.add_data(vec![data]).await.unwrap();

    println!("finished seeding first data batch");

    seed_index_data(&test_tenant.tenant_db, &search, "test_other_scope", 50_000).await;

    println!("finished seeding data");

    const ITERATIONS: usize = 5;

    let mut iterations = Vec::new();

    for i in 0..ITERATIONS {
        let start = Instant::now();
        let results = search
            .search_index(
                &["test".to_string()],
                SearchRequest {
                    query: Some(SEARCH_TEXT_PHRASE.to_string()),
                    include_content: true,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();
        let elapsed = start.elapsed();
        iterations.push(elapsed);
        println!("finished iteration {} {}ms", i, elapsed.as_millis());
        dbg!(results);
    }

    let min_duration = iterations.iter().min().unwrap();
    let max_duration = iterations.iter().max().unwrap();
    let sum_duration: Duration = iterations.iter().sum();
    let avg_duration = sum_duration / iterations.len() as u32;

    println!(
        "FIRST: min = {}ms, max = {}ms, avg = {}ms",
        min_duration.as_millis(),
        max_duration.as_millis(),
        avg_duration.as_millis()
    );

    let mut iterations = Vec::new();

    for i in 0..ITERATIONS {
        let start = Instant::now();
        let results = search
            .search_index(
                &["test".to_string()],
                SearchRequest {
                    query: Some(SEARCH_TEXT_PHRASE_2.to_string()),
                    include_content: true,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();
        let elapsed = start.elapsed();
        iterations.push(elapsed);
        println!("finished iteration {} {}ms", i, elapsed.as_millis());
        dbg!(results);
    }

    let min_duration = iterations.iter().min().unwrap();
    let max_duration = iterations.iter().max().unwrap();
    let sum_duration: Duration = iterations.iter().sum();
    let avg_duration = sum_duration / iterations.len() as u32;

    println!(
        "SECOND: min = {}ms, max = {}ms, avg = {}ms",
        min_duration.as_millis(),
        max_duration.as_millis(),
        avg_duration.as_millis()
    );
}

#[tokio::test]
#[ignore = "Benchmarking test for search performance"]
async fn test_search_typesense() {
    let test_tenant = create_test_tenant().await;
    let (search, _search_container) = create_search_index_typesense(&test_tenant).await;

    println!("finished initialize");

    let (document_box, root) = seed_index_data(&test_tenant.tenant_db, &search, "test", 100).await;

    // Seed our special search content
    let create_file = CreateFile {
        id: SEED_FILE_ID,
        name: SEED_FILE_ID.to_string(),
        folder_id: root.id,
        ..Default::default()
    };

    let data = create_file_index(
        &create_file,
        &document_box.scope,
        Some(ProcessingIndexMetadata {
            pages: Some(vec![DocumentPage {
                page: 0,
                content: SEARCH_TEXT_CONTENT.to_string(),
            }]),
        }),
    );
    File::create(&test_tenant.tenant_db, create_file)
        .await
        .unwrap();
    search.add_data(vec![data]).await.unwrap();

    // Seed our special search content
    let create_file = CreateFile {
        id: SEED_FILE_ID_2,
        name: SEED_FILE_ID_2.to_string(),
        folder_id: root.id,
        ..Default::default()
    };

    let data = create_file_index(
        &create_file,
        &document_box.scope,
        Some(ProcessingIndexMetadata {
            pages: Some(vec![DocumentPage {
                page: 0,
                content: SEARCH_TEXT_CONTENT_2.to_string(),
            }]),
        }),
    );
    File::create(&test_tenant.tenant_db, create_file)
        .await
        .unwrap();
    search.add_data(vec![data]).await.unwrap();

    println!("finished seeding first data batch");

    seed_index_data(&test_tenant.tenant_db, &search, "test_other_scope", 50_000).await;

    println!("finished seeding data");

    const ITERATIONS: usize = 5;

    let mut iterations = Vec::new();

    for i in 0..ITERATIONS {
        let start = Instant::now();
        let results = search
            .search_index(
                &["test".to_string()],
                SearchRequest {
                    query: Some(SEARCH_TEXT_PHRASE.to_string()),
                    include_content: true,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();
        let elapsed = start.elapsed();
        iterations.push(elapsed);
        println!("finished iteration {} {}ms", i, elapsed.as_millis());
        dbg!(results);
    }

    let min_duration = iterations.iter().min().unwrap();
    let max_duration = iterations.iter().max().unwrap();
    let sum_duration: Duration = iterations.iter().sum();
    let avg_duration = sum_duration / iterations.len() as u32;

    println!(
        "FIRST: min = {}ms, max = {}ms, avg = {}ms",
        min_duration.as_millis(),
        max_duration.as_millis(),
        avg_duration.as_millis()
    );

    let mut iterations = Vec::new();

    for i in 0..ITERATIONS {
        let start = Instant::now();
        let results = search
            .search_index(
                &["test".to_string()],
                SearchRequest {
                    query: Some(SEARCH_TEXT_PHRASE_2.to_string()),
                    include_content: true,
                    ..Default::default()
                },
                None,
            )
            .await
            .unwrap();
        let elapsed = start.elapsed();
        iterations.push(elapsed);
        println!("finished iteration {} {}ms", i, elapsed.as_millis());
        dbg!(results);
    }

    let min_duration = iterations.iter().min().unwrap();
    let max_duration = iterations.iter().max().unwrap();
    let sum_duration: Duration = iterations.iter().sum();
    let avg_duration = sum_duration / iterations.len() as u32;

    println!(
        "SECOND: min = {}ms, max = {}ms, avg = {}ms",
        min_duration.as_millis(),
        max_duration.as_millis(),
        avg_duration.as_millis()
    );
}