Custom Go bindings for Lock.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/bda80a7a Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/bda80a7a Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/bda80a7a Branch: refs/heads/master Commit: bda80a7aebecaf716ba9b3145a5687d8989ec8b4 Parents: 19ce9af Author: Marvin Humphrey <[email protected]> Authored: Tue Oct 27 19:24:51 2015 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Tue Oct 27 19:36:59 2015 -0700 ---------------------------------------------------------------------- go/build.go | 7 ++++++ go/lucy/store.go | 35 ++++++++++++++++++++++++++ go/lucy/store_test.go | 61 ++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/bda80a7a/go/build.go ---------------------------------------------------------------------- diff --git a/go/build.go b/go/build.go index 902ca2b..1d429ff 100644 --- a/go/build.go +++ b/go/build.go @@ -284,6 +284,13 @@ func specClasses(parcel *cfc.Parcel) { dhBinding := cfc.NewGoClass(parcel, "Lucy::Store::DirHandle") dhBinding.SpecMethod("Close", "Close() error") dhBinding.Register() + + lockBinding := cfc.NewGoClass(parcel, "Lucy::Store::Lock") + lockBinding.SpecMethod("Request", "Request() error") + lockBinding.SpecMethod("Obtain", "Obtain() error") + lockBinding.SpecMethod("Release", "Release() error") + lockBinding.SpecMethod("Clear_Stale", "ClearStale() error") + lockBinding.Register() } func build() { http://git-wip-us.apache.org/repos/asf/lucy/blob/bda80a7a/go/lucy/store.go ---------------------------------------------------------------------- diff --git a/go/lucy/store.go b/go/lucy/store.go index 50b98be..05d6dc3 100644 --- a/go/lucy/store.go +++ b/go/lucy/store.go @@ -662,3 +662,38 @@ func OpenFSDirHandle(path string) (dh FSDirHandle, err error) { }) return dh, err } + +func (lock *LockIMP) Request() error { + self := (*C.lucy_Lock)(clownfish.Unwrap(lock, "lock")) + success := C.LUCY_Lock_Request(self) + if !success { + cfErr := C.cfish_Err_get_error(); + return clownfish.WRAPAny(unsafe.Pointer(C.cfish_incref(unsafe.Pointer(cfErr)))).(error) + } + return nil +} + +func (lock *LockIMP) Obtain() error { + self := (*C.lucy_Lock)(clownfish.Unwrap(lock, "lock")) + success := C.LUCY_Lock_Obtain(self) + if !success { + cfErr := C.cfish_Err_get_error(); + return clownfish.WRAPAny(unsafe.Pointer(C.cfish_incref(unsafe.Pointer(cfErr)))).(error) + } + return nil +} + +func (lock *LockIMP) Release() error { + return clownfish.TrapErr(func() { + self := (*C.lucy_Lock)(clownfish.Unwrap(lock, "lock")) + C.LUCY_Lock_Release(self) + }) +} + + +func (lock *LockIMP) ClearStale() error { + return clownfish.TrapErr(func() { + self := (*C.lucy_Lock)(clownfish.Unwrap(lock, "lock")) + C.LUCY_Lock_Clear_Stale(self) + }) +} http://git-wip-us.apache.org/repos/asf/lucy/blob/bda80a7a/go/lucy/store_test.go ---------------------------------------------------------------------- diff --git a/go/lucy/store_test.go b/go/lucy/store_test.go index e243e46..fb2fdc7 100644 --- a/go/lucy/store_test.go +++ b/go/lucy/store_test.go @@ -624,3 +624,64 @@ func TestFSDirHandleAll(t *testing.T) { } runDirHandleCommonTests(t, folder, makeDH) } + +func runLockCommonTests(t *testing.T, makeLock func(string, string) Lock) { + var err error + lock := makeLock("foo", "dev.example.com") + other := makeLock("foo", "dev.example.com") + + if got := lock.GetName(); got != "foo" { + t.Errorf("GetName: %v", got) + } + if got := lock.GetHost(); got != "dev.example.com" { + t.Errorf("GetHost: %v", got) + } + + err = lock.Request() + if err != nil { + t.Errorf("Request: %v", err) + } + if !lock.IsLocked() { + t.Errorf("Lock should be locked, but IsLocked returned false") + } + if got := lock.GetLockPath(); len(got) == 0 { + // Lock path only valid when locked for shared locks. + t.Errorf("GetLockPath should work") + } + err = other.Request() + if other.Shared() && err != nil { + t.Errorf("SharedLock Request should succeed: %v", err) + } else if !other.Shared() && err == nil { + t.Errorf("Request should fail for locked resource") + } + err = lock.Release() + if err != nil { + t.Errorf("Request: %v", err) + } + other.Release() + err = lock.Obtain() + if err != nil { + t.Errorf("Obtain: %v", err) + } + + err = lock.ClearStale() + if err != nil { + t.Errorf("Nothing for ClearStale to do, but should still suceed: %v", err) + } +} + +func TestLockFileLockAll(t *testing.T) { + folder := NewRAMFolder("myindex") + makeLock := func(name, host string) Lock { + return NewLockFileLock(folder, name, host, 0, 1) + } + runLockCommonTests(t, makeLock) +} + +func TestSharedLockAll(t *testing.T) { + folder := NewRAMFolder("myindex") + makeLock := func(name, host string) Lock { + return NewSharedLock(folder, name, host, 0, 1) + } + runLockCommonTests(t, makeLock) +}
