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.

259 lines
5.7 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
  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. z := make([]uint64, 128)
  13. for i := range x {
  14. x[i] = uint16(i)
  15. }
  16. for i := range y {
  17. y[i] = uint32(i * 2)
  18. }
  19. for i := range z {
  20. z[i] = uint64(i * 3)
  21. }
  22. xbuf := make([]byte, len(x)*SIZE_Uint16)
  23. ybuf := make([]byte, len(x)*SIZE_Uint32)
  24. zbuf := make([]byte, len(x)*SIZE_Uint64)
  25. WriteUint16s(xbuf, x)
  26. WriteUint32s(ybuf, y)
  27. WriteUint64s(zbuf, z)
  28. df := &DataFile{
  29. xbuf: xbuf,
  30. ybuf: ybuf,
  31. zbuf: zbuf,
  32. xLen: len(xbuf),
  33. yLen: len(ybuf),
  34. zLen: len(zbuf),
  35. xReaderAt: util.NewBytesReader(xbuf),
  36. yReaderAt: util.NewBytesReader(ybuf),
  37. zReaderAt: util.NewBytesReader(zbuf),
  38. }
  39. dataLayout := make(map[FieldName]DataLayout)
  40. dataLayout["x"] = DataLayout{
  41. LayoutType: Uint16,
  42. SortType: Unsorted,
  43. }
  44. dataLayout["y"] = DataLayout{
  45. LayoutType: Uint32,
  46. SortType: Unsorted,
  47. }
  48. dataLayout["z"] = DataLayout{
  49. LayoutType: Uint64,
  50. SortType: Unsorted,
  51. }
  52. rows, err := df.ReadRows("x", dataLayout, Equal, NewDUint16(65))
  53. if err != nil {
  54. fmt.Printf("err: %v", err)
  55. return
  56. }
  57. for _, row := range rows {
  58. fmt.Printf("row %d width %d ", row.index, len(row.Datums))
  59. for i, d := range row.Datums {
  60. fmt.Printf("%d: %v ", i, d)
  61. }
  62. fmt.Println()
  63. }
  64. }
  65. type Operator int32
  66. type LayoutType int32
  67. type SortType int32
  68. const (
  69. Equal Operator = 0
  70. GreaterThan
  71. GreaterOrEqual
  72. LessThan
  73. LessOrEqual
  74. Uint16 LayoutType = 0
  75. Uint32 = 1
  76. Uint64 = 2
  77. Unsorted SortType = 0
  78. Ascending
  79. Descending
  80. )
  81. type DataFile struct {
  82. xbuf []byte
  83. ybuf []byte
  84. zbuf []byte
  85. xReaderAt io.ReaderAt
  86. yReaderAt io.ReaderAt
  87. zReaderAt io.ReaderAt
  88. xLen int
  89. yLen int
  90. zLen int
  91. }
  92. type DataLayout struct {
  93. LayoutType
  94. SortType
  95. }
  96. type FieldName string
  97. func (d *DataFile) ReadRows(field FieldName, layout map[FieldName]DataLayout, op Operator, operand Datum) (rows []*Row, err error) {
  98. if field == "x" {
  99. rows, err = pushDownReadRows(d.xReaderAt, d.xLen, layout[field], op, operand)
  100. if err != nil {
  101. return
  102. }
  103. err = hydrateRows(d.yReaderAt, d.yLen, layout["y"], rows)
  104. if err != nil {
  105. return
  106. }
  107. err = hydrateRows(d.zReaderAt, d.zLen, layout["z"], rows)
  108. if err != nil {
  109. return
  110. }
  111. }
  112. if field == "y" {
  113. rows, err = pushDownReadRows(d.yReaderAt, d.yLen, layout[field], op, operand)
  114. if err != nil {
  115. return
  116. }
  117. err = hydrateRows(d.xReaderAt, d.xLen, layout["x"], rows)
  118. if err != nil {
  119. return
  120. }
  121. err = hydrateRows(d.zReaderAt, d.zLen, layout["z"], rows)
  122. if err != nil {
  123. return
  124. }
  125. }
  126. return
  127. }
  128. type Row struct {
  129. index int
  130. Datums
  131. }
  132. func pushDownReadRows(readerAt io.ReaderAt, dataLen int, layout DataLayout, op Operator, operand Datum) (rows []*Row, err error) {
  133. if layout.LayoutType == Uint16 {
  134. if layout.SortType == Unsorted {
  135. buf := make([]byte, SIZE_Uint16)
  136. for i := 0; i < dataLen; i += SIZE_Uint16 {
  137. if n, err := readerAt.ReadAt(buf, int64(i)); n == SIZE_Uint16 && err == nil {
  138. d := NewDUint16(DUint16(binary.BigEndian.Uint16(buf)))
  139. cmp, err := d.Compare(operand)
  140. if err != nil {
  141. return rows, err
  142. }
  143. if cmp == 0 && op == Equal {
  144. println(1)
  145. rows = append(rows, &Row{
  146. index: i / SIZE_Uint16,
  147. Datums: []Datum{d},
  148. })
  149. }
  150. }
  151. }
  152. }
  153. }
  154. if layout.LayoutType == Uint32 {
  155. if layout.SortType == Unsorted {
  156. buf := make([]byte, SIZE_Uint32)
  157. for i := 0; i < dataLen; i += SIZE_Uint32 {
  158. if n, err := readerAt.ReadAt(buf, int64(i)); n == SIZE_Uint32 && err == nil {
  159. d := NewDUint32(DUint32(binary.BigEndian.Uint32(buf)))
  160. cmp, err := d.Compare(operand)
  161. if err != nil {
  162. return rows, err
  163. }
  164. if cmp == 0 && op == Equal {
  165. println(2)
  166. rows = append(rows, &Row{
  167. index: i / SIZE_Uint32,
  168. Datums: []Datum{d},
  169. })
  170. }
  171. }
  172. }
  173. }
  174. }
  175. if layout.LayoutType == Uint64 {
  176. if layout.SortType == Unsorted {
  177. buf := make([]byte, SIZE_Uint64)
  178. for i := 0; i < dataLen; i += SIZE_Uint64 {
  179. if n, err := readerAt.ReadAt(buf, int64(i)); n == SIZE_Uint64 && err == nil {
  180. d := NewDUint64(DUint64(binary.BigEndian.Uint64(buf)))
  181. cmp, err := d.Compare(operand)
  182. if err != nil {
  183. return rows, err
  184. }
  185. if cmp == 0 && op == Equal {
  186. println(2)
  187. rows = append(rows, &Row{
  188. index: i / SIZE_Uint64,
  189. Datums: []Datum{d},
  190. })
  191. }
  192. }
  193. }
  194. }
  195. }
  196. return
  197. }
  198. func hydrateRows(readerAt io.ReaderAt, dataLen int, layout DataLayout, rows []*Row) (err error) {
  199. if layout.LayoutType == Uint16 {
  200. if layout.SortType == Unsorted {
  201. buf := make([]byte, SIZE_Uint16)
  202. for _, row := range rows {
  203. if n, err := readerAt.ReadAt(buf, int64(row.index)*SIZE_Uint16); n == SIZE_Uint16 && err == nil {
  204. t := binary.BigEndian.Uint16(buf)
  205. d := NewDUint16(DUint16(t))
  206. println(3, "add", t)
  207. row.Datums = append(row.Datums, d)
  208. }
  209. }
  210. }
  211. }
  212. if layout.LayoutType == Uint32 {
  213. if layout.SortType == Unsorted {
  214. buf := make([]byte, SIZE_Uint32)
  215. for _, row := range rows {
  216. if n, err := readerAt.ReadAt(buf, int64(row.index)*SIZE_Uint32); n == SIZE_Uint32 && err == nil {
  217. t := binary.BigEndian.Uint32(buf)
  218. d := NewDUint32(DUint32(t))
  219. println(4, "add", t)
  220. row.Datums = append(row.Datums, d)
  221. }
  222. }
  223. }
  224. }
  225. if layout.LayoutType == Uint64 {
  226. if layout.SortType == Unsorted {
  227. buf := make([]byte, SIZE_Uint64)
  228. for _, row := range rows {
  229. if n, err := readerAt.ReadAt(buf, int64(row.index)*SIZE_Uint64); n == SIZE_Uint64 && err == nil {
  230. t := binary.BigEndian.Uint64(buf)
  231. d := NewDUint64(DUint64(t))
  232. println(5, "add", t)
  233. row.Datums = append(row.Datums, d)
  234. }
  235. }
  236. }
  237. }
  238. return
  239. }