src/server/pkg/hashtree/db.go
func (w *Writer) Write(n *MergeNode) error {
	// Marshal node if it was merged
	if n.nodeProto != nil {
		var err error
		n.v, err = n.nodeProto.Marshal()
		if err != nil {
			return err
		}
	}
	// Get size info from root node
	if bytes.Equal(n.k, nullByte) {
		if n.nodeProto == nil {
			n.nodeProto = &NodeProto{}
			if err := n.nodeProto.Unmarshal(n.v); err != nil {
				return err
			}
		}
		w.size = uint64(n.nodeProto.SubtreeSize)
	}
	// Write index for every index size bytes
	if w.offset > uint64(len(w.idxs)+1)*IndexSize {
		w.idxs = append(w.idxs, &Index{
			K:      n.k,
			Offset: w.offset,
		})
	}
	b, err := w.pbw.WriteBytes(n.k)
	if err != nil {
		return err
	}
	w.offset += uint64(b)
	b, err = w.pbw.WriteBytes(n.v)
	if err != nil {
		return err
	}
	w.offset += uint64(b)
	return nil
}
func (w *Writer) Write(n *MergeNode) error {
	// Marshal node if it was merged
	if n.nodeProto != nil {
		var err error
		n.v, err = n.nodeProto.Marshal()
		if err != nil {
			return err
		}
	}
	// Get size info from root node
	if bytes.Equal(n.k, nullByte) {
		if n.nodeProto == nil {
			n.nodeProto = &NodeProto{}
			if err := n.nodeProto.Unmarshal(n.v); err != nil {
				return err
			}
		}
		w.size = uint64(n.nodeProto.SubtreeSize)
	}
	// Write index for every index size bytes
	if w.offset > uint64(len(w.idxs)+1)*IndexSize {
		w.idxs = append(w.idxs, &Index{
			K:      n.k,
			Offset: w.offset,
		})
	}
	b, err := w.pbw.WriteBytes(n.k)
	if err != nil {
		return err
	}
	w.offset += uint64(b)
	b, err = w.pbw.WriteBytes(n.v)
	if err != nil {
		return err
	}
	w.offset += uint64(b)
	return nil
}
func ( w * Writer ) Write ( n * MergeNode ) error { // Marshal node if it was merged if n . nodeProto != nil { var err error n . v , err = n . nodeProto . Marshal ( ) if err != nil { return err } } // Get size info from root node if bytes . Equal ( n . k , nullByte ) { if n . nodeProto == nil { n . nodeProto = & NodeProto { } if err := n . nodeProto . Unmarshal ( n . v ) ; err != nil { return err } } w . size = uint64 ( n . nodeProto . SubtreeSize ) } // Write index for every index size bytes if w . offset > uint64 ( len ( w . idxs ) + 1 ) * IndexSize { w . idxs = append ( w . idxs , & Index { K : n . k , Offset : w . offset , } ) } b , err := w . pbw . WriteBytes ( n . k ) if err != nil { return err } w . offset += uint64 ( b ) b , err = w . pbw . WriteBytes ( n . v ) if err != nil { return err } w . offset += uint64 ( b ) return nil }
// Write writes the next merge node.
Write writes the next merge node .