Ejemplo n.º 1
0
//
// unmarshal key, value into a list of decoded fields
//
func (info indexinfo) unmarshalKeyValue(k, v []byte) ([]interface{}, error) {
	vkey, err := typedbuffer.DecodeAll(false, k)
	if err != nil {
		return nil, err
	}

	vval, err := typedbuffer.DecodeAll(false, v)
	if err != nil {
		return nil, err
	}

	lkey := len(vkey)
	lval := len(vval)

	fields := []interface{}{}

	var ival interface{}

	kk, lk := 0, len(info.iplist)

	for i := 0; i < lkey+lval; i++ {
		if kk < lk && uint(i) == info.iplist[kk].field {
			ival = vkey[info.iplist[kk].pos]
			kk += 1
		} else {
			ival = vval[0]
			vval = vval[1:]
		}

		fields = append(fields, ival)
	}

	return fields, nil
}
Ejemplo n.º 2
0
func Test_99_ForEach(t *testing.T) {
	indices := []string{
		"", // note that this contains the table description (info about indices)
		INDEX_1,
		INDEX_2,
	}

	for _, index := range indices {
		t.Log("content of index", index)

		n := 0

		if err := getTable(t).ForEach(index, func(k, v []byte) error {
			kk, _ := typedbuffer.DecodeAll(true, k)
			vv, _ := typedbuffer.DecodeAll(true, v)

			if index == "" {
				// the key should be the index name, not encoded
				kk = []interface{}{string(k)}
			}

			t.Logf("  k:%v, v:%v", kk, vv)
			n += 1
			return nil
		}); err != nil {
			t.Error("ForEach error", err)
		}

		t.Log("  total", n, "records")
	}
}