1
2
3
4 package org.treetank.io.jclouds;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.DataOutputStream;
8 import java.io.IOException;
9 import java.nio.ByteBuffer;
10 import java.util.concurrent.Callable;
11
12 import org.jclouds.blobstore.BlobStore;
13 import org.jclouds.blobstore.domain.Blob;
14 import org.jclouds.blobstore.domain.BlobBuilder;
15 import org.treetank.bucket.BucketFactory;
16 import org.treetank.bucket.UberBucket;
17 import org.treetank.bucket.interfaces.IBucket;
18 import org.treetank.exception.TTByteHandleException;
19 import org.treetank.exception.TTException;
20 import org.treetank.exception.TTIOException;
21 import org.treetank.io.IBackendWriter;
22 import org.treetank.io.bytepipe.IByteHandler.IByteHandlerPipeline;
23
24
25
26
27
28 public class JCloudsWriter implements IBackendWriter {
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49 private final JCloudsReader mReader;
50
51
52
53
54
55
56
57
58
59
60
61 public JCloudsWriter(BlobStore pBlobStore, BucketFactory pFac, IByteHandlerPipeline pByteHandler,
62 String pResourceName) throws TTException {
63 mReader = new JCloudsReader(pBlobStore, pFac, pByteHandler, pResourceName);
64
65
66
67
68
69
70
71
72
73
74 }
75
76
77
78
79 @Override
80 public IBucket read(long pKey) throws TTIOException {
81
82
83
84
85
86
87
88
89
90
91 final IBucket bucket = mReader.read(pKey);
92
93 return bucket;
94 }
95
96
97
98
99 @Override
100 public void write(final IBucket pBucket) throws TTIOException, TTByteHandleException {
101 try {
102
103
104
105
106
107 new WriteTask(pBucket).call();
108
109
110
111
112 } catch (final Exception exc) {
113 throw new TTIOException(exc);
114 }
115 }
116
117
118
119
120 @Override
121 public void close() throws TTIOException {
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139 mReader.close();
140 }
141
142
143
144
145 @Override
146 public UberBucket readUber() throws TTIOException {
147 return mReader.readUber();
148 }
149
150
151
152
153 @Override
154 public void writeUberBucket(UberBucket pBucket) throws TTException {
155 try {
156 long key = pBucket.getBucketKey();
157 write(pBucket);
158 BlobBuilder blobbuilder = mReader.mBlobStore.blobBuilder(Long.toString(-1L));
159 Blob blob = blobbuilder.build();
160 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
161 DataOutputStream dataOut = new DataOutputStream(byteOut);
162 dataOut.writeLong(key);
163 dataOut.close();
164 blob.setPayload(byteOut.toByteArray());
165 mReader.mBlobStore.putBlob(mReader.mResourceName, blob);
166 } catch (final IOException exc) {
167 throw new TTIOException(exc);
168 }
169
170 }
171
172
173
174
175
176
177
178 class WriteTask implements Callable<Long> {
179
180
181
182 final IBucket mBucket;
183
184 WriteTask(IBucket pBucket) {
185 this.mBucket = pBucket;
186 }
187
188 @Override
189 public Long call() throws Exception {
190 boolean finished = false;
191
192 while (!finished) {
193 try {
194 ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
195 DataOutputStream dataOut = new DataOutputStream(mReader.mByteHandler.serialize(byteOut));
196 mBucket.serialize(dataOut);
197 dataOut.close();
198
199
200 final byte[] data = byteOut.toByteArray();
201 final ByteBuffer buffer = ByteBuffer.allocate(4 + data.length);
202 buffer.putInt(buffer.capacity());
203 buffer.put(data);
204
205 BlobBuilder blobbuilder =
206 mReader.mBlobStore.blobBuilder(Long.toString(mBucket.getBucketKey()));
207 Blob blob = blobbuilder.build();
208
209 blob.setPayload(buffer.array());
210 mReader.mBlobStore.putBlob(mReader.mResourceName, blob);
211 } catch (Exception e) {
212
213 }
214 finished = mReader.mBlobStore.blobExists(mReader.mResourceName, Long.toString(mBucket.getBucketKey()));
215
216
217
218 }
219
220 return mBucket.getBucketKey();
221 }
222 }
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260 }