How to use Put method of run Package

Best Keploy code snippet using run.Put

bitcask_test.go

Source:bitcask_test.go Github

copy

Full Screen

...58 t.Run("Open", func(t *testing.T) {59 db, err = Open(testdir)60 assert.NoError(err)61 })62 t.Run("Put", func(t *testing.T) {63 err = db.Put([]byte([]byte("foo")), []byte("bar"))64 assert.NoError(err)65 })66 t.Run("Get", func(t *testing.T) {67 val, err := db.Get([]byte("foo"))68 assert.NoError(err)69 assert.Equal([]byte("bar"), val)70 })71 t.Run("Len", func(t *testing.T) {72 assert.Equal(1, db.Len())73 })74 t.Run("Has", func(t *testing.T) {75 assert.True(db.Has([]byte("foo")))76 })77 t.Run("Keys", func(t *testing.T) {78 keys := make([][]byte, 0)79 for key := range db.Keys() {80 keys = append(keys, key)81 }82 assert.Equal([][]byte{[]byte("foo")}, keys)83 })84 t.Run("Fold", func(t *testing.T) {85 var (86 keys [][]byte87 values [][]byte88 )89 err := db.Fold(func(key []byte) error {90 value, err := db.Get(key)91 if err != nil {92 return err93 }94 keys = append(keys, key)95 values = append(values, value)96 return nil97 })98 assert.NoError(err)99 assert.Equal([][]byte{[]byte("foo")}, keys)100 assert.Equal([][]byte{[]byte("bar")}, values)101 })102 t.Run("Delete", func(t *testing.T) {103 err := db.Delete([]byte("foo"))104 assert.NoError(err)105 _, err = db.Get([]byte("foo"))106 assert.Error(err)107 assert.Equal(ErrKeyNotFound, err)108 })109 t.Run("Sync", func(t *testing.T) {110 err = db.Sync()111 assert.NoError(err)112 })113 t.Run("Close", func(t *testing.T) {114 err = db.Close()115 assert.NoError(err)116 })117}118func TestDeleteAll(t *testing.T) {119 assert := assert.New(t)120 testdir, _ := ioutil.TempDir("", "bitcask")121 db, _ := Open(testdir)122 _ = db.Put([]byte("foo"), []byte("foo"))123 _ = db.Put([]byte("bar"), []byte("bar"))124 _ = db.Put([]byte("baz"), []byte("baz"))125 assert.Equal(3, db.Len())126 err := db.DeleteAll()127 assert.NoError(err)128 assert.Equal(0, db.Len())129 _, err = db.Get([]byte("foo"))130 assert.Equal(ErrKeyNotFound, err)131 _, err = db.Get([]byte("bar"))132 assert.Equal(ErrKeyNotFound, err)133 _, err = db.Get([]byte("baz"))134 assert.Equal(ErrKeyNotFound, err)135}136func TestReopen1(t *testing.T) {137 assert := assert.New(t)138 for i := 0; i < 10; i++ {139 testdir, _ := ioutil.TempDir("", "bitcask")140 db, _ := Open(testdir, WithMaxDatafileSize(1))141 _ = db.Put([]byte("foo"), []byte("bar"))142 _ = db.Put([]byte("foo"), []byte("bar1"))143 _ = db.Put([]byte("foo"), []byte("bar2"))144 _ = db.Put([]byte("foo"), []byte("bar3"))145 _ = db.Put([]byte("foo"), []byte("bar4"))146 _ = db.Put([]byte("foo"), []byte("bar5"))147 _ = db.Reopen()148 val, _ := db.Get([]byte("foo"))149 assert.Equal("bar5", string(val))150 }151}152func TestReopen(t *testing.T) {153 assert := assert.New(t)154 testdir, err := ioutil.TempDir("", "bitcask")155 assert.NoError(err)156 t.Run("Reopen", func(t *testing.T) {157 var (158 db *Bitcask159 err error160 )161 t.Run("Open", func(t *testing.T) {162 db, err = Open(testdir)163 assert.NoError(err)164 })165 t.Run("Put", func(t *testing.T) {166 err = db.Put([]byte("foo"), []byte("bar"))167 assert.NoError(err)168 })169 t.Run("Get", func(t *testing.T) {170 val, err := db.Get([]byte("foo"))171 assert.NoError(err)172 assert.Equal([]byte("bar"), val)173 })174 t.Run("Reopen", func(t *testing.T) {175 err = db.Reopen()176 assert.NoError(err)177 })178 t.Run("GetAfterReopen", func(t *testing.T) {179 val, err := db.Get([]byte("foo"))180 assert.NoError(err)181 assert.Equal([]byte("bar"), val)182 })183 t.Run("PutAfterReopen", func(t *testing.T) {184 err = db.Put([]byte("zzz"), []byte("foo"))185 assert.NoError(err)186 })187 t.Run("GetAfterReopenAndPut", func(t *testing.T) {188 val, err := db.Get([]byte("zzz"))189 assert.NoError(err)190 assert.Equal([]byte("foo"), val)191 })192 t.Run("Close", func(t *testing.T) {193 err = db.Close()194 assert.NoError(err)195 })196 })197}198func TestDeletedKeys(t *testing.T) {199 assert := assert.New(t)200 testdir, err := ioutil.TempDir("", "bitcask")201 assert.NoError(err)202 t.Run("Setup", func(t *testing.T) {203 var (204 db *Bitcask205 err error206 )207 t.Run("Open", func(t *testing.T) {208 db, err = Open(testdir)209 assert.NoError(err)210 })211 t.Run("Put", func(t *testing.T) {212 err = db.Put([]byte("foo"), []byte("bar"))213 assert.NoError(err)214 })215 t.Run("Get", func(t *testing.T) {216 val, err := db.Get([]byte("foo"))217 assert.NoError(err)218 assert.Equal([]byte("bar"), val)219 })220 t.Run("Delete", func(t *testing.T) {221 err := db.Delete([]byte("foo"))222 assert.NoError(err)223 _, err = db.Get([]byte("foo"))224 assert.Error(err)225 assert.Equal(ErrKeyNotFound, err)226 })227 t.Run("Sync", func(t *testing.T) {228 err = db.Sync()229 assert.NoError(err)230 })231 t.Run("Close", func(t *testing.T) {232 err = db.Close()233 assert.NoError(err)234 })235 })236 t.Run("Reopen", func(t *testing.T) {237 var (238 db *Bitcask239 err error240 )241 t.Run("Open", func(t *testing.T) {242 db, err = Open(testdir)243 assert.NoError(err)244 })245 t.Run("Get", func(t *testing.T) {246 _, err = db.Get([]byte("foo"))247 assert.Error(err)248 assert.Equal(ErrKeyNotFound, err)249 })250 t.Run("Close", func(t *testing.T) {251 err = db.Close()252 assert.NoError(err)253 })254 })255}256func TestConfigErrors(t *testing.T) {257 assert := assert.New(t)258 t.Run("CorruptConfig", func(t *testing.T) {259 testdir, err := ioutil.TempDir("", "bitcask")260 assert.NoError(err)261 defer os.RemoveAll(testdir)262 db, err := Open(testdir)263 assert.NoError(err)264 assert.NoError(db.Close())265 assert.NoError(ioutil.WriteFile(filepath.Join(testdir, "config.json"), []byte("foo bar baz"), 0600))266 _, err = Open(testdir)267 assert.Error(err)268 })269 t.Run("BadConfigPath", func(t *testing.T) {270 testdir, err := ioutil.TempDir("", "bitcask")271 assert.NoError(err)272 defer os.RemoveAll(testdir)273 assert.NoError(os.Mkdir(filepath.Join(testdir, "config.json"), 0700))274 _, err = Open(testdir)275 assert.Error(err)276 })277}278func TestReIndex(t *testing.T) {279 assert := assert.New(t)280 testdir, err := ioutil.TempDir("", "bitcask")281 assert.NoError(err)282 t.Run("Setup", func(t *testing.T) {283 var (284 db *Bitcask285 err error286 )287 t.Run("Open", func(t *testing.T) {288 db, err = Open(testdir)289 assert.NoError(err)290 })291 t.Run("Put", func(t *testing.T) {292 err = db.Put([]byte("foo"), []byte("bar"))293 assert.NoError(err)294 })295 t.Run("Get", func(t *testing.T) {296 val, err := db.Get([]byte("foo"))297 assert.NoError(err)298 assert.Equal([]byte("bar"), val)299 })300 t.Run("Sync", func(t *testing.T) {301 err = db.Sync()302 assert.NoError(err)303 })304 t.Run("Close", func(t *testing.T) {305 err = db.Close()306 assert.NoError(err)307 })308 t.Run("DeleteIndex", func(t *testing.T) {309 err := os.Remove(filepath.Join(testdir, "index"))310 assert.NoError(err)311 })312 })313 t.Run("Reopen", func(t *testing.T) {314 var (315 db *Bitcask316 err error317 )318 t.Run("Open", func(t *testing.T) {319 db, err = Open(testdir)320 assert.NoError(err)321 })322 t.Run("Get", func(t *testing.T) {323 val, err := db.Get([]byte("foo"))324 assert.NoError(err)325 assert.Equal([]byte("bar"), val)326 })327 t.Run("Close", func(t *testing.T) {328 err = db.Close()329 assert.NoError(err)330 })331 })332}333func TestReIndexDeletedKeys(t *testing.T) {334 assert := assert.New(t)335 testdir, err := ioutil.TempDir("", "bitcask")336 assert.NoError(err)337 t.Run("Setup", func(t *testing.T) {338 var (339 db *Bitcask340 err error341 )342 t.Run("Open", func(t *testing.T) {343 db, err = Open(testdir)344 assert.NoError(err)345 })346 t.Run("Put", func(t *testing.T) {347 err = db.Put([]byte("foo"), []byte("bar"))348 assert.NoError(err)349 })350 t.Run("Get", func(t *testing.T) {351 val, err := db.Get([]byte("foo"))352 assert.NoError(err)353 assert.Equal([]byte("bar"), val)354 })355 t.Run("Delete", func(t *testing.T) {356 err := db.Delete([]byte("foo"))357 assert.NoError(err)358 _, err = db.Get([]byte("foo"))359 assert.Error(err)360 assert.Equal(ErrKeyNotFound, err)361 })362 t.Run("Sync", func(t *testing.T) {363 err = db.Sync()364 assert.NoError(err)365 })366 t.Run("Close", func(t *testing.T) {367 err = db.Close()368 assert.NoError(err)369 })370 t.Run("DeleteIndex", func(t *testing.T) {371 err := os.Remove(filepath.Join(testdir, "index"))372 assert.NoError(err)373 })374 })375 t.Run("Reopen", func(t *testing.T) {376 var (377 db *Bitcask378 err error379 )380 t.Run("Open", func(t *testing.T) {381 db, err = Open(testdir)382 assert.NoError(err)383 })384 t.Run("Get", func(t *testing.T) {385 _, err := db.Get([]byte("foo"))386 assert.Error(err)387 assert.Equal(ErrKeyNotFound, err)388 })389 t.Run("Close", func(t *testing.T) {390 err = db.Close()391 assert.NoError(err)392 })393 })394}395func TestSync(t *testing.T) {396 assert := assert.New(t)397 testdir, err := ioutil.TempDir("", "bitcask")398 assert.NoError(err)399 var db *Bitcask400 t.Run("Open", func(t *testing.T) {401 db, err = Open(testdir, WithSync(true))402 assert.NoError(err)403 })404 t.Run("Put", func(t *testing.T) {405 key := []byte(strings.Repeat(" ", 17))406 value := []byte("foobar")407 err = db.Put(key, value)408 })409}410func TestMaxKeySize(t *testing.T) {411 assert := assert.New(t)412 testdir, err := ioutil.TempDir("", "bitcask")413 assert.NoError(err)414 var db *Bitcask415 t.Run("Open", func(t *testing.T) {416 db, err = Open(testdir, WithMaxKeySize(16))417 assert.NoError(err)418 })419 t.Run("Put", func(t *testing.T) {420 key := []byte(strings.Repeat(" ", 17))421 value := []byte("foobar")422 err = db.Put(key, value)423 assert.Error(err)424 assert.Equal(ErrKeyTooLarge, err)425 })426}427func TestMaxValueSize(t *testing.T) {428 assert := assert.New(t)429 testdir, err := ioutil.TempDir("", "bitcask")430 assert.NoError(err)431 var db *Bitcask432 t.Run("Open", func(t *testing.T) {433 db, err = Open(testdir, WithMaxValueSize(16))434 assert.NoError(err)435 })436 t.Run("Put", func(t *testing.T) {437 key := []byte("foo")438 value := []byte(strings.Repeat(" ", 17))439 err = db.Put(key, value)440 assert.Error(err)441 assert.Equal(ErrValueTooLarge, err)442 })443}444func TestStats(t *testing.T) {445 var (446 db *Bitcask447 err error448 )449 assert := assert.New(t)450 testdir, err := ioutil.TempDir("", "bitcask")451 assert.NoError(err)452 t.Run("Setup", func(t *testing.T) {453 t.Run("Open", func(t *testing.T) {454 db, err = Open(testdir)455 assert.NoError(err)456 })457 t.Run("Put", func(t *testing.T) {458 err := db.Put([]byte("foo"), []byte("bar"))459 assert.NoError(err)460 })461 t.Run("Get", func(t *testing.T) {462 val, err := db.Get([]byte("foo"))463 assert.NoError(err)464 assert.Equal([]byte("bar"), val)465 })466 t.Run("Stats", func(t *testing.T) {467 stats, err := db.Stats()468 assert.NoError(err)469 assert.Equal(stats.Datafiles, 0)470 assert.Equal(stats.Keys, 1)471 })472 t.Run("Sync", func(t *testing.T) {473 err = db.Sync()474 assert.NoError(err)475 })476 t.Run("Close", func(t *testing.T) {477 err = db.Close()478 assert.NoError(err)479 })480 })481}482func TestStatsError(t *testing.T) {483 var (484 db *Bitcask485 err error486 )487 assert := assert.New(t)488 testdir, err := ioutil.TempDir("", "bitcask")489 assert.NoError(err)490 t.Run("Setup", func(t *testing.T) {491 t.Run("Open", func(t *testing.T) {492 db, err = Open(testdir)493 assert.NoError(err)494 })495 t.Run("Put", func(t *testing.T) {496 err := db.Put([]byte("foo"), []byte("bar"))497 assert.NoError(err)498 })499 t.Run("Get", func(t *testing.T) {500 val, err := db.Get([]byte("foo"))501 assert.NoError(err)502 assert.Equal([]byte("bar"), val)503 })504 t.Run("Stats", func(t *testing.T) {505 stats, err := db.Stats()506 assert.NoError(err)507 assert.Equal(stats.Datafiles, 0)508 assert.Equal(stats.Keys, 1)509 })510 })511 t.Run("Test", func(t *testing.T) {512 skipIfWindows(t)513 t.Run("FabricatedDestruction", func(t *testing.T) {514 // This would never happen in reality :D515 // Or would it? :)516 err = os.RemoveAll(testdir)517 assert.NoError(err)518 })519 t.Run("Stats", func(t *testing.T) {520 _, err := db.Stats()521 assert.Error(err)522 })523 })524}525func TestMaxDatafileSize(t *testing.T) {526 var (527 db *Bitcask528 err error529 )530 assert := assert.New(t)531 testdir, err := ioutil.TempDir("", "bitcask")532 assert.NoError(err)533 defer os.RemoveAll(testdir)534 t.Run("Setup", func(t *testing.T) {535 t.Run("Open", func(t *testing.T) {536 db, err = Open(testdir, WithMaxDatafileSize(32))537 assert.NoError(err)538 })539 t.Run("Put", func(t *testing.T) {540 err := db.Put([]byte("foo"), []byte("bar"))541 assert.NoError(err)542 })543 })544 t.Run("Put", func(t *testing.T) {545 for i := 0; i < 10; i++ {546 err := db.Put([]byte(fmt.Sprintf("key_%d", i)), []byte("bar"))547 assert.NoError(err)548 }549 })550 t.Run("Sync", func(t *testing.T) {551 err = db.Sync()552 assert.NoError(err)553 })554 t.Run("Get", func(t *testing.T) {555 val, err := db.Get([]byte("foo"))556 assert.NoError(err)557 assert.Equal([]byte("bar"), val)558 for i := 0; i < 10; i++ {559 val, err = db.Get([]byte(fmt.Sprintf("key_%d", i)))560 assert.NoError(err)561 assert.Equal([]byte("bar"), val)562 }563 })564 t.Run("Close", func(t *testing.T) {565 err = db.Close()566 assert.NoError(err)567 })568}569func TestMerge(t *testing.T) {570 var (571 db *Bitcask572 err error573 )574 assert := assert.New(t)575 testdir, err := ioutil.TempDir("", "bitcask")576 assert.NoError(err)577 t.Run("Setup", func(t *testing.T) {578 t.Run("Open", func(t *testing.T) {579 db, err = Open(testdir, WithMaxDatafileSize(32))580 assert.NoError(err)581 })582 t.Run("Put", func(t *testing.T) {583 err := db.Put([]byte("foo"), []byte("bar"))584 assert.NoError(err)585 })586 s1, err := db.Stats()587 assert.NoError(err)588 assert.Equal(0, s1.Datafiles)589 assert.Equal(1, s1.Keys)590 t.Run("Put", func(t *testing.T) {591 for i := 0; i < 10; i++ {592 err := db.Put([]byte("foo"), []byte("bar"))593 assert.NoError(err)594 }595 })596 s2, err := db.Stats()597 assert.NoError(err)598 assert.Equal(5, s2.Datafiles)599 assert.Equal(1, s2.Keys)600 assert.True(s2.Size > s1.Size)601 t.Run("Merge", func(t *testing.T) {602 err := db.Merge()603 assert.NoError(err)604 })605 s3, err := db.Stats()606 assert.NoError(err)607 assert.Equal(1, s3.Datafiles)608 assert.Equal(1, s3.Keys)609 assert.True(s3.Size > s1.Size)610 assert.True(s3.Size < s2.Size)611 t.Run("Sync", func(t *testing.T) {612 err = db.Sync()613 assert.NoError(err)614 })615 t.Run("Close", func(t *testing.T) {616 err = db.Close()617 assert.NoError(err)618 })619 })620}621func TestGetErrors(t *testing.T) {622 assert := assert.New(t)623 t.Run("ReadError", func(t *testing.T) {624 testdir, err := ioutil.TempDir("", "bitcask")625 assert.NoError(err)626 defer os.RemoveAll(testdir)627 db, err := Open(testdir, WithMaxDatafileSize(32))628 assert.NoError(err)629 err = db.Put([]byte("foo"), []byte("bar"))630 assert.NoError(err)631 mockDatafile := new(mocks.Datafile)632 mockDatafile.On("FileID").Return(0)633 mockDatafile.On("ReadAt", int64(0), int64(22)).Return(634 internal.Entry{},635 ErrMockError,636 )637 db.curr = mockDatafile638 _, err = db.Get([]byte("foo"))639 assert.Error(err)640 assert.Equal(ErrMockError, err)641 })642 t.Run("ChecksumError", func(t *testing.T) {643 testdir, err := ioutil.TempDir("", "bitcask")644 assert.NoError(err)645 defer os.RemoveAll(testdir)646 db, err := Open(testdir, WithMaxDatafileSize(32))647 assert.NoError(err)648 err = db.Put([]byte("foo"), []byte("bar"))649 assert.NoError(err)650 mockDatafile := new(mocks.Datafile)651 mockDatafile.On("FileID").Return(0)652 mockDatafile.On("ReadAt", int64(0), int64(22)).Return(653 internal.Entry{654 Checksum: 0x0,655 Key: []byte("foo"),656 Offset: 0,657 Value: []byte("bar"),658 },659 nil,660 )661 db.curr = mockDatafile662 _, err = db.Get([]byte("foo"))663 assert.Error(err)664 assert.Equal(ErrChecksumFailed, err)665 })666}667func TestPutErrors(t *testing.T) {668 assert := assert.New(t)669 t.Run("WriteError", func(t *testing.T) {670 testdir, err := ioutil.TempDir("", "bitcask")671 assert.NoError(err)672 db, err := Open(testdir)673 assert.NoError(err)674 mockDatafile := new(mocks.Datafile)675 mockDatafile.On("Size").Return(int64(0))676 mockDatafile.On(677 "Write",678 internal.Entry{679 Checksum: 0x76ff8caa,680 Key: []byte("foo"),681 Offset: 0,682 Value: []byte("bar"),683 },684 ).Return(int64(0), int64(0), ErrMockError)685 db.curr = mockDatafile686 err = db.Put([]byte("foo"), []byte("bar"))687 assert.Error(err)688 assert.Equal(ErrMockError, err)689 })690 t.Run("SyncError", func(t *testing.T) {691 testdir, err := ioutil.TempDir("", "bitcask")692 assert.NoError(err)693 db, err := Open(testdir, WithSync(true))694 assert.NoError(err)695 mockDatafile := new(mocks.Datafile)696 mockDatafile.On("Size").Return(int64(0))697 mockDatafile.On(698 "Write",699 internal.Entry{700 Checksum: 0x78240498,701 Key: []byte("bar"),702 Offset: 0,703 Value: []byte("baz"),704 },705 ).Return(int64(0), int64(0), nil)706 mockDatafile.On("Sync").Return(ErrMockError)707 db.curr = mockDatafile708 err = db.Put([]byte("bar"), []byte("baz"))709 assert.Error(err)710 assert.Equal(ErrMockError, err)711 })712}713func TestOpenErrors(t *testing.T) {714 assert := assert.New(t)715 t.Run("BadPath", func(t *testing.T) {716 testdir, err := ioutil.TempDir("", "bitcask")717 assert.NoError(err)718 defer os.RemoveAll(testdir)719 assert.NoError(ioutil.WriteFile(filepath.Join(testdir, "foo"), []byte("foo"), 0600))720 _, err = Open(filepath.Join(testdir, "foo", "tmp.db"))721 assert.Error(err)722 })723 t.Run("BadOption", func(t *testing.T) {724 testdir, err := ioutil.TempDir("", "bitcask")725 assert.NoError(err)726 defer os.RemoveAll(testdir)727 withBogusOption := func() Option {728 return func(cfg *config.Config) error {729 return errors.New("mocked error")730 }731 }732 _, err = Open(testdir, withBogusOption())733 assert.Error(err)734 })735 t.Run("LoadDatafilesError", func(t *testing.T) {736 testdir, err := ioutil.TempDir("", "bitcask")737 assert.NoError(err)738 defer os.RemoveAll(testdir)739 db, err := Open(testdir)740 assert.NoError(err)741 err = db.Put([]byte("foo"), []byte("bar"))742 assert.NoError(err)743 err = db.Close()744 assert.NoError(err)745 // Simulate some horrible that happened to the datafiles!746 err = os.Rename(filepath.Join(testdir, "000000000.data"), filepath.Join(testdir, "000000000xxx.data"))747 assert.NoError(err)748 _, err = Open(testdir)749 assert.Error(err)750 assert.Equal("strconv.ParseInt: parsing \"000000000xxx\": invalid syntax", err.Error())751 })752}753func TestCloseErrors(t *testing.T) {754 assert := assert.New(t)755 testdir, err := ioutil.TempDir("", "bitcask")756 assert.NoError(err)757 defer os.RemoveAll(testdir)758 t.Run("CloseIndexError", func(t *testing.T) {759 db, err := Open(testdir, WithMaxDatafileSize(32))760 assert.NoError(err)761 mockIndexer := new(mocks.Indexer)762 mockIndexer.On("Save", db.trie, filepath.Join(db.path, "index")).Return(ErrMockError)763 db.indexer = mockIndexer764 err = db.Close()765 assert.Error(err)766 assert.Equal(ErrMockError, err)767 })768 t.Run("CloseDatafilesError", func(t *testing.T) {769 db, err := Open(testdir, WithMaxDatafileSize(32))770 assert.NoError(err)771 mockDatafile := new(mocks.Datafile)772 mockDatafile.On("Close").Return(ErrMockError)773 db.datafiles[0] = mockDatafile774 err = db.Close()775 assert.Error(err)776 assert.Equal(ErrMockError, err)777 })778 t.Run("CloseActiveDatafileError", func(t *testing.T) {779 db, err := Open(testdir, WithMaxDatafileSize(32))780 assert.NoError(err)781 mockDatafile := new(mocks.Datafile)782 mockDatafile.On("Close").Return(ErrMockError)783 db.curr = mockDatafile784 err = db.Close()785 assert.Error(err)786 assert.Equal(ErrMockError, err)787 })788}789func TestDeleteErrors(t *testing.T) {790 assert := assert.New(t)791 t.Run("WriteError", func(t *testing.T) {792 testdir, err := ioutil.TempDir("", "bitcask")793 assert.NoError(err)794 defer os.RemoveAll(testdir)795 db, err := Open(testdir, WithMaxDatafileSize(32))796 assert.NoError(err)797 err = db.Put([]byte("foo"), []byte("bar"))798 assert.NoError(err)799 mockDatafile := new(mocks.Datafile)800 mockDatafile.On("Size").Return(int64(0))801 mockDatafile.On(802 "Write",803 internal.Entry{804 Checksum: 0x0,805 Key: []byte("foo"),806 Offset: 0,807 Value: []byte{},808 },809 ).Return(int64(0), int64(0), ErrMockError)810 db.curr = mockDatafile811 err = db.Delete([]byte("foo"))812 assert.Error(err)813 })814}815func TestMergeErrors(t *testing.T) {816 assert := assert.New(t)817 t.Run("RemoveDatabaseDirectory", func(t *testing.T) {818 skipIfWindows(t)819 testdir, err := ioutil.TempDir("", "bitcask")820 assert.NoError(err)821 defer os.RemoveAll(testdir)822 db, err := Open(testdir, WithMaxDatafileSize(32))823 assert.NoError(err)824 assert.NoError(os.RemoveAll(testdir))825 err = db.Merge()826 assert.Error(err)827 })828 t.Run("EmptyCloseError", func(t *testing.T) {829 testdir, err := ioutil.TempDir("", "bitcask")830 assert.NoError(err)831 defer os.RemoveAll(testdir)832 db, err := Open(testdir)833 assert.NoError(err)834 mockDatafile := new(mocks.Datafile)835 mockDatafile.On("Close").Return(ErrMockError)836 db.curr = mockDatafile837 err = db.Merge()838 assert.Error(err)839 assert.Equal(ErrMockError, err)840 })841 t.Run("ReadError", func(t *testing.T) {842 testdir, err := ioutil.TempDir("", "bitcask")843 assert.NoError(err)844 defer os.RemoveAll(testdir)845 db, err := Open(testdir)846 assert.NoError(err)847 assert.NoError(db.Put([]byte("foo"), []byte("bar")))848 mockDatafile := new(mocks.Datafile)849 mockDatafile.On("FileID").Return(0)850 mockDatafile.On("ReadAt", int64(0), int64(22)).Return(851 internal.Entry{},852 ErrMockError,853 )854 db.curr = mockDatafile855 err = db.Merge()856 assert.Error(err)857 assert.Equal(ErrMockError, err)858 })859}860func TestConcurrent(t *testing.T) {861 var (862 db *Bitcask863 err error864 )865 assert := assert.New(t)866 testdir, err := ioutil.TempDir("", "bitcask")867 assert.NoError(err)868 t.Run("Setup", func(t *testing.T) {869 t.Run("Open", func(t *testing.T) {870 db, err = Open(testdir)871 assert.NoError(err)872 })873 t.Run("Put", func(t *testing.T) {874 err = db.Put([]byte("foo"), []byte("bar"))875 assert.NoError(err)876 })877 })878 t.Run("Concurrent", func(t *testing.T) {879 t.Run("Put", func(t *testing.T) {880 f := func(wg *sync.WaitGroup, x int) {881 defer func() {882 wg.Done()883 }()884 for i := 0; i <= 100; i++ {885 if i%x == 0 {886 key := []byte(fmt.Sprintf("k%d", i))887 value := []byte(fmt.Sprintf("v%d", i))888 err := db.Put(key, value)889 assert.NoError(err)890 }891 }892 }893 wg := &sync.WaitGroup{}894 go f(wg, 2)895 go f(wg, 3)896 go f(wg, 5)897 wg.Add(3)898 wg.Wait()899 })900 t.Run("Get", func(t *testing.T) {901 f := func(wg *sync.WaitGroup, N int) {902 defer func() {903 wg.Done()904 }()905 for i := 0; i <= N; i++ {906 value, err := db.Get([]byte("foo"))907 assert.NoError(err)908 assert.Equal([]byte("bar"), value)909 }910 }911 wg := &sync.WaitGroup{}912 go f(wg, 100)913 go f(wg, 100)914 go f(wg, 100)915 wg.Add(3)916 wg.Wait()917 })918 t.Run("Close", func(t *testing.T) {919 err = db.Close()920 assert.NoError(err)921 })922 })923}924func TestScan(t *testing.T) {925 assert := assert.New(t)926 testdir, err := ioutil.TempDir("", "bitcask")927 assert.NoError(err)928 var db *Bitcask929 t.Run("Setup", func(t *testing.T) {930 t.Run("Open", func(t *testing.T) {931 db, err = Open(testdir)932 assert.NoError(err)933 })934 t.Run("Put", func(t *testing.T) {935 var items = map[string][]byte{936 "1": []byte("1"),937 "2": []byte("2"),938 "3": []byte("3"),939 "food": []byte("pizza"),940 "foo": []byte([]byte("foo")),941 "fooz": []byte("fooz ball"),942 "hello": []byte("world"),943 }944 for k, v := range items {945 err = db.Put([]byte(k), v)946 assert.NoError(err)947 }948 })949 })950 t.Run("Scan", func(t *testing.T) {951 var (952 vals [][]byte953 expected = [][]byte{954 []byte("foo"),955 []byte("fooz ball"),956 []byte("pizza"),957 }958 )959 err = db.Scan([]byte("fo"), func(key []byte) error {960 val, err := db.Get(key)961 assert.NoError(err)962 vals = append(vals, val)963 return nil964 })965 vals = SortByteArrays(vals)966 assert.Equal(expected, vals)967 })968 t.Run("ScanErrors", func(t *testing.T) {969 err = db.Scan([]byte("fo"), func(key []byte) error {970 return ErrMockError971 })972 assert.Error(err)973 assert.Equal(ErrMockError, err)974 })975}976func TestLocking(t *testing.T) {977 assert := assert.New(t)978 testdir, err := ioutil.TempDir("", "bitcask")979 assert.NoError(err)980 db, err := Open(testdir)981 assert.NoError(err)982 defer db.Close()983 _, err = Open(testdir)984 assert.Error(err)985 assert.Equal(ErrDatabaseLocked, err)986}987type benchmarkTestCase struct {988 name string989 size int990}991func BenchmarkGet(b *testing.B) {992 currentDir, err := os.Getwd()993 if err != nil {994 b.Fatal(err)995 }996 testdir, err := ioutil.TempDir(currentDir, "bitcask_bench")997 if err != nil {998 b.Fatal(err)999 }1000 defer os.RemoveAll(testdir)1001 tests := []benchmarkTestCase{1002 {"128B", 128},1003 {"256B", 256},1004 {"512B", 512},1005 {"1K", 1024},1006 {"2K", 2048},1007 {"4K", 4096},1008 {"8K", 8192},1009 {"16K", 16384},1010 {"32K", 32768},1011 }1012 for _, tt := range tests {1013 b.Run(tt.name, func(b *testing.B) {1014 b.SetBytes(int64(tt.size))1015 key := []byte("foo")1016 value := []byte(strings.Repeat(" ", tt.size))1017 options := []Option{1018 WithMaxKeySize(uint32(len(key))),1019 WithMaxValueSize(uint64(tt.size)),1020 }1021 db, err := Open(testdir, options...)1022 if err != nil {1023 b.Fatal(err)1024 }1025 err = db.Put(key, value)1026 if err != nil {1027 b.Fatal(err)1028 }1029 b.ResetTimer()1030 for i := 0; i < b.N; i++ {1031 val, err := db.Get(key)1032 if err != nil {1033 b.Fatal(err)1034 }1035 if !bytes.Equal(val, value) {1036 b.Errorf("unexpected value")1037 }1038 }1039 b.StopTimer()1040 db.Close()1041 })1042 }1043}1044func BenchmarkPut(b *testing.B) {1045 currentDir, err := os.Getwd()1046 if err != nil {1047 b.Fatal(err)1048 }1049 tests := []benchmarkTestCase{1050 {"128B", 128},1051 {"256B", 256},1052 {"1K", 1024},1053 {"2K", 2048},1054 {"4K", 4096},1055 {"8K", 8192},1056 {"16K", 16384},1057 {"32K", 32768},1058 }1059 variants := map[string][]Option{1060 "NoSync": {1061 WithSync(false),1062 },1063 "Sync": {1064 WithSync(true),1065 },1066 }1067 for name, options := range variants {1068 testdir, err := ioutil.TempDir(currentDir, "bitcask_bench")1069 if err != nil {1070 b.Fatal(err)1071 }1072 defer os.RemoveAll(testdir)1073 db, err := Open(testdir, options...)1074 if err != nil {1075 b.Fatal(err)1076 }1077 defer db.Close()1078 for _, tt := range tests {1079 b.Run(tt.name+name, func(b *testing.B) {1080 b.SetBytes(int64(tt.size))1081 key := []byte("foo")1082 value := []byte(strings.Repeat(" ", tt.size))1083 b.ResetTimer()1084 for i := 0; i < b.N; i++ {1085 err := db.Put(key, value)1086 if err != nil {1087 b.Fatal(err)1088 }1089 }1090 })1091 }1092 }1093}1094func BenchmarkScan(b *testing.B) {1095 currentDir, err := os.Getwd()1096 if err != nil {1097 b.Fatal(err)1098 }1099 testdir, err := ioutil.TempDir(currentDir, "bitcask_bench")1100 if err != nil {1101 b.Fatal(err)1102 }1103 defer os.RemoveAll(testdir)1104 db, err := Open(testdir)1105 if err != nil {1106 b.Fatal(err)1107 }1108 defer db.Close()1109 var items = map[string][]byte{1110 "1": []byte("1"),1111 "2": []byte("2"),1112 "3": []byte("3"),1113 "food": []byte("pizza"),1114 "foo": []byte([]byte("foo")),1115 "fooz": []byte("fooz ball"),1116 "hello": []byte("world"),1117 }1118 for k, v := range items {1119 err := db.Put([]byte(k), v)1120 if err != nil {1121 b.Fatal(err)1122 }1123 }1124 var expected = [][]byte{[]byte("foo"), []byte("food"), []byte("fooz")}1125 b.ResetTimer()1126 for i := 0; i < b.N; i++ {1127 var keys [][]byte1128 err = db.Scan([]byte("fo"), func(key []byte) error {1129 keys = append(keys, key)1130 return nil1131 })1132 if err != nil {1133 b.Fatal(err)...

Full Screen

Full Screen

fragment.go

Source:fragment.go Github

copy

Full Screen

...10 Unknowns []Atom11 AtomPos12}13func (a MovieFrag) Marshal(b []byte) (n int) {14 pio.PutU32BE(b[4:], uint32(MOOF))15 n += a.marshal(b[8:]) + 816 pio.PutU32BE(b[0:], uint32(n))17 return18}19func (a MovieFrag) marshal(b []byte) (n int) {20 if a.Header != nil {21 n += a.Header.Marshal(b[n:])22 }23 for _, atom := range a.Tracks {24 n += atom.Marshal(b[n:])25 }26 for _, atom := range a.Unknowns {27 n += atom.Marshal(b[n:])28 }29 return30}31func (a MovieFrag) Len() (n int) {32 n += 833 if a.Header != nil {34 n += a.Header.Len()35 }36 for _, atom := range a.Tracks {37 n += atom.Len()38 }39 for _, atom := range a.Unknowns {40 n += atom.Len()41 }42 return43}44func (a *MovieFrag) Unmarshal(b []byte, offset int) (n int, err error) {45 (&a.AtomPos).setPos(offset, len(b))46 n += 847 for n+8 < len(b) {48 tag := Tag(pio.U32BE(b[n+4:]))49 size := int(pio.U32BE(b[n:]))50 if len(b) < n+size {51 err = parseErr("TagSizeInvalid", n+offset, err)52 return53 }54 switch tag {55 case MFHD:56 {57 atom := &MovieFragHeader{}58 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {59 err = parseErr("mfhd", n+offset, err)60 return61 }62 a.Header = atom63 }64 case TRAF:65 {66 atom := &TrackFrag{}67 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {68 err = parseErr("traf", n+offset, err)69 return70 }71 a.Tracks = append(a.Tracks, atom)72 }73 default:74 {75 atom := &Dummy{Tag_: tag, Data: b[n : n+size]}76 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {77 err = parseErr("", n+offset, err)78 return79 }80 a.Unknowns = append(a.Unknowns, atom)81 }82 }83 n += size84 }85 return86}87func (a MovieFrag) Children() (r []Atom) {88 if a.Header != nil {89 r = append(r, a.Header)90 }91 for _, atom := range a.Tracks {92 r = append(r, atom)93 }94 r = append(r, a.Unknowns...)95 return96}97func (a MovieFrag) Tag() Tag {98 return MOOF99}100const MFHD = Tag(0x6d666864)101type MovieFragHeader struct {102 Version uint8103 Flags uint32104 Seqnum uint32105 AtomPos106}107func (a MovieFragHeader) Marshal(b []byte) (n int) {108 pio.PutU32BE(b[4:], uint32(MFHD))109 n += a.marshal(b[8:]) + 8110 pio.PutU32BE(b[0:], uint32(n))111 return112}113func (a MovieFragHeader) marshal(b []byte) (n int) {114 pio.PutU8(b[n:], a.Version)115 n += 1116 pio.PutU24BE(b[n:], a.Flags)117 n += 3118 pio.PutU32BE(b[n:], a.Seqnum)119 n += 4120 return121}122func (a MovieFragHeader) Len() (n int) {123 n += 8124 n += 1125 n += 3126 n += 4127 return128}129func (a *MovieFragHeader) Unmarshal(b []byte, offset int) (n int, err error) {130 (&a.AtomPos).setPos(offset, len(b))131 n += 8132 if len(b) < n+1 {133 err = parseErr("Version", n+offset, err)134 return135 }136 a.Version = pio.U8(b[n:])137 n += 1138 if len(b) < n+3 {139 err = parseErr("Flags", n+offset, err)140 return141 }142 a.Flags = pio.U24BE(b[n:])143 n += 3144 if len(b) < n+4 {145 err = parseErr("Seqnum", n+offset, err)146 return147 }148 a.Seqnum = pio.U32BE(b[n:])149 n += 4150 return151}152func (a MovieFragHeader) Children() (r []Atom) {153 return154}155func (a MovieFragHeader) Tag() Tag {156 return MFHD157}158// TRUN is the atom type for TrackFragRun159const TRUN = Tag(0x7472756e)160// TrackFragRun atom161type TrackFragRun struct {162 Version uint8163 Flags TrackRunFlags164 DataOffset uint32165 FirstSampleFlags SampleFlags166 Entries []TrackFragRunEntry167 AtomPos168}169// TrackRunFlags is the type of TrackFragRun's Flags170type TrackRunFlags uint32171// Defined flags for TrackFragRun172const (173 TrackRunDataOffset TrackRunFlags = 0x01174 TrackRunFirstSampleFlags TrackRunFlags = 0x04175 TrackRunSampleDuration TrackRunFlags = 0x100176 TrackRunSampleSize TrackRunFlags = 0x200177 TrackRunSampleFlags TrackRunFlags = 0x400178 TrackRunSampleCTS TrackRunFlags = 0x800179)180func (a TrackFragRun) Marshal(b []byte) (n int) {181 pio.PutU32BE(b[4:], uint32(TRUN))182 n += a.marshal(b[8:]) + 8183 pio.PutU32BE(b[0:], uint32(n))184 return185}186func (a TrackFragRun) marshal(b []byte) (n int) {187 pio.PutU8(b[n:], a.Version)188 n += 1189 pio.PutU24BE(b[n:], uint32(a.Flags))190 n += 3191 pio.PutU32BE(b[n:], uint32(len(a.Entries)))192 n += 4193 if a.Flags&TrackRunDataOffset != 0 {194 {195 pio.PutU32BE(b[n:], a.DataOffset)196 n += 4197 }198 }199 if a.Flags&TrackRunFirstSampleFlags != 0 {200 {201 pio.PutU32BE(b[n:], uint32(a.FirstSampleFlags))202 n += 4203 }204 }205 for _, entry := range a.Entries {206 if a.Flags&TrackRunSampleDuration != 0 {207 pio.PutU32BE(b[n:], entry.Duration)208 n += 4209 }210 if a.Flags&TrackRunSampleSize != 0 {211 pio.PutU32BE(b[n:], entry.Size)212 n += 4213 }214 if a.Flags&TrackRunSampleFlags != 0 {215 pio.PutU32BE(b[n:], uint32(entry.Flags))216 n += 4217 }218 if a.Flags&TrackRunSampleCTS != 0 {219 if a.Version > 0 {220 pio.PutI32BE(b[:n], int32(entry.CTS))221 } else {222 pio.PutU32BE(b[n:], uint32(entry.CTS))223 }224 n += 4225 }226 }227 return228}229func (a TrackFragRun) Len() (n int) {230 n += 8231 n += 1232 n += 3233 n += 4234 if a.Flags&TrackRunDataOffset != 0 {235 {236 n += 4237 }238 }239 if a.Flags&TrackRunFirstSampleFlags != 0 {240 {241 n += 4242 }243 }244 for range a.Entries {245 if a.Flags&TrackRunSampleDuration != 0 {246 n += 4247 }248 if a.Flags&TrackRunSampleSize != 0 {249 n += 4250 }251 if a.Flags&TrackRunSampleFlags != 0 {252 n += 4253 }254 if a.Flags&TrackRunSampleCTS != 0 {255 n += 4256 }257 }258 return259}260func (a *TrackFragRun) Unmarshal(b []byte, offset int) (n int, err error) {261 (&a.AtomPos).setPos(offset, len(b))262 n += 8263 if len(b) < n+1 {264 err = parseErr("Version", n+offset, err)265 return266 }267 a.Version = pio.U8(b[n:])268 n += 1269 if len(b) < n+3 {270 err = parseErr("Flags", n+offset, err)271 return272 }273 a.Flags = TrackRunFlags(pio.U24BE(b[n:]))274 n += 3275 var _len_Entries uint32276 _len_Entries = pio.U32BE(b[n:])277 n += 4278 a.Entries = make([]TrackFragRunEntry, _len_Entries)279 if a.Flags&TrackRunDataOffset != 0 {280 {281 if len(b) < n+4 {282 err = parseErr("DataOffset", n+offset, err)283 return284 }285 a.DataOffset = pio.U32BE(b[n:])286 n += 4287 }288 }289 if a.Flags&TrackRunFirstSampleFlags != 0 {290 {291 if len(b) < n+4 {292 err = parseErr("FirstSampleFlags", n+offset, err)293 return294 }295 a.FirstSampleFlags = SampleFlags(pio.U32BE(b[n:]))296 n += 4297 }298 }299 for i := 0; i < int(_len_Entries); i++ {300 entry := &a.Entries[i]301 if a.Flags&TrackRunSampleDuration != 0 {302 entry.Duration = pio.U32BE(b[n:])303 n += 4304 }305 if a.Flags&TrackRunSampleSize != 0 {306 entry.Size = pio.U32BE(b[n:])307 n += 4308 }309 if a.Flags&TrackRunSampleFlags != 0 {310 entry.Flags = SampleFlags(pio.U32BE(b[n:]))311 n += 4312 }313 if a.Flags&TrackRunSampleCTS != 0 {314 if a.Version > 0 {315 entry.CTS = int32(pio.I32BE(b[n:]))316 } else {317 entry.CTS = int32(pio.U32BE(b[n:]))318 }319 n += 4320 }321 }322 return323}324func (a TrackFragRun) Children() (r []Atom) {325 return326}327type TrackFragRunEntry struct {328 Duration uint32329 Size uint32330 Flags SampleFlags331 CTS int32332}333func (a TrackFragRun) Tag() Tag {334 return TRUN335}336const TFDT = Tag(0x74666474)337type TrackFragDecodeTime struct {338 Version uint8339 Flags uint32340 Time uint64341 AtomPos342}343func (a TrackFragDecodeTime) Marshal(b []byte) (n int) {344 pio.PutU32BE(b[4:], uint32(TFDT))345 n += a.marshal(b[8:]) + 8346 pio.PutU32BE(b[0:], uint32(n))347 return348}349func (a TrackFragDecodeTime) marshal(b []byte) (n int) {350 pio.PutU8(b[n:], a.Version)351 n += 1352 pio.PutU24BE(b[n:], a.Flags)353 n += 3354 if a.Version != 0 {355 pio.PutU64BE(b[n:], a.Time)356 n += 8357 } else {358 pio.PutU32BE(b[n:], uint32(a.Time))359 n += 4360 }361 return362}363func (a TrackFragDecodeTime) Len() (n int) {364 n += 8365 n += 1366 n += 3367 if a.Version != 0 {368 n += 8369 } else {370 n += 4371 }372 return373}374func (a *TrackFragDecodeTime) Unmarshal(b []byte, offset int) (n int, err error) {375 (&a.AtomPos).setPos(offset, len(b))376 n += 8377 if len(b) < n+1 {378 err = parseErr("Version", n+offset, err)379 return380 }381 a.Version = pio.U8(b[n:])382 n += 1383 if len(b) < n+3 {384 err = parseErr("Flags", n+offset, err)385 return386 }387 a.Flags = pio.U24BE(b[n:])388 n += 3389 if a.Version != 0 {390 a.Time = pio.U64BE(b[n:])391 n += 8392 } else {393 a.Time = uint64(pio.U32BE(b[n:]))394 n += 4395 }396 return397}398func (a TrackFragDecodeTime) Children() (r []Atom) {399 return400}401func (a TrackFragDecodeTime) Tag() Tag {402 return TFDT403}404const TRAF = Tag(0x74726166)405type TrackFrag struct {406 Header *TrackFragHeader407 DecodeTime *TrackFragDecodeTime408 Run *TrackFragRun409 Unknowns []Atom410 AtomPos411}412func (a TrackFrag) Marshal(b []byte) (n int) {413 pio.PutU32BE(b[4:], uint32(TRAF))414 n += a.marshal(b[8:]) + 8415 pio.PutU32BE(b[0:], uint32(n))416 return417}418func (a TrackFrag) marshal(b []byte) (n int) {419 if a.Header != nil {420 n += a.Header.Marshal(b[n:])421 }422 if a.DecodeTime != nil {423 n += a.DecodeTime.Marshal(b[n:])424 }425 if a.Run != nil {426 n += a.Run.Marshal(b[n:])427 }428 for _, atom := range a.Unknowns {429 n += atom.Marshal(b[n:])430 }431 return432}433func (a TrackFrag) Len() (n int) {434 n += 8435 if a.Header != nil {436 n += a.Header.Len()437 }438 if a.DecodeTime != nil {439 n += a.DecodeTime.Len()440 }441 if a.Run != nil {442 n += a.Run.Len()443 }444 for _, atom := range a.Unknowns {445 n += atom.Len()446 }447 return448}449func (a *TrackFrag) Unmarshal(b []byte, offset int) (n int, err error) {450 (&a.AtomPos).setPos(offset, len(b))451 n += 8452 for n+8 < len(b) {453 tag := Tag(pio.U32BE(b[n+4:]))454 size := int(pio.U32BE(b[n:]))455 if len(b) < n+size {456 err = parseErr("TagSizeInvalid", n+offset, err)457 return458 }459 switch tag {460 case TFHD:461 {462 atom := &TrackFragHeader{}463 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {464 err = parseErr("tfhd", n+offset, err)465 return466 }467 a.Header = atom468 }469 case TFDT:470 {471 atom := &TrackFragDecodeTime{}472 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {473 err = parseErr("tfdt", n+offset, err)474 return475 }476 a.DecodeTime = atom477 }478 case TRUN:479 {480 atom := &TrackFragRun{}481 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {482 err = parseErr("trun", n+offset, err)483 return484 }485 a.Run = atom486 }487 default:488 {489 atom := &Dummy{Tag_: tag, Data: b[n : n+size]}490 if _, err = atom.Unmarshal(b[n:n+size], offset+n); err != nil {491 err = parseErr("", n+offset, err)492 return493 }494 a.Unknowns = append(a.Unknowns, atom)495 }496 }497 n += size498 }499 return500}501func (a TrackFrag) Children() (r []Atom) {502 if a.Header != nil {503 r = append(r, a.Header)504 }505 if a.DecodeTime != nil {506 r = append(r, a.DecodeTime)507 }508 if a.Run != nil {509 r = append(r, a.Run)510 }511 r = append(r, a.Unknowns...)512 return513}514func (a TrackFrag) Tag() Tag {515 return TRAF516}517func (a TrackFragRun) String() string {518 return fmt.Sprintf("dataoffset=%d", a.DataOffset)519}520// TFHD is the atom type for TrackFragHeader521const TFHD = Tag(0x74666864)522// TrackFragHeader atom523type TrackFragHeader struct {524 Version uint8525 Flags TrackFragFlags526 TrackID uint32527 BaseDataOffset uint64528 StsdID uint32529 DefaultDuration uint32530 DefaultSize uint32531 DefaultFlags SampleFlags532 AtomPos533}534// TrackFragFlags is the type of TrackFragHeader's Flags535type TrackFragFlags uint32536// Defined flags for TrackFragHeader537const (538 TrackFragBaseDataOffset TrackFragFlags = 0x01539 TrackFragStsdID TrackFragFlags = 0x02540 TrackFragDefaultDuration TrackFragFlags = 0x08541 TrackFragDefaultSize TrackFragFlags = 0x10542 TrackFragDefaultFlags TrackFragFlags = 0x20543 TrackFragDurationIsEmpty TrackFragFlags = 0x010000544 TrackFragDefaultBaseIsMOOF TrackFragFlags = 0x020000545)546func (a TrackFragHeader) Marshal(b []byte) (n int) {547 pio.PutU32BE(b[4:], uint32(TFHD))548 n += a.marshal(b[8:]) + 8549 pio.PutU32BE(b[0:], uint32(n))550 return551}552func (a TrackFragHeader) marshal(b []byte) (n int) {553 pio.PutU8(b[n:], a.Version)554 n += 1555 pio.PutU24BE(b[n:], uint32(a.Flags))556 n += 3557 pio.PutU32BE(b[n:], a.TrackID)558 n += 4559 if a.Flags&TrackFragBaseDataOffset != 0 {560 {561 pio.PutU64BE(b[n:], a.BaseDataOffset)562 n += 8563 }564 }565 if a.Flags&TrackFragStsdID != 0 {566 {567 pio.PutU32BE(b[n:], a.StsdID)568 n += 4569 }570 }571 if a.Flags&TrackFragDefaultDuration != 0 {572 {573 pio.PutU32BE(b[n:], a.DefaultDuration)574 n += 4575 }576 }577 if a.Flags&TrackFragDefaultSize != 0 {578 {579 pio.PutU32BE(b[n:], a.DefaultSize)580 n += 4581 }582 }583 if a.Flags&TrackFragDefaultFlags != 0 {584 {585 pio.PutU32BE(b[n:], uint32(a.DefaultFlags))586 n += 4587 }588 }589 return590}591func (a TrackFragHeader) Len() (n int) {592 n += 8593 n += 1594 n += 3595 n += 4596 if a.Flags&TrackFragBaseDataOffset != 0 {597 {598 n += 8599 }...

Full Screen

Full Screen

rleplus_writer.go

Source:rleplus_writer.go Github

copy

Full Screen

...3 "encoding/binary"4)5func EncodeRuns(rit RunIterator, buf []byte) ([]byte, error) {6 bv := writeBitvec(buf)7 bv.Put(0, 2)8 first := true9 varBuf := make([]byte, binary.MaxVarintLen64)10 for rit.HasNext() {11 run, err := rit.NextRun()12 if err != nil {13 return nil, err14 }15 if first {16 if run.Val {17 bv.Put(1, 1)18 } else {19 bv.Put(0, 1)20 }21 first = false22 }23 switch {24 case run.Len == 1:25 bv.Put(1, 1)26 case run.Len < 16:27 bv.Put(2, 2)28 bv.Put(byte(run.Len), 4)29 case run.Len >= 16:30 bv.Put(0, 2)31 numBytes := binary.PutUvarint(varBuf, run.Len)32 for i := 0; i < numBytes; i++ {33 bv.Put(varBuf[i], 8)34 }35 }36 }37 if first {38 bv.Put(0, 1)39 }40 return bv.Out(), nil41}

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1run.Put(1, "abc");2run.Put(2, "def");3run.Put(3, "ghi");4run.Put(4, "jkl");5run.Put(5, "mno");6run.Put(6, "pqr");7run.Put(7, "stu");8run.Put(8, "vwx");9run.Put(9, "yz");10run.Put(10, "123");11System.out.println(run.Get(1));12System.out.println(run.Get(2));13System.out.println(run.Get(3));14System.out.println(run.Get(4));15System.out.println(run.Get(5));16System.out.println(run.Get(6));17System.out.println(run.Get(7));18System.out.println(run.Get(8));19System.out.println(run.Get(9));20System.out.println(run.Get(10));21run.Delete(1);22run.Delete(2);23run.Delete(3);24run.Delete(4);25run.Delete(5);26run.Delete(6);27run.Delete(7);28run.Delete(8);29run.Delete(9);30run.Delete(10);31System.out.println(run.Get(1));32System.out.println(run.Get(2));33System.out.println(run.Get(3));34System.out.println(run.Get(4));35System.out.println(run.Get(5));36System.out.println(run.Get(6));37System.out.println(run.Get(7));38System.out.println(run.Get(8));39System.out.println(run.Get(9));40System.out.println(run.Get(10));41}42}

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1run.Put("Key1","Value1")2run.Put("Key2","Value2")3run.Put("Key3","Value3")4run.Get("Key1")5run.Get("Key2")6run.Get("Key3")7run.Delete("Key1")8run.Delete("Key2")9run.Delete("Key3")10run.Update("Key1","Value1")11run.Update("Key2","Value2")12run.Update("Key3","Value3")13run.Get("Key1")14run.Get("Key2")15run.Get("Key3")16run.List()

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4}5import (6func main() {7 fmt.Println("Hello, playground")8}9import (10func main() {11 fmt.Println("Hello, playground")12}13import (14func main() {15 fmt.Println("Hello, playground")16}17import (18func main() {19 fmt.Println("Hello, playground")20}21import (22func main() {23 fmt.Println("Hello, playground")24}25import (26func main() {27 fmt.Println("Hello, playground")28}29import (30func main() {31 fmt.Println("Hello, playground")32}33import (34func main() {35 fmt.Println("Hello, playground")36}37import (38func main() {39 fmt.Println("Hello, playground")40}41import (42func main() {43 fmt.Println("Hello, playground")44}45import (46func main() {47 fmt.Println("Hello, playground")48}49import (50func main() {51 fmt.Println("Hello, playground")52}53import

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import "fmt"2type run struct {3}4func (r *run) Put(v int) {5}6func main() {7 r := run{}8 r.Put(5)9 fmt.Println(r.value)10}11import "fmt"12type run struct {13}14func (r *run) Put(v int) {15}16func main() {17 r.Put(5)18 fmt.Println(r.value)19}20import "fmt"21type run struct {22}23func (r *run) Put(v int) {24}25func main() {26 r.Put(5)27 fmt.Println(r.value)28}29import "fmt"30type run struct {31}32func (r *run) Put(v int) {33}34func main() {35 r := &run{}36 r.Put(5)37 fmt.Println(r.value)38}39import "fmt"40type run struct {41}42func (r *run) Put(v int) {43}44func main() {45 r.Put(5)46 fmt.Println(r.value)47}

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("ls")4 err := cmd.Start()5 if err != nil {6 log.Fatal(err)7 }8 time.Sleep(1 * time.Second)9 err = cmd.Process.Kill()10 if err != nil {11 log.Fatal("failed to kill: ", err)12 }13}

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 runner := run.New()4 runner.Put("hello")5 fmt.Println(runner.Get())6}7import (8func main() {9 runner := run.New()10 fmt.Println(runner.Get())11}12In the above code, we have defined two packages, one is 1.go and another is 2.go. In 1.go, we have used Put() method of run class and in 2.go, we have used Get() method of run class. Now, if you run the above code, it will give you the following error:

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, playground")4 runtime.GOMAXPROCS(3)5 run := &run{}6 go run.Put(10)7 go run.Put(20)8 go run.Put(30)9 go run.Put(40)10 go run.Put(50)11 go run.Put(60)12 go run.Put(70)13 go run.Put(80)14 go run.Put(90)15 go run.Put(100)16 go run.Put(110)17 go run.Put(120)18 go run.Put(130)19 go run.Put(140)20 go run.Put(150)21 go run.Put(160)22 go run.Put(170)23 go run.Put(180)24 go run.Put(190)25 go run.Put(200)26 go run.Put(210)27 go run.Put(220)28 go run.Put(230)29 go run.Put(240)30 go run.Put(250)31 go run.Put(260)32 go run.Put(270)33 go run.Put(280)34 go run.Put(290)35 go run.Put(300)36 time.Sleep(time.Second * 10)37}38import (39func main() {40 fmt.Println("Hello, playground")

Full Screen

Full Screen

Put

Using AI Code Generation

copy

Full Screen

1type Run struct {2}3func (r *Run) Put() {4}5func (r *Run) Get() {6}7import (8func main() {9 r := new(run.Run)10 r.Put()11}12import (13func main() {14 r := new(run.Run)15 r.Get()16}

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.

Run Keploy automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful