Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion core/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 63 in core/eval.go

View workflow job for this annotation

GitHub Actions / build

(string) (type) is not an expression
Copy link

Copilot AI Apr 18, 2026

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.Value is interface{} so you need a type assertion/conversion to string before parsing (and handle the parse error, since stored values can still be non-numeric).

Suggested change
i,_:=strconv.ParseInt(obj.Value,(string),10,64)
value,ok:=obj.Value.(string)
if !ok{
return Encode(errors.New("ERR value is not an integer or out of range"),false)
}
i,err:=strconv.ParseInt(value,10,64)
if err!=nil{
return Encode(errors.New("ERR value is not an integer or out of range"),false)
}

Copilot uses AI. Check for mistakes.
i++
obj.Value=strconv.FormatInt(i,10)

return Encode(i,false)
}



func evalSET(args []string) []byte{
//similarly for evalSET
if len(args) <= 1{
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -226,6 +258,8 @@
buf.Write(evalEXPIRE(cmd.Args))
case "BGREWRITEAOF":
buf.Write(evalBGREWRITEAOF(cmd.Args))
case "INCR":
buf.Write(evalPING(cmd.Args))
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The INCR command dispatch is wired to evalPING here, so INCR will behave like PING. This should call evalINCR(cmd.Args) instead.

Suggested change
buf.Write(evalPING(cmd.Args))
buf.Write(evalINCR(cmd.Args))

Copilot uses AI. Check for mistakes.
default:
buf.Write(evalPING(cmd.Args))
}
Expand Down
26 changes: 26 additions & 0 deletions core/object.go
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{

Check failure on line 5 in core/object.go

View workflow job for this annotation

GitHub Actions / build

other declaration of Obj
TypeEncoding uint8
Comment on lines +4 to +6
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR title mentions "Object statistics lru", but this change introduces type/encoding bits and an INCR command; the only LRU-related change is a TODO. Consider updating the PR title (or adding the intended LRU/statistics implementation) so the PR intent matches the code.

Copilot uses AI. Check for mistakes.
Value interface{}
ExpiresAt int64
}
Comment on lines +5 to +9
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obj is defined here, but core/store.go also defines an Obj type in the same core package. Having two type Obj struct { ... } declarations in one package will not compile; consolidate into a single Obj definition (and update references accordingly).

Suggested change
type Obj struct{
TypeEncoding uint8
Value interface{}
ExpiresAt int64
}
// Obj is defined in core/store.go for the core package.

Copilot uses AI. Check for mistakes.
/*
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
Copy link

Copilot AI Apr 18, 2026

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.

Suggested change
var OBJ_ENCODING_ENDSTR uint8=8
var OBJ_ENCODING_EMBSTR uint8=8

Copilot uses AI. Check for mistakes.



5 changes: 4 additions & 1 deletion core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{

Check failure on line 12 in core/store.go

View workflow job for this annotation

GitHub Actions / build

Obj redeclared in this block
Value interface{} //which means we acn put literally anything,anyvalue and which would work fine
ExpiresAt int64 //is time ke baad expire hojayega wo
}
Expand All @@ -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
Expand All @@ -28,6 +30,7 @@

return &Obj{
Value: value,
TypeEncoding: oType|oEnc,
ExpiresAt: expiresAt,
}
Comment on lines 23 to 35
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NewObj sets TypeEncoding, but Obj as defined in this file does not have a TypeEncoding field, so this won’t compile. Either add TypeEncoding uint8 to the Obj struct here, or (preferably) remove this duplicate Obj definition and use a single Obj type shared with core/object.go.

Copilot uses AI. Check for mistakes.
}
Expand Down
18 changes: 18 additions & 0 deletions core/type_string.go
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

View workflow job for this annotation

GitHub Actions / build

undefined: err

Check failure on line 10 in core/type_string.go

View workflow job for this annotation

GitHub Actions / build

rr declared and not used
Copy link

Copilot AI Apr 18, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This ParseInt check won’t compile: it declares rr but then checks err (which is undefined), and rr is unused. Use the standard if _, err := strconv.ParseInt(...); err == nil { ... } pattern.

Suggested change
if _,rr:=strconv.ParseInt(v,10,64);err==nil{
if _, err := strconv.ParseInt(v, 10, 64); err == nil {

Copilot uses AI. Check for mistakes.
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

Check failure on line 14 in core/type_string.go

View workflow job for this annotation

GitHub Actions / build

undefined: OBJ_ENCODING_EMBSTR
Comment on lines +10 to +14
Copy link

Copilot AI Apr 18, 2026

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.

Suggested change
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

Copilot uses AI. Check for mistakes.
}
return oType,OBJ_ENCODING_RAW
}

63 changes: 63 additions & 0 deletions core/typeencoding.go
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
}
































Loading