From a83876328f8611c14f21c1f9e0c9919881fb8e93 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Jul 2024 00:56:01 +0330 Subject: [PATCH] perf(count): count command performance optimization (#122) --- core/database/database.go | 25 +++++-------------------- core/server/server.go | 2 +- version.go | 25 +++++++++---------------- 3 files changed, 15 insertions(+), 37 deletions(-) diff --git a/core/database/database.go b/core/database/database.go index ff95366..e3b2f4e 100644 --- a/core/database/database.go +++ b/core/database/database.go @@ -206,12 +206,7 @@ func (db *Database) CountSets(_ []string) string { db.RLock() defer db.RUnlock() - i := 0 - for range db.Sets { - i++ - } - - return fmt.Sprint(i) + return fmt.Sprint(len(db.Sets)) } func (db *Database) CountSubSets(args []string) string { @@ -222,17 +217,12 @@ func (db *Database) CountSubSets(args []string) string { return INVALID } - set, ok := db.Sets[args[0]] + subSet, ok := db.Sets[args[0]] if !ok { return SET_NOT_FOUND } - i := 0 - for range set { - i++ - } - - return fmt.Sprint(i) + return fmt.Sprint(len(subSet)) } func (db *Database) CountElements(args []string) string { @@ -243,17 +233,12 @@ func (db *Database) CountElements(args []string) string { return INVALID } - subSet, ok := db.Sets[args[0]][args[1]] + elms, ok := db.Sets[args[0]][args[1]] if !ok { return SUB_SET_NOT_FOUND } - i := 0 - for range subSet { - i++ - } - - return fmt.Sprint(i) + return fmt.Sprint(len(elms)) } func (db *Database) GetElements(args []string) string { diff --git a/core/server/server.go b/core/server/server.go index 38acc20..bc58ce9 100644 --- a/core/server/server.go +++ b/core/server/server.go @@ -113,7 +113,7 @@ func (s *Server) HandleConnection(conn net.Conn) { n, err := conn.Read(buffer) if err != nil { - ttlogger.Error("Connection closed", "remote address", conn.RemoteAddr().String(), "db-name", s.Config.Name) + ttlogger.Info("Connection closed", "remote address", conn.RemoteAddr().String(), "db-name", s.Config.Name) s.ActiveConnsMux.Lock() delete(s.ActiveConnections, conn) diff --git a/version.go b/version.go index e60cede..dfa7389 100644 --- a/version.go +++ b/version.go @@ -3,25 +3,18 @@ package timetracedb import "fmt" // These constants follow the semantic versioning 2.0.0 spec (http://semver.org/) -type Version struct { - Meta string - Major uint8 - Minor uint8 - Patch uint8 -} - -var version = Version{ - Major: 0, - Minor: 1, - Patch: 0, - Meta: "beta", -} +var ( + major = 0 + minor = 1 + patch = 0 + meta = "beta" +) func StringVersion() string { - v := fmt.Sprintf("%d.%d.%d", version.Major, version.Minor, version.Patch) + v := fmt.Sprintf("%d.%d.%d", major, minor, patch) - if version.Meta != "" { - v = fmt.Sprintf("%s-%s", v, version.Meta) + if meta != "" { + v = fmt.Sprintf("%s-%s", v, meta) } return v