Created
February 24, 2016 15:46
-
-
Save conoro/942106c4ab1e4fa26350 to your computer and use it in GitHub Desktop.
Test Stow + BoltDB in Golang
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
package main | |
import ( | |
// "hash/fnv" | |
"fmt" | |
"github.com/conoro/bolt" | |
"github.com/djherbis/stow" | |
"github.com/k0kubun/pp" | |
//"github.com/twinj/uuid" | |
) | |
type crawlFeed struct { | |
LastPostURL string | |
LastPostTitle string | |
LastPostContent string | |
LastPostDate string | |
LastCrawlTime string | |
LastCrawlStatus string | |
} | |
type subscription struct { | |
UserID string | |
ChannelID string | |
RSSUrl string | |
LastPostedURL string | |
Status string | |
} | |
//func hash(s string) uint32 { | |
// h := fnv.New32a() | |
// h.Write([]byte(s)) | |
// return h.Sum32() | |
//} | |
func main() { | |
// Open Database | |
db, err := bolt.Open("my.db", 0777, nil) | |
if err != nil { | |
fmt.Println(err) | |
} | |
fmt.Println("Got to here") | |
defer db.Close() | |
// Create a Store | |
// you will need to do gob.Register(Name{}) if you wish to use NewStore | |
crawlStore := stow.NewJSONStore(db, []byte("crawlstore")) | |
subscriptionStore := stow.NewJSONStore(db, []byte("subscriptionstore")) | |
// Put a few URLs in the crawlStore | |
crawlStore.Put([]byte("http://www.example1.com/feed"), &crawlFeed{"http://www.example1.com/feed/story1", "Feed 1 Story 1", "This is Story 1 from site 1 yall", "2016-02-02", "2016-02-04T15:03:05.999999999Z07:00", "success"}) | |
crawlStore.Put([]byte("http://www.example2.com/feed"), &crawlFeed{"http://www.example2.com/feed/story2", "Feed 2 Story 2", "This is Story 2 from site 2 yall", "2016-01-03", "2016-02-04T15:04:05.999999999Z07:00", "success"}) | |
crawlStore.Put([]byte("http://www.example3.com/feed"), &crawlFeed{"http://www.example3.com/feed/story1", "Feed 3 Story 1", "This is Story 1 from site 3 yall", "2016-01-07", "2016-02-04T15:05:05.999999999Z07:00", "success"}) | |
// Put a few subs in the subscriptionStore | |
simpleIndex := "channelid:" + "999543" + "rssurl:" + "http://www.example1.com/feed" | |
pp.Println(simpleIndex) | |
subscriptionStore.Put([]byte(simpleIndex), &subscription{"000987", "999543", "http://www.example1.com/feed", "http://www.example1.com/feed/story1", "active"}) | |
//hashIndex := strconv.FormatUint(uint64(hash(simpleIndex)), 10) | |
//pp.Println(hashIndex) | |
//subscriptionStore.Put([]byte(hashIndex), &subscription{"000987", "999544", "http://www.example2.com/feed", "http://www.example2.com/feed/story2", "active"}) | |
//UUIDIndex := uuid.NewV4().String() | |
//pp.Println(UUIDIndex) | |
//subscriptionStore.Put([]byte(UUIDIndex), &subscription{"111234", "999543", "http://www.example3.com/feed", "http://www.example3.com/feed/story1", "active"}) | |
// Get the data back | |
var feed1 crawlFeed | |
if crawlStore.Get([]byte("http://www.example1.com/feed"), &feed1) != stow.ErrNotFound { | |
pp.Println(feed1) | |
} | |
if crawlStore.Get([]byte("http://www.example2.com/feed"), &feed1) != stow.ErrNotFound { | |
pp.Println(feed1) | |
} | |
if crawlStore.Get([]byte("http://www.example3.com/feed"), &feed1) != stow.ErrNotFound { | |
pp.Println(feed1) | |
} | |
err = subscriptionStore.ForEach(func(singleSub subscription) { | |
pp.Print(singleSub) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment