Advertisement
Guest User

Untitled

a guest
Jan 18th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "strconv"
  6.  
  7. "github.com/jmoiron/sqlx"
  8.  
  9. _ "github.com/lib/pq"
  10. )
  11.  
  12. func main() {
  13. var db *sqlx.DB
  14. db, _ = sqlx.Open("postgres", "user=test password=test dbname=eavvsjsonb")
  15.  
  16. // Create the EAV tables
  17. db.MustExec(`
  18. CREATE TABLE entity (
  19. id SERIAL PRIMARY KEY,
  20. name TEXT,
  21. description TEXT
  22. );`)
  23. db.MustExec(`
  24. CREATE TABLE entity_attribute (
  25. id SERIAL PRIMARY KEY,
  26. name TEXT,
  27. description TEXT
  28. );`)
  29. db.MustExec(`
  30. CREATE TABLE entity_attribute_value (
  31. id SERIAL PRIMARY KEY,
  32. entity_id INT references entity(id),
  33. entity_attribute_id INT references entity_attribute(id),
  34. value TEXT
  35. );`)
  36.  
  37. // Create an 'all-in-one' table for JSONB
  38. db.MustExec(`
  39. CREATE TABLE entity_jsonb (
  40. id SERIAL PRIMARY KEY,
  41. name TEXT,
  42. description TEXT,
  43. properties JSONB
  44. );`)
  45.  
  46. // Create some attributes for EAV
  47. var colorId int
  48. db.Get(&colorId, "INSERT INTO entity_attribute (name, description) VALUES ('color', 'The color of the entity') RETURNING id;")
  49. var lengthId int
  50. db.Get(&lengthId, "INSERT INTO entity_attribute (name, description) VALUES ('lenght', 'The lenght of the entity') RETURNING id;")
  51. var widthId int
  52. db.Get(&widthId, "INSERT INTO entity_attribute (name, description) VALUES ('width', 'The width of the entity') RETURNING id;")
  53. var hasSomethingId int
  54. db.Get(&hasSomethingId, "INSERT INTO entity_attribute (name, description) VALUES ('hassomething', 'A bool expressing if the entity has something') RETURNING id;")
  55. var countryId int
  56. db.Get(&countryId, "INSERT INTO entity_attribute (name, description) VALUES ('country', 'The home country of the entity') RETURNING id;")
  57.  
  58. // InsertSomeEntities
  59. for i := 0; i < 1000000; i++ {
  60. e := getRandomEntity(i)
  61.  
  62. // Insert for EAV
  63. var eId int
  64. db.Get(&eId, "INSERT INTO entity (name, description) VALUES ($1, $2) RETURNING id;", e.Name, e.Description)
  65. db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, colorId, e.Properties["color"])
  66. db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, lengthId, e.Properties["length"])
  67. db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, widthId, e.Properties["width"])
  68. db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, hasSomethingId, e.Properties["hassomething"])
  69. db.Exec("INSERT INTO entity_attribute_value (entity_id, entity_attribute_id, value) VALUES ($1, $2, $3);", eId, countryId, e.Properties["country"])
  70.  
  71. // Insert for jsonb
  72. db.NamedExec(`INSERT INTO entity_jsonb (name, description, properties) VALUES (:name, :description, :properties);`, e)
  73.  
  74. // Report progress...
  75. if i%1000 == 0 {
  76. fmt.Println("Progress:", float64(i)/10000.0, "%")
  77. }
  78. }
  79.  
  80. // var test []Entity
  81. // db.Select(&test, `SELECT e.id, e.name, e.description FROM entity e
  82. // INNER JOIN entity_attribute_value eav ON eav.entity_id = e.id
  83. // INNER JOIN entity_attribute ea ON ea.id = eav.entity_attribute_id
  84. // WHERE ea.name = 'hassomething' and eav.value = 'true';`)
  85. // fmt.Println(test)
  86.  
  87. // drop all tables
  88. //db.MustExec("DROP TABLE IF EXISTS entity_jsonb;")
  89. //db.MustExec("DROP TABLE IF EXISTS entity_attribute_value;")
  90. //db.MustExec("DROP TABLE IF EXISTS entity_attribute;")
  91. //db.MustExec("DROP TABLE IF EXISTS entity;")
  92. }
  93.  
  94. func getRandomEntity(i int) (e Entity) {
  95. name := "Entity" + strconv.Itoa(i)
  96. description := "Entity number " + strconv.Itoa(i)
  97. length := i % 10
  98. width := 50.3285 / float64(length)
  99. hassomething := false
  100. if i%7 == 0 {
  101. hassomething = true
  102. }
  103. color := "red"
  104. if i%3 == 0 {
  105. color = "green"
  106. }
  107. country := "Belgium"
  108. if i%7 == 0 || i%86 == 0 {
  109. country = "The Netherlands"
  110. }
  111.  
  112. e = Entity{
  113. 0,
  114. name,
  115. description,
  116. PropertyMap{
  117. "color": color,
  118. "length": length,
  119. "width": width,
  120. "hassomething": hassomething,
  121. "country": country,
  122. },
  123. }
  124. return
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement