Created
January 26, 2016 22:03
-
-
Save rendon/2c86807772f4f203321e to your computer and use it in GitHub Desktop.
Testing mongoDB maximum document size storing only 64-bit IDs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Just out of curiosity let's see how many 64-bit IDs can we pack in a mongoDB | |
// document. | |
package main | |
import ( | |
"fmt" | |
"math/rand" | |
"time" | |
"gopkg.in/mgo.v2" | |
) | |
type Doc struct { | |
IDs []int64 `bson:"ids"` | |
} | |
func insert(n int) error { | |
ms, err := mgo.Dial("mongodb-server") | |
if err != nil { | |
return err | |
} | |
defer ms.Close() | |
col := ms.DB("fillingdocs").C("doc") | |
doc := Doc{ | |
IDs: make([]int64, n), | |
} | |
for i := 0; i < n; i++ { | |
doc.IDs[i] = rand.Int63() | |
} | |
if err := col.Insert(&doc); err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
rand.Seed(time.Now().UnixNano()) | |
low := 1 | |
high := 2000000 | |
for low < high { | |
mid := low + (high-low+1)/2 | |
if err := insert(mid); err != nil { | |
fmt.Printf("Too much: %s\n", err) | |
high = mid - 1 | |
} else { | |
fmt.Printf("Not enough\n") | |
low = mid | |
} | |
} | |
fmt.Printf("Max N: %d", high) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment