// Write atomically writes one or more buffers to the WAL.
Write atomically writes one or more buffers to the WAL.
// Write atomically writes one or more buffers to the WAL .
func (wal *WAL) write(bufs ...[]byte) (int, error) {
	wal.mx.Lock()
	defer wal.mx.Unlock()
	length := 0
	for _, b := range bufs {
		blen := len(b)
		if blen > maxEntrySize {
			fmt.Printf("Ignoring wal entry of size %v exceeding %v", humanize.Bytes(uint64(blen)), humanize.Bytes(uint64(maxEntrySize)))
			return 0, nil
		}
		length += blen
	}
	if length == 0 {
		return 0, nil
	}
	wal.h.Reset()
	for _, buf := range bufs {
		wal.h.Write(buf)
	}
	headerBuf := make([]byte, 4)
	encoding.PutUint32(headerBuf, uint32(length))
	n, err := wal.writer.Write(headerBuf)
	wal.position += int64(n)
	if err != nil {
		return 0, err
	}
	encoding.PutUint32(headerBuf, wal.h.Sum32())
	n, err = wal.writer.Write(headerBuf)
	wal.position += int64(n)
	if err != nil {
		return 0, err
	}
	for _, b := range bufs {
		n, err = wal.writer.Write(b)
		if err != nil {
			return 0, err
		}
		wal.position += int64(n)
	}
	if wal.syncImmediate {
		wal.doSync()
	}
	if wal.position >= maxSegmentSize {
		_, err = wal.writer.Write(sentinelBytes)
		if err != nil {
			return 0, err
		}
		err = wal.writer.Flush()
		if err != nil {
			return 0, err
		}
		err = wal.advance()
		if err != nil {
			return n, fmt.Errorf("Unable to advance to next file: %v", err)
		}
	}
	return n, nil
}
func ( wal * WAL ) write ( bufs ... [ ] byte ) ( int , error ) { wal . mx . Lock ( ) defer wal . mx . Unlock ( ) length := 0 for _ , b := range bufs { blen := len ( b ) if blen > maxEntrySize { fmt . Printf ( " " , humanize . Bytes ( uint64 ( blen ) ) , humanize . Bytes ( uint64 ( maxEntrySize ) ) ) return 0 , nil } length += blen } if length == 0 { return 0 , nil } wal . h . Reset ( ) for _ , buf := range bufs { wal . h . Write ( buf ) } headerBuf := make ( [ ] byte , 4 ) encoding . PutUint32 ( headerBuf , uint32 ( length ) ) n , err := wal . writer . Write ( headerBuf ) wal . position += int64 ( n ) if err != nil { return 0 , err } encoding . PutUint32 ( headerBuf , wal . h . Sum32 ( ) ) n , err = wal . writer . Write ( headerBuf ) wal . position += int64 ( n ) if err != nil { return 0 , err } for _ , b := range bufs { n , err = wal . writer . Write ( b ) if err != nil { return 0 , err } wal . position += int64 ( n ) } if wal . syncImmediate { wal . doSync ( ) } if wal . position >= maxSegmentSize { _ , err = wal . writer . Write ( sentinelBytes ) if err != nil { return 0 , err } err = wal . writer . Flush ( ) if err != nil { return 0 , err } err = wal . advance ( ) if err != nil { return n , fmt . Errorf ( " " , err ) } } return n , nil }
Write atomically writes one or more buffers to the WAL.
Write atomically writes one or more buffers to the WAL .
// Write length // Write checksum // Write sentinel length to mark end of file