Skip to content

Commit

Permalink
#9 refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthony4m committed Nov 22, 2024
1 parent 5758968 commit ea6739c
Showing 1 changed file with 53 additions and 76 deletions.
129 changes: 53 additions & 76 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,99 +8,76 @@ import (
"time"
)

func checkError(err error, message string) {
if err != nil {
log.Fatalf("%s: %v", message, err)
}
}

func main() {
// Define the database directory and block size
dbDir := filepath.Join(".", "mydb")
blockSize := 128 // 128 bytes per block
blockSize := 128
const Filename = "datafile.dat"

// Initialize FileMgr
fm, err := kfile.NewFileMgr(dbDir, blockSize)
if err != nil {
log.Fatalf("Failed to initialize FileMgr: %v", err)
}
checkError(err, "Failed to initialize FileMgr")
defer func() {
err := fm.Close()
if err != nil {
log.Printf("Failed to close FileMgr: %v", err)
}
checkError(fm.Close(), "Failed to close FileMgr")
}()

// Append a new block to "datafile.dat"
blk, err := fm.Append("datafile.dat")
if err != nil {
log.Fatalf("Failed to append block: %v", err)
}
blk, err := fm.Append(Filename)
checkError(err, "Failed to append block")
fmt.Printf("Appended Block: %v\n", blk)

// Create a new Page and write data
page := kfile.NewPage(blockSize)
err = page.SetInt(0, 42)
if err != nil {
log.Fatalf("Failed to set int: %v", err)
}
err = page.SetString(4, "Hello, Go!")
if err != nil {
log.Fatalf("Failed to set string: %v", err)
}
pageManager := kfile.NewPageManager(blockSize)
newPage := kfile.NewPage(blockSize, Filename)
pageID := kfile.NewPageId(kfile.BlockId{Filename: Filename, Blknum: blk.Number()})

err = page.SetDate(15, time.Now())
if err != nil {
log.Fatalf("Failed to set string: %v", err)
}
err = page.SetBool(24, true)
if err != nil {
log.Fatalf("Failed to set string: %v", err)
}
err = page.SetDate(25, time.Now())
if err != nil {
log.Fatalf("Failed to set string: %v", err)
}
err = newPage.SetInt(0, 42)
checkError(err, "Failed to set int")

// Write the Page to the block
err = fm.Write(blk, page)
if err != nil {
log.Fatalf("Failed to write to block: %v", err)
}
fmt.Printf("Written to Block: %v\n", blk)
err = newPage.SetString(4, "Hello, Go!")
checkError(err, "Failed to set string")

// Read the Page from the block
readPage := kfile.NewPage(blockSize)
err = fm.Read(blk, readPage)
if err != nil {
log.Fatalf("Failed to read from block: %v", err)
}
currentTime := time.Now()
err = newPage.SetDate(15, currentTime)
checkError(err, "Failed to set date")

err = newPage.SetBool(50, true)
checkError(err, "Failed to set bool")

err = fm.Write(blk, newPage)
checkError(err, "Failed to write to block")

readPage := kfile.NewPage(blockSize, Filename)
pageManager.SetPage(pageID, readPage)

err = fm.Read(blk, pageManager, pageID)
checkError(err, "Failed to read from block")

// Retrieve data from the read Page
intVal, err := readPage.GetInt(0)
if err != nil {
log.Fatalf("Failed to get int: %v", err)
}
strVal, err := readPage.GetString(4, 10)
if err != nil {
log.Fatalf("Failed to get string: %v", err)
}
dateVal, err := readPage.GetDate(15)
if err != nil {
log.Fatalf("Failed to get string: %v", err)
}
boolVal, err := readPage.GetBool(25)
if err != nil {
log.Fatalf("Failed to get string: %v", err)
}
checkError(err, "Failed to get int")

strVal, err := readPage.GetString(4, len("Hello, Go!"))
checkError(err, "Failed to get string")

dateVal, err := readPage.GetDate(30)
checkError(err, "Failed to get date")

boolVal, err := readPage.GetBool(50)
checkError(err, "Failed to get bool")

fmt.Printf("Integer: %d, String: %s, Date: %s, Bool: %v\n",
intVal, strVal, dateVal, boolVal)

fmt.Printf("Stats - Blocks Read: %d, Blocks Written: %d\n", fm.BlocksRead(), fm.BlocksWritten())
stats := fm.ReadLog()
stats1 := fm.BlockSize()
stats2 := fm.BlocksRead()
stats3 := fm.BlocksWritten()

fmt.Printf("Read from Block: %v\n", blk)
fmt.Printf("Integer Value: %d\n", intVal)
fmt.Printf("String Value: %s\n", strVal)
fmt.Printf("Date Value: %s\n", dateVal)
fmt.Printf("Bool Value: %s\n", boolVal)
fmt.Printf("Stats Value: %s\n", stats)
fmt.Printf("Stats1 Value: %s\n", stats1)
fmt.Printf("Stats2 Value: %s\n", stats2)
fmt.Printf("Stats3Value: %s\n", stats3)
fmt.Printf("Stats4Value: %s\n", readPage.Contents())
fmt.Printf("Stats: %v\n", stats)
fmt.Printf("Block Size: %d\n", stats1)
fmt.Printf("Blocks Read: %d\n", stats2)
fmt.Printf("Blocks Written: %d\n", stats3)
fmt.Printf("Stats4Value: %v\n", readPage.Contents())
}

0 comments on commit ea6739c

Please sign in to comment.