@@ -6,8 +6,8 @@ use std::sync::{Arc, Mutex};
66
77const PAGES_PER_PAGE_RANGE : usize = 16 ;
88
9+ #[ derive( Clone ) ]
910pub struct PageRange {
10- // Max amount of base pages should be set to 16
1111 base_pages : Vec < Arc < Mutex < PhysicalPage > > > ,
1212 tail_pages : Vec < Arc < Mutex < PhysicalPage > > > ,
1313
@@ -16,6 +16,14 @@ pub struct PageRange {
1616}
1717
1818impl PageRange {
19+ fn new ( ) -> Self {
20+ PageRange {
21+ base_pages : vec ! [ Arc :: new( Mutex :: new( PhysicalPage :: new( ) ) ) ] ,
22+ tail_pages : vec ! [ ] ,
23+ first_non_full_page : 0 ,
24+ }
25+ }
26+
1927 fn write ( & mut self , value : i64 ) {
2028 // Get the current page
2129 let cur_page = self . base_pages [ self . first_non_full_page ] . clone ( ) ;
@@ -49,14 +57,6 @@ impl PageRange {
4957 fn has_capacity ( & self ) -> bool {
5058 self . first_non_full_page < PAGES_PER_PAGE_RANGE
5159 }
52-
53- fn new ( ) -> Self {
54- PageRange {
55- base_pages : vec ! [ Arc :: new( Mutex :: new( PhysicalPage :: new( ) ) ) ] ,
56- tail_pages : vec ! [ ] ,
57- first_non_full_page : 0 ,
58- }
59- }
6060}
6161
6262struct RecordAddress {
@@ -79,16 +79,26 @@ enum DatabaseError {
7979pub struct RTable {
8080 pub name : String ,
8181 pub primary_key_column : i64 ,
82- pub page_ranges : Vec < Arc < Mutex < PageRange > > > ,
82+ pub page_range : PageRange ,
8383
8484 // TODO: Fix this to be the correct usage
8585 pub page_directory : HashMap < i64 , i64 > ,
86- // TODO: Add index
86+
87+ #[ pyo3( get) ]
88+ pub num_columns : i64 ,
8789}
8890
8991impl RTable {
9092 fn create_column ( & mut self ) -> usize {
91- 0usize
93+ let i = self . page_range . base_pages . len ( ) ;
94+
95+ self . page_range
96+ . base_pages
97+ . push ( Arc :: new ( Mutex :: new ( PhysicalPage :: new ( ) ) ) ) ;
98+
99+ self . num_columns += 1 ;
100+
101+ return i;
92102 }
93103
94104 fn _merge ( ) {
@@ -105,9 +115,12 @@ pub struct RDatabase {
105115
106116#[ pymethods]
107117impl RDatabase {
108- #[ staticmethod]
109- fn ping ( ) -> String {
110- return String :: from ( "pong!" ) ;
118+ #[ new]
119+ fn new ( ) -> Self {
120+ RDatabase {
121+ tables : vec ! [ ] ,
122+ tables_hashmap : HashMap :: new ( ) ,
123+ }
111124 }
112125
113126 fn open ( & self , _path : String ) {
@@ -121,9 +134,10 @@ impl RDatabase {
121134 fn create_table ( & mut self , name : String , num_columns : i64 , primary_key_column : i64 ) -> RTable {
122135 let mut t = RTable {
123136 name : name. clone ( ) ,
124- page_ranges : vec ! [ ] ,
137+ page_range : PageRange :: new ( ) ,
125138 primary_key_column,
126139 page_directory : HashMap :: new ( ) ,
140+ num_columns : 1 ,
127141 } ;
128142
129143 // Create num_columns amount of columns
@@ -168,22 +182,13 @@ impl RDatabase {
168182 // c0, c1, c2, c3
169183 for ( _, id) in self . tables_hashmap . iter_mut ( ) {
170184 if * id > i {
171- println ! ( "{} {}" , * id, i) ;
172185 * id -= 1 ;
173186 }
174187 }
175188
176189 // Remove from tables hashmap
177190 self . tables_hashmap . remove ( & name) ;
178191 }
179-
180- #[ new]
181- fn new ( ) -> Self {
182- RDatabase {
183- tables : vec ! [ ] ,
184- tables_hashmap : HashMap :: new ( ) ,
185- }
186- }
187192}
188193
189194#[ cfg( test) ]
0 commit comments