Skip to content

Commit 36aef93

Browse files
authored
Merge pull request #31 from picoded/mongodb-support
Mongodb support
2 parents 5d3eb16 + 2fbda38 commit 36aef93

18 files changed

Lines changed: 2538 additions & 146 deletions

src/main/java/picoded/dstack/FileWorkspace.java

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@
44
import picoded.core.conv.StringConv;
55

66
import java.io.File;
7+
import java.io.IOException;
78
import java.io.InputStream;
9+
import java.io.OutputStream;
810
import java.io.ByteArrayInputStream;
11+
import java.io.ByteArrayOutputStream;
912
import java.util.List;
1013
import java.util.Set;
1114

15+
import org.apache.commons.io.IOUtils;
16+
1217
/**
1318
* Represent a file storage backend for a workspace
1419
*
@@ -133,16 +138,46 @@ default void writeString(final String filepath, String content, String encoding)
133138
//--------------------------------------------------------------------------
134139

135140
/**
136-
* Get the input stream representation of a given filepath
141+
* Get the input stream representation of a given filepath.
142+
*
143+
* You are expected to close, the stream on your own, to avoid memory leaks
137144
*
138145
* @param filePath in the workspace to extract
139-
* @return the file contents, null if file does not exists
146+
* @return the file contents as an input stream, null if file does not exists
140147
*/
141148
default InputStream readInputStream(final String filePath) {
142149
byte[] byteArr = readByteArray(filePath);
143150
return new ByteArrayInputStream(byteArr);
144151
}
145152

153+
/**
154+
* Reads an input stream, and writes it to a fil, creating the file if it does not exist.
155+
* the parent directories of the file will be created if they do not exist.
156+
*
157+
* Note that depending on the implementaiton, this may not be optimized,
158+
* and may only return after the OutputStream is fully processedd.
159+
*
160+
* @param filepath in the workspace to extract
161+
* @param data the content to write to the file
162+
**/
163+
default void writeInputStream(final String filepath, final InputStream data) {
164+
// Converts it to bytearray respectively
165+
byte[] rawBytes = null;
166+
try {
167+
rawBytes = IOUtils.toByteArray(data);
168+
} catch (IOException e) {
169+
throw new RuntimeException(e);
170+
} finally {
171+
try {
172+
data.close();
173+
} catch (IOException e) {
174+
throw new RuntimeException(e);
175+
}
176+
}
177+
// Does the bytearray writes
178+
writeByteArray(filepath, rawBytes);
179+
}
180+
146181
//
147182
// Folder Pathing support
148183
//--------------------------------------------------------------------------

src/main/java/picoded/dstack/KeyLongMap.java

Lines changed: 5 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -180,39 +180,10 @@ default Long removeValue(Object key) {
180180
**/
181181
default Long addAndGet(Object key, Object delta) {
182182
//
183-
// NOTE : The default implmentation of addAndGet,
184-
// or getAndAdd relies on repetaed tries using
185-
// weakCompareAndSet, while functional.
186-
// Is highly inefficent in most cases
183+
// We simply use get and add, with the delta,
184+
// this reduce the amount of permutation needed to support
187185
//
188-
189-
// Validate and convert the key to String
190-
if (key == null) {
191-
throw new IllegalArgumentException("key cannot be null in addAndGet");
192-
}
193-
String keyAsString = key.toString();
194-
195-
// Attempt to update the key for 5 times before throwing exception
196-
for (int tries = 0; tries < 5; tries++) {
197-
// Retrieve value from key
198-
Long value = getValue(keyAsString);
199-
200-
// Assume value as 0 if not exist
201-
if (value == null) {
202-
value = new Long(0);
203-
}
204-
205-
// Calculate the updated value
206-
Long updatedValue = GenericConvert.toLong(delta) + value;
207-
208-
// Update the value with weakCompareAndSet and return
209-
if (weakCompareAndSet(keyAsString, value, updatedValue)) {
210-
return updatedValue;
211-
}
212-
}
213-
214-
// Throw exception due to number of retries exceeded the limit
215-
throw new RuntimeException("Number of retries exceeded limit for addAndGet");
186+
return getAndAdd(key, delta)+GenericConvert.toLong(delta);
216187
}
217188

218189
/**
@@ -233,7 +204,7 @@ default Long getAndAdd(Object key, Object delta) {
233204

234205
// Validate and convert the key to String
235206
if (key == null) {
236-
throw new IllegalArgumentException("key cannot be null in addAndGet");
207+
throw new IllegalArgumentException("key cannot be null in");
237208
}
238209
String keyAsString = key.toString();
239210

@@ -257,7 +228,7 @@ default Long getAndAdd(Object key, Object delta) {
257228
}
258229

259230
// Throw exception due to number of retries exceeded the limit
260-
throw new RuntimeException("Number of retries exceeded limit for addAndGet");
231+
throw new RuntimeException("Number of retries exceeded limit");
261232
}
262233

263234
/**

0 commit comments

Comments
 (0)