本站源代码
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

84 lines
2.1KB

  1. package roaring
  2. import (
  3. "encoding/binary"
  4. "errors"
  5. "fmt"
  6. "io"
  7. "github.com/tinylib/msgp/msgp"
  8. )
  9. // writeTo for runContainer16 follows this
  10. // spec: https://github.com/RoaringBitmap/RoaringFormatSpec
  11. //
  12. func (b *runContainer16) writeTo(stream io.Writer) (int, error) {
  13. buf := make([]byte, 2+4*len(b.iv))
  14. binary.LittleEndian.PutUint16(buf[0:], uint16(len(b.iv)))
  15. for i, v := range b.iv {
  16. binary.LittleEndian.PutUint16(buf[2+i*4:], v.start)
  17. binary.LittleEndian.PutUint16(buf[2+2+i*4:], v.length)
  18. }
  19. return stream.Write(buf)
  20. }
  21. func (b *runContainer32) writeToMsgpack(stream io.Writer) (int, error) {
  22. bts, err := b.MarshalMsg(nil)
  23. if err != nil {
  24. return 0, err
  25. }
  26. return stream.Write(bts)
  27. }
  28. func (b *runContainer16) writeToMsgpack(stream io.Writer) (int, error) {
  29. bts, err := b.MarshalMsg(nil)
  30. if err != nil {
  31. return 0, err
  32. }
  33. return stream.Write(bts)
  34. }
  35. func (b *runContainer32) readFromMsgpack(stream io.Reader) (int, error) {
  36. err := msgp.Decode(stream, b)
  37. return 0, err
  38. }
  39. func (b *runContainer16) readFromMsgpack(stream io.Reader) (int, error) {
  40. err := msgp.Decode(stream, b)
  41. return 0, err
  42. }
  43. var errCorruptedStream = errors.New("insufficient/odd number of stored bytes, corrupted stream detected")
  44. func (b *runContainer16) readFrom(stream io.Reader) (int, error) {
  45. b.iv = b.iv[:0]
  46. b.card = 0
  47. var numRuns uint16
  48. err := binary.Read(stream, binary.LittleEndian, &numRuns)
  49. if err != nil {
  50. return 0, err
  51. }
  52. nr := int(numRuns)
  53. encRun := make([]uint16, 2*nr)
  54. by := make([]byte, 4*nr)
  55. err = binary.Read(stream, binary.LittleEndian, &by)
  56. if err != nil {
  57. return 0, err
  58. }
  59. for i := range encRun {
  60. if len(by) < 2 {
  61. return 0, errCorruptedStream
  62. }
  63. encRun[i] = binary.LittleEndian.Uint16(by)
  64. by = by[2:]
  65. }
  66. for i := 0; i < nr; i++ {
  67. if i > 0 && b.iv[i-1].last() >= encRun[i*2] {
  68. return 0, fmt.Errorf("error: stored runContainer had runs that were not in sorted order!! (b.iv[i-1=%v].last = %v >= encRun[i=%v] = %v)", i-1, b.iv[i-1].last(), i, encRun[i*2])
  69. }
  70. b.iv = append(b.iv, interval16{start: encRun[i*2], length: encRun[i*2+1]})
  71. b.card += int64(encRun[i*2+1]) + 1
  72. }
  73. return 0, err
  74. }
上海开阖软件有限公司 沪ICP备12045867号-1