-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathInsert.java
More file actions
104 lines (91 loc) · 2.92 KB
/
Copy pathInsert.java
File metadata and controls
104 lines (91 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package simpledb;
import java.io.IOException;
/**
* Inserts tuples read from the child operator into the tableid specified in the
* constructor
*/
public class Insert extends Operator {
private static final long serialVersionUID = 1L;
private final TransactionId tid;
private final int tableId;
private DbIterator child;
private boolean hasBeenCalled;
/**
* Constructor.
*
* @param t
* The transaction running the insert.
* @param child
* The child operator from which to read tuples to be inserted.
* @param tableid
* The table in which to insert tuples.
* @throws DbException
* if TupleDesc of child differs from table into which we are to
* insert.
*/
public Insert(TransactionId t, DbIterator child, int tableid)
throws DbException {
this.tid = t;
this.child = child;
this.tableId = tableid;
}
public TupleDesc getTupleDesc() {
return new TupleDesc(new Type[]{Type.INT_TYPE});
}
@Override
public void open() throws DbException, TransactionAbortedException {
super.open();
child.open();
this.hasBeenCalled = false;
}
@Override
public void close() {
super.close();
child.close();
}
public void rewind() throws DbException, TransactionAbortedException {
close();
open();
}
/**
* Inserts tuples read from child into the tableid specified by the
* constructor. It returns a one field tuple containing the number of
* inserted records. Inserts should be passed through BufferPool. An
* instances of BufferPool is available via Database.getBufferPool(). Note
* that insert DOES NOT need check to see if a particular tuple is a
* duplicate before inserting it.
*
* @return A 1-field tuple containing the number of inserted records, or
* null if called more than once.
* @see Database#getBufferPool
* @see BufferPool#insertTuple
*/
protected Tuple fetchNext() throws TransactionAbortedException, DbException {
if (hasBeenCalled) {
return null;
}
int insertCount = 0;
hasBeenCalled = true;
while (child.hasNext()) {
Tuple tuple = child.next();
insertCount++;
try {
Database.getBufferPool().insertTuple(tid, tableId, tuple);
} catch (IOException e) {
throw new DbException("Insert failed");
}
}
Tuple tuple = new Tuple(getTupleDesc());
tuple.setField(0, new IntField(insertCount));
return tuple;
}
@Override
public DbIterator[] getChildren() {
return new DbIterator[]{child};
}
@Override
public void setChildren(DbIterator[] children) {
assert children.length == 1;
child = children[0];
}
}