/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 | 1 | pub(crate) async fn ft_create<C>( |
34 | 1 | mut connection_manager: C, |
35 | 1 | index: String, |
36 | 1 | options: FtCreateOptions, |
37 | 1 | schemas: Vec<SearchSchema>, |
38 | 1 | ) -> Result<(), RedisError> |
39 | 1 | where |
40 | 1 | C: ConnectionLike + Send, |
41 | 1 | { |
42 | 1 | let mut cmd = redis::cmd("FT.CREATE"); |
43 | 1 | let mut ft_create_cmd = cmd.arg(index).arg("ON").arg("HASH"); |
44 | 1 | if options.nohl { Branch (44:8): [True: 0, False: 0]
Branch (44:8): [Folded - Ignored]
Branch (44:8): [Folded - Ignored]
Branch (44:8): [True: 0, False: 0]
Branch (44:8): [True: 1, False: 0]
|
45 | 1 | ft_create_cmd = ft_create_cmd.arg("NOHL"); |
46 | 1 | }0 |
47 | 1 | if options.nofields { Branch (47:8): [True: 0, False: 0]
Branch (47:8): [Folded - Ignored]
Branch (47:8): [Folded - Ignored]
Branch (47:8): [True: 0, False: 0]
Branch (47:8): [True: 1, False: 0]
|
48 | 1 | ft_create_cmd = ft_create_cmd.arg("NOFIELDS"); |
49 | 1 | }0 |
50 | 1 | if options.nofreqs { Branch (50:8): [True: 0, False: 0]
Branch (50:8): [Folded - Ignored]
Branch (50:8): [Folded - Ignored]
Branch (50:8): [True: 0, False: 0]
Branch (50:8): [True: 1, False: 0]
|
51 | 1 | ft_create_cmd = ft_create_cmd.arg("NOFREQS"); |
52 | 1 | }0 |
53 | 1 | if options.nooffsets { Branch (53:8): [True: 0, False: 0]
Branch (53:8): [Folded - Ignored]
Branch (53:8): [Folded - Ignored]
Branch (53:8): [True: 0, False: 0]
Branch (53:8): [True: 1, False: 0]
|
54 | 1 | ft_create_cmd = ft_create_cmd.arg("NOOFFSETS"); |
55 | 1 | }0 |
56 | 1 | if let Some(seconds) = options.temporary { Branch (56:12): [True: 0, False: 0]
Branch (56:12): [Folded - Ignored]
Branch (56:12): [Folded - Ignored]
Branch (56:12): [True: 0, False: 0]
Branch (56:12): [True: 1, False: 0]
|
57 | 1 | ft_create_cmd = ft_create_cmd.arg("TEMPORARY").arg(seconds); |
58 | 1 | }0 |
59 | 1 | if !options.prefixes.is_empty() { Branch (59:8): [True: 0, False: 0]
Branch (59:8): [Folded - Ignored]
Branch (59:8): [Folded - Ignored]
Branch (59:8): [True: 0, False: 0]
Branch (59:8): [True: 1, False: 0]
|
60 | 1 | ft_create_cmd = ft_create_cmd.arg("PREFIX").arg(options.prefixes.len()); |
61 | 2 | for prefix1 in options.prefixes { |
62 | 1 | ft_create_cmd = ft_create_cmd.arg(prefix); |
63 | 1 | } |
64 | 0 | } |
65 | 1 | ft_create_cmd = ft_create_cmd.arg("SCHEMA"); |
66 | 3 | for schema2 in schemas { |
67 | 2 | ft_create_cmd = ft_create_cmd.arg(schema.field_name).arg("TAG"); |
68 | 2 | if schema.sortable { Branch (68:12): [True: 0, False: 0]
Branch (68:12): [Folded - Ignored]
Branch (68:12): [Folded - Ignored]
Branch (68:12): [True: 0, False: 0]
Branch (68:12): [True: 1, False: 1]
|
69 | 1 | ft_create_cmd = ft_create_cmd.arg("SORTABLE"); |
70 | 1 | } |
71 | | } |
72 | | |
73 | 1 | ft_create_cmd |
74 | 1 | .to_owned() |
75 | 1 | .exec_async(&mut connection_manager) |
76 | 1 | .await?; |
77 | 0 | Ok(()) |
78 | 1 | } |