Skip to content

Commit

Permalink
kv: 定义KVCtx,方便更新ctx
Browse files Browse the repository at this point in the history
  • Loading branch information
icycrystal4 committed Aug 30, 2023
1 parent 3c05e70 commit 798415d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
11 changes: 11 additions & 0 deletions endpoint/src/kv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,14 @@ fn transmute(ctx: &mut u64) -> &mut Context {
//assert_eq!(std::mem::size_of::<Context>(), 8);
unsafe { std::mem::transmute(ctx) }
}

trait KVCtx {
fn ctx(&mut self) -> &mut Context;
}

impl<T: protocol::Request> KVCtx for T {
#[inline(always)]
fn ctx(&mut self) -> &mut Context {
unsafe { std::mem::transmute(self.context_mut()) }
}
}
7 changes: 3 additions & 4 deletions endpoint/src/kv/topo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use crate::{Endpoint, Topology};

use super::config::MysqlNamespace;
use super::strategy::Strategist;
use super::KVCtx;
#[derive(Clone)]
pub struct KvService<B, E, Req, P> {
// 默认后端分片,一共shards.len()个分片,每个分片 shard[0]是master, shard[1..]是slave
Expand Down Expand Up @@ -104,9 +105,8 @@ where
type Item = Req;

fn send(&self, mut req: Self::Item) {
let runs = super::transmute(req.context_mut()).runs;
// req 是mc binary协议,需要展出字段,转换成sql
let key = if runs == 0 {
let key = if req.ctx().runs == 0 {
req.key()
} else {
req.origin_data().key()
Expand Down Expand Up @@ -135,8 +135,7 @@ where

let shard = unsafe { shards.get_unchecked(shard_idx) };

let ctx = super::transmute(req.context_mut());
if ctx.runs == 0 {
if req.ctx().runs == 0 {
//todo: 此处不应panic
let cmd = MysqlBuilder {}
.build_packets(&self.strategist, &req, &key)
Expand Down

0 comments on commit 798415d

Please sign in to comment.