Skip to content

Commit

Permalink
tracked entities as map
Browse files Browse the repository at this point in the history
  • Loading branch information
latolukasz committed Nov 29, 2023
1 parent e6cc2af commit df8984d
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 25 deletions.
6 changes: 3 additions & 3 deletions bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -1427,9 +1427,9 @@ func convertBindToRedisValue(bind Bind, schema *entitySchema) []any {
return values
}

func convertBindValueToRedisValue(value string) string {
if value == nullAsString || value == zeroAsString || value == zeroTimeAsString || value == zeroDateAsString {
return ""
func convertBindValueToRedisValue(value any) any {
if value == nil || value == "" {
return nullAsString
}
return value
}
9 changes: 8 additions & 1 deletion edit_entity_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package beeorm
import (
"fmt"
"reflect"
"strconv"
)

func EditEntityField[E any](c Context, entity *E, field string, value any, execute bool) error {
Expand All @@ -17,8 +18,9 @@ func EditEntityField[E any](c Context, entity *E, field string, value any, execu
}
if execute {
elem := reflect.ValueOf(entity).Elem()
id := elem.Field(0).Uint()
sql := "UPDATE `" + schema.GetTableName() + "` SET `" + field + "` = ? WHERE ID = ?"
schema.GetDB().Exec(c, sql, bindValue, elem.Field(0).Uint())
schema.GetDB().Exec(c, sql, bindValue, id)
fSetter := schema.fieldSetters[field]
if schema.hasLocalCache {
func() {
Expand All @@ -29,6 +31,11 @@ func EditEntityField[E any](c Context, entity *E, field string, value any, execu
} else {
fSetter(bindValue, elem)
}
if schema.hasRedisCache {
index := int64(schema.columnMapping[field] + 1)
rKey := schema.getCacheKey() + ":" + strconv.FormatUint(id, 10)
schema.redisCache.LSet(c, rKey, index, convertBindValueToRedisValue(bindValue))
}
}
return nil
}
18 changes: 13 additions & 5 deletions edit_entity_field_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,23 @@ type updateEntity struct {
DateNullable *time.Time
}

func TestUpdateExecuteNoCache(t *testing.T) {
testUpdateExecute(t, false, false)
func TestUpdateFieldExecuteNoCache(t *testing.T) {
testUpdateFieldExecute(t, false, false)
}

func TestUpdateExecuteLocalCache(t *testing.T) {
testUpdateExecute(t, true, false)
func TestUpdateFieldExecuteLocalCache(t *testing.T) {
testUpdateFieldExecute(t, true, false)
}

func testUpdateExecute(t *testing.T, local, redis bool) {
func TestUpdateFieldExecuteRedis(t *testing.T) {
testUpdateFieldExecute(t, false, true)
}

func TestUpdateFieldExecuteLocalCacheRedis(t *testing.T) {
testUpdateFieldExecute(t, true, true)
}

func testUpdateFieldExecute(t *testing.T, local, redis bool) {
var entity *updateEntity
var reference *updateEntityReference
c := PrepareTables(t, NewRegistry(), entity, reference)
Expand Down
32 changes: 18 additions & 14 deletions entity_deserialize.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ func deserializeFieldsFromRedis(data []string, fields *tableFields, elem reflect
}

func deserializeUintFromRedis(v string, f reflect.Value) {
if v == "" {
if v == nullAsString {
f.SetUint(0)
} else {
val, _ := strconv.ParseUint(v, 10, 64)
Expand All @@ -232,7 +232,7 @@ func deserializeUintFromRedis(v string, f reflect.Value) {
}

func deserializeReferencesFromRedis(v string, f reflect.Value) {
if v == "" {
if v == nullAsString {
f.SetZero()
} else {
val := reflect.New(f.Type().Elem())
Expand All @@ -244,7 +244,7 @@ func deserializeReferencesFromRedis(v string, f reflect.Value) {
}

func deserializeIntFromRedis(v string, f reflect.Value) {
if v == "" {
if v == nullAsString {
f.SetInt(0)
} else {
val, _ := strconv.ParseInt(v, 10, 64)
Expand All @@ -257,7 +257,7 @@ func deserializeBoolFromRedis(v string, f reflect.Value) {
}

func deserializeFloatFromRedis(v string, f reflect.Value) {
if v == "" {
if v == nullAsString {
f.SetFloat(0)
} else {
val, _ := strconv.ParseFloat(v, 64)
Expand All @@ -266,7 +266,7 @@ func deserializeFloatFromRedis(v string, f reflect.Value) {
}

func deserializeTimeFromRedis(v string, f reflect.Value) {
if v != "" {
if v != nullAsString {
t, _ := time.ParseInLocation(time.DateTime, v, time.UTC)
f.Set(reflect.ValueOf(t))
} else {
Expand All @@ -275,7 +275,7 @@ func deserializeTimeFromRedis(v string, f reflect.Value) {
}

func deserializeDateFromRedis(v string, f reflect.Value) {
if v != "" {
if v != nullAsString {
t, _ := time.ParseInLocation(time.DateOnly, v, time.UTC)
f.Set(reflect.ValueOf(t))
} else {
Expand All @@ -284,11 +284,15 @@ func deserializeDateFromRedis(v string, f reflect.Value) {
}

func deserializeStringFromRedis(v string, f reflect.Value) {
if v == nullAsString {
f.SetString("")
return
}
f.SetString(v)
}

func deserializeUIntegersPointersFromRedis(v string, f reflect.Value, size int) {
if v != "" {
if v != nullAsString {
asInt, _ := strconv.ParseUint(v, 10, 64)
switch size {
case 0:
Expand All @@ -312,7 +316,7 @@ func deserializeUIntegersPointersFromRedis(v string, f reflect.Value, size int)
}

func deserializeIntegersPointersFromRedis(v string, f reflect.Value, size int) {
if v != "" {
if v != nullAsString {
asInt, _ := strconv.ParseInt(v, 10, 64)
switch size {
case 0:
Expand All @@ -336,15 +340,15 @@ func deserializeIntegersPointersFromRedis(v string, f reflect.Value, size int) {
}

func deserializeBytesFromRedis(v string, f reflect.Value) {
if v == "" {
if v == nullAsString {
f.SetZero()
} else {
f.SetBytes([]byte(v))
}
}

func deserializeSliceStringFromRedis(v string, f reflect.Value) {
if v != "" {
if v != nullAsString {
values := strings.Split(v, ",")
l := len(values)
newSlice := reflect.MakeSlice(f.Type(), l, l)
Expand All @@ -358,7 +362,7 @@ func deserializeSliceStringFromRedis(v string, f reflect.Value) {
}

func deserializeBoolPointersFromRedis(v string, f reflect.Value) {
if v == cacheNilValue {
if v == nullAsString {
f.SetZero()
} else {
b := v != zeroAsString
Expand All @@ -367,7 +371,7 @@ func deserializeBoolPointersFromRedis(v string, f reflect.Value) {
}

func deserializeFloatPointersFromRedis(v string, f reflect.Value, size int) {
if v != "" {
if v != nullAsString {
asFloat, _ := strconv.ParseFloat(v, 64)
if size == 32 {
val := float32(asFloat)
Expand All @@ -381,7 +385,7 @@ func deserializeFloatPointersFromRedis(v string, f reflect.Value, size int) {
}

func deserializeTimePointersFromRedis(v string, f reflect.Value) {
if v != "" {
if v != nullAsString {
t, _ := time.ParseInLocation(time.DateTime, v, time.UTC)
f.Set(reflect.ValueOf(&t))
return
Expand All @@ -390,7 +394,7 @@ func deserializeTimePointersFromRedis(v string, f reflect.Value) {
}

func deserializeDatePointersFromRedis(v string, f reflect.Value) {
if v != "" {
if v != nullAsString {
t, _ := time.ParseInLocation(time.DateOnly, v, time.UTC)
f.Set(reflect.ValueOf(&t))
return
Expand Down
3 changes: 2 additions & 1 deletion flush.go
Original file line number Diff line number Diff line change
Expand Up @@ -518,9 +518,10 @@ func (c *contextImplementation) handleUpdates(async bool, schema *entitySchema,
}
if schema.hasRedisCache {
p := c.RedisPipeLine(schema.redisCache.GetCode())
rKey := schema.getCacheKey() + ":" + strconv.FormatUint(update.ID(), 10)
for column, val := range newBind {
index := int64(schema.columnMapping[column] + 1)
p.LSet(schema.getCacheKey()+":"+strconv.FormatUint(update.ID(), 10), index, convertBindValueToRedisValue(val))
p.LSet(rKey, index, convertBindValueToRedisValue(val))
}
}
for columnName := range schema.cachedReferences {
Expand Down
2 changes: 1 addition & 1 deletion redis_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func (r *redisCache) LSet(c Context, key string, index int64, value any) {
start := getNow(hasLogger)
_, err := r.client.LSet(c.Ctx(), key, index, value).Result()
if hasLogger {
message := fmt.Sprintf("LSET %d %v", index, value)
message := fmt.Sprintf("LSET %s %d %v", key, index, value)
r.fillLogFields(c, "LSET", message, start, false, err)
}
checkError(err)
Expand Down

0 comments on commit df8984d

Please sign in to comment.