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"
// "log"
"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 {
return
}
data := splitByteArray(job.Data, '\x00', 5)
data := bytes.Split(job.Data, []byte{'\x00'}, 5)
if len(data) != 5 {
err = os.NewError("Data Error.")
return

View File

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

View File

@ -10,6 +10,7 @@ The protocol was implemented by native way.
package gearman
import (
"bytes"
"os"
)
@ -84,31 +85,9 @@ type Job interface {
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
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 {
err = os.NewError("The input is not a error data.")
return

View File

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