package raftNode

//import (
//	"encoding/json"
//	"fmt"
//	"github.com/hashicorp/raft"
//	"io"
//	"io/ioutil"
//	"sync"
//)
//
////fsmApplyCmd ...
//type fsmApplyCmd struct {
//	Tree   string      `json:"tree"`
//	Key    string      `json:"key"`
//	Action string      `json:"action"`
//	Data   interface{} `json:"data"`
//}
//
//func NewRaftFsm() *raftFsm {
//	return &raftFsm{
//		dataTreeMp: map[string]*TreeData{},
//	}
//}
//
////raftFsm 跟随者
//type raftFsm struct {
//	lock       sync.Mutex   //互斥锁
//	rwLock     sync.RWMutex //读写锁
//	dataTreeMp map[string]*TreeData
//	Data       database
//}
//
//func (that *raftFsm) getTreeData(treeName ...string) *TreeData {
//	that.lock.Lock()
//	defer that.lock.Unlock()
//	_treeName := "default"
//	if len(treeName) > 0 {
//		_treeName = treeName[0]
//	}
//	_dataTree, _ok := that.dataTreeMp[_treeName]
//	if !_ok {
//		_dataTree = &TreeData{
//			Data: map[string]interface{}{},
//			lock: new(sync.RWMutex),
//		}
//		that.dataTreeMp[_treeName] = _dataTree
//	}
//	return _dataTree
//}
//
//func (that *raftFsm) List() []interface{} {
//	that.rwLock.RLock()
//	defer that.rwLock.RUnlock()
//	_slice := []interface{}{}
//	for _, _v := range that.dataTreeMp {
//		_slice = append(_slice, _v.List()...)
//	}
//	return _slice
//}
//
//func (that *raftFsm) Find(tree, key string) interface{} {
//	_treeData := that.getTreeData(tree).Get(key)
//	return _treeData
//}
//
////Apply flower同步写入
//func (that *raftFsm) Apply(l *raft.Log) interface{} {
//	that.rwLock.RLock()
//	defer that.rwLock.RUnlock()
//
//	_entry, _err := parseEntry(l.Data)
//	if _err != nil {
//		fmt.Println(string(l.Data), "====== raftFsm.Apply error 001 ======", _err)
//		return nil
//	}
//	_key := _entry.GetKey()
//	_val := _entry.GetVal()
//	//
//	_entryKey, _err := _entry.GetEntryKey()
//	if _err != nil {
//		fmt.Println(string(l.Data), "====== raftFsm.Apply error 002 ======", _err)
//		return nil
//	}
//	_tree := _entryKey.GetTree()
//	_treeName := "default"
//	if _tree != "" {
//		_treeName = _entryKey.GetTree()
//	}
//	//
//	_treeData := that.getTreeData(_treeName)
//	if _val == "" {
//		_treeData.Del(_key)
//		return nil
//	}
//	//
//	//fmt.Println(string(l.Data), "====== raftFsm.Apply ======", _treeName, _key, _val)
//	_treeData.Set(_key, _val)
//	return nil
//
//}
//
////Snapshot 返回快照接口类型
//func (that *raftFsm) Snapshot() (raft.FSMSnapshot, error) {
//	that.rwLock.Lock()
//	defer that.rwLock.Unlock()
//	that.Data = map[string]interface{}{}
//	for _k, _v := range that.dataTreeMp {
//		that.Data[_k] = _v.Copy()
//	}
//	return that.Data, nil
//}
//
////Restore 从快照中恢复到内存
//func (that *raftFsm) Restore(r io.ReadCloser) error {
//	that.rwLock.Lock()
//	defer that.rwLock.Unlock()
//	_byte, _err := ioutil.ReadAll(r)
//	if _err != nil {
//		fmt.Println("==========Restore err==========", _err.Error())
//	}
//	_data := map[string]interface{}{}
//	if _err = json.Unmarshal(_byte, &_data); _err != nil {
//		fmt.Println("==========Restore Unmarshal err==========", _err.Error())
//	}
//	for _k, _v := range _data {
//		_dataTree := that.getTreeData(_k)
//		_dataTree.Restore(_v.(map[string]interface{}))
//	}
//	return nil
//}
//
//type database map[string]interface{}
//
////Persist 生成快照
//func (that database) Persist(sink raft.SnapshotSink) error {
//	_byte, _err := json.Marshal(that)
//	if _err != nil {
//		fmt.Println("==========Persist err==========", _err.Error())
//		return _err
//	}
//	sink.Write(_byte)
//	sink.Close()
//	return nil
//}
//
//func (that database) Release() {
//}
