From 54724ca7906b24eb6a585967fb29155ac54fa0fb Mon Sep 17 00:00:00 2001 From: Mattias Wadman Date: Fri, 11 Mar 2022 11:09:35 +0100 Subject: [PATCH] wip --- format/sqlite3/sqlite3.go | 89 ++++- format/sqlite3/testdata/multi_tables.sql | 12 +- format/sqlite3/testdata/multi_tables.sql.db | Bin 28672 -> 28672 bytes .../sqlite3/testdata/multi_tables.sql.fqtest | 320 --------------- .../sqlite3/testdata/page_overflow.sql.fqtest | 158 -------- format/sqlite3/testdata/types.sql.fqtest | 376 ------------------ .../sqlite3/testdata/unused_pages.sql.fqtest | 233 ----------- 7 files changed, 90 insertions(+), 1098 deletions(-) diff --git a/format/sqlite3/sqlite3.go b/format/sqlite3/sqlite3.go index 99b95e1f2..ff2498ed6 100644 --- a/format/sqlite3/sqlite3.go +++ b/format/sqlite3/sqlite3.go @@ -62,6 +62,21 @@ func init() { }) } +type intStack struct { + s []int +} + +func (s *intStack) Push(n int) { s.s = append(s.s, n) } + +func (s *intStack) Pop() (int, bool) { + if len(s.s) == 0 { + return 0, false + } + var n int + n, s.s = s.s[0], s.s[1:] + return n, true +} + const sqlite3HeaderSize = 100 const ( @@ -106,12 +121,14 @@ var serialTypeMapper = scalar.Fn(func(s scalar.S) (scalar.S, error) { return s, nil }) +type pageType int + const ( - pageTypePtrmap = 0x00 - pageTypeBTreeIndexInterior = 0x02 - pageTypeBTreeTableInterior = 0x05 - pageTypeBTreeIndexLeaf = 0x0a - pageTypeBTreeTableLeaf = 0x0d + pageTypePtrmap pageType = 0x00 + pageTypeBTreeIndexInterior = 0x02 + pageTypeBTreeTableInterior = 0x05 + pageTypeBTreeIndexLeaf = 0x0a + pageTypeBTreeTableLeaf = 0x0d ) var pageTypeMap = scalar.UToSymStr{ @@ -299,6 +316,14 @@ func sqlite3DecodeTreePage(d *decode.D, h sqlite3Header, x int64, payLoadLen int } } +func sqlite3SeekPage(d *decode.D, h sqlite3Header, i int) { + pageOffset := h.pageSize * int64(i) + if i == 0 { + pageOffset += sqlite3HeaderSize + } + d.SeekAbs(pageOffset * 8) +} + func sqlite3Decode(d *decode.D, in interface{}) interface{} { var h sqlite3Header @@ -340,6 +365,49 @@ func sqlite3Decode(d *decode.D, in interface{}) interface{} { } }) + // pageTypes := map[int]pageType{} + // pageVisitStack := &intStack{} + // pageVisitStack.Push(0) + + // for { + // i, ok := pageVisitStack.Pop() + // if !ok { + // break + // } + // if _, ok := pageTypes[i]; ok { + // d.Fatalf("page %d already visited", i) + // } + + // sqlite3SeekPage(d, h, i) + // typ := d.U8() + + // switch typ { + // case pageTypeBTreeIndexInterior, + // pageTypeBTreeTableInterior: + + // d.U16() // start_free_blocks + // d.U16() // cell_start + // d.U8() // cell_fragments + // rightPointer := d.U32() + + // pageCells := d.U16() + // for i := uint64(0); i < pageCells; i++ { + + // } + + // switch typ { + // case pageTypeBTreeIndexInterior: + + // } + + // default: + // d.Fatalf("asd") + // } + + // } + + // return nil + d.FieldArray("pages", func(d *decode.D) { d.RangeSorted = false @@ -348,6 +416,16 @@ func sqlite3Decode(d *decode.D, in interface{}) interface{} { d.FieldValueStr("type", "page0_index_fill") }) + // for { + // i, ok := pageStack.Pop() + // if !ok { + // break + // } + // if _, ok := pageSeen[i]; ok { + // d.Fatalf("page %d already visited", i) + // } + // pageSeen[i] = struct{}{} + for i := 0; i < h.databaseSizePages; i++ { pageOffset := h.pageSize * int64(i) d.SeekAbs(pageOffset * 8) @@ -355,6 +433,7 @@ func sqlite3Decode(d *decode.D, in interface{}) interface{} { if i == 0 { d.SeekRel(sqlite3HeaderSize * 8) } + sqlite3SeekPage(d, h, i) d.FieldStruct("page", func(d *decode.D) { typ := d.FieldU8("type", pageTypeMap) diff --git a/format/sqlite3/testdata/multi_tables.sql b/format/sqlite3/testdata/multi_tables.sql index fb05445c4..7a285937b 100644 --- a/format/sqlite3/testdata/multi_tables.sql +++ b/format/sqlite3/testdata/multi_tables.sql @@ -1,17 +1,17 @@ CREATE TABLE aaa ( - cint int primary key, - ctext text + cint INT PRIMARY KEY, + ctext TEXT ); INSERT INTO aaa VALUES(123, "AAAAAAA"); CREATE TABLE bbb ( - cint int primary key, - ctext text + cint INT PRIMARY KEY, + ctext TEXT ); INSERT INTO bbb VALUES(456, "BBBBBBB"); CREATE TABLE ccc ( - cint int primary key, - ctext text + cint INT PRIMARY KEY, + ctext TEXT ); INSERT INTO ccc VALUES(789, "CCCCCCC"); diff --git a/format/sqlite3/testdata/multi_tables.sql.db b/format/sqlite3/testdata/multi_tables.sql.db index 7dfaa3c34ded4383b420dce1a5348a0337c1d699..68ea67ee134e90721d5f5ab91ea98c6823b67ead 100644 GIT binary patch delta 126 zcmZp8z}WDBal<}7c~8F(g@7PWU&o+G1#j0#9WDhRNG?gOC{YM;jR={1moG}0fEq4M WRhG#Q`D7-S@mmoxZ}WM6!vp|&=p|PG delta 126 zcmZp8z}WDBal<}7`OLf$g@U5Y+{B_vh3wQy9WDhRNG?gOC{X|rlkf6HDHBk`rK!p? U`5~Xo