Browse Source

wal: add walName function; cleanup test

Xiang Li 11 years ago
parent
commit
1d09c25f5f
3 changed files with 21 additions and 23 deletions
  1. 4 0
      wal/util.go
  2. 2 2
      wal/wal.go
  3. 15 21
      wal/wal_test.go

+ 4 - 0
wal/util.go

@@ -83,6 +83,10 @@ func parseWalName(str string) (seq, index int64, err error) {
 	return
 }
 
+func walName(seq, index int64) string {
+	return fmt.Sprintf("%016x-%016x.wal", seq, index)
+}
+
 func max(a, b int64) int64 {
 	if a > b {
 		return a

+ 2 - 2
wal/wal.go

@@ -76,7 +76,7 @@ func Create(dirpath string) (*WAL, error) {
 		return nil, err
 	}
 
-	p := path.Join(dirpath, fmt.Sprintf("%016x-%016x.wal", 0, 0))
+	p := path.Join(dirpath, walName(0, 0))
 	f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
 	if err != nil {
 		return nil, err
@@ -207,7 +207,7 @@ func (w *WAL) Cut() error {
 	log.Printf("wal.cut index=%d", w.enti+1)
 
 	// create a new wal file with name sequence + 1
-	fpath := path.Join(w.dir, fmt.Sprintf("%016x-%016x.wal", w.seq+1, w.enti+1))
+	fpath := path.Join(w.dir, walName(w.seq+1, w.enti+1))
 	f, err := os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
 	if err != nil {
 		return err

+ 15 - 21
wal/wal_test.go

@@ -18,7 +18,6 @@ package wal
 
 import (
 	"bytes"
-	"fmt"
 	"io/ioutil"
 	"os"
 	"path"
@@ -31,8 +30,6 @@ import (
 var (
 	infoData   = []byte("\b\xef\xfd\x02")
 	infoRecord = append([]byte("\x0e\x00\x00\x00\x00\x00\x00\x00\b\x01\x10\x99\xb5\xe4\xd0\x03\x1a\x04"), infoData...)
-
-	firstWalName = "0000000000000000-0000000000000000.wal"
 )
 
 func TestNew(t *testing.T) {
@@ -46,8 +43,8 @@ func TestNew(t *testing.T) {
 	if err != nil {
 		t.Fatalf("err = %v, want nil", err)
 	}
-	if g := path.Base(w.f.Name()); g != firstWalName {
-		t.Errorf("name = %+v, want %+v", g, firstWalName)
+	if g := path.Base(w.f.Name()); g != walName(0, 0) {
+		t.Errorf("name = %+v, want %+v", g, walName(0, 0))
 	}
 	w.Close()
 }
@@ -59,7 +56,7 @@ func TestNewForInitedDir(t *testing.T) {
 	}
 	defer os.RemoveAll(p)
 
-	os.Create(path.Join(p, firstWalName))
+	os.Create(path.Join(p, walName(0, 0)))
 	if _, err = Create(p); err == nil || err != os.ErrExist {
 		t.Errorf("err = %v, want %v", err, os.ErrExist)
 	}
@@ -72,13 +69,7 @@ func TestOpenAtIndex(t *testing.T) {
 	}
 	defer os.RemoveAll(dir)
 
-	f, err := os.Create(path.Join(dir, firstWalName))
-	if err != nil {
-		t.Fatal(err)
-	}
-	f.Close()
-	writef := fmt.Sprintf("%016x-%016x.wal", 1, 4)
-	f, err = os.Create(path.Join(dir, writef))
+	f, err := os.Create(path.Join(dir, walName(0, 0)))
 	if err != nil {
 		t.Fatal(err)
 	}
@@ -88,15 +79,15 @@ func TestOpenAtIndex(t *testing.T) {
 	if err != nil {
 		t.Fatalf("err = %v, want nil", err)
 	}
-	if g := path.Base(w.f.Name()); g != writef {
-		t.Errorf("name = %+v, want %+v", g, writef)
+	if g := path.Base(w.f.Name()); g != walName(0, 0) {
+		t.Errorf("name = %+v, want %+v", g, walName(0, 0))
 	}
-	if w.seq != 1 {
-		t.Errorf("seq = %d, want %d", w.seq, 1)
+	if w.seq != 0 {
+		t.Errorf("seq = %d, want %d", w.seq, 0)
 	}
 	w.Close()
 
-	wname := fmt.Sprintf("%016x-%016x.wal", 2, 10)
+	wname := walName(2, 10)
 	f, err = os.Create(path.Join(dir, wname))
 	if err != nil {
 		t.Fatal(err)
@@ -110,6 +101,9 @@ func TestOpenAtIndex(t *testing.T) {
 	if g := path.Base(w.f.Name()); g != wname {
 		t.Errorf("name = %+v, want %+v", g, wname)
 	}
+	if w.seq != 2 {
+		t.Errorf("seq = %d, want %d", w.seq, 2)
+	}
 	w.Close()
 
 	emptydir, err := ioutil.TempDir(os.TempDir(), "waltestempty")
@@ -142,7 +136,7 @@ func TestCut(t *testing.T) {
 	if err := w.Cut(); err != nil {
 		t.Fatal(err)
 	}
-	wname := fmt.Sprintf("%016x-%016x.wal", 1, 1)
+	wname := walName(1, 1)
 	if g := path.Base(w.f.Name()); g != wname {
 		t.Errorf("name = %s, want %s", g, wname)
 	}
@@ -154,7 +148,7 @@ func TestCut(t *testing.T) {
 	if err := w.Cut(); err != nil {
 		t.Fatal(err)
 	}
-	wname = fmt.Sprintf("%016x-%016x.wal", 2, 2)
+	wname = walName(2, 2)
 	if g := path.Base(w.f.Name()); g != wname {
 		t.Errorf("name = %s, want %s", g, wname)
 	}
@@ -313,7 +307,7 @@ func TestRecoverAfterCut(t *testing.T) {
 	}
 	w.Close()
 
-	if err := os.Remove(path.Join(p, "0000000000000004-0000000000000004.wal")); err != nil {
+	if err := os.Remove(path.Join(p, walName(4, 4))); err != nil {
 		t.Fatal(err)
 	}