From ce9d9f8b2183bc0a31bfa40e780ae07182bc7cf8 Mon Sep 17 00:00:00 2001 From: alex Date: Sun, 12 Jul 2026 14:09:36 +0200 Subject: added pubsub command registry --- pkg/commands/command-registry.go | 34 +++++++++++++++++++++++++++------- 1 file changed, 27 insertions(+), 7 deletions(-) (limited to 'pkg/commands/command-registry.go') diff --git a/pkg/commands/command-registry.go b/pkg/commands/command-registry.go index 068fe7c..19c8acc 100644 --- a/pkg/commands/command-registry.go +++ b/pkg/commands/command-registry.go @@ -2,7 +2,9 @@ package commands import ( "errors" + "net" "redisClone/pkg/core" + "redisClone/pkg/pubsub" ) var dbInstance core.RedisDB @@ -27,17 +29,13 @@ func RenameKeys(args []string) []string { return []string{args[1], args[2]} } -type CommandDef struct{ +type BaseCommand struct{ MinArgs int ExtractKeys func(args []string)[]string Execute func(args []string, getShard func(k string) *core.Shard) interface{} } -func DispatchBaseCommand(db core.RedisDB ,commandName string, args []string) interface{} { - cmd, exists := Registry[commandName] - if !exists { - return nil - } +func DispatchBaseCommand(db core.RedisDB ,cmd BaseCommand, args []string) interface{} { if len(args) < cmd.MinArgs { return errors.New("wrong number of arguments") @@ -50,7 +48,23 @@ func DispatchBaseCommand(db core.RedisDB ,commandName string, args []string) int return cmd.Execute(args, db.GetShard) } -var Registry = map[string]CommandDef{ +type PubSubCommand struct{ + MinArgs int + Execute func(hub *pubsub.Hub, conn net.Conn, args []string) interface{} +} + +func DispatchPubSub(conn net.Conn, h *pubsub.Hub, cmd PubSubCommand, args []string) { + if len(args) < cmd.MinArgs { + conn.Write(core.SerializeRESP(errors.New("ERR wrong number of arguments"))) + return + } + if res := cmd.Execute(h, conn, args); res != nil { + conn.Write(core.SerializeRESP(res)) + } +} + + +var BaseRegistry = map[string]BaseCommand{ "GET": { MinArgs: 2, ExtractKeys: SingleKey, @@ -171,4 +185,10 @@ var Registry = map[string]CommandDef{ ExtractKeys: NoKeys, Execute: Ping, }, +} + +var PubSubRegistry = map[string]PubSubCommand{ + "SUBSCRIBE": {MinArgs: 2, Execute: Subscribe}, + "UNSUBSCRIBE": {MinArgs: 1, Execute: Unsubscribe}, + "PUBLISH": {MinArgs: 3, Execute: Publish}, } \ No newline at end of file -- cgit v1.2.3