Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:derekchiang/hypergo
- Loading branch information
Showing
6 changed files
with
322 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package hypergo | ||
|
||
/* | ||
#cgo LDFLAGS: -lhyperdex-admin | ||
#include "hyperdex/admin.h" | ||
*/ | ||
import "C" | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// Admin APIs | ||
func (admin *Admin) DumpConfig() string { | ||
return <-admin.AsyncDumpConfig() | ||
} | ||
|
||
func (admin *Admin) AsyncDumpConfig() chan string { | ||
return admin.AsyncDumpConfigOrListSpaces("dump_config") | ||
} | ||
|
||
func (admin *Admin) ListSpaces() []string { | ||
c := admin.AsyncListSpaces() | ||
strs := make([]string, 0) | ||
for { | ||
s, ok := <-c | ||
if ok { | ||
if len(s) > 0 { | ||
strs = append(strs, s) | ||
} | ||
} else { | ||
break | ||
} | ||
} | ||
|
||
return strs | ||
} | ||
|
||
func (admin *Admin) AsyncListSpaces() chan string { | ||
return admin.AsyncDumpConfigOrListSpaces("list_spaces") | ||
} | ||
|
||
func (admin *Admin) AsyncDumpConfigOrListSpaces(funcName string) chan string { | ||
c := make(chan string, CHANNEL_BUFFER_SIZE) | ||
var C_return_code C.enum_hyperdex_admin_returncode | ||
var C_string *C.char | ||
var req_id int64 | ||
var success func() | ||
|
||
switch funcName { | ||
case "dump_config": | ||
req_id = int64(C.hyperdex_admin_dump_config(admin.ptr, &C_return_code, &C_string)) | ||
success = func() { | ||
c <- C.GoString(C_string) | ||
} | ||
case "list_spaces": | ||
req_id = int64(C.hyperdex_admin_list_spaces(admin.ptr, &C_return_code, &C_string)) | ||
success = func() { | ||
spaces := C.GoString(C_string) | ||
for _, s := range strings.Split(spaces, "\n") { | ||
c <- s | ||
} | ||
close(c) | ||
} | ||
} | ||
|
||
if req_id < 0 { | ||
panic(fmt.Sprintf("%s failed", funcName)) | ||
} | ||
|
||
req := adminRequest{ | ||
id: req_id, | ||
status: &C_return_code, | ||
success: success, | ||
} | ||
|
||
admin.requests = append(admin.requests, req) | ||
return c | ||
} | ||
|
||
func (admin *Admin) ValidateSpace(description string) bool { | ||
var C_return_code C.enum_hyperdex_admin_returncode | ||
|
||
valid := C.hyperdex_admin_validate_space(admin.ptr, | ||
C.CString(description), &C_return_code) | ||
|
||
if valid < 0 { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} | ||
|
||
func (admin *Admin) AddSpace(description string) error { | ||
return <-admin.AsyncAddSpace(description) | ||
} | ||
|
||
func (admin *Admin) AsyncAddSpace(description string) ErrorChannel { | ||
return admin.AsyncAddOrRemoveSpace(description, "add") | ||
} | ||
|
||
func (admin *Admin) RemoveSpace(description string) error { | ||
return <-admin.AsyncRemoveSpace(description) | ||
} | ||
|
||
func (admin *Admin) AsyncRemoveSpace(description string) ErrorChannel { | ||
return admin.AsyncAddOrRemoveSpace(description, "remove") | ||
} | ||
|
||
func (admin *Admin) AsyncAddOrRemoveSpace(description string, funcName string) ErrorChannel { | ||
var C_return_code C.enum_hyperdex_admin_returncode | ||
errCh := make(chan error, CHANNEL_BUFFER_SIZE) | ||
var req_id int64 | ||
switch funcName { | ||
case "add": | ||
req_id = int64(C.hyperdex_admin_add_space(admin.ptr, | ||
C.CString(description), &C_return_code)) | ||
case "remove": | ||
req_id = int64(C.hyperdex_admin_rm_space(admin.ptr, | ||
C.CString(description), &C_return_code)) | ||
} | ||
|
||
req := adminRequest{ | ||
id: req_id, | ||
status: &C_return_code, | ||
success: func() { | ||
errCh <- nil | ||
}, | ||
failure: errChannelFailureCallbackForAdmin(errCh), | ||
} | ||
|
||
admin.requests = append(admin.requests, req) | ||
|
||
return errCh | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package hypergo | ||
|
||
import "testing" | ||
|
||
func TestDumpConfig(t *testing.T) { | ||
admin, err := NewAdmin("127.0.0.1", 1982) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer admin.Destroy() | ||
|
||
println(admin.DumpConfig()) | ||
} | ||
|
||
func TestValidateSpace(t *testing.T) { | ||
admin, err := NewAdmin("127.0.0.1", 1982) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer admin.Destroy() | ||
|
||
validSpace := ` | ||
space profiles | ||
key username | ||
attributes | ||
string name, | ||
float height, | ||
int profile_views, | ||
list(string) pending_requests, | ||
list(float) ratings, | ||
set(string) hobbies, | ||
set(int) ages, | ||
map(string, string) unread_messages, | ||
map(string, int) upvotes | ||
subspace name | ||
subspace height | ||
subspace profile_views | ||
` | ||
if !(admin.ValidateSpace(validSpace) == true) { | ||
t.Fatalf("The following space schema is valid, "+ | ||
"but ValidateSpace says otherwise: %s", validSpace) | ||
} | ||
|
||
// Note that the fourth line starts with "strings", which is invalid | ||
invalidSpace := ` | ||
space profiles | ||
key username | ||
attributes | ||
strings name, | ||
float height, | ||
int profile_views, | ||
list(string) pending_requests, | ||
list(float) ratings, | ||
set(string) hobbies, | ||
set(int) ages, | ||
map(string, string) unread_messages, | ||
map(string, int) upvotes | ||
subspace name | ||
subspace height | ||
subspace profile_views | ||
` | ||
if !(admin.ValidateSpace(invalidSpace) == false) { | ||
t.Fatalf("The following space schema is invalid, "+ | ||
"but ValidateSpace says otherwise: %s", validSpace) | ||
} | ||
} | ||
|
||
func TestListSpaces(t *testing.T) { | ||
admin, err := NewAdmin("127.0.0.1", 1982) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer admin.Destroy() | ||
for _, space := range admin.ListSpaces() { | ||
println(space) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
package hypergo | ||
|
||
// User-facing APIs | ||
// Data-manipulating APIs | ||
|
||
/* | ||
#cgo LDFLAGS: -lhyperdex-client | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.