-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing object encodings and implementing INCR
- Loading branch information
1 parent
249cb3a
commit 9acc214
Showing
5 changed files
with
98 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package core | ||
|
||
// TODO: change ExpiresAt it to LRU Bits as handled by Redis | ||
type Obj struct { | ||
TypeEncoding uint8 | ||
Value interface{} | ||
ExpiresAt int64 | ||
} | ||
|
||
var OBJ_TYPE_STRING uint8 = 0 << 4 | ||
|
||
var OBJ_ENCODING_RAW uint8 = 0 | ||
var OBJ_ENCODING_INT uint8 = 1 | ||
var OBJ_ENCODING_EMBSTR uint8 = 8 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package core | ||
|
||
import "strconv" | ||
|
||
// Similar to | ||
// tryObjectEncoding function in Redis | ||
func deduceTypeEncoding(v string) (uint8, uint8) { | ||
oType := OBJ_TYPE_STRING | ||
if _, err := strconv.ParseInt(v, 10, 64); err == nil { | ||
return oType, OBJ_ENCODING_INT | ||
} | ||
if len(v) <= 44 { | ||
return oType, OBJ_ENCODING_EMBSTR | ||
} | ||
return oType, OBJ_ENCODING_RAW | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package core | ||
|
||
import "errors" | ||
|
||
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 operation is not permitted on this encoding") | ||
} | ||
return nil | ||
} |