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.

194 lines
4.2 KiB

  1. package data
  2. import (
  3. "encoding/binary"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "io"
  7. "testing"
  8. )
  9. func TestRead(t *testing.T) {
  10. x := make([]uint16, 128)
  11. y := make([]uint32, 128)
  12. for i := range x {
  13. x[i] = uint16(i)
  14. }
  15. for i := range y {
  16. y[i] = uint32(i * 32)
  17. }
  18. xbuf := make([]byte, len(x)*SIZE_Uint16)
  19. ybuf := make([]byte, len(x)*SIZE_Uint32)
  20. WriteUint16s(xbuf, x)
  21. WriteUint32s(ybuf, y)
  22. df := &DataFile{
  23. xbuf: xbuf,
  24. ybuf: ybuf,
  25. xLen: len(xbuf),
  26. yLen: len(ybuf),
  27. xReaderAt: util.NewBytesReader(xbuf),
  28. yReaderAt: util.NewBytesReader(ybuf),
  29. }
  30. dataLayout := make(map[FieldName]DataLayout)
  31. dataLayout["x"] = DataLayout{
  32. LayoutType: Uint16,
  33. SortType: Unsorted,
  34. }
  35. dataLayout["y"] = DataLayout{
  36. LayoutType: Uint32,
  37. SortType: Unsorted,
  38. }
  39. rows, err := df.ReadRows("x", dataLayout, Equal, NewDUint16(65))
  40. if err != nil {
  41. fmt.Printf("err: %v", err)
  42. return
  43. }
  44. for _, row := range rows {
  45. fmt.Printf("row %d width %d ", row.index, len(row.Datums))
  46. for i, d := range row.Datums {
  47. fmt.Printf("%d: %v ", i, d)
  48. }
  49. fmt.Println()
  50. }
  51. }
  52. type Operator int32
  53. type LayoutType int32
  54. type SortType int32
  55. const (
  56. Equal Operator = 0
  57. GreaterThan
  58. GreaterOrEqual
  59. LessThan
  60. LessOrEqual
  61. Uint16 LayoutType = 0
  62. Uint32 = 1
  63. Unsorted SortType = 0
  64. Ascending
  65. Descending
  66. )
  67. type DataFile struct {
  68. xbuf []byte
  69. ybuf []byte
  70. xReaderAt io.ReaderAt
  71. xLen int
  72. yReaderAt io.ReaderAt
  73. yLen int
  74. }
  75. type DataLayout struct {
  76. LayoutType
  77. SortType
  78. }
  79. type FieldName string
  80. func (d *DataFile) ReadRows(field FieldName, layout map[FieldName]DataLayout, op Operator, operand Datum) (rows []*Row, err error) {
  81. if field == "x" {
  82. rows, err = pushDownReadRows(d.xReaderAt, d.xLen, layout[field], op, operand)
  83. if err != nil {
  84. return
  85. }
  86. err = hydrateRows(d.yReaderAt, d.yLen, layout["y"], rows)
  87. }
  88. if field == "y" {
  89. rows, err = pushDownReadRows(d.yReaderAt, d.yLen, layout[field], op, operand)
  90. if err != nil {
  91. return
  92. }
  93. err = hydrateRows(d.xReaderAt, d.xLen, layout["x"], rows)
  94. }
  95. return
  96. }
  97. type Row struct {
  98. index int
  99. Datums
  100. }
  101. func pushDownReadRows(readerAt io.ReaderAt, dataLen int, layout DataLayout, op Operator, operand Datum) (rows []*Row, err error) {
  102. if layout.LayoutType == Uint16 {
  103. if layout.SortType == Unsorted {
  104. buf := make([]byte, SIZE_Uint16)
  105. for i := 0; i < dataLen; i += SIZE_Uint16 {
  106. if n, err := readerAt.ReadAt(buf, int64(i)); n == SIZE_Uint16 && err == nil {
  107. d := NewDUint16(DUint16(binary.BigEndian.Uint16(buf)))
  108. cmp, err := d.Compare(operand)
  109. if err != nil {
  110. return rows, err
  111. }
  112. if cmp == 0 && op == Equal {
  113. println(1)
  114. rows = append(rows, &Row{
  115. index: i / SIZE_Uint16,
  116. Datums: []Datum{d},
  117. })
  118. }
  119. }
  120. }
  121. }
  122. }
  123. if layout.LayoutType == Uint32 {
  124. if layout.SortType == Unsorted {
  125. buf := make([]byte, SIZE_Uint32)
  126. for i := 0; i < dataLen; i += SIZE_Uint32 {
  127. if n, err := readerAt.ReadAt(buf, int64(i)); n == SIZE_Uint32 && err == nil {
  128. d := NewDUint32(DUint32(binary.BigEndian.Uint32(buf)))
  129. cmp, err := d.Compare(operand)
  130. if err != nil {
  131. return rows, err
  132. }
  133. if cmp == 0 && op == Equal {
  134. println(2)
  135. rows = append(rows, &Row{
  136. index: i / SIZE_Uint32,
  137. Datums: []Datum{d},
  138. })
  139. }
  140. }
  141. }
  142. }
  143. }
  144. return
  145. }
  146. func hydrateRows(readerAt io.ReaderAt, dataLen int, layout DataLayout, rows []*Row) (err error) {
  147. if layout.LayoutType == Uint16 {
  148. if layout.SortType == Unsorted {
  149. buf := make([]byte, SIZE_Uint16)
  150. for _, row := range rows {
  151. if n, err := readerAt.ReadAt(buf, int64(row.index)*SIZE_Uint16); n == SIZE_Uint16 && err == nil {
  152. t := binary.BigEndian.Uint16(buf)
  153. d := NewDUint16(DUint16(t))
  154. println(3, "add", t)
  155. row.Datums = append(row.Datums, d)
  156. }
  157. }
  158. }
  159. }
  160. if layout.LayoutType == Uint32 {
  161. if layout.SortType == Unsorted {
  162. buf := make([]byte, SIZE_Uint32)
  163. for _, row := range rows {
  164. if n, err := readerAt.ReadAt(buf, int64(row.index)*SIZE_Uint32); n == SIZE_Uint32 && err == nil {
  165. t := binary.BigEndian.Uint32(buf)
  166. d := NewDUint32(DUint32(t))
  167. println(4, "add", t)
  168. row.Datums = append(row.Datums, d)
  169. }
  170. }
  171. }
  172. }
  173. return
  174. }