Skip to content

Commit

Permalink
bytes context and remove sync_log
Browse files Browse the repository at this point in the history
  • Loading branch information
overvenus committed May 14, 2018
1 parent 93543ee commit daf9fb9
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 40 deletions.
2 changes: 1 addition & 1 deletion proto/eraftpb.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ message Entry {
uint64 index = 3;
bytes data = 4;
bool sync_log = 5;
uint64 context = 6;
bytes context = 6;
}

message SnapshotMetadata {
Expand Down
51 changes: 29 additions & 22 deletions src/eraftpb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub struct Entry {
pub index: u64,
pub data: ::std::vec::Vec<u8>,
pub sync_log: bool,
pub context: u64,
pub context: ::std::vec::Vec<u8>,
// special fields
unknown_fields: ::protobuf::UnknownFields,
cached_size: ::protobuf::CachedSize,
Expand Down Expand Up @@ -179,26 +179,37 @@ impl Entry {
&mut self.sync_log
}

// uint64 context = 6;
// bytes context = 6;

pub fn clear_context(&mut self) {
self.context = 0;
self.context.clear();
}

// Param is passed by value, moved
pub fn set_context(&mut self, v: u64) {
pub fn set_context(&mut self, v: ::std::vec::Vec<u8>) {
self.context = v;
}

pub fn get_context(&self) -> u64 {
self.context
// Mutable pointer to the field.
// If field is not initialized, it is initialized with default value first.
pub fn mut_context(&mut self) -> &mut ::std::vec::Vec<u8> {
&mut self.context
}

// Take field
pub fn take_context(&mut self) -> ::std::vec::Vec<u8> {
::std::mem::replace(&mut self.context, ::std::vec::Vec::new())
}

fn get_context_for_reflect(&self) -> &u64 {
pub fn get_context(&self) -> &[u8] {
&self.context
}

fn mut_context_for_reflect(&mut self) -> &mut u64 {
fn get_context_for_reflect(&self) -> &::std::vec::Vec<u8> {
&self.context
}

fn mut_context_for_reflect(&mut self) -> &mut ::std::vec::Vec<u8> {
&mut self.context
}
}
Expand Down Expand Up @@ -244,11 +255,7 @@ impl ::protobuf::Message for Entry {
self.sync_log = tmp;
},
6 => {
if wire_type != ::protobuf::wire_format::WireTypeVarint {
return ::std::result::Result::Err(::protobuf::rt::unexpected_wire_type(wire_type));
}
let tmp = is.read_uint64()?;
self.context = tmp;
::protobuf::rt::read_singular_proto3_bytes_into(wire_type, is, &mut self.context)?;
},
_ => {
::protobuf::rt::read_unknown_or_skip_group(field_number, wire_type, is, self.mut_unknown_fields())?;
Expand Down Expand Up @@ -277,8 +284,8 @@ impl ::protobuf::Message for Entry {
if self.sync_log != false {
my_size += 2;
}
if self.context != 0 {
my_size += ::protobuf::rt::value_size(6, self.context, ::protobuf::wire_format::WireTypeVarint);
if !self.context.is_empty() {
my_size += ::protobuf::rt::bytes_size(6, &self.context);
}
my_size += ::protobuf::rt::unknown_fields_size(self.get_unknown_fields());
self.cached_size.set(my_size);
Expand All @@ -301,8 +308,8 @@ impl ::protobuf::Message for Entry {
if self.sync_log != false {
os.write_bool(5, self.sync_log)?;
}
if self.context != 0 {
os.write_uint64(6, self.context)?;
if !self.context.is_empty() {
os.write_bytes(6, &self.context)?;
}
os.write_unknown_fields(self.get_unknown_fields())?;
::std::result::Result::Ok(())
Expand Down Expand Up @@ -373,7 +380,7 @@ impl ::protobuf::MessageStatic for Entry {
Entry::get_sync_log_for_reflect,
Entry::mut_sync_log_for_reflect,
));
fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeUint64>(
fields.push(::protobuf::reflect::accessor::make_simple_field_accessor::<_, ::protobuf::types::ProtobufTypeBytes>(
"context",
Entry::get_context_for_reflect,
Entry::mut_context_for_reflect,
Expand Down Expand Up @@ -2624,7 +2631,7 @@ static file_descriptor_proto_data: &'static [u8] = b"\
\n\x04term\x18\x02\x20\x01(\x04R\x04term\x12\x14\n\x05index\x18\x03\x20\
\x01(\x04R\x05index\x12\x12\n\x04data\x18\x04\x20\x01(\x0cR\x04data\x12\
\x19\n\x08sync_log\x18\x05\x20\x01(\x08R\x07syncLog\x12\x18\n\x07context\
\x18\x06\x20\x01(\x04R\x07context\"o\n\x10SnapshotMetadata\x121\n\nconf_\
\x18\x06\x20\x01(\x0cR\x07context\"o\n\x10SnapshotMetadata\x121\n\nconf_\
state\x18\x01\x20\x01(\x0b2\x12.eraftpb.ConfStateR\tconfState\x12\x14\n\
\x05index\x18\x02\x20\x01(\x04R\x05index\x12\x12\n\x04term\x18\x03\x20\
\x01(\x04R\x04term\"U\n\x08Snapshot\x12\x12\n\x04data\x18\x01\x20\x01(\
Expand Down Expand Up @@ -2684,9 +2691,9 @@ static file_descriptor_proto_data: &'static [u8] = b"\
\x04\x04\x12\x04\r\x04\x0c\x13\n\x0c\n\x05\x04\0\x02\x04\x05\x12\x03\r\
\x04\x08\n\x0c\n\x05\x04\0\x02\x04\x01\x12\x03\r\t\x11\n\x0c\n\x05\x04\0\
\x02\x04\x03\x12\x03\r\x14\x15\n\x0b\n\x04\x04\0\x02\x05\x12\x03\x0e\x04\
\x17\n\r\n\x05\x04\0\x02\x05\x04\x12\x04\x0e\x04\r\x16\n\x0c\n\x05\x04\0\
\x02\x05\x05\x12\x03\x0e\x04\n\n\x0c\n\x05\x04\0\x02\x05\x01\x12\x03\x0e\
\x0b\x12\n\x0c\n\x05\x04\0\x02\x05\x03\x12\x03\x0e\x15\x16\n\n\n\x02\x04\
\x16\n\r\n\x05\x04\0\x02\x05\x04\x12\x04\x0e\x04\r\x16\n\x0c\n\x05\x04\0\
\x02\x05\x05\x12\x03\x0e\x04\t\n\x0c\n\x05\x04\0\x02\x05\x01\x12\x03\x0e\
\n\x11\n\x0c\n\x05\x04\0\x02\x05\x03\x12\x03\x0e\x14\x15\n\n\n\x02\x04\
\x01\x12\x04\x11\0\x15\x01\n\n\n\x03\x04\x01\x01\x12\x03\x11\x08\x18\n\
\x0b\n\x04\x04\x01\x02\0\x12\x03\x12\x04\x1d\n\r\n\x05\x04\x01\x02\0\x04\
\x12\x04\x12\x04\x11\x1a\n\x0c\n\x05\x04\x01\x02\0\x06\x12\x03\x12\x04\r\
Expand Down
19 changes: 2 additions & 17 deletions src/raw_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,41 +256,26 @@ impl<T: Storage> RawNode<T> {
}

// Propose proposes data be appended to the raft log.
pub fn propose(&mut self, data: Vec<u8>, sync_log: bool) -> Result<()> {
self.propose_with_context(data, sync_log, 0)
}

// Propose proposes data be appended to the raft log with context.
pub fn propose_with_context(&mut self, data: Vec<u8>, sync_log: bool, context: u64) -> Result<()> {
pub fn propose(&mut self, data: Vec<u8>, context: Vec<u8>) -> Result<()> {
let mut m = Message::new();
m.set_msg_type(MessageType::MsgPropose);
m.set_from(self.raft.id);
let mut e = Entry::new();
e.set_data(data);
if sync_log {
e.set_sync_log(true);
}
e.set_context(context);
m.set_entries(RepeatedField::from_vec(vec![e]));
self.raft.step(m)
}

// ProposeConfChange proposes a config change.
#[allow(needless_pass_by_value)]
pub fn propose_conf_change(&mut self, cc: ConfChange) -> Result<()> {
self.propose_conf_change_with_context(cc, 0)
}

// ProposeConfChange proposes a config change with context.
#[allow(needless_pass_by_value)]
pub fn propose_conf_change_with_context(&mut self, cc: ConfChange, context: u64) -> Result<()> {
pub fn propose_conf_change(&mut self, cc: ConfChange, context: Vec<u8>) -> Result<()> {
let data = protobuf::Message::write_to_bytes(&cc)?;
let mut m = Message::new();
m.set_msg_type(MessageType::MsgPropose);
let mut e = Entry::new();
e.set_entry_type(EntryType::EntryConfChange);
e.set_data(data);
e.set_sync_log(true);
e.set_context(context);
m.set_entries(RepeatedField::from_vec(vec![e]));
self.raft.step(m)
Expand Down

0 comments on commit daf9fb9

Please sign in to comment.