Exemple #1
0
func (s *Space) ScanIndex(name, value string, scanner Scanner) {
	if reader := s.findIndex(name, value); reader != nil {
		for {
			// If the next byte is 0, then that's an empty event offset,
			// marking the end of the index. Nothing more to see here.
			if next := reader.Peek(1); len(next) == 0 || next[0] == 0 {
				return
			}

			// Each entry in the index is a 64 bit integer for the
			// event's block offset in the file, and a 16 bit integer
			// for the event's offset within the block (as each block
			// is 4096 bytes long)
			block := binary.ReadInt64(reader)
			offset := binary.ReadInt16(reader)

			// Move to the event's block
			s.blocks.Seek(s.offset+block, 0)

			// Read all data prior to the current event's offset.
			binary.ReadBytes(s.blocks, offset)

			event := pullEvent(s.blocks)

			if event == nil || !scanner(event) {
				return
			}
		}
	}
}
Exemple #2
0
func decodeEvent(b []byte) (*Event, error) {
	buf := bytes.NewBuffer(b)

	size := binary.ReadUvarint(buf)
	data := binary.ReadBytes(buf, size)

	numOffsets := int(binary.ReadUvarint(buf))

	offsets := make(map[string]int64)

	for i := 0; i < numOffsets; i++ {
		length := binary.ReadUvarint(buf)
		name := string(binary.ReadBytes(buf, length))
		offset := binary.ReadUvarint(buf)

		offsets[name] = offset
	}

	return NewEvent(data, offsets), nil
}
Exemple #3
0
func pullEvent(r *blocks.Reader) (e *Event) {
	size := binary.ReadUvarint(r)
	timestamp := int(binary.ReadInt32(r))
	data := binary.ReadBytes(r, size)

	if len(data) > 0 {
		e = &Event{Data: data, Timestamp: timestamp}
	}

	return
}
Exemple #4
0
func readonly(path string) (Stream, error) {
	file, err := os.Open(path)
	if err != nil {
		return nil, err
	}

	header := binary.ReadBytes(file, int64(len(MAGIC_HEADER)))

	if string(header) != string(MAGIC_HEADER) {
		return nil, CORRUPTED_HEADER
	}

	return newClosedStream(file)
}