19
19
#include " utilities/merge_operators/string_append/stringappend2.h"
20
20
21
21
namespace ROCKSDB_NAMESPACE {
22
+ namespace {
23
+ std::string EncodeAsUint64 (uint64_t v) {
24
+ std::string dst;
25
+ PutFixed64 (&dst, v);
26
+ return dst;
27
+ }
28
+ } // namespace
22
29
class DBBasicTestWithTimestamp : public DBBasicTestWithTimestampBase {
23
30
public:
24
31
DBBasicTestWithTimestamp ()
@@ -3746,17 +3753,42 @@ INSTANTIATE_TEST_CASE_P(
3746
3753
test::UserDefinedTimestampTestMode::kStripUserDefinedTimestamp ,
3747
3754
test::UserDefinedTimestampTestMode::kNormal ));
3748
3755
3749
- TEST_F (DBBasicTestWithTimestamp, EnableDisableUDT) {
3756
+ // Test params:
3757
+ // 1) whether to flush before close
3758
+ class EnableDisableUDTTest : public DBBasicTestWithTimestampBase ,
3759
+ public testing::WithParamInterface<bool > {
3760
+ public:
3761
+ EnableDisableUDTTest ()
3762
+ : DBBasicTestWithTimestampBase(" /enable_disable_udt" ) {}
3763
+ };
3764
+
3765
+ INSTANTIATE_TEST_CASE_P (EnableDisableUDTTest, EnableDisableUDTTest,
3766
+ ::testing::Values (true , false ));
3767
+
3768
+ TEST_P (EnableDisableUDTTest, Basic) {
3750
3769
Options options = CurrentOptions ();
3770
+ // Un-flushed data before close will involve a WAL replay on DB reopen.
3771
+ bool flush_before_close = GetParam ();
3751
3772
options.env = env_;
3752
- // Create a column family without user-defined timestamps.
3753
3773
options.comparator = BytewiseComparator ();
3754
3774
options.persist_user_defined_timestamps = true ;
3755
3775
DestroyAndReopen (options);
3756
3776
3777
+ ReadOptions ropts;
3778
+ std::string read_ts;
3779
+ std::string value;
3780
+ std::string key_ts;
3781
+
3757
3782
// Create one SST file, its user keys have no user-defined timestamps.
3758
- ASSERT_OK (db_->Put (WriteOptions (), " foo" , " val1" ));
3759
- ASSERT_OK (Flush (0 ));
3783
+ ASSERT_OK (db_->Put (WriteOptions (), " foo" , " val0" ));
3784
+ ASSERT_OK (db_->Put (WriteOptions (), " bar" , " val0" ));
3785
+ ASSERT_OK (db_->DeleteRange (WriteOptions (), " bar" , " baz" ));
3786
+ ASSERT_OK (db_->Get (ReadOptions (), " foo" , &value));
3787
+ ASSERT_EQ (" val0" , value);
3788
+ ASSERT_TRUE (db_->Get (ReadOptions (), " bar" , &value).IsNotFound ());
3789
+ if (flush_before_close) {
3790
+ ASSERT_OK (Flush (0 ));
3791
+ }
3760
3792
Close ();
3761
3793
3762
3794
// Reopen the existing column family and enable user-defined timestamps
@@ -3765,47 +3797,63 @@ TEST_F(DBBasicTestWithTimestamp, EnableDisableUDT) {
3765
3797
options.persist_user_defined_timestamps = false ;
3766
3798
options.allow_concurrent_memtable_write = false ;
3767
3799
Reopen (options);
3768
-
3769
- std::string value;
3770
- ASSERT_TRUE (db_->Get (ReadOptions (), " foo" , &value).IsInvalidArgument ());
3771
- std::string read_ts;
3772
- PutFixed64 (&read_ts, 0 );
3773
- ReadOptions ropts;
3800
+ // Read data from previous session before and after compaction.
3801
+ read_ts = EncodeAsUint64 (1 );
3774
3802
Slice read_ts_slice = read_ts;
3775
3803
ropts.timestamp = &read_ts_slice;
3776
- std::string key_ts;
3777
- // Entries in pre-existing SST files are treated as if they have minimum
3778
- // user-defined timestamps.
3779
- ASSERT_OK (db_->Get (ropts, " foo" , &value, &key_ts));
3780
- ASSERT_EQ (" val1" , value);
3781
- ASSERT_EQ (read_ts, key_ts);
3804
+ for (int i = 0 ; i < 2 ; i++) {
3805
+ ASSERT_TRUE (db_->Get (ReadOptions (), " foo" , &value).IsInvalidArgument ());
3806
+ // Entries in pre-existing SST files are treated as if they have minimum
3807
+ // user-defined timestamps.
3808
+ ASSERT_OK (db_->Get (ropts, " foo" , &value, &key_ts));
3809
+ ASSERT_EQ (" val0" , value);
3810
+ ASSERT_EQ (EncodeAsUint64 (0 ), key_ts);
3811
+ ASSERT_TRUE (db_->Get (ropts, " bar" , &value, &key_ts).IsNotFound ());
3812
+ ASSERT_OK (db_->CompactRange (CompactRangeOptions (), nullptr , nullptr ));
3813
+ }
3782
3814
3783
3815
// Do timestamped read / write.
3784
- std::string write_ts;
3785
- PutFixed64 (&write_ts, 1 );
3786
- ASSERT_OK (db_->Put (WriteOptions (), " foo" , write_ts, " val2" ));
3787
- read_ts.clear ();
3788
- PutFixed64 (&read_ts, 1 );
3816
+ ASSERT_OK (db_->Put (WriteOptions (), " foo" , EncodeAsUint64 (1 ), " val1" ));
3817
+ ASSERT_OK (db_->Put (WriteOptions (), " bar" , EncodeAsUint64 (1 ), " val1" ));
3818
+ ASSERT_OK (db_->DeleteRange (WriteOptions (), " bar" , " baz" , EncodeAsUint64 (2 )));
3789
3819
ASSERT_OK (db_->Get (ropts, " foo" , &value, &key_ts));
3790
- ASSERT_EQ (" val2" , value);
3791
- ASSERT_EQ (write_ts, key_ts);
3820
+ ASSERT_EQ (" val1" , value);
3821
+ ASSERT_EQ (EncodeAsUint64 (1 ), key_ts);
3822
+ ASSERT_OK (db_->Get (ropts, " bar" , &value, &key_ts));
3823
+ ASSERT_EQ (" val1" , value);
3824
+ ASSERT_EQ (EncodeAsUint64 (1 ), key_ts);
3825
+ read_ts = EncodeAsUint64 (2 );
3826
+ ASSERT_TRUE (db_->Get (ropts, " bar" , &value, &key_ts).IsNotFound ());
3792
3827
// The user keys in this SST file don't have user-defined timestamps either,
3793
3828
// because `persist_user_defined_timestamps` flag is set to false.
3794
- ASSERT_OK (Flush (0 ));
3829
+ if (flush_before_close) {
3830
+ ASSERT_OK (Flush (0 ));
3831
+ }
3795
3832
Close ();
3796
3833
3797
3834
// Reopen the existing column family while disabling user-defined timestamps.
3798
3835
options.comparator = BytewiseComparator ();
3799
3836
Reopen (options);
3800
3837
3801
- ASSERT_TRUE (db_->Get (ropts, " foo" , &value).IsInvalidArgument ());
3802
- ASSERT_OK (db_->Get (ReadOptions (), " foo" , &value));
3803
- ASSERT_EQ (" val2" , value);
3838
+ // Read data from previous session before and after compaction.
3839
+ for (int i = 0 ; i < 2 ; i++) {
3840
+ ASSERT_TRUE (db_->Get (ropts, " foo" , &value).IsInvalidArgument ());
3841
+ ASSERT_OK (db_->Get (ReadOptions (), " foo" , &value));
3842
+ ASSERT_EQ (" val1" , value);
3843
+ ASSERT_TRUE (db_->Get (ReadOptions (), " bar" , &value).IsNotFound ());
3844
+ ASSERT_OK (db_->CompactRange (CompactRangeOptions (), nullptr , nullptr ));
3845
+ }
3804
3846
3805
3847
// Continue to write / read the column family without user-defined timestamps.
3806
- ASSERT_OK (db_->Put (WriteOptions (), " foo" , " val3" ));
3848
+ ASSERT_OK (db_->Put (WriteOptions (), " foo" , " val2" ));
3849
+ ASSERT_OK (db_->Put (WriteOptions (), " bar" , " val2" ));
3850
+ ASSERT_OK (db_->DeleteRange (WriteOptions (), " bar" , " baz" ));
3807
3851
ASSERT_OK (db_->Get (ReadOptions (), " foo" , &value));
3808
- ASSERT_EQ (" val3" , value);
3852
+ ASSERT_EQ (" val2" , value);
3853
+ ASSERT_TRUE (db_->Get (ReadOptions (), " bar" , &value).IsNotFound ());
3854
+ if (flush_before_close) {
3855
+ ASSERT_OK (Flush (0 ));
3856
+ }
3809
3857
Close ();
3810
3858
}
3811
3859
0 commit comments