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
use serde::Serialize;
use serde_json::Error;

use crate::Mutation;

impl Mutation {
    ///
    /// Create new Dgraph Mutation object.
    ///
    /// Mutation represent required modification of data in DB.
    /// Mutation provides two main ways to set data: JSON and RDF N-Quad.
    /// You can choose whichever way is convenient.
    /// JSON way has implemented to helper functions.
    ///
    pub fn new() -> Self {
        Self {
            ..Default::default()
        }
    }

    ///
    /// Can be applied on a Mutation object to not run conflict detection over the index,
    /// which would decrease the number of transaction conflicts and aborts.
    /// However, this would come at the cost of potentially inconsistent upsert operations.
    ///
    /// # Example
    ///
    /// ```
    /// use dgraph_tonic::Mutation;
    /// let mut mu = Mutation::new().with_ignored_index_conflict();
    /// ```
    ///
    #[cfg(feature = "dgraph-1-0")]
    pub fn with_ignored_index_conflict(mut self) -> Self {
        self.ignore_index_conflict = true;
        self
    }

    ///
    /// Set set JSON data in Mutation.
    ///
    /// # Arguments
    ///
    /// * `value` - ref to struct which can be serialized into JSON
    ///
    /// # Errors
    ///
    /// Return serde_json:Error when value cannot be serialized to JSON format
    ///
    /// # Examples
    ///
    /// ```
    /// use dgraph_tonic::Mutation;
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct Person {
    ///   uid: String,
    ///   name: String,
    /// }
    ///
    /// let p = Person {
    ///   uid:  "_:alice".into(),
    ///   name: "Alice".into(),
    /// };
    ///
    /// let mut mu = Mutation::new();
    /// mu.set_set_json(&p).expect("JSON");
    /// ```
    ///
    pub fn set_set_json<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
    where
        T: Serialize,
    {
        let set_json = serde_json::to_vec(&value)?;
        self.set_json = set_json;
        Ok(())
    }

    ///
    /// Set delete JSON data in Mutation.
    ///
    /// # Arguments
    ///
    /// * `value` - ref to struct which can be serialized into JSON
    ///
    /// # Errors
    ///
    /// Return serde_json:Error when value cannot be serialized to JSON format
    ///
    /// # Examples
    ///
    /// ```
    /// use dgraph_tonic::Mutation;
    /// use serde::Serialize;
    ///
    /// #[derive(Serialize)]
    /// struct Person {
    ///   uid: String,
    ///   name: Option<String>,
    /// }
    ///
    /// let p = Person {
    ///   uid:  "_:0x1".into(),
    ///   name: None,
    /// };
    ///
    /// let mut mu = Mutation::new();
    /// //remove name predicate
    /// mu.set_delete_json(&p).expect("JSON");
    /// ```
    ///
    pub fn set_delete_json<T: ?Sized>(&mut self, value: &T) -> Result<(), Error>
    where
        T: Serialize,
    {
        let delete_json = serde_json::to_vec(&value)?;
        self.delete_json = delete_json;
        Ok(())
    }

    ///
    /// Set set Nquads in Mutation.
    ///
    /// # Arguments
    ///
    /// * `nquads` - set nquads
    ///
    /// # Examples
    ///
    /// ```
    /// use dgraph_tonic::Mutation;
    ///
    /// let mut mu = Mutation::new();
    /// //remove name predicate
    /// mu.set_set_nquads(r#"uid(user) <email> "correct_email@dgraph.io" ."#);
    /// ```
    ///
    pub fn set_set_nquads<S: Into<String>>(&mut self, nquads: S) {
        let n_quads: String = nquads.into();
        self.set_nquads = n_quads.as_bytes().to_vec();
    }

    ///
    /// Set delete Nquads in Mutation.
    ///
    /// # Arguments
    ///
    /// * `nquads` - delete nquads
    ///
    /// # Examples
    ///
    /// ```
    /// use dgraph_tonic::Mutation;
    ///
    /// let mut mu = Mutation::new();
    /// //remove name predicate
    /// mu.set_set_nquads(r#"uid(user) <email> * ."#);
    /// ```
    ///
    pub fn set_delete_nquads<S: Into<String>>(&mut self, nquads: S) {
        let n_quads: String = nquads.into();
        self.del_nquads = n_quads.as_bytes().to_vec();
    }

    ///
    /// Set set condition in Mutation.
    ///
    /// # Arguments
    ///
    /// * `cond` - set nquads
    ///
    /// # Examples
    ///
    /// ```
    /// use dgraph_tonic::Mutation;
    ///
    /// let mut mu = Mutation::new();
    /// //remove name predicate
    /// mu.set_cond("@if(eq(len(user), 1))");
    /// ```
    ///
    pub fn set_cond<S: Into<String>>(&mut self, cond: S) {
        self.cond = cond.into();
    }
}