首页

    博客标题搜索-更新

    标签:algorithm,go,blog

    博客开始支持标题搜索中说到两种方式(redis sorted set和trie)保存前缀,并提供前缀搜索.那么如果要更新,就只有删除之前保存的所有数据

    redis sorted set

    • 将title和path的映射全部取出
    • 对每个title调用Convert方法,生成前缀slice
    • 对slice里每个前缀ZREM,从sorted set移除
    • 删除title和path的映射
    func deleteZset() {
        ...
        resp := redisClient.Cmd("HGETALL", "title_path")
        elems, _ := resp.Array()
    
        var title_path_ikeys []interface{} = make([]interface{}, len(elems)+1)
        title_path_ikeys[0] = "title_path"
    
        for i, el := range elems {
            title, _ := el.Str()
            title_path_ikeys[i+1] = title
            prefixList := Convert(title)
            for _, key := range prefixList {
                redisClient.PipeAppend("ZREM", key, title)
            }
        }
    
        redisClient.PipeAppend("HDEL", title_path_ikeys...)
        redisClient.PipeResp()
    }
    

    trie

    删除过程类似于dfs的后序遍历,删除hash中所有指向的节点后,删除该节点

    func DeleteAll(root string) {
        ...
        var dfs func(head string, str string)
        dfs = func(head string, str string) {
            keys := []string{}
            resp, i := redisClient.Cmd("HGETALL", head), 0
            elems, _ := resp.Array()
            for i < len(elems) {
                key, _ := elems[i].Str()
                val, _ := elems[i+1].Str()
                keys = append(keys, key)
                if key == "end" {
                    if val == "1" {
                        i += 2
                    }
                } else {
                    ch, _ := elems[i].Int()
                    dfs(val, str+string(ch))
                }
                i += 2
            }
    
            var ikeys []interface{} = make([]interface{}, len(keys)+1)
            ikeys[0] = head
            for i, d := range keys {
                ikeys[i+1] = d
            }
    
            redisClient.PipeAppend("HDEL", ikeys...)
        }
        dfs(root, "")
    
        redisClient.PipeResp()
        redisClient.Cmd("DEL", "root")
    }
    

    不定期更新