@@ -53,6 +53,82 @@ TEST_F(TimeSeriesTest, Create) {
5353 EXPECT_EQ (s.ToString (), " Invalid argument: key already exists" );
5454}
5555
56+ TEST_F (TimeSeriesTest, Alter) {
57+ redis::TSCreateOption option;
58+ option.retention_time = 3600 ;
59+ option.chunk_size = 1024 ;
60+ option.labels = {{" type" , " runtime" }, {" compiler" , " gcc" }, {" machine" , " Linux" }};
61+ key_ = " pa" ;
62+
63+ EXPECT_TRUE (ts_db_->Create (*ctx_, key_, option).ok ());
64+
65+ redis::TSInfoResult res;
66+ option.retention_time = 200 ;
67+ EXPECT_TRUE (ts_db_->Alter (*ctx_, key_, option, static_cast <uint8_t >(redis::TSAlterMode::RETENTION)).ok ());
68+ EXPECT_TRUE (ts_db_->Info (*ctx_, key_, &res).ok ());
69+ EXPECT_EQ (res.metadata .retention_time , 200 );
70+ EXPECT_EQ (res.metadata .chunk_size , 1024 );
71+ EXPECT_EQ (res.labels .size (), 3 );
72+
73+ // Update chunk size and verify other fields are unaffected.
74+ option.chunk_size = 128 ;
75+ EXPECT_TRUE (ts_db_->Alter (*ctx_, key_, option, static_cast <uint8_t >(redis::TSAlterMode::CHUNK_SIZE)).ok ());
76+ ts_db_->Info (*ctx_, key_, &res);
77+ EXPECT_EQ (res.metadata .retention_time , 200 );
78+ EXPECT_EQ (res.metadata .chunk_size , 128 );
79+ EXPECT_EQ (res.labels .size (), 3 );
80+
81+ // Verify records are properly inserted.
82+ std::vector<TSSample> samples = {{10 , 23 }, {12 , 24.5 }};
83+ std::vector<TSChunk::AddResult> results;
84+ results.resize (samples.size ());
85+ EXPECT_TRUE (ts_db_->MAdd (*ctx_, key_, samples, &results).ok ());
86+ EXPECT_EQ (results[0 ].sample .ts , 10 );
87+ EXPECT_EQ (results[1 ].sample .ts , 12 );
88+
89+ // Update labels and verify
90+ res.labels .clear ();
91+ option.labels = {{" compiler" , " gcc_12" }, {" version" , " 123" }};
92+ EXPECT_TRUE (ts_db_->Alter (*ctx_, key_, option, static_cast <uint8_t >(redis::TSAlterMode::LABELS)).ok ());
93+ EXPECT_TRUE (ts_db_->Info (*ctx_, key_, &res).ok ());
94+ EXPECT_EQ (res.metadata .retention_time , 200 );
95+ EXPECT_EQ (res.metadata .chunk_size , 128 );
96+ EXPECT_EQ (res.labels .size (), 2 );
97+ redis::LabelKVPair first = {" version" , " 123" }, second = {" compiler" , " gcc_12" };
98+ EXPECT_TRUE (std::find_if (res.labels .begin (), res.labels .end (), [first](const auto &label) {
99+ return first.k == label.k && first.v == label.v ;
100+ }) != res.labels .end ());
101+ EXPECT_TRUE (std::find_if (res.labels .begin (), res.labels .end (), [second](const auto &label) {
102+ return second.k == label.k && second.v == label.v ;
103+ }) != res.labels .end ());
104+
105+ // Verify reverse-indexes are properly updated.
106+ key_ = " pavani" ;
107+ redis::TSCreateOption second_option;
108+ second_option.labels = {{" compiler" , " gcc_12" }, {" Desktop" , " Lubuntu" }};
109+ EXPECT_TRUE (ts_db_->Create (*ctx_, key_, second_option).ok ());
110+
111+ redis::TSMGetOption::FilterOption filters;
112+ filters.labels_equals = {{" compiler" , {" gcc_12" }}};
113+ std::vector<std::string> query_res;
114+ EXPECT_TRUE (ts_db_->QueryIndex (*ctx_, filters, &query_res).ok ());
115+ EXPECT_EQ (query_res.size (), 2 );
116+ EXPECT_TRUE (std::find (query_res.begin (), query_res.end (), " pa" ) != query_res.end ());
117+ EXPECT_TRUE (std::find (query_res.begin (), query_res.end (), " pavani" ) != query_res.end ());
118+
119+ // old labels should be deleted.
120+ filters.labels_equals .clear ();
121+ filters.labels_equals = {{" machine" , {" Linux" }}};
122+ query_res.clear ();
123+ EXPECT_TRUE (ts_db_->QueryIndex (*ctx_, filters, &query_res).ok ());
124+ EXPECT_TRUE (query_res.empty ());
125+
126+ key_ = " pavni" ;
127+ auto s = ts_db_->Alter (*ctx_, key_, option, 1 );
128+ EXPECT_FALSE (s.ok ());
129+ EXPECT_EQ (s.ToString (), " Invalid argument: key not exists" );
130+ }
131+
56132TEST_F (TimeSeriesTest, Add) {
57133 redis::TSCreateOption option;
58134 option.retention_time = 3600 ;
0 commit comments