mirror of
https://github.com/jimeh/ozu.io.git
synced 2026-02-19 08:06:39 +00:00
Hash URLs with SHA1 for lookup by URL operations
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package shortner
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
|
||||
"github.com/jimeh/go-base58"
|
||||
"github.com/jimeh/ozu.io/storage"
|
||||
)
|
||||
@@ -79,5 +82,6 @@ func (s *Shortner) makeUIDKey(uid []byte) []byte {
|
||||
}
|
||||
|
||||
func (s *Shortner) makeURLKey(rawURL []byte) []byte {
|
||||
return append(urlKeyPrefix, rawURL...)
|
||||
urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL))
|
||||
return append(urlKeyPrefix, urlSHA...)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package shortner
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -33,8 +35,9 @@ func (s *ShortnerSuite) SetupTest() {
|
||||
func (s *ShortnerSuite) TestShortenExisting() {
|
||||
rawURL := []byte("http://google.com/")
|
||||
uid := []byte("ig")
|
||||
urlSHA := fmt.Sprintf("%x", sha1.Sum(rawURL))
|
||||
|
||||
s.store.On("Get", append([]byte("url:"), rawURL...)).Return(uid, nil)
|
||||
s.store.On("Get", append([]byte("url:"), urlSHA...)).Return(uid, nil)
|
||||
|
||||
resultUID, resultURL, err := s.shortner.Shorten(rawURL)
|
||||
s.NoError(err)
|
||||
@@ -47,10 +50,11 @@ func (s *ShortnerSuite) TestShortenNew() {
|
||||
rawURL := []byte("https://google.com")
|
||||
url := []byte("https://google.com/")
|
||||
uid := []byte("ig")
|
||||
urlKey := append([]byte("url:"), fmt.Sprintf("%x", sha1.Sum(url))...)
|
||||
|
||||
s.store.On("Get", append([]byte("url:"), url...)).Return(nil, s.errNotFound)
|
||||
s.store.On("Get", urlKey).Return(nil, s.errNotFound)
|
||||
s.store.On("NextSequence").Return(1001, nil)
|
||||
s.store.On("Set", append([]byte("url:"), url...), uid).Return(nil)
|
||||
s.store.On("Set", urlKey, uid).Return(nil)
|
||||
s.store.On("Set", append([]byte("uid:"), uid...), url).Return(nil)
|
||||
|
||||
rUID, rURL, err := s.shortner.Shorten(rawURL)
|
||||
@@ -99,8 +103,9 @@ func (s *ShortnerSuite) TestShortenInvalidURL() {
|
||||
func (s *ShortnerSuite) TestShortenStoreError() {
|
||||
url := []byte("https://google.com/")
|
||||
storeErr := errors.New("leveldb: something wrong")
|
||||
urlKey := append([]byte("url:"), fmt.Sprintf("%x", sha1.Sum(url))...)
|
||||
|
||||
s.store.On("Get", append([]byte("url:"), url...)).Return(nil, storeErr)
|
||||
s.store.On("Get", urlKey).Return(nil, storeErr)
|
||||
|
||||
rUID, rURL, err := s.shortner.Shorten(url)
|
||||
s.Nil(rUID)
|
||||
|
||||
Reference in New Issue
Block a user