本文整理汇总了Golang中github.com/couchbase/sync_gateway/base.SetFromArray函数的典型用法代码示例。如果您正苦于以下问题:Golang SetFromArray函数的具体用法?Golang SetFromArray怎么用?Golang SetFromArray使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了SetFromArray函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestUnavailableWebhook
func TestUnavailableWebhook(t *testing.T) {
ids := make([]string, 20)
for i := 0; i < 20; i++ {
ids[i] = fmt.Sprintf("%d", i)
}
eventForTest := func(i int) (Body, base.Set) {
testBody := Body{
"_id": ids[i],
"value": i,
}
var channelSet base.Set
if i%2 == 0 {
channelSet = base.SetFromArray([]string{"Even"})
} else {
channelSet = base.SetFromArray([]string{"Odd"})
}
return testBody, channelSet
}
// Test unreachable webhook
em := NewEventManager()
em.Start(0, -1)
webhookHandler, _ := NewWebhook("http://badhost:1000/echo", "", nil)
em.RegisterEventHandler(webhookHandler, DocumentChange)
for i := 0; i < 10; i++ {
body, channels := eventForTest(i)
em.RaiseDocumentChangeEvent(body, channels)
}
time.Sleep(50 * time.Millisecond)
}
示例2: TestEqualsWithUnequalSet
func TestEqualsWithUnequalSet(t *testing.T) {
set1 := TimedSet{"ABC": 17, "CBS": 23, "BBC": 1}
set2 := base.SetFromArray([]string{"ABC", "BBC"})
assert.True(t, !set1.Equals(set2))
set3 := base.SetFromArray([]string{"ABC", "BBC", "CBS", "FOO"})
assert.True(t, !set1.Equals(set3))
}
示例3: TestEqualsWithUnequalSet
func TestEqualsWithUnequalSet(t *testing.T) {
set1 := TimedSet{"ABC": NewVbSimpleSequence(17), "CBS": NewVbSimpleSequence(23), "BBC": NewVbSimpleSequence(1)}
set2 := base.SetFromArray([]string{"ABC", "BBC"})
assert.True(t, !set1.Equals(set2))
set3 := base.SetFromArray([]string{"ABC", "BBC", "CBS", "FOO"})
assert.True(t, !set1.Equals(set3))
}
示例4: TestDocumentChangeEvent
func TestDocumentChangeEvent(t *testing.T) {
em := NewEventManager()
em.Start(0, -1)
// Setup test data
ids := make([]string, 20)
for i := 0; i < 20; i++ {
ids[i] = fmt.Sprintf("%d", i)
}
eventForTest := func(i int) (Body, base.Set) {
testBody := Body{
"_id": ids[i],
"value": i,
}
var channelSet base.Set
if i%2 == 0 {
channelSet = base.SetFromArray([]string{"Even"})
} else {
channelSet = base.SetFromArray([]string{"Odd"})
}
return testBody, channelSet
}
resultChannel := make(chan Body, 10)
//Setup test handler
testHandler := &TestingHandler{HandledEvent: DocumentChange}
testHandler.SetChannel(resultChannel)
em.RegisterEventHandler(testHandler, DocumentChange)
//Raise events
for i := 0; i < 10; i++ {
body, channels := eventForTest(i)
em.RaiseDocumentChangeEvent(body, "", channels)
}
// wait for Event Manager queue worker to process
time.Sleep(100 * time.Millisecond)
// Diagnostics for failures
channelSize := len(resultChannel)
if channelSize != 10 {
log.Printf("Expected 10 change events, got %v", channelSize)
for {
select {
case result := <-resultChannel:
log.Printf("Change event: %v", result)
default:
break
}
}
}
assert.True(t, channelSize == 10)
}
示例5: TestCollectAccessWarningsUsersInDb
// As long as there is one user in the db, there should be no warnings
func TestCollectAccessWarningsUsersInDb(t *testing.T) {
sc := NewServerContext(&ServerConfig{})
dbServer := "walrus:"
dbConfig := &DbConfig{
BucketConfig: BucketConfig{Server: &dbServer},
Name: "db",
}
_, err := sc.AddDatabaseFromConfig(dbConfig)
if err != nil {
panic(fmt.Sprintf("Error from AddDatabaseFromConfig: %v", err))
}
dbContext := sc.Database("db")
// create user
spec := map[string]*db.PrincipalConfig{
"foo": {
Disabled: false,
ExplicitChannels: base.SetFromArray([]string{"*"}),
},
}
// add a user to the db
sc.installPrincipals(dbContext, spec, "user")
warnings := collectAccessRelatedWarnings(dbConfig, dbContext)
assert.Equals(t, len(warnings), 0)
}
示例6: TestSlowExecutionProcessing
// Test sending many events with slow-running execution to validate they get dropped after hitting
// the max concurrent goroutines
func TestSlowExecutionProcessing(t *testing.T) {
em := NewEventManager()
em.Start(0, -1)
var logKeys = map[string]bool{
"Events": true,
}
base.UpdateLogKeys(logKeys, true)
ids := make([]string, 20)
for i := 0; i < 20; i++ {
ids[i] = fmt.Sprintf("%d", i)
}
eventForTest := func(i int) (Body, base.Set) {
testBody := Body{
"_id": ids[i],
"value": i,
}
var channelSet base.Set
if i%2 == 0 {
channelSet = base.SetFromArray([]string{"Even"})
} else {
channelSet = base.SetFromArray([]string{"Odd"})
}
return testBody, channelSet
}
resultChannel := make(chan Body, 100)
testHandler := &TestingHandler{HandledEvent: DocumentChange, handleDelay: 500}
testHandler.SetChannel(resultChannel)
em.RegisterEventHandler(testHandler, DocumentChange)
for i := 0; i < 20; i++ {
body, channels := eventForTest(i % 10)
em.RaiseDocumentChangeEvent(body, "", channels)
}
// wait for Event Manager queue worker to process
time.Sleep(2 * time.Second)
fmt.Println("resultChannel:", len(resultChannel))
assert.True(t, len(resultChannel) == 20)
}
示例7: _allChannels
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)
}
示例8: TestUnhandledEvent
func TestUnhandledEvent(t *testing.T) {
em := NewEventManager()
em.Start(0, -1)
ids := make([]string, 20)
for i := 0; i < 20; i++ {
ids[i] = fmt.Sprintf("%d", i)
}
eventForTest := func(i int) (Body, base.Set) {
testBody := Body{
"_id": ids[i],
"value": i,
}
var channelSet base.Set
if i%2 == 0 {
channelSet = base.SetFromArray([]string{"Even"})
} else {
channelSet = base.SetFromArray([]string{"Odd"})
}
return testBody, channelSet
}
resultChannel := make(chan Body, 10)
// create handler for UserAdd events
testHandler := &TestingHandler{HandledEvent: UserAdd}
testHandler.SetChannel(resultChannel)
em.RegisterEventHandler(testHandler, UserAdd)
// send DocumentChange events to handler
for i := 0; i < 10; i++ {
body, channels := eventForTest(i)
em.RaiseDocumentChangeEvent(body, channels)
}
// Wait for Event Manager queue worker to process
time.Sleep(50 * time.Millisecond)
// Validate that no events were handled
assert.True(t, len(resultChannel) == 0)
}
示例9: AsSet
// 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)
}
示例10: _addToCache
// Adds an entry to the appropriate channels' caches, returning the affected channels. lateSequence
// flag indicates whether it was a change arriving out of sequence
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
// If it's a late sequence, we want to add to all channel late queues within a single write lock,
// to avoid a changes feed seeing the same late sequence in different iteration loops (and sending
// twice)
func() {
if change.Skipped {
c.lateSeqLock.Lock()
base.LogTo("Sequences", "Acquired late sequence lock for %d", change.Sequence)
defer c.lateSeqLock.Unlock()
}
for channelName, removal := range ch {
if removal == nil || removal.Seq == change.Sequence {
channelCache := c._getChannelCache(channelName)
channelCache.addToCache(change, removal != nil)
addedTo = append(addedTo, channelName)
if change.Skipped {
channelCache.AddLateSequence(change)
}
}
}
if EnableStarChannelLog {
channelCache := c._getChannelCache(channels.UserStarChannel)
channelCache.addToCache(change, false)
addedTo = append(addedTo, channels.UserStarChannel)
if change.Skipped {
channelCache.AddLateSequence(change)
}
}
}()
// 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)
}
示例11: TestCustomHandler
func TestCustomHandler(t *testing.T) {
em := NewEventManager()
em.Start(0, -1)
ids := make([]string, 20)
for i := 0; i < 20; i++ {
ids[i] = fmt.Sprintf("%d", i)
}
eventForTest := func(i int) (Body, base.Set) {
testBody := Body{
"_id": ids[i],
"value": i,
}
var channelSet base.Set
if i%2 == 0 {
channelSet = base.SetFromArray([]string{"Even"})
} else {
channelSet = base.SetFromArray([]string{"Odd"})
}
return testBody, channelSet
}
resultChannel := make(chan Body, 20)
testHandler := &TestingHandler{HandledEvent: DocumentChange}
testHandler.SetChannel(resultChannel)
em.RegisterEventHandler(testHandler, DocumentChange)
for i := 0; i < 10; i++ {
body, channels := eventForTest(i)
em.RaiseDocumentChangeEvent(body, "", channels)
}
// wait for Event Manager queue worker to process
time.Sleep(50 * time.Millisecond)
assert.True(t, len(resultChannel) == 10)
}
示例12: UnmarshalJSON
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
}
示例13: SetFromArray
// 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(UserStarChannel)
case ExpandStar:
if result.Contains(UserStarChannel) {
result = base.SetOf(UserStarChannel)
}
}
return result, nil
}
示例14: RevDiff
// Given a document ID and a set of revision IDs, looks up which ones are not known. Returns an
// array of the unknown revisions, and an array of known revisions that might be recent ancestors.
func (db *Database) RevDiff(docid string, revids []string) (missing, possible []string) {
if strings.HasPrefix(docid, "_design/") && db.user != nil {
return // Users can't upload design docs, so ignore them
}
doc, err := db.GetDoc(docid)
if err != nil {
if !base.IsDocNotFoundError(err) {
base.Warn("RevDiff(%q) --> %T %v", docid, err, err)
// If something goes wrong getting the doc, treat it as though it's nonexistent.
}
missing = revids
return
}
// Check each revid to see if it's in the doc's rev tree:
revtree := doc.History
revidsSet := base.SetFromArray(revids)
possibleSet := make(map[string]bool)
for _, revid := range revids {
if !revtree.contains(revid) {
missing = append(missing, revid)
// Look at the doc's leaves for a known possible ancestor:
if gen, _ := ParseRevID(revid); gen > 1 {
revtree.forEachLeaf(func(possible *RevInfo) {
if !revidsSet.Contains(possible.ID) {
possibleGen, _ := ParseRevID(possible.ID)
if possibleGen < gen && possibleGen >= gen-100 {
possibleSet[possible.ID] = true
} else if possibleGen == gen && possible.Parent != "" {
possibleSet[possible.Parent] = true // since parent is < gen
}
}
})
}
}
}
// Convert possibleSet to an array (possible)
if len(possibleSet) > 0 {
possible = make([]string, 0, len(possibleSet))
for revid, _ := range possibleSet {
possible = append(possible, revid)
}
}
return
}
示例15: pollPrincipals
// PollPrincipals checks the principal counts, stored in the index, to determine whether there's been
// a change to a user or role that should trigger a notification for that principal.
func (k *kvChangeIndexReader) pollPrincipals() {
// Principal polling is strictly for notification handling, so skip if no notify function is defined
if k.onChange == nil {
return
}
k.activePrincipalCountsLock.Lock()
defer k.activePrincipalCountsLock.Unlock()
// Check whether ANY principals have been updated since last poll, before doing the work of retrieving individual keys
overallCount, err := k.indexReadBucket.Incr(base.KTotalPrincipalCountKey, 0, 0, 0)
if err != nil {
base.Warn("Principal polling encountered error getting overall count:%v", err)
return
}
if overallCount == k.overallPrincipalCount {
return
}
k.overallPrincipalCount = overallCount
// There's been a change - check whether any of our active principals have changed
var changedWaitKeys []string
for principalID, currentCount := range k.activePrincipalCounts {
key := fmt.Sprintf(base.KPrincipalCountKeyFormat, principalID)
newCount, err := k.indexReadBucket.Incr(key, 0, 0, 0)
if err != nil {
base.Warn("Principal polling encountered error getting overall count for key %s:%v", key, err)
continue
}
if newCount != currentCount {
k.activePrincipalCounts[principalID] = newCount
waitKey := strings.TrimPrefix(key, base.KPrincipalCountKeyPrefix)
changedWaitKeys = append(changedWaitKeys, waitKey)
}
}
if len(changedWaitKeys) > 0 {
k.onChange(base.SetFromArray(changedWaitKeys))
}
}