예제 #1
0
파일: writer.go 프로젝트: jmptrader/esdb
// The ESDB index is a SSTable mapping space
// ids to their offsets in the file.
func (w *Writer) writeIndex() (int64, error) {
	w.written = true

	buf := new(bytes.Buffer)
	st := sst.NewWriter(buf)

	w.spaceIds.Sort()

	// For each defined space, we index the space's
	// byte offset in the file and the length in bytes
	// of all data in the space.
	for _, spaceId := range w.spaceIds {
		b := new(bytes.Buffer)

		binary.WriteInt64(b, w.spaceOffsets[spaceId])
		binary.WriteInt64(b, w.spaceLengths[spaceId])

		if err := st.Set([]byte(spaceId), b.Bytes()); err != nil {
			return 0, err
		}
	}

	if err := st.Close(); err != nil {
		return 0, err
	}

	return buf.WriteTo(w.file)
}
예제 #2
0
// 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)
}
예제 #3
0
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
}