From 5841d48ef6e9b8bd6b2a5629ade250f1734da57a Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Fri, 4 Aug 2023 15:50:55 +0330 Subject: [PATCH 1/7] feat: init data structures --- cmd/main.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 0c0256b..444e6bd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1 +1,13 @@ -package cmd \ No newline at end of file +package main + +import ( + + "github.com/zurvan-lab/TimeTraceDB/src" +) + + +func main() { + database:= src.CreateDataBase() + database.InitSocket() + database.InitUsers() +} \ No newline at end of file From 46791ab812125fc24136b7cae043b73da5b8c639 Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Fri, 4 Aug 2023 15:52:09 +0330 Subject: [PATCH 2/7] feat: init data structures --- go.mod | 2 ++ go.sum | 3 +++ src/.keep | 0 src/config.go | 47 +++++++++++++++++++++++++++++++++++++++++++++++ src/connection.go | 9 +++++++++ src/database.go | 35 +++++++++++++++++++++++++++++++++++ src/element.go | 20 ++++++++++++++++++++ src/set.go | 22 ++++++++++++++++++++++ src/user.go | 26 ++++++++++++++++++++++++++ 9 files changed, 164 insertions(+) create mode 100644 go.sum delete mode 100644 src/.keep create mode 100644 src/config.go create mode 100644 src/connection.go create mode 100644 src/database.go create mode 100644 src/element.go create mode 100644 src/set.go create mode 100644 src/user.go diff --git a/go.mod b/go.mod index c1be89b..b5d9c86 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,5 @@ module github.com/zurvan-lab/TimeTraceDB go 1.20 + +require gopkg.in/yaml.v2 v2.4.0 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..7534661 --- /dev/null +++ b/go.sum @@ -0,0 +1,3 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= diff --git a/src/.keep b/src/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/src/config.go b/src/config.go new file mode 100644 index 0000000..7800995 --- /dev/null +++ b/src/config.go @@ -0,0 +1,47 @@ +package src + +import ( + "os" + + "gopkg.in/yaml.v2" +) + +type Config struct { + Authorization bool + Proc struct { + Cores, Threads int + } + Listen struct { + Ip, Port string + } + Log struct { + Path string + } + User struct { + Name string + Token string + } +} + +func createConfig() *Config { + return &Config{} +} + +func ReadConfigFile(path string) *Config { + file, err := os.Open(path) + if err != nil { + // TODO: log + } + defer file.Close() + + config := createConfig() + + decoder := yaml.NewDecoder(file) + err = decoder.Decode(&config) + if err != nil { + // TODO: log + } + //TODO: validate config + //TODO: Log + return config +} diff --git a/src/connection.go b/src/connection.go new file mode 100644 index 0000000..97ac54c --- /dev/null +++ b/src/connection.go @@ -0,0 +1,9 @@ +package src + +import "net" + +type Connection struct { + Connection net.Listener + User User + Authorized bool +} \ No newline at end of file diff --git a/src/database.go b/src/database.go new file mode 100644 index 0000000..e255e6c --- /dev/null +++ b/src/database.go @@ -0,0 +1,35 @@ +package src + +import ( + "net" + "os" +) + +type Database struct { + Sets Sets + Config Config + socket net.Listener + Users Users +} + +func CreateDataBase() *Database { + return &Database{ + Sets: *NewSets(), + Config: *ReadConfigFile(""), + } +} + +func (db *Database)InitSocket() { + var err error + db.socket ,err = net.Listen("tcp",db.Config.Listen.Ip+":"+db.Config.Listen.Port) + if err != nil { + os.Exit(1) + } +} + +func (db *Database) InitUsers() { + users:= CreateUsers() + db.Users = *users + cmds:= []string{"all"} + users.NewUser(db.Config.User.Name,db.Config.User.Token,cmds) +} diff --git a/src/element.go b/src/element.go new file mode 100644 index 0000000..13178c3 --- /dev/null +++ b/src/element.go @@ -0,0 +1,20 @@ +package src + +import ( + "time" +) + +type Value struct { + Data interface{} + Time time.Time +} + +type Elements map[string]Value + +func NewElements() *Elements { + return &Elements{} +} + +func (e *Elements) AddElement(key string, v Value) { + (*e)[key] = v +} diff --git a/src/set.go b/src/set.go new file mode 100644 index 0000000..d0dabb4 --- /dev/null +++ b/src/set.go @@ -0,0 +1,22 @@ +package src + + +type Set struct { + Elements Elements +} + +type Sets map[string]Set + +func NewSets() *Sets { + return &Sets{} +} + +func (s *Sets) NewSet(key string) { + (*s)[key] = Set{ + Elements: *NewElements(), + } +} + +func (s *Set) NewElement(key string, v Value) { + s.Elements.AddElement(key,v) +} \ No newline at end of file diff --git a/src/user.go b/src/user.go new file mode 100644 index 0000000..f10e250 --- /dev/null +++ b/src/user.go @@ -0,0 +1,26 @@ +package src + +import ( + "math/rand" +) + +type User struct { + Name, Token string + Cmds []string +} + +type Users map[int]User + +func CreateUsers() *Users { + return &Users{} +} + +func (u *Users) NewUser(name, Token string, cmds []string) { + for { + id := rand.Intn(99999999) + if _, ok := (*u)[id]; !ok { + (*u)[id] = User{Name: name, Cmds: cmds, Token: Token} + break + } + } +} From f7aef2792af6e96481397ca555389541ef49c3ea Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Fri, 4 Aug 2023 16:06:50 +0330 Subject: [PATCH 3/7] fix: lint bug --- cmd/main.go | 8 +++----- src/config.go | 9 +++++---- src/connection.go | 2 +- src/database.go | 22 +++++++++++----------- src/set.go | 5 ++--- src/user.go | 4 ++-- utils/.keep | 0 utils/random.go | 15 +++++++++++++++ 8 files changed, 39 insertions(+), 26 deletions(-) delete mode 100644 utils/.keep create mode 100644 utils/random.go diff --git a/cmd/main.go b/cmd/main.go index 444e6bd..18dc633 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,13 +1,11 @@ package main import ( - "github.com/zurvan-lab/TimeTraceDB/src" ) - -func main() { - database:= src.CreateDataBase() +func main() { + database := src.CreateDataBase() database.InitSocket() database.InitUsers() -} \ No newline at end of file +} diff --git a/src/config.go b/src/config.go index 7800995..4d8f5a4 100644 --- a/src/config.go +++ b/src/config.go @@ -3,6 +3,7 @@ package src import ( "os" + "github.com/zurvan-lab/TimeTraceDB/log" "gopkg.in/yaml.v2" ) @@ -12,13 +13,13 @@ type Config struct { Cores, Threads int } Listen struct { - Ip, Port string + IP, Port string } Log struct { Path string } User struct { - Name string + Name string Token string } } @@ -30,7 +31,7 @@ func createConfig() *Config { func ReadConfigFile(path string) *Config { file, err := os.Open(path) if err != nil { - // TODO: log + log.Error("Can not open the config file", "error: ", err) } defer file.Close() @@ -39,7 +40,7 @@ func ReadConfigFile(path string) *Config { decoder := yaml.NewDecoder(file) err = decoder.Decode(&config) if err != nil { - // TODO: log + log.Error("Can not decode the Config Yaml file", "error: ", err) } //TODO: validate config //TODO: Log diff --git a/src/connection.go b/src/connection.go index 97ac54c..aafd24a 100644 --- a/src/connection.go +++ b/src/connection.go @@ -6,4 +6,4 @@ type Connection struct { Connection net.Listener User User Authorized bool -} \ No newline at end of file +} diff --git a/src/database.go b/src/database.go index e255e6c..de6eefe 100644 --- a/src/database.go +++ b/src/database.go @@ -6,30 +6,30 @@ import ( ) type Database struct { - Sets Sets - Config Config - socket net.Listener - Users Users + Sets Sets + Config Config + socket net.Listener + Users Users } func CreateDataBase() *Database { return &Database{ - Sets: *NewSets(), + Sets: *NewSets(), Config: *ReadConfigFile(""), } } -func (db *Database)InitSocket() { +func (db *Database) InitSocket() { var err error - db.socket ,err = net.Listen("tcp",db.Config.Listen.Ip+":"+db.Config.Listen.Port) + db.socket, err = net.Listen("tcp", db.Config.Listen.IP+":"+db.Config.Listen.Port) if err != nil { os.Exit(1) } } -func (db *Database) InitUsers() { - users:= CreateUsers() +func (db *Database) InitUsers() { + users := CreateUsers() db.Users = *users - cmds:= []string{"all"} - users.NewUser(db.Config.User.Name,db.Config.User.Token,cmds) + cmds := []string{"all"} + users.NewUser(db.Config.User.Name, db.Config.User.Token, cmds) } diff --git a/src/set.go b/src/set.go index d0dabb4..4407c04 100644 --- a/src/set.go +++ b/src/set.go @@ -1,6 +1,5 @@ package src - type Set struct { Elements Elements } @@ -18,5 +17,5 @@ func (s *Sets) NewSet(key string) { } func (s *Set) NewElement(key string, v Value) { - s.Elements.AddElement(key,v) -} \ No newline at end of file + s.Elements.AddElement(key, v) +} diff --git a/src/user.go b/src/user.go index f10e250..e7e30cc 100644 --- a/src/user.go +++ b/src/user.go @@ -1,7 +1,7 @@ package src import ( - "math/rand" + "github.com/zurvan-lab/TimeTraceDB/utils" ) type User struct { @@ -17,7 +17,7 @@ func CreateUsers() *Users { func (u *Users) NewUser(name, Token string, cmds []string) { for { - id := rand.Intn(99999999) + id, _ := utils.GenerateRandomNumber(1, 99999999) if _, ok := (*u)[id]; !ok { (*u)[id] = User{Name: name, Cmds: cmds, Token: Token} break diff --git a/utils/.keep b/utils/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/utils/random.go b/utils/random.go new file mode 100644 index 0000000..18a5c1e --- /dev/null +++ b/utils/random.go @@ -0,0 +1,15 @@ +package utils + +import ( + "crypto/rand" + "math/big" +) + +func GenerateRandomNumber(min, max int) (int, error) { + n, err := rand.Int(rand.Reader, big.NewInt(int64(max-min+1))) + if err != nil { + return 0, err + } + + return int(n.Int64()) + min, nil +} From 39a444036b14a79070bf777c64180100270fc197 Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Fri, 4 Aug 2023 17:07:22 +0330 Subject: [PATCH 4/7] feat: read config.yaml --- cmd/main.go | 7 ++++++- config/config.yaml | 17 +++++++++-------- src/config.go | 42 ++++++++++++++++++++++++++++-------------- src/database.go | 6 +++--- 4 files changed, 46 insertions(+), 26 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 18dc633..d45964b 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,11 +1,16 @@ package main import ( + "fmt" + "os" + "github.com/zurvan-lab/TimeTraceDB/src" ) func main() { - database := src.CreateDataBase() + database := src.CreateDataBase(os.Args[1]) database.InitSocket() database.InitUsers() + + fmt.Println(database.Users) } diff --git a/config/config.yaml b/config/config.yaml index 084bfe8..ff0b94b 100644 --- a/config/config.yaml +++ b/config/config.yaml @@ -2,17 +2,18 @@ name: TimeTraceConfig authorization: false proc: - - cores: 2 - - threads: 4 + cores: 2 + threads: 4 server: - - listen: localhost - - port: 7070 + listen: localhost + port: 7070 log: - - path: ./log + path: ./log user: - - name: root - - token: super_secret_token - - cmd: [] + name: root + token: super_secret_token + cmd: + - all diff --git a/src/config.go b/src/config.go index 4d8f5a4..ec5ce7b 100644 --- a/src/config.go +++ b/src/config.go @@ -1,6 +1,7 @@ package src import ( + "fmt" "os" "github.com/zurvan-lab/TimeTraceDB/log" @@ -8,20 +9,32 @@ import ( ) type Config struct { - Authorization bool - Proc struct { - Cores, Threads int - } - Listen struct { - IP, Port string - } - Log struct { - Path string - } - User struct { - Name string - Token string - } + Name string `yaml:"name"` + Authorization bool `yaml:"authorization"` + Proc Proc `yaml:"proc"` + Listen Listen `yaml:"server"` + Log Log `yaml:"log"` + FirstUser FirstUser `yaml:"user"` +} + +type Proc struct { + Cores int `yaml:"cores"` + Threads int `yaml:"threads"` +} + +type Listen struct { + IP string `yaml:"listen"` + Port string `yaml:"port"` +} + +type Log struct { + Path string `yaml:"path"` +} + +type FirstUser struct { + Name string `yaml:"name"` + Token string `yaml:"token"` + Cmd []string `yaml:"cmd"` } func createConfig() *Config { @@ -29,6 +42,7 @@ func createConfig() *Config { } func ReadConfigFile(path string) *Config { + fmt.Println(path) file, err := os.Open(path) if err != nil { log.Error("Can not open the config file", "error: ", err) diff --git a/src/database.go b/src/database.go index de6eefe..5d28e5a 100644 --- a/src/database.go +++ b/src/database.go @@ -12,10 +12,10 @@ type Database struct { Users Users } -func CreateDataBase() *Database { +func CreateDataBase(path string) *Database { return &Database{ Sets: *NewSets(), - Config: *ReadConfigFile(""), + Config: *ReadConfigFile(path), } } @@ -31,5 +31,5 @@ func (db *Database) InitUsers() { users := CreateUsers() db.Users = *users cmds := []string{"all"} - users.NewUser(db.Config.User.Name, db.Config.User.Token, cmds) + users.NewUser(db.Config.FirstUser.Name, db.Config.FirstUser.Token, cmds) } From 14710c3a05fe9d0700108c49c908514e14e00a34 Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Fri, 4 Aug 2023 18:42:34 +0330 Subject: [PATCH 5/7] feat: operations --- src/set.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/src/set.go b/src/set.go index 4407c04..d1ed61b 100644 --- a/src/set.go +++ b/src/set.go @@ -19,3 +19,74 @@ func (s *Sets) NewSet(key string) { func (s *Set) NewElement(key string, v Value) { s.Elements.AddElement(key, v) } + +// Get Sets Name +func (s *Sets) GetSetsName() []string { + keys := make([]string, 0, len(*s)) + for key := range *s { + keys = append(keys, key) + } + return keys +} + +// Drop Set +func (s *Sets) DropSet(SetName string) { + delete(*s, SetName) +} + +// Clean Set +func (s *Sets) CleanSet(SetName string) { + if _, ok := (*s)[SetName]; ok { + (*s)[SetName] = Set{} + } +} + +// Get Elements of a set +func (s *Set) Get(Key string) Value { + return s.Elements[Key] +} + +// Count elements of a set +func (s *Set) Count() int { + return len((*s).Elements) +} + +// TODO: Find List of Elements from a set + +// Remove Element from a set +func (s *Set) Remove(Key string) { + delete((*s).Elements, Key) +} + +// TODO: Remove list of elements from a set + +// Update Element from a set +func (s *Set) Upadte(Key string, NewValue Value) { + (s.Elements)[Key] = NewValue +} + +// TODO: Update list of elements from a set + +// Get All Keys +func (s *Set) GetKeys() []string { + keys := make([]string, 0, len(s.Elements)) + for key := range s.Elements { + keys = append(keys, key) + } + return keys +} + +// Get All Values +func (s *Set) GetValues() []Value { + values := make([]Value, 0, len(s.Elements)) + + for v := range s.Elements { + values = append(values, s.Elements[v]) + } + return values +} + +// TODO: Create Snapshot +// TODO: End Snapshot + +// TODO: Sort With Limit And Offset From 4dd5df9aaffa029d560666f8307c8bdcb2b5d534 Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Sun, 6 Aug 2023 11:20:26 +0330 Subject: [PATCH 6/7] set features --- src/set.go | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/set.go b/src/set.go index d1ed61b..167bedd 100644 --- a/src/set.go +++ b/src/set.go @@ -51,22 +51,16 @@ func (s *Set) Count() int { return len((*s).Elements) } -// TODO: Find List of Elements from a set - // Remove Element from a set func (s *Set) Remove(Key string) { delete((*s).Elements, Key) } -// TODO: Remove list of elements from a set - // Update Element from a set func (s *Set) Upadte(Key string, NewValue Value) { (s.Elements)[Key] = NewValue } -// TODO: Update list of elements from a set - // Get All Keys func (s *Set) GetKeys() []string { keys := make([]string, 0, len(s.Elements)) @@ -88,5 +82,9 @@ func (s *Set) GetValues() []Value { // TODO: Create Snapshot // TODO: End Snapshot - +// TODO: Find List of Elements from a set // TODO: Sort With Limit And Offset +// TODO: regex search +// TODO: Update list of elements from a set +// TODO: Remove list of elements from a set +// TODO: handles foreach operation on a set From 5339da43feeac8dedc5cf8b04084a193f3ad394e Mon Sep 17 00:00:00 2001 From: zigBalthazar Date: Tue, 8 Aug 2023 20:16:35 +0330 Subject: [PATCH 7/7] init execution --- src/execution.go | 2 ++ src/set.go | 3 ++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 src/execution.go diff --git a/src/execution.go b/src/execution.go new file mode 100644 index 0000000..0c4ad1a --- /dev/null +++ b/src/execution.go @@ -0,0 +1,2 @@ +package src + diff --git a/src/set.go b/src/set.go index 167bedd..2a662b1 100644 --- a/src/set.go +++ b/src/set.go @@ -81,10 +81,11 @@ func (s *Set) GetValues() []Value { } // TODO: Create Snapshot +// TODO: Create schedule Snapshot // TODO: End Snapshot // TODO: Find List of Elements from a set // TODO: Sort With Limit And Offset // TODO: regex search // TODO: Update list of elements from a set // TODO: Remove list of elements from a set -// TODO: handles foreach operation on a set +// TODO: handles foreach operation on a set \ No newline at end of file