Exemplo n.º 1
0
// reload reads back info about b from b.filename.
// b.filename should be set.
func (b *Buffer) reload() error {
	stat, err := os.Stat(b.filename)
	if err != nil {
		return err
	}
	fsize := uint64(stat.Size())
	b.capacity = fsize - metadata

	f, err := os.OpenFile(b.filename, os.O_RDWR, 0600)
	if err != nil {
		return err
	}

	data, err := syscall.Mmap(
		int(f.Fd()), 0, int(fsize),
		syscall.PROT_READ|syscall.PROT_WRITE, syscall.MAP_SHARED,
	)
	if err != nil {
		return err
	}
	b.data = data

	off := int(b.capacity)
	b.first = binary.GetLittleEndianUint64(b.data, off)
	b.last = binary.GetLittleEndianUint64(b.data, off+8)
	b.nextSeq = binary.GetLittleEndianUint64(b.data, off+16)
	b.biggest = binary.GetLittleEndianUint32(b.data, off+24)
	b.length = binary.GetLittleEndianUint64(b.data, off+28)

	return nil
}
Exemplo n.º 2
0
// size returns the size of frame encoded in the frame.
// If the complete frame was read it'd match the length.
// It returns 0 if not enough of the frame has been read
// to determine the encoded size.
func (f frame) size() uint32 {
	if len(f) < size {
		return 0
	}
	return binary.GetLittleEndianUint32(f, 0)
}