2017-08-14 23:36:22 +05:30
|
|
|
package db
|
2017-06-30 21:10:25 +05:30
|
|
|
|
|
|
|
import (
|
2017-08-10 05:44:28 +05:30
|
|
|
"bytes"
|
|
|
|
"compress/zlib"
|
2017-06-30 21:10:25 +05:30
|
|
|
"encoding/json"
|
2017-08-14 23:36:22 +05:30
|
|
|
"fmt"
|
2017-08-10 05:44:28 +05:30
|
|
|
"io"
|
2017-06-30 21:10:25 +05:30
|
|
|
"log"
|
2017-08-10 05:44:28 +05:30
|
|
|
"strings"
|
2017-08-15 03:33:02 +05:30
|
|
|
"time"
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-10 05:44:28 +05:30
|
|
|
"github.com/mediocregopher/radix.v2/pool"
|
2017-06-30 21:10:25 +05:30
|
|
|
"github.com/mediocregopher/radix.v2/redis"
|
|
|
|
"github.com/mediocregopher/radix.v2/util"
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2018-02-16 02:43:57 +05:30
|
|
|
"github.com/elyby/chrly/interfaces"
|
|
|
|
"github.com/elyby/chrly/model"
|
2017-06-30 21:10:25 +05:30
|
|
|
)
|
|
|
|
|
2017-08-14 23:36:22 +05:30
|
|
|
type RedisFactory struct {
|
|
|
|
Host string
|
|
|
|
Port int
|
|
|
|
PoolSize int
|
2018-01-23 21:13:37 +05:30
|
|
|
connection *pool.Pool
|
2017-08-10 05:44:28 +05:30
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
// TODO: maybe we should manually return connection to the pool?
|
|
|
|
|
2017-08-18 03:20:23 +05:30
|
|
|
func (f RedisFactory) CreateSkinsRepository() (interfaces.SkinsRepository, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
connection, err := f.getConnection()
|
2017-08-10 05:44:28 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-14 23:36:22 +05:30
|
|
|
return &redisDb{connection}, nil
|
|
|
|
}
|
|
|
|
|
2017-08-18 03:20:23 +05:30
|
|
|
func (f RedisFactory) CreateCapesRepository() (interfaces.CapesRepository, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
panic("capes repository not supported for this storage type")
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func (f RedisFactory) getConnection() (*pool.Pool, error) {
|
2017-08-14 23:36:22 +05:30
|
|
|
if f.connection == nil {
|
|
|
|
if f.Host == "" {
|
|
|
|
return nil, &ParamRequired{"host"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if f.Port == 0 {
|
|
|
|
return nil, &ParamRequired{"port"}
|
|
|
|
}
|
|
|
|
|
|
|
|
addr := fmt.Sprintf("%s:%d", f.Host, f.Port)
|
2018-01-23 21:13:37 +05:30
|
|
|
conn, err := pool.New("tcp", addr, f.PoolSize)
|
2017-08-14 23:36:22 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
f.connection = conn
|
2017-08-15 03:33:02 +05:30
|
|
|
|
|
|
|
go func() {
|
|
|
|
period := 5
|
|
|
|
for {
|
|
|
|
time.Sleep(time.Duration(period) * time.Second)
|
|
|
|
resp := f.connection.Cmd("PING")
|
|
|
|
if resp.Err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Redis not pinged. Try to reconnect")
|
2018-01-23 21:13:37 +05:30
|
|
|
conn, err := pool.New("tcp", addr, f.PoolSize)
|
2017-08-15 03:33:02 +05:30
|
|
|
if err != nil {
|
|
|
|
log.Printf("Cannot reconnect to redis: %v\n", err)
|
|
|
|
log.Printf("Waiting %d seconds to retry\n", period)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
f.connection = conn
|
|
|
|
log.Println("Reconnected")
|
|
|
|
}
|
|
|
|
}()
|
2017-08-14 23:36:22 +05:30
|
|
|
}
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2017-08-14 23:36:22 +05:30
|
|
|
return f.connection, nil
|
2017-08-10 05:44:28 +05:30
|
|
|
}
|
|
|
|
|
2017-06-30 21:10:25 +05:30
|
|
|
type redisDb struct {
|
2018-01-23 21:13:37 +05:30
|
|
|
conn *pool.Pool
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
const accountIdToUsernameKey = "hash:username-to-account-id"
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
func (db *redisDb) FindByUsername(username string) (*model.Skin, error) {
|
2018-01-23 21:13:37 +05:30
|
|
|
return findByUsername(username, db.getConn())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *redisDb) FindByUserId(id int) (*model.Skin, error) {
|
|
|
|
return findByUserId(id, db.getConn())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *redisDb) Save(skin *model.Skin) error {
|
|
|
|
return save(skin, db.getConn())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *redisDb) RemoveByUserId(id int) error {
|
|
|
|
return removeByUserId(id, db.getConn())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *redisDb) RemoveByUsername(username string) error {
|
|
|
|
return removeByUsername(username, db.getConn())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *redisDb) getConn() util.Cmder {
|
|
|
|
conn, _ := db.conn.Get()
|
|
|
|
return conn
|
|
|
|
}
|
|
|
|
|
|
|
|
func findByUsername(username string, conn util.Cmder) (*model.Skin, error) {
|
2017-08-09 21:41:53 +05:30
|
|
|
if username == "" {
|
2017-08-17 05:17:35 +05:30
|
|
|
return nil, &SkinNotFoundError{username}
|
2017-08-09 21:41:53 +05:30
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
redisKey := buildUsernameKey(username)
|
|
|
|
response := conn.Cmd("GET", redisKey)
|
2017-06-30 21:10:25 +05:30
|
|
|
if response.IsType(redis.Nil) {
|
2017-08-17 05:17:35 +05:30
|
|
|
return nil, &SkinNotFoundError{username}
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
encodedResult, err := response.Bytes()
|
2017-08-17 05:17:35 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
result, err := zlibDecode(encodedResult)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-30 21:10:25 +05:30
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
var skin *model.Skin
|
|
|
|
err = json.Unmarshal(result, &skin)
|
|
|
|
if err != nil {
|
2017-09-29 16:29:29 +05:30
|
|
|
return nil, err
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2017-08-17 05:17:35 +05:30
|
|
|
skin.OldUsername = skin.Username
|
|
|
|
|
|
|
|
return skin, nil
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func findByUserId(id int, conn util.Cmder) (*model.Skin, error) {
|
|
|
|
response := conn.Cmd("HGET", accountIdToUsernameKey, id)
|
2017-06-30 21:10:25 +05:30
|
|
|
if response.IsType(redis.Nil) {
|
2017-09-11 19:19:08 +05:30
|
|
|
return nil, &SkinNotFoundError{"unknown"}
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
username, _ := response.Str()
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
return findByUsername(username, conn)
|
2017-06-30 21:10:25 +05:30
|
|
|
}
|
2017-08-10 05:44:28 +05:30
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func removeByUserId(id int, conn util.Cmder) error {
|
|
|
|
record, err := findByUserId(id, conn)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*SkinNotFoundError); !ok {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
conn.Cmd("MULTI")
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
conn.Cmd("HDEL", accountIdToUsernameKey, id)
|
|
|
|
if record != nil {
|
|
|
|
conn.Cmd("DEL", buildUsernameKey(record.Username))
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Cmd("EXEC")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeByUsername(username string, conn util.Cmder) error {
|
|
|
|
record, err := findByUsername(username, conn)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(*SkinNotFoundError); !ok {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Cmd("MULTI")
|
|
|
|
|
|
|
|
conn.Cmd("DEL", buildUsernameKey(record.Username))
|
|
|
|
if record != nil {
|
|
|
|
conn.Cmd("HDEL", accountIdToUsernameKey, record.UserId)
|
|
|
|
}
|
|
|
|
|
|
|
|
conn.Cmd("EXEC")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func save(skin *model.Skin, conn util.Cmder) error {
|
|
|
|
conn.Cmd("MULTI")
|
|
|
|
|
|
|
|
// If user has changed username, then we must delete his old username record
|
2017-08-17 05:17:35 +05:30
|
|
|
if skin.OldUsername != "" && skin.OldUsername != skin.Username {
|
2018-01-23 21:13:37 +05:30
|
|
|
conn.Cmd("DEL", buildUsernameKey(skin.OldUsername))
|
2017-08-17 05:17:35 +05:30
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
// If this is a new record or if the user has changed username, we set the value in the hash table
|
2017-08-17 05:17:35 +05:30
|
|
|
if skin.OldUsername != "" || skin.OldUsername != skin.Username {
|
|
|
|
conn.Cmd("HSET", accountIdToUsernameKey, skin.UserId, skin.Username)
|
|
|
|
}
|
|
|
|
|
|
|
|
str, _ := json.Marshal(skin)
|
2018-01-23 21:13:37 +05:30
|
|
|
conn.Cmd("SET", buildUsernameKey(skin.Username), zlibEncode(str))
|
2017-08-17 05:17:35 +05:30
|
|
|
|
|
|
|
conn.Cmd("EXEC")
|
|
|
|
|
|
|
|
skin.OldUsername = skin.Username
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-23 21:13:37 +05:30
|
|
|
func buildUsernameKey(username string) string {
|
2017-08-10 05:44:28 +05:30
|
|
|
return "username:" + strings.ToLower(username)
|
|
|
|
}
|
|
|
|
|
|
|
|
func zlibEncode(str []byte) []byte {
|
|
|
|
var buff bytes.Buffer
|
|
|
|
writer := zlib.NewWriter(&buff)
|
|
|
|
writer.Write(str)
|
|
|
|
writer.Close()
|
|
|
|
|
|
|
|
return buff.Bytes()
|
|
|
|
}
|
|
|
|
|
|
|
|
func zlibDecode(bts []byte) ([]byte, error) {
|
|
|
|
buff := bytes.NewReader(bts)
|
|
|
|
reader, readError := zlib.NewReader(buff)
|
|
|
|
if readError != nil {
|
|
|
|
return nil, readError
|
|
|
|
}
|
|
|
|
|
|
|
|
resultBuffer := new(bytes.Buffer)
|
|
|
|
io.Copy(resultBuffer, reader)
|
|
|
|
reader.Close()
|
|
|
|
|
|
|
|
return resultBuffer.Bytes(), nil
|
|
|
|
}
|