Skip to content

Commit

Permalink
handle writing better
Browse files Browse the repository at this point in the history
  • Loading branch information
chirst committed Jun 26, 2024
1 parent 3f00202 commit e49c778
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions pager/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,12 @@
// memory. It also handles locking.
package pager

// TODO think about the similarities and differences between pageCache and
// dirtyPages. Think about why the pageCache stores raw bytes of a page and the
// dirtyPages stores a pointer to a page. Think about how caching should be
// handled during a write. For instance, newPage hits dirtyPages, but not
// pageCache.
// TODO try and remove specific integer types in favor of just int.

import (
"bytes"
"encoding/binary"
"slices"
"sort"
"sync"

Expand Down Expand Up @@ -143,15 +139,23 @@ func (p *Pager) EndWrite() error {
}

func (p *Pager) GetPage(pageNumber uint16) *Page {
if v, hit := p.pageCache.Get(int(pageNumber)); hit {
ap := p.allocatePage(pageNumber, v)
if p.isWriting {
p.dirtyPages = append(p.dirtyPages, ap)
// During a write pages are collected in the dirtyPages buffer. These pages
// must be retrieved from the buffer as they are modified because the file
// is becoming outdated.
if p.isWriting {
dpn := slices.IndexFunc(p.dirtyPages, func(dp *Page) bool {
return dp.number == pageNumber
})
if dpn != -1 {
return p.dirtyPages[dpn]
}
} else {
if v, hit := p.pageCache.Get(int(pageNumber)); hit {
return p.allocatePage(pageNumber, v)
}
return ap
}
page := make([]byte, PAGE_SIZE)
// Page number subtracted by one since 0 is reserved as a pointer to nothing
// Page number subtracted by 1 since 0 is reserved as a pointer to nothing.
p.store.ReadAt(page, int64(ROOT_PAGE_START+(pageNumber-1)*PAGE_SIZE))
ap := p.allocatePage(pageNumber, page)
if p.isWriting {
Expand All @@ -164,10 +168,7 @@ func (p *Pager) GetPage(pageNumber uint16) *Page {
func (p *Pager) WritePage(page *Page) error {
// Page number subtracted by one since 0 is reserved as a pointer to nothing
_, err := p.store.WriteAt(page.content, int64(ROOT_PAGE_START+(page.GetNumber()-1)*PAGE_SIZE))
if err != nil {
return err
}
return nil
return err
}

func (p *Pager) writeMaxPageNumber() {
Expand Down

0 comments on commit e49c778

Please sign in to comment.