Browse Source

sending less data to client

Simon 2 năm trước cách đây
mục cha
commit
e59bb32afc
1 tập tin đã thay đổi với 18 bổ sung28 xóa
  1. 18 28
      lib/databaseRead.go

+ 18 - 28
lib/databaseRead.go

@@ -20,9 +20,6 @@ var dbR = make(map[string][]Tweet)
 
 var archive = make(map[string][]Tweet)
 
-//has to be dividable by 32
-var minimumBlockSize int
-
 //needs to be dividable by roundsBeforUpdate
 var roundsBeforeArchiving = 12
 var roundsBeforeArchivingInc = roundsBeforeArchiving
@@ -40,7 +37,6 @@ func NewEntries(inputTweets []Tweet, whereTo int) {
 	var position int = 0
 	for _, tweet := range inputTweets {
 		for index := range tweet.Topics {
-			//fmt.Println("topic to put in", tweet.Topics[index])
 			//new topic
 			if _, ok := tmpdb[tweet.Topics[index]]; !ok {
 				if whereTo == 0 {
@@ -78,20 +74,15 @@ func NewEntries(inputTweets []Tweet, whereTo int) {
 
 //todo! add round to pirquery only get tweets that have been posted from that round onward
 func GetTweets(pirQuery []byte, dataLength int, whereFrom int, pubKey [32]byte) []byte {
-	//fmt.Println("query", pirQuery)
-	//fmt.Println("dbR", dbR)
 	tmpdb := dbR
 	if whereFrom == 1 {
 		tmpdb = archive
 	}
-	//50 is an estimated nr that works, there probably is a smarter way to do this
-	minimumBlockSize = 50 * maxTweetAmount(whereFrom)
 
 	var wantedTopics = getNamesForTopics(pirQuery, whereFrom)
 	tweetsToReturn := make([][]Tweet, len(wantedTopics))
 	for index, wantedTopic := range wantedTopics {
 		for _, tweet := range tmpdb[wantedTopic] {
-			//fmt.Println(tweet)
 			//new Tweet
 			if tweet.Text != "" {
 				tweet.RoundPosted = 0
@@ -109,21 +100,6 @@ func GetTweets(pirQuery []byte, dataLength int, whereFrom int, pubKey [32]byte)
 	return tweetsToByteArray(tweetsToReturn, whereFrom, wantedTopics)
 }
 
-func maxTweetAmount(whereFrom int) int {
-	tmpdb := dbR
-	if whereFrom == 1 {
-		tmpdb = archive
-	}
-	var max int = 0
-	for i := range tmpdb {
-		nrOfTweets := len(tmpdb[i])
-		if nrOfTweets > max {
-			max = nrOfTweets
-		}
-	}
-	return max
-}
-
 func getNamesForTopics(wantedIndices []byte, whereFrom int) []string {
 	var topicNames []string
 	tmpTopicList := topicList
@@ -143,6 +119,9 @@ func getNamesForTopics(wantedIndices []byte, whereFrom int) []string {
 
 //transform struct to byte array for sending
 func tweetsToByteArray(tweetsToReturn [][]Tweet, whereFrom int, wantedTopics []string) []byte {
+	//50 is an estimated nr that works, there probably is a smarter way to do this
+	minimumBlockSize := 50 * maxTweetAmount(whereFrom)
+
 	tweetsAsBytes := make([]byte, minimumBlockSize)
 	for _, block := range tweetsToReturn {
 		var topicPadding []string
@@ -188,13 +167,26 @@ func tweetsToByteArray(tweetsToReturn [][]Tweet, whereFrom int, wantedTopics []s
 		blockToAppend = append(blockToAppend, padding...)
 		Xor(blockToAppend, tweetsAsBytes)
 
-		//fmt.Println(tweetsAsBytes)
 	}
 
-	//fmt.Println("length Returned", len(tweetsAsBytes))
 	return tweetsAsBytes
 }
 
+func maxTweetAmount(whereFrom int) int {
+	tmpdb := dbR
+	if whereFrom == 1 {
+		tmpdb = archive
+	}
+	var max int = 0
+	for i := range tmpdb {
+		nrOfTweets := len(tmpdb[i])
+		if nrOfTweets > max {
+			max = nrOfTweets
+		}
+	}
+	return max
+}
+
 //gets the Tweet at index from wantedTopic for padding
 func getNextTweet(wantedTopic string, index, whereFrom int) ([]byte, bool) {
 	tmpdb := dbR
@@ -225,7 +217,6 @@ func getNextTweet(wantedTopic string, index, whereFrom int) ([]byte, bool) {
 		}
 	}
 
-	//fmt.Println("nextTweet", tweetToReturn)
 	var tweetToReturnBytes []byte
 	for _, topic := range tweetToReturn.Topics {
 		tweetToReturnBytes = append(tweetToReturnBytes, []byte(topic)...)
@@ -288,7 +279,6 @@ func CleanUpdbR(round int) {
 			}
 			dbR[topic] = tweets
 		}
-		//fmt.Println("tweetsToArchive", len(tweetsToArchive))
 
 		NewEntries(tweetsToArchive, 1)