splitByteArray -> bytes.Split, fixed bug in the process.

This commit is contained in:
Jonathan Wills 2011-05-29 01:54:02 -04:00
parent 28ac570cbc
commit 22866616cd
4 changed files with 9 additions and 27 deletions

View File

@ -10,6 +10,7 @@ import (
"sync" "sync"
// "log" // "log"
"strconv" "strconv"
"bytes"
) )
/* /*
@ -204,7 +205,7 @@ func (client *Client) Status(handle string) (known, running bool, numerator, den
if job, err = client.readLastJob(STATUS_RES); err != nil { if job, err = client.readLastJob(STATUS_RES); err != nil {
return return
} }
data := splitByteArray(job.Data, '\x00', 5) data := bytes.Split(job.Data, []byte{'\x00'}, 5)
if len(data) != 5 { if len(data) != 5 {
err = os.NewError("Data Error.") err = os.NewError("Data Error.")
return return

View File

@ -6,6 +6,7 @@ package gearman
import ( import (
"os" "os"
"bytes"
// "log" // "log"
) )
@ -65,7 +66,7 @@ func (job *ClientJob) Result() (data []byte, err os.Error) {
err = os.NewError("Work exception.") err = os.NewError("Work exception.")
fallthrough fallthrough
case WORK_COMPLETE: case WORK_COMPLETE:
s := splitByteArray(job.Data, '\x00', 2) s := bytes.Split(job.Data, []byte{'\x00'}, 2)
if len(s) != 2 { if len(s) != 2 {
err = os.NewError("Invalid data.") err = os.NewError("Invalid data.")
return return
@ -84,7 +85,7 @@ func (job *ClientJob) Update() (data []byte, err os.Error) {
err = os.NewError("The job is not a update.") err = os.NewError("The job is not a update.")
return return
} }
s := splitByteArray(job.Data, '\x00', 2) s := bytes.Split(job.Data, []byte{'\x00'}, 2)
if len(s) != 2 { if len(s) != 2 {
err = os.NewError("Invalid data.") err = os.NewError("Invalid data.")
return return

View File

@ -10,6 +10,7 @@ The protocol was implemented by native way.
package gearman package gearman
import ( import (
"bytes"
"os" "os"
) )
@ -84,31 +85,9 @@ type Job interface {
Encode() []byte Encode() []byte
} }
// Splite the byte array by a byte
func splitByteArray(slice []byte, spot byte, limit int) (data [][]byte) {
data = make([][]byte, 0, 10)
start, end := 0, 0
for i, v := range slice {
if v == spot {
if start != end {
data = append(data, slice[start:end])
if (limit > 0) && (limit - 1 == len(data)) {
start = end
break
}
}
start, end = i+1, i+1
} else {
end++
}
}
data = append(data, slice[start:])
return
}
// Extract the error message // Extract the error message
func getError(data []byte) (eno os.Errno, err os.Error) { func getError(data []byte) (eno os.Errno, err os.Error) {
rel := splitByteArray(data, '\x00', 2) rel := bytes.Split(data, []byte{'\x00'}, 2)
if len(rel) != 2 { if len(rel) != 2 {
err = os.NewError("The input is not a error data.") err = os.NewError("The input is not a error data.")
return return

View File

@ -7,6 +7,7 @@ package gearman
import ( import (
"os" "os"
"sync" "sync"
"bytes"
// "log" // "log"
) )
@ -223,7 +224,7 @@ func (worker *Worker) exec(job *WorkerJob) (err os.Error) {
} else { } else {
limit = 4 limit = 4
} }
jobdata := splitByteArray(job.Data, '\x00', limit) jobdata := bytes.Split(job.Data, []byte{'\x00'}, limit)
job.Handle = string(jobdata[0]) job.Handle = string(jobdata[0])
funcname := string(jobdata[1]) funcname := string(jobdata[1])
if job.DataType == JOB_ASSIGN { if job.DataType == JOB_ASSIGN {