Skip to content

Commit 8bb20a7

Browse files
committed
Core_FileWorkspaceMap enforcing system setup on write
1 parent 610efd0 commit 8bb20a7

2 files changed

Lines changed: 99 additions & 14 deletions

File tree

src/main/java/picoded/dstack/core/Core_FileWorkspace.java

Lines changed: 66 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ public class Core_FileWorkspace implements FileWorkspace {
3737
**/
3838
protected String _oid = null;
3939

40+
/**
41+
* Boolean flag, if true, indicates that the current FileWorkspace is uninitialized
42+
* if so, we will setup the workspace if needed.
43+
*/
44+
protected boolean _isUninitialized = false;
45+
4046
// Constructor
4147
//----------------------------------------------
4248

@@ -62,6 +68,7 @@ public Core_FileWorkspace(Core_FileWorkspaceMap inMain, String inOID) {
6268
// Issue a GUID
6369
if (_oid == null) {
6470
_oid = GUID.base58();
71+
_isUninitialized = true;
6572
}
6673

6774
if (_oid.length() < 4) {
@@ -97,6 +104,15 @@ public String _oid() {
97104
public void setupWorkspace() {
98105
main.setupWorkspace(_oid());
99106
}
107+
108+
/**
109+
* Calls setupWorkspace if _isUninitialized is true
110+
*/
111+
protected void setupUninitializedWorkspace() {
112+
if( _isUninitialized ) {
113+
setupWorkspace();
114+
}
115+
}
100116

101117
// File / Folder string normalization
102118
//--------------------------------------------------------------------------
@@ -150,6 +166,9 @@ private static String normalizeFolderPathString(final String folderPath) {
150166
* @return true, if file exists (and writable), false if it does not. Possible a folder
151167
*/
152168
public boolean fileExist(final String filepath) {
169+
if( _isUninitialized ) {
170+
return false;
171+
}
153172
return main.backend_fileExist(_oid, normalizeFilePathString(filepath));
154173
}
155174

@@ -159,6 +178,9 @@ public boolean fileExist(final String filepath) {
159178
* @param filepath in the workspace to delete
160179
*/
161180
public void removeFile(final String filepath) {
181+
if( _isUninitialized ) {
182+
return;
183+
}
162184
main.backend_removeFile(_oid, normalizeFilePathString(filepath));
163185
}
164186

@@ -173,6 +195,9 @@ public void removeFile(final String filepath) {
173195
* @return the file contents, null if file does not exists
174196
*/
175197
public InputStream readInputStream(final String filepath) {
198+
if( _isUninitialized ) {
199+
return null;
200+
}
176201
return main.backend_fileReadInputStream(_oid, normalizeFilePathString(filepath));
177202
}
178203

@@ -185,6 +210,7 @@ public InputStream readInputStream(final String filepath) {
185210
* @param data the content to write to the file
186211
**/
187212
public void writeInputStream(final String filepath, final InputStream data) {
213+
setupUninitializedWorkspace();
188214
main.backend_fileWriteInputStream(_oid, normalizeFilePathString(filepath), data);
189215
}
190216

@@ -199,6 +225,9 @@ public void writeInputStream(final String filepath, final InputStream data) {
199225
* @return the file contents, null if file does not exists
200226
*/
201227
public byte[] readByteArray(final String filepath) {
228+
if( _isUninitialized ) {
229+
return null;
230+
}
202231
return main.backend_fileRead(_oid, normalizeFilePathString(filepath));
203232
}
204233

@@ -211,6 +240,7 @@ public byte[] readByteArray(final String filepath) {
211240
* @param data the content to write to the file
212241
**/
213242
public void writeByteArray(final String filepath, final byte[] data) {
243+
setupUninitializedWorkspace();
214244
main.backend_fileWrite(_oid, normalizeFilePathString(filepath), data);
215245
}
216246

@@ -224,20 +254,8 @@ public void writeByteArray(final String filepath, final byte[] data) {
224254
* @param data the content to write to the file
225255
**/
226256
public void appendByteArray(final String filepath, final byte[] data) {
227-
// Normalize the file path
228-
String path = normalizeFilePathString(filepath);
229-
230-
// Get existing data
231-
byte[] read = readByteArray(path);
232-
if (read == null) {
233-
writeByteArray(path, data);
234-
}
235-
236-
// Append new data to existing data
237-
byte[] jointData = ArrayConv.addAll(read, data);
238-
239-
// Write the new joint data
240-
writeByteArray(path, jointData);
257+
setupUninitializedWorkspace();
258+
main.backend_fileAppendByteArray(_oid, normalizeFilePathString(filepath), data);
241259
}
242260

243261
// Folder Pathing support
@@ -250,6 +268,9 @@ public void appendByteArray(final String filepath, final byte[] data) {
250268
* @param folderPath in the workspace (note, folderPath is normalized to end with "/")
251269
*/
252270
public void removeFolderPath(final String folderPath) {
271+
if( _isUninitialized ) {
272+
return;
273+
}
253274
main.backend_removeFolderPath(_oid, normalizeFolderPathString(folderPath));
254275
}
255276

@@ -260,6 +281,9 @@ public void removeFolderPath(final String folderPath) {
260281
* @return true if folderPath is valid
261282
*/
262283
public boolean folderPathExist(final String folderPath) {
284+
if( _isUninitialized ) {
285+
return false;
286+
}
263287
return main.backend_folderPathExist(_oid, normalizeFolderPathString(folderPath));
264288
}
265289

@@ -269,6 +293,7 @@ public boolean folderPathExist(final String folderPath) {
269293
* @param folderPath in the workspace (note, folderPath is normalized to end with "/")
270294
*/
271295
public void ensureFolderPath(final String folderPath) {
296+
setupUninitializedWorkspace();
272297
main.backend_ensureFolderPath(_oid, normalizeFolderPathString(folderPath));
273298
}
274299

@@ -290,6 +315,9 @@ public void ensureFolderPath(final String folderPath) {
290315
* @return DataObject created timestamp in ms
291316
*/
292317
public long createdTimestamp(final String filepath) {
318+
if( _isUninitialized ) {
319+
return -1;
320+
}
293321
return main.backend_createdTimestamp(_oid, normalizeFilePathString(filepath));
294322
}
295323

@@ -302,6 +330,9 @@ public long createdTimestamp(final String filepath) {
302330
* @return DataObject created timestamp in ms
303331
*/
304332
public long modifiedTimestamp(final String filepath) {
333+
if( _isUninitialized ) {
334+
return -1;
335+
}
305336
return main.backend_modifiedTimestamp(_oid, normalizeFilePathString(filepath));
306337
}
307338

@@ -324,6 +355,9 @@ public long modifiedTimestamp(final String filepath) {
324355
* @param destinationFile
325356
*/
326357
public void moveFile(final String sourceFile, final String destinationFile) {
358+
if( _isUninitialized ) {
359+
return;
360+
}
327361
main.backend_moveFile(_oid, normalizeFilePathString(sourceFile),
328362
normalizeFilePathString(destinationFile));
329363
}
@@ -345,6 +379,9 @@ public void moveFile(final String sourceFile, final String destinationFile) {
345379
* @param destinationFolder
346380
*/
347381
public void moveFolderPath(final String sourceFolder, final String destinationFolder) {
382+
if( _isUninitialized ) {
383+
return;
384+
}
348385
main.backend_moveFolderPath(_oid, normalizeFolderPathString(sourceFolder),
349386
normalizeFolderPathString(destinationFolder));
350387
}
@@ -368,6 +405,9 @@ public void moveFolderPath(final String sourceFolder, final String destinationFo
368405
* @param destinationFile
369406
*/
370407
public void copyFile(final String sourceFile, final String destinationFile) {
408+
if( _isUninitialized ) {
409+
return;
410+
}
371411
main.backend_copyFile(_oid, normalizeFilePathString(sourceFile),
372412
normalizeFilePathString(destinationFile));
373413
}
@@ -389,6 +429,9 @@ public void copyFile(final String sourceFile, final String destinationFile) {
389429
* @param destinationFolder
390430
*/
391431
public void copyFolderPath(final String sourceFolder, final String destinationFolder) {
432+
if( _isUninitialized ) {
433+
return;
434+
}
392435
main.backend_copyFolderPath(_oid, normalizeFolderPathString(sourceFolder),
393436
normalizeFolderPathString(destinationFolder));
394437
}
@@ -406,6 +449,9 @@ public void copyFolderPath(final String sourceFolder, final String destinationFo
406449
*/
407450
public Set<String> getFileAndFolderPathSet(final String folderPath, final int minDepth,
408451
final int maxDepth) {
452+
if( _isUninitialized ) {
453+
return new HashSet<>();
454+
}
409455
return main.backend_getFileAndFolderPathSet(_oid, normalizeFolderPathString(folderPath),
410456
minDepth, maxDepth);
411457
}
@@ -419,6 +465,9 @@ public Set<String> getFileAndFolderPathSet(final String folderPath, final int mi
419465
* @return list of path strings - relative to the given folderPath
420466
*/
421467
public Set<String> getFilePathSet(final String folderPath, final int minDepth, final int maxDepth) {
468+
if( _isUninitialized ) {
469+
return new HashSet<>();
470+
}
422471
return main.backend_getFilePathSet(_oid, normalizeFolderPathString(folderPath), minDepth,
423472
maxDepth);
424473
}
@@ -433,6 +482,9 @@ public Set<String> getFilePathSet(final String folderPath, final int minDepth, f
433482
*/
434483
public Set<String> getFolderPathSet(final String folderPath, final int minDepth,
435484
final int maxDepth) {
485+
if( _isUninitialized ) {
486+
return new HashSet<>();
487+
}
436488
return main.backend_getFolderPathSet(_oid, normalizeFolderPathString(folderPath), minDepth,
437489
maxDepth);
438490
}

src/main/java/picoded/dstack/core/Core_FileWorkspaceMap.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package picoded.dstack.core;
22

3+
import picoded.core.conv.ArrayConv;
4+
35
// Java imports
46

57
// Picoded imports
@@ -230,6 +232,37 @@ public void backend_fileWriteInputStream(final String oid, final String filepath
230232
backend_fileWrite(oid, filepath, rawBytes);
231233
}
232234

235+
/**
236+
* [Internal use, to be extended in future implementation]
237+
*
238+
* Writes the full byte array of a file in the backend
239+
*
240+
* This overwrite is useful for backends which supports this flow.
241+
* Else it would simply be a wrapper over the non-stream version.
242+
*
243+
* @param ObjectID of workspace
244+
* @param filepath to use for the workspace
245+
* @param data to write the file with
246+
**/
247+
public void backend_fileAppendByteArray(final String oid, final String filepath,
248+
final byte[] data) {
249+
250+
// Get the existing byte array
251+
byte[] read = backend_fileRead(oid, filepath);
252+
253+
// Just write it as it is (read is null)
254+
if (read == null) {
255+
backend_fileWrite(oid, filepath, data);
256+
return;
257+
}
258+
259+
// Append new data to existing data
260+
byte[] jointData = ArrayConv.addAll(read, data);
261+
262+
// Write the new joint data
263+
backend_fileWrite(oid, filepath, jointData);
264+
}
265+
233266
// Folder Pathing support
234267
//--------------------------------------------------------------------------
235268

0 commit comments

Comments
 (0)