(ctx context.Context, viewStore sgbucket.ViewStore)
| 393 | } |
| 394 | |
| 395 | func installViews(ctx context.Context, viewStore sgbucket.ViewStore) error { |
| 396 | |
| 397 | // syncData specifies the path to Sync Gateway sync metadata used in the map function - |
| 398 | // in the document body when xattrs available, in the mobile xattr when xattrs enabled. |
| 399 | syncData := fmt.Sprintf(`var sync |
| 400 | if (meta.xattrs === undefined || meta.xattrs.%[1]s === undefined) { |
| 401 | sync = doc.%[2]s |
| 402 | } else { |
| 403 | sync = meta.xattrs.%[1]s |
| 404 | } |
| 405 | `, base.SyncXattrName, base.SyncPropertyName) |
| 406 | |
| 407 | // View for _all_docs |
| 408 | // Key is docid; value is [revid, sequence] |
| 409 | alldocs_map := `function (doc, meta) { |
| 410 | %[1]s |
| 411 | if (sync === undefined || meta.id.substring(0,6) == "%[2]s") |
| 412 | return; |
| 413 | if ((sync.flags & 1) || sync.deleted) |
| 414 | return; |
| 415 | var channels = sync.channels; |
| 416 | var channelNames = []; |
| 417 | for (ch in channels) { |
| 418 | if (channels[ch] == null) |
| 419 | channelNames.push(ch); |
| 420 | } |
| 421 | emit(meta.id, {r:sync.rev, s:sync.sequence, c:channelNames}); }` |
| 422 | alldocs_map = fmt.Sprintf(alldocs_map, syncData, base.SyncDocPrefix) |
| 423 | |
| 424 | // View for importing unknown docs |
| 425 | // Key is [existing?, docid] where 'existing?' is false for unknown docs |
| 426 | import_map := `function (doc, meta) { |
| 427 | %s |
| 428 | if(meta.id.substring(0,6) != "%s") { |
| 429 | var exists = (sync !== undefined); |
| 430 | emit([exists, meta.id], null); } }` |
| 431 | import_map = fmt.Sprintf(import_map, syncData, base.SyncDocPrefix) |
| 432 | |
| 433 | // Sessions view - used for session delete |
| 434 | // Key is username; value is docid |
| 435 | sessions_map := `function (doc, meta) { |
| 436 | var prefix = meta.id.substring(0,%d); |
| 437 | if (prefix == %q) |
| 438 | emit(doc.username, meta.id);}` |
| 439 | sessions_map = fmt.Sprintf(sessions_map, len(base.SessionPrefixRoot), base.SessionPrefixRoot) |
| 440 | |
| 441 | // Tombstones view - used for view tombstone compaction |
| 442 | // Key is purge time; value is docid |
| 443 | tombstones_map := `function (doc, meta) { |
| 444 | %s |
| 445 | if (sync !== undefined && sync.tombstoned_at !== undefined) |
| 446 | emit(sync.tombstoned_at, meta.id);}` |
| 447 | tombstones_map = fmt.Sprintf(tombstones_map, syncData) |
| 448 | |
| 449 | // All-principals view |
| 450 | // Key is name; value is true for user, false for role |
| 451 | principals_map := `function (doc, meta) { |
| 452 | var prefix = meta.id.substring(0,11); |
no test coverage detected