Using master pg library
This commit is contained in:
parent
d9a95a997c
commit
e0854aa001
8 changed files with 43 additions and 76 deletions
|
@ -1,4 +1,4 @@
|
|||
CREATE EXTENSION pg_trgm;
|
||||
--CREATE EXTENSION pg_trgm;
|
||||
|
||||
CREATE TABLE books (
|
||||
id varchar(16) primary key,
|
||||
|
@ -21,6 +21,7 @@ CREATE TABLE books (
|
|||
-- Books column indexes
|
||||
CREATE INDEX CONCURRENTLY books_lang_idx ON books (lang);
|
||||
CREATE INDEX CONCURRENTLY books_isbn_idx ON books (isbn);
|
||||
CREATE INDEX CONCURRENTLY books_active_idx ON books (active);
|
||||
|
||||
-- Books trigram indexes
|
||||
CREATE INDEX CONCURRENTLY books_title_idx ON books USING GIN (title gin_trgm_ops);
|
||||
|
|
|
@ -38,13 +38,6 @@ func coverHandler(h handler) {
|
|||
return
|
||||
}
|
||||
|
||||
if !book.Active {
|
||||
if !h.sess.IsModerator() {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
file := coverFile
|
||||
if vars["size"] == "small" {
|
||||
file = coverSmallFile
|
||||
|
|
|
@ -18,8 +18,8 @@ type Book struct {
|
|||
Lang string
|
||||
Isbn string
|
||||
FileSize int
|
||||
Cover bool
|
||||
Active bool
|
||||
Cover bool `sql:",notnull"`
|
||||
Active bool `sql:",notnull"`
|
||||
UploadDate time.Time
|
||||
Tsv string
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ func (db *pgDB) AddBook(book Book) error {
|
|||
book.UploadDate = time.Now()
|
||||
}
|
||||
|
||||
return db.sql.Create(&book)
|
||||
return db.sql.Insert(&book)
|
||||
}
|
||||
|
||||
// GetBooks matching query
|
||||
|
@ -39,8 +39,6 @@ func (db *pgDB) GetBooks(query string, length int, start int) (books []Book, num
|
|||
return db.getBooks(true, query, length, start)
|
||||
}
|
||||
|
||||
// TODO: func (db *pgDB) GetBooksIter() Iter {
|
||||
|
||||
// GetNewBooks returns a list of books in the incoming queue and the number of books
|
||||
// in the queue
|
||||
func (db *pgDB) GetNewBooks(query string, length int, start int) (books []Book, num int, err error) {
|
||||
|
@ -48,45 +46,42 @@ func (db *pgDB) GetNewBooks(query string, length int, start int) (books []Book,
|
|||
}
|
||||
|
||||
func (db *pgDB) getBooks(active bool, query string, length int, start int) (books []Book, num int, err error) {
|
||||
column := []string{}
|
||||
columnParams := []interface{}{}
|
||||
searchCondition := "active = "
|
||||
rank := []string{}
|
||||
rankParams := []interface{}{}
|
||||
searchCondition := ""
|
||||
if active {
|
||||
searchCondition = "true"
|
||||
searchCondition += "active is true"
|
||||
} else {
|
||||
searchCondition = "false"
|
||||
searchCondition += "active is not true"
|
||||
}
|
||||
searchParams := []interface{}{}
|
||||
|
||||
textQuery, columnQuerys, trigramQuerys := buildQuery(query)
|
||||
for _, c := range columnQuerys {
|
||||
searchCondition = searchCondition + " AND " + c.column + " = ?"
|
||||
searchCondition += " AND " + c.column + " = ?"
|
||||
searchParams = append(searchParams, c.value)
|
||||
}
|
||||
for _, c := range trigramQuerys {
|
||||
column = append(column, "word_similarity(?, "+c.column+")")
|
||||
columnParams = append(columnParams, c.value)
|
||||
searchCondition = searchCondition + " AND " + c.column + " %> ?"
|
||||
rank = append(rank, "word_similarity(?, "+c.column+")")
|
||||
rankParams = append(rankParams, c.value)
|
||||
searchCondition += " AND " + c.column + " %> ?"
|
||||
searchParams = append(searchParams, c.value)
|
||||
}
|
||||
if textQuery != "" {
|
||||
column = append(column, "ts_rank(tsv, to_tsquery(?))")
|
||||
columnParams = append(columnParams, textQuery)
|
||||
searchCondition = searchCondition + " AND to_tsquery(?) @@ tsv"
|
||||
rank = append(rank, "ts_rank(tsv, to_tsquery(?))")
|
||||
rankParams = append(rankParams, textQuery)
|
||||
searchCondition += " AND to_tsquery(?) @@ tsv"
|
||||
searchParams = append(searchParams, textQuery)
|
||||
}
|
||||
|
||||
columnStr := "*"
|
||||
order := "upload_date DESC"
|
||||
if len(column) > 0 {
|
||||
columnStr = "*, " + strings.Join(column, "+") + " AS rank"
|
||||
order = "rank DESC, upload_date DESC"
|
||||
if len(rank) > 0 {
|
||||
order = strings.Join(rank, "+") + " DESC, upload_date DESC"
|
||||
}
|
||||
|
||||
num, err = db.sql.Model(&books).
|
||||
ColumnExpr(columnStr, columnParams...).
|
||||
Where(searchCondition, searchParams...).
|
||||
Order(order).
|
||||
OrderExpr(order, rankParams...).
|
||||
Offset(start).
|
||||
Limit(length).
|
||||
SelectAndCountEstimate(100)
|
||||
|
@ -169,7 +164,6 @@ type columnq struct {
|
|||
}
|
||||
|
||||
func buildQuery(query string) (textQuery string, columnQuerys []columnq, trigramQuerys []columnq) {
|
||||
// FIXME: does *Querys need initialization??
|
||||
words := strings.Split(query, " ")
|
||||
for _, w := range words {
|
||||
if w == "" {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"gopkg.in/pg.v4"
|
||||
"github.com/go-pg/pg"
|
||||
)
|
||||
|
||||
type DB interface {
|
||||
|
@ -45,7 +45,12 @@ type Options struct {
|
|||
|
||||
// Init the database connection
|
||||
func Init(options Options) (DB, error) {
|
||||
network := "tcp"
|
||||
if options.Addr[0] == '/' {
|
||||
network = "unix"
|
||||
}
|
||||
sql := pg.Connect(&pg.Options{
|
||||
Network: network,
|
||||
Addr: options.Addr,
|
||||
User: options.User,
|
||||
Password: options.Password,
|
||||
|
|
|
@ -18,7 +18,7 @@ func (db *pgDB) AddNews(text string) error {
|
|||
}
|
||||
|
||||
func (db *pgDB) addRawNews(text string, date time.Time) error {
|
||||
return db.sql.Create(&New{
|
||||
return db.sql.Insert(&New{
|
||||
Text: text,
|
||||
Date: date,
|
||||
})
|
||||
|
|
|
@ -46,7 +46,7 @@ func (db *pgDB) addRawUser(name string, hpass []byte, salt []byte, role string)
|
|||
Salt: salt,
|
||||
Role: role,
|
||||
}
|
||||
return db.sql.Create(&u)
|
||||
return db.sql.Insert(&u)
|
||||
}
|
||||
|
||||
func (db *pgDB) GetRole(name string) (string, error) {
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
"github.com/gorilla/mux"
|
||||
"github.com/meskio/epubgo"
|
||||
"gitlab.com/trantor/trantor/lib/database"
|
||||
"gitlab.com/trantor/trantor/lib/storage"
|
||||
)
|
||||
|
||||
type chapter struct {
|
||||
|
@ -105,7 +106,7 @@ func getChapters(e *epubgo.Epub, file string, id string, base string) []chapter
|
|||
|
||||
func listChapters(nav *epubgo.NavigationIterator, depth int) []chapter {
|
||||
var chapters []chapter
|
||||
var err error = nil
|
||||
var err error
|
||||
for err == nil {
|
||||
var c chapter
|
||||
c.Label = nav.Title()
|
||||
|
@ -128,7 +129,7 @@ func listChapters(nav *epubgo.NavigationIterator, depth int) []chapter {
|
|||
|
||||
func readStartHandler(h handler) {
|
||||
id := mux.Vars(h.r)["id"]
|
||||
e, _ := openReadEpub(h)
|
||||
e := openReadEpub(id, h.store)
|
||||
if e == nil {
|
||||
log.Warn("Open epub returns an empty file")
|
||||
notFound(h)
|
||||
|
@ -148,7 +149,7 @@ func readStartHandler(h handler) {
|
|||
func readHandler(h handler) {
|
||||
id := mux.Vars(h.r)["id"]
|
||||
file := mux.Vars(h.r)["file"]
|
||||
e, book := openReadEpub(h)
|
||||
e := openReadEpub(id, h.store)
|
||||
if e == nil {
|
||||
notFound(h)
|
||||
return
|
||||
|
@ -158,6 +159,11 @@ func readHandler(h handler) {
|
|||
var data readData
|
||||
data.S = GetStatus(h)
|
||||
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
author := ""
|
||||
if len(book.Authors) > 0 {
|
||||
author = " by " + book.Authors[0]
|
||||
|
@ -177,38 +183,23 @@ func readHandler(h handler) {
|
|||
h.template.load(h, "read", data)
|
||||
}
|
||||
|
||||
func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
|
||||
var book database.Book
|
||||
id := mux.Vars(h.r)["id"]
|
||||
if id == "" {
|
||||
return nil, book
|
||||
}
|
||||
book, err := h.db.GetBookID(id)
|
||||
func openReadEpub(id string, store storage.Store) *epubgo.Epub {
|
||||
f, err := store.Get(id, epubFile)
|
||||
if err != nil {
|
||||
return nil, book
|
||||
}
|
||||
if !book.Active {
|
||||
if !h.sess.IsModerator() {
|
||||
return nil, book
|
||||
}
|
||||
}
|
||||
|
||||
f, err := h.store.Get(book.ID, epubFile)
|
||||
if err != nil {
|
||||
return nil, book
|
||||
return nil
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
info, err := f.Stat()
|
||||
if err != nil {
|
||||
return nil, book
|
||||
return nil
|
||||
}
|
||||
|
||||
e, err := epubgo.Load(f, info.Size())
|
||||
if err != nil {
|
||||
return nil, book
|
||||
return nil
|
||||
}
|
||||
return e, book
|
||||
return e
|
||||
}
|
||||
|
||||
func contentHandler(h handler) {
|
||||
|
@ -228,16 +219,6 @@ func contentHandler(h handler) {
|
|||
}
|
||||
|
||||
func openEpubFile(h handler, id string, file string) error {
|
||||
book, err := h.db.GetBookID(id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !book.Active {
|
||||
if !h.sess.IsModerator() {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
f, err := h.store.Get(id, epubFile)
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -84,13 +84,6 @@ func downloadHandler(h handler) {
|
|||
return
|
||||
}
|
||||
|
||||
if !book.Active {
|
||||
if !h.sess.IsModerator() {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
f, err := h.store.Get(book.ID, epubFile)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
|
|
Reference in a new issue