datapage.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. /*
  11. Package pageview contains object wrappers for different page types.
  12. DataPage
  13. DataPage is a page which holds actual data.
  14. FreeLogicalSlotPage
  15. FreeLogicalSlotPage is a page which holds information about free logical slots.
  16. The page stores the slot location in a slotinfo data structure.
  17. FreePhysicalSlotPage
  18. FreePhysicalSlotPage is a page which holds information about free physical slots.
  19. The page stores the slot location and its size in a slotinfo data structure
  20. (see util/slotsize.go).
  21. SlotInfoPage
  22. SlotInfoPage is the super-struct for all page views which manage slotinfos.
  23. Slotinfo are location (see util/location.go) pointers into the data store containing
  24. record id and offset.
  25. TransPage
  26. TransPage is a page which holds data to translate between physical and logical
  27. slots.
  28. */
  29. package pageview
  30. import (
  31. "fmt"
  32. "devt.de/krotik/eliasdb/storage/file"
  33. "devt.de/krotik/eliasdb/storage/paging/view"
  34. )
  35. /*
  36. OffsetFirst is a pointer to first element on the page
  37. */
  38. const OffsetFirst = view.OffsetData
  39. // OffsetData is declared in freephysicalslotpage
  40. /*
  41. DataPage data structure
  42. */
  43. type DataPage struct {
  44. *SlotInfoPage
  45. }
  46. /*
  47. NewDataPage creates a new page which holds actual data.
  48. */
  49. func NewDataPage(record *file.Record) *DataPage {
  50. checkDataPageMagic(record)
  51. dp := &DataPage{NewSlotInfoPage(record)}
  52. return dp
  53. }
  54. /*
  55. checkDataPageMagic checks if the magic number at the beginning of
  56. the wrapped record is valid.
  57. */
  58. func checkDataPageMagic(record *file.Record) bool {
  59. magic := record.ReadInt16(0)
  60. if magic == view.ViewPageHeader+view.TypeDataPage {
  61. return true
  62. }
  63. panic("Unexpected header found in DataPage")
  64. }
  65. /*
  66. DataSpace returns the available data space on this page.
  67. */
  68. func (dp *DataPage) DataSpace() uint16 {
  69. return uint16(len(dp.Record.Data()) - OffsetData)
  70. }
  71. /*
  72. OffsetFirst returns the pointer to the first element on the page.
  73. */
  74. func (dp *DataPage) OffsetFirst() uint16 {
  75. return dp.Record.ReadUInt16(OffsetFirst)
  76. }
  77. /*
  78. SetOffsetFirst sets the pointer to the first element on the page.
  79. */
  80. func (dp *DataPage) SetOffsetFirst(first uint16) {
  81. if first > 0 && first < OffsetData {
  82. panic(fmt.Sprint("Cannot set offset of first element on DataPage below ", OffsetData))
  83. }
  84. dp.Record.WriteUInt16(OffsetFirst, first)
  85. }