Skip to content

Instantly share code, notes, and snippets.

@axllent
Created August 8, 2022 22:18
Show Gist options
  • Save axllent/6d7f01846a184743838992965355bf79 to your computer and use it in GitHub Desktop.
Save axllent/6d7f01846a184743838992965355bf79 to your computer and use it in GitHub Desktop.
A test for GCReclaim
package main
import (
"fmt"
"log"
"math/rand"
"time"
"github.com/ostafen/clover/v2"
)
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
func checkErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func main() {
db, err := clover.Open("./db", clover.WithGCReclaimInterval(time.Second*5))
checkErr(err)
defer db.Close()
has, err := db.HasCollection("test")
checkErr(err)
if !has {
checkErr(db.CreateCollection("test"))
// checkErr(db.CreateIndex("test", "timestamp"))
}
// create a 10,000 byte string
str := RandStringRunes(10000)
log.Println("Inserting 100,000 documents")
for i := 0; i < 100000; i++ {
if i > 0 && i%1000 == 0 {
log.Printf("Inserted %d documents", i)
}
doc := clover.NewDocument()
doc.Set("timestamp", time.Now().UnixNano())
doc.Set("Data", str)
_, err := db.InsertOne("test", doc)
checkErr(err)
}
printDocumentsInDB(db)
fmt.Println("Deleting 100,000 documents")
deleteAll(db)
printDocumentsInDB(db)
for i := 0; i < 10; i++ {
n := 1000
log.Printf("Inserting %d documents", n)
for i := 0; i < n; i++ {
doc := clover.NewDocument()
doc.Set("timestamp", time.Now().UnixNano())
doc.Set("Data", str)
_, err := db.InsertOne("test", doc)
checkErr(err)
}
printDocumentsInDB(db)
log.Printf("Deleting %d documents", n)
deleteAll(db)
printDocumentsInDB(db)
}
log.Println("Pause an extra 30 seconds to show GC isn't cleaning up an empty database effectively")
time.Sleep(30 * time.Second)
}
func deleteAll(db *clover.DB) {
for {
toDelete, err := db.Count(clover.NewQuery("test"))
checkErr(err)
if toDelete == 0 {
break
}
checkErr(db.Delete(clover.NewQuery("test").Limit(2500)))
}
// makes no difference if I delete documents or drop/create the collection
// checkErr(db.DropCollection("test"))
// checkErr(db.CreateCollection("test"))
}
// print totals
func printDocumentsInDB(db *clover.DB) {
n, err := db.Count(clover.NewQuery("test"))
checkErr(err)
log.Printf("There are now %d documents in DB", n)
}
// RandStringRunes generates a random string
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment