Skip to content

Commit

Permalink
#6635 Add exception when trying to use CSV/LineRecordReader without f…
Browse files Browse the repository at this point in the history
…irst initializing it
  • Loading branch information
AlexDBlack committed Oct 30, 2018
1 parent 68646b9 commit fdffabd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Expand Up @@ -28,6 +28,7 @@
import org.datavec.api.split.StringSplit;
import org.datavec.api.writable.Text;
import org.datavec.api.writable.Writable;
import org.nd4j.base.Preconditions;
import org.nd4j.linalg.primitives.Triple;

import java.io.*;
Expand All @@ -48,21 +49,25 @@ public class LineRecordReader extends BaseRecordReader {
protected int lineIndex = 0; //Line index within the current split
protected Configuration conf;
protected InputSplit inputSplit;
protected boolean initialized;

@Override
public void initialize(InputSplit split) throws IOException, InterruptedException {
this.inputSplit = split;
this.iter = getIterator(0);
this.initialized = true;
}

@Override
public void initialize(Configuration conf, InputSplit split) throws IOException, InterruptedException {
this.conf = conf;
initialize(split);
this.initialized = true;
}

@Override
public List<Writable> next() {
Preconditions.checkState(initialized, "Record reader has not been initialized");
List<Writable> ret = new ArrayList<>();

if (iter.hasNext()) {
Expand Down Expand Up @@ -98,6 +103,8 @@ public List<Writable> next() {

@Override
public boolean hasNext() {
Preconditions.checkState(initialized, "Record reader has not been initialized");

if (iter != null && iter.hasNext()) {
return true;
} else {
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.datavec.api.split.InputSplit;
import org.datavec.api.writable.Text;
import org.datavec.api.writable.Writable;
import org.nd4j.base.Preconditions;

import java.io.BufferedReader;
import java.io.DataInputStream;
Expand Down Expand Up @@ -189,6 +190,7 @@ protected List<Writable> parseLine(String line) {
}

protected String readStringLine(){
Preconditions.checkState(initialized, "RecordReader has not been initialized before use");
Text t = (Text) super.next().iterator().next();
return t.toString();
}
Expand Down
Expand Up @@ -314,4 +314,24 @@ public void testStreamReset() throws Exception {
e.printStackTrace();
}
}

@Test
public void testUsefulExceptionNoInit(){

CSVRecordReader rr = new CSVRecordReader(0, ',');

try{
rr.hasNext();
fail("Expected exception");
} catch (Exception e){
assertTrue(e.getMessage(), e.getMessage().contains("initialized"));
}

try{
rr.next();
fail("Expected exception");
} catch (Exception e){
assertTrue(e.getMessage(), e.getMessage().contains("initialized"));
}
}
}

0 comments on commit fdffabd

Please sign in to comment.