Skip to content

Commit

Permalink
option just to create gorage just in memory, without needing to creat…
Browse files Browse the repository at this point in the history
…e a file
  • Loading branch information
ax4w committed Nov 23, 2023
1 parent bbe3be4 commit e41db9a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
12 changes: 12 additions & 0 deletions gorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type Gorage struct {
Log bool
Path string
Tables []Table
memOnly bool
}

/*
Expand Down Expand Up @@ -160,6 +161,9 @@ func (g *Gorage) CreateTable(name string) *Table {
Save the loaded gorage
*/
func (g *Gorage) Save() {
if g.memOnly {
return
}
err := os.Truncate(g.Path, 0)
if err != nil {
panic(err.Error())
Expand Down Expand Up @@ -194,6 +198,14 @@ func Open(path string) *Gorage {
return &g
}

func CreateMemOnly(allowDuplicates, log bool) *Gorage {
return &Gorage{
AllowDuplicated: allowDuplicates,
Log: log,
memOnly: true,
}
}

/*
Create a new Gorage
Expand Down
12 changes: 12 additions & 0 deletions gorage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,15 @@ func TestComplete(t *testing.T) {
userTable.Where(":Handle == '@Emily_Backup' || :Name == 'Carl'").Delete()
gorage.Save()
}

func TestCreateMemOnly(t *testing.T) {
g := CreateMemOnly(true, false)
tab := g.CreateTable("Test")
tab.AddColumn("Name", STRING)
tab.Insert([]interface{}{"Tom"})
tab.Insert([]interface{}{"Tom"})
for _, v := range tab.Rows {
println(v[0].(string))
}
g.Close()
}

0 comments on commit e41db9a

Please sign in to comment.