@@ -750,4 +750,51 @@ func TestStreamWriterIncremental(t *testing.T) {
750750 require .NoError (t , err )
751751 })
752752 })
753+
754+ t .Run ("multiple incremental with older data first" , func (t * testing.T ) {
755+ runBadgerTest (t , nil , func (t * testing.T , db * DB ) {
756+ buf := z .NewBuffer (10 << 20 , "test" )
757+ defer func () { require .NoError (t , buf .Release ()) }()
758+ KVToBuffer (& pb.KV {
759+ Key : []byte ("a1" ),
760+ Value : []byte ("val1" ),
761+ Version : 11 ,
762+ }, buf )
763+ sw := db .NewStreamWriter ()
764+ require .NoError (t , sw .PrepareIncremental (), "sw.PrepareIncremental() failed" )
765+ require .NoError (t , sw .Write (buf ), "sw.Write() failed" )
766+ require .NoError (t , sw .Flush (), "sw.Flush() failed" )
767+
768+ buf = z .NewBuffer (10 << 20 , "test" )
769+ defer func () { require .NoError (t , buf .Release ()) }()
770+ KVToBuffer (& pb.KV {
771+ Key : []byte ("a2" ),
772+ Value : []byte ("val2" ),
773+ Version : 9 ,
774+ }, buf )
775+ sw = db .NewStreamWriter ()
776+ require .NoError (t , sw .PrepareIncremental (), "sw.PrepareIncremental() failed" )
777+ require .NoError (t , sw .Write (buf ), "sw.Write() failed" )
778+ require .NoError (t , sw .Flush (), "sw.Flush() failed" )
779+
780+ // This will move the maxTs to 10 (earlier, without the fix)
781+ require .NoError (t , db .Update (func (txn * Txn ) error {
782+ return txn .Set ([]byte ("a1" ), []byte ("val3" ))
783+ }))
784+ // This will move the maxTs to 11 (earliler, without the fix)
785+ require .NoError (t , db .Update (func (txn * Txn ) error {
786+ return txn .Set ([]byte ("a3" ), []byte ("val4" ))
787+ }))
788+
789+ // And now, the first write with val1 will resurface (without the fix)
790+ require .NoError (t , db .View (func (txn * Txn ) error {
791+ item , err := txn .Get ([]byte ("a1" ))
792+ require .NoError (t , err )
793+ val , err := item .ValueCopy (nil )
794+ require .NoError (t , err )
795+ require .Equal (t , "val3" , string (val ))
796+ return nil
797+ }))
798+ })
799+ })
753800}
0 commit comments