· 6 years ago · Mar 05, 2019, 04:24 AM
1 public void marshall() throws IOException {
2 /* not yet implemented */
3
4 // find the primary key column of the table, and its index if it exists
5 Column pkey = this.table.primaryKeyColumn();
6
7 int pkey_index = -10;
8 Object pkey_value = null;
9
10 if (pkey != null) {
11 pkey_index = pkey.getIndex();
12 pkey_value = this.values[pkey_index];
13 }
14
15
16
17
18 // Assigning the key value
19 TupleOutput keyBuffer = null;
20 if (pkey!= null) {
21 keyBuffer = new TupleOutput();
22 if (pkey_value.getClass().equals(Integer.class)) {
23 keyBuffer.writeInt((Integer)pkey_value);
24 } else {
25 keyBuffer.writeBytes((String)pkey_value);
26 }
27 byte[] bytes = keyBuffer.getBufferBytes();
28 int numBytes = keyBuffer.getBufferLength();
29 DatabaseEntry key_to_insert = new DatabaseEntry(bytes, 0, numBytes);
30 this.key = key_to_insert;
31 }
32
33
34 // Assigning the data value
35
36 int num_headers = this.values.length + 1;
37 int[] headers = new int[num_headers];
38
39 // to accomplish this, I'm going to create the header of offsets as if there were no primary key,
40 // and then subsequently perform elementwise subtraction if there is a key.
41
42 for (int i = 0; i < num_headers; i++) {
43 // Starting offset for all headers, but will have to move if the primary key is the first term
44 if (i == 0) {
45 headers[i] = num_headers * 4;
46 } else {
47 // down here I define each length of header by class.
48 if (this.values[i-1].getClass().equals(Integer.class)) {
49 headers[i] = headers[i-1] + 4;
50 } else if (this.values[i-1].getClass().equals(Double.class)) {
51 headers[i] = headers[i-1] + 8;
52 } else if (this.values[i-1].getClass().equals(Character.class)) {
53 headers[i] = headers[i-1] + 1;
54 } else {
55 headers[i] = headers[i-1] + ((String)this.values[i-1]).length();
56 }
57 }
58 }
59
60
61
62 // manipulation of some headers due to the presence of a primary key
63 if (pkey != null) {
64
65 int to_subtract = 0;
66
67 if (pkey_value.getClass().equals(Integer.class))
68 to_subtract = 4;
69 else if (pkey_value.getClass().equals(Double.class))
70 to_subtract = 8;
71 else if (pkey_value.getClass().equals(Character.class))
72 to_subtract = 1;
73 else
74 to_subtract = ((String)pkey_value).length();
75
76 for (int i = 0; i<num_headers; i++) {
77 if (i == pkey_index) {
78 headers[i] = -2;
79 } else if (i>pkey_index) {
80 headers[i] -= to_subtract;
81 }
82 }
83 }
84
85 //This is where the marshalling of the data occurs
86
87 TupleOutput valueBuffer = new TupleOutput();
88 //giving the headers of metadata
89 for (int i = 0; i<num_headers; i++) {
90 valueBuffer.writeInt(headers[i]);
91 }
92 // writing the data
93 for (int i = 0; i<this.values.length; i++) {
94 // we don't want to include our primary key in the row of marshalled data
95 if (i == pkey_index) {
96 continue;
97 // the rest of these if statements separate different write methods by class
98 } else if (this.values[i].getClass().equals(Integer.class)) {
99 valueBuffer.writeInt((Integer)this.values[i]);
100 } else if (this.values[i].getClass().equals(Double.class)) {
101 valueBuffer.writeDouble((Double)this.values[i]);
102 } else if (this.values[i].getClass().equals(Character.class)) {
103 valueBuffer.writeChar((Character)this.values[i]);
104 } else {
105 valueBuffer.writeBytes((String)this.values[i]);
106 }
107 }
108 valueBuffer.close();
109
110 byte[] bytes = valueBuffer.getBufferBytes();
111 int numBytes = valueBuffer.getBufferLength();
112 DatabaseEntry value = new DatabaseEntry(bytes, 0, numBytes);
113 this.data = value;
114
115
116 }