|
| 1 | +package database |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "redis-based-on-go/redis/protocol" |
| 6 | +) |
| 7 | + |
| 8 | +// HSET KEY_NAME FIELD VALUE |
| 9 | +// setHash 为哈希表中的字段赋值 若哈希表不存在则创建并进行 HSET 操作 若字段已经存在于哈希表中则覆盖 |
| 10 | +func setHash(db *DB, args [][]byte) protocol.Reply { |
| 11 | + if len(args) < 3 || len(args)%2 != 1 { |
| 12 | + return &protocol.ARGUEMENTS_NUMBER_ERR_REPLY |
| 13 | + } |
| 14 | + var hash *Hash |
| 15 | + existed := db.isExisted(string(args[0])) |
| 16 | + if existed { |
| 17 | + data, err := db.getData(string(args[0])) |
| 18 | + if err != nil { |
| 19 | + return &protocol.Error{Data: err.Error()} |
| 20 | + } |
| 21 | + var ok bool |
| 22 | + hash, ok = data.(*Hash) |
| 23 | + if !ok { |
| 24 | + return &protocol.TYPE_ERR_REPLY |
| 25 | + } |
| 26 | + } else { |
| 27 | + hash = makeSimpleHash() |
| 28 | + } |
| 29 | + insertCount := 0 |
| 30 | + for i := 0; i < len(args)/2; i++ { |
| 31 | + insertCount += hash.set(string(args[2*i+1]), string(args[2*i+2])) |
| 32 | + } |
| 33 | + db.setData(string(args[0]), hash) |
| 34 | + return &protocol.Integer{Data: insertCount} |
| 35 | +} |
| 36 | + |
| 37 | +// HGET KEY_NAME FIELD_NAME |
| 38 | +// getHash 返回哈希表中指定字段的值 |
| 39 | +func getHash(db *DB, args [][]byte) protocol.Reply { |
| 40 | + if len(args) < 2 { |
| 41 | + return &protocol.ARGUEMENTS_NUMBER_ERR_REPLY |
| 42 | + } |
| 43 | + data, err := db.getData(string(args[0])) |
| 44 | + if err != nil { |
| 45 | + return &protocol.Error{Data: err.Error()} |
| 46 | + } else { |
| 47 | + hash, ok := data.(*Hash) |
| 48 | + if !ok { |
| 49 | + return &protocol.TYPE_ERR_REPLY |
| 50 | + } |
| 51 | + value, ok := hash.table[string(args[1])] |
| 52 | + if !ok { |
| 53 | + return &protocol.Error{Data: fmt.Sprintf("hash field '%s' not found", args[1])} |
| 54 | + } else { |
| 55 | + return &protocol.BulkString{Data: value} |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// HGETALL KEY_NAME |
| 61 | +// getAllField 返回哈希表中所有的字段和值 若 key 不存在则返回空列表 |
| 62 | +func getAllField(db *DB, args [][]byte) protocol.Reply { |
| 63 | + if len(args) != 1 { |
| 64 | + return &protocol.ARGUEMENTS_NUMBER_ERR_REPLY |
| 65 | + } |
| 66 | + data, err := db.getData(string(args[0])) |
| 67 | + if err != nil { |
| 68 | + return &protocol.EMPTY_ARRAY |
| 69 | + } else { |
| 70 | + hash, ok := data.(*Hash) |
| 71 | + if !ok { |
| 72 | + return &protocol.TYPE_ERR_REPLY |
| 73 | + } |
| 74 | + all := hash.getAll() |
| 75 | + replys := make([]protocol.Reply, len(all)) |
| 76 | + for i, s := range all { |
| 77 | + replys[i] = &protocol.BulkString{Data: s} |
| 78 | + } |
| 79 | + return &protocol.Array{Data: replys} |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func initHashCommand() { |
| 84 | + commandTable["hset"] = &command{name: "hset", executor: setHash} |
| 85 | + commandTable["hget"] = &command{name: "hget", executor: getHash} |
| 86 | + commandTable["hgetall"] = &command{name: "hgetall", executor: getAllField} |
| 87 | +} |
0 commit comments