chrly/db/redis.go

308 lines
6.4 KiB
Go
Raw Normal View History

package db
import (
"bytes"
"compress/zlib"
"encoding/json"
"fmt"
"github.com/elyby/chrly/http"
"io"
2019-04-25 04:53:10 +05:30
"strconv"
"strings"
"time"
"github.com/mediocregopher/radix.v2/pool"
"github.com/mediocregopher/radix.v2/redis"
"github.com/mediocregopher/radix.v2/util"
"github.com/elyby/chrly/model"
"github.com/elyby/chrly/mojangtextures"
)
type RedisFactory struct {
2019-05-06 19:42:37 +05:30
Host string
Port int
PoolSize int
pool *pool.Pool
}
func (f *RedisFactory) CreateSkinsRepository() (http.SkinsRepository, error) {
2019-04-25 04:53:10 +05:30
return f.createInstance()
}
func (f *RedisFactory) CreateCapesRepository() (http.CapesRepository, error) {
2019-04-25 04:53:10 +05:30
panic("capes repository not supported for this storage type")
}
func (f *RedisFactory) CreateMojangUuidsRepository() (mojangtextures.UuidsStorage, error) {
2019-04-25 04:53:10 +05:30
return f.createInstance()
}
2019-05-06 19:42:37 +05:30
func (f *RedisFactory) createInstance() (*redisDb, error) {
p, err := f.getPool()
if err != nil {
return nil, err
}
2019-05-06 19:42:37 +05:30
return &redisDb{p}, nil
}
2019-05-06 19:42:37 +05:30
func (f *RedisFactory) getPool() (*pool.Pool, error) {
if f.pool == 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)
conn, err := pool.New("tcp", addr, f.PoolSize)
if err != nil {
return nil, err
}
2019-05-06 19:42:37 +05:30
f.pool = conn
}
2019-05-06 19:42:37 +05:30
return f.pool, nil
}
type redisDb struct {
2019-05-06 19:42:37 +05:30
pool *pool.Pool
}
const accountIdToUsernameKey = "hash:username-to-account-id"
2019-04-25 04:53:10 +05:30
const mojangUsernameToUuidKey = "hash:mojang-username-to-uuid"
func (db *redisDb) FindByUsername(username string) (*model.Skin, error) {
2019-05-06 19:42:37 +05:30
conn, err := db.pool.Get()
if err != nil {
return nil, err
}
defer db.pool.Put(conn)
return findByUsername(username, conn)
}
func (db *redisDb) FindByUserId(id int) (*model.Skin, error) {
2019-05-06 19:42:37 +05:30
conn, err := db.pool.Get()
if err != nil {
return nil, err
}
defer db.pool.Put(conn)
return findByUserId(id, conn)
}
func (db *redisDb) Save(skin *model.Skin) error {
2019-05-06 19:42:37 +05:30
conn, err := db.pool.Get()
if err != nil {
return err
}
defer db.pool.Put(conn)
return save(skin, conn)
}
func (db *redisDb) RemoveByUserId(id int) error {
2019-05-06 19:42:37 +05:30
conn, err := db.pool.Get()
if err != nil {
return err
}
defer db.pool.Put(conn)
return removeByUserId(id, conn)
}
func (db *redisDb) RemoveByUsername(username string) error {
2019-05-06 19:42:37 +05:30
conn, err := db.pool.Get()
if err != nil {
return err
}
defer db.pool.Put(conn)
return removeByUsername(username, conn)
}
2019-04-25 04:53:10 +05:30
func (db *redisDb) GetUuid(username string) (string, error) {
2019-05-06 19:42:37 +05:30
conn, err := db.pool.Get()
if err != nil {
return "", err
}
defer db.pool.Put(conn)
2019-04-25 04:53:10 +05:30
return findMojangUuidByUsername(username, conn)
2019-04-25 04:53:10 +05:30
}
2019-05-06 19:42:37 +05:30
func (db *redisDb) StoreUuid(username string, uuid string) error {
conn, err := db.pool.Get()
if err != nil {
return err
}
defer db.pool.Put(conn)
2019-05-06 19:42:37 +05:30
return storeMojangUuid(username, uuid, conn)
}
func findByUsername(username string, conn util.Cmder) (*model.Skin, error) {
if username == "" {
return nil, &http.SkinNotFoundError{username}
}
redisKey := buildUsernameKey(username)
response := conn.Cmd("GET", redisKey)
2019-05-06 19:42:37 +05:30
if !response.IsType(redis.Str) {
return nil, &http.SkinNotFoundError{username}
}
encodedResult, err := response.Bytes()
if err != nil {
return nil, err
}
result, err := zlibDecode(encodedResult)
if err != nil {
return nil, err
}
var skin *model.Skin
err = json.Unmarshal(result, &skin)
if err != nil {
return nil, err
}
skin.OldUsername = skin.Username
return skin, nil
}
func findByUserId(id int, conn util.Cmder) (*model.Skin, error) {
response := conn.Cmd("HGET", accountIdToUsernameKey, id)
2019-05-06 19:42:37 +05:30
if !response.IsType(redis.Str) {
return nil, &http.SkinNotFoundError{"unknown"}
}
username, _ := response.Str()
return findByUsername(username, conn)
}
func removeByUserId(id int, conn util.Cmder) error {
record, err := findByUserId(id, conn)
if err != nil {
if _, ok := err.(*http.SkinNotFoundError); !ok {
return err
}
}
conn.Cmd("MULTI")
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.(*http.SkinNotFoundError); ok {
2019-05-06 19:42:37 +05:30
return nil
}
2019-05-06 19:42:37 +05:30
return err
}
conn.Cmd("MULTI")
conn.Cmd("DEL", buildUsernameKey(record.Username))
2019-05-06 19:42:37 +05:30
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
if skin.OldUsername != "" && skin.OldUsername != skin.Username {
conn.Cmd("DEL", buildUsernameKey(skin.OldUsername))
}
// If this is a new record or if the user has changed username, we set the value in the hash table
if skin.OldUsername != "" || skin.OldUsername != skin.Username {
conn.Cmd("HSET", accountIdToUsernameKey, skin.UserId, skin.Username)
}
str, _ := json.Marshal(skin)
conn.Cmd("SET", buildUsernameKey(skin.Username), zlibEncode(str))
conn.Cmd("EXEC")
skin.OldUsername = skin.Username
return nil
}
2019-04-25 04:53:10 +05:30
func findMojangUuidByUsername(username string, conn util.Cmder) (string, error) {
response := conn.Cmd("HGET", mojangUsernameToUuidKey, strings.ToLower(username))
if response.IsType(redis.Nil) {
return "", &mojangtextures.ValueNotFound{}
2019-04-25 04:53:10 +05:30
}
data, _ := response.Str()
parts := strings.Split(data, ":")
timestamp, _ := strconv.ParseInt(parts[1], 10, 64)
storedAt := time.Unix(timestamp, 0)
if storedAt.Add(time.Hour * 24 * 30).Before(time.Now()) {
return "", &mojangtextures.ValueNotFound{}
2019-04-25 04:53:10 +05:30
}
return parts[0], nil
}
2019-05-06 19:42:37 +05:30
func storeMojangUuid(username string, uuid string, conn util.Cmder) error {
2019-04-25 04:53:10 +05:30
value := uuid + ":" + strconv.FormatInt(time.Now().Unix(), 10)
2019-05-06 19:42:37 +05:30
res := conn.Cmd("HSET", mojangUsernameToUuidKey, strings.ToLower(username), value)
if res.IsType(redis.Err) {
return res.Err
}
return nil
2019-04-25 04:53:10 +05:30
}
func buildUsernameKey(username string) string {
return "username:" + strings.ToLower(username)
}
func zlibEncode(str []byte) []byte {
var buff bytes.Buffer
writer := zlib.NewWriter(&buff)
2019-05-06 19:42:37 +05:30
_, _ = 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)
2019-05-06 19:42:37 +05:30
_, _ = io.Copy(resultBuffer, reader)
reader.Close()
return resultBuffer.Bytes(), nil
}