Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed failing test cases #43

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"makefile.extensionOutputFolder": "./.vscode",
"go.testFlags": ["-v"]
}
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
)

const (
Limit = 20
)

func main() {
Expand Down Expand Up @@ -48,7 +54,9 @@ func handleSearch(searcher Searcher) func(w http.ResponseWriter, r *http.Request
w.Write([]byte("missing search query in URL params"))
return
}
results := searcher.Search(query[0])
page := page(r.URL.Query().Get("p"))

results := searcher.Search(query[0], page)
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
err := enc.Encode(results)
Expand All @@ -68,15 +76,33 @@ func (s *Searcher) Load(filename string) error {
return fmt.Errorf("Load: %w", err)
}
s.CompleteWorks = string(dat)
s.SuffixArray = suffixarray.New(dat)
lowerWorks := strings.ToLower(s.CompleteWorks)
s.SuffixArray = suffixarray.New([]byte(lowerWorks))
return nil
}

func (s *Searcher) Search(query string) []string {
func (s *Searcher) Search(query string, page int) []string {
query = strings.ToLower(query)
idxs := s.SuffixArray.Lookup([]byte(query), -1)
offset := page * Limit
if len(idxs) >= (offset + Limit) {
idxs = idxs[offset : offset+Limit]
} else if offset < len(idxs) {
idxs = idxs[offset:]
} else { // out of range, send empty
idxs = []int{}
}
results := []string{}
for _, idx := range idxs {
results = append(results, s.CompleteWorks[idx-250:idx+250])
}
return results
}

func page(s string) int {
if i, err := strconv.Atoi(s); err == nil {
return i
}
log.Printf("*> unexpected error occured when converting string %s to int on `page` sending zero\n", s)
return 0
}
25 changes: 23 additions & 2 deletions static/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
const Controller = {
search: (ev) => {
ev.preventDefault();
page = 0;
const form = document.getElementById("form");
const data = Object.fromEntries(new FormData(form));
const response = fetch(`/search?q=${data.query}`).then((response) => {
const response = fetch(`/search?q=${data.query}&p=${page}`).then((response) => {
response.json().then((results) => {
Controller.updateTable(results);
page = page + 1
});
});
},

loadmore: (ev) => {
ev.preventDefault();
const form = document.getElementById("form");
const data = Object.fromEntries(new FormData(form));
const response = fetch(`/search?q=${data.query}&p=${page}`).then((response) => {
response.json().then((results) => {
Controller.updateTable(results);
page = page + 1
});
});
},
Expand All @@ -16,9 +30,16 @@ const Controller = {
for (let result of results) {
rows.push(`<tr><td>${result}</td></tr>`);
}
table.innerHTML = rows;
if (page == 0){
table.innerHTML = rows;
} else {
table.innerHTML = table.innerHTML + rows;
}
},
};

var page = 0;
const form = document.getElementById("form");
const button = document.getElementById("load-more");
form.addEventListener("submit", Controller.search);
button.addEventListener("click", Controller.loadmore);
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<tbody id="table-body"></tbody>
</table>
</p>
<button>Load More</button>
<button id="load-more">Load More</button>
<script src="app.js"></script>
</body>

Expand Down