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 }
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 }
func (s *Space) findGroupingOffset(name string) int64 { if val, err := s.index.Get([]byte("g" + name)); err == nil { // The entry in the SSTable index for groupings // is variable length integers integers for the // offset and length of the index within // the file. In this case, we just need the // offset to find it's starting point. return binary.ReadUvarint(bytes.NewReader(val)) } return 0 }
func (db *DB) Recovery(b []byte) error { buf := bytes.NewBuffer(b) db.setCurrent(uint64(binary.ReadInt64(buf))) db.MostRecent = binary.ReadInt64(buf) count := int(binary.ReadUvarint(buf)) for i := 0; i < count; i++ { db.addClosed(uint64(binary.ReadInt64(buf))) } return nil }
func (s *closedStream) First(name, value string) (int64, error) { index := name + ":" + value val, err := s.index.Get([]byte(index)) if err != nil { if err.Error() == "not found" { return 0, nil } else { return 0, err } } b := bytes.NewReader(val) return binary.ReadUvarint(b), nil }
// Finds and returns an index by it's name and value. func (s *Space) findIndex(name, value string) *blocks.Reader { if val, err := s.index.Get([]byte("i" + name + ":" + value)); err == nil { // The entry in the SSTable index for indexes // is variable length integers integers for the // offset and length of the index within // the file. In this case, we just need the // offset to find it's starting point. offset := binary.ReadUvarint(bytes.NewReader(val)) // An index is block encoded, so fire up // a block reader, and seek to the start // of the index. reader := blocks.NewReader(s.reader, 4096) reader.Seek(s.offset+offset, 0) return reader } return nil }
func TestWriteSpaceIndexes(t *testing.T) { w := new(bytes.Buffer) writer := newSpace(w, []byte("a")) e1data := []byte("abc") e2data := []byte("b") e3data := []byte("c") e4data := []byte("def") e1 := newEvent(e1data, 1) e2 := newEvent(e2data, 3) e3 := newEvent(e3data, 2) e4 := newEvent(e4data, 4) writer.add(e1, "1", map[string]string{"a": "1"}) writer.add(e2, "1", map[string]string{"a": "2"}) writer.add(e3, "1", map[string]string{"a": "2"}) writer.add(e4, "1", map[string]string{"a": "1"}) writer.write() var tests = []struct { key string offset int length int evs [][]byte indexed events }{ {"g1", 1, 32, [][]byte{e4data, e2data, e3data, e1data}, nil}, {"ia:1", 33, 21, nil, events{e4, e1}}, {"ia:2", 54, 23, nil, events{e2, e3}}, } sst, _ := findSpaceIndex(bytes.NewReader(w.Bytes()), 0, int64(w.Len())) for i, test := range tests { val, _ := sst.Get([]byte(test.key)) r := bytes.NewReader(val) offset := binary.ReadUvarint(r) length := binary.ReadUvarint(r) if int(offset) != test.offset || int(length) != test.length { t.Errorf("Case %d: Wrong grouping encoding: want: %d,%d found: %d,%d", i, test.offset, test.length, offset, length) } reader := blocks.NewByteReader(w.Bytes(), 4096) reader.Seek(int64(offset), 0) if len(test.evs) > 0 { for j, data := range test.evs { if e := pullEvent(reader); !reflect.DeepEqual(e.Data, data) { t.Errorf("Case %d/%d: Wrong event found: want: %s found: %s", i, j, data, e.Data) } } if e := pullEvent(reader); e != nil { t.Errorf("Wrong event found: want: nil found: %s", e.Data) } } for j, event := range test.indexed { block := binary.ReadInt64(reader) offset := binary.ReadInt16(reader) if block != event.block || int(offset) != event.offset { t.Errorf("Case %d/%d: Wrong event index: want: %d,%d found: %d,%d", i, j, event.block, event.offset, block, offset) } } } }
func TestWriteSpaceGrouping(t *testing.T) { w := new(bytes.Buffer) writer := newSpace(w, []byte("a")) e1data := []byte("abc") e2data := []byte("b") e3data := []byte("c") e4data := []byte("def") e1 := newEvent(e1data, 1) e2 := newEvent(e2data, 3) e3 := newEvent(e3data, 2) e4 := newEvent(e4data, 4) writer.add(e1, "1", nil) writer.add(e2, "2", nil) writer.add(e3, "2", nil) writer.add(e4, "1", nil) writer.write() sst, _ := findSpaceIndex(bytes.NewReader(w.Bytes()), 0, int64(w.Len())) var tests = []struct { key string offset int length int data [][]byte timestamps []int }{ {"g1", 1, 20, [][]byte{e4data, e1data}, []int{4, 1}}, {"g2", 21, 16, [][]byte{e2data, e3data}, []int{3, 2}}, } for i, test := range tests { val, _ := sst.Get([]byte(test.key)) r := bytes.NewReader(val) offset := binary.ReadUvarint(r) length := binary.ReadUvarint(r) if int(offset) != test.offset || int(length) != test.length { t.Errorf("Case %d: Wrong grouping encoding: want: %d,%d found: %d,%d", i, test.offset, test.length, offset, length) } reader := blocks.NewByteReader(w.Bytes(), 4096) reader.Seek(int64(offset), 0) for j, data := range test.data { found := pullEvent(reader) if string(found.Data) != string(data) { t.Errorf("Case %d/%d: Wrong event data found: want: %s found: %s", i, j, data, found.Data) } } reader.Seek(int64(offset), 0) for j, ts := range test.timestamps { found := pullEvent(reader) if found.Timestamp != ts { t.Errorf("Case %d/%d: Wrong event timestamp found: want: %d found: %d", i, j, ts, found.Timestamp) } } if e := pullEvent(reader); e != nil { t.Errorf("Wrong event found: want: nil found: %s", e.Data) } } }