Skip to content

Commit

Permalink
Fixed .update() request
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowneee committed Jul 30, 2023
1 parent 8679dca commit a322ea1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 19 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0


## Unreleased
### Fixed
- `.update()` request sends correct request type.

### Added
- `Executor` trait, which sends encoded request;
- `.stream()`, `.transaction()` and `.transaction_builder()` methods moved to `Executor` trait;
Expand Down
28 changes: 24 additions & 4 deletions examples/schema.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use tarantool_rs::{schema::SpaceMetadata, Connection, IteratorType};
use rmpv::Value;
use tarantool_rs::{Connection, IteratorType};
use tracing::info;

#[tokio::main]
Expand All @@ -10,11 +11,30 @@ async fn main() -> Result<(), anyhow::Error> {
let data = conn.find_space_by_name("clients").await?;
info!("{:?}", data);
let space = data.unwrap();
space.upsert(vec!["=".into()], vec![2.into()]).await?;
info!(
"{:?}",
"Pre: {:?}",
space
.select::<(i64,)>(0, None, None, Some(IteratorType::All), vec![])
.select::<(i64, String)>(0, None, None, Some(IteratorType::All), vec![])
.await?
);
space
.upsert(vec!["=".into()], vec![2.into(), "Second".into()])
.await?;
space
.update(
0,
vec![2.into()],
vec![Value::Array(vec![
"=".into(),
2.into(),
"Second (updated)".into(),
])],
)
.await?;
info!(
"Post: {:?}",
space
.select::<(i64, String)>(0, None, None, Some(IteratorType::All), vec![])
.await?
);
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/client/connection.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::fmt;
use std::{
fmt,
sync::{
atomic::{AtomicU32, Ordering},
Arc,
Expand Down
14 changes: 6 additions & 8 deletions src/client/schema/space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ use serde::{de::DeserializeOwned, Deserialize};

use super::{IndexMetadata, SystemSpacesId};
use crate::{
client::ConnectionLike,
codec::request::{Insert, Select},
utils::UniqueIdNameMap,
Error, Executor, IteratorType, Result,
client::ConnectionLike, utils::UniqueIdNameMap, Error, Executor, IteratorType, Result,
};

/// Space metadata from with its indices metadata from [system views](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/system_views/).
/// Space metadata with its indices metadata from [system views](https://www.tarantool.io/en/doc/latest/reference/reference_lua/box_space/system_views/).
#[derive(Clone, Deserialize)]
pub struct SpaceMetadata {
id: u32,
Expand Down Expand Up @@ -73,9 +70,10 @@ impl SpaceMetadata {
)
.await?
.into_iter()
.next() else {
return Ok(None)
};
.next()
else {
return Ok(None);
};
this.load_indices(conn).await?;
Ok(Some(this))
}
Expand Down
8 changes: 5 additions & 3 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,12 @@ impl Decoder for ClientCodec {
type Error = CodecDecodeError;

fn decode(&mut self, src: &mut BytesMut) -> Result<Option<Self::Item>, Self::Error> {
let Some(next_frame_length) = self.length_decoder
let Some(next_frame_length) = self
.length_decoder
.decode(src)
.map_err(CodecDecodeError::Decode)? else {
return Ok(None);
.map_err(CodecDecodeError::Decode)?
else {
return Ok(None);
};
if src.len() >= next_frame_length {
self.length_decoder.reset();
Expand Down
7 changes: 4 additions & 3 deletions src/codec/request/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
errors::EncodingError,
};

use super::Request;
use super::{Request, INDEX_BASE_VALUE};

#[derive(Clone, Debug)]
pub(crate) struct Update {
Expand All @@ -38,14 +38,15 @@ impl Request for Update {
where
Self: Sized,
{
RequestType::Replace
RequestType::Update
}

// NOTE: `&mut buf: mut` is required since I don't get why compiler complain
fn encode(&self, mut buf: &mut dyn Write) -> Result<(), EncodingError> {
rmp::encode::write_map_len(&mut buf, 4)?;
rmp::encode::write_map_len(&mut buf, 5)?;
write_kv_u32(buf, keys::SPACE_ID, self.space_id)?;
write_kv_u32(buf, keys::INDEX_ID, self.index_id)?;
write_kv_u32(buf, keys::INDEX_BASE, INDEX_BASE_VALUE)?;
write_kv_array(buf, keys::KEY, &self.keys)?;
write_kv_array(buf, keys::TUPLE, &self.tuple)?;
Ok(())
Expand Down

0 comments on commit a322ea1

Please sign in to comment.