diff --git a/TODO.md b/TODO.md new file mode 100644 index 0000000..eeb11f2 --- /dev/null +++ b/TODO.md @@ -0,0 +1,26 @@ +### Todo +- [ ] Get Sets Name +- [ ] Drop Set +- [ ] Clean Set +- [ ] Get Elements of a set +- [ ] Count elements of a set +- [ ] Remove Element from a set +- [ ] Update Element from a set +- [ ] Get All Keys +- [ ] Get All Values +- [ ] Create Snapshot +- [ ] Create schedule Snapshot +- [ ] End Snapshot +- [ ] Find List of Elements from a set +- [ ] Sort With Limit And Offset +- [ ] regex search +- [ ] Update list of elements from a set +- [ ] Remove list of elements from a set +- [ ] handles foreach operation on a set +- [ ] HAVE QUERY => T/F + + +### In Progress + + +### ✓ Done diff --git a/cmd/main.go b/cmd/main.go index 9321304..7f37084 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -3,11 +3,11 @@ package main import ( "os" - "github.com/zurvan-lab/TimeTraceDB/src" + "github.com/zurvan-lab/TimeTraceDB/core/database" ) func main() { - database := src.CreateDataBase(os.Args[1]) + database := database.CreateDataBase(os.Args[0]) database.InitSocket() database.InitUsers() } diff --git a/src/config.go b/config/config.go similarity index 90% rename from src/config.go rename to config/config.go index b12f8c5..10c11f5 100644 --- a/src/config.go +++ b/config/config.go @@ -1,4 +1,4 @@ -package src +package config import ( "os" @@ -40,7 +40,11 @@ func createConfig() *Config { return &Config{} } -func ReadConfigFile(path string) *Config { +func (c Config) BasicCheck() error { + return nil +} + +func LoadFromFile(path string) *Config { 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/core/database/database.go similarity index 70% rename from src/database.go rename to core/database/database.go index 5d28e5a..6ce0345 100644 --- a/src/database.go +++ b/core/database/database.go @@ -1,21 +1,24 @@ -package src +package database import ( "net" "os" + + "github.com/zurvan-lab/TimeTraceDB/config" + "github.com/zurvan-lab/TimeTraceDB/core" ) type Database struct { Sets Sets - Config Config + Config config.Config socket net.Listener - Users Users + Users core.Users } func CreateDataBase(path string) *Database { return &Database{ Sets: *NewSets(), - Config: *ReadConfigFile(path), + Config: *config.LoadFromFile(path), } } @@ -28,7 +31,7 @@ func (db *Database) InitSocket() { } func (db *Database) InitUsers() { - users := CreateUsers() + users := core.CreateUsers() db.Users = *users cmds := []string{"all"} users.NewUser(db.Config.FirstUser.Name, db.Config.FirstUser.Token, cmds) diff --git a/core/database/element.go b/core/database/element.go new file mode 100644 index 0000000..cea2790 --- /dev/null +++ b/core/database/element.go @@ -0,0 +1,12 @@ +package database + +import "time" + +type Element struct { + value string + time time.Time +} + +func NewElement(v string) Element { + return Element{value: v, time: time.Now()} +} diff --git a/core/database/set.go b/core/database/set.go new file mode 100644 index 0000000..719797a --- /dev/null +++ b/core/database/set.go @@ -0,0 +1,21 @@ +package database + +type set map[string]SubSet + +type Sets map[string]set + +func NewSets() *Sets { + return &Sets{} +} + +func newSet() *set { + return &set{} +} + +func (s *Sets) AddSet(name string) { + (*s)[name] = *newSet() +} + +func (s set) AddSubSet(name string) { + (s)[name] = *NewSubSet() +} diff --git a/core/database/subSets.go b/core/database/subSets.go new file mode 100644 index 0000000..d55d187 --- /dev/null +++ b/core/database/subSets.go @@ -0,0 +1,17 @@ +package database + +type SubSets map[string]SubSet +type SubSet []Element + +func NewSubSets() SubSets { + return SubSets{} +} + +func NewSubSet() *SubSet { + return &SubSet{} +} + +func (db *Database) PushElement(s string, sb string, e Element) { + r := db.Sets[s][sb] + db.Sets[s][sb] = append(r, e) +} diff --git a/core/execution.go b/core/execution.go new file mode 100644 index 0000000..9a8bc95 --- /dev/null +++ b/core/execution.go @@ -0,0 +1 @@ +package core diff --git a/src/user.go b/core/user.go similarity index 96% rename from src/user.go rename to core/user.go index e7e30cc..90177c5 100644 --- a/src/user.go +++ b/core/user.go @@ -1,4 +1,4 @@ -package src +package core import ( "github.com/zurvan-lab/TimeTraceDB/utils" diff --git a/log/logger.go b/log/logger.go index 1b32fa9..a2b1837 100644 --- a/log/logger.go +++ b/log/logger.go @@ -23,6 +23,8 @@ func addFields(event *zerolog.Event, keyvals ...interface{}) *zerolog.Event { event.Stringer(key, v) case []byte: event.Str(key, fmt.Sprintf("%v", hex.EncodeToString(v))) + case error: + event.AnErr(key, v) default: event.Any(key, v) } diff --git a/src/connection.go b/src/connection.go deleted file mode 100644 index aafd24a..0000000 --- a/src/connection.go +++ /dev/null @@ -1,9 +0,0 @@ -package src - -import "net" - -type Connection struct { - Connection net.Listener - User User - Authorized bool -} diff --git a/src/element.go b/src/element.go deleted file mode 100644 index 13178c3..0000000 --- a/src/element.go +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index d1ed61b..0000000 --- a/src/set.go +++ /dev/null @@ -1,92 +0,0 @@ -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) -} - -// 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 diff --git a/utils/random.go b/utils/random.go index 18a5c1e..cb8f9e0 100644 --- a/utils/random.go +++ b/utils/random.go @@ -13,3 +13,11 @@ func GenerateRandomNumber(min, max int) (int, error) { return int(n.Int64()) + min, nil } + +func BytesToString(data []byte) string { + return string(data) +} + +func StringToBytes(s string) []byte { + return []byte(s) +}