-
Notifications
You must be signed in to change notification settings - Fork 0
Object statistics lru #11
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
ca4a02d
4ca2614
ca9034b
f3b6d91
6f55d55
d7cdaa9
461c28c
a0f4405
d5e620d
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package config | ||
|
|
||
| var Host string="0.0.0.0" | ||
| var Port int=7379 | ||
|
|
||
| var KeysLimit int=100 //like ye hum threshold set krdiye hai like our database still support atmax this many keys | ||
|
|
||
| //will evict EvictionRation of keys whenever evictionruns | ||
| //it would dictate whenever my eviction is triggered, how many keys i will be evicting. | ||
| var EvictionRatio flaot64=0.40 //Generally aise real scenario me kabhi itna to nahi hi krte hai but still | ||
|
|
||
| var EvictionStrategy string="allkeys-random" | ||
| var AOFFile string="./velox.aof" | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -140,7 +140,10 @@ func evalGET(args []string)[]byte{ | |||||||||
| return RESP_NIL | ||||||||||
| } | ||||||||||
|
|
||||||||||
|
|
||||||||||
| //if key already expired then return nil | ||||||||||
| if hasExpired(obj){ | ||||||||||
| return RESP_NIL | ||||||||||
| } | ||||||||||
| //return te RESP encoded value | ||||||||||
| // c.Write(Encode(obj.Value,false)) | ||||||||||
| // return nil | ||||||||||
|
|
@@ -184,6 +187,20 @@ func evalTTL(args []string) []byte{ | |||||||||
| return RESP_MINUS_2 | ||||||||||
| } | ||||||||||
|
|
||||||||||
| exp.isExpirySet:=getExpiry(obj) | ||||||||||
| if !isExpirySet{ | ||||||||||
| return RESP_MINUS_1 | ||||||||||
| } | ||||||||||
|
|
||||||||||
| //if key expired i.e key does not exist hence return -2 | ||||||||||
| if uint64(time.Now().UnixMilli())>exp{ | ||||||||||
| return RESP_MINUS_2 | ||||||||||
| } | ||||||||||
|
|
||||||||||
| //compute the time remaining for the key to expire and | ||||||||||
| //return the RESP encoded form of it | ||||||||||
| durationMS:=exp-uint64(time.Now().UnixMilli()) | ||||||||||
|
|
||||||||||
| // c.Write(Encode(int64(durationMS/1000),false)) | ||||||||||
| // return nil | ||||||||||
| return Encode(int64(durationMs/1000), false) | ||||||||||
|
Comment on lines
+190
to
206
|
||||||||||
|
|
@@ -220,7 +237,9 @@ func evalEXPIRE(args []string) []byte{ | |||||||||
| return RESP_ZERO | ||||||||||
| } | ||||||||||
|
|
||||||||||
| obj.ExpiresAt = time.Now().UnixMilli() + exDurationSec*1000 | ||||||||||
| // obj.ExpiresAt = time.Now().UnixMilli() + exDurationSec*1000 | ||||||||||
|
|
||||||||||
| setExpiry(obj,exDurationSec*1000) | ||||||||||
|
|
||||||||||
| //1 print krenge if the timeout was set | ||||||||||
| // c.Write([]byte(":1\r\n")) | ||||||||||
|
|
@@ -234,6 +253,14 @@ func evalBGREWRITEAOF(args []string) []byte{ | |||||||||
| return RESP_OK | ||||||||||
| } | ||||||||||
|
|
||||||||||
| func evalINFO(args []string)[]byte{ | ||||||||||
| var info []byte | ||||||||||
| buf:=bytes.NewBuffer(info) | ||||||||||
| for i := range KeyspaceStat{ | ||||||||||
| buf.WriteString(fmt.Sprintf("db%d:keys=%d,expire=0,avg_ttl=0\r\n",i,KeyspaceStat[i]["keys"])) | ||||||||||
| } | ||||||||||
|
Comment on lines
+258
to
+261
|
||||||||||
| } | ||||||||||
|
Comment on lines
+256
to
+262
|
||||||||||
|
|
||||||||||
| // func EvalAndRespond(cmd *Rediscmd,c net.Conn)error{ | ||||||||||
| func EvalAndRespond(cmds []*RedisCmd, c io.ReadWriter) error{ | ||||||||||
| //It's job is like depending on what job is sent to us | ||||||||||
|
|
@@ -260,6 +287,12 @@ func EvalAndRespond(cmds []*RedisCmd, c io.ReadWriter) error{ | |||||||||
| buf.Write(evalBGREWRITEAOF(cmd.Args)) | ||||||||||
| case "INCR": | ||||||||||
| buf.Write(evalPING(cmd.Args)) | ||||||||||
| case "INFO": | ||||||||||
| buf.Write(evalINFO(cmd.Args)) | ||||||||||
| case "CLIENT": | ||||||||||
| buf.Write(evalCLIENT(cmd.Args)) | ||||||||||
| case "LATENCY": | ||||||||||
| buf.Write(evalLATENCY(cmd.Args)) | ||||||||||
|
Comment on lines
+292
to
+295
|
||||||||||
| case "CLIENT": | |
| buf.Write(evalCLIENT(cmd.Args)) | |
| case "LATENCY": | |
| buf.Write(evalLATENCY(cmd.Args)) |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -7,6 +7,8 @@ whenevr a cache is full, we will be evicting the first key which we do find | |||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| package core | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| import "github.com/sharpsalt/Velox-In-Memory-Database/config" | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| //Evcits the first key it found while iterating the map | ||||||||||||||||||||||||||||||||
| //TODP: Make it efficient by doing thrugh somehting | ||||||||||||||||||||||||||||||||
| func evictFirst(){ | ||||||||||||||||||||||||||||||||
|
|
@@ -16,9 +18,81 @@ func evictFirst(){ | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||
| The approximated LRU algorithm | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| func getCurrentClock() uint32{ | ||||||||||||||||||||||||||||||||
| return uint32(time.Now().Unix())&0x00FFFFF // it would give us 24 bit clock resolution of the time | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+24
to
+26
|
||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| func getIdleTime(LastAccessdAt uint32) uint32{ | ||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||
| it gives me current clock and if it is greater than lastaccessedat then for anytimestamp then diff, other (max-last)*c | ||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||
| c:=getCurrentClock() | ||||||||||||||||||||||||||||||||
| if c>=LastAccessdAt{ | ||||||||||||||||||||||||||||||||
| return c-LastAccessdAt | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| return (0x00FFFFF-LastAccessdAt)*c | ||||||||||||||||||||||||||||||||
|
Comment on lines
+30
to
+36
|
||||||||||||||||||||||||||||||||
| it gives me current clock and if it is greater than lastaccessedat then for anytimestamp then diff, other (max-last)*c | |
| */ | |
| c:=getCurrentClock() | |
| if c>=LastAccessdAt{ | |
| return c-LastAccessdAt | |
| } | |
| return (0x00FFFFF-LastAccessdAt)*c | |
| it gives me current clock and if it is greater than lastaccessedat then for anytimestamp then diff, | |
| otherwise the elapsed time is the remaining ticks until wrap plus the current tick value | |
| */ | |
| c:=getCurrentClock() | |
| if c>=LastAccessdAt{ | |
| return c-LastAccessdAt | |
| } | |
| return (0x00FFFFF-LastAccessdAt) + c + 1 |
Copilot
AI
Apr 23, 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.
populateEvictionPool references store[k].lastaccessedat, but the field on Obj is LastAccessedAt (exported) and PoolItem uses a different name as well. This mismatch will not compile; align field names and casing across Obj, PoolItem, and call sites.
Copilot
AI
Apr 23, 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.
evictAllKeysLRU has several syntax/type errors (funct, invalid evictCount assignment expression, malformed for loop initializer). As written, the LRU eviction path will not compile; fix the function signature and compute evictCount as EvictionRatio * KeysLimit similar to evictAllKeysRandom.
Copilot
AI
Apr 23, 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.
The evict switch has invalid Go syntax and mismatched function names/strategy strings (evictfirst vs evictFirst, allkets-random, case:). This will not compile and also won’t match the configured strategy values; fix case labels and call the correct functions.
| evictfirst() | |
| case "allkets-random": | |
| evictAllkeysRandom() | |
| case: "allkeys-lru": | |
| evictFirst() | |
| case "allkeys-random": | |
| evictAllKeysRandom() | |
| case "allkeys-lru": |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||||||||||
| package core | ||||||||||||||
|
|
||||||||||||||
| import{ | ||||||||||||||
|
Check failure on line 3 in core/evictionpool.go
|
||||||||||||||
| "sort" | ||||||||||||||
| } | ||||||||||||||
|
Comment on lines
+3
to
+5
|
||||||||||||||
| import{ | |
| "sort" | |
| } | |
| import ( | |
| "sort" | |
| ) |
Copilot
AI
Apr 23, 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.
ByIdleTime doesn't implement sort.Interface because the method must be named Len(), not len(). Without Len, sort.Sort(ByIdleTime(...)) will not compile.
| func (a ByIdleTime) len() int{ | |
| func (a ByIdleTime) Len() int{ |
Copilot
AI
Apr 23, 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.
Push contains multiple identifier/syntax issues that prevent compilation (&PoolItem(key:key,...), ietm vs item, items undefined, etc.). Clean up the composite literal and use a single consistently-named *PoolItem variable when updating keyset and appending to pool.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,19 @@ import ( | |
| "time" | ||
| ) | ||
|
|
||
| func hasExpired(obj *Obj) bool{ | ||
| exp,ok:=expires[obj] | ||
| if !ok{ | ||
| return false | ||
| } | ||
| return exp<=uint64(time.Now().UnixMilli()) | ||
| } | ||
|
|
||
| func getExpiry(obj *Obj)(uint64,bool){ | ||
| exp,ok:=expires[obj] | ||
| return exp,ok | ||
| } | ||
|
Comment on lines
+8
to
+19
|
||
|
|
||
| //Delete all the expired keys- the active way | ||
| //Sampling approach: https://redis.io/commands/expire/ | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,10 @@ | ||||||||||||
| package core | ||||||||||||
|
|
||||||||||||
| var KeyspaceStat [4]map[string]int //just all global object | ||||||||||||
| //eg i support 4 databases within my redis | ||||||||||||
| //in redis you can have 16 databases in redis itself, by deault it goes from db0,db1,db2,...db15 | ||||||||||||
|
|
||||||||||||
| //for which db,which metric,which value | ||||||||||||
| func UpdatDBStat(num int,metric string,value int){ | ||||||||||||
|
||||||||||||
| func UpdatDBStat(num int,metric string,value int){ | |
| func UpdatDBStat(num int,metric string,value int){ | |
| if KeyspaceStat[num] == nil { | |
| KeyspaceStat[num] = make(map[string]int) | |
| } |
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.
This file duplicates configuration variables already declared in
config/config.go(Host,Port,KeysLimit, etc.), which will cause redeclaration compile errors within theconfigpackage. Alsoflaot64is a misspelling offloat64. Prefer updating the existing config file (or removing one) and fix the type name.