Skip to content

Commit e3fa294

Browse files
committed
テスト追加&バグ修正
1 parent 0021a4f commit e3fa294

4 files changed

Lines changed: 79 additions & 4 deletions

File tree

plan/multi_buffer_product_plan.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ func (m *MultiBufferProductPlan) Open() (scan.Scan, error) {
4141
return nil, fmt.Errorf("tried to create copy records from rhs in MultiBuffProduct: %w", err)
4242
}
4343

44-
return query.NewMultiBufferProductScan(m.tx, leftScan, temp.TableName(), temp.Layout())
44+
return query.NewMultiBufferProductScan(m.tx, leftScan, temp.TableName() + ".tbl", temp.Layout())
4545
}
4646

4747
func (m *MultiBufferProductPlan) copyRecordsFrom(p Plan) (*query.TempTable, error) {
4848
src, err := p.Open()
4949
if err != nil {
5050
return nil, fmt.Errorf("tried to open TablePlan in MultiBufferProduct: %w", err)
5151
}
52-
schema := m.Schema()
52+
schema := p.Schema()
5353
tt, err := query.NewTempTable(m.tx, schema)
5454
if err != nil {
5555
return nil, fmt.Errorf("tried to create TempTable in MultiBufferProduct: %w", err)

plan/multi_buffer_product_test.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package plan
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMultiBufferProduct(t *testing.T) {
8+
db, planner, _ := createTester(t)
9+
10+
// Create a transaction
11+
tx, err := db.NewTx()
12+
if err != nil {
13+
t.Fatalf("Failed to create transaction: %v", err)
14+
}
15+
defer tx.Commit()
16+
17+
// Create a query plan for todo table
18+
plan1, err := planner.CreateQueryPlan("SELECT id, title FROM todo", tx)
19+
if err != nil {
20+
t.Fatalf("Failed to create query plan: %v", err)
21+
}
22+
23+
// create a query plan for todo_detail table
24+
plan2, err := planner.CreateQueryPlan("SELECT todo_id, detail, some_score FROM todo_detail", tx)
25+
if err != nil {
26+
t.Fatalf("Failed to create query plan for todo_detail: %v", err)
27+
}
28+
29+
// Create a MultiBufferProductPlan
30+
multiBufferProductPlan := NewMultiBufferProductPcan(tx, plan1, plan2)
31+
if multiBufferProductPlan == nil {
32+
t.Fatal("Failed to create MultiBufferProductPlan")
33+
}
34+
35+
// check schema
36+
if len(multiBufferProductPlan.Schema().Fields()) != 5 {
37+
t.Errorf("Expected 5 fields, got %d", len(multiBufferProductPlan.Schema().Fields()))
38+
}
39+
if !multiBufferProductPlan.Schema().HasField("id") ||
40+
!multiBufferProductPlan.Schema().HasField("title") ||
41+
!multiBufferProductPlan.Schema().HasField("todo_id") ||
42+
!multiBufferProductPlan.Schema().HasField("detail") {
43+
t.Error("Schema does not contain expected fields")
44+
}
45+
46+
// Open the scan
47+
scan, err := multiBufferProductPlan.Open()
48+
if err != nil {
49+
t.Fatalf("Failed to open scan: %v", err)
50+
}
51+
defer scan.Close()
52+
53+
// check scan length
54+
length := 0
55+
for {
56+
ok, err := scan.Next()
57+
if err != nil {
58+
t.Fatalf("Failed to get next record: %v", err)
59+
}
60+
if !ok {
61+
break
62+
}
63+
length++
64+
}
65+
if length != 12 {
66+
t.Errorf("Expected 6 records, got %d", length)
67+
}
68+
}

query/multi_buffer_product_scan.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package query
22

33
import (
44
"fmt"
5+
"log"
56

67
"github.com/team-gonyo/gonyo-db/buffer"
78
"github.com/team-gonyo/gonyo-db/record"
@@ -51,7 +52,10 @@ func NewMultiBufferProductScan(
5152
func (m *MultiBufferProductScan) BeforeFirst() {
5253
m.nextBlockNum = 0
5354
// FIXME: エラー握りつぶし
54-
_, _ = m.useNextChunk()
55+
_, err := m.useNextChunk()
56+
if err != nil {
57+
log.Printf("failed to invoke MultiBufferProductScan#BeforeFirst(): %v", err)
58+
}
5559
}
5660

5761
// チャンクを移動する

tx/buffer_list.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ func (bl *bufferList) pin(blk file.BlockId) error {
3737
}
3838

3939
func (bl *bufferList) unpin(blk file.BlockId) {
40-
buff := bl.buffers[blk]
40+
buff, ok := bl.buffers[blk]
41+
if !ok {
42+
return // ブロックが存在しない場合は何もしない
43+
}
4144
bl.bufferManager.Unpin(buff)
4245

4346
bl.pins[blk]--

0 commit comments

Comments
 (0)