tx.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. package storage
  2. import (
  3. "fmt"
  4. "sort"
  5. "strings"
  6. "sync"
  7. )
  8. // ErrSerialization is returned when a transaction's optimistic validation fails
  9. // because a concurrent transaction committed a conflicting change.
  10. var ErrSerialization = fmt.Errorf("serialization failure: concurrent transaction modified the database")
  11. // Session is a buffered SQL transaction/session wrapper around a TableManager.
  12. // While in a transaction, writes are staged in memory, reads observe that
  13. // staged overlay, and COMMIT issues a single atomic compare-and-swap batch
  14. // write. Rollback simply discards the staged changes (no compensating durable
  15. // writes). A Session is used for both autocommit statements (where operations
  16. // delegate straight to the durable TableManager) and buffered transactions.
  17. type Session struct {
  18. schema *SchemaManager
  19. table *TableManager
  20. mu sync.Mutex
  21. inTx bool
  22. aborted bool
  23. // overlay holds staged writes keyed by lowercased table then data key.
  24. overlay map[string]map[string]*overlayEntry
  25. // log records mutations in order so savepoints can roll back.
  26. log []txMutation
  27. // reads records the durable LSN of every key the transaction observed
  28. // (0 means the key was observed absent). It becomes the compare set at
  29. // commit and also captures the base LSN of every written key.
  30. reads map[string]uint64
  31. // scanGens records the per-table generation captured by the first scan of
  32. // each table, validated at commit to detect phantoms. Indexed equality reads
  33. // use predicateGens so writes to other index values do not cause conflicts.
  34. scanGens map[string]uint64
  35. predicateGens map[string]predicateRead
  36. }
  37. type predicateRead struct {
  38. table string
  39. gen uint64
  40. }
  41. type overlayEntry struct {
  42. row Row
  43. absent bool
  44. }
  45. type txMutation struct {
  46. table string
  47. key string
  48. prev *overlayEntry
  49. }
  50. // NewSession creates a session wrapping the given schema and table managers.
  51. func NewSession(schema *SchemaManager, table *TableManager) *Session {
  52. return &Session{
  53. schema: schema,
  54. table: table,
  55. overlay: make(map[string]map[string]*overlayEntry),
  56. reads: make(map[string]uint64),
  57. scanGens: make(map[string]uint64),
  58. predicateGens: make(map[string]predicateRead),
  59. }
  60. }
  61. // Begin starts a buffered transaction.
  62. func (s *Session) Begin() error {
  63. s.mu.Lock()
  64. defer s.mu.Unlock()
  65. if s.inTx {
  66. return fmt.Errorf("cannot start a transaction within a transaction")
  67. }
  68. s.inTx = true
  69. s.aborted = false
  70. s.overlay = make(map[string]map[string]*overlayEntry)
  71. s.log = nil
  72. s.reads = make(map[string]uint64)
  73. s.scanGens = make(map[string]uint64)
  74. s.predicateGens = make(map[string]predicateRead)
  75. return nil
  76. }
  77. // InTx reports whether a transaction is in progress.
  78. func (s *Session) InTx() bool {
  79. s.mu.Lock()
  80. defer s.mu.Unlock()
  81. return s.inTx
  82. }
  83. // Abort marks the transaction as aborted without discarding state.
  84. func (s *Session) Abort() {
  85. s.mu.Lock()
  86. defer s.mu.Unlock()
  87. if s.inTx {
  88. s.aborted = true
  89. }
  90. }
  91. // Snapshot returns the current mutation-log position for a savepoint.
  92. func (s *Session) Snapshot() int {
  93. s.mu.Lock()
  94. defer s.mu.Unlock()
  95. return len(s.log)
  96. }
  97. // RollbackTo discards mutations after the given savepoint position.
  98. func (s *Session) RollbackTo(pos int) {
  99. s.mu.Lock()
  100. defer s.mu.Unlock()
  101. for i := len(s.log) - 1; i >= pos && i >= 0; i-- {
  102. m := s.log[i]
  103. if m.prev == nil {
  104. delete(s.overlay[m.table], m.key)
  105. if len(s.overlay[m.table]) == 0 {
  106. delete(s.overlay, m.table)
  107. }
  108. } else {
  109. if s.overlay[m.table] == nil {
  110. s.overlay[m.table] = make(map[string]*overlayEntry)
  111. }
  112. s.overlay[m.table][m.key] = m.prev
  113. }
  114. }
  115. if pos < len(s.log) {
  116. s.log = s.log[:pos]
  117. }
  118. s.aborted = false
  119. }
  120. // Rollback discards the transaction without writing anything durable.
  121. func (s *Session) Rollback() error {
  122. s.mu.Lock()
  123. defer s.mu.Unlock()
  124. if !s.inTx {
  125. return fmt.Errorf("cannot rollback: no transaction in progress")
  126. }
  127. s.resetLocked()
  128. return nil
  129. }
  130. // Commit validates and durably applies the staged transaction in one atomic
  131. // compare-and-swap batch write. It discards the commit LSN; callers that need a
  132. // ordering cursor use CommitWithLSN.
  133. func (s *Session) Commit() error {
  134. _, err := s.CommitWithLSN()
  135. return err
  136. }
  137. // CommitWithLSN behaves like Commit and additionally returns the LSN assigned
  138. // to the durable batch. A read-only or empty transaction returns zero because
  139. // no new durable state was published.
  140. func (s *Session) CommitWithLSN() (uint64, error) {
  141. s.mu.Lock()
  142. defer s.mu.Unlock()
  143. if !s.inTx {
  144. return 0, fmt.Errorf("cannot commit: no transaction in progress")
  145. }
  146. if s.aborted {
  147. s.resetLocked()
  148. return 0, fmt.Errorf("current transaction is aborted")
  149. }
  150. // Acquire the per-table gates, escalating any table whose UNIQUE index
  151. // appeared after the initial unlocked probe. See acquireCommitLocks.
  152. _, _, unlockAll, uniqueTables, err := s.acquireCommitLocks()
  153. if err != nil {
  154. s.resetLocked()
  155. return 0, err
  156. }
  157. defer unlockAll()
  158. // Validate scan generations for phantom detection.
  159. for t, gen := range s.scanGens {
  160. if s.table.generation(t) != gen {
  161. s.resetLocked()
  162. return 0, ErrSerialization
  163. }
  164. }
  165. for key, predicate := range s.predicateGens {
  166. if s.table.predicateGeneration(key) != predicate.gen {
  167. s.resetLocked()
  168. return 0, ErrSerialization
  169. }
  170. }
  171. // Validate the final overlay of every unique-indexed written table against
  172. // durable rows. This runs under the exclusive gate acquired above, so it
  173. // cannot race a concurrent writer. Duplicate unique values fail the commit
  174. // with a "UNIQUE constraint failed" error rather than ErrSerialization.
  175. for t := range uniqueTables {
  176. entries := s.overlay[t]
  177. pending := make([]Row, 0, len(entries))
  178. // Exclude every overlay data key: rows being written, updated, or deleted
  179. // are all replaced by this transaction, so their durable unique values
  180. // must not self-conflict with the staged state (e.g. delete a row and
  181. // re-insert the same primary key with a different rowid).
  182. excluded := make(map[string]bool, len(entries))
  183. for key, e := range entries {
  184. excluded[key] = true
  185. if !e.absent {
  186. pending = append(pending, e.row)
  187. }
  188. }
  189. if err := s.table.validateUniqueRows(t, pending, excluded); err != nil {
  190. s.resetLocked()
  191. return 0, err
  192. }
  193. }
  194. // Build the compare set from every observed key.
  195. checks := make([]CompareCheck, 0, len(s.reads))
  196. for key, lsn := range s.reads {
  197. checks = append(checks, CompareCheck{Key: []byte(key), LSN: lsn})
  198. }
  199. sort.Slice(checks, func(i, j int) bool { return string(checks[i].Key) < string(checks[j].Key) })
  200. // Build the batch ops from the staged overlay.
  201. ops := make([]BatchOp, 0)
  202. for _, entries := range s.overlay {
  203. for key, e := range entries {
  204. if e.absent {
  205. ops = append(ops, BatchOp{Op: batchDelete, Key: []byte(key)})
  206. } else {
  207. data, err := encodeRow(e.row)
  208. if err != nil {
  209. s.resetLocked()
  210. return 0, err
  211. }
  212. ops = append(ops, BatchOp{Op: batchPut, Key: []byte(key), Value: data})
  213. }
  214. }
  215. }
  216. sort.Slice(ops, func(i, j int) bool { return string(ops[i].Key) < string(ops[j].Key) })
  217. // A read-only transaction has nothing to write; commit trivially.
  218. if len(ops) == 0 {
  219. s.resetLocked()
  220. return 0, nil
  221. }
  222. var committed bool
  223. var commitLSN uint64
  224. err = s.table.pool.WithClient(func(c *KVClient) error {
  225. lsn, ok, err := c.CompareBatchWrite(checks, ops, nil)
  226. commitLSN = lsn
  227. committed = ok
  228. return err
  229. })
  230. if err != nil {
  231. s.resetLocked()
  232. return 0, err
  233. }
  234. if !committed {
  235. s.resetLocked()
  236. return 0, ErrSerialization
  237. }
  238. // Advance generations and invalidate derived caches for written tables.
  239. for t := range s.overlay {
  240. s.table.InvalidateCache(t)
  241. s.table.bumpIndexPredicateWildcard(t)
  242. s.table.bumpGeneration(t)
  243. }
  244. s.resetLocked()
  245. return commitLSN, nil
  246. }
  247. // acquireCommitLocks acquires the per-table gates needed to commit the staged
  248. // transaction, returning the held locks, whether each is exclusive, an unlock
  249. // function, and the set of written tables that carry a UNIQUE index. A written
  250. // table with a UNIQUE index is locked exclusively so its final overlay can be
  251. // validated against durable rows. The uniqueness probe is re-checked under the
  252. // held gates so a concurrent CREATE UNIQUE INDEX cannot slip in after the probe
  253. // and leave a duplicate unvalidated.
  254. func (s *Session) acquireCommitLocks() (locks []*sync.RWMutex, exclusive []bool, unlockAll func(), uniqueTables map[string]bool, err error) {
  255. unlock := func(ls []*sync.RWMutex, ex []bool) {
  256. for i := len(ls) - 1; i >= 0; i-- {
  257. if ex[i] {
  258. ls[i].Unlock()
  259. } else {
  260. ls[i].RUnlock()
  261. }
  262. }
  263. }
  264. for {
  265. // Tables read through scans or predicates need an exclusive validation
  266. // gate. Tables that are only written use the shared publication gate,
  267. // allowing disjoint optimistic commits to proceed concurrently while
  268. // still excluding scanner commits.
  269. affected := make(map[string]bool)
  270. for t := range s.overlay {
  271. affected[t] = false
  272. }
  273. for t := range s.scanGens {
  274. affected[t] = true
  275. }
  276. for _, predicate := range s.predicateGens {
  277. affected[predicate.table] = true
  278. }
  279. // Tables written in this transaction that carry a UNIQUE index need the
  280. // exclusive gate so their final overlay can be validated against durable
  281. // rows without racing another writer.
  282. uniqueTables = make(map[string]bool)
  283. for t := range s.overlay {
  284. uniq, err := s.table.hasUniqueIndex(t)
  285. if err != nil {
  286. return nil, nil, nil, nil, err
  287. }
  288. if uniq {
  289. uniqueTables[t] = true
  290. affected[t] = true
  291. }
  292. }
  293. tables := make([]string, 0, len(affected))
  294. for t := range affected {
  295. tables = append(tables, t)
  296. }
  297. sort.Strings(tables)
  298. locks = make([]*sync.RWMutex, len(tables))
  299. exclusive = make([]bool, len(tables))
  300. for i, t := range tables {
  301. locks[i] = s.table.tableLock(t)
  302. exclusive[i] = affected[t]
  303. }
  304. for i, lock := range locks {
  305. if exclusive[i] {
  306. lock.Lock()
  307. } else {
  308. lock.RLock()
  309. }
  310. }
  311. // Re-check under the held gates. A concurrent CREATE UNIQUE INDEX can
  312. // only have completed before we acquired the gate (it needs the exclusive
  313. // gate), so this probe is authoritative; escalate and retry if a written
  314. // table gained a unique index.
  315. retry := false
  316. for i, t := range tables {
  317. if exclusive[i] {
  318. continue
  319. }
  320. if _, written := s.overlay[t]; !written {
  321. continue
  322. }
  323. uniq, err := s.table.hasUniqueIndex(t)
  324. if err != nil {
  325. unlock(locks, exclusive)
  326. return nil, nil, nil, nil, err
  327. }
  328. if uniq {
  329. retry = true
  330. break
  331. }
  332. }
  333. if retry {
  334. unlock(locks, exclusive)
  335. continue
  336. }
  337. unlockAll = func() { unlock(locks, exclusive) }
  338. return locks, exclusive, unlockAll, uniqueTables, nil
  339. }
  340. }
  341. func (s *Session) resetLocked() {
  342. s.inTx = false
  343. s.aborted = false
  344. s.overlay = make(map[string]map[string]*overlayEntry)
  345. s.log = nil
  346. s.reads = make(map[string]uint64)
  347. s.scanGens = make(map[string]uint64)
  348. s.predicateGens = make(map[string]predicateRead)
  349. }
  350. func (s *Session) stagePut(table, key string, row Row) {
  351. tl := strings.ToLower(table)
  352. if s.overlay[tl] == nil {
  353. s.overlay[tl] = make(map[string]*overlayEntry)
  354. }
  355. s.log = append(s.log, txMutation{table: tl, key: key, prev: s.overlay[tl][key]})
  356. s.overlay[tl][key] = &overlayEntry{row: cloneRow(row)}
  357. if _, ok := s.reads[key]; !ok {
  358. s.reads[key] = 0
  359. }
  360. }
  361. func (s *Session) stageDelete(table, key string, row Row) {
  362. tl := strings.ToLower(table)
  363. if s.overlay[tl] == nil {
  364. s.overlay[tl] = make(map[string]*overlayEntry)
  365. }
  366. s.log = append(s.log, txMutation{table: tl, key: key, prev: s.overlay[tl][key]})
  367. // Keep the deleted row so COMMIT can exempt its rowid from the unique-index
  368. // scan and release its unique value.
  369. s.overlay[tl][key] = &overlayEntry{row: cloneRow(row), absent: true}
  370. if _, ok := s.reads[key]; !ok {
  371. s.reads[key] = 0
  372. }
  373. }
  374. // GetByPK retrieves a row by primary key, observing the staged overlay in a
  375. // transaction and recording the observed LSN for validation.
  376. func (s *Session) GetByPK(table, pk string) (Row, error) {
  377. s.mu.Lock()
  378. defer s.mu.Unlock()
  379. if !s.inTx {
  380. return s.table.GetByPK(table, pk)
  381. }
  382. key := s.table.dataKey(table, pk)
  383. tl := strings.ToLower(table)
  384. if e, ok := s.overlay[tl][key]; ok {
  385. if e.absent {
  386. return nil, ErrKeyNotFound
  387. }
  388. return cloneRow(e.row), nil
  389. }
  390. row, lsn, err := s.table.getByPKWithLSN(table, pk)
  391. if err != nil {
  392. if err == ErrKeyNotFound {
  393. s.reads[key] = 0
  394. return nil, ErrKeyNotFound
  395. }
  396. return nil, err
  397. }
  398. s.reads[key] = lsn
  399. return row, nil
  400. }
  401. // Select scans a table, merging the staged overlay so a transaction sees its
  402. // own writes, and records per-row LSNs plus the table generation.
  403. func (s *Session) Select(table string, filter func(Row) bool) ([]Row, error) {
  404. s.mu.Lock()
  405. if !s.inTx {
  406. s.mu.Unlock()
  407. return s.table.Select(table, filter)
  408. }
  409. defer s.mu.Unlock()
  410. return s.selectLocked(table, filter)
  411. }
  412. // SelectLimited bounds an autocommit scan. Explicit transactions retain the
  413. // complete overlay/read-set behavior until bounded transactional scans have a
  414. // separate phantom/overlay contract; their caller still applies LIMIT later.
  415. func (s *Session) SelectLimited(table string, filter func(Row) bool, maxRows int) ([]Row, error) {
  416. s.mu.Lock()
  417. if s.inTx {
  418. defer s.mu.Unlock()
  419. return s.selectLocked(table, filter)
  420. }
  421. s.mu.Unlock()
  422. if maxRows == 0 {
  423. return []Row{}, nil
  424. }
  425. return s.table.SelectWithLimit(table, filter, maxRows, 0)
  426. }
  427. func (s *Session) selectLocked(table string, filter func(Row) bool) ([]Row, error) {
  428. schema, err := s.schema.GetSchema(table)
  429. if err != nil {
  430. return nil, err
  431. }
  432. tl := strings.ToLower(table)
  433. tableLock := s.table.tableLock(tl)
  434. tableLock.RLock()
  435. defer tableLock.RUnlock()
  436. if _, ok := s.scanGens[tl]; !ok {
  437. s.scanGens[tl] = s.table.generation(tl)
  438. }
  439. overlay := s.overlay[tl]
  440. var rows []Row
  441. err = s.table.scanRowsWithLSN(table, func(row Row, lsn uint64) (bool, error) {
  442. key := s.table.dataKey(table, fmt.Sprintf("%v", row[schema.PrimaryKey]))
  443. if _, ok := overlay[key]; ok {
  444. return false, nil
  445. }
  446. s.reads[key] = lsn
  447. if filter == nil || filter(row) {
  448. rows = append(rows, row)
  449. }
  450. return false, nil
  451. })
  452. if err != nil {
  453. return nil, err
  454. }
  455. for _, e := range overlay {
  456. if e.absent {
  457. continue
  458. }
  459. if filter == nil || filter(e.row) {
  460. rows = append(rows, cloneRow(e.row))
  461. }
  462. }
  463. return rows, nil
  464. }
  465. // SelectByIndex reads only matching durable rows while capturing their LSNs,
  466. // then merges the transaction overlay. The table generation protects against
  467. // matching rows being inserted or removed after the lookup.
  468. func (s *Session) SelectByIndex(table, indexName string, colValue interface{}) ([]Row, error) {
  469. s.mu.Lock()
  470. defer s.mu.Unlock()
  471. if !s.inTx {
  472. return s.table.SelectByIndex(table, indexName, colValue)
  473. }
  474. tableKey := strings.ToLower(table)
  475. index, err := s.schema.GetIndex(indexName)
  476. if err != nil {
  477. return nil, err
  478. }
  479. col := index.Columns[0].Name
  480. want := formatIndexValue(colValue)
  481. versions, predicate, err := s.table.selectByIndexWithLSN(table, indexName, colValue)
  482. if err != nil {
  483. return nil, err
  484. }
  485. if _, ok := s.predicateGens[predicate.valueKey]; !ok {
  486. s.predicateGens[predicate.valueKey] = predicateRead{table: tableKey, gen: predicate.valueGen}
  487. }
  488. if _, ok := s.predicateGens[predicate.wildcardKey]; !ok {
  489. s.predicateGens[predicate.wildcardKey] = predicateRead{table: tableKey, gen: predicate.wildcardGen}
  490. }
  491. overlay := s.overlay[tableKey]
  492. rows := make([]Row, 0, len(versions)+len(overlay))
  493. for _, version := range versions {
  494. if _, staged := overlay[version.key]; staged {
  495. continue
  496. }
  497. s.reads[version.key] = version.lsn
  498. rows = append(rows, version.row)
  499. }
  500. for _, entry := range overlay {
  501. if !entry.absent && formatIndexValue(entry.row[col]) == want {
  502. rows = append(rows, cloneRow(entry.row))
  503. }
  504. }
  505. return rows, nil
  506. }
  507. // SelectByIndexKey retrieves the rows whose index key equals valueKey, merging
  508. // the staged overlay so a transaction sees its own writes. valueKey is the
  509. // formatted key from TableManager.IndexRowKey, which supports plain, composite,
  510. // and expression indexes uniformly.
  511. func (s *Session) SelectByIndexKey(table string, idx *Index, valueKey string) ([]Row, error) {
  512. s.mu.Lock()
  513. defer s.mu.Unlock()
  514. if !s.inTx {
  515. versions, _, err := s.table.selectByIndexKeyWithLSN(table, idx.Name, valueKey)
  516. if err != nil {
  517. return nil, err
  518. }
  519. rows := make([]Row, len(versions))
  520. for i := range versions {
  521. rows[i] = versions[i].row
  522. }
  523. return rows, nil
  524. }
  525. tableKey := strings.ToLower(table)
  526. versions, predicate, err := s.table.selectByIndexKeyWithLSN(table, idx.Name, valueKey)
  527. if err != nil {
  528. return nil, err
  529. }
  530. if _, ok := s.predicateGens[predicate.valueKey]; !ok {
  531. s.predicateGens[predicate.valueKey] = predicateRead{table: tableKey, gen: predicate.valueGen}
  532. }
  533. if _, ok := s.predicateGens[predicate.wildcardKey]; !ok {
  534. s.predicateGens[predicate.wildcardKey] = predicateRead{table: tableKey, gen: predicate.wildcardGen}
  535. }
  536. overlay := s.overlay[tableKey]
  537. rows := make([]Row, 0, len(versions)+len(overlay))
  538. for _, version := range versions {
  539. if _, staged := overlay[version.key]; staged {
  540. continue
  541. }
  542. s.reads[version.key] = version.lsn
  543. rows = append(rows, version.row)
  544. }
  545. for _, entry := range overlay {
  546. if entry.absent {
  547. continue
  548. }
  549. key, kerr := s.table.IndexRowKey(idx, entry.row)
  550. if kerr != nil {
  551. return nil, kerr
  552. }
  553. if key == valueKey {
  554. rows = append(rows, cloneRow(entry.row))
  555. }
  556. }
  557. return rows, nil
  558. }
  559. // CountFast returns the exact row count, observing the staged overlay in a
  560. // transaction.
  561. func (s *Session) CountFast(table string) (int, error) {
  562. s.mu.Lock()
  563. defer s.mu.Unlock()
  564. if !s.inTx {
  565. return s.table.CountFast(table)
  566. }
  567. rows, err := s.selectLocked(table, nil)
  568. if err != nil {
  569. return 0, err
  570. }
  571. return len(rows), nil
  572. }
  573. // Insert stages an insert in a transaction, or performs a durable autocommit
  574. // insert otherwise.
  575. func (s *Session) Insert(table string, row Row) error {
  576. _, err := s.InsertWithRowID(table, row)
  577. return err
  578. }
  579. // InsertWithRowID stages an insert in a transaction, or performs a durable
  580. // autocommit insert otherwise, returning the actual generated ROWID.
  581. func (s *Session) InsertWithRowID(table string, row Row) (int64, error) {
  582. s.mu.Lock()
  583. defer s.mu.Unlock()
  584. if !s.inTx {
  585. return s.table.InsertWithRowID(table, row)
  586. }
  587. return s.insertLocked(table, row)
  588. }
  589. // InsertBulk stages or durably bulk-inserts multiple rows.
  590. func (s *Session) InsertBulk(table string, rows []Row) (int, error) {
  591. count, _, err := s.InsertBulkWithLastRowID(table, rows)
  592. return count, err
  593. }
  594. // InsertBulkWithLastRowID is InsertBulk, additionally returning the ROWID of the
  595. // last staged/persisted row (0 when nothing was inserted).
  596. func (s *Session) InsertBulkWithLastRowID(table string, rows []Row) (int, int64, error) {
  597. s.mu.Lock()
  598. defer s.mu.Unlock()
  599. if !s.inTx {
  600. return s.table.InsertBulkWithLastRowID(table, rows)
  601. }
  602. var lastRowID int64
  603. count := 0
  604. for _, row := range rows {
  605. rid, err := s.insertLocked(table, row)
  606. if err != nil {
  607. return count, lastRowID, err
  608. }
  609. lastRowID = rid
  610. count++
  611. }
  612. return count, lastRowID, nil
  613. }
  614. // insertLocked is the transaction insert helper (caller holds s.mu).
  615. func (s *Session) insertLocked(table string, row Row) (int64, error) {
  616. nr, key, err := s.table.prepareInsert(table, row)
  617. if err != nil {
  618. return 0, err
  619. }
  620. rowid, _ := rowIDFromRow(nr)
  621. schema, err := s.schema.GetSchema(table)
  622. if err != nil {
  623. return 0, err
  624. }
  625. pk := fmt.Sprintf("%v", nr[schema.PrimaryKey])
  626. tl := strings.ToLower(table)
  627. if e, ok := s.overlay[tl][key]; ok {
  628. if !e.absent {
  629. return 0, fmt.Errorf("duplicate primary key: %s", pk)
  630. }
  631. } else {
  632. _, lsn, err := s.table.getByPKWithLSN(table, pk)
  633. if err == nil {
  634. s.reads[key] = lsn
  635. return 0, fmt.Errorf("duplicate primary key: %s", pk)
  636. }
  637. if err != ErrKeyNotFound {
  638. return 0, err
  639. }
  640. s.reads[key] = 0
  641. }
  642. s.stagePut(table, key, nr)
  643. return rowid, nil
  644. }
  645. // UpdateByPK stages or durably applies a single-row update.
  646. func (s *Session) UpdateByPK(table, pk string, updateFn func(Row) (Row, error)) (Row, bool, error) {
  647. s.mu.Lock()
  648. if !s.inTx {
  649. s.mu.Unlock()
  650. return s.table.UpdateByPK(table, pk, updateFn)
  651. }
  652. defer s.mu.Unlock()
  653. schema, err := s.schema.GetSchema(table)
  654. if err != nil {
  655. return nil, false, err
  656. }
  657. key := s.table.dataKey(table, pk)
  658. row, err := s.getByPKLocked(table, pk)
  659. if err == ErrKeyNotFound {
  660. return nil, false, nil
  661. }
  662. if err != nil {
  663. return nil, false, err
  664. }
  665. oldRow := cloneRow(row)
  666. updates, err := s.runUpdateFn(updateFn, row)
  667. if err != nil {
  668. return nil, false, err
  669. }
  670. for name, value := range updates {
  671. for _, column := range schema.Columns {
  672. if strings.EqualFold(name, column.Name) {
  673. row[column.Name] = value
  674. break
  675. }
  676. }
  677. }
  678. if err := validateNotNull(schema, row); err != nil {
  679. return nil, false, err
  680. }
  681. s.stagePut(table, key, row)
  682. return oldRow, true, nil
  683. }
  684. // runUpdateFn releases the session lock while invoking the update callback so the
  685. // callback can run nested reads (e.g. a scalar subquery in SET) through the same
  686. // session without deadlocking on s.mu. A connection executes one statement at a
  687. // time, so the staged overlay cannot change while the callback runs. The lock is
  688. // re-acquired before returning.
  689. func (s *Session) runUpdateFn(updateFn func(Row) (Row, error), row Row) (Row, error) {
  690. s.mu.Unlock()
  691. defer s.mu.Lock()
  692. return updateFn(row)
  693. }
  694. // DeleteByPK stages or durably applies a single-row delete.
  695. func (s *Session) DeleteByPK(table, pk string) (Row, bool, error) {
  696. s.mu.Lock()
  697. defer s.mu.Unlock()
  698. if !s.inTx {
  699. return s.table.DeleteByPK(table, pk)
  700. }
  701. key := s.table.dataKey(table, pk)
  702. row, err := s.getByPKLocked(table, pk)
  703. if err == ErrKeyNotFound {
  704. return nil, false, nil
  705. }
  706. if err != nil {
  707. return nil, false, err
  708. }
  709. s.stageDelete(table, key, row)
  710. return row, true, nil
  711. }
  712. // getByPKLocked reads a row observing the overlay (caller holds s.mu).
  713. func (s *Session) getByPKLocked(table, pk string) (Row, error) {
  714. key := s.table.dataKey(table, pk)
  715. tl := strings.ToLower(table)
  716. if e, ok := s.overlay[tl][key]; ok {
  717. if e.absent {
  718. return nil, ErrKeyNotFound
  719. }
  720. return cloneRow(e.row), nil
  721. }
  722. row, lsn, err := s.table.getByPKWithLSN(table, pk)
  723. if err != nil {
  724. if err == ErrKeyNotFound {
  725. s.reads[key] = 0
  726. return nil, ErrKeyNotFound
  727. }
  728. return nil, err
  729. }
  730. s.reads[key] = lsn
  731. return row, nil
  732. }
  733. // UpdateFunc stages or durably applies a scan-based update.
  734. func (s *Session) UpdateFunc(table string, updateFn func(Row) (Row, error), filter func(Row) bool) (int, error) {
  735. s.mu.Lock()
  736. if !s.inTx {
  737. s.mu.Unlock()
  738. return s.table.UpdateFunc(table, updateFn, filter)
  739. }
  740. defer s.mu.Unlock()
  741. schema, err := s.schema.GetSchema(table)
  742. if err != nil {
  743. return 0, err
  744. }
  745. rows, err := s.selectLocked(table, filter)
  746. if err != nil {
  747. return 0, err
  748. }
  749. count := 0
  750. for _, row := range rows {
  751. oldRow := cloneRow(row)
  752. oldKey := s.table.dataKey(table, fmt.Sprintf("%v", row[schema.PrimaryKey]))
  753. updates, err := s.runUpdateFn(updateFn, row)
  754. if err != nil {
  755. return count, err
  756. }
  757. for name, value := range updates {
  758. for _, column := range schema.Columns {
  759. if strings.EqualFold(name, column.Name) {
  760. row[column.Name] = value
  761. break
  762. }
  763. }
  764. }
  765. if err := validateNotNull(schema, row); err != nil {
  766. return count, err
  767. }
  768. newKey := s.table.dataKey(table, fmt.Sprintf("%v", row[schema.PrimaryKey]))
  769. if newKey != oldKey {
  770. // The primary key changed: move the staged row instead of leaving a
  771. // stale copy under the old key, and refuse to overwrite another row.
  772. if _, gerr := s.getByPKLocked(table, fmt.Sprintf("%v", row[schema.PrimaryKey])); gerr == nil {
  773. return count, fmt.Errorf("duplicate primary key: %v", row[schema.PrimaryKey])
  774. } else if gerr != ErrKeyNotFound {
  775. return count, gerr
  776. }
  777. s.stageDelete(table, oldKey, oldRow)
  778. }
  779. s.stagePut(table, newKey, row)
  780. count++
  781. }
  782. return count, nil
  783. }
  784. // Delete stages or durably applies a scan-based delete.
  785. func (s *Session) Delete(table string, filter func(Row) bool) (int, error) {
  786. s.mu.Lock()
  787. if !s.inTx {
  788. s.mu.Unlock()
  789. return s.table.Delete(table, filter)
  790. }
  791. defer s.mu.Unlock()
  792. schema, err := s.schema.GetSchema(table)
  793. if err != nil {
  794. return 0, err
  795. }
  796. rows, err := s.selectLocked(table, filter)
  797. if err != nil {
  798. return 0, err
  799. }
  800. count := 0
  801. for _, row := range rows {
  802. key := s.table.dataKey(table, fmt.Sprintf("%v", row[schema.PrimaryKey]))
  803. s.stageDelete(table, key, row)
  804. count++
  805. }
  806. return count, nil
  807. }
  808. // ClearIndex passes through to the durable table manager.
  809. func (s *Session) ClearIndex(indexName, tableName string, columns []string) error {
  810. return s.table.ClearIndex(indexName, tableName, columns)
  811. }
  812. // InvalidateCache passes through to the durable table manager.
  813. func (s *Session) InvalidateCache(table string) {
  814. s.table.InvalidateCache(table)
  815. }
  816. // BuildIndex passes through to the durable table manager.
  817. func (s *Session) BuildIndex(indexName, tableName string, columns []string) error {
  818. return s.table.BuildIndex(indexName, tableName, columns)
  819. }