Added URL submission

This commit is contained in:
2018-11-17 16:47:39 +00:00
committed by cdramey
parent 412c625916
commit 5c669c8b93
9 changed files with 175 additions and 62 deletions

View File

@ -3,12 +3,10 @@ package bolt
import (
"encoding/binary"
"qurl/qurl"
// "bytes"
// "fmt"
)
var (
QURLBucket = []byte{0x00}
QURLBucket = []byte{0x00}
ReverseBucket = []byte{0x01}
URLField = []byte{0x00}
@ -17,7 +15,12 @@ var (
BrowserField = []byte{0x03}
)
func (stor *BoltStorage) AddQURL(qurl *qurl.QURL) error {
func (stor *BoltStorage) AddQURL(q *qurl.QURL) error {
err := q.CheckValid()
if err != nil {
return err
}
tx, err := stor.DB.Begin(true)
if err != nil {
return err
@ -30,16 +33,21 @@ func (stor *BoltStorage) AddQURL(qurl *qurl.QURL) error {
}
// Populate the ID from the sequence if we don't have one
if qurl.ID == 0 {
qurl.ID, err = rb.NextSequence()
if q.ID == 0 {
s, err := rb.NextSequence()
if err != nil {
return err
}
// Bolt's sequence starts at 1, so for
// backwards compatibility we have subtract
// one so we're zero-based
q.ID = (s - 1)
}
// Create a byte array from the ID
bid := make([]byte, 8)
binary.BigEndian.PutUint64(bid, qurl.ID)
binary.BigEndian.PutUint64(bid, q.ID)
// Add an entry into the reverse indexed bucket for quickly
// determining if a URL is already in the database
@ -47,7 +55,7 @@ func (stor *BoltStorage) AddQURL(qurl *qurl.QURL) error {
if err != nil {
return err
}
err = ab.Put([]byte(qurl.URL), bid)
err = ab.Put([]byte(q.URL), bid)
qb, err := rb.CreateBucketIfNotExists(bid)
if err != nil {
@ -55,14 +63,14 @@ func (stor *BoltStorage) AddQURL(qurl *qurl.QURL) error {
}
// Write the ID to URL
err = qb.Put(URLField, []byte(qurl.URL))
err = qb.Put(URLField, []byte(q.URL))
if err != nil {
return err
}
if !qurl.Created.IsZero() {
if !q.Created.IsZero() {
// Create byte array from the Created date
bdt, err := qurl.Created.MarshalBinary()
bdt, err := q.Created.MarshalBinary()
if err != nil {
return err
}
@ -74,15 +82,15 @@ func (stor *BoltStorage) AddQURL(qurl *qurl.QURL) error {
}
}
if qurl.IP != nil {
err = qb.Put(IPField, qurl.IP)
if q.IP != nil {
err = qb.Put(IPField, q.IP)
if err != nil {
return err
}
}
if len(qurl.Browser) > 0 {
err = qb.Put(BrowserField, []byte(qurl.Browser))
if len(q.Browser) > 0 {
err = qb.Put(BrowserField, []byte(q.Browser))
if err != nil {
return err
}
@ -107,7 +115,10 @@ func (stor *BoltStorage) SetQURLSequence(seq uint64) error {
return err
}
qb.SetSequence(seq)
// Since the sequence number is decremented by one
// for backwards compatibility (see above)
// we increment it by one when setting the sequence
qb.SetSequence(seq + 1)
if err := tx.Commit(); err != nil {
return err
@ -118,10 +129,10 @@ func (stor *BoltStorage) SetQURLSequence(seq uint64) error {
func (stor *BoltStorage) GetQURLByURL(u string) (*qurl.QURL, error) {
tx, err := stor.DB.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()
if err != nil {
return nil, err
}
defer tx.Rollback()
ab := tx.Bucket(ReverseBucket)
if ab == nil {
@ -143,46 +154,12 @@ func (stor *BoltStorage) GetQURLByURL(u string) (*qurl.QURL, error) {
return nil, nil
}
qurl := &qurl.QURL{ ID: binary.BigEndian.Uint64(bid) }
q := &qurl.QURL{ID: binary.BigEndian.Uint64(bid)}
qu := qb.Get(URLField)
if qu != nil {
qurl.URL = string(qu)
q.URL = string(qu)
}
return qurl, nil
return q, nil
}
/*
func (stor *BoltStorage) GetQURLByURL(u string) (*qurl.QURL, error) {
tx, err := stor.DB.Begin(false)
if err != nil {
return nil, err
}
defer tx.Rollback()
rb := tx.Bucket(QURLBucket)
if rb == nil {
return nil, nil
}
bu := []byte(u)
rc := rb.Cursor()
for k, _ := rc.First(); k != nil; k, _ = rc.Next() {
qb := rb.Bucket(k)
if qb == nil {
continue
}
qu := qb.Get(URLField)
if bytes.Equal(bu, qu) {
qurl := &qurl.QURL{
ID: binary.BigEndian.Uint64(k),
URL: string(qu),
}
return qurl, nil
}
}
return nil, nil
}
*/