func writeGetResponse(w *bufio.Writer, key []byte, item *ybc.Item, shouldWriteCasid bool, scratchBuf *[]byte) bool { var casid uint64 var buf [casidSize + flagsSize]byte n, err := item.Read(buf[:]) if err != nil { log.Printf("error when reading item metadata: [%s]", err) return false } if n != len(buf) { log.Printf("Unexpected result returned from ybc.Item.Read(): %d. Expected %d", n, len(buf)) return false } if shouldWriteCasid { casid = binary.LittleEndian.Uint64(buf[:]) } flags := binary.LittleEndian.Uint32(buf[casidSize:]) size := item.Available() if !writeStr(w, strValue) || !writeStr(w, key) || !writeWs(w) || !writeUint32(w, flags, scratchBuf) || !writeWs(w) || !writeInt(w, size, scratchBuf) { return false } if shouldWriteCasid { if !writeWs(w) || !writeUint64(w, casid, scratchBuf) { return false } } return writeStr(w, strCrLf) && writeItem(w, item, size) }
func setItemValue(it *ybc.Item, item *Item) error { size := it.Available() item.Value = make([]byte, size) n, err := it.Read(item.Value) if err != nil { log.Fatalf("Unexpected error in Item.Read(size=%d): [%s]", size, err) } if n != size { log.Fatalf("Unexpected number of bytes read in Item.Read(size=%d): %d", size, n) } return nil }
func updateLocalItem(cache ybc.Cacher, it *ybc.Item, key []byte, casid uint64, flags, validateTtl uint32) { size := it.Available() defer it.Seek(-int64(size), 2) txn := writeItemMetadata(cache, key, size, casid, flags, validateTtl) if txn == nil { return } defer txn.Commit() n, err := txn.ReadFrom(it) if err != nil { log.Fatalf("Unexpected error in SetTxn.ReadFrom(size=%d): [%s]", size, err) } if n != int64(size) { log.Fatalf("Unexpected number of bytes copied in SetTxn.ReadFrom(size=%d): %d", size, n) } }
func writeCgetResponse(w *bufio.Writer, etag uint64, item *ybc.Item, scratchBuf *[]byte) bool { var validateTtl, flags uint32 if err := binaryRead(item, &validateTtl, "validateTtl"); err != nil { return false } if err := binaryRead(item, &flags, "flags"); err != nil { return false } size := item.Available() expiration := item.Ttl() return writeStr(w, strValue) && writeInt(w, size, scratchBuf) && writeWs(w) && writeUint32(w, flags, scratchBuf) && writeWs(w) && writeExpiration(w, expiration, scratchBuf) && writeWs(w) && writeUint64(w, etag, scratchBuf) && writeWs(w) && writeUint32(w, validateTtl, scratchBuf) && writeCrLf(w) && writeItem(w, item, size) }
func writeGetResponse(w *bufio.Writer, key []byte, item *ybc.Item, cas bool, scratchBuf *[]byte) bool { var flags uint32 if err := binaryRead(item, &flags, "flags"); err != nil { return false } size := item.Available() if !writeStr(w, strValue) || !writeStr(w, key) || !writeWs(w) || !writeUint32(w, flags, scratchBuf) || !writeWs(w) || !writeInt(w, size, scratchBuf) { return false } if cas { if !writeWs(w) || !writeZero(w) { return false } } return writeCrLf(w) && writeItem(w, item, size) }
func updateLocalItem(cache ybc.Cacher, it *ybc.Item, item *Item, etag uint64, validateTtl uint32) { size := it.Available() offset := it.Size() - size defer it.Seek(int64(offset), 0) txn := writeItemMetadata(cache, item.Key, size, it.Ttl(), etag, validateTtl, item.Flags) if txn == nil { return } defer txn.Commit() n, err := txn.ReadFrom(it) if err != nil { log.Fatalf("Unexpected error in SetTxn.ReadFrom(size=%d): [%s]", size, err) } if n != int64(size) { log.Fatalf("Unexpected number of bytes copied in SetTxn.ReadFrom(size=%d): %d", size, n) } }