cache_commit.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package storage
  2. import "strings"
  3. type cachedRowChange struct {
  4. pk string
  5. rowid int64
  6. absent bool
  7. values map[string]string
  8. }
  9. type tableCachePlan struct {
  10. table string
  11. rows []cachedRowChange
  12. indexes []string
  13. drop []string
  14. countDelta int
  15. }
  16. // Prepare under an exclusive table gate, before durable publication. Shared-gate
  17. // optimistic commits still invalidate: otherwise same-key commits could publish
  18. // derived state in the opposite order from their durable CAS operations.
  19. func (m *TableManager) prepareTableCache(table string, entries map[string]*overlayEntry, reads map[string]uint64) (*tableCachePlan, error) {
  20. indexes, err := m.schema.ListTableIndexes(table)
  21. if err != nil {
  22. return nil, err
  23. }
  24. plan := &tableCachePlan{table: strings.ToLower(table)}
  25. var warm []*Index
  26. m.cacheMu.RLock()
  27. for _, idx := range indexes {
  28. name := strings.ToLower(idx.Name)
  29. if _, ok := m.indexCache[name]; !ok {
  30. continue
  31. }
  32. if !cacheableColumnIndex(idx) {
  33. plan.drop = append(plan.drop, name)
  34. continue
  35. }
  36. warm = append(warm, idx)
  37. plan.indexes = append(plan.indexes, name)
  38. }
  39. m.cacheMu.RUnlock()
  40. for key, entry := range entries {
  41. if entry.absent {
  42. if reads[key] != 0 {
  43. plan.countDelta--
  44. }
  45. } else if reads[key] == 0 {
  46. plan.countDelta++
  47. }
  48. if len(warm) == 0 {
  49. continue
  50. }
  51. row := cachedRowChange{pk: strings.TrimPrefix(key, m.dataPrefix(table)), absent: entry.absent, values: make(map[string]string, len(warm))}
  52. if !entry.absent {
  53. row.rowid, _ = rowIDFromRow(entry.row)
  54. for _, idx := range warm {
  55. value, err := m.buildIndexValue(entry.row, idx.Columns)
  56. if err != nil {
  57. return nil, err
  58. }
  59. row.values[strings.ToLower(idx.Name)] = formatIndexValue(value)
  60. }
  61. }
  62. plan.rows = append(plan.rows, row)
  63. }
  64. return plan, nil
  65. }
  66. func (m *TableManager) applyTableCache(plan *tableCachePlan) {
  67. m.cacheMu.Lock()
  68. defer m.cacheMu.Unlock()
  69. for _, name := range plan.drop {
  70. delete(m.indexCache, name)
  71. delete(m.indexRowValues, name)
  72. delete(m.indexTable, name)
  73. }
  74. keys := m.rowKeyCache[plan.table]
  75. reverse := m.keyRowCache[plan.table]
  76. // Remove every previous owner before adding any replacement (PK moves, rowid
  77. // replacement and swaps must not depend on Go map iteration order).
  78. for _, row := range plan.rows {
  79. id, ok := reverse[row.pk]
  80. if !ok {
  81. continue
  82. }
  83. for _, name := range plan.indexes {
  84. if value, ok := m.indexRowValues[name][id]; ok {
  85. m.removeIndexEntryLocked(name, value, id)
  86. }
  87. }
  88. delete(keys, id)
  89. delete(reverse, row.pk)
  90. }
  91. for _, row := range plan.rows {
  92. if row.absent {
  93. continue
  94. }
  95. keys[row.rowid] = row.pk
  96. reverse[row.pk] = row.rowid
  97. for name, value := range row.values {
  98. m.indexCache[name][value] = append(m.indexCache[name][value], row.rowid)
  99. m.indexRowValues[name][row.rowid] = value
  100. }
  101. }
  102. if m.countsInit[plan.table] {
  103. m.counts[plan.table] += plan.countDelta
  104. }
  105. }
  106. // Call with cacheMu held when publishing a complete durable index snapshot.
  107. func (m *TableManager) installIndexCacheLocked(name, table string, values map[string][]int64, keys map[int64]string) {
  108. m.indexCache[name] = values
  109. m.indexTable[name] = table
  110. m.rowKeyCache[table] = keys
  111. reverse := make(map[string]int64, len(keys))
  112. for id, key := range keys {
  113. reverse[key] = id
  114. }
  115. m.keyRowCache[table] = reverse
  116. indexed := make(map[int64]string, len(keys))
  117. for value, ids := range values {
  118. for _, id := range ids {
  119. indexed[id] = value
  120. }
  121. }
  122. m.indexRowValues[name] = indexed
  123. }
  124. func (m *TableManager) removeIndexEntryLocked(name, value string, id int64) {
  125. values := m.indexCache[name]
  126. if values == nil {
  127. return
  128. }
  129. ids := values[value]
  130. remaining := ids[:0]
  131. for _, candidate := range ids {
  132. if candidate != id {
  133. remaining = append(remaining, candidate)
  134. }
  135. }
  136. if len(remaining) == 0 {
  137. delete(values, value)
  138. } else {
  139. values[value] = remaining
  140. }
  141. if indexed, ok := m.indexRowValues[name][id]; ok && indexed == value {
  142. delete(m.indexRowValues[name], id)
  143. }
  144. }