couchbase_core/memdx/
ops_util.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use crate::memdx::client::ResponseContext;
20use crate::memdx::dispatcher::Dispatcher;
21use crate::memdx::error::Result;
22use crate::memdx::magic::Magic;
23use crate::memdx::opcode::OpCode;
24use crate::memdx::packet::RequestPacket;
25use crate::memdx::pendingop::StandardPendingOp;
26use crate::memdx::request::{GetCollectionIdRequest, PingRequest};
27use crate::memdx::response::{GetCollectionIdResponse, PingResponse};
28
29pub struct OpsUtil {}
30
31impl OpsUtil {
32    pub async fn get_collection_id<D>(
33        &self,
34        dispatcher: &D,
35        request: GetCollectionIdRequest<'_>,
36    ) -> Result<StandardPendingOp<GetCollectionIdResponse>>
37    where
38        D: Dispatcher,
39    {
40        let full_name = format!("{}.{}", &request.scope_name, &request.collection_name);
41
42        let op = dispatcher
43            .dispatch(
44                RequestPacket {
45                    magic: Magic::Req,
46                    op_code: OpCode::GetCollectionId,
47                    datatype: 0,
48                    vbucket_id: None,
49                    cas: None,
50                    extras: None,
51                    key: None,
52                    value: Some(full_name.as_bytes()),
53                    framing_extras: None,
54                    opaque: None,
55                },
56                Some(ResponseContext {
57                    cas: None,
58                    subdoc_info: None,
59                    is_persistent: false,
60                    scope_name: Some(request.scope_name.to_string()),
61                    collection_name: Some(request.collection_name.to_string()),
62                }),
63            )
64            .await?;
65
66        Ok(StandardPendingOp::new(op))
67    }
68
69    pub async fn ping<D>(
70        &self,
71        dispatcher: &D,
72        _request: PingRequest<'_>,
73    ) -> Result<StandardPendingOp<PingResponse>>
74    where
75        D: Dispatcher,
76    {
77        let op = dispatcher
78            .dispatch(
79                RequestPacket {
80                    magic: Magic::Req,
81                    op_code: OpCode::Noop,
82                    datatype: 0,
83                    vbucket_id: None,
84                    cas: None,
85                    extras: None,
86                    key: None,
87                    value: None,
88                    framing_extras: None,
89                    opaque: None,
90                },
91                None,
92            )
93            .await?;
94
95        Ok(StandardPendingOp::new(op))
96    }
97}