Example #1
0
// append appends a key/value pair, which may also be a restart point.
func (w *Writer) append(key, value []byte, restart bool) {
	nShared := 0
	if restart {
		w.restarts = append(w.restarts, uint32(w.buf.Len()))
	} else {
		nShared = db.SharedPrefixLen(w.prevKey, key)
	}
	w.prevKey = append(w.prevKey[:0], key...)
	w.nEntries++
	n := binary.PutUvarint(w.tmp[0:], uint64(nShared))
	n += binary.PutUvarint(w.tmp[n:], uint64(len(key)-nShared))
	n += binary.PutUvarint(w.tmp[n:], uint64(len(value)))
	w.buf.Write(w.tmp[:n])
	w.buf.Write(key[nShared:])
	w.buf.Write(value)
}
Example #2
0
// append appends a key/value pair, which may also be a restart point.
func (w *Writer) append(key, value []byte, restart bool) {
	nShared := 0
	if restart {
		// 记录重启点
		w.restarts = append(w.restarts, uint32(w.buf.Len()))
	} else {
		// 当前key与前一个key可以共享存储的部分的长度
		nShared = db.SharedPrefixLen(w.prevKey, key)
	}
	w.prevKey = append(w.prevKey[:0], key...)
	w.nEntries++
	// 先存入共享的部分的长度
	n := binary.PutUvarint(w.tmp[0:], uint64(nShared))
	// 然后记录共享部分之外当前key的剩余部分
	n += binary.PutUvarint(w.tmp[n:], uint64(len(key)-nShared))
	// 存入value的长度
	n += binary.PutUvarint(w.tmp[n:], uint64(len(value)))
	w.buf.Write(w.tmp[:n])
	// 写入非共享部分的key值
	w.buf.Write(key[nShared:])
	// 写入value
	w.buf.Write(value)
}