Coverage Report

Created: 2026-06-04 10:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/build/source/nativelink-store/src/redis_utils/ft_create.rs
Line
Count
Source
1
// Copyright 2025 The NativeLink Authors. All rights reserved.
2
//
3
// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//    See LICENSE file for details
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
use redis::RedisError;
16
use redis::aio::ConnectionLike;
17
18
pub(crate) struct SearchSchema {
19
    pub field_name: String,
20
    pub sortable: bool,
21
}
22
23
#[allow(clippy::struct_excessive_bools)]
24
pub(crate) struct FtCreateOptions {
25
    pub prefixes: Vec<String>,
26
    pub nohl: bool,
27
    pub nofields: bool,
28
    pub nofreqs: bool,
29
    pub nooffsets: bool,
30
    pub temporary: Option<u64>,
31
}
32
33
3
pub(crate) async fn ft_create<C>(
34
3
    mut connection_manager: C,
35
3
    index: String,
36
3
    options: FtCreateOptions,
37
3
    schemas: Vec<SearchSchema>,
38
3
) -> Result<(), RedisError>
39
3
where
40
3
    C: ConnectionLike + Send,
41
3
{
42
3
    let mut cmd = redis::cmd("FT.CREATE");
43
3
    let mut ft_create_cmd = cmd.arg(index).arg("ON").arg("HASH");
44
3
    if options.nohl {
45
3
        ft_create_cmd = ft_create_cmd.arg("NOHL");
46
3
    
}0
47
3
    if options.nofields {
48
3
        ft_create_cmd = ft_create_cmd.arg("NOFIELDS");
49
3
    
}0
50
3
    if options.nofreqs {
51
3
        ft_create_cmd = ft_create_cmd.arg("NOFREQS");
52
3
    
}0
53
3
    if options.nooffsets {
54
3
        ft_create_cmd = ft_create_cmd.arg("NOOFFSETS");
55
3
    
}0
56
3
    if let Some(seconds) = options.temporary {
57
3
        ft_create_cmd = ft_create_cmd.arg("TEMPORARY").arg(seconds);
58
3
    
}0
59
3
    if !options.prefixes.is_empty() {
60
3
        ft_create_cmd = ft_create_cmd.arg("PREFIX").arg(options.prefixes.len());
61
3
        for prefix in options.prefixes {
62
3
            ft_create_cmd = ft_create_cmd.arg(prefix);
63
3
        }
64
0
    }
65
3
    ft_create_cmd = ft_create_cmd.arg("SCHEMA");
66
6
    for schema in 
schemas3
{
67
6
        ft_create_cmd = ft_create_cmd.arg(schema.field_name).arg("TAG");
68
6
        if schema.sortable {
69
3
            ft_create_cmd = ft_create_cmd.arg("SORTABLE");
70
3
        }
71
    }
72
73
3
    ft_create_cmd
74
3
        .to_owned()
75
3
        .exec_async(&mut connection_manager)
76
3
        .await?;
77
0
    Ok(())
78
3
}