From 6b0244c5529cd43d6f5b09075d56c9e8e3248849 Mon Sep 17 00:00:00 2001 From: Jim Myhrberg Date: Mon, 11 Jul 2016 14:08:59 +0100 Subject: [PATCH] Hash URLs with SHA1 for lookup by URL operations --- shortner/shortner.go | 6 +++++- shortner/shortner_test.go | 13 +++++++++---- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/shortner/shortner.go b/shortner/shortner.go index d2a91c4..3efaa88 100644 --- a/shortner/shortner.go +++ b/shortner/shortner.go @@ -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...) } diff --git a/shortner/shortner_test.go b/shortner/shortner_test.go index ffe31dc..b44bc8c 100644 --- a/shortner/shortner_test.go +++ b/shortner/shortner_test.go @@ -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)