Skip to content

Commit

Permalink
sort key
Browse files Browse the repository at this point in the history
  • Loading branch information
padraicbc committed Apr 7, 2021
1 parent 8c4e26e commit a588461
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 75 deletions.
10 changes: 7 additions & 3 deletions cmd/build/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ var (
func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string,
componentStr string, destFile string, stylePath string, tempBuildDir string) error {

layoutFD := common.GetOrSet(layoutPath)
var layoutFD *common.FData

// will break router if no layout check and can't use HasPrefix as themes breaks that
if common.UseMemFS {
layoutFD = common.GetOrSet(layoutPath, "")
if strings.Contains(layoutPath, "layouts/") {
// if hash is the same skip. common.Get(layoutPath).Hash will be 0 initially
if layoutFD.Hash > 0 && layoutFD.Hash == common.CRC32Hasher([]byte(componentStr)) {
Expand Down Expand Up @@ -270,7 +271,10 @@ func compileSvelte(ctx *v8go.Context, SSRctx *v8go.Context, layoutPath string,
if err != nil {
return fmt.Errorf("Could not add SSR Component for %s: %w%s\n", layoutPath, err, common.Caller())
}
// again store for no change
layoutFD.SSR = []byte(ssrStr)

if common.UseMemFS {
// again store for no change
layoutFD.SSR = []byte(ssrStr)
}
return nil
}
76 changes: 38 additions & 38 deletions common/mapfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package common
import (
"fmt"
"hash/crc32"
"log"
"path/filepath"
"sort"
"strings"
Expand Down Expand Up @@ -108,9 +107,13 @@ func Exists(k string) bool {
}

// GetOrSet wil return existing or new "empty" FData. Used for the layouts now.
func GetOrSet(k string) *FData {
func GetOrSet(k, sourceFile string) *FData {
mapFS.mu.Lock()
defer mapFS.mu.Unlock()
// if there is a source file then add the mapping.
if sourceFile != "" {
mapFS.sourceToDest[sourceFile] = k
}
return getOrSet(k, mapFS.fs)
}

Expand All @@ -122,6 +125,7 @@ func getOrSet(k string, fs map[string]*FData) *FData {
}
d := &FData{}
fs[clean] = d
setEntry(clean, mapFS.entries)

return d
}
Expand Down Expand Up @@ -160,34 +164,31 @@ func BinSearchIndex(x string) int {

func binSearchIndex(x string, a []string) int {
return sort.Search(len(a), func(i int) bool {
return x <= a[i]
})
}

// lowest order by path and highest order of that dir wins
func sortByDir(eles []string) {
if len(eles) == 1 {
return
}
sort.SliceStable(eles, func(i, j int) bool {
// keeps the dir files together
a, b := eles[i], eles[j]
cnta, cntb := strings.Count(a, "/"), strings.Count(b, "/")
// if the same keep the last one which is what happens in findFile regular readdir....
if cnta == cntb {
// make same dir files come last
if filepath.Dir(a) == filepath.Dir(b) {
return a > b
}
return a < b
}
// else use the count of /
return cnta < cntb
// Not a great way to check but we will only pass a dir in from GoPack when looking for a .js|.mjs file
// todo: use a flag. Could also store info but 99.9% we just have and care about files..

ad, af := x, ""
if filepath.Ext(x) != "" {
ad, af = filepath.Split(x)
}
bd, bf := filepath.Split(a[i])
// if we stored dirs this would also be requiredbut we don't...
// if filepath.Ext(a[i]) == "" {
// bd, bf = a[i], ""
// }

// we want <= when we have case of the same file exactly, i.e searching for particular exisiting.
// < would go right of.
if ad == bd {
return af <= bf
}
// again want start of dir entries.
return ad <= bd
})
}

// adds the element and keeps sorted, O(log n) where to place and constant time insert bar we hit cap and resize?
// adds the element and keeps sorted, O(log n). O(n) in worst case for copy((*entries)[i+1:], (*entries)[i:])?
// https://github.com/golang/go/wiki/SliceTricks
func setEntry(x string, entries *[]string) {
if len(*entries) == 0 {
Expand Down Expand Up @@ -239,7 +240,6 @@ func startFrom(x string, entries *[]string) <-chan string {

func deleteEntry(x string, entries *[]string) {
i := getEntryIndex(x, entries)
log.Println(i, x)
if i != -1 && (*entries)[i] == x {
*entries = append((*entries)[:i], (*entries)[i+1:]...)
}
Expand All @@ -251,27 +251,27 @@ func SearchPath(path string) (string, error) {
return searchPath(path, mapFS.entries)
}

// pulls highest ordered file from same dir or higesht ordered from a sub
func searchPath(path string, entries *[]string) (string, error) {

out := []string{}
var foundFile string
// todo: store last file flag so we know we have left dir which would remove some checks
for entry := range startFrom(path, entries) {
// another dir/path beyond what our current "dir(s)" of interest
if !(strings.HasPrefix(entry, path)) {
// Either another dir/path beyond what our current "dir(s)" of interest
// if we are in next dir or a subdir and already fond a match then stop.
// continuing with a match is like os.Walk through subdirs.
if !strings.HasPrefix(entry, path) || (foundFile != "" && filepath.Dir(entry) != path) {
break
}

if strings.HasSuffix(entry, ".js") || strings.HasSuffix(entry, ".mjs") {
out = append(out, entry)
foundFile = entry

}

}

if len(out) > 0 {

// This can break as can the ReadDir logic in findJSFile.
// Need set more logic, maybe hierarchical order like path/index.js/path/index.mjs ...
sortByDir(out)
return out[0], nil
if foundFile == "" {
return "", fmt.Errorf("Could not find file %s%s\n", path, Caller())
}
return "", fmt.Errorf("Could not find file %s%s\n", path, Caller())
return foundFile, nil
}
88 changes: 54 additions & 34 deletions common/mapfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,55 @@ package common

import (
"fmt"
"math/rand"
"testing"
)

var unsort = []string{
"public/spa/web_modules/svelte/index.mjs",
"public/spa/web_modules/svelte/internal/index.mjs",
"public/spa/web_modules/svelte/transition/index.mjs",
"public/spa/web_modules/svelte/easing/index.mjs",
"public/spa/web_modules/svelte/store/index.mjs",
"public/spa/web_modules/regexparam/dist/regexparam.mjs",
"public/spa/web_modules/svelte/animate/index.mjs",
"public/spa/web_modules/navaid/dist/navaid.mjs",
"public/spa/web_modules/svelte/compiler.mjs",
func unsortSli() []string {
out := []string{}
for _, s := range sorted {
out = append(out, s)
}
rand.Shuffle(len(out), func(i, j int) {
out[i], out[j] = out[j], out[i]
})
return out
}

var sorted = []string{
"public/spa/web_modules/navaid/dist/navaid.js",
"public/spa/web_modules/navaid/dist/navaid.min.js",
"public/spa/web_modules/navaid/dist/navaid.mjs",
"public/spa/web_modules/regexparam/dist/regexparam.js",
"public/spa/web_modules/regexparam/dist/regexparam.min.js",
"public/spa/web_modules/regexparam/dist/regexparam.mjs",
"public/spa/web_modules/svelte/animate/index.mjs",
"public/spa/web_modules/svelte/compiler.js",
"public/spa/web_modules/svelte/compiler.mjs",
"public/spa/web_modules/svelte/easing/index.mjs",
"public/spa/web_modules/svelte/index.js",
"public/spa/web_modules/svelte/index.mjs",
"public/spa/web_modules/svelte/register.js",
"public/spa/web_modules/svelte/animate/index.js",
"public/spa/web_modules/svelte/animate/index.mjs",
"public/spa/web_modules/svelte/easing/index.js",
"public/spa/web_modules/svelte/easing/index.mjs",
"public/spa/web_modules/svelte/internal/index.js",
"public/spa/web_modules/svelte/internal/index.mjs",
"public/spa/web_modules/svelte/motion/index.js",
"public/spa/web_modules/svelte/motion/index.mjs",
"public/spa/web_modules/svelte/store/index.js",
"public/spa/web_modules/svelte/store/index.mjs",
"public/spa/web_modules/svelte/transition/index.js",
"public/spa/web_modules/svelte/transition/index.mjs",
}

func insert() *[]string {
out := &[]string{}
for _, s := range unsortSli() {
setEntry(s, out)
}
return out

}
func Test_binSearchIndex(t *testing.T) {
type args struct {
x string
Expand Down Expand Up @@ -64,16 +86,16 @@ func Test_setEntry(t *testing.T) {
{
name: "four",
args: args{
x: "e",
x: "e.txt",
ind: 4,
entries: []string{"a", "b", "c", "d", "f", "g", "h"}},
entries: []string{"a.txt", "b.txt", "c.txt", "d.txt", "f.txt", "g.txt", "h.txt"}},
},
{
name: "seven",
args: args{
x: "i",
x: "i.txt",
ind: 7,
entries: []string{"a", "b", "c", "d", "f", "g", "h"}},
entries: []string{"a.txt", "b.txt", "c.txt", "d.txt", "f.txt", "g.txt", "h.txt"}},
},
}
for _, tt := range tests {
Expand All @@ -98,18 +120,24 @@ func Test_getEntryIndex(t *testing.T) {
}{
{
name: "zero",
args: args{x: "public/spa/web_modules/abc/one.svelte", entries: &sorted},
args: args{x: "public/spa/web_modules/aaaa.svelte", entries: &sorted},
want: 0,
},
{
name: "two",
args: args{x: "public/spa/web_modules/svelte/animate/index.mjs", entries: &sorted},
want: 2,
name: "aDir",
args: args{x: "public/spa/web_modules/svelte/animate", entries: &sorted},
want: 11,
},

{
name: "five",
args: args{x: "public/spa/web_modules/regexparam/dist/regexparam.mjs", entries: &sorted},
want: 5,
},

{
name: "minus1" + fmt.Sprint(len(sorted)-1),
args: args{x: "public/spa/web_modules/z/theend.svelte", entries: &sorted},
args: args{x: "public/spa/web_modules/z/z/z/theend.svelte", entries: &sorted},
want: -1,
},
}
Expand All @@ -124,12 +152,12 @@ func Test_getEntryIndex(t *testing.T) {

func Test_deleteEntry(t *testing.T) {

entries := []string{"bar", "foo"}
entries := []string{"bar.txt", "foo.txt"}
t.Run("deleteBar", func(t *testing.T) {
deleteEntry("bar", &entries)
deleteEntry("bar.txt", &entries)

if len(entries) != 1 || (entries[0]) != "foo" {
t.Errorf("deleteEntry() = %v, want %v", entries, "[foo]")
if len(entries) != 1 || (entries[0]) != "foo.txt" {
t.Errorf("deleteEntry() = %v, want %v", entries, "[foo.txt]")
}
})

Expand All @@ -156,17 +184,9 @@ func Test_searchPath(t *testing.T) {
}
}

func insert(s ...string) *[]string {
out := &[]string{}
for _, s := range unsort {
setEntry(s, out)
}
return out

}
func Test_isOrdered(t *testing.T) {

got := insert(unsort...)
got := insert()

for i, e := range *got {
if sorted[i] != e {
Expand Down

0 comments on commit a588461

Please sign in to comment.