diff --git a/webapp/go/isuda.go b/webapp/go/isuda.go index 9f0ac00..6f57b69 100644 --- a/webapp/go/isuda.go +++ b/webapp/go/isuda.go @@ -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 { @@ -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)) diff --git a/webapp/go/type.go b/webapp/go/type.go index 24a5835..2bf3ec0 100644 --- a/webapp/go/type.go +++ b/webapp/go/type.go @@ -13,6 +13,7 @@ type Entry struct { UpdatedAt time.Time CreatedAt time.Time KeywordLength int + KeywordHash string Html string Stars []Star @@ -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 }