Skip to content

Commit

Permalink
fix quiet mode parsing for meta_set
Browse files Browse the repository at this point in the history
  • Loading branch information
mrattle committed Oct 23, 2024
1 parent 5fc14d0 commit 1f31e1f
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 163 deletions.
48 changes: 37 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,8 +358,9 @@ impl Client {
K: AsRef<[u8]>,
V: AsMemcachedValue,
{
let kr = key.as_ref();
let kr = Self::validate_key_length(key.as_ref())?;
let vr = value.as_bytes();
let mut quiet_mode = false;

self.conn.write_all(b"ms ").await?;
self.conn.write_all(kr).await?;
Expand All @@ -371,23 +372,48 @@ impl Client {
if let Some(meta_flags) = meta_flags {
self.conn.write_all(b" ").await?;
self.conn.write_all(meta_flags.join(" ").as_bytes()).await?;
if meta_flags.contains(&"q") {
quiet_mode = true;
}
}

self.conn.write_all(b"\r\n").await?;
self.conn.write_all(vr.as_ref()).await?;
self.conn.write_all(b"\r\n").await?;

if quiet_mode {
self.conn.write_all(b"mn\r\n").await?;
}

self.conn.flush().await?;

match self.drive_receive(parse_meta_set_response).await? {
Response::Status(Status::Stored) => Ok(None),
Response::Status(s) => Err(s.into()),
Response::Data(d) => d
.map(|mut items| {
let item = items.remove(0);
Ok(item)
})
.transpose(),
_ => Err(Error::Protocol(Status::Error(ErrorKind::Protocol(None)))),
if quiet_mode {
match self.drive_receive(parse_meta_set_response).await? {
Response::Status(Status::NoOp) => {
println!("got Status:: NoOp");
Ok(None)
}
Response::Status(s) => Err(s.into()),
Response::Data(d) => d
.map(|mut items| {
let item = items.remove(0);
Ok(item)
})
.transpose(),
_ => Err(Error::Protocol(Status::Error(ErrorKind::Protocol(None)))),
}
} else {
match self.drive_receive(parse_meta_set_response).await? {
Response::Status(Status::Stored) => Ok(None),
Response::Status(s) => Err(s.into()),
Response::Data(d) => d
.map(|mut items| {
let item = items.remove(0);
Ok(item)
})
.transpose(),
_ => Err(Error::Protocol(Status::Error(ErrorKind::Protocol(None)))),
}
}
}

Expand Down
21 changes: 20 additions & 1 deletion src/parser/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,20 @@ fn parse_meta_get_data_value(buf: &[u8]) -> IResult<&[u8], Response> {
}

fn parse_meta_set_data_value(buf: &[u8]) -> IResult<&[u8], Response> {
println!("buf: {:?}", std::str::from_utf8(buf).unwrap());
println!("this is triggering before status parsing");
let (input, status) = parse_meta_set_status(buf)?;
println!("this is triggering after status parsing");
match status {
// match arm for "HD" response
Response::Status(Status::Stored) => {
println!("Status:: Stored this is triggering before meta flag parsing");
// no value (data block) or size in this case, potentially just flags
let (input, meta_values_array) = parse_meta_flag_values_as_slice(input)
.map_err(|_| nom::Err::Failure(nom::error::Error::new(buf, Fail)))?;
println!("this is triggering before crlf parsing");
let (input, _) = crlf(input)?; // consume the trailing crlf and leave the buffer empty
println!("this is triggering after crlf parsing");

// early return if there were no flags passed in
if meta_values_array.is_empty() {
Expand All @@ -176,11 +182,13 @@ fn parse_meta_set_data_value(buf: &[u8]) -> IResult<&[u8], Response> {
}
// match arm for "NS" response
Response::Status(Status::NotStored) => {
println!("Status:: NotStored this is triggering before meta flag parsing");
// no value (data block) or size in this case, potentially just flags
let (input, meta_values_array) = parse_meta_flag_values_as_slice(input)
.map_err(|_| nom::Err::Failure(nom::error::Error::new(buf, Fail)))?;
println!("this is triggering before crlf parsing");
let (input, _) = crlf(input)?; // consume the trailing crlf and leave the buffer empty

println!("this is triggering after crlf parsing");
// early return if there were no flags passed in
if meta_values_array.is_empty() {
return Ok((input, Response::Status(Status::NotStored)));
Expand All @@ -196,9 +204,12 @@ fn parse_meta_set_data_value(buf: &[u8]) -> IResult<&[u8], Response> {
}
// match arm for "EX" response
Response::Status(Status::Exists) => {
println!("Status:: Exists this is triggering before meta flag parsing");
// no value (data block) or size in this case, potentially just flags
let (input, meta_values_array) = parse_meta_flag_values_as_slice(input)?;
println!("this is triggering before crlf parsing");
let (input, _) = crlf(input)?; // consume the trailing crlf and leave the buffer empty
println!("this is triggering after crlf parsing");

// early return if there were no flags passed in
if meta_values_array.is_empty() {
Expand All @@ -215,9 +226,12 @@ fn parse_meta_set_data_value(buf: &[u8]) -> IResult<&[u8], Response> {
}
// match arm for "NF" response
Response::Status(Status::NotFound) => {
println!("Status:: NotFound this is triggering before meta flag parsing");
// no value (data block) or size in this case, potentially just flags
let (input, meta_values_array) = parse_meta_flag_values_as_slice(input)?;
println!("this is triggering before crlf parsing");
let (input, _) = crlf(input)?; // consume the trailing crlf and leave the buffer empty
println!("this is triggering after crlf parsing");

// early return if there were no flags passed in
if meta_values_array.is_empty() {
Expand All @@ -232,6 +246,11 @@ fn parse_meta_set_data_value(buf: &[u8]) -> IResult<&[u8], Response> {

Ok((input, Response::Data(Some(vec![value]))))
}
Response::Status(Status::NoOp) => {
println!("Status:: NoOp");
println!("input: {:?}", std::str::from_utf8(input).unwrap());
Ok((input, Response::Status(Status::NoOp)))
}
_ => Err(nom::Err::Error(nom::error::Error::new(
input,
nom::error::ErrorKind::Eof,
Expand Down
Loading

0 comments on commit 1f31e1f

Please sign in to comment.