2016-01-15 01:27:30 +00:00
|
|
|
package storage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/blevesearch/bleve"
|
|
|
|
"github.com/khlieng/dispatch/Godeps/_workspace/src/github.com/boltdb/bolt"
|
|
|
|
)
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
func (u *User) LogMessage(server, from, to, content string) error {
|
|
|
|
message := Message{
|
|
|
|
Server: server,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
Content: content,
|
|
|
|
Time: time.Now().Unix(),
|
|
|
|
}
|
2016-01-15 01:27:30 +00:00
|
|
|
bucketKey := server + ":" + to
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
err := u.messageLog.Batch(func(tx *bolt.Tx) error {
|
|
|
|
b, err := tx.Bucket(bucketMessages).CreateBucketIfNotExists([]byte(bucketKey))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
message.ID, _ = b.NextSequence()
|
2016-01-15 01:27:30 +00:00
|
|
|
|
2016-01-17 22:33:52 +00:00
|
|
|
data, err := message.MarshalMsg(nil)
|
2016-01-17 20:15:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.Put(idToBytes(message.ID), data)
|
2016-01-15 01:27:30 +00:00
|
|
|
})
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return u.messageIndex.Index(bucketKey+":"+strconv.FormatUint(message.ID, 10), message)
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetLastMessages(server, channel string, count int) ([]Message, error) {
|
|
|
|
messages := make([]Message, count)
|
|
|
|
|
|
|
|
u.messageLog.View(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(bucketMessages).Bucket([]byte(server + ":" + channel))
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := b.Cursor()
|
|
|
|
|
2016-01-17 20:15:29 +00:00
|
|
|
for _, v := c.Last(); count > 0 && v != nil; _, v = c.Prev() {
|
2016-01-15 01:27:30 +00:00
|
|
|
count--
|
2016-01-17 22:33:52 +00:00
|
|
|
messages[count].UnmarshalMsg(v)
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2016-01-18 02:21:58 +00:00
|
|
|
if count == 0 {
|
|
|
|
return messages, nil
|
|
|
|
} else if count < len(messages) {
|
2016-01-15 01:27:30 +00:00
|
|
|
return messages[count:], nil
|
|
|
|
}
|
2016-01-18 02:21:58 +00:00
|
|
|
|
|
|
|
return nil, nil
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) GetMessages(server, channel string, count int, fromID uint64) ([]Message, error) {
|
|
|
|
messages := make([]Message, count)
|
|
|
|
|
|
|
|
u.messageLog.View(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(bucketMessages).Bucket([]byte(server + ":" + channel))
|
|
|
|
if b == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
c := b.Cursor()
|
2016-01-17 20:15:29 +00:00
|
|
|
c.Seek(idToBytes(fromID))
|
2016-01-15 01:27:30 +00:00
|
|
|
|
|
|
|
for k, v := c.Prev(); count > 0 && k != nil; k, v = c.Prev() {
|
|
|
|
count--
|
2016-01-17 22:33:52 +00:00
|
|
|
messages[count].UnmarshalMsg(v)
|
2016-01-15 01:27:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2016-01-18 02:21:58 +00:00
|
|
|
if count == 0 {
|
|
|
|
return messages, nil
|
|
|
|
} else if count < len(messages) {
|
2016-01-15 01:27:30 +00:00
|
|
|
return messages[count:], nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) SearchMessages(server, channel, phrase string) ([]Message, error) {
|
|
|
|
serverQuery := bleve.NewMatchQuery(server)
|
|
|
|
serverQuery.SetField("server")
|
|
|
|
channelQuery := bleve.NewMatchQuery(channel)
|
|
|
|
channelQuery.SetField("to")
|
2016-01-17 20:15:29 +00:00
|
|
|
contentQuery := bleve.NewFuzzyQuery(phrase)
|
2016-01-15 01:27:30 +00:00
|
|
|
contentQuery.SetField("content")
|
|
|
|
|
|
|
|
query := bleve.NewBooleanQuery([]bleve.Query{serverQuery, channelQuery, contentQuery}, nil, nil)
|
|
|
|
|
|
|
|
search := bleve.NewSearchRequest(query)
|
|
|
|
searchResults, err := u.messageIndex.Search(search)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
messages := []Message{}
|
|
|
|
u.messageLog.View(func(tx *bolt.Tx) error {
|
|
|
|
b := tx.Bucket(bucketMessages)
|
|
|
|
|
|
|
|
for _, hit := range searchResults.Hits {
|
|
|
|
idx := strings.LastIndex(hit.ID, ":")
|
|
|
|
bc := b.Bucket([]byte(hit.ID[:idx]))
|
2016-01-17 20:15:29 +00:00
|
|
|
id, _ := strconv.ParseUint(hit.ID[idx+1:], 10, 64)
|
2016-01-15 01:27:30 +00:00
|
|
|
|
2016-01-17 22:33:52 +00:00
|
|
|
message := Message{}
|
|
|
|
message.UnmarshalMsg(bc.Get(idToBytes(id)))
|
2016-01-15 01:27:30 +00:00
|
|
|
messages = append(messages, message)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return messages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (u *User) openMessageLog() error {
|
|
|
|
err := os.MkdirAll(Path.User(u.Username), 0700)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.messageLog, err = bolt.Open(Path.Log(u.Username), 0600, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
u.messageLog.Update(func(tx *bolt.Tx) error {
|
|
|
|
tx.CreateBucketIfNotExists(bucketMessages)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
indexPath := Path.Index(u.Username)
|
|
|
|
u.messageIndex, err = bleve.Open(indexPath)
|
|
|
|
if err == bleve.ErrorIndexPathDoesNotExist {
|
|
|
|
mapping := bleve.NewIndexMapping()
|
|
|
|
u.messageIndex, err = bleve.New(indexPath, mapping)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2016-01-19 21:02:12 +00:00
|
|
|
|
|
|
|
func (u *User) closeMessageLog() {
|
|
|
|
u.messageLog.Close()
|
|
|
|
u.messageIndex.Close()
|
|
|
|
}
|