Commits

w...@styx.org  committed c8d84a4

expose some more of the api...

  • Participants
  • Parent commits 101f85b

Comments (0)

Files changed (2)

 	C.kcdbdel(kc.db)
 }
 
+func (kc *KCDB) Path() (path string, err os.Error) {
+	cpath := C.kcdbpath(kc.db)
+	defer C.kcfree(unsafe.Pointer(cpath))
+	path = C.GoString(cpath)
+	if path == "" {
+		err = kc.error()
+	}
+	return
+}
+
+func (kc *KCDB) Status() (status string, err os.Error) {
+	cstatus := C.kcdbstatus(kc.db)
+	if cstatus == nil {
+		err = kc.error()
+	}
+	status = C.GoString(cstatus)
+	C.kcfree(unsafe.Pointer(cstatus))
+	return
+}
 func (kc *KCDB) Count() (count uint64, err os.Error) {
 	ccount := C.kcdbcount(kc.db)
 	if ccount == -1 {
 	return
 }
 
-func (kc *KCDB) Copy(dest string) (err os.Error) {
-	cdest := C.CString(dest)
-	defer C.free(unsafe.Pointer(cdest))
-	if C.kcdbcopy(kc.db, cdest) == 0 {
+func (kc *KCDB) Copy(filename string) (err os.Error) {
+	cfilename := C.CString(filename)
+	defer C.free(unsafe.Pointer(cfilename))
+	if C.kcdbcopy(kc.db, cfilename) == 0 {
+		err = kc.error()
+	}
+	return
+}
+
+func (kc *KCDB) Dump(filename string) (err os.Error) {
+	cfilename := C.CString(filename)
+	defer C.free(unsafe.Pointer(cfilename))
+	if C.kcdbdumpsnap(kc.db, cfilename) == 0 {
+		err = kc.error()
+	}
+	return
+}
+func (kc *KCDB) Load(filename string) (err os.Error) {
+	cfilename := C.CString(filename)
+	defer C.free(unsafe.Pointer(cfilename))
+	if C.kcdbcopy(kc.db, cfilename) == 0 {
 		err = kc.error()
 	}
 	return

File cabinet_test.go

 package cabinet
 
 import (
+	"fmt"
 	"testing"
 	"os"
 )
 
 var test_db string = "casket.kch"
+var test_db2 string = "casket2.kch"
+var test_dump string = "casket.dump"
 
-func newCabinet(t *testing.T) *KCDB { 
+func newCabinet(t *testing.T, filename string) *KCDB { 
 	kc := New()
-	err := kc.Open(test_db)
+	err := kc.Open(filename)
 	if err != nil {
-		t.Fatal(err)
+		t.Fatalf("Open(): %s", err)
 	}
 	return kc
 }
 
 func delCabinet(t *testing.T, kc *KCDB) {
-	err := kc.Close()
+	path, err := kc.Path()
 	if err != nil {
-		t.Fatal(err)
+		t.Fatalf("Path(): %s", err)
+	}
+	err = kc.Close()
+	if err != nil {
+		t.Fatalf("Close(): %s", err)
 	}
 	kc.Del()
-	err = os.Remove(test_db)
+	err = os.Remove(path)
 	if err != nil {
-		t.Fatal(err)
+		t.Fatal("Remove(%s): %s", path, err)
 	}
 }
 
 }
 
 func TestAdd(t *testing.T) {
-	kc := newCabinet(t)
+	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
 	addKVP(t, kc, "hello", "world")
 	getKVP(t, kc, "hello", "world")
 	getKVP(t, kc, "bit", "bucket")
 	getKVP(t, kc, "foo", "bar")
+
+	status, err := kc.Status()
+	if err != nil {
+		t.Errorf("Status(): %s", err)
+	}
+	path, err := kc.Path()
+	if err != nil {
+		t.Errorf("Path(): %s", err)
+	}
+	count, err := kc.Count()
+	if err != nil {
+		t.Errorf("Count(): %s", err)
+	}
+	size, err := kc.Size()
+	if err != nil {
+		t.Errorf("Size(): %s", err)
+	}
+	fmt.Printf("%s %d bytes %d records\n%s", path, size, count, status)
 }
 
 func TestSet(t *testing.T) {
-	kc := newCabinet(t)
+	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
 	addKVP(t, kc, "hello", "world")
 }
 
 func TestReplace(t *testing.T) {
-	kc := newCabinet(t)
+	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
 	err := kc.Replace("foo", "bar")
 }
 
 func TestAppend(t *testing.T) {
-	kc := newCabinet(t)
+	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
 	addKVP(t, kc, "numbers", "one")
 }
 
 func TestIterators(t *testing.T) {
-	kc := newCabinet(t)
+	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
 	addKVP(t, kc, "hello", "world")
 }
 
 func TestClear(t *testing.T) {
-	kc := newCabinet(t)
+	kc := newCabinet(t, test_db)
 	defer delCabinet(t, kc)
 
 	addKVP(t, kc, "hello", "world")
 	if count != uint64(0) {
 		t.Errorf("Count() after Clear() expected %d got %d", 0, count)
 	}
+}
+
+func TestCopy(t *testing.T) {
+	kc := newCabinet(t, test_db)
+	defer delCabinet(t, kc)
+
+	addKVP(t, kc, "hello", "world")
+	addKVP(t, kc, "bit", "bucket")
+	addKVP(t, kc, "foo", "bar")
+
+	err := kc.Copy(test_db2)
+	if err != nil {
+		t.Fatalf("Copy(%s): %s", test_db2, err)
+	}
+
+	kc2 := newCabinet(t, test_db2)
+	defer delCabinet(t, kc2)
+
+	count, _ := kc.Count()
+	count2, _ := kc2.Count()
+	if count != count2 {
+		t.Errorf("Count() (copy) expected %d got %d", count, count2)
+	}
+}
+
+func TestDump(t *testing.T) {
+	return // broken??
+
+	kc := newCabinet(t, test_db)
+	defer delCabinet(t, kc)
+	kc2 := newCabinet(t, test_db2)
+	defer delCabinet(t, kc2)
+
+	addKVP(t, kc, "hello", "world")
+	addKVP(t, kc, "bit", "bucket")
+	addKVP(t, kc, "foo", "bar")
+
+	err := kc.Dump(test_dump)
+	if err != nil {
+		t.Fatalf("Dump(%s): %s", test_dump, err)
+	}
+
+	err = kc.Load(test_dump)
+	if err != nil {
+		t.Fatalf("Load(%s): %s", test_dump, err)
+	}
+
+	count, _ := kc.Count()
+	count2, _ := kc2.Count()
+	if count != count2 {
+		t.Errorf("Count() (copy) expected %d got %d", count, count2)
+	}
 }