Skip to content

Commit

Permalink
Merge pull request isucon#9 from cocodrips/use-keyword-hash
Browse files Browse the repository at this point in the history
Use keyword hash
  • Loading branch information
ku-mu committed Aug 19, 2018
2 parents bf844d1 + 8fdc910 commit 0c55ca6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 23 deletions.
56 changes: 39 additions & 17 deletions webapp/go/isuda.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,36 @@ func initializeHandler(w http.ResponseWriter, r *http.Request) {
_, err := db.Exec(`DELETE FROM entry WHERE id > 7101`)
panicIf(err)

// keywordsの初期化
//replaceKeyword()

_, err = db.Exec("TRUNCATE star")
panicIf(err)

//err = updateKeywordHash()
//panicIf(err)
re.JSON(w, http.StatusOK, map[string]string{"result": "ok"})
}

func updateKeywordHash() error {
rows, err := db.Query(`SELECT id, keyword from entry`)
if err != nil && err != sql.ErrNoRows {
panicIf(err)
}

for rows.Next() {
e := Entry{}
// err := rows.Scan(&e.ID, &e.AuthorID, &e.Keyword, &e.Description, &e.UpdatedAt, &e.CreatedAt)
err := rows.Scan(&e.ID, &e.Keyword)
panicIf(err)
fmt.Println(e.Keyword)
value := fmt.Sprintf("%x", sha1.Sum([]byte(e.Keyword)))
fmt.Println(value)
_, err = db.Exec(`UPDATE entry set keyword_hash = ? where id = ?`, value, e.ID)
if err != nil {
return err
}
}
rows.Close()
return nil
}

func topHandler(w http.ResponseWriter, r *http.Request) {
// めちゃくちゃ重い
if err := setName(w, r); err != nil {
Expand Down Expand Up @@ -344,43 +365,44 @@ func keywordByKeywordDeleteHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/", http.StatusFound)
}

func getKeywords() []string {
keywords := make([]string, 0, 500)
func getKeywords() []Keywords {
keywords := make([]Keywords, 0, 500)
rows, err := db.Query(`SELECT keyword FROM entry ORDER BY keyword_length DESC`)
panicIf(err)

for rows.Next() {
s := ""
err := rows.Scan(&s)
keyword := Keywords{}
err := rows.Scan(&keyword.Keywords)
panicIf(err)
keywords = append(keywords, s)
keywords = append(keywords, keyword)
}

return keywords
}

func htmlify(w http.ResponseWriter, r *http.Request, content string, keywords []string) string {
func htmlify(w http.ResponseWriter, r *http.Request, content string, keywords []Keywords) string {
if content == "" {
return ""
}

kw2sha := make(map[string]string)
n := len(keywords)
for i := 0; i < n; i++ {
kw := keywords[n-i-1]

//n := len(keywords)
i := 0
for _, keyword := range keywords {
kw := keyword.Keywords
hasKeyword := strings.Contains(content, kw)

if !hasKeyword {
continue
}

kw2sha[kw] = "isuda_" + fmt.Sprintf("%x", sha1.Sum([]byte(kw)))
i++
kw2sha[kw] = fmt.Sprintf("isuda_%v_isuda", i)
content = strings.Replace(content, kw, kw2sha[kw], -1)
}

for i := 0; i < n; i++ {
kw := keywords[n-i-1]
for _, keyword := range keywords {
kw := keyword.Keywords
if hash, ok := kw2sha[kw]; ok {
content = strings.Replace(content, kw, kw2sha[kw], -1)
u, err := r.URL.Parse(baseUrl.String() + "/keyword/" + pathURIEscape(kw))
Expand Down
8 changes: 2 additions & 6 deletions webapp/go/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type Entry struct {
UpdatedAt time.Time
CreatedAt time.Time
KeywordLength int
KeywordHash string

Html string
Stars []Star
Expand All @@ -33,15 +34,10 @@ type Star struct {
CreatedAt time.Time `json:"created_at"`
}

type KeyWordCache struct {
Name string
Value string
update_at time.Time
}

type Keywords struct {
Keywords string
KeywordLength int
KeywordHash string
}


Expand Down

0 comments on commit 0c55ca6

Please sign in to comment.