@@ -6,11 +6,21 @@ package schema
66// Each table is a new file
77
88import (
9+ // "fmt"
910 "github.com/chaitya62/noobdb/buffer"
1011 "github.com/chaitya62/noobdb/storage/disk"
1112 "github.com/chaitya62/noobdb/storage/page"
1213)
1314
15+ const (
16+ SCHEMA_ID = iota
17+ SCHEMA_TABLE_ID
18+ SCHEMA_TABLE_NAME
19+ SCHEMA_COLUMN_POSITION
20+ SCHEMA_COLUMN_NAME
21+ SCHEMA_COLUMN_TYPE
22+ )
23+
1424const schema_file = "schema.txt"
1525
1626type SchemaTable struct {
@@ -26,6 +36,55 @@ func (st *SchemaTable) Init() {
2636 st .bpm = buffer .GetNewBufferPoolManager (3 , st .dmi )
2737}
2838
39+ func (st * SchemaTable ) GetDefaultRow () page.Tuple {
40+ tuple := new (page.TupleImpl )
41+ tuple .Init (st ._schema [:])
42+ return tuple
43+ }
44+
45+ func (st * SchemaTable ) Insert (tuple page.Tuple ) error {
46+ no_of_pages := st .dmi .GetNumberOfPages ()
47+ var page_id uint32
48+ var _schemaPage * page.SchemaPage
49+ if no_of_pages == 0 {
50+ // create a new page
51+ // when schema table is empty
52+ page_id = uint32 (0 )
53+ _page := st .bpm .AllocatePage (page_id )
54+ _pageImpl := _page .(* page.PageImpl )
55+ _schemaPage = & page.SchemaPage {PageImpl : * _pageImpl }
56+ _schemaPage .Init ()
57+ } else {
58+ page_id = uint32 (no_of_pages - 1 )
59+ _page := st .bpm .GetPage (page_id )
60+ _pageImpl := _page .(* page.PageImpl )
61+ _schemaPage = & page.SchemaPage {PageImpl : * _pageImpl }
62+ }
63+
64+ st .bpm .PinPage (page_id )
65+ err := _schemaPage .InsertTuple (tuple )
66+ st .bpm .UnPinPage (page_id )
67+
68+ // page is full
69+ // allocate new page
70+ if err != nil {
71+ page_id = uint32 (page_id + 1 )
72+ _page := st .bpm .AllocatePage (page_id )
73+ _pageImpl := _page .(* page.PageImpl )
74+ _schemaPage = & page.SchemaPage {PageImpl : * _pageImpl }
75+ _schemaPage .Init ()
76+
77+ // we assume each tuple can fit in a page of size 4096 bytes
78+ st .bpm .PinPage (page_id )
79+ err = _schemaPage .InsertTuple (tuple )
80+ st .bpm .UnPinPage (page_id )
81+
82+ }
83+
84+ return nil
85+
86+ }
87+
2988//TODO: Convert this to an iterator design pattern
3089// https://ewencp.org/blog/golang-iterators/index.html
3190func (st * SchemaTable ) Iterator () {
@@ -46,3 +105,8 @@ func (st *SchemaTable) Iterator() {
46105 }
47106 }
48107}
108+
109+ // sanatize and persist everything to disk
110+ func (st * SchemaTable ) Close () {
111+ st .bpm .PersistAll ()
112+ }
0 commit comments