示例#1
0
func (m cdbMap) Visit(visit func(NeedleValue) error) (err error) {
	fh, err := os.Open(m.fn1)
	if err != nil {
		return fmt.Errorf("cannot open %s: %s", m.fn1, err)
	}
	defer fh.Close()
	walk := func(elt cdb.Element) error {
		if len(elt.Key) != 8 {
			return nil
		}
		return visit(NeedleValue{Key: Key(util.BytesToUint64(elt.Key)),
			Offset: util.BytesToUint32(elt.Data[:4]),
			Size:   util.BytesToUint32(elt.Data[4:8])})
	}
	if err = cdb.DumpMap(fh, walk); err != nil {
		return err
	}
	if m.c2 == nil {
		return nil
	}
	fh.Close()
	if fh, err = os.Open(m.fn2); err != nil {
		return fmt.Errorf("cannot open %s: %s", m.fn2, err)
	}
	return cdb.DumpMap(fh, walk)
}
示例#2
0
文件: needle.go 项目: xujianhai/gopa
func (n *Needle) Read(r io.Reader, size uint32) (int, error) {
	bytes := make([]byte, size+16+4)
	ret, e := r.Read(bytes)
	n.Cookie = util.BytesToUint32(bytes[0:4])
	n.Key = util.BytesToUint64(bytes[4:12])
	n.Size = util.BytesToUint32(bytes[12:16])
	n.Data = bytes[16 : 16+size]
	n.Checksum = int32(util.BytesToUint32(bytes[16+size : 16+size+4]))
	return ret, e
}
示例#3
0
文件: needle.go 项目: xujianhai/gopa
func ParseKeyHash(key_hash_string string) (uint64, uint32) {
	key_hash_bytes, khe := hex.DecodeString(key_hash_string)
	key_hash_len := len(key_hash_bytes)
	if khe != nil || key_hash_len <= 4 {
		log.Error("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
		return 0, 0
	}
	key := util.BytesToUint64(key_hash_bytes[0 : key_hash_len-4])
	hash := util.BytesToUint32(key_hash_bytes[key_hash_len-4 : key_hash_len])
	return key, hash
}
示例#4
0
文件: needle.go 项目: xujianhai/gopa
func ReadNeedle(r *os.File) (*Needle, uint32) {
	n := new(Needle)
	bytes := make([]byte, 16)
	count, e := r.Read(bytes)
	if count <= 0 || e != nil {
		return nil, 0
	}
	n.Cookie = util.BytesToUint32(bytes[0:4])
	n.Key = util.BytesToUint64(bytes[4:12])
	n.Size = util.BytesToUint32(bytes[12:16])
	rest := 8 - ((n.Size + 16 + 4) % 8)
	r.Seek(int64(n.Size+4+rest), 1)
	return n, 16 + n.Size + 4 + rest
}
示例#5
0
func LoadNeedleMap(file *os.File) *NeedleMap {
	nm := NewNeedleMap(file)
	bytes := make([]byte, 16*RowsToRead)
	count, e := nm.indexFile.Read(bytes)
	if count > 0 {
		fstat, _ := file.Stat()
		log.Info("Loading index file", fstat.Name(), "size", fstat.Size())
	}
	for count > 0 && e == nil {
		for i := 0; i < count; i += 16 {
			key := util.BytesToUint64(bytes[i : i+8])
			offset := util.BytesToUint32(bytes[i+8 : i+12])
			size := util.BytesToUint32(bytes[i+12 : i+16])
			if offset > 0 {
				nm.m[key] = &NeedleValue{Offset: offset, Size: size}
			} else {
				delete(nm.m, key)
			}
		}

		count, e = nm.indexFile.Read(bytes)
	}
	return nm
}
示例#6
0
func (n *Needle) readNeedleHeader(bytes []byte) {
	n.Cookie = util.BytesToUint32(bytes[0:4])
	n.Id = util.BytesToUint64(bytes[4:12])
	n.Size = util.BytesToUint32(bytes[12:NeedleHeaderSize])
}