示例#1
0
文件: insert.go 项目: seagatesoft/fs2
func (self *BpTree) newVarcharKey(n uint64, key []byte, allowDups bool) (vkey []byte, err error) {
	var has bool
	err = self.doLeaf(n, func(n *leaf) error {
		var idx int
		idx, has, err = find(self.varchar, n, key)
		if err != nil {
			return err
		}
		if has {
			vkey = make([]byte, n.meta.keySize)
			copy(vkey, n.key(idx))
			return self.varchar.Ref(*slice.AsUint64(&vkey))
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	if has {
		return vkey, nil
	}
	k, err := self.varchar.Alloc(len(key))
	if err != nil {
		return nil, err
	}
	err = self.varchar.Do(k, func(data []byte) error {
		copy(data, key)
		return nil
	})
	if err != nil {
		return nil, err
	}
	return slice.Uint64AsSlice(&k), nil
}
示例#2
0
文件: insert.go 项目: seagatesoft/fs2
func (self *BpTree) checkValue(value []byte) ([]byte, error) {
	if self.meta.flags&consts.VARCHAR_VALS != 0 {
		v, err := self.varchar.Alloc(len(value))
		if err != nil {
			return nil, err
		}
		err = self.varchar.Do(v, func(data []byte) error {
			copy(data, value)
			return nil
		})
		if err != nil {
			return nil, err
		}
		value = slice.Uint64AsSlice(&v)
	} else if len(value) != int(self.meta.valSize) {
		return nil, errors.Errorf("value was the wrong size")
	}
	return value, nil
}
示例#3
0
文件: list.go 项目: postfix/fs2
func (l *List) newBlk(key []byte) (uint64, error) {
	a, err := l.bf.Allocate()
	if err != nil {
		return 0, err
	}
	err = l.idxTree.Add(key, slice.Uint64AsSlice(&a))
	if err != nil {
		return 0, err
	}
	err = l.bf.Do(a, 1, func(bytes []byte) error {
		blk := l.asIdx(bytes)
		blk.Init()
		return nil
	})
	if err != nil {
		return 0, err
	}
	return a, nil
}
示例#4
0
func TestEndOfPureRunVarchar(x *testing.T) {
	t := (*T)(x)
	for TEST := 0; TEST < 1; TEST++ {
		bpt, clean := t.bpt()
		k, err := bpt.varchar.Alloc(17)
		t.assert_nil(err)
		t.assert_nil(bpt.varchar.Do(k, func(data []byte) error {
			copy(data, []byte{1, 0, 1, 0, 1, 0, 1, 0})
			return nil
		}))
		KEY := slice.Uint64AsSlice(&k)
		kvs := make([]*KV, 0, 2000)
		start, err := bpt.newLeaf()
		t.assert_nil(err)
		cur := start
		for i := 0; i < rand.Intn(500)+255; i++ {
			kv := &KV{
				key:   KEY,
				value: t.rand_value(8),
			}
			kvs = append(kvs, kv)
			var fits bool = false
			t.assert_nil(bpt.doLeaf(cur, func(cur *leaf) error {
				fits = cur.fitsAnother()
				return nil
			}))
			if !fits {
				next, err := bpt.newLeaf()
				t.assert_nil(err)
				t.assert_nil(bpt.insertListNode(next, cur, 0))
				cur = next
			}
			t.assert_nil(bpt.doLeaf(cur, func(cur *leaf) error {
				return cur.putKV(bpt.varchar, kv.key, kv.value)
			}))
		}
		end, err := bpt.endOfPureRun(start)
		t.assert_nil(err)
		t.assert("end should be cur", end == cur)
		clean()
	}
}