better architecture for file system

This commit is contained in:
Lorenz Hohermuth 2024-05-14 14:13:59 +02:00
parent 4f43d824a6
commit cca5052df8
3 changed files with 40 additions and 14 deletions

View File

@ -20,14 +20,15 @@ var registryCmd = &cobra.Command{
Long: `Distributes tokens from a file or a direcotry
into a HashMap and prints the Map`,
Run: func(cmd *cobra.Command, args []string) {
tokens := splitter.SplitFile(args[0])
entrys := splitter.SplitFile(args[0])
tr := registry.TokenRegistry{ Map: make(map[string]int64)}
tr.DistributeTokens(tokens)
tr.DistributeTokens(entrys.CreateMatrix())
tw := new(tabwriter.Writer)
tw.Init(os.Stdout, 0, 8, 0, '\t', 0)
for v, k := range tr.Map {
fmt.Fprintf(tw, "value: \"%s\" \t key: %b \n", v, k)
fmt.Fprintf(tw, "value: \"%s\" \t key: %d \n", v, k)
}
tw.Flush()
},

View File

@ -0,0 +1,20 @@
package splitter
type RegistryEntry struct {
Name string
ChildEntrys []RegistryEntry
Tokens []string
}
func (re RegistryEntry) CreateMatrix() [][]string {
if re.ChildEntrys != nil {
mat := [][]string{}
for _, child := range re.ChildEntrys {
mat = addToMatrix(mat, child.CreateMatrix())
}
return mat
} else {
return [][]string{re.Tokens}
}
}

View File

@ -6,10 +6,7 @@ import (
"strings"
)
func SplitFile(path string) [][]string {
if path == "" {
return make([][]string, 0)
}
func SplitFile(path string) RegistryEntry {
info, err := os.Stat(path)
if err != nil {
panic(err)
@ -17,19 +14,27 @@ func SplitFile(path string) [][]string {
if info.IsDir() {
paths := getFilePathsFromDir(path)
matrix := make([][]string, len(paths))
arr := make([]RegistryEntry, len(paths))
for _, filePath := range paths {
matrix = addToMatrix(matrix, SplitFile(filePath))
if filePath != "" {
arr = append(arr, SplitFile(filePath))
}
}
return RegistryEntry{
Name: info.Name(),
Tokens: nil,
ChildEntrys: arr,
}
return matrix
} else {
matrix := make([][]string, 1)
matrix[0] = tokenize(getFileContent(path))
return matrix
return RegistryEntry{
Name: info.Name(),
Tokens: tokenize(getFileContent(path)),
ChildEntrys: nil,
}
}
}
func getFileContent(path string) string{
data, err := os.ReadFile(path)
if err != nil {