searchresult.go 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package interpreter
  11. import (
  12. "bytes"
  13. "encoding/csv"
  14. "fmt"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "devt.de/krotik/eliasdb/graph/data"
  19. )
  20. /*
  21. SearchHeader is the header of a search result.
  22. */
  23. type SearchHeader struct {
  24. ResPrimaryKind string // Primary node kind
  25. ResPartition string // Partition of result
  26. ColLabels []string // Labels for columns
  27. ColFormat []string // Format for columns
  28. ColData []string // Data which should be displayed in the columns
  29. }
  30. /*
  31. Partition returns the partition of a search result.
  32. */
  33. func (sh *SearchHeader) Partition() string {
  34. return sh.ResPartition
  35. }
  36. /*
  37. PrimaryKind returns the primary kind of a search result.
  38. */
  39. func (sh *SearchHeader) PrimaryKind() string {
  40. return sh.ResPrimaryKind
  41. }
  42. /*
  43. Labels returns all column labels of a search result.
  44. */
  45. func (sh *SearchHeader) Labels() []string {
  46. return sh.ColLabels
  47. }
  48. /*
  49. Format returns all column format definitions of a search result.
  50. */
  51. func (sh *SearchHeader) Format() []string {
  52. return sh.ColFormat
  53. }
  54. /*
  55. Data returns the data which is displayed in each column of a search result.
  56. (e.g. 1:n:name - Name of starting nodes,
  57. 3:e:key - Key of edge traversed in the second traversal)
  58. */
  59. func (sh *SearchHeader) Data() []string {
  60. return sh.ColData
  61. }
  62. /*
  63. SearchResult data structure. A search result represents the result of an EQL query.
  64. */
  65. type SearchResult struct {
  66. name string // Name to identify the result
  67. query string // Query which produced the search result
  68. withFlags *withFlags // With flags which should be applied to the result
  69. SearchHeader // Embedded search header
  70. colFunc []FuncShow // Function which transforms the data
  71. Source [][]string // Special string holding the data source (node / edge) for each column
  72. Data [][]interface{} // Data which is held by this search result
  73. }
  74. /*
  75. newSearchResult creates a new search result object.
  76. */
  77. func newSearchResult(rtp *eqlRuntimeProvider, query string) *SearchResult {
  78. cdl := make([]string, 0, len(rtp.colData))
  79. for i, cd := range rtp.colData {
  80. if rtp.colFunc[i] != nil {
  81. colDataSpec := strings.SplitN(cd, ":", 2)
  82. cdl = append(cdl, colDataSpec[0]+":func:"+rtp.colFunc[i].name()+"()")
  83. } else {
  84. cdl = append(cdl, cd)
  85. }
  86. }
  87. return &SearchResult{rtp.name, query, rtp.withFlags, SearchHeader{rtp.primaryKind, rtp.part, rtp.colLabels, rtp.colFormat,
  88. cdl}, rtp.colFunc, make([][]string, 0), make([][]interface{}, 0)}
  89. }
  90. /*
  91. addRow adds a row to the result.
  92. */
  93. func (sr *SearchResult) addRow(rowNodes []data.Node, rowEdges []data.Edge) error {
  94. var pos int
  95. var isNode bool
  96. var err error
  97. src := make([]string, 0, len(sr.ColData))
  98. row := make([]interface{}, 0, len(sr.ColData))
  99. addNil := func() {
  100. src = append(src, "")
  101. row = append(row, nil)
  102. }
  103. addNode := func(n data.Node, attr string) {
  104. if n == nil {
  105. addNil()
  106. return
  107. }
  108. src = append(src, "n:"+n.Kind()+":"+n.Key())
  109. row = append(row, n.Attr(attr))
  110. }
  111. addEdge := func(e data.Edge, attr string) {
  112. if e == nil {
  113. addNil()
  114. return
  115. }
  116. row = append(row, e.Attr(attr))
  117. src = append(src, "e:"+e.Kind()+":"+e.Key())
  118. }
  119. // Pick only the data which is needed for the result
  120. for i, colData := range sr.ColData {
  121. attr := ""
  122. // Row data should be picked from the node
  123. colDataSpec := strings.SplitN(colData, ":", 3)
  124. if len(colDataSpec) != 3 {
  125. return &ResultError{sr.name, ErrInvalidColData, "Column data spec must have 3 items: " + colData}
  126. }
  127. posstring := colDataSpec[0]
  128. if colDataSpec[1] == "func" {
  129. pos, _ = strconv.Atoi(posstring)
  130. } else {
  131. if colDataSpec[1] == "n" {
  132. isNode = true
  133. } else if colDataSpec[1] == "e" {
  134. isNode = false
  135. } else {
  136. return &ResultError{sr.name, ErrInvalidColData, "Invalid data source '" + colDataSpec[1] + "' (either n - Node or e - Edge)"}
  137. }
  138. attr = colDataSpec[2]
  139. pos, err = strconv.Atoi(posstring)
  140. if err != nil || pos < 1 {
  141. return &ResultError{sr.name, ErrInvalidColData, "Invalid data index: " + colData}
  142. }
  143. }
  144. // Make pos an index
  145. pos--
  146. // Check if the row data should come from a function transformation
  147. // or from a node itself
  148. if cf := sr.colFunc[i]; cf != nil {
  149. fres, fsrc, err := sr.colFunc[i].eval(rowNodes[pos], rowEdges[pos])
  150. if err != nil {
  151. return err
  152. }
  153. row = append(row, fres)
  154. src = append(src, fsrc)
  155. } else {
  156. if isNode {
  157. addNode(rowNodes[pos], attr)
  158. } else {
  159. addEdge(rowEdges[pos], attr)
  160. }
  161. }
  162. }
  163. sr.Source = append(sr.Source, src)
  164. sr.Data = append(sr.Data, row)
  165. return nil
  166. }
  167. /*
  168. finish is called once all rows have been added.
  169. */
  170. func (sr *SearchResult) finish() {
  171. // Apply filtering
  172. if len(sr.withFlags.notnullCol) > 0 || len(sr.withFlags.uniqueCol) > 0 {
  173. uniqueMaps := make([]map[string]int, len(sr.withFlags.uniqueCol))
  174. for i := range uniqueMaps {
  175. uniqueMaps[i] = make(map[string]int)
  176. }
  177. // Using downward loop so we can remove the current element if necessary
  178. for i := len(sr.Data) - 1; i >= 0; i-- {
  179. row := sr.Data[i]
  180. cont := false
  181. // Apply not null
  182. for _, nn := range sr.withFlags.notnullCol {
  183. if row[nn] == nil {
  184. sr.Data = append(sr.Data[:i], sr.Data[i+1:]...)
  185. cont = true
  186. break
  187. }
  188. }
  189. if cont {
  190. continue
  191. }
  192. // Apply unique
  193. for j, u := range sr.withFlags.uniqueCol {
  194. if _, ok := uniqueMaps[j][fmt.Sprint(row[u])]; ok {
  195. uniqueMaps[j][fmt.Sprint(row[u])]++
  196. sr.Data = append(sr.Data[:i], sr.Data[i+1:]...)
  197. break
  198. } else {
  199. uniqueMaps[j][fmt.Sprint(row[u])] = 1
  200. }
  201. }
  202. }
  203. // Add unique counts if necessary
  204. for j, uc := range sr.withFlags.uniqueColCnt {
  205. u := sr.withFlags.uniqueCol[j]
  206. if uc {
  207. for _, row := range sr.Data {
  208. row[u] = fmt.Sprintf("%v (%d)", row[u], uniqueMaps[j][fmt.Sprint(row[u])])
  209. }
  210. }
  211. }
  212. }
  213. // Apply ordering
  214. for i, ordering := range sr.withFlags.ordering {
  215. sort.Stable(&SearchResultRowComparator{ordering == withOrderingAscending,
  216. sr.withFlags.orderingCol[i], sr.Data, sr.Source})
  217. }
  218. }
  219. /*
  220. Header returns all column headers.
  221. */
  222. func (sr *SearchResult) Header() *SearchHeader {
  223. return &sr.SearchHeader
  224. }
  225. /*
  226. Query returns the query which produced this result.
  227. */
  228. func (sr *SearchResult) Query() string {
  229. return sr.query
  230. }
  231. /*
  232. RowCount returns the number of rows of the result.
  233. */
  234. func (sr *SearchResult) RowCount() int {
  235. return len(sr.Data)
  236. }
  237. /*
  238. Row returns a row of the result.
  239. */
  240. func (sr *SearchResult) Row(line int) []interface{} {
  241. return sr.Data[line]
  242. }
  243. /*
  244. Rows returns all rows.
  245. */
  246. func (sr *SearchResult) Rows() [][]interface{} {
  247. return sr.Data
  248. }
  249. /*
  250. RowSource returns the sources of a result row.
  251. Format is either: <n/e>:<kind>:<key> or q:<query>
  252. */
  253. func (sr *SearchResult) RowSource(line int) []string {
  254. return sr.Source[line]
  255. }
  256. /*
  257. RowSources returns the sources of a result.
  258. */
  259. func (sr *SearchResult) RowSources() [][]string {
  260. return sr.Source
  261. }
  262. /*
  263. String returns a string representation of this search result.
  264. */
  265. func (sr *SearchResult) String() string {
  266. var buf bytes.Buffer
  267. buf.WriteString("Labels: ")
  268. buf.WriteString(strings.Join(sr.ColLabels, ", "))
  269. buf.WriteString("\n")
  270. buf.WriteString("Format: ")
  271. buf.WriteString(strings.Join(sr.ColFormat, ", "))
  272. buf.WriteString("\n")
  273. buf.WriteString("Data: ")
  274. buf.WriteString(strings.Join(sr.ColData, ", "))
  275. buf.WriteString("\n")
  276. // Render the table
  277. for _, row := range sr.Data {
  278. for i, col := range row {
  279. if col != nil {
  280. buf.WriteString(fmt.Sprint(col))
  281. } else {
  282. buf.WriteString("<not set>")
  283. }
  284. if i < len(row)-1 {
  285. buf.WriteString(", ")
  286. }
  287. }
  288. buf.WriteString("\n")
  289. }
  290. return buf.String()
  291. }
  292. /*
  293. CSV returns this search result as comma-separated strings.
  294. */
  295. func (sr *SearchResult) CSV() string {
  296. var buf bytes.Buffer
  297. labels := sr.Header().ColLabels
  298. strData := make([][]string, len(sr.Data)+1)
  299. // Prepare string data
  300. strData[0] = make([]string, len(labels))
  301. for i, s := range labels {
  302. strData[0][i] = s
  303. }
  304. for i, row := range sr.Data {
  305. strData[i+1] = make([]string, len(row))
  306. for j, s := range row {
  307. strData[i+1][j] = fmt.Sprint(s)
  308. }
  309. }
  310. // Write CSV data into buffer
  311. w := csv.NewWriter(&buf)
  312. w.WriteAll(strData)
  313. return buf.String()
  314. }
  315. // Util functions
  316. // ==============
  317. /*
  318. SearchResultRowComparator is a comparator object used for sorting the result
  319. */
  320. type SearchResultRowComparator struct {
  321. Ascening bool // Sort should be ascending
  322. Column int // Column to sort
  323. Data [][]interface{} // Data to sort
  324. Source [][]string // Source entries which follow the data
  325. }
  326. func (c SearchResultRowComparator) Len() int {
  327. return len(c.Data)
  328. }
  329. func (c SearchResultRowComparator) Less(i, j int) bool {
  330. c1 := c.Data[i][c.Column]
  331. c2 := c.Data[j][c.Column]
  332. num1, err := strconv.ParseFloat(fmt.Sprint(c1), 64)
  333. if err == nil {
  334. num2, err := strconv.ParseFloat(fmt.Sprint(c2), 64)
  335. if err == nil {
  336. if c.Ascening {
  337. return num1 < num2
  338. }
  339. return num1 > num2
  340. }
  341. }
  342. if c.Ascening {
  343. return fmt.Sprintf("%v", c1) < fmt.Sprintf("%v", c2)
  344. }
  345. return fmt.Sprintf("%v", c1) > fmt.Sprintf("%v", c2)
  346. }
  347. func (c SearchResultRowComparator) Swap(i, j int) {
  348. c.Data[i], c.Data[j] = c.Data[j], c.Data[i]
  349. c.Source[i], c.Source[j] = c.Source[j], c.Source[i]
  350. }
  351. // Testing functions
  352. // =================
  353. type rowSort SearchResult
  354. func (s rowSort) Len() int {
  355. return len(s.Data)
  356. }
  357. func (s rowSort) Swap(i, j int) {
  358. s.Data[i], s.Data[j] = s.Data[j], s.Data[i]
  359. s.Source[i], s.Source[j] = s.Source[j], s.Source[i]
  360. }
  361. func (s rowSort) Less(i, j int) bool {
  362. keyString := func(data []interface{}) string {
  363. var ret bytes.Buffer
  364. for _, d := range data {
  365. ret.WriteString(fmt.Sprintf("%v", d))
  366. }
  367. return ret.String()
  368. }
  369. return keyString(s.Data[i]) < keyString(s.Data[j])
  370. }
  371. /*
  372. StableSort sorts the rows of the result in a stable 100% reproducible way.
  373. */
  374. func (sr *SearchResult) StableSort() {
  375. sort.Stable(rowSort(*sr))
  376. }