예제 #1
0
func (r *IdxReader) Read(b []byte) (int, error) {
	if len(b) == 0 {
		return 0, nil
	}
	readed := 0
	buf := [HashSize]byte{}
	for {
		n, err := ReadHelper(r.r, buf[:])
		if n != HashSize {
			return readed, errors.New("fastpush IdxReader error: size % HashSize != 0")
		}
		if err == io.EOF && n == 0 {
			break
		} else if err != nil && err != io.EOF {
			return readed, err
		}
		find := false
		if fingerdb != nil {
			_, find = fingerdb.Find(buf)
		}
		if find {
			b[readed] = '1'
		} else {
			b[readed] = '0'
		}
		readed++
		if readed == len(b) {
			break
		}
	}
	if readed == 0 {
		return 0, io.EOF
	}
	return readed, nil
}
예제 #2
0
func (r *CntReader) Read(b []byte) (int, error) {
	if len(b) == 0 {
		return 0, io.EOF
	}
	readed := 0
	hashbuf := [HashSize]byte{}
	for {
		if len(r.cur) == 0 {
			n, err := ReadHelper(r.r, hashbuf[:])
			if err == io.EOF && n == 0 {
				break
			} else if err != nil && err != io.EOF {
				return readed, err
			}
			if n != HashSize {
				return readed, fmt.Errorf("read format error: size not HashSize")
			}
			//fmt.Printf("read hash [%x]\n",hashbuf)
			length, err := binary.ReadUvarint(r.r)
			if err != nil {
				return readed, err
			}
			//exists
			if length == 0 {
				metadata, find := fingerdb.Find(hashbuf)
				if !find {
					return readed, fmt.Errorf("hash not found")
				}
				file, err := fingerdb.NewBlockReader(metadata)
				if err != nil {
					return readed, err
				}

				/*
					blk,find := blockMap[hashbuf]
					if !find {
						return readed,fmt.Errorf("hash not found")
					}
					filename := blk.filepath
					off := blk.off
					file,err := fileOpenSeeker.OpenSeek(filename,off)
					if err != nil{
						return readed,err
					}
					len := blk.length*/

				len := metadata.Length
				r.cur = make([]byte, len, len)
				n, err := ReadHelper(file, r.cur)
				if err != nil && err != io.EOF {
					return readed, err
				}
				if uint32(n) != len {
					return readed, fmt.Errorf("read local file length wrong")
				}
				file.Close()
			} else {
				r.cur = make([]byte, length, length)
				n, err := ReadHelper(r.r, r.cur)
				r.container.WriteBlock(hashbuf, r.cur)
				if err != nil && err != io.EOF {
					return readed, err
				}
				if uint64(n) != length {
					return readed, fmt.Errorf("read net file length wrong")
				}
			}
		}
		n := copy(b[readed:], r.cur)
		readed += n
		r.cur = r.cur[n:]
		if readed >= len(b) {
			break
		}
	}
	if readed == 0 {
		return 0, io.EOF
	}
	return readed, nil
}