@@ -37,10 +37,9 @@ var argsLen = map[string]int{
3737
3838type RedisServer struct {
3939 listen net.Listener
40- store store.ScanStore
40+ store store.MVCCStore
4141 coordinator kv.Coordinator
4242 redisTranscoder * redisTranscoder
43- listStore * store.ListStore
4443 // TODO manage membership from raft log
4544 leaderRedis map [raft.ServerAddress ]string
4645
@@ -72,15 +71,12 @@ type redisResult struct {
7271 err error
7372}
7473
75- func store2list (st store.ScanStore ) * store.ListStore { return store .NewListStore (st ) }
76-
77- func NewRedisServer (listen net.Listener , store store.ScanStore , coordinate * kv.Coordinate , leaderRedis map [raft.ServerAddress ]string ) * RedisServer {
74+ func NewRedisServer (listen net.Listener , store store.MVCCStore , coordinate * kv.Coordinate , leaderRedis map [raft.ServerAddress ]string ) * RedisServer {
7875 r := & RedisServer {
7976 listen : listen ,
8077 store : store ,
8178 coordinator : coordinate ,
8279 redisTranscoder : newRedisTranscoder (),
83- listStore : store2list (store ),
8480 leaderRedis : leaderRedis ,
8581 }
8682
@@ -112,6 +108,10 @@ func getConnState(conn redcon.Conn) *connState {
112108 return st
113109}
114110
111+ func (r * RedisServer ) readTS () uint64 {
112+ return snapshotTS (r .coordinator .Clock ())
113+ }
114+
115115func (r * RedisServer ) Run () error {
116116 err := redcon .Serve (r .listen ,
117117 func (conn redcon.Conn , cmd redcon.Command ) {
@@ -211,7 +211,8 @@ func (r *RedisServer) get(conn redcon.Conn, cmd redcon.Command) {
211211 }
212212
213213 if r .coordinator .IsLeader () {
214- v , err := r .store .Get (context .Background (), cmd .Args [1 ])
214+ readTS := r .readTS ()
215+ v , err := r .store .GetAt (context .Background (), cmd .Args [1 ], readTS )
215216 if err != nil {
216217 switch {
217218 case errors .Is (err , store .ErrKeyNotFound ):
@@ -276,7 +277,8 @@ func (r *RedisServer) exists(conn redcon.Conn, cmd redcon.Command) {
276277 return
277278 }
278279
279- ok , err := r .store .Exists (context .Background (), cmd .Args [1 ])
280+ readTS := r .readTS ()
281+ ok , err := r .store .ExistsAt (context .Background (), cmd .Args [1 ], readTS )
280282 if err != nil {
281283 conn .WriteError (err .Error ())
282284 return
@@ -325,7 +327,8 @@ func (r *RedisServer) localKeys(pattern []byte) ([][]byte, error) {
325327}
326328
327329func (r * RedisServer ) localKeysExact (pattern []byte ) ([][]byte , error ) {
328- res , err := r .store .Exists (context .Background (), pattern )
330+ readTS := r .readTS ()
331+ res , err := r .store .ExistsAt (context .Background (), pattern , readTS )
329332 if err != nil {
330333 return nil , errors .WithStack (err )
331334 }
@@ -338,7 +341,8 @@ func (r *RedisServer) localKeysExact(pattern []byte) ([][]byte, error) {
338341func (r * RedisServer ) localKeysPattern (pattern []byte ) ([][]byte , error ) {
339342 start := r .patternStart (pattern )
340343
341- keys , err := r .store .Scan (context .Background (), start , nil , math .MaxInt )
344+ readTS := r .readTS ()
345+ keys , err := r .store .ScanAt (context .Background (), start , nil , math .MaxInt , readTS )
342346 if err != nil {
343347 return nil , errors .WithStack (err )
344348 }
@@ -828,13 +832,24 @@ func clampRange(start, end, length int) (int, int) {
828832}
829833
830834func (r * RedisServer ) loadListMeta (ctx context.Context , key []byte ) (store.ListMeta , bool , error ) {
831- meta , exists , err := r .listStore .LoadMeta (ctx , key )
832- return meta , exists , errors .WithStack (err )
835+ readTS := r .readTS ()
836+ val , err := r .store .GetAt (ctx , store .ListMetaKey (key ), readTS )
837+ if err != nil {
838+ if errors .Is (err , store .ErrKeyNotFound ) {
839+ return store.ListMeta {}, false , nil
840+ }
841+ return store.ListMeta {}, false , errors .WithStack (err )
842+ }
843+ meta , err := store .UnmarshalListMeta (val )
844+ if err != nil {
845+ return store.ListMeta {}, false , errors .WithStack (err )
846+ }
847+ return meta , true , nil
833848}
834849
835850func (r * RedisServer ) isListKey (ctx context.Context , key []byte ) (bool , error ) {
836- isList , err := r .listStore . IsList (ctx , key )
837- return isList , errors . WithStack ( err )
851+ _ , exists , err := r .loadListMeta (ctx , key )
852+ return exists , err
838853}
839854
840855func (r * RedisServer ) buildRPushOps (meta store.ListMeta , key []byte , values [][]byte ) ([]* kv.Elem [kv.OP ], store.ListMeta , error ) {
@@ -895,7 +910,8 @@ func (r *RedisServer) deleteList(ctx context.Context, key []byte) error {
895910 start := listItemKey (key , math .MinInt64 )
896911 end := listItemKey (key , math .MaxInt64 )
897912
898- kvs , err := r .store .Scan (ctx , start , end , math .MaxInt )
913+ readTS := r .readTS ()
914+ kvs , err := r .store .ScanAt (ctx , start , end , math .MaxInt , readTS )
899915 if err != nil {
900916 return errors .WithStack (err )
901917 }
@@ -926,7 +942,8 @@ func (r *RedisServer) fetchListRange(ctx context.Context, key []byte, meta store
926942 startKey := listItemKey (key , startSeq )
927943 endKey := listItemKey (key , endSeq + 1 ) // exclusive
928944
929- kvs , err := r .store .Scan (ctx , startKey , endKey , int (endIdx - startIdx + 1 ))
945+ readTS := r .readTS ()
946+ kvs , err := r .store .ScanAt (ctx , startKey , endKey , int (endIdx - startIdx + 1 ), readTS )
930947 if err != nil {
931948 return nil , errors .WithStack (err )
932949 }
@@ -1039,7 +1056,8 @@ func (r *RedisServer) tryLeaderGet(key []byte) ([]byte, error) {
10391056 defer conn .Close ()
10401057
10411058 cli := pb .NewRawKVClient (conn )
1042- resp , err := cli .RawGet (context .Background (), & pb.RawGetRequest {Key : key })
1059+ ts := r .readTS ()
1060+ resp , err := cli .RawGet (context .Background (), & pb.RawGetRequest {Key : key , Ts : ts })
10431061 if err != nil {
10441062 return nil , errors .WithStack (err )
10451063 }
@@ -1048,8 +1066,9 @@ func (r *RedisServer) tryLeaderGet(key []byte) ([]byte, error) {
10481066}
10491067
10501068func (r * RedisServer ) getValue (key []byte ) ([]byte , error ) {
1069+ readTS := r .readTS ()
10511070 if r .coordinator .IsLeader () {
1052- v , err := r .store .Get (context .Background (), key )
1071+ v , err := r .store .GetAt (context .Background (), key , readTS )
10531072 return v , errors .WithStack (err )
10541073 }
10551074 return r .tryLeaderGet (key )
0 commit comments