-
Notifications
You must be signed in to change notification settings - Fork 0
Object statistics lru #10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
18b1c75
e320dbb
3a96b5c
99b515f
742e833
217f7ad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -38,6 +38,37 @@ | |||||
| 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 @@ | |||||
| 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 @@ | |||||
|
|
||||||
| //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 @@ | |||||
| buf.Write(evalEXPIRE(cmd.Args)) | ||||||
| case "BGREWRITEAOF": | ||||||
| buf.Write(evalBGREWRITEAOF(cmd.Args)) | ||||||
| case "INCR": | ||||||
| buf.Write(evalPING(cmd.Args)) | ||||||
|
||||||
| buf.Write(evalPING(cmd.Args)) | |
| buf.Write(evalINCR(cmd.Args)) |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,26 @@ | ||||||||||||||
| package core | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| //TODO: Change ExpiresAt it to LRU Bits as handled by Redis | ||||||||||||||
| type Obj struct{ | ||||||||||||||
| TypeEncoding uint8 | ||||||||||||||
|
Comment on lines
+4
to
+6
|
||||||||||||||
| Value interface{} | ||||||||||||||
| ExpiresAt int64 | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+5
to
+9
|
||||||||||||||
| type Obj struct{ | |
| TypeEncoding uint8 | |
| Value interface{} | |
| ExpiresAt int64 | |
| } | |
| // Obj is defined in core/store.go for the core package. |
Copilot
AI
Apr 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OBJ_ENCODING_ENDSTR looks like a typo and doesn’t match usage elsewhere (OBJ_ENCODING_EMBSTR in deduceTypeEncoding). As written, deduceTypeEncoding won’t compile; rename/align the constant (and its value) so the encoding name is consistent across the package.
| var OBJ_ENCODING_ENDSTR uint8=8 | |
| var OBJ_ENCODING_EMBSTR uint8=8 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,7 @@ | |
| var store map[string]*Obj | ||
| //the best datastrcuture to hold key value is hash table so we are using it | ||
|
|
||
| type Obj struct{ | ||
| Value interface{} //which means we acn put literally anything,anyvalue and which would work fine | ||
| ExpiresAt int64 //is time ke baad expire hojayega wo | ||
| } | ||
|
|
@@ -18,7 +18,9 @@ | |
| 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 @@ | |
|
|
||
| return &Obj{ | ||
| Value: value, | ||
| TypeEncoding: oType|oEnc, | ||
| ExpiresAt: expiresAt, | ||
| } | ||
|
Comment on lines
23
to
35
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -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{ | ||||||||||||||||||||||
|
Check failure on line 10 in core/type_string.go
|
||||||||||||||||||||||
|
||||||||||||||||||||||
| if _,rr:=strconv.ParseInt(v,10,64);err==nil{ | |
| if _, err := strconv.ParseInt(v, 10, 64); err == nil { |
Copilot
AI
Apr 18, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OBJ_ENCODING_EMBSTR is referenced here but no such identifier exists in the package (the constants in core/object.go define OBJ_ENCODING_ENDSTR instead). Define OBJ_ENCODING_EMBSTR or rename usages/constants so they match.
| 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 | |
| if _,err:=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_ENDSTR //Object encoding is embedded string |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
| } | ||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strconv.ParseInt(obj.Value,(string),10,64)is invalid Go and won’t compile;obj.Valueisinterface{}so you need a type assertion/conversion tostringbefore parsing (and handle the parse error, since stored values can still be non-numeric).