// Adds an entry to the appropriate channels' caches, returning the affected channels. func (c *changeCache) _addToCache(change *LogEntry) base.Set { if change.Sequence >= c.nextSequence { c.nextSequence = change.Sequence + 1 } if change.DocID == "" { return nil // this was a placeholder for an unused sequence } addedTo := make([]string, 0, 4) ch := change.Channels change.Channels = nil // not needed anymore, so free some memory for channelName, removal := range ch { if removal == nil || removal.Seq == change.Sequence { c._getChannelCache(channelName).addToCache(change, removal != nil) addedTo = append(addedTo, channelName) } } if EnableStarChannelLog { c._getChannelCache("*").addToCache(change, false) addedTo = append(addedTo, "*") } // Record a histogram of the overall lag from the time the doc was saved: lag := time.Since(change.TimeSaved) lagMs := int(lag/(100*time.Millisecond)) * 100 changeCacheExpvars.Add(fmt.Sprintf("lag-total-%04dms", lagMs), 1) // ...and from the time the doc was received from Tap: lag = time.Since(change.TimeReceived) lagMs = int(lag/(100*time.Millisecond)) * 100 changeCacheExpvars.Add(fmt.Sprintf("lag-queue-%04dms", lagMs), 1) return base.SetFromArray(addedTo) }
func (c *changeCache) _allChannels() base.Set { array := make([]string, len(c.channelCaches)) i := 0 for name, _ := range c.channelCaches { array[i] = name i++ } return base.SetFromArray(array) }
// Converts a TimedSet to a Set func (set TimedSet) AsSet() base.Set { if set == nil { return nil } result := make([]string, 0, len(set)) for ch, _ := range set { result = append(result, ch) } return base.SetFromArray(result) }
func (user *userImpl) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &user.userImplBody); err != nil { return err } else if err := json.Unmarshal(data, &user.roleImpl); err != nil { return err } // Migrate "admin_roles" field: if user.OldExplicitRoles_ != nil { user.ExplicitRoles_ = ch.AtSequence(base.SetFromArray(user.OldExplicitRoles_), 1) user.OldExplicitRoles_ = nil } return nil }
// Creates a new Set from an array of strings. Returns an error if any names are invalid. func SetFromArray(names []string, mode StarMode) (base.Set, error) { for _, name := range names { if !IsValidChannel(name) { return nil, illegalChannelError(name) } } result := base.SetFromArray(names) switch mode { case RemoveStar: result = result.Removing("*") case ExpandStar: if result.Contains("*") { result = base.SetOf("*") } } return result, nil }
// Updates or creates a principal from a PrincipalConfig structure. func (dbc *DatabaseContext) UpdatePrincipal(newInfo PrincipalConfig, isUser bool, allowReplace bool) (replaced bool, err error) { // Get the existing principal, or if this is a POST make sure there isn't one: var princ auth.Principal var user auth.User authenticator := dbc.Authenticator() if isUser { user, err = authenticator.GetUser(*newInfo.Name) princ = user } else { princ, err = authenticator.GetRole(*newInfo.Name) } if err != nil { return } replaced = (princ != nil) if !replaced { // If user/role didn't exist already, instantiate a new one: if isUser { user, err = authenticator.NewUser(*newInfo.Name, "", nil) princ = user } else { princ, err = authenticator.NewRole(*newInfo.Name, nil) } if err != nil { return } } else if !allowReplace { err = base.HTTPErrorf(http.StatusConflict, "Already exists") return } // Update the persistent sequence number of this principal: nextSeq, err := dbc.sequences.nextSequence() if err != nil { return } princ.SetSequence(nextSeq) // Now update the Principal object from the properties in the request, first the channels: updatedChannels := princ.ExplicitChannels() if updatedChannels == nil { updatedChannels = ch.TimedSet{} } updatedChannels.UpdateAtSequence(newInfo.ExplicitChannels, nextSeq) princ.SetExplicitChannels(updatedChannels) // Then the user-specific fields like roles: if isUser { user.SetEmail(newInfo.Email) if newInfo.Password != nil { user.SetPassword(*newInfo.Password) } user.SetDisabled(newInfo.Disabled) updatedRoles := user.ExplicitRoles() if updatedRoles == nil { updatedRoles = ch.TimedSet{} } updatedRoles.UpdateAtSequence(base.SetFromArray(newInfo.ExplicitRoleNames), nextSeq) user.SetExplicitRoles(updatedRoles) } // And finally save the Principal: err = authenticator.Save(princ) return }
func (e AllDocsEntry) Equal(e2 AllDocsEntry) bool { return e.DocID == e2.DocID && e.RevID == e2.RevID && e.Sequence == e2.Sequence && base.SetFromArray(e.Channels).Equals(base.SetFromArray(e2.Channels)) }