[][src]Function cql_db::write_value

pub fn write_value<TStore: CqlWritable>(
    db_location: &str,
    location: &[u64],
    value: TStore::ValueType
) -> Result<(), Error>

Writes the given value to the given location in the database.

There is an unchecked version of this function if required.

Errors

Will return any I/O errors encountered during the execution of the function. If an error is returned it is not guaranteed that no bytes have been written to the requested location.

Additionally, the following Cql errors may be returned:

  • A DimensionsOutOfRangeError will be returned if the provided location.len() is not equal to the number of dimensions in the database.
  • An IndexOutOfRangeError will be returned if any of the provided indexes in location are less than 1, or greater than that dimension's capacity.
  • An ElementsNotLinkedError will be returned if the provided elements have not been linked.
// Create a database with a maximum capacity of `[2, 5, 3, 2]`
cql_db::create_db::<U64>(
    DATABASE_LOCATION,
    &[2, 5, 6]
)?;

cql_db::link_dimensions::<U64>(
    DATABASE_LOCATION,
    &[2, 4],
)?;

let result = match cql_db::write_value::<U64>(
    DATABASE_LOCATION,
    // not enough dimensions
    &[2, 4],
    10
) {
    Err(error) => match error {
        error::Error::Cql(cql_error) => Some(cql_error),
        _ => None,
    }
    _ => None,
};

assert_eq!(
    result.unwrap(),
    Error::DimensionsOutOfRangeError {
        requested: 2,
        min: 3, // 4 - 1
        max: 3, // 4 - 1
    }
);

let result2 = match cql_db::write_value::<U64>(
    DATABASE_LOCATION,
    // dimension[2] index is too large
    &[2, 4, 7],
    10
) {
    Err(error) => match error {
        error::Error::Cql(cql_error) => Some(cql_error),
        _ => None,
    }
    _ => None,
};

assert_eq!(
    result2.unwrap(),
    Error::IndexOutOfRangeError {
        dimension_index: 2,
        requested: 7,
        min: 1,
        max: 6,
    }
);

let result3 = match cql_db::write_value::<U64>(
    DATABASE_LOCATION,
    // location[2, 3] has not been linked
    &[2, 3, 3],
    10
) {
    Err(error) => match error {
        error::Error::Cql(cql_error) => Some(cql_error),
        _ => None,
    }
    _ => None,
};

assert_eq!(
    result3.unwrap(),
    Error::ElementsNotLinkedError {
        x_dimension: 0,
        x: 2,
        y_dimension: 1,
        y: 3,
    }
);

Panics

Function should not panic. If you get it to panic, please raise an issue in github.

Examples

cql_db::create_db::<U64>(
    DATABASE_LOCATION,
    &[2, 5, 3, 2]
)?;

// higher order elements must be linked before they can be writen to
cql_db::link_dimensions::<U64>(
    DATABASE_LOCATION,
    &[2, 4, 3],
)?;

// Write `5` to location `[2, 4, 3, 1]`
cql_db::write_value::<U64>(
    DATABASE_LOCATION,
    &[2, 4, 3, 1],
    5
)?;