Insert out-of-sequence entry into the cache. If the docId is already present in a later sequence, we skip the insert. If the docId is already present in an earlier sequence, we remove the earlier sequence.
(log *LogEntries, change *LogEntry)
| 515 | // sequence, we skip the insert. If the docId is already present in an earlier sequence, |
| 516 | // we remove the earlier sequence. |
| 517 | func (c *singleChannelCacheImpl) insertChange(log *LogEntries, change *LogEntry) { |
| 518 | |
| 519 | defer func() { |
| 520 | c.cachedDocIDs[change.DocID] = struct{}{} |
| 521 | c.UpdateCacheUtilization(change, 1) |
| 522 | }() |
| 523 | |
| 524 | end := len(*log) - 1 |
| 525 | |
| 526 | insertAtIndex := 0 |
| 527 | |
| 528 | _, docIDExists := c.cachedDocIDs[change.DocID] |
| 529 | |
| 530 | // Walk log backwards until we find the point where we should insert this change. |
| 531 | // (recall that logentries is sorted in ascending sequence order) |
| 532 | for i := end; i >= 0; i-- { |
| 533 | currLog := (*log)[i] |
| 534 | if insertAtIndex == 0 && change.Sequence > currLog.Sequence { |
| 535 | insertAtIndex = i + 1 |
| 536 | } |
| 537 | if docIDExists { |
| 538 | if currLog.DocID == change.DocID { |
| 539 | if currLog.Sequence >= change.Sequence { |
| 540 | // we've already cached a later revision of this document, can ignore update |
| 541 | return |
| 542 | } else { |
| 543 | // found existing prior to insert position. Decrement utilization for replaced version |
| 544 | c.UpdateCacheUtilization((*log)[i], -1) |
| 545 | if i == insertAtIndex-1 { |
| 546 | // The sequence is adjacent to another with the same docId - replace it |
| 547 | // instead of inserting |
| 548 | (*log)[i] = change |
| 549 | return |
| 550 | } else { |
| 551 | // Shift and insert to remove the old entry and add the new one |
| 552 | copy((*log)[i:insertAtIndex-1], (*log)[i+1:insertAtIndex]) |
| 553 | (*log)[insertAtIndex-1] = change |
| 554 | return |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | // We didn't find a match for DocID, so standard insert. Append an nil entry, shift existing, and insert. |
| 562 | *log = append(*log, nil) |
| 563 | copy((*log)[insertAtIndex+1:], (*log)[insertAtIndex:]) |
| 564 | (*log)[insertAtIndex] = change |
| 565 | return |
| 566 | } |
| 567 | |
| 568 | // Prepends an array of entries to this one, skipping ones that I already have. |
| 569 | // The new array needs to overlap with my current log, i.e. must contain the same sequence as |
no test coverage detected