How to use compact method of db Package

Best Syzkaller code snippet using db.compact

corrupt_test.go

Source:corrupt_test.go Github

copy

Full Screen

...216func TestCorruptDB_Table(t *testing.T) {217 h := newDbCorruptHarness(t)218 defer h.close()219 h.build(100)220 h.compactMem()221 h.compactRangeAt(0, "", "")222 h.compactRangeAt(1, "", "")223 h.closeDB()224 h.corrupt(storage.TypeTable, -1, 100, 1)225 h.openDB()226 h.check(99, 99)227}228func TestCorruptDB_TableIndex(t *testing.T) {229 h := newDbCorruptHarness(t)230 defer h.close()231 h.build(10000)232 h.compactMem()233 h.closeDB()234 h.corrupt(storage.TypeTable, -1, -2000, 500)235 h.openDB()236 h.check(5000, 9999)237}238func TestCorruptDB_MissingManifest(t *testing.T) {239 rnd := rand.New(rand.NewSource(0x0badda7a))240 h := newDbCorruptHarnessWopt(t, &opt.Options{241 BlockCacheCapacity: 100,242 Strict: opt.StrictJournalChecksum,243 WriteBuffer: 1000 * 60,244 })245 defer h.close()246 h.build(1000)247 h.compactMem()248 h.buildShuffled(1000, rnd)249 h.compactMem()250 h.deleteRand(500, 1000, rnd)251 h.compactMem()252 h.buildShuffled(1000, rnd)253 h.compactMem()254 h.deleteRand(500, 1000, rnd)255 h.compactMem()256 h.buildShuffled(1000, rnd)257 h.compactMem()258 h.closeDB()259 h.forceRemoveAll(storage.TypeManifest)260 h.openAssert(false)261 h.recover()262 h.check(1000, 1000)263 h.build(1000)264 h.compactMem()265 h.compactRange("", "")266 h.closeDB()267 h.recover()268 h.check(1000, 1000)269}270func TestCorruptDB_SequenceNumberRecovery(t *testing.T) {271 h := newDbCorruptHarness(t)272 defer h.close()273 h.put("foo", "v1")274 h.put("foo", "v2")275 h.put("foo", "v3")276 h.put("foo", "v4")277 h.put("foo", "v5")278 h.closeDB()279 h.recover()280 h.getVal("foo", "v5")281 h.put("foo", "v6")282 h.getVal("foo", "v6")283 h.reopenDB()284 h.getVal("foo", "v6")285}286func TestCorruptDB_SequenceNumberRecoveryTable(t *testing.T) {287 h := newDbCorruptHarness(t)288 defer h.close()289 h.put("foo", "v1")290 h.put("foo", "v2")291 h.put("foo", "v3")292 h.compactMem()293 h.put("foo", "v4")294 h.put("foo", "v5")295 h.compactMem()296 h.closeDB()297 h.recover()298 h.getVal("foo", "v5")299 h.put("foo", "v6")300 h.getVal("foo", "v6")301 h.reopenDB()302 h.getVal("foo", "v6")303}304func TestCorruptDB_CorruptedManifest(t *testing.T) {305 h := newDbCorruptHarness(t)306 defer h.close()307 h.put("foo", "hello")308 h.compactMem()309 h.compactRange("", "")310 h.closeDB()311 h.corrupt(storage.TypeManifest, -1, 0, 1000)312 h.openAssert(false)313 h.recover()314 h.getVal("foo", "hello")315}316func TestCorruptDB_CompactionInputError(t *testing.T) {317 h := newDbCorruptHarness(t)318 defer h.close()319 h.build(10)320 h.compactMem()321 h.closeDB()322 h.corrupt(storage.TypeTable, -1, 100, 1)323 h.openDB()324 h.check(9, 9)325 h.build(10000)326 h.check(10000, 10000)327}328func TestCorruptDB_UnrelatedKeys(t *testing.T) {329 h := newDbCorruptHarness(t)330 defer h.close()331 h.build(10)332 h.compactMem()333 h.closeDB()334 h.corrupt(storage.TypeTable, -1, 100, 1)335 h.openDB()336 h.put(string(tkey(1000)), string(tval(1000, ctValSize)))337 h.getVal(string(tkey(1000)), string(tval(1000, ctValSize)))338 h.compactMem()339 h.getVal(string(tkey(1000)), string(tval(1000, ctValSize)))340}341func TestCorruptDB_Level0NewerFileHasOlderSeqnum(t *testing.T) {342 h := newDbCorruptHarness(t)343 defer h.close()344 h.put("a", "v1")345 h.put("b", "v1")346 h.compactMem()347 h.put("a", "v2")348 h.put("b", "v2")349 h.compactMem()350 h.put("a", "v3")351 h.put("b", "v3")352 h.compactMem()353 h.put("c", "v0")354 h.put("d", "v0")355 h.compactMem()356 h.compactRangeAt(1, "", "")357 h.closeDB()358 h.recover()359 h.getVal("a", "v3")360 h.getVal("b", "v3")361 h.getVal("c", "v0")362 h.getVal("d", "v0")363}364func TestCorruptDB_RecoverInvalidSeq_Issue53(t *testing.T) {365 h := newDbCorruptHarness(t)366 defer h.close()367 h.put("a", "v1")368 h.put("b", "v1")369 h.compactMem()370 h.put("a", "v2")371 h.put("b", "v2")372 h.compactMem()373 h.put("a", "v3")374 h.put("b", "v3")375 h.compactMem()376 h.put("c", "v0")377 h.put("d", "v0")378 h.compactMem()379 h.compactRangeAt(0, "", "")380 h.closeDB()381 h.recover()382 h.getVal("a", "v3")383 h.getVal("b", "v3")384 h.getVal("c", "v0")385 h.getVal("d", "v0")386}387func TestCorruptDB_MissingTableFiles(t *testing.T) {388 h := newDbCorruptHarness(t)389 defer h.close()390 h.put("a", "v1")391 h.put("b", "v1")392 h.compactMem()393 h.put("c", "v2")394 h.put("d", "v2")395 h.compactMem()396 h.put("e", "v3")397 h.put("f", "v3")398 h.closeDB()399 h.removeOne(storage.TypeTable)400 h.openAssert(false)401}402func TestCorruptDB_RecoverTable(t *testing.T) {403 h := newDbCorruptHarnessWopt(t, &opt.Options{404 WriteBuffer: 112 * opt.KiB,405 CompactionTableSize: 90 * opt.KiB,406 Filter: filter.NewBloomFilter(10),407 })408 defer h.close()409 h.build(1000)410 h.compactMem()411 h.compactRangeAt(0, "", "")412 h.compactRangeAt(1, "", "")413 seq := h.db.seq414 h.closeDB()415 h.corrupt(storage.TypeTable, 0, 1000, 1)416 h.corrupt(storage.TypeTable, 3, 10000, 1)417 // Corrupted filter shouldn't affect recovery.418 h.corrupt(storage.TypeTable, 3, 113888, 10)419 h.corrupt(storage.TypeTable, -1, 20000, 1)420 h.recover()421 if h.db.seq != seq {422 t.Errorf("invalid seq, want=%d got=%d", seq, h.db.seq)423 }424 h.check(985, 985)425}...

Full Screen

Full Screen

compact

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := leveldb.OpenFile("testdb", nil)4 if err != nil {5 fmt.Println(err)6 }7 defer db.Close()8 db.CompactRange(opt.Range{nil, nil})9}

Full Screen

Full Screen

compact

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := sql.Open("sqlite3", "./foo.db")4 if err != nil {5 log.Fatal(err)6 }7 defer db.Close()8 _, err = db.Exec("VACUUM")9 if err != nil {10 log.Fatal(err)11 }12 fmt.Println("Database compacted")13}

Full Screen

Full Screen

compact

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})4 if err != nil {5 log.Fatal(err)6 }7 defer db.Close()8 err = db.Update(func(tx *bolt.Tx) error {9 _, err := tx.CreateBucketIfNotExists([]byte("MyBucket"))10 if err != nil {11 return fmt.Errorf("create bucket: %s", err)12 }13 })14 if err != nil {15 log.Fatal(err)16 }17 f, err := os.OpenFile("my.db", os.O_RDWR, 0666)18 if err != nil {19 log.Fatal(err)20 }21 defer f.Close()22 if err := f.Truncate(10); err != nil {23 log.Fatal(err)24 }25 if err := f.Close(); err != nil {26 log.Fatal(err)27 }28 db, err = bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})29 if err != nil {30 log.Fatal(err)31 }32 defer db.Close()33}34import (35func main() {36 db, err := bolt.Open("my.db", 0600, &bolt.Options{Timeout: 1 * time.Second})37 if err != nil {38 log.Fatal(err)39 }40 defer db.Close()

Full Screen

Full Screen

compact

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := leveldb.OpenFile("db", &opt.Options{ErrorIfMissing: true})4 if err != nil {5 fmt.Println(err)6 }7 defer db.Close()8 err = db.CompactRange(nil, nil)9 if err != nil {10 fmt.Println(err)11 }12}

Full Screen

Full Screen

compact

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 db, err := leveldb.OpenFile("db", nil)4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 defer db.Close()9 for i := 0; i < 1000; i++ {10 key := []byte("key" + strconv.Itoa(i))11 value := []byte("value" + strconv.Itoa(i))12 err = db.Put(key, value, nil)13 if err != nil {14 fmt.Println(err)15 os.Exit(1)16 }17 }18 start := time.Now()19 db.CompactRange(nil, nil)20 fmt.Println("Compact time:", time.Since(start))21 for i := 0; i < 1000; i++ {22 key := []byte("key" + strconv.Itoa(i))23 value := []byte("value" + strconv.Itoa(i))24 err = db.Put(key, value, nil)25 if err != nil {26 fmt.Println(err)27 os.Exit(1)28 }29 }30 start = time.Now()31 db.CompactRange(nil, nil)32 fmt.Println("Compact time:", time.Since(start))33}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful