From 90c93b8fa4a94a8f3b0e5095afe48185994a89cf Mon Sep 17 00:00:00 2001 From: Harm Aarts Date: Thu, 5 Dec 2013 15:30:26 +0100 Subject: [PATCH] Adds a database. This is a really simple map. Calls to it just check if the key exists and return a bool. In case of a false it adds the key. The key is just a string which might or might not be sufficient. --- database.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 database.go diff --git a/database.go b/database.go new file mode 100644 index 0000000..9b5a867 --- /dev/null +++ b/database.go @@ -0,0 +1,33 @@ +/* +Credits go to github.com/SlyMarbo/rss for inspiring this solution. +*/ +package feeder + +type database struct { + request chan string + response chan bool + known map[string]struct{} +} + +func (d *database) Run() { + d.known = make(map[string]struct{}) + var s string + + for { + s = <-d.request + if _, ok := d.known[s]; ok { + d.response <- true + } else { + d.response <- false + d.known[s] = struct{}{} + } + } +} + +func NewDatabase() *database { + database := new(database) + database.request = make(chan string) + database.response = make(chan bool) + go database.Run() + return database +}