Skip to content

Commit 27eac07

Browse files
Use Boltdb directly.
1 parent 7dbc5fc commit 27eac07

File tree

4 files changed

+82
-41
lines changed

4 files changed

+82
-41
lines changed

config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"regexp"
88
"time"
99

10-
"github.com/asggo/store"
10+
"github.com/boltdb/bolt"
1111
)
1212

1313
type Keyword struct {
@@ -24,7 +24,7 @@ type Regex struct {
2424

2525
type Config struct {
2626
keys map[string]time.Time
27-
ds *store.Store
27+
db *bolt.DB
2828
Keywords []*Keyword // A list of keywords to search for in the data.
2929
Regexes []*Regex // A list of regular expressions to test against data.
3030
Buckets []string `json:"buckets"` // List of buckets we need to create.

process.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func savePaste(key, content string) {
1414
return
1515
}
1616

17-
conf.ds.Write("pastes", key, []byte(content))
17+
writeDB(conf.db, "pastes", key, []byte(content))
1818
}
1919

2020
func processRegexes(key, content string) {
@@ -32,15 +32,15 @@ func processRegexes(key, content string) {
3232

3333
for k := range items {
3434
rKey := fmt.Sprintf("%s-%s-%d", r.Prefix, key, k)
35-
conf.ds.Write("regexes", rKey, []byte(items[k]))
35+
writeDB(conf.db, "regexes", rKey, []byte(items[k]))
3636
}
3737
case "one":
3838
match := r.compiled.FindString(content)
3939
rKey := fmt.Sprintf("%s-%s", r.Prefix, key)
4040

4141
if match != "" {
4242
save = true
43-
conf.ds.Write("regexes", rKey, []byte(match))
43+
writeDB(conf.db, "regexes", rKey, []byte(match))
4444
}
4545
default:
4646
}
@@ -59,7 +59,7 @@ func processKeywords(key, content string) {
5959

6060
if strings.Contains(strings.ToLower(content), strings.ToLower(kwd.Keyword)) {
6161
save = true
62-
conf.ds.Write("keywords", kwdKey, []byte(key))
62+
writeDB(conf.db, "keywords", kwdKey, []byte(key))
6363
}
6464
}
6565

@@ -68,10 +68,9 @@ func processKeywords(key, content string) {
6868
}
6969
}
7070

71-
7271
func processContent(key, content string) {
73-
conf.ds = getStoreConn()
74-
defer conf.ds.Close()
72+
conf.db = getDBConn()
73+
defer conf.db.Close()
7574

7675
processRegexes(key, content)
7776
processKeywords(key, content)

scrape.go

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,11 @@
11
package main
22

33
import (
4-
"log"
54
"time"
6-
7-
"github.com/asggo/store"
85
)
96

107
var conf Config
118

12-
func getStoreConn() *store.Store {
13-
var ds *store.Store
14-
var err error
15-
16-
for tries := 1; tries < 20; tries += 2 {
17-
// Create our connection to the key value store.
18-
ds, err = store.NewStore(conf.DbFile)
19-
if err != nil {
20-
log.Printf("[-] Cannot open database: %s\n", err)
21-
time.Sleep(1 << uint(tries) * time.Millisecond)
22-
} else {
23-
break
24-
}
25-
}
26-
27-
return ds
28-
}
29-
30-
func initStore(s *store.Store) {
31-
for _, bucket := range conf.Buckets {
32-
s.CreateBucket(bucket)
33-
}
34-
}
35-
369
func cleanKeys() {
3710
now := time.Now()
3811
max := time.Duration(conf.MaxTime) * time.Second
@@ -51,11 +24,7 @@ func scrape() {
5124

5225
func main() {
5326
conf = newConfig()
54-
55-
// Ensure our database is initialized.
56-
ds := getStoreConn()
57-
initStore(ds)
58-
ds.Close()
27+
initDB()
5928

6029
for {
6130
scrape()

store.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"time"
6+
7+
"github.com/boltdb/bolt"
8+
)
9+
10+
func getDBConn() *bolt.DB {
11+
for tries := 1; tries < 20; tries += 2 {
12+
timeout := 1 << uint(tries) * time.Millisecond
13+
14+
db, err := bolt.Open(conf.DbFile, 0640, &bolt.Options{Timeout: timeout})
15+
if err == nil {
16+
return db
17+
}
18+
19+
log.Printf("[-] Database locked waiting...\n")
20+
}
21+
22+
return nil
23+
}
24+
25+
func initDB() {
26+
db := getDBConn()
27+
28+
for _, bucket := range conf.Buckets {
29+
createBucket(db, bucket)
30+
}
31+
32+
db.Close()
33+
}
34+
35+
func createBucket(db *bolt.DB, bucket string) error {
36+
return db.Update(func(tx *bolt.Tx) error {
37+
_, err := tx.CreateBucketIfNotExists([]byte(bucket))
38+
if err != nil {
39+
return err
40+
}
41+
42+
return nil
43+
})
44+
}
45+
46+
func writeDB(db *bolt.DB, bucket, key string, value []byte) error {
47+
return db.Update(func(tx *bolt.Tx) error {
48+
b := tx.Bucket([]byte(bucket))
49+
50+
return b.Put([]byte(key), []byte(value))
51+
})
52+
}
53+
54+
func readDB(db *bolt.DB, bucket, key string) []byte {
55+
var val []byte
56+
57+
db.View(func(tx *bolt.Tx) error {
58+
b := tx.Bucket([]byte(bucket))
59+
val = b.Get([]byte(key))
60+
61+
return nil
62+
})
63+
64+
return val
65+
}
66+
67+
func deleteDB(db *bolt.DB, bucket, key string) error {
68+
return db.Update(func(tx *bolt.Tx) error {
69+
b := tx.Bucket([]byte(bucket))
70+
71+
return b.Delete([]byte(key))
72+
})
73+
}

0 commit comments

Comments
 (0)