paths.go
func SeekPathConflict(c *bolt.Cursor, path []byte) ([]byte, []byte) {
	// Validation
	if len(path) == 0 {
		return nil, nil
	}
	if path[0] != '/' {
		return nil, nil
	}

	// Fast-path for exact and prefix match.
	if k, v := c.Seek(path); bytes.Equal(k, path) {
		return k, v
	} else if bytes.HasPrefix(k, path) {
		// Any prefixed k is good enough when path ends in '/'.
		if path[len(path)-1] == '/' {
			return nil, nil
		}

		// If k's last element is longer it could be a conflict.
		if k[len(path)] == '/' {
			return nil, nil
		}
	}

	// Prefix scan.
	i := 0
	for {
		i++

		// Match slash.
		prefix := path[:i]
		k, v := c.Seek(prefix)
		if !bytes.HasPrefix(k, prefix) {
			return nil, nil
		}

		// Exact match is a conflict for trailing slash.
		if i == len(path) {
			if len(k) == len(path) {
				return k, v
			}
			return nil, nil
		}

		// Advance cursor past exact match to first prefix match.
		if len(k) == len(prefix) {
			k, v = c.Next()
			if !bytes.HasPrefix(k, prefix) {
				return nil, nil
			}
		}

		// Find end of element.
		offset := bytes.IndexByte(path[i:], '/')
		last := offset < 0
		if last {
			i = len(path)
		} else {
			i += offset
		}

		switch k[len(prefix)] {
		case '*':
			return k, v

		case ':':
			// Find end of element.
			kPrefix := k
			offset := bytes.IndexByte(k[len(prefix):], '/')
			if offset > 0 {
				kPrefix = k[:len(prefix)+offset]
			}

			// Exact match required through variable element.
			prefix = path[:i]
			if !bytes.Equal(prefix, kPrefix) {
				return k, v
			}

			if last {
				// Exact match is a conflict for the last element.
				if k, v = c.Seek(prefix); bytes.Equal(k, prefix) {
					return k, v
				}
				return nil, nil
			}

		default:
			// Static (non-variable) element required.
			next := path[len(prefix)]
			if next == ':' || next == '*' {
				return k, v
			}

			prefix = path[:i]
			k, v = c.Seek(prefix)

			if last {
				// Exact match is a conflict for the last element.
				if bytes.Equal(k, prefix) {
					return k, v
				}
				return nil, nil
			}

			if !bytes.HasPrefix(k, prefix) {
				return nil, nil
			}
		}
	}
}
func SeekPathConflict(c *bolt.Cursor, path []byte) ([]byte, []byte) {
	// Validation
	if len(path) == 0 {
		return nil, nil
	}
	if path[0] != '/' {
		return nil, nil
	}

	// Fast-path for exact and prefix match.
	if k, v := c.Seek(path); bytes.Equal(k, path) {
		return k, v
	} else if bytes.HasPrefix(k, path) {
		// Any prefixed k is good enough when path ends in '/'.
		if path[len(path)-1] == '/' {
			return nil, nil
		}

		// If k's last element is longer it could be a conflict.
		if k[len(path)] == '/' {
			return nil, nil
		}
	}

	// Prefix scan.
	i := 0
	for {
		i++

		// Match slash.
		prefix := path[:i]
		k, v := c.Seek(prefix)
		if !bytes.HasPrefix(k, prefix) {
			return nil, nil
		}

		// Exact match is a conflict for trailing slash.
		if i == len(path) {
			if len(k) == len(path) {
				return k, v
			}
			return nil, nil
		}

		// Advance cursor past exact match to first prefix match.
		if len(k) == len(prefix) {
			k, v = c.Next()
			if !bytes.HasPrefix(k, prefix) {
				return nil, nil
			}
		}

		// Find end of element.
		offset := bytes.IndexByte(path[i:], '/')
		last := offset < 0
		if last {
			i = len(path)
		} else {
			i += offset
		}

		switch k[len(prefix)] {
		case '*':
			return k, v

		case ':':
			// Find end of element.
			kPrefix := k
			offset := bytes.IndexByte(k[len(prefix):], '/')
			if offset > 0 {
				kPrefix = k[:len(prefix)+offset]
			}

			// Exact match required through variable element.
			prefix = path[:i]
			if !bytes.Equal(prefix, kPrefix) {
				return k, v
			}

			if last {
				// Exact match is a conflict for the last element.
				if k, v = c.Seek(prefix); bytes.Equal(k, prefix) {
					return k, v
				}
				return nil, nil
			}

		default:
			// Static (non-variable) element required.
			next := path[len(prefix)]
			if next == ':' || next == '*' {
				return k, v
			}

			prefix = path[:i]
			k, v = c.Seek(prefix)

			if last {
				// Exact match is a conflict for the last element.
				if bytes.Equal(k, prefix) {
					return k, v
				}
				return nil, nil
			}

			if !bytes.HasPrefix(k, prefix) {
				return nil, nil
			}
		}
	}
}
// SeekPathConflict seeks an entry which conflicts with `path`, and returns the first encountered or `nil, nil` if none
// is found.