예제 #1
0
파일: node.go 프로젝트: baeeq/god
func newNode(segment []Nibble, byteValue []byte, treeValue *Tree, timestamp int64, empty bool, use int) *node {
	return &node{
		segment:   segment,
		byteValue: byteValue,
		byteHash:  murmur.HashBytes(byteValue),
		treeValue: treeValue,
		timestamp: timestamp,
		hash:      make([]byte, murmur.Size),
		children:  make([]*node, 1<<(8/parts)),
		empty:     empty,
		use:       use,
	}
}
예제 #2
0
파일: node.go 프로젝트: baeeq/god
// del will return this node or a child replacement after removing the value type defined by use (byteValue and/or treeValue).
func (self *node) del(prefix, segment []Nibble, use int, now int64) (result *node, oldBytes []byte, oldTree *Tree, timestamp int64, existed int) {
	if self == nil {
		return
	}
	beyond_segment := false
	beyond_self := false
	for i := 0; ; i++ {
		beyond_segment = i >= len(segment)
		beyond_self = i >= len(self.segment)
		if beyond_segment && beyond_self {
			if self.use&^use != 0 {
				if self.use&use&byteValue != 0 {
					oldBytes = self.byteValue
					existed |= byteValue
					self.byteValue, self.byteHash, self.use = nil, murmur.HashBytes(nil), self.use&^byteValue
				}
				if self.use&use&treeValue != 0 {
					oldTree = self.treeValue
					existed |= treeValue
					self.treeValue, self.use = nil, self.use&^treeValue
				}
				result, timestamp = self, self.timestamp
				self.rehash(append(prefix, segment...), now)
			} else {
				n_children := 0
				var a_child *node
				for _, child := range self.children {
					if child != nil {
						n_children++
						a_child = child
					}
				}
				if n_children > 1 || self.segment == nil {
					result, oldBytes, oldTree, timestamp, existed = self, self.byteValue, self.treeValue, self.timestamp, self.use
					self.byteValue, self.byteHash, self.treeValue, self.empty, self.use, self.timestamp = nil, murmur.HashBytes(nil), nil, true, 0, 0
					self.rehash(append(prefix, segment...), now)
				} else if n_children == 1 {
					a_child.setSegment(append(self.segment, a_child.segment...))
					result, oldBytes, oldTree, timestamp, existed = a_child, self.byteValue, self.treeValue, self.timestamp, self.use
				} else {
					result, oldBytes, oldTree, timestamp, existed = nil, self.byteValue, self.treeValue, self.timestamp, self.use
				}
			}
			return
		} else if beyond_segment {
			result, oldBytes, oldTree, timestamp, existed = self, nil, nil, 0, 0
			return
		} else if beyond_self {
			prefix = append(prefix, self.segment...)
			self.children[segment[i]], oldBytes, oldTree, timestamp, existed = self.children[segment[i]].del(prefix, segment[i:], use, now)
			if self.empty && prefix != nil {
				n_children := 0
				for _, child := range self.children {
					if child != nil {
						n_children++
					}
				}
				if n_children == 0 {
					result = nil
				} else {
					result = self
					self.rehash(prefix, now)
				}
			} else {
				result = self
				self.rehash(prefix, now)
			}
			return
		} else if self.segment[i] != segment[i] {
			result, oldBytes, oldTree, timestamp, existed = self, nil, nil, 0, 0
			return
		}
	}
	panic("Shouldn't happen")
}