2017-06-13 07:12:58 +00:00
|
|
|
package main
|
|
|
|
|
2017-06-13 08:56:48 +00:00
|
|
|
import "log"
|
2017-06-13 07:12:58 +00:00
|
|
|
import "sync"
|
|
|
|
import "database/sql"
|
2017-06-13 08:56:48 +00:00
|
|
|
import "./query_gen/lib"
|
2017-06-13 07:12:58 +00:00
|
|
|
|
|
|
|
var topics TopicStore
|
|
|
|
|
|
|
|
type TopicStore interface {
|
|
|
|
Load(id int) error
|
|
|
|
Get(id int) (*Topic, error)
|
|
|
|
GetUnsafe(id int) (*Topic, error)
|
|
|
|
CascadeGet(id int) (*Topic, error)
|
|
|
|
BypassGet(id int) (*Topic, error)
|
|
|
|
Set(item *Topic) error
|
|
|
|
Add(item *Topic) error
|
|
|
|
AddUnsafe(item *Topic) error
|
|
|
|
Remove(id int) error
|
|
|
|
RemoveUnsafe(id int) error
|
|
|
|
AddLastTopic(item *Topic, fid int) error
|
|
|
|
GetLength() int
|
|
|
|
GetCapacity() int
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
type MemoryTopicStore struct {
|
2017-06-13 07:12:58 +00:00
|
|
|
items map[int]*Topic
|
|
|
|
length int
|
|
|
|
capacity int
|
|
|
|
get *sql.Stmt
|
|
|
|
sync.RWMutex
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func NewMemoryTopicStore(capacity int) *MemoryTopicStore {
|
|
|
|
stmt, 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-06-15 11:40:35 +00:00
|
|
|
return &MemoryTopicStore{
|
2017-06-13 08:56:48 +00:00
|
|
|
items:make(map[int]*Topic),
|
|
|
|
capacity:capacity,
|
|
|
|
get:stmt,
|
|
|
|
}
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) Get(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-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) GetUnsafe(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-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) CascadeGet(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
|
|
|
|
}
|
|
|
|
|
|
|
|
topic = &Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
|
|
|
if err == nil {
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.Add(topic)
|
|
|
|
}
|
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) BypassGet(id int) (*Topic, error) {
|
2017-06-13 07:12:58 +00:00
|
|
|
topic := &Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) Load(id int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
topic := &Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
|
|
|
if err == nil {
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.Set(topic)
|
|
|
|
} else {
|
|
|
|
sts.Remove(id)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) Set(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-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) Add(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-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) AddUnsafe(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-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) Remove(id int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
sts.Lock()
|
|
|
|
delete(sts.items,id)
|
|
|
|
sts.Unlock()
|
|
|
|
sts.length--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
func (sts *MemoryTopicStore) RemoveUnsafe(id int) error {
|
2017-06-13 07:12:58 +00:00
|
|
|
delete(sts.items,id)
|
|
|
|
sts.length--
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
type SqlTopicStore struct {
|
|
|
|
get *sql.Stmt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewSqlTopicStore() *SqlTopicStore {
|
2017-06-15 11:40:35 +00:00
|
|
|
stmt, 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)
|
|
|
|
}
|
|
|
|
return &SqlTopicStore{stmt}
|
2017-06-13 07:12:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *SqlTopicStore) Get(id int) (*Topic, error) {
|
|
|
|
topic := Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
return &topic, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *SqlTopicStore) GetUnsafe(id int) (*Topic, error) {
|
|
|
|
topic := Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
return &topic, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *SqlTopicStore) CascadeGet(id int) (*Topic, error) {
|
|
|
|
topic := Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
return &topic, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *SqlTopicStore) BypassGet(id int) (*Topic, error) {
|
|
|
|
topic := &Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
return topic, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *SqlTopicStore) Load(id int) error {
|
|
|
|
topic := Topic{ID:id}
|
|
|
|
err := sts.get.QueryRow(id).Scan(&topic.Title, &topic.Content, &topic.CreatedBy, &topic.CreatedAt, &topic.Is_Closed, &topic.Sticky, &topic.ParentID, &topic.IpAddress, &topic.PostCount, &topic.LikeCount, &topic.Data)
|
2017-06-28 12:05:26 +00:00
|
|
|
topic.Slug = name_to_slug(topic.Title)
|
2017-06-13 07:12:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Placeholder methods, the actual queries are done elsewhere
|
|
|
|
func (sts *SqlTopicStore) Set(item *Topic) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sts *SqlTopicStore) Add(item *Topic) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sts *SqlTopicStore) AddUnsafe(item *Topic) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sts *SqlTopicStore) Remove(id int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sts *SqlTopicStore) RemoveUnsafe(id int) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sts *SqlTopicStore) AddLastTopic(item *Topic, fid int) error {
|
|
|
|
// Coming Soon...
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
func (sts *SqlTopicStore) GetCapacity() int {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sts *SqlTopicStore) GetLength() int {
|
|
|
|
return 0 // Return the total number of topics on the forums?
|
|
|
|
}
|