// GetOrCreate implements the NodeCache interface for nodeCacheStandard.
168580
func (ncs *nodeCacheStandard) GetOrCreate(
	ptr data.BlockPointer, name string, parent Node, et data.EntryType) (
	n Node, err error) {
	var rootWrappers []func(Node) Node
	defer func() {
		if n != nil {
			n = ncs.wrapNodeStandard(n, rootWrappers, parent)
		}
	}()

	if !ptr.IsValid() {
		// Temporary code to track down bad block
		// pointers. Remove when not needed anymore.
		panic(InvalidBlockRefError{ptr.Ref()})
	}

	if name == "" {
		return nil, EmptyNameError{ptr.Ref()}
	}

	ncs.lock.Lock()
	defer ncs.lock.Unlock()
	rootWrappers = ncs.rootWrappers
	entry, ok := ncs.nodes[ptr.Ref()]
	if ok {
		// If the entry happens to be unlinked, we may be in a
		// situation where a node got unlinked and then recreated, but
		// someone held onto a node the whole time and so it never got
		// removed from the cache.  In that case, forcibly remove it
		// from the cache to make room for the new node.
		if parent != nil && entry.core.parent == nil {
			delete(ncs.nodes, ptr.Ref())
		} else {
			return ncs.makeNodeStandardForEntryLocked(entry), nil
		}
	}

	if parent != nil {
		// Make sure a child can be made for this parent.
		_, err := ncs.newChildForParentLocked(parent)
		if err != nil {
			return nil, err
		}
	}

	entry = &nodeCacheEntry{
		core: newNodeCore(ptr, name, parent, ncs, et),
	}
	ncs.nodes[ptr.Ref()] = entry
	return ncs.makeNodeStandardForEntryLocked(entry), nil
}
679853
func (c *client) UserCreate(in *User) (*User, error) {
	out := new(User)
	uri := fmt.Sprintf(pathUsers, c.addr)
	err := c.post(uri, in, out)
	return out, err
}
174279
func (r *RepositoryRelease) GetCreatedAt() Timestamp {
	if r == nil || r.CreatedAt == nil {
		return Timestamp{}
	}
	return *r.CreatedAt
}
374332
func (tc *TabletStatsCache) getOrCreateEntry(target *querypb.Target) *tabletStatsCacheEntry {
	// Fast path (most common path too): Read-lock, return the entry.
	if e := tc.getEntry(target.Keyspace, target.Shard, target.TabletType); e != nil {
		return e
	}

	// Slow path: Lock, will probably have to add the entry at some level.
	tc.mu.Lock()
	defer tc.mu.Unlock()

	s, ok := tc.entries[target.Keyspace]
	if !ok {
		s = make(map[string]map[topodatapb.TabletType]*tabletStatsCacheEntry)
		tc.entries[target.Keyspace] = s
	}
	t, ok := s[target.Shard]
	if !ok {
		t = make(map[topodatapb.TabletType]*tabletStatsCacheEntry)
		s[target.Shard] = t
	}
	e, ok := t[target.TabletType]
	if !ok {
		e = &tabletStatsCacheEntry{
			all:        make(map[string]*TabletStats),
			aggregates: make(map[string]*querypb.AggregateStats),
		}
		t[target.TabletType] = e
	}
	return e
}
558225
func (c *Cache) GetOrCreateBytes(id string, create func() ([]byte, error)) (ItemInfo, []byte, error) {
	id = cleanID(id)

	c.nlocker.Lock(id)
	defer c.nlocker.Unlock(id)

	info := ItemInfo{Name: id}

	if r := c.getOrRemove(id); r != nil {
		defer r.Close()
		b, err := ioutil.ReadAll(r)
		return info, b, err
	}

	b, err := create()
	if err != nil {
		return info, nil, err
	}

	if c.maxAge == 0 {
		return info, b, nil
	}

	if err := afero.WriteReader(c.Fs, id, bytes.NewReader(b)); err != nil {
		return info, nil, err
	}
	return info, b, nil

}
410449
func (p *PagesBuild) GetCreatedAt() Timestamp {
	if p == nil || p.CreatedAt == nil {
		return Timestamp{}
	}
	return *p.CreatedAt
}
144555
func (i *Installation) GetCreatedAt() Timestamp {
	if i == nil || i.CreatedAt == nil {
		return Timestamp{}
	}
	return *i.CreatedAt
}
567453
func configsCreate(db *gorm.DB, config *Config) (*Config, error) {
	return config, db.Create(config).Error
}
720202
func (t *Tr) contextOrCachedNode(id ID, cache bool) (*Node, error) {
	if t.context != nil {
		n := t.context.getNode(id)
		if n != nil {
			return n, nil
		}
	}

	return t.cacher.getNode(t, id, cache)
}
875585
func (flf *fileLedgerFactory) GetOrCreate(chainID string) (blockledger.ReadWriter, error) {
	flf.mutex.Lock()
	defer flf.mutex.Unlock()

	key := chainID
	// check cache
	ledger, ok := flf.ledgers[key]
	if ok {
		return ledger, nil
	}
	// open fresh
	blockStore, err := flf.blkstorageProvider.OpenBlockStore(key)
	if err != nil {
		return nil, err
	}
	ledger = NewFileLedger(blockStore)
	flf.ledgers[key] = ledger
	return ledger, nil
}
675149
func (s *OrganizationConfigStore) FindOrCreate(ctx context.Context, orgID string) (*chronograf.OrganizationConfig, error) {
	var c chronograf.OrganizationConfig
	err := s.client.db.Update(func(tx *bolt.Tx) error {
		err := s.get(ctx, tx, orgID, &c)
		if err == chronograf.ErrOrganizationConfigNotFound {
			c = newOrganizationConfig(orgID)
			return s.put(ctx, tx, &c)
		}
		return err
	})

	if err != nil {
		return nil, err
	}
	return &c, nil
}
421511
func (ac *APIController) NetworkCreate(network *contivModel.Network) error {
	log.Infof("Received NetworkCreate: %+v", network)

	// Make sure global settings is valid
	if err := validateGlobalConfig(network.Encap); err != nil {
		return fmt.Errorf("Global configuration is not ready: %v", err.Error())
	}

	// Make sure tenant exists
	if network.TenantName == "" {
		return core.Errorf("Invalid tenant name")
	}

	tenant := contivModel.FindTenant(network.TenantName)
	if tenant == nil {
		return core.Errorf("Tenant not found")
	}

	for key := range tenant.LinkSets.Networks {
		networkDetail := contivModel.FindNetwork(key)
		if networkDetail == nil {
			log.Errorf("Network key %s not found", key)
			return fmt.Errorf("network key %s not found", key)
		}

		// Check for overlapping subnetv6 if existing and current subnetv6 is non-empty
		if network.Ipv6Subnet != "" && networkDetail.Ipv6Subnet != "" {
			flagv6 := netutils.IsOverlappingSubnetv6(network.Ipv6Subnet, networkDetail.Ipv6Subnet)
			if flagv6 == true {
				log.Errorf("Overlapping of Subnetv6 Networks")
				return errors.New("network " + networkDetail.NetworkName + " conflicts with subnetv6  " + network.Ipv6Subnet)
			}
		}

		// Check for overlapping subnet if existing and current subnet is non-empty
		if network.Subnet != "" && networkDetail.Subnet != "" {
			flag := netutils.IsOverlappingSubnet(network.Subnet, networkDetail.Subnet)
			if flag == true {
				log.Errorf("Overlapping of Networks")
				return errors.New("network " + networkDetail.NetworkName + " conflicts with subnet " + network.Subnet)
			}
		}
	}

	// If there is an EndpointGroup with the same name as this network, reject.
	nameClash := contivModel.FindEndpointGroup(network.Key)
	if nameClash != nil {
		return core.Errorf("EndpointGroup %s conflicts with the network name",
			nameClash.GroupName)
	}

	// Get the state driver
	stateDriver, err := utils.GetStateDriver()
	if err != nil {
		return err
	}

	// Build network config
	networkCfg := intent.ConfigNetwork{
		Name:           network.NetworkName,
		NwType:         network.NwType,
		PktTagType:     network.Encap,
		PktTag:         network.PktTag,
		SubnetCIDR:     network.Subnet,
		Gateway:        network.Gateway,
		IPv6SubnetCIDR: network.Ipv6Subnet,
		IPv6Gateway:    network.Ipv6Gateway,
		CfgdTag:        network.CfgdTag,
	}

	// Create the network
	err = master.CreateNetwork(networkCfg, stateDriver, network.TenantName)
	if err != nil {
		log.Errorf("Error creating network {%+v}. Err: %v", network, err)
		return err
	}

	// Setup links
	modeldb.AddLink(&network.Links.Tenant, tenant)
	modeldb.AddLinkSet(&tenant.LinkSets.Networks, network)

	// Save the tenant too since we added the links
	err = tenant.Write()
	if err != nil {
		log.Errorf("Error updating tenant state(%+v). Err: %v", tenant, err)
		return err
	}

	return nil
}
1334949
func (c *Changelog) GetSubsectionOrCreate(versionNum, subsectionName string) *Subsection {
	version := c.GetVersionOrCreate(versionNum)
	subsection := c.GetSubsection(versionNum, subsectionName)
	if subsection == nil {
		subsection = NewSubsection(subsectionName)
		version.Subsections = append(version.Subsections, subsection)
	}
	return subsection
}
438491
func (s *Server) AccessTokensCreate(token *AccessToken) (*AccessToken, error) {
	signed, err := signToken(s.Secret, token)
	if err != nil {
		return token, err
	}

	token.Token = signed

	return token, token.IsValid()
}
202075
func (s *Server) CreateNode(w http.ResponseWriter, req *http.Request) {
	config := &adapters.NodeConfig{}

	err := json.NewDecoder(req.Body).Decode(config)
	if err != nil && err != io.EOF {
		http.Error(w, err.Error(), http.StatusBadRequest)
		return
	}

	node, err := s.network.NewNodeWithConfig(config)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	s.JSON(w, http.StatusCreated, node.NodeInfo())
}
1156525
func (c *LayeredCache) GetOrCreateSecondaryCache(primary string) *SecondaryCache {
	primaryBkt := c.bucket(primary)
	bkt := primaryBkt.getSecondaryBucket(primary)
	primaryBkt.Lock()
	if bkt == nil {
		bkt = &bucket{lookup: make(map[string]*Item)}
		primaryBkt.buckets[primary] = bkt
	}
	primaryBkt.Unlock()
	return &SecondaryCache{
		bucket: bkt,
		pCache: c,
	}
}
729816
func (ncs *nodeCacheStandard) Get(ref data.BlockRef) (n Node) {
	if ref == (data.BlockRef{}) {
		return nil
	}

	// Temporary code to track down bad block pointers. Remove (or
	// return an error) when not needed anymore.
	if !ref.IsValid() {
		panic(InvalidBlockRefError{ref})
	}

	var rootWrappers []func(Node) Node
	var parent Node
	defer func() {
		if n != nil {
			n = ncs.wrapNodeStandard(n, rootWrappers, parent)
		}
	}()

	ncs.lock.Lock()
	defer ncs.lock.Unlock()
	rootWrappers = ncs.rootWrappers
	entry, ok := ncs.nodes[ref]
	if !ok {
		return nil
	}
	ns := ncs.makeNodeStandardForEntryLocked(entry)
	parent = ns.core.parent // get while under lock
	return ns
}