-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds deduplicator to avoid duplicate urls scrape
- Loading branch information
Showing
9 changed files
with
346 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package deduper | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
) | ||
|
||
type Deduper interface { | ||
AddIfNotExists(context.Context, string) bool | ||
} | ||
|
||
func New() Deduper { | ||
return &hashmap{ | ||
seen: make(map[uint64]struct{}), | ||
mux: &sync.RWMutex{}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package deduper | ||
|
||
import ( | ||
"context" | ||
"hash/fnv" | ||
"sync" | ||
) | ||
|
||
var _ Deduper = (*hashmap)(nil) | ||
|
||
type hashmap struct { | ||
mux *sync.RWMutex | ||
seen map[uint64]struct{} | ||
} | ||
|
||
func (d *hashmap) AddIfNotExists(_ context.Context, key string) bool { | ||
d.mux.RLock() | ||
if _, ok := d.seen[d.hash(key)]; ok { | ||
d.mux.RUnlock() | ||
return false | ||
} | ||
|
||
d.mux.RUnlock() | ||
|
||
d.mux.Lock() | ||
defer d.mux.Unlock() | ||
|
||
if _, ok := d.seen[d.hash(key)]; ok { | ||
return false | ||
} | ||
|
||
d.seen[d.hash(key)] = struct{}{} | ||
|
||
return true | ||
} | ||
|
||
func (d *hashmap) hash(key string) uint64 { | ||
h := fnv.New64() | ||
h.Write([]byte(key)) | ||
|
||
return h.Sum64() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters