Skip to content

Commit

Permalink
add Destroy methods for cleanup (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetam authored Sep 23, 2016
1 parent d50c494 commit 221aede
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 8 deletions.
24 changes: 24 additions & 0 deletions lm2.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ func (rc *recordCache) close() {
rc.f.Close()
}

func (rc *recordCache) destroy() error {
rc.close()
return os.Remove(rc.f.Name())
}

func (rc *recordCache) findLastLessThan(key string) int64 {
rc.lock.RLock()
defer rc.lock.RUnlock()
Expand Down Expand Up @@ -827,3 +832,22 @@ func (c *Collection) Version() int64 {
func (c *Collection) Stats() Stats {
return c.stats.clone()
}

// Destroy closes the collection and removes its associated data files.
func (c *Collection) Destroy() error {
c.Close()
var err error
err = os.Remove(c.f.Name())
if err != nil {
return err
}
err = c.wal.Destroy()
if err != nil {
return err
}
err = c.cache.destroy()
if err != nil {
return err
}
return nil
}
23 changes: 15 additions & 8 deletions lm2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestCopy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

const N = 1000
firstWriteStart := time.Now()
Expand All @@ -51,7 +51,7 @@ func TestCopy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c2.Close()
defer c2.Destroy()

cur, err := c.NewCursor()
if err != nil {
Expand Down Expand Up @@ -121,7 +121,7 @@ func TestWriteBatch(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

wb := NewWriteBatch()
wb.Set("key1", "1")
Expand Down Expand Up @@ -200,7 +200,7 @@ func TestWriteBatch1(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

const N = 500
for i := 0; i < N; i++ {
Expand All @@ -220,7 +220,7 @@ func TestWriteBatch1Concurrent(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

const N = 50
const NumGoroutines = 8
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestWriteBatch2(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

wb := NewWriteBatch()

Expand Down Expand Up @@ -405,6 +405,11 @@ func TestWriteCloseOpen(t *testing.T) {
i++
}
t.Logf("%+v", c.Stats())

err = c.Destroy()
if err != nil {
t.Fatal(err)
}
}

func TestReadLastEntry(t *testing.T) {
Expand Down Expand Up @@ -436,14 +441,16 @@ func TestReadLastEntry(t *testing.T) {
t.Errorf("expected %d records, got %d", 2, entry.NumRecords)
}
t.Logf("%+v", c.Stats())

c.Destroy()
}

func TestSeekToFirstKey(t *testing.T) {
c, err := NewCollection("/tmp/test_seektofirstkey.lm2", 100)
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

wb := NewWriteBatch()
wb.Set("a", "1")
Expand Down Expand Up @@ -479,7 +486,7 @@ func TestOverwriteFirstKey(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer c.Close()
defer c.Destroy()

wb := NewWriteBatch()
wb.Set("a", "1")
Expand Down
5 changes: 5 additions & 0 deletions wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,8 @@ func (w *wal) Truncate() error {
func (w *wal) Close() {
w.f.Close()
}

func (w *wal) Destroy() error {
w.Close()
return os.Remove(w.f.Name())
}

0 comments on commit 221aede

Please sign in to comment.