Exemplo n.º 1
0
func (self *Store) Op_iterate(operation *operations.Iterate) (
	ret bucket.BucketReturn) {

	database, err := self.Database()
	if err != nil {
		ret = &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
		return
	}

	r, err := self.createScanRangeForIterate(operation, self.State.BucketId)
	if err != nil {
		ret = &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorClient, err)}
		return
	}
	iterator := database.NewIterator(r, nil)
	defer iterator.Release()

	var exhausted bool
	exhausted = !iterator.First()
	resultNumber := 0
	var numberOfErrors uint32
	numberOfErrors = 0
	for !exhausted {
		key, err := typescommon.PrefixedKeyBytesToKey(
			self.State.BucketId, iterator.Key())
		if err != nil {
			numberOfErrors++
		} else {
			value, err := types.NewArrayFromBytes(iterator.Value())
			if err != nil {
				numberOfErrors++
			} else {
				// Ok
				iterateState := new(operations.IterateState)
				iterateState.ResultNumber = uint32(resultNumber)
				iterateState.Key = key
				iterateState.Value = value

				end := operation.Function(iterateState)
				if end {
					break
				}
			}
		}
		resultNumber++
		exhausted = iterator.Next()
		exhausted = !iterator.Valid() //TODO: Why do we need this too?
	}

	return &operations.IterateReturn{
		Status:         retcode.NewStatusOk(),
		NumberOfErrors: numberOfErrors,
	}
}
Exemplo n.º 2
0
func (self *Store) Op_get(operation *operations.Get) (ret bucket.BucketReturn) {
	database, err := self.Database()
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	keyBytes := self.KeyToBytesAndPrefix(operation.Key)
	value, err := database.Get(keyBytes, nil)
	if err == leveldb.ErrNotFound {
		return &bucket.ReturnGeneric{retcode.NewStatus(retcode.OkNotFound)}
	}
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}
	keyConstructed, err := types.NewArrayFromBytes(value)
	if err != nil {
		return &bucket.ReturnGeneric{retcode.NewStatusError(retcode.ErrorServer, err)}
	}

	return &operations.GetReturn{
		Status: retcode.NewStatusOk(),
		Value:  keyConstructed,
	}
}