int ObReadParam::serialize_reserve_param(char * buf, const int64_t buf_len, int64_t & pos) const { ObObj obj; // serialize RESERVER PARAM FIELD obj.set_ext(ObActionFlag::RESERVE_PARAM_FIELD); int ret = obj.serialize(buf, buf_len, pos); if (ret == OB_SUCCESS) { obj.set_int(get_is_read_consistency()); ret = obj.serialize(buf, buf_len, pos); } return ret; }
TEST(ObObj,Serialize_int_add) { ObObj t; t.set_int(9900); int64_t len = t.get_serialize_size(); ASSERT_EQ(len,3); char *buf = (char *)malloc(len); int64_t pos = 0; ASSERT_EQ(t.serialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); ObObj f; pos = 0; ASSERT_EQ(f.deserialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); int64_t r = 0; int64_t l = 0; ASSERT_EQ(f.get_type(),t.get_type()); f.get_int(r); t.get_int(l); ASSERT_EQ(r,l); free(buf); }
TEST(ObObj,Serialize_bool) { ObObj t; t.set_bool(true); int64_t len = t.get_serialize_size(); ASSERT_EQ(len,2); char *buf = (char *)malloc(len); int64_t pos = 0; ASSERT_EQ(t.serialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); ObObj f; pos = 0; ASSERT_EQ(f.deserialize(buf,len,pos),OB_SUCCESS); ASSERT_EQ(pos,len); bool r = false; bool l = false; ASSERT_EQ(f.get_type(),t.get_type()); f.get_bool(r); t.get_bool(l); ASSERT_EQ(r,l); free(buf); }
TEST(ObObj,Serialize_createtime) { ObCreateTime data[] = {1,28,1,static_cast<int64_t>(UINT32_MAX),INT64_MAX,124543900,221348087492}; int64_t need_len = 0; for(uint32_t i=0;i<sizeof(data)/sizeof(data[0]);++i) { need_len += serialization::encoded_length_createtime(data[i]); } char *buf = (char *)malloc(need_len); ObObj t; int64_t pos = 0; for(uint32_t i=0;i<sizeof(data)/sizeof(data[0]);++i) { t.reset(); t.set_createtime(data[i]); ASSERT_EQ(t.serialize(buf,need_len,pos),OB_SUCCESS); } pos = 0; ObCreateTime v = 0; for(uint32_t i=0;i<sizeof(data)/sizeof(data[0]);++i) { t.reset(); ASSERT_EQ(t.deserialize(buf,need_len,pos),OB_SUCCESS); ASSERT_EQ(t.get_createtime(v),OB_SUCCESS); ASSERT_EQ(v,data[i]); } free(buf); }
TEST(ObObj,int_boundary) { int64_t need_len = 0; int64_t data[] = {1,28,-1,INT32_MIN,static_cast<int64_t>(UINT32_MAX),INT64_MAX,INT64_MIN,UINT64_MAX,124543900}; for(uint32_t i=0;i<sizeof(data)/sizeof(data[0]);++i) { need_len += serialization::encoded_length_int(data[i]); } char *buf = (char *)malloc(need_len); ObObj t; int64_t pos = 0; for(uint32_t i=0;i<sizeof(data)/sizeof(data[0]);++i) { t.reset(); t.set_int(data[i]); ASSERT_EQ(t.serialize(buf,need_len,pos),OB_SUCCESS); } pos = 0; int64_t v = 0; for(uint32_t i=0;i<sizeof(data)/sizeof(data[0]);++i) { t.reset(); ASSERT_EQ(t.deserialize(buf,need_len,pos),OB_SUCCESS); ASSERT_EQ(t.get_int(v),OB_SUCCESS); ASSERT_EQ(v,data[i]); } free(buf); }
int set_str_obj_value(char * buf, const int64_t buf_len, int64_t & pos, const ObString &value) { int ret = OB_SUCCESS; ObObj obj; obj.set_varchar(value); ret = obj.serialize(buf, buf_len, pos); return ret; }
int set_int_obj_value(char * buf, const int64_t buf_len, int64_t & pos, const int64_t value) { int ret = OB_SUCCESS; ObObj obj; obj.set_int(value); ret = obj.serialize(buf, buf_len, pos); return ret; }
int ObSqlGetParam::serialize_int(char* buf, const int64_t buf_len, int64_t& pos, const int64_t value) const { int ret = OB_SUCCESS; ObObj obj; obj.set_int(value); ret = obj.serialize(buf, buf_len, pos); return ret; }
int ObSqlGetParam::serialize_flag(char* buf, const int64_t buf_len, int64_t& pos, const int64_t flag) const { int ret = OB_SUCCESS; ObObj obj; obj.set_ext(flag); ret = obj.serialize(buf, buf_len, pos); return ret; }
TEST(ObObj, test_bug) { ObObj obj; obj.set_int(100, false); char buf[2048]; int64_t len = 2048; int64_t pos = 0; ASSERT_EQ(obj.serialize(buf, len, pos), OB_SUCCESS); pos = 0; ObObj temp; temp.set_int(200, true); temp.reset(); ASSERT_EQ(temp.deserialize(buf, len, pos), OB_SUCCESS); ASSERT_EQ(temp == obj, true); ObObj test; test.set_int(300, true); test = temp; ASSERT_EQ(test == temp, true); }
TEST(ObObj, performance) { // int64_t start_time = tbsys::CTimeUtil::getTime(); //const int64_t MAX_COUNT = 10 * 1000 * 1000L; const int64_t MAX_COUNT = 1000L; ObObj obj; char buf[2048]; int64_t len = 2048; int64_t pos = 0; int64_t data = 0; for (int64_t i = 0; i < MAX_COUNT; ++i) { obj.set_int(i); pos = 0; ASSERT_EQ(obj.serialize(buf, len, pos), OB_SUCCESS); pos = 0; ASSERT_EQ(obj.deserialize(buf, len, pos), OB_SUCCESS); ASSERT_EQ(obj.get_int(data), OB_SUCCESS); ASSERT_EQ(data, i); } const char *tmp = "Hello12344556666777777777777545352454254254354354565463241242354345345345235345"; ObObj obj2; ObString string; ObString string2; string.assign_ptr(const_cast<char *>(tmp), 1024); obj2.set_varchar(string); for (register int64_t i = 0; i < MAX_COUNT; ++i ) { pos = 0; ASSERT_EQ(obj2.serialize(buf, len, pos), OB_SUCCESS); pos = 0; ASSERT_EQ(obj2.deserialize(buf, len, pos), OB_SUCCESS); ASSERT_EQ(obj2.get_varchar(string2), OB_SUCCESS); } // int64_t end_time = tbsys::CTimeUtil::getTime(); //printf("using time:%ld\n", end_time - start_time); }
int ObSqlReadParam::serialize_end_param(char * buf, const int64_t buf_len, int64_t & pos) const { ObObj obj; obj.set_ext(ObActionFlag::END_PARAM_FIELD); return obj.serialize(buf, buf_len, pos); }
////////////////////// SERIALIZATION /////////////////////// int ObSqlReadParam::serialize_basic_param(char * buf, const int64_t buf_len, int64_t & pos) const { ObObj obj; int ret = OB_SUCCESS; // read consistency if (ret == OB_SUCCESS) { obj.set_int(get_is_read_consistency()); if (OB_SUCCESS != (ret = obj.serialize(buf, buf_len, pos))) { TBSYS_LOG(WARN, "fail to serialize basic param. buf=%p, buf_len=%ld, pos=%ld, ret=%d", buf, buf_len, pos, ret); } } // result cached if (ret == OB_SUCCESS) { obj.set_int(get_is_result_cached()); if (OB_SUCCESS != (ret = obj.serialize(buf, buf_len, pos))) { TBSYS_LOG(WARN, "fail to serialize basic param. buf=%p, buf_len=%ld, pos=%ld, ret=%d", buf, buf_len, pos, ret); } } // data version if (ret == OB_SUCCESS) { obj.set_int(get_data_version()); if (OB_SUCCESS != (ret = obj.serialize(buf, buf_len, pos))) { TBSYS_LOG(WARN, "fail to serialize basic param. buf=%p, buf_len=%ld, pos=%ld, ret=%d", buf, buf_len, pos, ret); } } // table id if (OB_SUCCESS == ret) { if (OB_INVALID_ID != table_id_) { obj.set_int(table_id_); if (OB_SUCCESS != (ret = obj.serialize(buf, buf_len, pos))) { TBSYS_LOG(WARN, "fail to serialize table id = %lu", table_id_); } } else { TBSYS_LOG(WARN, "Invalid table_id_. table_id_=%ld", table_id_); ret = OB_INVALID_ARGUMENT; } } // renamed table id if (OB_SUCCESS == ret) { if (OB_INVALID_ID != renamed_table_id_) { obj.set_int(renamed_table_id_); if (OB_SUCCESS != (ret = obj.serialize(buf, buf_len, pos))) { TBSYS_LOG(WARN, "fail to serialize renamed table id = %lu", renamed_table_id_); } } else { TBSYS_LOG(WARN, "Invalid renamed_table_id_. renamed_table_id_=%ld", renamed_table_id_); ret = OB_INVALID_ARGUMENT; } } if (OB_SUCCESS == ret) { obj.set_bool(only_static_data_); if (OB_SUCCESS != (ret = obj.serialize(buf, buf_len, pos))) { TBSYS_LOG(WARN, "fail to serialize bool:ret[%d]", ret); } } return ret; }