tidwall / redcon

Redis compatible server framework for Go
MIT License
2.16k stars 159 forks source link

is there any possibility of returning WriteBulk without pre specifying how many to return? #44

Open hiqsociety opened 3 years ago

hiqsociety commented 3 years ago

if i'm getting data from db, is this possible without pre specifying with WriteArray() first?

for it.Seek(); it.Valid(); it.Next() { conn.WriteBulk([]byte, it.Key.Data()) }

tidwall commented 2 years ago

No. You will need to call WriteArray, which writes the number of items in the array first. If you don't know how many items there will be then you can buffer them like:


// buffer and count the items
var count int
var buf bufio.Buffer
w := redcon.NewWriter(&buf)
for it.Seek(); it.Valid(); it.Next() {
    w.WriteBulk(it.Key.Data())
    count++
}

// write the array header and the buffered items.
conn.WriteArray(count)
conn.WriteRaw(buf.Bytes())

Then wr