func fetchLatestMessage(msg *pandora.Message, db querier, inbox string, now time.Time, dur time.Duration) error { err := reEnqueueMessages(db, now) if err != nil { return err } inboxId, err := findInbox(db, inbox, false) if err != nil { return err } var buf []byte var id int64 err = db.QueryRow(`select id, mid, status, receivedat, sendwhen, deliverycount from pgstore_messages where receiverid = $1 and lid is null and sendwhen <= $2 and status <> $3 order by sendwhen asc limit 1`, inboxId, now, pandora.StatusConfirmed).Scan(&id, &buf, &msg.Status, &msg.ReceivedAt, &msg.SendWhen, &msg.DeliveryCount) if err == sql.ErrNoRows { return pandora.ErrNoMessages } if err != nil { return err } msg.Mid = &pandora.SHA1Key{} copy(msg.Mid.Bytes(), buf) msg.CalcualteLeaseFor(now, dur) _, err = db.Exec("update pgstore_messages set lid = $1, deliverycount = deliverycount + 1, leaseuntil = $2 where id = $3", msg.Lid.Bytes(), msg.LeasedUntil, id) return err }
func fetchHeaders(out []pandora.Message, db querier, inbox string, now, min time.Time) ([]pandora.Message, error) { err := reEnqueueMessages(db, now) if err != nil { return nil, err } inboxId, err := findInbox(db, inbox, false) if err != nil { return nil, err } results, err := db.Query(`select mid, status, receivedat, sendwhen, deliverycount from pgstore_messages where receiverid = $1 and lid is null and sendwhen <= $2 and receivedat > $3 and status <> $4 order by receivedat asc, sendwhen asc limit $5`, inboxId, now, min, pandora.StatusConfirmed, len(out)) if err != nil { return nil, err } defer results.Close() var idx int var buf []byte for results.Next() { var msg *pandora.Message if idx < len(out) { msg = &out[idx] } else { break } msg.Mid = &pandora.SHA1Key{} err := results.Scan(&buf, &msg.Status, &msg.ReceivedAt, &msg.SendWhen, &msg.DeliveryCount) if err != nil { return out[:idx], err } copy(msg.Mid.Bytes(), buf) idx++ } return out[:idx], err }