Tune and test Go bindings for Snapshot.

Project: http://git-wip-us.apache.org/repos/asf/lucy/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/a8bb3b5e
Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/a8bb3b5e
Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/a8bb3b5e

Branch: refs/heads/master
Commit: a8bb3b5e03d3a28996705488ecbdbf54b4206f4e
Parents: 8eefef8
Author: Marvin Humphrey <[email protected]>
Authored: Sat Oct 31 15:12:23 2015 -0700
Committer: Marvin Humphrey <[email protected]>
Committed: Sun Nov 1 10:00:35 2015 -0800

----------------------------------------------------------------------
 go/build.go           |  6 ++++++
 go/lucy/index.go      | 29 +++++++++++++++++++++++++++++
 go/lucy/index_test.go | 30 ++++++++++++++++++++++++++++++
 3 files changed, 65 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/a8bb3b5e/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index 75fe5b5..3b0a95d 100644
--- a/go/build.go
+++ b/go/build.go
@@ -177,6 +177,12 @@ func specClasses(parcel *cfc.Parcel) {
        tvBinding.SetSuppressCtor(true)
        tvBinding.Register()
 
+       snapshotBinding := cfc.NewGoClass(parcel, "Lucy::Index::Snapshot")
+       snapshotBinding.SpecMethod("List", "List() []string")
+       snapshotBinding.SpecMethod("Read_File", "ReadFile(Folder, string) 
(Snapshot, error)")
+       snapshotBinding.SpecMethod("Write_File", "WriteFile(Folder, string) 
error")
+       snapshotBinding.Register()
+
        schemaBinding := cfc.NewGoClass(parcel, "Lucy::Plan::Schema")
        schemaBinding.SpecMethod("All_Fields", "AllFields() []string")
        schemaBinding.Register()

http://git-wip-us.apache.org/repos/asf/lucy/blob/a8bb3b5e/go/lucy/index.go
----------------------------------------------------------------------
diff --git a/go/lucy/index.go b/go/lucy/index.go
index 43dc90c..18a9992 100644
--- a/go/lucy/index.go
+++ b/go/lucy/index.go
@@ -21,6 +21,7 @@ package lucy
 #include "Lucy/Index/IndexManager.h"
 #include "Lucy/Index/BackgroundMerger.h"
 #include "Lucy/Index/TermVector.h"
+#include "Lucy/Index/Snapshot.h"
 #include "Lucy/Document/Doc.h"
 #include "Lucy/Plan/Schema.h"
 #include "Clownfish/Hash.h"
@@ -338,3 +339,31 @@ func (tv *TermVectorIMP) GetEndOffsets() []int32 {
        self := (*C.lucy_TermVector)(clownfish.Unwrap(tv, "tv"))
        return i32ArrayToSlice(C.LUCY_TV_Get_End_Offsets(self))
 }
+
+func (s *SnapshotIMP) List() []string {
+       self := (*C.lucy_Snapshot)(clownfish.Unwrap(s, "s"))
+       retvalC := C.LUCY_Snapshot_List(self)
+       defer C.cfish_decref(unsafe.Pointer(retvalC))
+       return vecToStringSlice(retvalC)
+}
+
+func (s *SnapshotIMP) ReadFile(folder Folder, path string) (Snapshot, error) {
+       err := clownfish.TrapErr(func() {
+               self := (*C.lucy_Snapshot)(clownfish.Unwrap(s, "s"))
+               folderC := (*C.lucy_Folder)(clownfish.Unwrap(folder, "folder"))
+               pathC := (*C.cfish_String)(clownfish.GoToClownfish(path, 
unsafe.Pointer(C.CFISH_STRING), false))
+               defer C.cfish_decref(unsafe.Pointer(pathC))
+               C.LUCY_Snapshot_Read_File(self, folderC, pathC)
+       })
+       return s, err
+}
+
+func (s *SnapshotIMP) WriteFile(folder Folder, path string) error {
+       return clownfish.TrapErr(func() {
+               self := (*C.lucy_Snapshot)(clownfish.Unwrap(s, "s"))
+               folderC := (*C.lucy_Folder)(clownfish.Unwrap(folder, "folder"))
+               pathC := (*C.cfish_String)(clownfish.GoToClownfish(path, 
unsafe.Pointer(C.CFISH_STRING), false))
+               defer C.cfish_decref(unsafe.Pointer(pathC))
+               C.LUCY_Snapshot_Write_File(self, folderC, pathC)
+       })
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/a8bb3b5e/go/lucy/index_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/index_test.go b/go/lucy/index_test.go
index bdfe05a..c4e70f9 100644
--- a/go/lucy/index_test.go
+++ b/go/lucy/index_test.go
@@ -323,3 +323,33 @@ func TestTermVectorMisc(t *testing.T) {
                t.Errorf("Unsuccessful serialization round trip")
        }
 }
+
+func TestSnapshotMisc(t *testing.T) {
+       var err error
+       snapshot := NewSnapshot()
+       snapshot.AddEntry("foo")
+       snapshot.AddEntry("bar")
+       snapshot.DeleteEntry("bar")
+       if got := snapshot.NumEntries(); got != 1 {
+               t.Errorf("Add/DeleteEntry, NumEntries: %d", got)
+       }
+       if got := snapshot.List(); !reflect.DeepEqual(got, []string{"foo"}) {
+               t.Errorf("List: %v", got)
+       }
+       folder := NewRAMFolder("")
+       err = snapshot.WriteFile(folder, "")
+       if err != nil {
+               t.Errorf("WriteFile: %v", err)
+       }
+       other := NewSnapshot()
+       _, err = other.ReadFile(folder, "")
+       if err != nil {
+               t.Errorf("ReadFile: %v", err)
+       }
+
+       path := "snapshot_4.json"
+       snapshot.SetPath(path)
+       if got := snapshot.GetPath(); got != path {
+               t.Errorf("SetPath/GetPath: %v", path)
+       }
+}

Reply via email to