func (s *Storage) Switch(i int) error {
	err := s.SetAD(i)
	if err != nil {
		return wrapErr(err)
	}
	return nil
}
Switch will change Active Document (AD) to the given index
package db
import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"strings"
	"sync"
	e "github.com/ulfox/dby/errors"
	"gopkg.in/yaml.v2"
)
type erf = func(e interface{}, p ...interface{}) error
var wrapErr erf = e.WrapErr
type Storage struct {
	sync.Mutex
	*state
	SQL  *SQL
	Path string
	mem  bool
}
func NewStorageFactory(p ...interface{}) (*Storage, error) {
	var path string = "local/dby.yaml"
	var inMem bool = true
	if len(p) > 0 {
		switch i := p[0].(type) {
		case string:
			path = i
			inMem = false
		case bool:
			inMem = i
		}
	}
	state := &Storage{
		SQL:   NewSQLFactory(),
		state: newStateFactory(),
		Path:  path,
		mem:   inMem,
	}
	err := state.dbinit()
	if err != nil {
		return nil, wrapErr(err)
	}
	return state, nil
}
func (s *Storage) Close() error {
	if !s.mem {
		err := s.Write()
		if err != nil {
			return wrapErr(err)
		}
	}
	s.Clear()
	s.SQL.Clear()
	return nil
}
func (s *Storage) dbinit() error {
	if s.mem {
		s.PushData(emptyMap())
		s.SetAD(0)
		return nil
	}
	stateDir := filepath.Dir(s.Path)
	err := makeDirs(stateDir, 0700)
	if err != nil {
		return wrapErr(err)
	}
	stateExists, err := fileExists(s.Path)
	if err != nil {
		return wrapErr(err)
	}
	if !stateExists {
		s.PushData(emptyMap())
		s.SetAD(0)
		err = s.Write()
		if err != nil {
			return wrapErr(err)
		}
	}
	err = s.Read()
	if err != nil {
		return wrapErr(err)
	}
	return nil
}
func (s *Storage) SetNames(f, l string) error {
	for i := range s.GetAllData() {
		s.SetAD(i)
		kind, err := s.GetPath(strings.ToLower(f))
		if err != nil {
			continue
		}
		name, err := s.GetPath(strings.ToLower(l))
		if err != nil {
			continue
		}
		sKind, ok := kind.(string)
		if !ok {
			wrapErr(fieldNotString, strings.ToLower(f), kind)
		}
		sName, ok := name.(string)
		if !ok {
			wrapErr(fieldNotString, strings.ToLower(l), name)
		}
		err = s.addDoc(
			fmt.Sprintf(
				"%s/%s",
				strings.ToLower(sKind),
				strings.ToLower(sName),
			),
			i,
		)
		if err != nil {
			return wrapErr(err)
		}
	}
	return nil
}
func (s *Storage) SetName(n string, i int) error {
	err := s.Switch(i)
	if err != nil {
		return wrapErr(err)
	}
	err = s.addDoc(n, i)
	if err != nil {
		return wrapErr(err)
	}
	return nil
}
func (s *Storage) DeleteDoc(i int) error {
	err := s.DeleteData(i)
	if err != nil {
		return wrapErr(err)
	}
	return nil
}