main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. if err := session.Query("DROP KEYSPACE gocql_test").Exec(); err != nil {
  31. log.Println("drop keyspace", err)
  32. }
  33. if err := session.Query(`CREATE KEYSPACE gocql_test
  34. WITH replication = {
  35. 'class' : 'SimpleStrategy',
  36. 'replication_factor' : 1
  37. }`).Exec(); err != nil {
  38. return err
  39. }
  40. if err := session.Query("USE gocql_test").Exec(); err != nil {
  41. return err
  42. }
  43. if err := session.Query(`CREATE TABLE page (
  44. title varchar,
  45. revid timeuuid,
  46. body varchar,
  47. views bigint,
  48. protected boolean,
  49. modified timestamp,
  50. tags set<varchar>,
  51. attachments map<varchar, text>,
  52. PRIMARY KEY (title, revid)
  53. )`).Exec(); err != nil {
  54. return err
  55. }
  56. if err := session.Query(`CREATE TABLE page_stats (
  57. title varchar,
  58. views counter,
  59. PRIMARY KEY (title)
  60. )`).Exec(); err != nil {
  61. return err
  62. }
  63. return nil
  64. }
  65. var pageTestData = []*Page{
  66. &Page{
  67. Title: "Frontpage",
  68. RevId: uuid.TimeUUID(),
  69. Body: "Welcome to this wiki page!",
  70. Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  71. Tags: []string{"start", "important", "test"},
  72. Attachments: map[string]Attachment{
  73. "logo": Attachment("\x00company logo\x00"),
  74. "favicon": Attachment("favicon.ico"),
  75. },
  76. },
  77. &Page{
  78. Title: "Foobar",
  79. RevId: uuid.TimeUUID(),
  80. Body: "foo::Foo f = new foo::Foo(foo::Foo::INIT);",
  81. Modified: time.Date(2013, time.August, 13, 9, 52, 3, 0, time.UTC),
  82. },
  83. }
  84. func insertTestData() error {
  85. for _, page := range pageTestData {
  86. if err := session.Query(`INSERT INTO page
  87. (title, revid, body, views, protected, modified, tags, attachments)
  88. VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
  89. page.Title, page.RevId, page.Body, page.Views, page.Protected,
  90. page.Modified, page.Tags, page.Attachments).Exec(); err != nil {
  91. return err
  92. }
  93. }
  94. return nil
  95. }
  96. func insertBatch() error {
  97. batch := gocql.NewBatch(gocql.LoggedBatch)
  98. for _, page := range pageTestData {
  99. batch.Query(`INSERT INTO page
  100. (title, revid, body, views, protected, modified, tags, attachments)
  101. VALUES (?, ?, ?, ?, ?, ?, ?, ?)`,
  102. page.Title, page.RevId, page.Body, page.Views, page.Protected,
  103. page.Modified, page.Tags, page.Attachments)
  104. }
  105. if err := session.ExecuteBatch(batch); err != nil {
  106. return err
  107. }
  108. return nil
  109. }
  110. func getPage(title string, revid uuid.UUID) (*Page, error) {
  111. p := new(Page)
  112. err := session.Query(`SELECT title, revid, body, views, protected, modified,
  113. tags, attachments
  114. FROM page WHERE title = ? AND revid = ? LIMIT 1`, title, revid).Scan(
  115. &p.Title, &p.RevId, &p.Body, &p.Views, &p.Protected, &p.Modified,
  116. &p.Tags, &p.Attachments)
  117. return p, err
  118. }
  119. func main() {
  120. if err := initSchema(); err != nil {
  121. log.Fatal("initSchema: ", err)
  122. }
  123. if err := insertTestData(); err != nil {
  124. log.Fatal("insertTestData: ", err)
  125. }
  126. var count int
  127. if err := session.Query("SELECT COUNT(*) FROM page").Scan(&count); err != nil {
  128. log.Fatal("getCount: ", err)
  129. }
  130. if count != len(pageTestData) {
  131. log.Printf("count: expected %d, got %d", len(pageTestData), count)
  132. }
  133. for _, original := range pageTestData {
  134. page, err := getPage(original.Title, original.RevId)
  135. if err != nil {
  136. log.Print("getPage: ", err)
  137. continue
  138. }
  139. sort.Sort(sort.StringSlice(page.Tags))
  140. sort.Sort(sort.StringSlice(original.Tags))
  141. if !reflect.DeepEqual(page, original) {
  142. log.Printf("page: expected %#v, got %#v\n", original, page)
  143. }
  144. }
  145. for _, original := range pageTestData {
  146. if err := session.Query("DELETE FROM page WHERE title = ? AND revid = ?",
  147. original.Title, original.RevId).Exec(); err != nil {
  148. log.Println("delete:", err)
  149. }
  150. }
  151. if err := session.Query("SELECT COUNT(*) FROM page").Scan(&count); err != nil {
  152. log.Fatal("getCount: ", err)
  153. }
  154. if count != 0 {
  155. log.Printf("count: expected %d, got %d", len(pageTestData), count)
  156. }
  157. if err := insertBatch(); err != nil {
  158. log.Fatal("insertBatch: ", err)
  159. }
  160. }