2017-09-13 15:09:13 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Gosora Topic Store
|
|
|
|
* Copyright Azareal 2017 - 2018
|
|
|
|
*
|
|
|
|
*/
|
2017-06-13 07:12:58 +00:00
|
|
|
package main
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"./query_gen/lib"
|
|
|
|
)
|
2017-06-13 07:12:58 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
// TODO: Add the watchdog goroutine
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Add BulkGetMap
|
|
|
|
// TODO: Add some sort of update method
|
2017-06-13 07:12:58 +00:00
|
|
|
var topics TopicStore
|
|
|
|
|
|
|
|
type TopicStore interface {
|
2017-09-15 22:20:01 +00:00
|
|
|
Reload(id int) error // ? - Should we move this to TopicCache? Might require us to do a lot more casting in Gosora though...
|
2017-06-13 07:12:58 +00:00
|
|
|
Get(id int) (*Topic, error)
|
|
|
|
BypassGet(id int) (*Topic, error)
|
2017-09-15 22:20:01 +00:00
|
|
|
Delete(id int) error
|
|
|
|
Exists(id int) bool
|
2017-06-13 07:12:58 +00:00
|
|
|
AddLastTopic(item *Topic, fid int) error
|
2017-09-15 22:20:01 +00:00
|
|
|
GetGlobalCount() int
|
|
|
|
}
|
|
|
|
|
|
|
|
type TopicCache interface {
|
|
|
|
CacheGet(id int) (*Topic, error)
|
|
|
|
GetUnsafe(id int) (*Topic, error)
|
|
|
|
CacheSet(item *Topic) error
|
|
|
|
CacheAdd(item *Topic) error
|
|
|
|
CacheAddUnsafe(item *Topic) error
|
|
|
|
CacheRemove(id int) error
|
|
|
|
CacheRemoveUnsafe(id int) error
|
2017-06-13 07:12:58 +00:00
|
|
|
GetLength() int
|
2017-09-15 22:20:01 +00:00
|
|
|
SetCapacity(capacity int)
|
2017-06-13 07:12:58 +00:00
|
|
|
GetCapacity() int
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
type MemoryTopicStore struct {
|
2017-09-15 22:20:01 +00:00
|
|
|
items map[int]*Topic
|
|
|
|
length int
|
|
|
|
capacity int
|
|
|
|
get *sql.Stmt
|
|
|
|
exists *sql.Stmt
|
|
|
|
topicCount *sql.Stmt
|
2017-06-13 07:12:58 +00:00
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-09-13 15:09:13 +00:00
|
|
|
// NewMemoryTopicStore gives you a new instance of MemoryTopicStore
|
2017-06-15 11:40:35 +00:00
|
|
|
func NewMemoryTopicStore(capacity int) *MemoryTopicStore {
|
2017-09-15 22:20:01 +00:00
|
|
|
getStmt, err := qgen.Builder.SimpleSelect("topics", "title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
existsStmt, err := qgen.Builder.SimpleSelect("topics", "tid", "tid = ?", "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
topicCountStmt, err := qgen.Builder.SimpleCount("topics", "", "")
|
2017-06-13 08:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-06-15 11:40:35 +00:00
|
|
|
return &MemoryTopicStore{
|
2017-09-15 22:20:01 +00:00
|
|
|
items: make(map[int]*Topic),
|
|
|
|
capacity: capacity,
|
|
|
|
get: getStmt,
|
|
|
|
exists: existsStmt,
|
|
|
|
topicCount: topicCountStmt,
|
2017-06-13 08:56:48 +00:00
|
|
|
}
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *MemoryTopicStore) CacheGet(id int) (*Topic, error) {
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.RLock()
|
|
|
|
item, ok := sts.items[id]
|
|
|
|
sts.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
2017-06-28 12:05:26 +00:00
|
|
|
return item, ErrNoRows
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *MemoryTopicStore) CacheGetUnsafe(id int) (*Topic, error) {
|
2017-06-13 07:12:58 +00:00
|
|
|
item, ok := sts.items[id]
|
|
|
|
if ok {
|
|
|
|
return item, nil
|
|
|
|
}
|
2017-06-28 12:05:26 +00:00
|
|
|
return item, ErrNoRows
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *MemoryTopicStore) Get(id int) (*Topic, error) {
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.RLock()
|
|
|
|
topic, ok := sts.items[id]
|
|
|
|
sts.RUnlock()
|
|
|
|
if ok {
|
|
|
|
return topic, nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
topic = &Topic{ID: id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-13 07:12:58 +00:00
|
|
|
if err == nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-09-15 22:20:01 +00:00
|
|
|
_ = sts.CacheAdd(topic)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) BypassGet(id int) (*Topic, error) {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := &Topic{ID: id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *MemoryTopicStore) Reload(id int) error {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := &Topic{ID: id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-13 07:12:58 +00:00
|
|
|
if err == nil {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-09-15 22:20:01 +00:00
|
|
|
_ = sts.CacheSet(topic)
|
2017-06-13 07:12:58 +00:00
|
|
|
} else {
|
2017-09-15 22:20:01 +00:00
|
|
|
_ = sts.CacheRemove(id)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Use a transaction here
|
|
|
|
func (sts *MemoryTopicStore) Delete(id int) error {
|
|
|
|
topic, err := sts.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil // Already gone, maybe we should check for other errors here
|
|
|
|
}
|
|
|
|
|
|
|
|
topicCreator, err := users.Get(topic.CreatedBy)
|
|
|
|
if err == nil {
|
|
|
|
wcount := wordCount(topic.Content)
|
|
|
|
err = topicCreator.decreasePostStats(wcount, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fstore.DecrementTopicCount(topic.ParentID)
|
|
|
|
if err != nil && err != ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sts.Lock()
|
|
|
|
sts.CacheRemoveUnsafe(id)
|
|
|
|
_, err = delete_topic_stmt.Exec(id)
|
|
|
|
if err != nil {
|
|
|
|
sts.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sts.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *MemoryTopicStore) Exists(id int) bool {
|
|
|
|
return sts.exists.QueryRow(id).Scan(&id) == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *MemoryTopicStore) CacheSet(item *Topic) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.Lock()
|
|
|
|
_, ok := sts.items[item.ID]
|
|
|
|
if ok {
|
|
|
|
sts.items[item.ID] = item
|
|
|
|
} else if sts.length >= sts.capacity {
|
|
|
|
sts.Unlock()
|
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
} else {
|
|
|
|
sts.items[item.ID] = item
|
|
|
|
sts.length++
|
|
|
|
}
|
|
|
|
sts.Unlock()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *MemoryTopicStore) CacheAdd(item *Topic) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
if sts.length >= sts.capacity {
|
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
}
|
|
|
|
sts.Lock()
|
|
|
|
sts.items[item.ID] = item
|
|
|
|
sts.Unlock()
|
|
|
|
sts.length++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Make these length increments thread-safe. Ditto for the other DataStores
|
|
|
|
func (sts *MemoryTopicStore) CacheAddUnsafe(item *Topic) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
if sts.length >= sts.capacity {
|
|
|
|
return ErrStoreCapacityOverflow
|
|
|
|
}
|
|
|
|
sts.items[item.ID] = item
|
|
|
|
sts.length++
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// TODO: Make these length decrements thread-safe. Ditto for the other DataStores
|
|
|
|
func (sts *MemoryTopicStore) CacheRemove(id int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.Lock()
|
2017-09-03 04:50:31 +00:00
|
|
|
delete(sts.items, id)
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.Unlock()
|
|
|
|
sts.length--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *MemoryTopicStore) CacheRemoveUnsafe(id int) error {
|
2017-09-03 04:50:31 +00:00
|
|
|
delete(sts.items, id)
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.length--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// ? - What is this? Do we need it? Should it be in the main store interface?
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) AddLastTopic(item *Topic, fid int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
// Coming Soon...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) GetLength() int {
|
2017-06-13 07:12:58 +00:00
|
|
|
return sts.length
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) SetCapacity(capacity int) {
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.capacity = capacity
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) GetCapacity() int {
|
2017-06-13 07:12:58 +00:00
|
|
|
return sts.capacity
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// Return the total number of topics on these forums
|
|
|
|
func (sts *MemoryTopicStore) GetGlobalCount() int {
|
|
|
|
var tcount int
|
|
|
|
err := sts.topicCount.QueryRow().Scan(&tcount)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return tcount
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
type SQLTopicStore struct {
|
2017-09-15 22:20:01 +00:00
|
|
|
get *sql.Stmt
|
|
|
|
exists *sql.Stmt
|
|
|
|
topicCount *sql.Stmt
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
func NewSQLTopicStore() *SQLTopicStore {
|
2017-09-15 22:20:01 +00:00
|
|
|
getStmt, err := qgen.Builder.SimpleSelect("topics", "title, content, createdBy, createdAt, is_closed, sticky, parentID, ipaddress, postCount, likeCount, data", "tid = ?", "", "")
|
2017-06-13 08:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2017-09-15 22:20:01 +00:00
|
|
|
existsStmt, err := qgen.Builder.SimpleSelect("topics", "tid", "tid = ?", "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
topicCountStmt, err := qgen.Builder.SimpleCount("topics", "", "")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
return &SQLTopicStore{
|
|
|
|
get: getStmt,
|
|
|
|
exists: existsStmt,
|
|
|
|
topicCount: topicCountStmt,
|
|
|
|
}
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
func (sts *SQLTopicStore) Get(id int) (*Topic, error) {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := Topic{ID: id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return &topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
func (sts *SQLTopicStore) BypassGet(id int) (*Topic, error) {
|
2017-09-03 04:50:31 +00:00
|
|
|
topic := &Topic{ID: id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.IsClosed, &topic.Sticky, &topic.ParentID, &topic.IPAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
|
|
|
topic.Link = buildTopicURL(nameToSlug(topic.Title), id)
|
2017-06-13 07:12:58 +00:00
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *SQLTopicStore) Reload(id int) error {
|
|
|
|
return sts.exists.QueryRow(id).Scan(&id)
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
func (sts *SQLTopicStore) Exists(id int) bool {
|
|
|
|
return sts.exists.QueryRow(id).Scan(&id) == nil
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
2017-09-15 22:20:01 +00:00
|
|
|
|
|
|
|
// TODO: Use a transaction here
|
|
|
|
func (sts *SQLTopicStore) Delete(id int) error {
|
|
|
|
topic, err := sts.Get(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil // Already gone, maybe we should check for other errors here
|
|
|
|
}
|
|
|
|
|
|
|
|
topicCreator, err := users.Get(topic.CreatedBy)
|
|
|
|
if err == nil {
|
|
|
|
wcount := wordCount(topic.Content)
|
|
|
|
err = topicCreator.decreasePostStats(wcount, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = fstore.DecrementTopicCount(topic.ParentID)
|
|
|
|
if err != nil && err != ErrNoRows {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = delete_topic_stmt.Exec(id)
|
|
|
|
return err
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
2017-09-15 22:20:01 +00:00
|
|
|
|
2017-09-10 16:57:22 +00:00
|
|
|
func (sts *SQLTopicStore) AddLastTopic(item *Topic, fid int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
// Coming Soon...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-09-15 22:20:01 +00:00
|
|
|
// Return the total number of topics on these forums
|
|
|
|
func (sts *SQLTopicStore) GetGlobalCount() int {
|
|
|
|
var tcount int
|
|
|
|
err := sts.topicCount.QueryRow().Scan(&tcount)
|
|
|
|
if err != nil {
|
|
|
|
LogError(err)
|
|
|
|
}
|
|
|
|
return tcount
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|