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

	// Exact match fast-path
	if k, v := c.Seek(path); bytes.Equal(k, path) {
		return k, v
	}

	// Prefix scan
	prefixBuf := bytes.NewBuffer(make([]byte, 0, len(path)))
	for {
		// Match slash
		prefixBuf.WriteByte('/')
		prefix := prefixBuf.Bytes()
		k, v := c.Seek(prefix)
		if !bytes.HasPrefix(k, prefix) {
			return nil, nil
		}
		// Advance past '/'
		path = path[1:]

		// Exact match required for trailing slash.
		if len(path) == 0 {
			if len(k) == len(prefix) {
				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.
		i := bytes.IndexByte(path, '/')
		last := i < 0

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

		case ':':
			// Append variable path element to prefix
			ki := bytes.IndexByte(k[len(prefix):], '/')
			if ki < 0 {
				prefixBuf.Write(k[len(prefix):])
			} else {
				prefixBuf.Write(k[len(prefix) : len(prefix)+ki])
			}

			if last {
				// Exact match required for last element.
				prefix = prefixBuf.Bytes()
				if k, v = c.Seek(prefix); bytes.Equal(k, prefix) {
					return k, v
				}
				return nil, nil
			}

		default:
			// Append path component to prefix.
			if last {
				prefixBuf.Write(path)
			} else {
				prefixBuf.Write(path[:i])
			}

			prefix = prefixBuf.Bytes()
			k, v = c.Seek(prefix)

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

			// Prefix match required for other elements.
			if !bytes.HasPrefix(k, prefix) {
				return nil, nil
			}
		}

		// Advance past element.
		path = path[i:]
	}
}
func SeekPathMatch(c *bolt.Cursor, path []byte) ([]byte, []byte) {
	// Validation
	if len(path) == 0 {
		return nil, nil
	}
	if path[0] != '/' {
		return nil, nil
	}

	// Exact match fast-path
	if k, v := c.Seek(path); bytes.Equal(k, path) {
		return k, v
	}

	// Prefix scan
	prefixBuf := bytes.NewBuffer(make([]byte, 0, len(path)))
	for {
		// Match slash
		prefixBuf.WriteByte('/')
		prefix := prefixBuf.Bytes()
		k, v := c.Seek(prefix)
		if !bytes.HasPrefix(k, prefix) {
			return nil, nil
		}
		// Advance past '/'
		path = path[1:]

		// Exact match required for trailing slash.
		if len(path) == 0 {
			if len(k) == len(prefix) {
				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.
		i := bytes.IndexByte(path, '/')
		last := i < 0

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

		case ':':
			// Append variable path element to prefix
			ki := bytes.IndexByte(k[len(prefix):], '/')
			if ki < 0 {
				prefixBuf.Write(k[len(prefix):])
			} else {
				prefixBuf.Write(k[len(prefix) : len(prefix)+ki])
			}

			if last {
				// Exact match required for last element.
				prefix = prefixBuf.Bytes()
				if k, v = c.Seek(prefix); bytes.Equal(k, prefix) {
					return k, v
				}
				return nil, nil
			}

		default:
			// Append path component to prefix.
			if last {
				prefixBuf.Write(path)
			} else {
				prefixBuf.Write(path[:i])
			}

			prefix = prefixBuf.Bytes()
			k, v = c.Seek(prefix)

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

			// Prefix match required for other elements.
			if !bytes.HasPrefix(k, prefix) {
				return nil, nil
			}
		}

		// Advance past element.
		path = path[i:]
	}
}
// SeekPathMatch seeks an entry which matches `path`, or returns `nil, nil` when no match is found.
// Returned key may be `path`, or a matching dynamic path.
// Matches are exclusive if the set of keys are conflict free (see SeekPathConflict).