Skip to content
This repository has been archived by the owner on Dec 7, 2019. It is now read-only.

Commit

Permalink
Add safeReadAll() and return the result wrapped in a ReadResult object
Browse files Browse the repository at this point in the history
  • Loading branch information
riclage committed Dec 11, 2017
1 parent 8bb12d4 commit 5dccfac
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 283 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
import com.nytimes.android.external.fs3.filesystem.FileSystem;
import com.nytimes.android.external.store3.base.DiskAllRead;

import com.nytimes.android.external.store3.base.ReadResult;
import com.nytimes.android.external.store3.base.ReadResultFactory;
import java.io.FileNotFoundException;

import javax.annotation.Nonnull;

import io.reactivex.Observable;
import io.reactivex.exceptions.Exceptions;
import okio.BufferedSource;
import okio.Okio;

/**
* FSReader is used when persisting from file system
Expand All @@ -24,6 +25,25 @@ public FSAllReader(FileSystem fileSystem) {
this.fileSystem = fileSystem;
}

@Nonnull
@Override
public Observable<ReadResult<BufferedSource>> safeReadAll(@Nonnull final String path) {
return Observable.defer(() -> {
Observable<ReadResult<BufferedSource>> bufferedSourceObservable = null;
try {
bufferedSourceObservable = Observable
.fromIterable(fileSystem.list(path))
.flatMap(s ->
Observable.defer(() -> Observable.just(fileSystem.read(s)))
.map(ReadResultFactory::createSuccessResult)
.onErrorReturn(ReadResultFactory::createFailureResult));
} catch (FileNotFoundException e) {
throw Exceptions.propagate(e);
}
return bufferedSourceObservable;
});
}

@Nonnull
@Override
public Observable<BufferedSource> readAll(@Nonnull final String path) throws FileNotFoundException {
Expand All @@ -32,10 +52,13 @@ public Observable<BufferedSource> readAll(@Nonnull final String path) throws Fil
try {
bufferedSourceObservable = Observable
.fromIterable(fileSystem.list(path))
.flatMap(s ->
Observable.defer(() -> Observable.just(fileSystem.read(s)))
.onErrorReturn(throwable -> Okio.buffer(
ReadResultBufferedSourceFactory.createFailureResult(throwable))));
.map(s -> {
try {
return fileSystem.read(s);
} catch (FileNotFoundException e) {
throw Exceptions.propagate(e);
}
});
} catch (FileNotFoundException e) {
throw Exceptions.propagate(e);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.nytimes.android.external.fs3.filesystem.FileSystem;
import com.nytimes.android.external.store3.base.AllPersister;
import com.nytimes.android.external.store3.base.ReadResult;
import com.nytimes.android.external.store3.base.impl.BarCode;

import java.io.FileNotFoundException;
Expand Down Expand Up @@ -35,6 +36,12 @@ public SourceAllPersister(FileSystem fileSystem) {
sourceFileWriter = new FSWriter<>(fileSystem, new BarCodeReadAllPathResolver());
}

@Nonnull
@Override
public Observable<ReadResult<BufferedSource>> safeReadAll(@Nonnull String path) {
return sourceFileAllReader.safeReadAll(path);
}

@Nonnull
@Override
public Observable<BufferedSource> readAll(@Nonnull final String path) throws FileNotFoundException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.nytimes.android.external.fs3.filesystem.FileSystem;
import com.nytimes.android.external.fs3.filesystem.FileSystemFactory;

import com.nytimes.android.external.store3.base.ReadResult;
import org.junit.Test;

import java.io.ByteArrayInputStream;
Expand Down Expand Up @@ -47,6 +48,26 @@ public void readAll() throws IOException {
}

@Test
public void safeReadAllWithCrash() throws IOException {
File tempDir = createTempDir();
FileSystem fileSystem = new CrashOnReadFileSystem(tempDir);

//write different data to File System for each barcode
fileSystem.write(FOLDER + "/key.txt", source(CHALLAH));
fileSystem.write(FOLDER + "/key_crash.txt", source(CHALLAH));
fileSystem.write(FOLDER + "/" + INNER_FOLDER + "/key2.txt", source(CHALLAH_CHALLAH));
FSAllReader reader = new FSAllReader(fileSystem);
//read back all values for the FOLDER
Observable<ReadResult<BufferedSource>> observable = reader.safeReadAll(FOLDER);
observable.test()
.assertValueAt(0, bufferedSourceReadResult ->
bufferedSourceReadResult.getResult().readUtf8().equals(CHALLAH))
.assertValueAt(1, bufferedSourceReadResult -> !bufferedSourceReadResult.isSuccess())
.assertValueAt(2, bufferedSourceReadResult ->
bufferedSourceReadResult.getResult().readUtf8().equals(CHALLAH_CHALLAH));
}

@Test(expected = RuntimeException.class)
public void readAllWithCrash() throws IOException {
File tempDir = createTempDir();
FileSystem fileSystem = new CrashOnReadFileSystem(tempDir);
Expand Down
Loading

0 comments on commit 5dccfac

Please sign in to comment.