You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.2 KiB

5 years ago
  1. package log_buffer
  2. import "time"
  3. type MemBuffer struct {
  4. buf []byte
  5. size int
  6. startTime time.Time
  7. stopTime time.Time
  8. }
  9. type SealedBuffers struct {
  10. buffers []*MemBuffer
  11. }
  12. func newSealedBuffers(size int) *SealedBuffers {
  13. sbs := &SealedBuffers{}
  14. sbs.buffers = make([]*MemBuffer, size)
  15. for i := 0; i < size; i++ {
  16. sbs.buffers[i] = &MemBuffer{
  17. buf: make([]byte, BufferSize),
  18. }
  19. }
  20. return sbs
  21. }
  22. func (sbs *SealedBuffers) SealBuffer(startTime, stopTime time.Time, buf []byte, pos int) (newBuf []byte) {
  23. oldMemBuffer := sbs.buffers[0]
  24. size := len(sbs.buffers)
  25. for i := 0; i < size-1; i++ {
  26. sbs.buffers[i].buf = sbs.buffers[i+1].buf
  27. sbs.buffers[i].size = sbs.buffers[i+1].size
  28. sbs.buffers[i].startTime = sbs.buffers[i+1].startTime
  29. sbs.buffers[i].stopTime = sbs.buffers[i+1].stopTime
  30. }
  31. sbs.buffers[size-1].buf = buf
  32. sbs.buffers[size-1].size = pos
  33. sbs.buffers[size-1].startTime = startTime
  34. sbs.buffers[size-1].stopTime = stopTime
  35. return oldMemBuffer.buf
  36. }
  37. func (mb *MemBuffer) locateByTs(lastReadTime time.Time) (pos int) {
  38. lastReadTs := lastReadTime.UnixNano()
  39. for pos < len(mb.buf) {
  40. size, t := readTs(mb.buf, pos)
  41. if t > lastReadTs {
  42. return
  43. }
  44. pos += size + 4
  45. }
  46. return len(mb.buf)
  47. }