예제 #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
파일: db.go 프로젝트: jmptrader/esdb
func (db *DB) Save() ([]byte, error) {
	buf := &bytes.Buffer{}

	binary.WriteInt64(buf, int64(db.current))
	binary.WriteInt64(buf, db.MostRecent)

	binary.WriteUvarint(buf, len(db.closed))

	for _, commit := range db.closed {
		binary.WriteInt64(buf, int64(commit))
	}

	return buf.Bytes(), nil
}
예제 #3
0
파일: writer.go 프로젝트: jmptrader/esdb
func (w *Writer) writeFooter(indexLen int64) (err error) {
	buf := new(bytes.Buffer)

	binary.WriteInt64(buf, indexLen)

	_, err = buf.WriteTo(w.file)

	return
}
예제 #4
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
}
예제 #5
0
// writes block/offset locations for all events
// associated with the given index to the file
// in timestamp descending order.
// Marks the event with which block it's located in,
// as well as the offset within the block.
func writeIndexBlocks(i *index, out io.Writer) {
	sort.Stable(sort.Reverse(i.evs))

	writer := blocks.NewWriter(out, 4096)

	for _, event := range i.evs {
		// Each entry in the index is
		// the block the event is located in,
		// and the offset within the block.
		binary.WriteInt64(writer, event.block)
		binary.WriteInt16(writer, event.offset)
	}

	// Mark the end of the grouping's events with an empty event.
	writer.Write([]byte{0})

	writer.Flush()

	i.length += int64(writer.Written)
}
예제 #6
0
func (w *spaceWriter) writeFooter(written int64, out io.Writer, indexLen int64) (length int64, err error) {
	buf := new(bytes.Buffer)
	binary.WriteInt64(buf, indexLen)
	return buf.WriteTo(out)
}