disk: add generic path trie implementation

Add a simple implementation of a path trie structure that can be
used to look up assoicated data for any given path. The constructor
will build the trie from a dict of paths to associated data. Later
modification is currently not support. Add tests for it creation
and lookup.
This commit is contained in:
Christian Kellner 2022-08-06 13:33:25 +02:00
parent 1ca2efe6cf
commit 00555722b2
2 changed files with 257 additions and 0 deletions

View file

@ -0,0 +1,134 @@
package disk
import (
"testing"
"github.com/osbuild/osbuild-composer/internal/common"
"github.com/stretchr/testify/assert"
)
func TestNewPathTrieFromMap(t *testing.T) {
assert := assert.New(t)
type testCase struct {
entries map[string]interface{}
trie *PathTrie
}
tests := []testCase{
{
entries: map[string]interface{}{},
trie: &PathTrie{
Name: []string{},
},
},
{
entries: map[string]interface{}{
"/": common.IntToPtr(1),
},
trie: &PathTrie{
Name: []string{},
Payload: common.IntToPtr(1),
},
},
{
entries: map[string]interface{}{
"/": common.IntToPtr(1),
"/var": common.IntToPtr(2),
"/var/lib/chrony": common.IntToPtr(3),
"/var/lib/chrony/logs": common.IntToPtr(4),
"/var/lib/osbuild": common.IntToPtr(5),
"/var/lib/osbuild/store/cache": common.IntToPtr(6),
"/boot": common.IntToPtr(7),
"/boot/efi": common.IntToPtr(8),
},
trie: &PathTrie{
Name: []string{},
Payload: common.IntToPtr(1),
Paths: []*PathTrie{
{
Name: []string{"boot"},
Payload: common.IntToPtr(7),
Paths: []*PathTrie{
{
Name: []string{"efi"},
Payload: common.IntToPtr(8),
},
},
},
{
Name: []string{"var"},
Payload: common.IntToPtr(2),
Paths: []*PathTrie{
{
Name: []string{"lib", "chrony"},
Payload: common.IntToPtr(3),
Paths: []*PathTrie{
{
Name: []string{"logs"},
Payload: common.IntToPtr(4),
},
},
},
{
Name: []string{"lib", "osbuild"},
Payload: common.IntToPtr(5),
Paths: []*PathTrie{
{
Name: []string{"store", "cache"},
Payload: common.IntToPtr(6),
},
},
},
},
},
},
},
},
}
for _, tc := range tests {
have := NewPathTrieFromMap(tc.entries)
assert.NotNil(have)
assert.Equal(tc.trie, have)
}
}
func TestPathTrieLookup(t *testing.T) {
assert := assert.New(t)
entries := map[string]interface{}{
"/": "/",
"/boot": "/boot",
"/boot/efi": "/boot/efi",
"/var": "/var",
"/var/lib/osbuild": "/var/lib/osbuild",
"/var/lib/osbuild/store/cache": "/var/lib/osbuild/store/cache",
"/var/lib/chrony": "/var/lib/chrony",
"/var/lib/chrony/logs": "/var/lib/chrony/logs",
}
trie := NewPathTrieFromMap(entries)
testCases := map[string]string{
"/": "/",
"/srv": "/",
"/srv/data": "/",
"/boot": "/boot",
"/boot/efi": "/boot/efi",
"/boot/grub2": "/boot",
"/boot/efi/fedora": "/boot/efi",
"/var/lib/osbuild": "/var/lib/osbuild",
"/var/lib/osbuild/test": "/var/lib/osbuild",
"/var/lib/chrony": "/var/lib/chrony",
"/var/lib/chrony/test": "/var/lib/chrony",
"/var/lib/chrony/logs": "/var/lib/chrony/logs",
"/var/lib/chrony/logs/data": "/var/lib/chrony/logs",
}
for k, v := range testCases {
node, _ := trie.Lookup(k)
assert.NotNil(node)
assert.Equal(v, node.Payload, "Lookup path: '%s' (%+v)", k, node.Name)
}
}

123
internal/disk/path_trie.go Normal file
View file

@ -0,0 +1,123 @@
package disk
import (
"sort"
"strings"
)
// splits the path into its individual components. Retruns the
// empty list if the path is just the absolute root, i.e. "/".
func pathTrieSplitPath(path string) []string {
path = strings.Trim(path, "/")
if path == "" {
return []string{}
}
return strings.Split(path, "/")
}
type PathTrie struct {
Name []string
Paths []*PathTrie
Payload interface{}
}
// match checks if the given trie is a prefix of path
func (trie *PathTrie) match(path []string) bool {
if len(trie.Name) > len(path) {
return false
}
for i := range trie.Name {
if path[i] != trie.Name[i] {
return false
}
}
return true
}
func (trie *PathTrie) get(path []string) (*PathTrie, []string) {
if len(path) < 1 {
panic("programming error: expected root node")
}
var node *PathTrie
for i := range trie.Paths {
if trie.Paths[i].match(path) {
node = trie.Paths[i]
break
}
}
// no subpath match, we are the best match
if node == nil {
return trie, path
}
// node, or one of its sub-nodes, is a match
prefix := len(node.Name)
// the node is a perfect match, return it
if len(path) == prefix {
return node, nil
}
// check if any sub-path's of node match
return node.get(path[prefix:])
}
func (trie *PathTrie) add(path []string) *PathTrie {
node := &PathTrie{Name: path}
if trie.Paths == nil {
trie.Paths = make([]*PathTrie, 0, 1)
}
trie.Paths = append(trie.Paths, node)
return node
}
// Construct a new trie from a map of paths to their payloads.
// Returns the root node of the trie.
func NewPathTrieFromMap(entries map[string]interface{}) *PathTrie {
root := &PathTrie{Name: []string{}}
keys := make([]string, 0, len(entries))
for k := range entries {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
node, left := root.Lookup(k)
if len(left) > 0 {
node = node.add(left)
}
node.Payload = entries[k]
}
return root
}
// Lookup returns the node that is the prefix of path and
// the unmatched path segment. Must be called on the root
// trie node.
func (root *PathTrie) Lookup(path string) (*PathTrie, []string) {
if len(root.Name) != 0 {
panic("programming error: lookup on non-root trie node")
}
elements := pathTrieSplitPath(path)
if len(elements) == 0 {
return root, elements
}
return root.get(elements)
}