databaseRead.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. package lib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. )
  7. //topicPointer and textPointer should not be exported
  8. //mb move tweet reconstruction to dbRead from servers
  9. type Tweet struct {
  10. TopicPointer string
  11. TextPointer int
  12. Topics []string
  13. Text string
  14. RoundPosted int
  15. }
  16. var dbR = make(map[string][]Tweet)
  17. var archive = make(map[string][]Tweet)
  18. //has to be dividable by 32
  19. var minimumBlockSize int
  20. var roundsBeforeArchiving = 3
  21. var topicList []string
  22. var archiveTopicList []string
  23. func NewEntries(inputTweets []Tweet, whereTo int) {
  24. tmpdb := dbR
  25. if whereTo == 1 {
  26. tmpdb = archive
  27. }
  28. var position int = 0
  29. for _, tweet := range inputTweets {
  30. for index := range tweet.Topics {
  31. //fmt.Println("topic to put in", tweet.Topics[index])
  32. //new topic
  33. if _, ok := tmpdb[tweet.Topics[index]]; !ok {
  34. if whereTo == 0 {
  35. topicList = append(topicList, tweet.Topics[index])
  36. } else {
  37. archiveTopicList = append(archiveTopicList, tweet.Topics[index])
  38. }
  39. }
  40. //new tweet
  41. if index == 0 {
  42. position = len(tmpdb[tweet.Topics[0]])
  43. tmpdb[tweet.Topics[index]] = append(tmpdb[tweet.Topics[0]], tweet)
  44. } else {
  45. //known tweet
  46. //setting pointer for all other Topics
  47. topic := tweet.Topics[index]
  48. var pointerTweet Tweet
  49. pointerTweet.TopicPointer = tweet.Topics[0]
  50. pointerTweet.TextPointer = position
  51. pointerTweet.Topics = nil
  52. pointerTweet.Text = ""
  53. tmpdb[topic] = append(tmpdb[topic], pointerTweet)
  54. }
  55. }
  56. }
  57. if whereTo == 0 {
  58. dbR = tmpdb
  59. } else {
  60. archive = tmpdb
  61. }
  62. }
  63. //todo! add round to pirquery only get tweets that have been posted from that round onward
  64. func GetTweets(pirQuery []byte, dataLength int, whereFrom int, pubKey [32]byte) []byte {
  65. //fmt.Println("query", pirQuery)
  66. //fmt.Println("dbR", dbR)
  67. tmpdb := dbR
  68. if whereFrom == 1 {
  69. tmpdb = archive
  70. }
  71. minimumBlockSize = dataLength * maxTweetAmount(whereFrom)
  72. var wantedTopics = getNamesForTopics(pirQuery, whereFrom)
  73. tweetsToReturn := make([][]Tweet, len(wantedTopics))
  74. for index, wantedTopic := range wantedTopics {
  75. for _, tweet := range tmpdb[wantedTopic] {
  76. //fmt.Println(tweet)
  77. //new Tweet
  78. if tweet.Text != "" {
  79. tweet.RoundPosted = 0
  80. tweetsToReturn[index] = append(tweetsToReturn[index], tweet)
  81. } else {
  82. //"copied" tweet
  83. //find tweet with pointers
  84. tweet = tmpdb[tweet.TopicPointer][tweet.TextPointer]
  85. tweet.RoundPosted = 0
  86. tweetsToReturn[index] = append(tweetsToReturn[index], tweet)
  87. }
  88. }
  89. }
  90. return tweetsToByteArray(tweetsToReturn)
  91. }
  92. func maxTweetAmount(whereFrom int) int {
  93. tmpdb := dbR
  94. if whereFrom == 1 {
  95. tmpdb = archive
  96. }
  97. var max int = 0
  98. for i := range tmpdb {
  99. nrOfTweets := len(dbR[i])
  100. if nrOfTweets > max {
  101. max = nrOfTweets
  102. }
  103. }
  104. return max
  105. }
  106. func getNamesForTopics(wantedIndices []byte, whereFrom int) []string {
  107. var topicNames []string
  108. tmpTopicList := topicList
  109. if whereFrom == 1 {
  110. tmpTopicList = archiveTopicList
  111. }
  112. for index, element := range wantedIndices {
  113. if element == 1 {
  114. topicNames = append(topicNames, tmpTopicList[index])
  115. }
  116. }
  117. return topicNames
  118. }
  119. //transform struct to byte array for sending
  120. func tweetsToByteArray(tweetsToReturn [][]Tweet) []byte {
  121. tweetsAsBytes := make([]byte, minimumBlockSize)
  122. for _, block := range tweetsToReturn {
  123. var tweetToAppend []byte
  124. for _, tweet := range block {
  125. for _, topic := range tweet.Topics {
  126. tweetToAppend = append(tweetToAppend, []byte(topic)...)
  127. tweetToAppend = append(tweetToAppend, ","...)
  128. }
  129. //replaces last "," with ";;" bc there is text following and not another topic
  130. tweetToAppend = tweetToAppend[:len(tweetToAppend)-1]
  131. tweetToAppend = append(tweetToAppend, []byte(";;")[:]...)
  132. tweetToAppend = append(tweetToAppend, []byte(tweet.Text)...)
  133. tweetToAppend = append(tweetToAppend, []byte(";")[0])
  134. }
  135. //adds padding
  136. tweetToAppend = append(tweetToAppend, []byte(";;")[:]...)
  137. length := minimumBlockSize - len(tweetToAppend)
  138. //fmt.Println("len", len(tweetToAppend))
  139. //todo! replace with grouping
  140. //grouping using topics from recovered tweets
  141. if length < 0 {
  142. fmt.Println("<0", string(tweetToAppend))
  143. }
  144. padding := bytes.Repeat([]byte(";"), length)
  145. tweetToAppend = append(tweetToAppend, padding...)
  146. Xor(tweetToAppend, tweetsAsBytes)
  147. //fmt.Println(tweetsAsBytes)
  148. }
  149. //fmt.Println("length Returned", len(tweetsAsBytes))
  150. return tweetsAsBytes
  151. }
  152. //see func name
  153. func GetTopicList(whereFrom int) ([]byte, int) {
  154. tmpTopicList := topicList
  155. if whereFrom == 1 {
  156. tmpTopicList = archiveTopicList
  157. }
  158. if (len(tmpTopicList)) == 0 {
  159. return nil, 0
  160. }
  161. topicByteArray := new(bytes.Buffer)
  162. json.NewEncoder(topicByteArray).Encode(tmpTopicList)
  163. return topicByteArray.Bytes(), len(tmpTopicList)
  164. }
  165. //iterates through full dbR and moves old tweets to archive
  166. func CleanUpdbR(round int) {
  167. //is broken
  168. return
  169. if roundsBeforeArchiving-round == 0 {
  170. roundsBeforeArchiving = roundsBeforeArchiving + roundsBeforeArchiving
  171. var tweetsToArchive []Tweet
  172. for j := range dbR {
  173. tweets := dbR[j]
  174. for i := len(tweets) - 1; i >= 0; i-- {
  175. if round-roundsBeforeArchiving == tweets[i].RoundPosted {
  176. //only adds the tweet to the archive when there is text
  177. if tweets[i].Text != "" {
  178. tweetsToArchive = append(tweetsToArchive, tweets[i])
  179. }
  180. //delets the tweet from the array
  181. tweets = append(tweets[:i], tweets[i+1:]...)
  182. }
  183. }
  184. dbR[j] = tweets
  185. }
  186. NewEntries(tweetsToArchive, 1)
  187. //redoes the whole dbR to correct pointers
  188. var tweetsToMain []Tweet
  189. for i := range dbR {
  190. tweets := dbR[i]
  191. for _, tweet := range tweets {
  192. if tweet.Text != "" {
  193. tweetsToMain = append(tweetsToMain, tweet)
  194. }
  195. }
  196. }
  197. dbR = nil
  198. dbR = make(map[string][]Tweet)
  199. topicList = nil
  200. NewEntries(tweetsToMain, 0)
  201. }
  202. }