From e0854aa001c90aac942bdd8f0cd0449434922e80 Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Thu, 18 May 2017 22:16:16 +0000 Subject: [PATCH] Using master pg library --- createdb.sql | 3 ++- lib/cover.go | 7 ------ lib/database/books.go | 42 +++++++++++++++------------------- lib/database/database.go | 7 +++++- lib/database/news.go | 2 +- lib/database/users.go | 2 +- lib/reader.go | 49 ++++++++++++---------------------------- lib/trantor.go | 7 ------ 8 files changed, 43 insertions(+), 76 deletions(-) diff --git a/createdb.sql b/createdb.sql index 4b330e9..f3ef2f8 100644 --- a/createdb.sql +++ b/createdb.sql @@ -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); diff --git a/lib/cover.go b/lib/cover.go index fa3811e..3300934 100644 --- a/lib/cover.go +++ b/lib/cover.go @@ -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 diff --git a/lib/database/books.go b/lib/database/books.go index 8998a87..6f8720e 100644 --- a/lib/database/books.go +++ b/lib/database/books.go @@ -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 == "" { diff --git a/lib/database/database.go b/lib/database/database.go index c4f5d01..77f5d81 100644 --- a/lib/database/database.go +++ b/lib/database/database.go @@ -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, diff --git a/lib/database/news.go b/lib/database/news.go index c3c5702..5fb55c8 100644 --- a/lib/database/news.go +++ b/lib/database/news.go @@ -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, }) diff --git a/lib/database/users.go b/lib/database/users.go index 69985f1..6e082fd 100644 --- a/lib/database/users.go +++ b/lib/database/users.go @@ -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) { diff --git a/lib/reader.go b/lib/reader.go index f0abc11..0961f46 100644 --- a/lib/reader.go +++ b/lib/reader.go @@ -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 diff --git a/lib/trantor.go b/lib/trantor.go index d99da9f..9ae5277 100644 --- a/lib/trantor.go +++ b/lib/trantor.go @@ -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)