示例#1
0
func (m *Mutate) serializeNoReflect() *pb.MutateRequest {
	// We need to convert everything in the values field
	// to a protobuf ColumnValue
	bytevalues := make([]*pb.MutationProto_ColumnValue, len(m.values))
	i := 0
	for k, v := range m.values {
		qualvals := make([]*pb.MutationProto_ColumnValue_QualifierValue, len(v))
		j := 0
		// And likewise, each item in each column needs to be converted to a
		// protobuf QualifierValue
		for k1, v1 := range v {
			qualvals[j] = &pb.MutationProto_ColumnValue_QualifierValue{
				Qualifier: []byte(k1),
				Value:     v1,
			}
			if m.mutationType == pb.MutationProto_DELETE {
				tmp := pb.MutationProto_DELETE_MULTIPLE_VERSIONS
				qualvals[j].DeleteType = &tmp
			}
			j++
		}
		bytevalues[i] = &pb.MutationProto_ColumnValue{
			Family:         []byte(k),
			QualifierValue: qualvals,
		}
		i++
	}
	durability := pb.MutationProto_Durability(m.durability)
	mProto := &pb.MutationProto{
		Row:         m.key,
		MutateType:  &m.mutationType,
		ColumnValue: bytevalues,
		Durability:  &durability,
	}
	if m.timestamp != MaxTimestamp {
		mProto.Timestamp = &m.timestamp
	}
	return &pb.MutateRequest{
		Region:   m.regionSpecifier(),
		Mutation: mProto,
	}
}
示例#2
0
// serializeWithReflect is a helper function for Serialize. It is used when
// there is a struct with tagged fields to be serialized.
func (m *Mutate) serializeWithReflect() (*pb.MutateRequest, error) {
	typeOf := reflect.TypeOf(m.data)
	valueOf := reflect.Indirect(reflect.ValueOf(m.data))

	columns := make(map[string][]*pb.MutationProto_ColumnValue_QualifierValue)

	for i := 0; i < typeOf.NumField(); i++ {
		field := typeOf.Field(i)
		if field.PkgPath != "" {
			// This is an unexported field of the struct, so we're going to
			// ignore it
			continue
		}

		tagval := field.Tag.Get("hbase")
		if tagval == "" {
			// If the tag is empty, we're going to ignore this field
			continue
		}
		cnames := strings.SplitN(tagval, ":", 2)
		if len(cnames) != 2 {
			// If the tag doesn't contain a colon, it's set improperly
			return nil, fmt.Errorf("Invalid column family and column qualifier: \"%s\"", cnames)
		}
		cfamily := cnames[0]
		cqualifier := cnames[1]

		binaryValue, err := valueToBytes(valueOf.Field(i))
		if err != nil {
			return nil, err
		}

		qualVal := &pb.MutationProto_ColumnValue_QualifierValue{
			Qualifier: []byte(cqualifier),
			Value:     binaryValue,
		}

		if m.mutationType == pb.MutationProto_DELETE {
			tmp := pb.MutationProto_DELETE_MULTIPLE_VERSIONS
			qualVal.DeleteType = &tmp
		}
		columns[cfamily] = append(columns[cfamily], qualVal)
	}

	pbcolumns := make([]*pb.MutationProto_ColumnValue, 0, len(columns))
	for k, v := range columns {
		colval := &pb.MutationProto_ColumnValue{
			Family:         []byte(k),
			QualifierValue: v,
		}
		pbcolumns = append(pbcolumns, colval)

	}
	durability := pb.MutationProto_Durability(m.durability)
	mProto := &pb.MutationProto{
		Row:         m.key,
		MutateType:  &m.mutationType,
		ColumnValue: pbcolumns,
		Durability:  &durability,
	}
	if m.timestamp != MaxTimestamp {
		mProto.Timestamp = &m.timestamp
	}
	return &pb.MutateRequest{
		Region:   m.regionSpecifier(),
		Mutation: mProto,
	}, nil
}