main.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package main
  5. import (
  6. "log"
  7. "reflect"
  8. "sort"
  9. "time"
  10. "github.com/tux21b/gocql"
  11. "github.com/tux21b/gocql/uuid"
  12. )
  13. var session *gocql.Session
  14. func init() {
  15. cluster := gocql.NewCluster("127.0.0.1")
  16. session = cluster.CreateSession()
  17. }
  18. type Page struct {
  19. Title string
  20. RevId uuid.UUID
  21. Body string
  22. Views int64
  23. Protected bool
  24. Modified time.Time
  25. Tags []string
  26. Attachments map[string]Attachment
  27. }
  28. type Attachment []byte
  29. func initSchema() error {
  30. session.Query("DROP KEYSPACE gocql_test").Exec()
  31. if err := session.Query(`CREATE KEYSPACE gocql_test
  32. WITH replication = {
  33. 'class' : 'SimpleStrategy',
  34. 'replication_factor' : 1
  35. }`).Exec(); err != nil {
  36. return err
  37. }
  38. if err := session.Query("USE gocql_test").Exec(); err != nil {
  39. return err
  40. }
  41. if err := session.Query(`CREATE TABLE page (
  42. title varchar,
  43. revid timeuuid,
  44. body varchar,
  45. views bigint,
  46. protected boolean,
  47. modified timestamp,
  48. tags set<varchar>,
  49. attachments map<varchar, text>,
  50. PRIMARY KEY (title, revid)
  51. )`).Exec(); err != nil {
  52. return err
  53. }
  54. if err := session.Query(`CREATE TABLE page_stats (
  55. title varchar,
  56. views counter,
  57. PRIMARY KEY (title)
  58. )`).Exec(); err != nil {
  59. return err
  60. }
  61. return nil
  62. }
  63. var pageTestData = []*Page{
  64. &Page{
  65. Title: "Frontpage",
  66. RevId: uuid.TimeUUID(),
  67. Body: "Welcome to this wiki page!",
  68. Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  69. Tags: []string{"start", "important", "test"},
  70. Attachments: map[string]Attachment{
  71. "logo": Attachment("\x00company logo\x00"),
  72. "favicon": Attachment("favicon.ico"),
  73. },
  74. },
  75. &Page{
  76. Title: "Foobar",
  77. RevId: uuid.TimeUUID(),
  78. Body: "foo::Foo f = new foo::Foo(foo::Foo::INIT);",
  79. Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  80. },
  81. }
  82. func insertTestData() error {
  83. for _, page := range pageTestData {
  84. if err := session.Query(`INSERT INTO page
  85. (title, revid, body, views, protected, modified, tags, attachments)
  86. VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
  87. page.Title, page.RevId, page.Body, page.Views, page.Protected,
  88. page.Modified, page.Tags, page.Attachments).Exec(); err != nil {
  89. return err
  90. }
  91. }
  92. return nil
  93. }
  94. func insertBatch() error {
  95. batch := gocql.NewBatch(gocql.LoggedBatch)
  96. for _, page := range pageTestData {
  97. batch.Query(`INSERT INTO page
  98. (title, revid, body, views, protected, modified, tags, attachments)
  99. VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
  100. page.Title, page.RevId, page.Body, page.Views, page.Protected,
  101. page.Modified, page.Tags, page.Attachments)
  102. }
  103. if err := session.ExecuteBatch(batch); err != nil {
  104. return err
  105. }
  106. return nil
  107. }
  108. func getPage(title string, revid uuid.UUID) (*Page, error) {
  109. p := new(Page)
  110. err := session.Query(`SELECT title, revid, body, views, protected, modified,
  111. tags, attachments
  112. FROM page WHERE title = ? AND revid = ?`, title, revid).Scan(
  113. &p.Title, &p.RevId, &p.Body, &p.Views, &p.Protected, &p.Modified,
  114. &p.Tags, &p.Attachments)
  115. return p, err
  116. }
  117. func main() {
  118. if err := initSchema(); err != nil {
  119. log.Fatal("initSchema: ", err)
  120. }
  121. if err := insertTestData(); err != nil {
  122. log.Fatal("insertTestData: ", err)
  123. }
  124. var count int
  125. if err := session.Query("SELECT COUNT(*) FROM page").Scan(&count); err != nil {
  126. log.Fatal("getCount: ", err)
  127. }
  128. if count != len(pageTestData) {
  129. log.Printf("count: expected %d, got %d", len(pageTestData), count)
  130. }
  131. for _, original := range pageTestData {
  132. page, err := getPage(original.Title, original.RevId)
  133. if err != nil {
  134. log.Print("getPage: ", err)
  135. continue
  136. }
  137. sort.Sort(sort.StringSlice(page.Tags))
  138. sort.Sort(sort.StringSlice(original.Tags))
  139. if !reflect.DeepEqual(page, original) {
  140. log.Printf("page: expected %#v, got %#v\n", original, page)
  141. }
  142. }
  143. for _, original := range pageTestData {
  144. if err := session.Query("DELETE FROM page WHERE title = ? AND revid = ?",
  145. original.Title, original.RevId).Exec(); err != nil {
  146. log.Println("delete:", err)
  147. }
  148. }
  149. if err := session.Query("SELECT COUNT(*) FROM page").Scan(&count); err != nil {
  150. log.Fatal("getCount: ", err)
  151. }
  152. if count != 0 {
  153. log.Printf("count: expected %d, got %d", len(pageTestData), count)
  154. }
  155. if err := insertBatch(); err != nil {
  156. log.Fatal("insertBatch: ", err)
  157. }
  158. }