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

Shakesearch submission #35

Closed
wants to merge 1 commit into from
Closed
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
35 changes: 33 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"log"
"net/http"
"os"
"strconv"
"strings"
)

func main() {
Expand Down Expand Up @@ -48,10 +50,38 @@ func handleSearch(searcher Searcher) func(w http.ResponseWriter, r *http.Request
w.Write([]byte("missing search query in URL params"))
return
}
page, ok := r.URL.Query()["p"]
if !ok || len(page[0]) < 1 {
page = []string{"1"}
}

pageNum, err := strconv.ParseInt(page[0], 10, 64)
if err != nil || pageNum < 1 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("invalid page number"))
return
}
pageIdx := pageNum - 1
pageSize := 20
results := searcher.Search(query[0])

start := int(pageIdx) * pageSize
end := (int(pageIdx) + 1) * pageSize

// Check the bounds before slicing
if start > len(results) {
start = len(results)
}
if end > len(results) {
end = len(results)
}

// Slice the results
results = results[start:end]

buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
err := enc.Encode(results)
err = enc.Encode(results)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("encoding failure"))
Expand All @@ -68,11 +98,12 @@ func (s *Searcher) Load(filename string) error {
return fmt.Errorf("Load: %w", err)
}
s.CompleteWorks = string(dat)
s.SuffixArray = suffixarray.New(dat)
s.SuffixArray = suffixarray.New([]byte(strings.ToLower(s.CompleteWorks)))
return nil
}

func (s *Searcher) Search(query string) []string {
query = strings.ToLower(query)
idxs := s.SuffixArray.Lookup([]byte(query), -1)
results := []string{}
for _, idx := range idxs {
Expand Down
24 changes: 22 additions & 2 deletions static/app.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
const Controller = {
currentPage: 1,
search: (ev) => {
ev.preventDefault();
Controller.currentPage = 1;
const form = document.getElementById("form");
const data = Object.fromEntries(new FormData(form));
const response = fetch(`/search?q=${data.query}`).then((response) => {
fetch(`/search?q=${data.query}`).then((response) => {
response.json().then((results) => {
Controller.updateTable(results);
});
});
},

loadMore: (ev) => {
ev.preventDefault();
Controller.currentPage = Controller.currentPage + 1;
const searchQuery = document.getElementById("query").value;
fetch(`/search?q=${searchQuery}&p=${Controller.currentPage}`).then((response) => {
response.json().then((results) => {
Controller.updateTable(results);
});
Expand All @@ -16,9 +29,16 @@ const Controller = {
for (let result of results) {
rows.push(`<tr><td>${result}</td></tr>`);
}
table.innerHTML = rows;
if (Controller.currentPage > 1) {
table.innerHTML = table.innerHTML + rows.join("");
} else {
table.innerHTML = rows.join("");
}
},
};

const form = document.getElementById("form");
form.addEventListener("submit", Controller.search);

const loadMore = document.getElementById("load-more");
loadMore.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