You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucy.apache.org by ma...@apache.org on 2015/10/31 22:02:47 UTC

[10/20] lucy git commit: Customize DirHandle Go bindings.

Customize DirHandle Go bindings.


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

Branch: refs/heads/LUCY-282-test-index-go-pt1
Commit: 19ce9af135eacbb991284cb58dad35297d7f384d
Parents: 5bd926f
Author: Marvin Humphrey <ma...@rectangular.com>
Authored: Mon Oct 26 21:18:51 2015 -0700
Committer: Marvin Humphrey <ma...@rectangular.com>
Committed: Mon Oct 26 21:18:51 2015 -0700

----------------------------------------------------------------------
 go/build.go           |  4 +++
 go/lucy/store.go      | 23 ++++++++++++
 go/lucy/store_test.go | 87 ++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 114 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucy/blob/19ce9af1/go/build.go
----------------------------------------------------------------------
diff --git a/go/build.go b/go/build.go
index 3921e18..902ca2b 100644
--- a/go/build.go
+++ b/go/build.go
@@ -280,6 +280,10 @@ func specClasses(parcel *cfc.Parcel) {
 	fhBinding.SpecMethod("Grow", "Grow(int64) error")
 	fhBinding.SpecMethod("Close", "Close() error")
 	fhBinding.Register()
+
+	dhBinding := cfc.NewGoClass(parcel, "Lucy::Store::DirHandle")
+	dhBinding.SpecMethod("Close", "Close() error")
+	dhBinding.Register()
 }
 
 func build() {

http://git-wip-us.apache.org/repos/asf/lucy/blob/19ce9af1/go/lucy/store.go
----------------------------------------------------------------------
diff --git a/go/lucy/store.go b/go/lucy/store.go
index e89fbbe..50b98be 100644
--- a/go/lucy/store.go
+++ b/go/lucy/store.go
@@ -23,6 +23,8 @@ package lucy
 #include "Lucy/Store/Folder.h"
 #include "Lucy/Store/InStream.h"
 #include "Lucy/Store/OutStream.h"
+#include "Lucy/Store/DirHandle.h"
+#include "Lucy/Store/FSDirHandle.h"
 #include "Lucy/Store/FileHandle.h"
 #include "Lucy/Store/FSFileHandle.h"
 #include "Lucy/Store/RAMFileHandle.h"
@@ -639,3 +641,24 @@ func OpenRAMFileHandle(path string, flags uint32, ramFile RAMFile) (fh RAMFileHa
 	})
 	return fh, err
 }
+
+func (dh *DirHandleIMP) Close() error {
+	return clownfish.TrapErr(func() {
+		self := (*C.lucy_DirHandle)(clownfish.Unwrap(dh, "dh"))
+		C.LUCY_DH_Close(self)
+	})
+}
+
+func OpenFSDirHandle(path string) (dh FSDirHandle, err error) {
+	err = clownfish.TrapErr(func() {
+		pathC := (*C.cfish_String)(clownfish.GoToClownfish(path, unsafe.Pointer(C.CFISH_STRING), false))
+		defer C.cfish_decref(unsafe.Pointer(pathC))
+		cfObj := C.lucy_FSDH_open(pathC)
+		if cfObj == nil {
+			cfErr := C.cfish_Err_get_error();
+			panic(clownfish.WRAPAny(unsafe.Pointer(C.cfish_incref(unsafe.Pointer(cfErr)))).(error))
+		}
+		dh = WRAPFSDirHandle(unsafe.Pointer(cfObj))
+	})
+	return dh, err
+}

http://git-wip-us.apache.org/repos/asf/lucy/blob/19ce9af1/go/lucy/store_test.go
----------------------------------------------------------------------
diff --git a/go/lucy/store_test.go b/go/lucy/store_test.go
index 762141b..e243e46 100644
--- a/go/lucy/store_test.go
+++ b/go/lucy/store_test.go
@@ -537,3 +537,90 @@ func TestFSFileHandleAll(t *testing.T) {
 	}
 	runFileHandleCommonTests(t, makeFH)
 }
+
+func runDirHandleCommonTests(t *testing.T, folder Folder, makeDH func() DirHandle) {
+	var err error
+	err = folder.Initialize()
+	if err != nil {
+		t.Errorf("Initialize: %v", err)
+		return
+	}
+	err = folder.MkDir("stuff")
+	if err != nil {
+		t.Errorf("MkDir: %v", err)
+		return
+	}
+	out, err := folder.OpenOut("hello")
+	if err != nil {
+		t.Errorf("OpenOut: %v", err)
+		return
+	}
+	out.Close()
+	if err != nil {
+		t.Errorf("Close OutStream: %v", err)
+		return
+	}
+
+	dh := makeDH()
+	if dh == nil {
+		t.Errorf("Failed to open DirHandle: %v", err)
+		return
+	}
+	if got := dh.GetDir(); got != folder.GetPath() {
+		t.Errorf("GetDir didn't match: '%v' '%v'", got, folder.GetPath())
+	}
+	count := 0
+	for dh.Next() {
+		count += 1
+		entry := dh.GetEntry()
+		switch entry {
+		case "hello":
+			if dh.EntryIsDir() {
+				t.Errorf("Entry should not be directory")
+			}
+			if dh.EntryIsSymlink() {
+				t.Errorf("File should not be symlink")
+			}
+		case "stuff":
+			if !dh.EntryIsDir() {
+				t.Errorf("Entry should be directory")
+			}
+			if dh.EntryIsSymlink() {
+				t.Errorf("Dir should not be symlink")
+			}
+		default:
+			t.Errorf("Unexpected entry: '%s'", entry)
+		}
+	}
+	if count != 2 {
+		t.Errorf("Didn't get to all entries, found only %d", count)
+	}
+
+	err = dh.Close()
+	if err != nil {
+		t.Errorf("Close: %v", err)
+	}
+}
+
+func TestRAMDirHandleAll(t *testing.T) {
+	folder := NewRAMFolder("myramdir")
+	makeDH := func() DirHandle {
+		return NewRAMDirHandle(folder)
+	}
+	runDirHandleCommonTests(t, folder, makeDH)
+}
+
+func TestFSDirHandleAll(t *testing.T) {
+	path := "_fsdirhandle_go_tests"
+	defer os.RemoveAll(path)
+	folder := NewFSFolder(path)
+	makeDH := func() DirHandle {
+		dh, err := OpenFSDirHandle(folder.GetPath())
+		if err != nil {
+			t.Errorf("Failed to open DirHandle: %v", err)
+			return nil
+		}
+		return dh
+	}
+	runDirHandleCommonTests(t, folder, makeDH)
+}