databaseRead.go 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. package lib
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "sort"
  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. //needs to be dividable by roundsBeforUpdate
  19. var roundsBeforeArchiving = -1
  20. var roundsBeforeArchivingInc = roundsBeforeArchiving
  21. var bytesSaved float64
  22. var topicList []string
  23. var archiveTopicList []string
  24. func NewEntries(inputTweets []Tweet, whereTo int) {
  25. tmpdb := dbR
  26. if whereTo == 1 {
  27. tmpdb = archive
  28. }
  29. var position int = 0
  30. for _, tweet := range inputTweets {
  31. for index := range tweet.Topics {
  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. if whereTo == 1 {
  48. bytesSaved += float64(len(tweet.Topics)) + float64(len(tweet.Text))
  49. }
  50. topic := tweet.Topics[index]
  51. var pointerTweet Tweet
  52. pointerTweet.TopicPointer = tweet.Topics[0]
  53. pointerTweet.TextPointer = position
  54. pointerTweet.Topics = nil
  55. pointerTweet.Text = ""
  56. tmpdb[topic] = append(tmpdb[topic], pointerTweet)
  57. }
  58. }
  59. }
  60. if whereTo == 0 {
  61. dbR = tmpdb
  62. } else {
  63. archive = tmpdb
  64. }
  65. }
  66. //todo! add round to pirquery only get tweets that have been posted from that round onward
  67. func GetTweets(pirQuery []byte, dataLength int, whereFrom int, pubKey [32]byte) []byte {
  68. tmpdb := dbR
  69. if whereFrom == 1 {
  70. tmpdb = archive
  71. }
  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. //new Tweet
  77. if tweet.Text != "" {
  78. tweet.RoundPosted = 0
  79. tweetsToReturn[index] = append(tweetsToReturn[index], tweet)
  80. } else {
  81. //"copied" tweet
  82. //find tweet with pointers
  83. tweet = tmpdb[tweet.TopicPointer][tweet.TextPointer]
  84. tweet.RoundPosted = 0
  85. tweetsToReturn[index] = append(tweetsToReturn[index], tweet)
  86. }
  87. }
  88. }
  89. return tweetsToByteArray(tweetsToReturn, whereFrom, wantedTopics, dataLength)
  90. }
  91. func getNamesForTopics(wantedIndices []byte, whereFrom int) []string {
  92. var topicNames []string
  93. tmpTopicList := topicList
  94. if whereFrom == 1 {
  95. tmpTopicList = archiveTopicList
  96. }
  97. for index, element := range wantedIndices {
  98. if index == len(tmpTopicList) {
  99. break
  100. }
  101. if element == 1 {
  102. topicNames = append(topicNames, tmpTopicList[index])
  103. }
  104. }
  105. return topicNames
  106. }
  107. //transform struct to byte array for sending
  108. func tweetsToByteArray(tweetsToReturn [][]Tweet, whereFrom int, wantedTopics []string, dataLength int) []byte {
  109. minimumBlockSize := dataLength * maxLength(whereFrom)
  110. tweetsAsBytes := make([]byte, minimumBlockSize)
  111. for _, block := range tweetsToReturn {
  112. var topicPadding []string
  113. var blockToAppend []byte
  114. for index, tweet := range block {
  115. for topicIndex, topic := range tweet.Topics {
  116. blockToAppend = append(blockToAppend, []byte(topic)...)
  117. blockToAppend = append(blockToAppend, ","...)
  118. //gets the topic used for padding
  119. if topicIndex > 0 && index < len(wantedTopics) && topic != wantedTopics[index] {
  120. topicPadding = append(topicPadding, topic)
  121. }
  122. }
  123. //replaces last "," with ";;" bc there is text following and not another topic
  124. blockToAppend = blockToAppend[:len(blockToAppend)-1]
  125. blockToAppend = append(blockToAppend, []byte(";;")[:]...)
  126. blockToAppend = append(blockToAppend, []byte(tweet.Text)...)
  127. blockToAppend = append(blockToAppend, []byte(";")[0])
  128. }
  129. //adds padding
  130. blockToAppend = append(blockToAppend, []byte(";;;")[:]...)
  131. remainingLength := minimumBlockSize - len(blockToAppend)
  132. //grouping using topic from recovered tweets
  133. index := len(topicPadding) - 1
  134. for index >= 0 && remainingLength > 0 && topicPadding[index] != "" {
  135. paddingTweet, err := getNextTweet(topicPadding[index], index, whereFrom)
  136. if err {
  137. break
  138. }
  139. if remainingLength < len(paddingTweet) {
  140. break
  141. }
  142. blockToAppend = append(blockToAppend, paddingTweet...)
  143. remainingLength -= len(paddingTweet)
  144. index--
  145. }
  146. padding := bytes.Repeat([]byte(";"), remainingLength)
  147. blockToAppend = append(blockToAppend, padding...)
  148. Xor(blockToAppend, tweetsAsBytes)
  149. }
  150. return tweetsAsBytes
  151. }
  152. func maxLength(whereFrom int) int {
  153. tmpdb := dbR
  154. if whereFrom == 1 {
  155. tmpdb = archive
  156. }
  157. var max int = 0
  158. for i := range tmpdb {
  159. nrOfTweets := len(tmpdb[i])
  160. if nrOfTweets > max {
  161. max = nrOfTweets
  162. }
  163. }
  164. return max
  165. }
  166. //gets the Tweet at index from wantedTopic for padding
  167. func getNextTweet(wantedTopic string, index, whereFrom int) ([]byte, bool) {
  168. tmpdb := dbR
  169. if whereFrom == 1 {
  170. tmpdb = archive
  171. }
  172. var tweetToReturn Tweet
  173. for tweetIndex, tweet := range tmpdb[wantedTopic] {
  174. if len(tmpdb[wantedTopic]) <= index {
  175. return nil, true
  176. } else if tweetIndex < index {
  177. continue
  178. } else if tweetIndex > index {
  179. break
  180. }
  181. //new Tweet
  182. if tweet.Text != "" {
  183. tweet.RoundPosted = 0
  184. tweetToReturn = tweet
  185. } else {
  186. //"copied" tweet
  187. //find tweet with pointers
  188. tweet = tmpdb[tweet.TopicPointer][tweet.TextPointer]
  189. tweet.RoundPosted = 0
  190. tweetToReturn = tweet
  191. }
  192. }
  193. var tweetToReturnBytes []byte
  194. for _, topic := range tweetToReturn.Topics {
  195. tweetToReturnBytes = append(tweetToReturnBytes, []byte(topic)...)
  196. tweetToReturnBytes = append(tweetToReturnBytes, ","...)
  197. }
  198. //replaces last "," with ";;" bc there is text following and not another topic
  199. tweetToReturnBytes = tweetToReturnBytes[:len(tweetToReturnBytes)-1]
  200. tweetToReturnBytes = append(tweetToReturnBytes, []byte(";;")[:]...)
  201. tweetToReturnBytes = append(tweetToReturnBytes, []byte(tweetToReturn.Text)...)
  202. tweetToReturnBytes = append(tweetToReturnBytes, []byte(";")[0])
  203. return tweetToReturnBytes, false
  204. }
  205. //see func name
  206. func GetTopicList(whereFrom int) ([]byte, int) {
  207. tmpTopicList := topicList
  208. if whereFrom == 1 {
  209. tmpTopicList = archiveTopicList
  210. }
  211. if (len(tmpTopicList)) == 0 {
  212. return nil, 0
  213. }
  214. topicByteArray := new(bytes.Buffer)
  215. json.NewEncoder(topicByteArray).Encode(tmpTopicList)
  216. return topicByteArray.Bytes(), len(tmpTopicList)
  217. }
  218. //iterates through full dbR and moves old tweets to archive
  219. func CleanUpdbR(round int) {
  220. if roundsBeforeArchiving == -1 {
  221. return
  222. }
  223. if roundsBeforeArchivingInc-round == 0 {
  224. keys := make([]string, len(dbR))
  225. i := 0
  226. for k := range dbR {
  227. keys[i] = k
  228. i++
  229. }
  230. sort.Strings(keys)
  231. var tweetsToArchive []Tweet
  232. for _, topic := range keys {
  233. tweets := dbR[topic]
  234. for i := len(tweets) - 1; i >= 0; i-- {
  235. if round-roundsBeforeArchiving >= tweets[i].RoundPosted {
  236. //only adds the tweet to the archive when there is text
  237. if tweets[i].Text != "" {
  238. tweetsToArchive = append(tweetsToArchive, tweets[i])
  239. }
  240. //delets the tweet from the array
  241. row := append(tweets[:i], tweets[i+1:]...)
  242. tweets = row
  243. }
  244. }
  245. dbR[topic] = tweets
  246. }
  247. NewEntries(tweetsToArchive, 1)
  248. roundsBeforeArchivingInc += roundsBeforeArchiving
  249. //redoes the whole dbR to correct pointers
  250. var tweetsToMain []Tweet
  251. for _, topic := range keys {
  252. tweets := dbR[topic]
  253. for _, tweet := range tweets {
  254. if tweet.Text != "" {
  255. tweetsToMain = append(tweetsToMain, tweet)
  256. }
  257. }
  258. }
  259. dbR = nil
  260. dbR = make(map[string][]Tweet)
  261. topicList = nil
  262. NewEntries(tweetsToMain, 0)
  263. }
  264. }
  265. func GetBytesSaved() float64 {
  266. return bytesSaved / float64(len(archive))
  267. }