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

Use keyword hash #1

Open
wants to merge 5 commits 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
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