Skip to content

Commit

Permalink
Prefix matching on frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamcha committed Sep 26, 2014
1 parent 3e050e9 commit ef54951
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
2 changes: 2 additions & 0 deletions frontend/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ func handler(w http.ResponseWriter, r *http.Request) {
err = httpInfo(w, operation)
case OpDelete:
err = httpDelete(w, operation)
case OpPrefixMatch:
err = httpMatch(w, operation)
default:
err = &HTTPError{Message: "I don't get what you're trying to do", Code: 400}
}
Expand Down
5 changes: 3 additions & 2 deletions frontend/goleg/highlevel.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ func (d Database) Squish() bool {
return CSquish(d.db) == 1
}

func (d Database) PrefixMatch(prefix string) []string {
return CPrefixMatch(d.db, prefix, uintptr(len(prefix)))
func (d Database) PrefixMatch(prefix string) (bool, []string) {
len, out := CPrefixMatch(d.db, prefix, uintptr(len(prefix)))
return len >= 0, out

This comment has been minimized.

Copy link
@kyleterry

kyleterry Sep 26, 2014

Member

Go is great

}
15 changes: 11 additions & 4 deletions frontend/goleg/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,27 +188,34 @@ func CCas(db *C.ol_database, key string, klen uintptr, value []byte, vsize uintp
return int(C.ol_cas(db, ckey, cklen, cvalue, cvsize, covalue, covsize))
}

func CPrefixMatch(db *C.ol_database, prefix string, plen uintptr) []string {
func CPrefixMatch(db *C.ol_database, prefix string, plen uintptr) (int, []string) {
// Turn parameters into their C counterparts
cprefix := C.CString(prefix)
defer C.free(unsafe.Pointer(cprefix))

cplen := (C.size_t)(plen)

var ptr *C.ol_val_array
length := int(C.ol_prefix_match(db, cprefix, cplen, ptr))
// Call native function
var ptr C.ol_val_array
length := int(C.ol_prefix_match(db, cprefix, cplen, &ptr))
if length < 0 {
return length, nil
}

// Set array structure
hdr := reflect.SliceHeader{
Data: uintptr(unsafe.Pointer(ptr)),
Len: length,
Cap: length,
}
strSlice := *(*[]*C.char)(unsafe.Pointer(&hdr))
// Create GoString array
out := make([]string, 0)
for i := range strSlice {
out = append(out, C.GoString(strSlice[i]))
C.free(unsafe.Pointer(strSlice[i]))
}
// Free structure
C.free(unsafe.Pointer(ptr))
return out
return length, out
}
16 changes: 15 additions & 1 deletion frontend/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io/ioutil"
"net/http"
"strconv"
"strings"
"time"
)

Expand All @@ -17,7 +18,7 @@ const (
OpCursorLast = "_last"
OpCursorNext = "._next"
OpCursorPrev = "._prev"
OpPrefixMatch = "_match"
OpPrefixMatch = "._match"
)

func httpGet(w http.ResponseWriter, op Operation) *HTTPError {
Expand Down Expand Up @@ -97,3 +98,16 @@ func httpDelete(w http.ResponseWriter, op Operation) *HTTPError {
fmt.Fprintf(w, "Key deleted successfully!")
return nil
}

func httpMatch(w http.ResponseWriter, op Operation) *HTTPError {
has, res := op.Database.PrefixMatch(op.Key)
if !has {
return &HTTPError{Code: 500, Message: "Something went horribly wrong..."}
}
if len(res) == 0 {
return &HTTPError{Code: 404, Message: "No matches found"}
}

fmt.Fprintf(w, strings.Join(res, "\n"))
return nil
}

0 comments on commit ef54951

Please sign in to comment.