Permalink
Browse files

#6635 Add exception when trying to use CSV/LineRecordReader without f…

…irst initializing it
  • Loading branch information...
AlexDBlack committed Oct 30, 2018
1 parent 68646b9 commit fdffabd38bc8e5f2498a144576864f7dc5c33fa8
@@ -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.*;
@@ -48,21 +49,25 @@
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()) {
@@ -98,6 +103,8 @@ public void initialize(Configuration conf, InputSplit split) throws IOException,

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

if (iter != null && iter.hasNext()) {
return true;
} else {
@@ -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;
@@ -189,6 +190,7 @@ public boolean hasNext() {
}

protected String readStringLine(){
Preconditions.checkState(initialized, "RecordReader has not been initialized before use");
Text t = (Text) super.next().iterator().next();
return t.toString();
}
@@ -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.