// The space index is a SSTable mapping grouping/index // names to their offsets in the file. func (w *spaceWriter) writeIndex(written int64, out io.Writer) (length int64, err error) { buf := new(bytes.Buffer) st := sst.NewWriter(buf) sort.Stable(w.indexNames) // For each grouping or index, we index the section's // byte offset in the file and the length in bytes // of all data in the grouping/index. for _, name := range w.indexNames { buf := new(bytes.Buffer) binary.WriteUvarint64(buf, w.indexes[name].offset) binary.WriteUvarint64(buf, w.indexes[name].length) if err = st.Set([]byte(name), buf.Bytes()); err != nil { return } } if err = st.Close(); err != nil { return } return buf.WriteTo(out) }
func (s *openStream) Close() (err error) { if s.Closed() { return } err = s.init() if err != nil { return err } // Write nil event, to signal end of events. binary.WriteInt32At(s.stream, 0, s.offset) s.offset += 4 indexes := make(sort.StringSlice, 0, len(s.tails)) for name, _ := range s.tails { indexes = append(indexes, name) } sort.Stable(indexes) buf := new(bytes.Buffer) st := sst.NewWriter(buf) // For each grouping or index, we index the section's // byte offset in the file and the length in bytes // of all data in the grouping/index. for _, name := range indexes { buf := new(bytes.Buffer) binary.WriteUvarint64(buf, s.tails[name]) if err = st.Set([]byte(name), buf.Bytes()); err != nil { return } } if err = st.Close(); err != nil { return } binary.WriteInt64(buf, int64(len(buf.Bytes()))) buf.Write([]byte(MAGIC_FOOTER)) _, err = s.stream.WriteAt(buf.Bytes(), s.offset) if err == nil { s.closed = true } if closer, ok := s.stream.(io.Closer); ok { return closer.Close() } return }
func (e *Event) encode() []byte { buf := bytes.NewBuffer([]byte{}) binary.WriteUvarint(buf, len(e.Data)) buf.Write(e.Data) binary.WriteUvarint(buf, len(e.offsets)) for name, offset := range e.offsets { binary.WriteUvarint(buf, len(name)) buf.Write([]byte(name)) binary.WriteUvarint64(buf, offset) } return buf.Bytes() }