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
//! Methods for managing indexes.

use rincon_core::api::method::{Method, Operation, Parameters, Prepare, RpcReturnType};
use rincon_core::arango::protocol::{FIELD_CODE, FIELD_ID, PARAM_COLLECTION,
    PATH_API_INDEX};
use super::types::*;

/// Returns an `IndexList` with an attribute indexes containing an array of all
/// index descriptions for the given collection.
///
/// The same information is also available in the identifiers as a hash map with
/// the index handles as keys.
#[derive(Debug, Clone, PartialEq)]
pub struct GetIndexList {
    collection_name: String,
}

impl GetIndexList {
    pub fn new<C>(collection_name: C) -> Self
        where C: Into<String>
    {
        GetIndexList {
            collection_name: collection_name.into(),
        }
    }

    pub fn of_collection<C>(collection_name: C) -> Self
        where C: Into<String>
    {
        GetIndexList::new(collection_name)
    }

    pub fn collection_name(&self) -> &str {
        &self.collection_name
    }
}

impl Method for GetIndexList {
    type Result = IndexList;
    const RETURN_TYPE: RpcReturnType = RpcReturnType {
        result_field: None,
        code_field: Some(FIELD_CODE),
    };
}

impl Prepare for GetIndexList {
    type Content = ();

    fn operation(&self) -> Operation {
        Operation::Read
    }

    fn path(&self) -> String {
        String::from(PATH_API_INDEX)
    }

    fn parameters(&self) -> Parameters {
        let mut params = Parameters::with_capacity(1);
        params.insert(PARAM_COLLECTION, self.collection_name.to_owned());
        params
    }

    fn header(&self) -> Parameters {
        Parameters::empty()
    }

    fn content(&self) -> Option<&Self::Content> {
        None
    }
}

/// Returns the index description for an index of a collection.
#[derive(Debug, Clone, PartialEq)]
pub struct GetIndex {
    index_id: IndexId,
}

impl GetIndex {
    pub fn new(index_id: IndexId) -> Self {
        GetIndex {
            index_id,
        }
    }

    pub fn index_id(&self) -> &IndexId {
        &self.index_id
    }
}

impl Method for GetIndex {
    type Result = Index;
    const RETURN_TYPE: RpcReturnType = RpcReturnType {
        result_field: None,
        code_field: Some(FIELD_CODE),
    };
}

impl Prepare for GetIndex {
    type Content = ();

    fn operation(&self) -> Operation {
        Operation::Read
    }

    fn path(&self) -> String {
        String::from(PATH_API_INDEX)
            + "/" + self.index_id.collection_name()
            + "/" + self.index_id.index_key()
    }

    fn parameters(&self) -> Parameters {
        Parameters::empty()
    }

    fn header(&self) -> Parameters {
        Parameters::empty()
    }

    fn content(&self) -> Option<&Self::Content> {
        None
    }
}

/// Creates a new index in the collection of the given collection name. The
/// type of the index and its details are given in the index parameter.
#[derive(Debug, Clone, PartialEq)]
pub struct CreateIndex {
    collection_name: String,
    index: NewIndex,
}

impl CreateIndex {
    pub fn new<C, I>(collection_name: C, index: I) -> Self
        where C: Into<String>, I: Into<NewIndex>
    {
        CreateIndex {
            collection_name: collection_name.into(),
            index: index.into(),
        }
    }

    pub fn collection_name(&self) -> &str {
        &self.collection_name
    }

    pub fn index(&self) -> &NewIndex {
        &self.index
    }
}

impl Method for CreateIndex {
    type Result = Index;
    const RETURN_TYPE: RpcReturnType = RpcReturnType {
        result_field: None,
        code_field: Some(FIELD_CODE),
    };
}

impl Prepare for CreateIndex {
    type Content = NewIndex;

    fn operation(&self) -> Operation {
        Operation::Create
    }

    fn path(&self) -> String {
        String::from(PATH_API_INDEX)
    }

    fn parameters(&self) -> Parameters {
        let mut params = Parameters::with_capacity(1);
        params.insert(PARAM_COLLECTION, self.collection_name.to_owned());
        params
    }

    fn header(&self) -> Parameters {
        Parameters::empty()
    }

    fn content(&self) -> Option<&Self::Content> {
        Some(&self.index)
    }
}

/// Deletes the index with the given index id.
#[derive(Debug, Clone, PartialEq)]
pub struct DeleteIndex {
    index_id: IndexId,
}

impl DeleteIndex {
    pub fn new(index_id: IndexId) -> Self {
        DeleteIndex {
            index_id,
        }
    }

    pub fn index_id(&self) -> &IndexId {
        &self.index_id
    }
}

impl Method for DeleteIndex {
    type Result = IndexIdOption;
    const RETURN_TYPE: RpcReturnType = RpcReturnType {
        result_field: Some(FIELD_ID),
        code_field: Some(FIELD_CODE),
    };
}

impl Prepare for DeleteIndex {
    type Content = ();

    fn operation(&self) -> Operation {
        Operation::Delete
    }

    fn path(&self) -> String {
        String::from(PATH_API_INDEX)
            + "/" + self.index_id.collection_name()
            + "/" + self.index_id.index_key()
    }

    fn parameters(&self) -> Parameters {
        Parameters::empty()
    }

    fn header(&self) -> Parameters {
        Parameters::empty()
    }

    fn content(&self) -> Option<&Self::Content> {
        None
    }
}