From 18b1c756b57733a358046153955b7a0ed0ade72d Mon Sep 17 00:00:00 2001 From: Srijan Verma Date: Sat, 18 Apr 2026 11:39:39 +0530 Subject: [PATCH 1/6] feat: addded typeObject and TypeEncoding in the creation of New Object --- core/store.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/core/store.go b/core/store.go index d3d1e8c..ab9e81e 100644 --- a/core/store.go +++ b/core/store.go @@ -18,7 +18,9 @@ func init(){ store=make(map[string]*Obj) } -func NewObj(value interface{},DurationMs int64) *Obj{ +//eralier we used to take (value interface{},DurationMs int64) +//but now we are also stroing type encoding so, 1 for object type and 1 for encdojgn +func NewObj(value interface{},DurationMs int64,oType uint8,oEnc uint8) *Obj{ //creating a new object, setting things up and returning another object //since we want to store abolution expires instead of doing it multiple time that's why we have created this fucntion var expiresAt int64=-1 @@ -28,6 +30,7 @@ func NewObj(value interface{},DurationMs int64) *Obj{ return &Obj{ Value: value, + TypeEncoding: oType|enc, ExpiresAt: expiresAt, } } From e320dbb6dd395a51c511e556d1963d160f51e6e3 Mon Sep 17 00:00:00 2001 From: Srijan Verma Date: Sat, 18 Apr 2026 11:40:16 +0530 Subject: [PATCH 2/6] fix: fixed minor bugs about encoding type --- core/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/store.go b/core/store.go index ab9e81e..4170c03 100644 --- a/core/store.go +++ b/core/store.go @@ -30,7 +30,7 @@ func NewObj(value interface{},DurationMs int64,oType uint8,oEnc uint8) *Obj{ return &Obj{ Value: value, - TypeEncoding: oType|enc, + TypeEncoding: oType|oEnc, ExpiresAt: expiresAt, } } From 3a96b5cee5b7e770d593c04385ecfa9e2cbcc4ab Mon Sep 17 00:00:00 2001 From: Srijan Verma Date: Sat, 18 Apr 2026 11:41:03 +0530 Subject: [PATCH 3/6] feat: Implemented dedueTypeEncoding to get oType and oEncoding --- core/type_string.go | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 core/type_string.go diff --git a/core/type_string.go b/core/type_string.go new file mode 100644 index 0000000..3a907cc --- /dev/null +++ b/core/type_string.go @@ -0,0 +1,18 @@ +package core + +import "strconv" + + +//Similar to +//tryObjectEncoding Function in redis +func deduceTypeEncoding(v string)(uint8,uint8){ + oType:=OBJ_TYPE_STRING + if _,rr:=strconv.ParseInt(v,10,64);err==nil{ + return oType,OBJ_ENCODING_INT //we are converting ans stroing int as a string inside my redis object + } + if len(v)<=44{ + return oType,OBJ_ENCODING_EMBSTR //Object encoding is embedded string + } + return oType,OBJ_ENCODING_RAW +} + From 99b515fcce6f2cc8239b2feae2d0a88a2743e31e Mon Sep 17 00:00:00 2001 From: Srijan Verma Date: Sat, 18 Apr 2026 11:41:43 +0530 Subject: [PATCH 4/6] feat: Implemented typeencoding.go --- core/typeencoding.go | 63 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 core/typeencoding.go diff --git a/core/typeencoding.go b/core/typeencoding.go new file mode 100644 index 0000000..c1eea64 --- /dev/null +++ b/core/typeencoding.go @@ -0,0 +1,63 @@ +package core +import "errors" + +/* +From a particular type encoding object if you want to get type of it +type is definately the first 4 bits + +so inorder to extract that i am right shifting by 4 and then left shift by 4 +*/ +func getType(te uint8) uint8{ + return (te>>4)<<4 +} + +func getEncoding(te uint8) uint8{ + return te & 0b00001111 +} + +func assertType(te uint8,t uint8)error{ + if getType(te)!=t{ + return errors.New("the operation is not permitted on this type") + } + return nil +} + + +func assertEncoding(te uint8,e uint8)error{ + if getEncoding(te)!=e{ + return errors.New("the operations is not permitted on this encoding") + } + return nil +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + From 742e8335dc5ece3cfd02a455e967168272c9fcc3 Mon Sep 17 00:00:00 2001 From: Srijan Verma Date: Sat, 18 Apr 2026 11:42:24 +0530 Subject: [PATCH 5/6] feat: Implement Redis Objec --- core/object.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 core/object.go diff --git a/core/object.go b/core/object.go new file mode 100644 index 0000000..fb83dfd --- /dev/null +++ b/core/object.go @@ -0,0 +1,26 @@ +package core + + +//TODO: Change ExpiresAt it to LRU Bits as handled by Redis +type Obj struct{ + TypeEncoding uint8 + Value interface{} + ExpiresAt int64 +} +/* +C and c++ give us ways to apply bit fields on set, means in each set i can assign a fix number of bits +so i am deifning type encoding + + +if i am storing type encoding in c/c++, i could do it via bit field s since i don't have assembler here so +we are doing like this way, or or krke krrhe hai +*/ + +var OBJ_TYPE_STRING uint8=0<<4 //because first 4 bits i want to set + +var OBJ_ENCODING_RAW uint8=0 +var OBJ_ENCODING_INT uint8=1 +var OBJ_ENCODING_ENDSTR uint8=8 + + + From 217f7add61109601fe66f66aae48b73f53466cf9 Mon Sep 17 00:00:00 2001 From: Srijan Verma Date: Sat, 18 Apr 2026 11:43:18 +0530 Subject: [PATCH 6/6] feat: added case and evalINCR --- core/eval.go | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/core/eval.go b/core/eval.go index e57da2e..d07d166 100644 --- a/core/eval.go +++ b/core/eval.go @@ -38,6 +38,37 @@ func evalPING(args []string) []byte{ return b } + +func evalINCR(args []string)[]byte{ + if len(args)!=1{ + return Encode(errors.New("ERR wrong number of arguments for 'incr' command"),false) + } + + var key string=args[0] //first wala argument lenge hum isme + obj:=Get(key) + //basically we will get an object and if that obejct doesn;t exits then we will create a new object + if obj==nil{ + obj=NewObj("0",-1,OBJ_TYPE_STRING,OBJ_ENCODING_INT) + Put(key,obj) + } + + if err:=assertType(obj.TypeEncoding,OBJ_TYPE_STRING);err!=nil{ + return Encode(err,false) + } + + if err:=assertEncoding(obj.TypeEncoding,OBJ_ENCODING_INT);err!=nil{ + return Encode(err,false) + } + //which means value is indeed an integer + i,_:=strconv.ParseInt(obj.Value,(string),10,64) + i++ + obj.Value=strconv.FormatInt(i,10) + + return Encode(i,false) +} + + + func evalSET(args []string) []byte{ //similarly for evalSET if len(args) <= 1{ @@ -49,6 +80,7 @@ func evalSET(args []string) []byte{ var exDurationMs int64 = -1//as we know ki default value of expiration is -1 key, value = args[0], args[1] + otype,oEnc:=deduceTypeEncoding(value) for i := 2; i < len(args); i++{ //as we are only implementing expiration as of now par SET functions implements a lot of other options too @@ -75,7 +107,7 @@ func evalSET(args []string) []byte{ //after this we have key,value set and it is ptional for expiration //so we will create a new Object - Put(key, NewObj(value, exDurationMs)) + Put(key, NewObj(value, exDurationMs, otype, oEnc)) // c.Write([]byte("+OK\r\n")) // return nil return RESP_OK @@ -226,6 +258,8 @@ func EvalAndRespond(cmds []*RedisCmd, c io.ReadWriter) error{ buf.Write(evalEXPIRE(cmd.Args)) case "BGREWRITEAOF": buf.Write(evalBGREWRITEAOF(cmd.Args)) + case "INCR": + buf.Write(evalPING(cmd.Args)) default: buf.Write(evalPING(cmd.Args)) }