Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
perf(count): count command performance optimization (#122)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jul 11, 2024
1 parent bb6b7e0 commit a838763
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 37 deletions.
25 changes: 5 additions & 20 deletions core/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion core/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
25 changes: 9 additions & 16 deletions version.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit a838763

Please sign in to comment.