func (u *UnitedQueue) MultiPush(key string, datas [][]byte) error {
	key = strings.TrimPrefix(key, "/")
	key = strings.TrimSuffix(key, "/")
	for i, data := range datas {
		if len(data) <= 0 {
			cause := "message " + strconv.Itoa(i) + " has no content"
			return utils.NewError(
				utils.ErrBadRequest,
				cause,
			)
		}
	}
	u.topicsLock.RLock()
	t, ok := u.topics[key]
	u.topicsLock.RUnlock()
	if !ok {
		return utils.NewError(
			utils.ErrTopicNotExisted,
			`queue multiPush`,
		)
	}
	return t.mPush(datas)
}
MultiPush implements MultiPush interface
package queue
import (
	"encoding/binary"
	"errors"
	"log"
	"strconv"
	"strings"
	"sync"
	"time"
	"github.com/buaazp/uq/store"
	"github.com/buaazp/uq/utils"
	"github.com/coreos/go-etcd/etcd"
)
const (
	storageKeyWord   string        = "UnitedQueueKey"
	bgBackupInterval time.Duration = 10 * time.Second
	bgCleanInterval  time.Duration = 20 * time.Second
	bgCleanTimeout   time.Duration = 5 * time.Second
	keyTopicStore    string        = ":store"
	keyTopicHead     string        = ":head"
	keyTopicTail     string        = ":tail"
	keyLineStore     string        = ":store"
	keyLineHead      string        = ":head"
	keyLineRecycle   string        = ":recycle"
	keyLineInflight  string        = ":inflight"
)
type UnitedQueue struct {
	topics     map[string]*topic
	topicsLock sync.RWMutex
	storage    store.Storage
	etcdLock   sync.RWMutex
	selfAddr   string
	etcdClient *etcd.Client
	etcdKey    string
	etcdStop   chan bool
	wg         sync.WaitGroup
}
func NewUnitedQueue(storage store.Storage, ip string, port int, etcdServers []string, etcdKey string) (*UnitedQueue, error) {
	topics := make(map[string]*topic)
	etcdStop := make(chan bool)
	uq := new(UnitedQueue)
	uq.topics = topics
	uq.storage = storage
	uq.etcdStop = etcdStop
	if len(etcdServers) > 0 {
		selfAddr := utils.Addrcat(ip, port)
		uq.selfAddr = selfAddr
		etcdClient := etcd.NewClient(etcdServers)
		uq.etcdClient = etcdClient
		uq.etcdKey = etcdKey
	}
	err := uq.loadQueue()
	if err != nil {
		return nil, err
	}
	go uq.etcdRun()
	return uq, nil
}
func (u *UnitedQueue) setData(key string, data []byte) error {
	err := u.storage.Set(key, data)
	if err != nil {
		return utils.NewError(
			utils.ErrInternalError,
			err.Error(),
		)
	}
	return nil
}
func (u *UnitedQueue) getData(key string) ([]byte, error) {
	data, err := u.storage.Get(key)
	if err != nil {
		return nil, utils.NewError(
			utils.ErrInternalError,
			err.Error(),
		)
	}
	return data, nil
}
func (u *UnitedQueue) delData(key string) error {
	err := u.storage.Del(key)
	if err != nil {
		return utils.NewError(
			utils.ErrInternalError,
			err.Error(),
		)
	}
	return nil
}
func (u *UnitedQueue) exportTopics() error {
	u.topicsLock.RLock()
	defer u.topicsLock.RUnlock()
	for _, t := range u.topics {
		err := t.exportLines()
		if err != nil {
			log.Printf("topic[%s] export lines error: %s", t.name, err)
			continue
		}
		t.linesLock.RLock()
		err = t.exportTopic()
		t.linesLock.RUnlock()
		if err != nil {
			log.Printf("topic[%s] export error: %s", t.name, err)
			continue
		}
	}
	return nil
}
func (u *UnitedQueue) genQueueStore() *UnitedQueueStore {
	topics := make([]string, len(u.topics))
	i := 0
	for topicName := range u.topics {
		topics[i] = topicName
		i++
	}
	qs := new(UnitedQueueStore)
	qs.Topics = topics
	return qs
}
func (u *UnitedQueue) exportQueue() error {
	qs := u.genQueueStore()
	buf, err := qs.Marshal()
	if err != nil {
		return utils.NewError(
			utils.ErrInternalError,
			err.Error(),
		)
	}
	err = u.setData(storageKeyWord, buf)
	if err != nil {
		return err
	}
	return nil
}
func (u *UnitedQueue) loadTopic(topicName string, ts UnitedTopicStore) (*topic, error) {
	t := new(topic)
	t.name = topicName
	t.persist = ts.Persist
	t.q = u
	t.quit = make(chan bool)
	t.headKey = topicName + keyTopicHead
	topicHeadData, err := u.getData(t.headKey)
	if err != nil {
		return nil, err
	}
	t.head = binary.LittleEndian.Uint64(topicHeadData)
	t.tailKey = topicName + keyTopicTail
	topicTailData, err := u.getData(t.tailKey)
	if err != nil {
		return nil, err
	}
	t.tail = binary.LittleEndian.Uint64(topicTailData)
	lines := make(map[string]*line)
	for _, lineName := range ts.Lines {
		lineStoreKey := topicName + "/" + lineName
		lineStoreData, err := u.getData(lineStoreKey)
		if err != nil {
			return nil, err
		}
		if len(lineStoreData) == 0 {
			return nil, errors.New("line backup data missing: " + lineStoreKey)
		}
		var ls UnitedLineStore
		err = ls.Unmarshal(lineStoreData)
		if err != nil {
			return nil, err
		}
		l, err := t.loadLine(lineName, ls)
		if err != nil {
			continue
		}
		lines[lineName] = l
	}
	t.lines = lines
	u.registerTopic(t.name)
	t.start()
	return t, nil
}
func (u *UnitedQueue) loadQueue() error {
	unitedQueueStoreData, err := u.getData(storageKeyWord)
	if err != nil {
		return nil
	}
	if len(unitedQueueStoreData) > 0 {
		var qs UnitedQueueStore
		err := qs.Unmarshal(unitedQueueStoreData)
		if err != nil {
			return err
		}
		for _, topicName := range qs.Topics {
			topicStoreData, err := u.getData(topicName)
			if err != nil {
				return err
			}
			if len(topicStoreData) == 0 {
				return errors.New("topic backup data missing: " + topicName)
			}
			var ts UnitedTopicStore
			err = ts.Unmarshal(topicStoreData)
			if err != nil {
				return err
			}
			t, err := u.loadTopic(topicName, ts)
			if err != nil {
				return err
			}
			u.topicsLock.Lock()
			u.topics[topicName] = t
			u.topicsLock.Unlock()
		}
	}
	return nil
}
func (u *UnitedQueue) newTopic(name string, persist bool) (*topic, error) {
	lines := make(map[string]*line)
	t := new(topic)
	t.name = name
	t.persist = persist
	t.lines = lines
	t.head = 0
	t.headKey = name + keyTopicHead
	t.tail = 0
	t.tailKey = name + keyTopicTail
	t.q = u
	t.quit = make(chan bool)
	err := t.exportHead()
	if err != nil {
		return nil, err
	}
	err = t.exportTail()
	if err != nil {
		return nil, err
	}
	t.start()
	return t, nil
}
func (u *UnitedQueue) createTopic(name string, persist, fromEtcd bool) error {
	u.topicsLock.RLock()
	_, ok := u.topics[name]
	u.topicsLock.RUnlock()
	if ok {
		return utils.NewError(
			utils.ErrTopicExisted,
			`queue createTopic`,
		)
	}
	t, err := u.newTopic(name, persist)
	if err != nil {
		return err
	}
	u.topicsLock.Lock()
	defer u.topicsLock.Unlock()
	u.topics[name] = t
	err = u.exportQueue()
	if err != nil {
		t.remove()
		delete(u.topics, name)
		return err
	}
	if !fromEtcd {
		u.registerTopic(t.name)
	}
	log.Printf("topic[%s] created.", name)
	return nil
}
func (u *UnitedQueue) create(key, arg string, fromEtcd bool) error {
	key = strings.TrimPrefix(key, "/")
	key = strings.TrimSuffix(key, "/")
	var topicName, lineName string
	var err error
	parts := strings.Split(key, "/")
	if len(parts) < 1 || len(parts) > 2 {
		return utils.NewError(
			utils.ErrBadKey,
			`create key parts error: `+utils.ItoaQuick(len(parts)),
		)
	}
	topicName = parts[0]
	if topicName == "" {
		return utils.NewError(
			utils.ErrBadKey,
			`create topic is nil`,
		)
	}
	if len(parts) == 2 {
		lineName = parts[1]
		var recycle time.Duration
		if arg != "" {
			recycle, err = time.ParseDuration(arg)
			if err != nil {
				return utils.NewError(
					utils.ErrBadRequest,
					err.Error(),
				)
			}
		}
		u.topicsLock.RLock()
		t, ok := u.topics[topicName]
		u.topicsLock.RUnlock()
		if !ok {
			return utils.NewError(
				utils.ErrTopicNotExisted,
				`queue create`,
			)
		}
		err = t.createLine(lineName, recycle, fromEtcd)
		if err != nil {
			return err
		}
	} else {
		var persist bool
		if arg == "persist" {
			persist = true
		}
		err = u.createTopic(topicName, persist, fromEtcd)
		if err != nil {
			return err
		}
	}
	return err
}
func (u *UnitedQueue) Create(key, arg string) error {
	return u.create(key, arg, false)
}
func (u *UnitedQueue) Push(key string, data []byte) error {
	key = strings.TrimPrefix(key, "/")
	key = strings.TrimSuffix(key, "/")
	if len(data) <= 0 {
		return utils.NewError(
			utils.ErrBadRequest,
			`message has no content`,
		)
	}
	u.topicsLock.RLock()
	t, ok := u.topics[key]
	u.topicsLock.RUnlock()
	if !ok {
		return utils.NewError(
			utils.ErrTopicNotExisted,
			`queue push`,
		)
	}
	return t.push(data)
}