Permalink
Browse files

persist.LoadAll + UnmarshalerFactory

  • Loading branch information...
jeffjenkins committed Nov 28, 2015
1 parent cc407bc commit 0a44c967cd4061593ba22aaf15cb816d78fdbab3
Showing with 43 additions and 0 deletions.
  1. +7 −0 exchange/exchange.go
  2. +29 −0 persist/persist.go
  3. +7 −0 queue/queue.go
View
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
"github.com/jeffjenkins/mq/amqp"
"github.com/jeffjenkins/mq/binding"
"github.com/jeffjenkins/mq/gen"
@@ -12,6 +13,12 @@ import (
"time"
)
type ExchangeStateFactory struct{}
func (qsf *ExchangeStateFactory) New() proto.Unmarshaler {
return &gen.ExchangeState{}
}
var EXCHANGES_BUCKET_NAME = []byte("exchanges")
const (
View
@@ -6,6 +6,10 @@ import (
"github.com/gogo/protobuf/proto"
)
type UnmarshalerFactory interface {
New() proto.Unmarshaler
}
//
// Persist
//
@@ -94,6 +98,31 @@ func LoadManyBoltTx(bucket *bolt.Bucket, objs map[string]proto.Unmarshaler) erro
return nil
}
func LoadAll(db *bolt.DB, bucket []byte, factory UnmarshalerFactory) (map[string]proto.Unmarshaler, error) {
ret := make(map[string]proto.Unmarshaler)
err := db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(bucket)
if bucket == nil {
return nil
}
// iterate through queues
cursor := bucket.Cursor()
for name, data := cursor.First(); name != nil; name, data = cursor.Next() {
obj := factory.New()
err := obj.Unmarshal(data)
if err != nil {
return fmt.Errorf("Could not unmarshal key %s", string(name))
}
ret[string(name)] = obj
}
return nil
})
if err != nil {
return nil, err
}
return ret, nil
}
//
// Depersist
//
View
@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"github.com/boltdb/bolt"
"github.com/gogo/protobuf/proto"
"github.com/jeffjenkins/mq/amqp"
"github.com/jeffjenkins/mq/consumer"
"github.com/jeffjenkins/mq/gen"
@@ -18,6 +19,12 @@ import (
var QUEUE_BUCKET_NAME = []byte("queues")
type QueueStateFactory struct{}
func (qsf *QueueStateFactory) New() proto.Unmarshaler {
return &gen.QueueState{}
}
type Queue struct {
gen.QueueState
autoDelete bool

0 comments on commit 0a44c96

Please sign in to comment.