MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / Init

Method Init

db/change_cache.go:152–194  ·  view source on GitHub ↗

HOUSEKEEPING: Initializes a new changeCache. lastSequence is the last known database sequence assigned. notifyChangeFunc is an optional function that will be called to notify of channel changes. After calling Init(), you must call .Start() to start using the cache, otherwise it will be in a locked s

(ctx context.Context, dbContext *DatabaseContext, channelCache ChannelCache, notifyChange func(context.Context, channels.Set), options *CacheOptions, metaKeys *base.MetadataKeys)

Source from the content-addressed store, hash-verified

150// and callers will block on trying to obtain the lock.
151
152func (c *changeCache) Init(ctx context.Context, dbContext *DatabaseContext, channelCache ChannelCache, notifyChange func(context.Context, channels.Set), options *CacheOptions, metaKeys *base.MetadataKeys) error {
153 c.db = dbContext
154 c.logCtx = ctx
155
156 c.notifyChangeFunc = notifyChange
157 c.receivedSeqs = make(map[uint64]struct{})
158 c.terminator = make(chan bool)
159 c.initTime = time.Now()
160 c.skippedSeqs = NewSkippedSequenceSkiplist()
161 c.lastAddPendingTime = time.Now().UnixNano()
162 c.sgCfgPrefix = dbContext.MetadataKeys.SGCfgPrefix(c.db.Options.GroupID)
163 c.metaKeys = metaKeys
164
165 // init cache options
166 if options != nil {
167 c.options = *options
168 } else {
169 c.options = DefaultCacheOptions()
170 }
171
172 c.channelCache = channelCache
173
174 base.InfofCtx(ctx, base.KeyCache, "Initializing changes cache for %s with options %+v", base.UD(c.db.Name), c.options)
175
176 heap.Init(&c.pendingLogs)
177
178 // background tasks that perform housekeeping duties on the cache
179 bgt, err := NewBackgroundTask(ctx, "InsertPendingEntries", c.InsertPendingEntries, c.options.CachePendingSeqMaxWait/2, c.terminator)
180 if err != nil {
181 return err
182 }
183 c.backgroundTasks = append(c.backgroundTasks, bgt)
184
185 bgt, err = NewBackgroundTask(ctx, "CleanSkippedSequenceQueue", c.CleanSkippedSequenceQueue, c.options.CacheSkippedSeqMaxWait/2, c.terminator)
186 if err != nil {
187 return err
188 }
189 c.backgroundTasks = append(c.backgroundTasks, bgt)
190
191 // Lock the cache -- not usable until .Start() called. This fixes the DCP startup race condition documented in SG #3558.
192 c.lock.Lock()
193 return nil
194}
195
196func (c *changeCache) Start(initialSequence uint64) error {
197

Calls 8

InfofCtxFunction · 0.92
UDFunction · 0.92
DefaultCacheOptionsFunction · 0.85
NewBackgroundTaskFunction · 0.85
SGCfgPrefixMethod · 0.80
InitMethod · 0.65
LockMethod · 0.45