kunt

golang IRC bot
git clone git://git.2f30.org/kunt.git
Log | Files | Refs | LICENSE

commit a01240a6834260d743ad092573590de66d375fd6
parent ca96dc547a1dea7ced8003b6b1062a78455e0997
Author: sin <sin@2f30.org>
Date:   Sat Apr 27 17:29:49 +0100

fsdb: Update fsdb

Diffstat:
src/fsdb/fsdb.go | 48+++++++++++++++++++++++++++++++++++-------------
src/kunt/kunt.go | 20++++++++++++++++++--
2 files changed, 53 insertions(+), 15 deletions(-)
diff --git a/src/fsdb/fsdb.go b/src/fsdb/fsdb.go @@ -68,9 +68,9 @@ func (d *Fsdb) Sync() error { if err != nil { return err } + d.dbMap[k] = fsdbVal{false, v.data} fmt.Printf("Synced entry (%d, %v)\n", k, v.data) - d.dbMap[k] = fsdbVal{false, v.data} } } return nil @@ -82,12 +82,27 @@ func (d *Fsdb) String() string { defer d.lock.Unlock() s := fmt.Sprintf("*** %s DB DUMP ***\n", d.name) for k, v := range d.dbMap { - s += fmt.Sprintf("k: %d -> v: %v\n", - k, v) + dirty := "" + if v.dirty { + dirty = "yes" + } else { + dirty = "no" + } + s += fmt.Sprintf("k: %d - dirty: %s\n", + k, dirty) } return s } +// Mark all entries as synced but do not actually write them out to disk +func (d *Fsdb) virtSync() { + d.lock.Lock() + defer d.lock.Unlock() + for k, v := range d.dbMap { + d.dbMap[k] = fsdbVal{false, v.data} + } +} + // Load the DB from disk func (d *Fsdb) Load() error { _, err := os.Stat(d.path) @@ -112,18 +127,27 @@ func (d *Fsdb) Load() error { if err != nil { return err } - err = d.putIdx(i, b, false) + if d.Dup(b) { + return fmt.Errorf("Duplicate entry: %s", b) + } + err = d.Put(i, b) if err != nil { return err } return nil } - return filepath.Walk(d.path, walkFunc) + err = filepath.Walk(d.path, walkFunc) + if err == nil { + d.virtSync() + } + return err } // Check if `buf' already exists in the DB // Expects `d.lock' to be held by caller -func (d *Fsdb) dup(buf []byte) bool { +func (d *Fsdb) Dup(buf []byte) bool { + d.lock.Lock() + defer d.lock.Unlock() for _, v := range d.dbMap { if string(v.data) == string(buf) { return true @@ -132,16 +156,17 @@ func (d *Fsdb) dup(buf []byte) bool { return false } -func (d *Fsdb) putIdx(key int, buf []byte, dirty bool) error { +func (d *Fsdb) Put(key int, buf []byte) error { d.lock.Lock() defer d.lock.Unlock() if key < 0 { return fmt.Errorf("Invalid key: %d", key) } - if d.dup(buf) { - return fmt.Errorf("Duplicate entry: %s", buf) + _, ok := d.dbMap[key] + if ok { + return fmt.Errorf("Key %d already in use", key) } - d.dbMap[key] = fsdbVal{dirty, buf} + d.dbMap[key] = fsdbVal{true, buf} return nil } @@ -149,9 +174,6 @@ func (d *Fsdb) putIdx(key int, buf []byte, dirty bool) error { func (d *Fsdb) Append(buf []byte) (int, error) { d.lock.Lock() defer d.lock.Unlock() - if d.dup(buf) { - return -1, fmt.Errorf("Duplicate entry: %s", buf) - } d.dbMap[len(d.dbMap)] = fsdbVal{true, buf} return -1, nil } diff --git a/src/kunt/kunt.go b/src/kunt/kunt.go @@ -120,6 +120,11 @@ func cmdAddQuote(msg irc.IrcMessage) { s = strings.TrimSpace(s) s += "\n" buf := []byte(s) + if quoteDb.Dup(buf) { + kunt.ircCtx.SendPrivmsg(msg.Params[0], + fmt.Errorf("Duplicate entry: %s", buf).Error()) + return + } _, err := quoteDb.Append(buf) if err != nil { kunt.ircCtx.SendPrivmsg(msg.Params[0], err.Error()) @@ -165,6 +170,11 @@ func cmdAddUrl(msg irc.IrcMessage) { s = strings.TrimSpace(s) s += "\n" buf := []byte(s) + if urlDb.Dup(buf) { + kunt.ircCtx.SendPrivmsg(msg.Params[0], + fmt.Errorf("Duplicate entry: %s", buf).Error()) + return + } _, err := urlDb.Append(buf) if err != nil { kunt.ircCtx.SendPrivmsg(msg.Params[0], err.Error()) @@ -274,8 +284,14 @@ func main() { quoteDb = fsdb.NewFsdb("Quotes", "db/quotes", "quote") urlDb = fsdb.NewFsdb("Urls", "db/urls", "url") - quoteDb.Load() - urlDb.Load() + err := quoteDb.Load() + if err != nil { + log.Fatal(err) + } + err = urlDb.Load() + if err != nil { + log.Fatal(err) + } rand.Seed(time.Now().UnixNano())