// NewState constructs a minimal empty state, containing an empty root module.
763727
func NewState() *State {
	modules := map[string]*Module{}
	modules[addrs.RootModuleInstance.String()] = NewModule(addrs.RootModuleInstance)
	return &State{
		Modules: modules,
	}
}
20548
func LoadState(db dbm.DB, version int64) (*State, error) {
	s := NewState(db)
	err := s.writeState.forest.Load(version)
	if err != nil {
		return nil, fmt.Errorf("could not load MutableForest at version %d: %v", version, err)
	}
	// Populate stats. If this starts taking too long, store the value rather than the full scan at startup
	err = s.IterateAccounts(func(acc *acm.Account) error {
		if len(acc.Code) > 0 {
			s.writeState.accountStats.AccountsWithCode++
		} else {
			s.writeState.accountStats.AccountsWithoutCode++
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	// load the validator ring
	ring, err := LoadValidatorRing(version, DefaultValidatorsWindowSize, s.writeState.forest.GetImmutable)
	if err != nil {
		return nil, err
	}
	s.writeState.ring = ring
	s.ReadState.History = ring

	return s, nil
}
259936
func (s *DiskSnapshot) SetState(v string) *DiskSnapshot {
	s.State = &v
	return s
}
899525
func (se *StateEngine) UseState(addr string, s States) States {
	if addr == "" {
		addr = "."
	}

	se.add(addr, s)

	return s
}
1014299
func newState(name string, ops chan<- Operation, newStatePolicy state.NewPolicyFunc) *environState {
	buf := make([]byte, 8192)
	buf = buf[:runtime.Stack(buf, false)]
	s := &environState{
		name:           name,
		ops:            ops,
		newStatePolicy: newStatePolicy,
		insts:          make(map[instance.Id]*dummyInstance),
		creator:        string(buf),
	}
	return s
}
1206351
func shimNewState(newState *states.State, providers map[string]terraform.ResourceProvider) (*terraform.State, error) {
	state := terraform.NewState()

	// in the odd case of a nil state, let the helper packages handle it
	if newState == nil {
		return nil, nil
	}

	for _, newMod := range newState.Modules {
		mod := state.AddModule(newMod.Addr)

		for name, out := range newMod.OutputValues {
			outputType := ""
			val := hcl2shim.ConfigValueFromHCL2(out.Value)
			ty := out.Value.Type()
			switch {
			case ty == cty.String:
				outputType = "string"
			case ty.IsTupleType() || ty.IsListType():
				outputType = "list"
			case ty.IsMapType():
				outputType = "map"
			}

			mod.Outputs[name] = &terraform.OutputState{
				Type:      outputType,
				Value:     val,
				Sensitive: out.Sensitive,
			}
		}

		for _, res := range newMod.Resources {
			resType := res.Addr.Type
			providerType := res.ProviderConfig.ProviderConfig.Type

			resource := getResource(providers, providerType, res.Addr)

			for key, i := range res.Instances {
				flatmap, err := shimmedAttributes(i.Current, resource)
				if err != nil {
					return nil, fmt.Errorf("error decoding state for %q: %s", resType, err)
				}

				resState := &terraform.ResourceState{
					Type: resType,
					Primary: &terraform.InstanceState{
						ID:         flatmap["id"],
						Attributes: flatmap,
						Tainted:    i.Current.Status == states.ObjectTainted,
					},
					Provider: res.ProviderConfig.String(),
				}
				if i.Current.SchemaVersion != 0 {
					resState.Primary.Meta = map[string]interface{}{
						"schema_version": i.Current.SchemaVersion,
					}
				}

				for _, dep := range i.Current.Dependencies {
					resState.Dependencies = append(resState.Dependencies, dep.String())
				}

				// convert the indexes to the old style flapmap indexes
				idx := ""
				switch key.(type) {
				case addrs.IntKey:
					// don't add numeric index values to resources with a count of 0
					if len(res.Instances) > 1 {
						idx = fmt.Sprintf(".%d", key)
					}
				case addrs.StringKey:
					idx = "." + key.String()
				}

				mod.Resources[res.Addr.String()+idx] = resState

				// add any deposed instances
				for _, dep := range i.Deposed {
					flatmap, err := shimmedAttributes(dep, resource)
					if err != nil {
						return nil, fmt.Errorf("error decoding deposed state for %q: %s", resType, err)
					}

					deposed := &terraform.InstanceState{
						ID:         flatmap["id"],
						Attributes: flatmap,
						Tainted:    dep.Status == states.ObjectTainted,
					}
					if dep.SchemaVersion != 0 {
						deposed.Meta = map[string]interface{}{
							"schema_version": dep.SchemaVersion,
						}
					}

					resState.Deposed = append(resState.Deposed, deposed)
				}
			}
		}
	}

	return state, nil
}
547445
func NewState(
	caller base.APICaller,
	authTag names.UnitTag,
) *State {
	facadeCaller := base.NewFacadeCaller(
		caller,
		uniterFacade,
	)
	state := &State{
		ModelWatcher:     common.NewModelWatcher(facadeCaller),
		APIAddresser:     common.NewAPIAddresser(facadeCaller),
		UpgradeSeriesAPI: common.NewUpgradeSeriesAPI(facadeCaller, authTag),
		StorageAccessor:  NewStorageAccessor(facadeCaller),
		facade:           facadeCaller,
		unitTag:          authTag,
	}

	newWatcher := func(result params.NotifyWatchResult) watcher.NotifyWatcher {
		return apiwatcher.NewNotifyWatcher(caller, result)
	}
	state.LeadershipSettings = NewLeadershipSettingsAccessor(
		facadeCaller.FacadeCall,
		newWatcher,
		ErrIfNotVersionFn(2, state.BestAPIVersion()),
	)
	return state
}
664693
func NewStateTransition(evm *vm.EVM, msg Message, gp *GasPool) *StateTransition {
	return &StateTransition{
		gp:       gp,
		evm:      evm,
		msg:      msg,
		gasPrice: msg.GasPrice(),
		value:    msg.Value(),
		data:     msg.Data(),
		state:    evm.StateDB,
	}
}
966421
func NewState(opts ...Options) *LState {
	var ls *LState
	if len(opts) == 0 {
		ls = newLState(Options{
			CallStackSize: CallStackSize,
			RegistrySize:  RegistrySize,
		})
		ls.OpenLibs()
	} else {
		if opts[0].CallStackSize < 1 {
			opts[0].CallStackSize = CallStackSize
		}
		if opts[0].RegistrySize < 128 {
			opts[0].RegistrySize = RegistrySize
		}
		ls = newLState(opts[0])
		if !opts[0].SkipOpenLibs {
			ls.OpenLibs()
		}
	}
	return ls
}
717521
func (s *Filesystem) WriteState(state *states.State) error {
	// TODO: this should use a more robust method of writing state, by first
	// writing to a temp file on the same filesystem, and renaming the file over
	// the original.

	defer s.mutex()()

	if s.readFile == nil {
		err := s.refreshState()
		if err != nil {
			return err
		}
	}

	return s.writeState(state, nil)
}
652497
func (s *WorkGroupSummary) SetState(v string) *WorkGroupSummary {
	s.State = &v
	return s
}
889761
func newStatefulSets(c *AppsV1beta1Client, namespace string) *statefulSets {
	return &statefulSets{
		client: c.RESTClient(),
		ns:     namespace,
	}
}
1359809
func (c *upgradeContext) State() StateBackend {
	if c.st == nil {
		panic("State not available from this context")
	}
	return c.st
}
560820
func (c *ContainerInit) changeState(state State, err string, exitStatus int) {
	if err != "" {
		logger.Debug("changing state", "fn", "changeState", "state", state, "err", err)
	} else if exitStatus != -1 {
		logger.Debug("changing state", "fn", "changeState", "state", state, "exitStatus", exitStatus)
	} else {
		logger.Debug("changing state", "fn", "changeState", "state", state)
	}

	c.state = state
	c.error = err
	c.exitStatus = exitStatus

	c.streamsMtx.RLock()
	defer c.streamsMtx.RUnlock()
	for ch := range c.streams {
		ch <- StateChange{State: state, Error: err, ExitStatus: exitStatus}
	}
}
138384
func NewStateBackend(st *state.State) (Backend, error) {
	m, err := st.Model()
	if err != nil {
		return nil, err
	}
	if m.Type() != state.ModelTypeIAAS {
		return nil, errors.NotSupportedf("Firewall Rules for non-IAAS models")
	}
	return &stateShim{
		State: st,
		Model: m,
	}, nil
}
1105458
func (s *State) Empty() bool {
	if s == nil {
		return true
	}
	s.Lock()
	defer s.Unlock()

	return len(s.Modules) == 0
}
232533
func NewStateMachine(p *Plugin) *StateMachine {
	sm := StateMachine{
		state:  0,
		plugin: p,
	}
	sm.states = map[string]int{}
	sm.resetFn = func(*Msg) {}
	return &sm
}