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

refactor: databse functions and data structure #43

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
### Todo
- [ ] Get Sets Name
- [ ] Drop Set
- [ ] Clean Set
- [ ] Get Elements of a set
- [ ] Count elements of a set
- [ ] Remove Element from a set
Expand All @@ -24,3 +22,5 @@


### ✓ Done
- [X] Clean Set
- [X] Drop Set
4 changes: 2 additions & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

func main() {
database := database.CreateDataBase(os.Args[0])
database.InitSocket()
database := database.Init(os.Args[0])
database.AddSet("test")
}
78 changes: 65 additions & 13 deletions core/database/database.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,81 @@
package database

import (
"net"
"os"

"github.com/zurvan-lab/TimeTraceDB/config"
)

type Database struct {
Sets Sets
Config config.Config
socket net.Listener
Config *config.Config
}

func CreateDataBase(path string) *Database {
func Init(path string) *Database {
return &Database{
Sets: *NewSets(),
Config: *config.LoadFromFile(path),
Sets: make(Sets),
Config: config.LoadFromFile(path),
}
}

func (db *Database) AddSet(name string) string {
db.Sets[name] = make(Set)
return "DONE"
}

func (db *Database) AddSubSet(set, name string) string {
s, ok := db.Sets[set]
if !ok {
return "SETNF"
}
s[name] = make(SubSet, 0)
return "DONE"
}

func (db *Database) PushElement(set, subset string, e Element) string {
_, ok := db.Sets[set][subset]
if !ok {
return "SUBSETNF"
}
db.Sets[set][subset] = append(db.Sets[set][subset], e)
return "DONE"
}

func (db *Database) DropSet(name string) string {
_, ok := db.Sets[name]
if !ok {
return "SETNF"
}
delete(db.Sets, name)
return "DONE"
}

func (db *Database) DropSubSet(set, subset string) string {
_, ok := db.Sets[set][subset]
if !ok {
return "SUBETNF"
}
delete(db.Sets[set], subset)
return "DONE"
}

func (db *Database) CleanSets() string {
db.Sets = make(Sets)
return "DONE"
}

func (db *Database) CleanSet(name string) string {
_, ok := db.Sets[name]
if !ok {
return "SETNF"
}
db.Sets[name] = make(Set)
return "DONE"
}

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) CleanSubSet(set, subset string) string {
_, ok := db.Sets[set][subset]
if !ok {
return "SUBETNF"
}
db.Sets[set][subset] = make(SubSet, 0)
return "DONE"
}
5 changes: 0 additions & 5 deletions core/database/element.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ package database

import "time"

type Element struct {
value string
time time.Time
}

func NewElement(v string) Element {
return Element{value: v, time: time.Now()}
}
21 changes: 0 additions & 21 deletions core/database/set.go

This file was deleted.

19 changes: 0 additions & 19 deletions core/database/subSets.go

This file was deleted.

14 changes: 14 additions & 0 deletions core/database/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package database

import "time"

type Element struct {
value string
time time.Time
}

type (
Sets map[string]Set
Set map[string]SubSet
SubSet []Element
)