// GlobalStore creates an Option to set a key to a certain value in
// the globalStore.
967001
func GlobalStore(key string, value interface{}) Option {
	return func(p *parser) Option {
		old := p.cur.globalStore[key]
		p.cur.globalStore[key] = value
		return GlobalStore(key, old)
	}
}
583967
func (c Config) SetGlobal(option GlobalOption, value string) {
	if globals, ok := c[Globals]; ok {
		if settings, ok := globals.(map[GlobalOption]string); ok {
			settings[option] = value
		}
	}
}
30182
func (c Config) GetGlobal(option GlobalOption) (string, error) {
	if globals, ok := c[Globals]; ok {
		if settings, ok := globals.(map[GlobalOption]string); ok {
			if value, ok := settings[option]; ok {
				return value, nil
			}
			return "", fmt.Errorf("global setting for %s not found", option)
		}
	}
	return "", fmt.Errorf("no global config options found")

}
516661
func SetGlobalVariable(endpoint, key string, value interface{}) error {
	var inst *Instance
	var exists bool
	if inst, exists = connectionPool[endpoint]; !exists {
		return errNotRegistered
	}
	if !globalKeyExp.MatchString(key) {
		return errKeyInvalid
	}
	if _, err := inst.connection.Exec(fmt.Sprintf("SET GLOBAL %s=?", key), value); err != nil {
		return err
	}
	return nil
}
901394
func NewSecretStore(g *GlobalContext, username NormalizedUsername) SecretStore {
	store := g.SecretStore()
	if store != nil {
		return &SecretStoreImp{
			username: username,
			store:    store,
		}
	}
	return nil
}
149877
func (g *Goon) Key(src interface{}) *datastore.Key {
	if k, err := g.KeyError(src); err == nil {
		return k
	}
	return nil
}
1293921
func DefaultStoreOptions(rootless bool, rootlessUid int) (StoreOptions, error) {
	var (
		defaultRootlessRunRoot   string
		defaultRootlessGraphRoot string
		err                      error
	)
	storageOpts := defaultStoreOptions
	if rootless && rootlessUid != 0 {
		storageOpts, err = getRootlessStorageOpts(rootlessUid)
		if err != nil {
			return storageOpts, err
		}
	}

	storageConf, err := DefaultConfigFile(rootless && rootlessUid != 0)
	if err != nil {
		return storageOpts, err
	}
	if _, err = os.Stat(storageConf); err == nil {
		defaultRootlessRunRoot = storageOpts.RunRoot
		defaultRootlessGraphRoot = storageOpts.GraphRoot
		storageOpts = StoreOptions{}
		ReloadConfigurationFile(storageConf, &storageOpts)
	}

	if !os.IsNotExist(err) {
		return storageOpts, errors.Wrapf(err, "cannot stat %s", storageConf)
	}

	if rootless && rootlessUid != 0 {
		if err == nil {
			// If the file did not specify a graphroot or runroot,
			// set sane defaults so we don't try and use root-owned
			// directories
			if storageOpts.RunRoot == "" {
				storageOpts.RunRoot = defaultRootlessRunRoot
			}
			if storageOpts.GraphRoot == "" {
				storageOpts.GraphRoot = defaultRootlessGraphRoot
			}
		} else {
			if err := os.MkdirAll(filepath.Dir(storageConf), 0755); err != nil {
				return storageOpts, errors.Wrapf(err, "cannot make directory %s", filepath.Dir(storageConf))
			}
			file, err := os.OpenFile(storageConf, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
			if err != nil {
				return storageOpts, errors.Wrapf(err, "cannot open %s", storageConf)
			}

			tomlConfiguration := getTomlStorage(&storageOpts)
			defer file.Close()
			enc := toml.NewEncoder(file)
			if err := enc.Encode(tomlConfiguration); err != nil {
				os.Remove(storageConf)

				return storageOpts, errors.Wrapf(err, "failed to encode %s", storageConf)
			}
		}
	}
	return storageOpts, nil
}
419900
func (s *GlobalStore) Keys(startKey []byte, limit int) (keys mock.Keys, err error) {
	err = s.client.Call(&keys, "mockStore_keys", startKey, limit)
	return keys, err
}
1315663
func (l *State) Global(name string) {
	g := l.global.registry.atInt(RegistryIndexGlobals)
	l.push(name)
	l.stack[l.top-1] = l.tableAt(g, l.stack[l.top-1])
}
995978
func SetGlobalExternalKeyStore(s UnsafeExternalKeyStore) {
	externalKeyStoreMu.Lock()
	defer externalKeyStoreMu.Unlock()
	externalKeyStore = TypeSafeExternalKeyStoreProxy{s}
	externalKeyStoreInitialized = false
}
1030246
func NewPGStore(dbURL string, keyPairs ...[]byte) (*PGStore, error) {
	db, err := sql.Open("postgres", dbURL)
	if err != nil {
		// Ignore and return nil.
		return nil, err
	}
	return NewPGStoreFromPool(db, keyPairs...)
}
521467
func (b *Builder) WithStore(store store.Store) *Builder {
	b.settings.Store = store
	return b
}
1314989
func DeleteGlobal(key string) error {
	collections.globalMutex.Lock()
	obj := collections.globals[key]
	collections.globalMutex.Unlock()
	if obj == nil {
		log.Errorf("global %s not found", key)
		return errors.New("global not found")
	}

	// Check if we handle this object
	if objCallbackHandler.GlobalCb == nil {
		log.Errorf("No callback registered for global object")
		return errors.New("Invalid object type")
	}

	// Perform callback
	err := objCallbackHandler.GlobalCb.GlobalDelete(obj)
	if err != nil {
		log.Errorf("GlobalDelete retruned error for: %+v. Err: %v", obj, err)
		return err
	}

	// delete it from modeldb
	collections.globalMutex.Lock()
	err = obj.Delete()
	collections.globalMutex.Unlock()
	if err != nil {
		log.Errorf("Error deleting global %s. Err: %v", obj.Key, err)
	}

	// delete it from cache
	collections.globalMutex.Lock()
	delete(collections.globals, key)
	collections.globalMutex.Unlock()

	return nil
}
1077186
func (fkvs *FileKeyValueStore) Store(key interface{}, value interface{}) error {
	if key == nil {
		return errors.New("key is nil")
	}
	if value == nil {
		return errors.New("value is nil")
	}
	file, err := fkvs.keySerializer(key)
	if err != nil {
		return err
	}
	valueBytes, err := fkvs.marshaller(value)
	if err != nil {
		return err
	}
	err = os.MkdirAll(filepath.Dir(file), newDirMode)
	if err != nil {
		return err
	}
	return ioutil.WriteFile(file, valueBytes, newFileMode)
}
774977
func (o *ClientOptions) SetStore(s Store) *ClientOptions {
	o.Store = s
	return o
}
600123
func (s *GlobalStore) lock(addr common.Address, key []byte) (unlock func(), err error) {
	start := time.Now()
	nodeLockKey := addr.Hex()
	for {
		_, loaded := s.nodesLocks.LoadOrStore(nodeLockKey, struct{}{})
		if !loaded {
			break
		}
		time.Sleep(lockCheckDelay)
		if time.Since(start) > lockTimeout {
			return nil, errLockTimeout
		}
	}
	start = time.Now()
	keyLockKey := common.Bytes2Hex(key)
	for {
		_, loaded := s.keysLocks.LoadOrStore(keyLockKey, struct{}{})
		if !loaded {
			break
		}
		time.Sleep(lockCheckDelay)
		if time.Since(start) > lockTimeout {
			return nil, errLockTimeout
		}
	}
	return func() {
		s.nodesLocks.Delete(nodeLockKey)
		s.keysLocks.Delete(keyLockKey)
	}, nil
}
549048
func (l *State) SetGlobal(name string) {
	l.checkElementCount(1)
	g := l.global.registry.atInt(RegistryIndexGlobals)
	l.push(name)
	l.setTableAt(g, l.stack[l.top-1], l.stack[l.top-2])
	l.top -= 2 // pop value and key
}