Exemplo n.º 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)
}
Exemplo n.º 2
0
func LoadNewNeedleMap(file *os.File) CompactMap {
	m := NewCompactMap()
	bytes := make([]byte, 16*1024)
	count, e := file.Read(bytes)
	if count > 0 {
		fstat, _ := file.Stat()
		glog.V(0).Infoln("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 {
				m.Set(Key(key), offset, size)
			} else {
				//delete(m, key)
			}
		}

		count, e = file.Read(bytes)
	}
	return m
}
Exemplo n.º 3
0
func (n *Needle) readNeedleDataVersion2(bytes []byte) {
	index, lenBytes := 0, len(bytes)
	if index < lenBytes {
		n.DataSize = util.BytesToUint32(bytes[index : index+4])
		index = index + 4
		n.Data = bytes[index : index+int(n.DataSize)]
		index = index + int(n.DataSize)
		n.Flags = bytes[index]
		index = index + 1
	}
	if index < lenBytes && n.HasName() {
		n.NameSize = uint8(bytes[index])
		index = index + 1
		n.Name = bytes[index : index+int(n.NameSize)]
		index = index + int(n.NameSize)
	}
	if index < lenBytes && n.HasMime() {
		n.MimeSize = uint8(bytes[index])
		index = index + 1
		n.Mime = bytes[index : index+int(n.MimeSize)]
		index = index + int(n.MimeSize)
	}
	if index < lenBytes && n.HasLastModifiedDate() {
		n.LastModified = util.BytesToUint64(bytes[index : index+LastModifiedBytesLength])
		index = index + LastModifiedBytesLength
	}
	if index < lenBytes && n.HasTtl() {
		n.Ttl = LoadTTLFromBytes(bytes[index : index+TtlBytesLength])
		index = index + TtlBytesLength
	}
}
Exemplo n.º 4
0
func (n *Needle) Read(r *os.File, offset int64, size uint32, version Version) (ret int, err error) {
	switch version {
	case Version1:
		bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
		if ret, err = r.ReadAt(bytes, offset); err != nil {
			return
		}
		n.readNeedleHeader(bytes)
		n.Data = bytes[NeedleHeaderSize : NeedleHeaderSize+size]
		checksum := util.BytesToUint32(bytes[NeedleHeaderSize+size : NeedleHeaderSize+size+NeedleChecksumSize])
		newChecksum := NewCRC(n.Data)
		if checksum != newChecksum.Value() {
			return 0, errors.New("CRC error! Data On Disk Corrupted!")
		}
		n.Checksum = newChecksum
		return
	case Version2:
		if size == 0 {
			return 0, nil
		}
		bytes := make([]byte, NeedleHeaderSize+size+NeedleChecksumSize)
		if ret, err = r.ReadAt(bytes, offset); err != nil {
			return
		}
		if ret != int(NeedleHeaderSize+size+NeedleChecksumSize) {
			return 0, errors.New("File Entry Not Found!")
		}
		n.readNeedleHeader(bytes)
		if n.Size != size {
			return 0, fmt.Errorf("File Entry Not Found! Needle %d Memory %d", n.Size, size)
		}
		n.readNeedleDataVersion2(bytes[NeedleHeaderSize : NeedleHeaderSize+int(n.Size)])
		checksum := util.BytesToUint32(bytes[NeedleHeaderSize+n.Size : NeedleHeaderSize+n.Size+NeedleChecksumSize])
		newChecksum := NewCRC(n.Data)
		if checksum != newChecksum.Value() {
			return 0, errors.New("CRC error! Data On Disk Corrupted!")
		}
		n.Checksum = newChecksum
		return
	}
	return 0, fmt.Errorf("Unsupported Version! (%d)", version)
}
Exemplo n.º 5
0
func (m cdbMap) Get(key uint64) (element *NeedleValue, ok bool) {
	var (
		data []byte
		k    []byte = make([]byte, 8)
		err  error
	)
	util.Uint64toBytes(k, key)

	//优先从c1读取,如果失败则尝试读c2,因为每个表只能存储有限数量的records
	if data, err = m.c1.Data(k); err != nil || data == nil {
		if m.c2 == nil {
			return nil, false
		}
		if data, err = m.c2.Data(k); err != nil || data == nil {
			return nil, false
		}
	}
	return &NeedleValue{Key: Key(key), Offset: util.BytesToUint32(data[:4]),
		Size: util.BytesToUint32(data[4:])}, true
}
Exemplo n.º 6
0
func ParseKeyHash(key_hash_string string) (uint64, uint32, error) {
	key_hash_bytes, khe := hex.DecodeString(key_hash_string)
	key_hash_len := len(key_hash_bytes)
	if khe != nil || key_hash_len <= 4 {
		glog.V(0).Infoln("Invalid key_hash", key_hash_string, "length:", key_hash_len, "error", khe)
		return 0, 0, errors.New("Invalid key and hash:" + key_hash_string)
	}
	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, nil
}
Exemplo n.º 7
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])
}