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
use bigtable::MutateRowsRequest_Entry;
use data::{Mutation, Mutation_SetCell, ReadModifyWriteRule};
use error::BTErr;
use goauth::auth::Token;
use method::{BigTable, MutateRows, ReadModifyWriteRow, ReadRows, SampleRowKeys};
use protobuf::RepeatedField;
use request::BTRequest;
use serde_json;
use support::Table;
use utils::*;

pub fn get_row_prefix(prefix: Option<&str>) -> String {
    match prefix {
        Some(x) => String::from(x),
        None => String::from("r"),
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Row {
    pub row_key: String,
    pub family: String,
    pub qualifier: String,
    pub value: String,
}

impl Default for Row {
    fn default() -> Self {
        Row {
            row_key: String::from("dummy_row_key"),
            family: String::from("dummy_column_family"),
            qualifier: String::from("dummy_column_qualifier"),
            value: String::from("dummy_value"),
        }
    }
}

/// ```
/// extern crate bigtable as bt;
///
/// use bt::utils::*;
/// use bt::error::BTErr;
/// use bt::wraps;
///
/// fn main() {
/// # #[allow(dead_code)]
/// # fn write_rows() -> Result<(), BTErr> {
///
///     let mut rows: Vec<wraps::Row> = vec!(wraps::Row::default()); // put some real data here
///     let token = get_auth_token("dummy_credentials_file_for_tests.json", true)?;
///     let table = Default::default();
///
///     let _ = wraps::bulk_write_rows(&mut rows, &token, table);
///
/// #    Ok(())
/// # }
/// }
/// ```
pub fn bulk_write_rows(rows: &mut Vec<Row>, token: &Token, table: Table) -> Result<String, BTErr> {
    let mut req = BTRequest {
        base: None,
        table,
        method: MutateRows::new(),
    };

    let mut mutate_entries = Vec::new();

    for row in rows.drain(..) {
        let mut mutate_entry = MutateRowsRequest_Entry::new();
        mutate_entry.set_row_key(encode_str(&row.row_key));

        let mut mutations: Vec<Mutation> = Vec::new();
        let mut mutation = Mutation::new();

        let set_cell = make_setcell_mutation(&row.qualifier, &row.family, encode_str(&row.value));

        mutation.set_set_cell(set_cell);
        mutations.push(mutation);
        mutate_entry.set_mutations(RepeatedField::from_vec(mutations));
        mutate_entries.push(mutate_entry);
    }

    req.method
        .payload_mut()
        .set_entries(RepeatedField::from_vec(mutate_entries));

    let response = req.execute(token)?;
    Ok(serde_json::to_string(&response)?)
}

/// ```
/// extern crate bigtable as bt;
///
/// use bt::utils::*;
/// use bt::error::BTErr;
/// use bt::wraps;
///
/// fn main() {
/// # #[allow(dead_code)]
/// # fn write_rows() -> Result<(), BTErr> {
///
///     let mut rows: Vec<wraps::Row> = vec!(wraps::Row::default()); // put some real data here
///     let token = get_auth_token("dummy_credentials_file_for_tests.json", true)?;
///     let table = Default::default();
///
///     let _ = wraps::write_rows(&mut rows, &token, &table);
///
/// #    Ok(())
/// # }
/// }
/// ```
pub fn write_rows(rows: &mut Vec<Row>, token: &Token, table: &Table) -> Result<usize, BTErr> {
    let mut total = 0;

    for row in rows.drain(..) {
        let mut req = BTRequest {
            base: None,
            table: table.clone(),
            method: ReadModifyWriteRow::new(),
        };

        let mut rules: Vec<ReadModifyWriteRule> = Vec::new();

        let rule = make_readmodifywrite_rule(&row.qualifier, &row.family, encode_str(&row.value));

        rules.push(rule);

        req.method
            .payload_mut()
            .set_row_key(encode_str(&row.row_key));
        req.method
            .payload_mut()
            .set_rules(RepeatedField::from_vec(rules));

        let _ = req.execute(token)?;
        total += 1;
    }
    Ok(total)
}

/// ```
/// extern crate bigtable as bt;
///
/// use bt::utils::*;
/// use bt::error::BTErr;
/// use bt::wraps;
///
/// fn main() {
/// # #[allow(dead_code)]
/// # fn read_rows(limit: i64) -> Result<(), BTErr> {
///
///    let token = get_auth_token("dummy_credentials_file_for_tests.json", true)?;
///    let table = Default::default();
///
///    let _ = wraps::read_rows(&table, &token, Some(limit));
///
/// # Ok(())
/// # }
/// }
/// ```
pub fn read_rows(
    table: &Table,
    token: &Token,
    rows_limit: Option<i64>,
) -> Result<serde_json::Value, BTErr> {
    let mut req = BTRequest {
        base: None,
        table: table.clone(),
        method: ReadRows::new(),
    };

    if let Some(x) = rows_limit {
        req.method.payload_mut().set_rows_limit(x);
    }

    let response = req.execute(token)?;
    Ok(response)
}

fn make_setcell_mutation(
    column_qualifier: &str,
    column_family: &str,
    blob: Vec<u8>,
) -> Mutation_SetCell {
    let mut set_cell = Mutation_SetCell::new();
    set_cell.set_column_qualifier(encode_str(column_qualifier));
    set_cell.set_family_name(String::from(column_family));
    set_cell.set_timestamp_micros(-1);
    set_cell.set_value(blob);
    set_cell
}

fn make_readmodifywrite_rule(
    column_qualifier: &str,
    column_familiy: &str,
    blob: Vec<u8>,
) -> ReadModifyWriteRule {
    let mut rule = ReadModifyWriteRule::new();
    rule.set_family_name(String::from(column_familiy));
    rule.set_column_qualifier(encode_str(column_qualifier));
    rule.set_append_value(blob);
    rule
}

fn sample_row_keys(token: &Token) -> Result<String, BTErr> {
    let req = BTRequest {
        base: None,
        table: Default::default(),
        method: SampleRowKeys::new(),
    };
    let response = req.execute(token)?;
    Ok(serde_json::to_string(&response)?)
}