Exemplo n.º 1
0
    int ObObj::apply(const ObObj &mutation)
    {
      int64_t org_type = get_type();
      int64_t org_ext = get_ext();
      int err = OB_SUCCESS;
      ObCreateTime create_time = 0;
      ObModifyTime modify_time = 0;
      bool is_add = false;
      bool org_is_add = false;
      if (ObSeqType == mutation.get_type()
        || ObMinType >= mutation.get_type()
        || ObMaxType <= mutation.get_type())
      {
        TBSYS_LOG(WARN,"unsupported type [type:%d]", static_cast<int32_t>(mutation.get_type()));
        err = OB_INVALID_ARGUMENT;
      }
      if (OB_SUCCESS == err 
        && ObExtendType != get_type()
        && ObNullType != get_type()
        && ObExtendType != mutation.get_type()
        && ObNullType != mutation.get_type()
        && get_type() != mutation.get_type())
      {
        TBSYS_LOG(WARN,"type not coincident [this->type:%d,mutation.type:%d]", 
          static_cast<int>(get_type()), static_cast<int>(mutation.get_type()));
        err = OB_INVALID_ARGUMENT;
      }
      _ObjValue value, mutation_value;
      if (OB_SUCCESS == err)
      {
        bool ext_val_can_change =  (ObActionFlag::OP_ROW_DOES_NOT_EXIST == get_ext())  ||  (ObNullType == get_type());
        bool org_is_nop = (ObActionFlag::OP_NOP == get_ext());
        switch (mutation.get_type())
        {
        case ObNullType: 
          set_null();
          break;
        case ObVarcharType:
          *this = mutation;
          break;
        case ObIntType:
          if (ext_val_can_change || org_is_nop)
          {
            set_int(0);
          }
          err = get_int(value.int_val,org_is_add);
          if (OB_SUCCESS == err)
          {
            err = mutation.get_int(mutation_value.int_val,is_add);
          }
          if (OB_SUCCESS == err)
          {
            if (is_add)
            {
              value.int_val += mutation_value.int_val;
            }
            else
            {
              value.int_val = mutation_value.int_val;
            }
            set_int(value.int_val, (org_is_add || org_is_nop) && is_add);
          }
          break;
        case ObFloatType:
          if (ext_val_can_change || org_is_nop)
          {
            set_float(0);
          }
          err = get_float(value.float_val,org_is_add);
          if (OB_SUCCESS == err)
          {
            err = mutation.get_float(mutation_value.float_val, is_add);
          }
          if (OB_SUCCESS == err)
          {
            if (is_add)
            {
              value.float_val += mutation_value.float_val;
            }
            else
            {
              value.float_val = mutation_value.float_val;
            }
            set_float(value.float_val,is_add && (org_is_add || org_is_nop));
          }
          break;
        case ObDoubleType:
          if (ext_val_can_change || org_is_nop)
          {
            set_double(0);
          }
          err = get_double(value.double_val,org_is_add);
          if (OB_SUCCESS == err)
          {
            err = mutation.get_double(mutation_value.double_val,is_add);
          }
          if (OB_SUCCESS == err)
          {
            if (is_add)
            {
              value.double_val += mutation_value.double_val;
            }
            else
            {
              value.double_val = mutation_value.double_val;
            }
            set_double(value.double_val, (org_is_add || org_is_nop) && is_add);
          }
          break;
        case ObDateTimeType:
          if (ext_val_can_change || org_is_nop)
          {
            set_datetime(0);
          }
          err = get_datetime(value.second_val,org_is_add);
          if (OB_SUCCESS == err)
          {
            err = mutation.get_datetime(mutation_value.second_val,is_add);
          }
          if (OB_SUCCESS == err)
          {
            if (is_add)
            {
              value.second_val += mutation_value.second_val;
            }
            else
            {
              value.second_val = mutation_value.second_val;
            }
            set_datetime(value.second_val,is_add && (org_is_add || org_is_nop));
          }
          break;
        case ObPreciseDateTimeType:
          if (ext_val_can_change || org_is_nop)
          {
            set_precise_datetime(0);
          }
          err = get_precise_datetime(value.microsecond_val,org_is_add);
          if (OB_SUCCESS == err)
          {
            err = mutation.get_precise_datetime(mutation_value.microsecond_val,is_add);
          }
          if (OB_SUCCESS == err)
          {
            if (is_add)
            {
              value.microsecond_val += mutation_value.microsecond_val;
            }
            else
            {
              value.microsecond_val = mutation_value.microsecond_val;
            }
            set_precise_datetime(value.microsecond_val,is_add && (org_is_add || org_is_nop));
          }
          break;
        case ObExtendType:
          switch (mutation.get_ext())
          {
          case ObActionFlag::OP_DEL_ROW:
          case ObActionFlag::OP_DEL_TABLE:
            /// used for join, if right row was deleted, set the cell to null
            set_null();
            break;
          case ObActionFlag::OP_ROW_DOES_NOT_EXIST:
            /// do nothing
            break;
          case ObActionFlag::OP_NOP:
            if (get_ext() == ObActionFlag::OP_ROW_DOES_NOT_EXIST
              || get_ext() == ObActionFlag::OP_DEL_ROW)
            {
              set_null();
            }
            break;
          default:
            TBSYS_LOG(ERROR,"unsupported ext value [value:%ld]", mutation.get_ext());
            err = OB_INVALID_ARGUMENT;
            break;
          }

          break;
        case ObCreateTimeType:
          err = mutation.get_createtime(create_time);
          if (OB_SUCCESS == err)
          {
            if (ext_val_can_change || org_is_nop)
            {
              set_createtime(create_time);
            }
          }
          if (OB_SUCCESS == err)
          {
            err = get_createtime(value.create_time_val);
          }
          if (OB_SUCCESS == err)
          {
            err = mutation.get_createtime(mutation_value.create_time_val);
          }
          if (OB_SUCCESS == err)
          {
            set_createtime(std::min<ObCreateTime>(value.create_time_val,mutation_value.create_time_val));
          }
          break;
        case ObModifyTimeType:
          err = mutation.get_modifytime(modify_time);
          if (OB_SUCCESS == err)
          {
            if (ext_val_can_change || org_is_nop)
            {
              set_modifytime(modify_time);
            }
          }
          if (OB_SUCCESS == err)
          {
            err = get_modifytime(value.modify_time_val);
          }
          if (OB_SUCCESS == err)
          {
            err = mutation.get_modifytime(mutation_value.modify_time_val);
          }
          if (OB_SUCCESS == err)
          {
            set_modifytime(std::max<ObCreateTime>(value.modify_time_val,mutation_value.modify_time_val));
          }
          break;
        default:
          /* case ObSeqType: */
          TBSYS_LOG(ERROR,"unsupported type [type:%d]", static_cast<int32_t>(mutation.get_type()));
          err = OB_INVALID_ARGUMENT;
          break;
        }
      }
      if(OB_SUCCESS != err)
      {
        TBSYS_LOG(WARN,"fail to apply [this->type:%ld,this->ext:%ld,mutation->type:%d,mutation->ext:%ld, err:%d]", 
          org_type, org_ext, mutation.get_type(), mutation.get_ext(), err);
      }
      return err;
    }
Exemplo n.º 2
0
 bool ObObj::operator==(const ObObj &that_obj) const
 {
   bool result = false;
   int err = OB_SUCCESS;
   if ((get_type() == ObNullType) && (that_obj.get_type() == ObNullType))
   {
     result = true;
   }
   else if ((get_type() == ObNullType) || (that_obj.get_type() == ObNullType))
   {
     result = false;
   }
   else if (!can_compare(that_obj))
   {
     result = false;
     TBSYS_LOG(ERROR, "logic error, cann't compare two obj with different type [this.type:%d,"
       "that.type:%d]", get_type(), that_obj.get_type());
   }
   else
   {
     _ObjValue this_value;
     _ObjValue that_value;
     int64_t this_timestamp = 0;
     int64_t that_timestamp = 0;
     ObObjType type = get_type();
     switch (type)
     {
     case ObIntType:
       err = get_int(this_value.int_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_int(that_value.int_val);
         if (OB_SUCCESS == err)
         {
           result = this_value.int_val == that_value.int_val;
         }
       }
       break;
     case ObVarcharType:
       err = get_varchar(this_value.varchar_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_varchar(that_value.varchar_val);
         if (OB_SUCCESS == err)
         {
           result = this_value.varchar_val == that_value.varchar_val;
         }
       }
       break;
     case ObFloatType:
       err = get_float(this_value.float_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_float(that_value.float_val);
         if (OB_SUCCESS == err)
         {
           result = fabsf(this_value.float_val - that_value.float_val) < FLOAT_EPSINON;
         }
       }
       break;
     case ObDoubleType:
       err = get_double(this_value.double_val);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_double(that_value.double_val);
         if (OB_SUCCESS == err)
         {
           result = fabs(this_value.double_val - that_value.double_val) < DOUBLE_EPSINON;
         }
       }
       break;
     case ObDateTimeType:
     case ObPreciseDateTimeType:
     case ObCreateTimeType:
     case ObModifyTimeType:
       err = get_timestamp(this_timestamp);
       if (OB_SUCCESS == err)
       {
         err = that_obj.get_timestamp(that_timestamp);
         if (OB_SUCCESS == err)
         {
           result = this_timestamp == that_timestamp;
         }
       }
       break;
       /*case ObSeqType:
         /// @todo (wushi [email protected]) sequence
         break;*/
     default:
       result = OB_ERR_UNEXPECTED;
       TBSYS_LOG(ERROR,"unexpected obj type [obj.type:%d]", type);
       break;
     }
   }
   return result;
 }