func (r *Reader) getDataIter(dataBH blockHandle, slice *util.Range, checksum, fillCache bool) iterator.Iterator {
	if r.cache != nil {
		// Get/set block cache.
		var err error
		cache, ok := r.cache.Get(dataBH.offset, func() (ok bool, value interface{}, charge int, fin cache.SetFin) {
			if !fillCache {
				return
			}
			var dataBlock *block
			dataBlock, err = r.readBlock(dataBH, checksum)
			if err == nil {
				ok = true
				value = dataBlock
				charge = int(dataBH.length)
				fin = func() {
					r.bpool.Put(dataBlock.data)
					dataBlock.data = nil
				}
			}
			return
		})
		if err != nil {
			return iterator.NewEmptyIterator(err)
		}
		if ok {
			dataBlock := cache.Value().(*block)
			if !dataBlock.checksum && (r.checksum || checksum) {
				if !verifyChecksum(dataBlock.data) {
					return iterator.NewEmptyIterator(errors.New("leveldb/table: Reader: invalid block (checksum mismatch)"))
				}
				dataBlock.checksum = true
			}
			iter := dataBlock.newIterator(slice, false, cache)
			return iter
		}
	}
	dataBlock, err := r.readBlock(dataBH, checksum)
	if err != nil {
		return iterator.NewEmptyIterator(err)
	}
	iter := dataBlock.newIterator(slice, false, releaseBlock{r, dataBlock})
	return iter
}
Beispiel #2
0
func (t *Reader) getDataIter(bi *bInfo, ro opt.ReadOptionsGetter) (it *block.Iterator, cache cache.Object, err error) {
	var b *block.Reader

	if t.cache != nil {
		cache, _ = t.cache.Get(bi.offset, func() (ok bool, value interface{}, charge int, fin func()) {
			var buf []byte
			buf, err = bi.readAll(t.r, ro.HasFlag(opt.RFVerifyChecksums))
			if err != nil {
				return
			}
			b, err = block.NewReader(buf, t.o.GetComparer())
			if err != nil {
				return
			}
			ok = true
			value = b
			charge = int(bi.size)
			return
		})

		if err != nil {
			return
		}

		if b == nil {
			b = cache.Value().(*block.Reader)
		}
	} else {
		var buf []byte
		buf, err = bi.readAll(t.r, ro.HasFlag(opt.RFVerifyChecksums))
		if err != nil {
			return
		}
		b, err = block.NewReader(buf, t.o.GetComparer())
		if err != nil {
			return
		}
	}

	it = b.NewIterator()
	return
}
Beispiel #3
0
func (t *Reader) getDataIter(bi *bInfo, ro opt.ReadOptionsGetter) (it *block.Iterator, cache cache.Object, err error) {
	var b *block.Reader

	if t.cache != nil {
		var ok bool
		cache, ok = t.cache.Get(bi.offset, func() (ok bool, value interface{}, charge int, fin func()) {
			if ro.HasFlag(opt.RFDontFillCache) {
				return
			}
			b, err = t.getBlock(bi, ro)
			if err == nil {
				ok = true
				value = b
				charge = int(bi.size)
			}
			return
		})

		if err != nil {
			return
		}

		if !ok {
			b, err = t.getBlock(bi, ro)
			if err != nil {
				return
			}
		} else if b == nil {
			b = cache.Value().(*block.Reader)
		}
	} else {
		b, err = t.getBlock(bi, ro)
		if err != nil {
			return
		}
	}

	it = b.NewIterator()
	return
}
func (r *Reader) getDataIter(dataBH blockHandle, slice *util.Range, checksum, fillCache bool) iterator.Iterator {
	if r.cache != nil {
		// Get/set block cache.
		var err error
		cache := r.cache.Get(dataBH.offset, func() (charge int, value interface{}) {
			if !fillCache {
				return 0, nil
			}
			var dataBlock *block
			dataBlock, err = r.readBlock(dataBH, checksum)
			if err != nil {
				return 0, nil
			}
			return int(dataBH.length), dataBlock
		})
		if err != nil {
			return iterator.NewEmptyIterator(err)
		}
		if cache != nil {
			dataBlock := cache.Value().(*block)
			if !dataBlock.checksum && (r.checksum || checksum) {
				if !verifyChecksum(dataBlock.data) {
					return iterator.NewEmptyIterator(errors.New("leveldb/table: Reader: invalid block (checksum mismatch)"))
				}
				dataBlock.checksum = true
			}
			iter := dataBlock.newIterator(slice, false, cache)
			return iter
		}
	}
	dataBlock, err := r.readBlock(dataBH, checksum)
	if err != nil {
		return iterator.NewEmptyIterator(err)
	}
	iter := dataBlock.newIterator(slice, false, dataBlock)
	return iter
}