Browse Source

fix the import scan

Michael Ekstrand 11 months ago
parent
commit
5ee11423df
2 changed files with 7 additions and 12 deletions
  1. 6
    9
      src/commands/import_json.rs
  2. 1
    3
      src/io.rs

+ 6
- 9
src/commands/import_json.rs

@@ -9,13 +9,12 @@ use structopt::StructOpt;
 use flate2::bufread::MultiGzDecoder;
 use indicatif::{ProgressBar, ProgressStyle};
 use sha1::Sha1;
-use anyhow::{Result, anyhow};
+use anyhow::{Result};
 use serde::{Deserialize};
 use toml;
 
 use crate::io::{HashRead, HashWrite, DelimPrinter};
 use crate::cleaning::*;
-use crate::tsv::split_first;
 use crate::db::{DbOpts, CopyRequest};
 use crate::tracking::StageOpts;
 use crate::logging::set_progress;
@@ -91,29 +90,27 @@ impl ImportSpec {
     let mut jsbuf = String::new();
     let mut n = 0;
     for line in src.lines() {
-      let mut line = line?;
+      let line = line?;
       let mut delim = DelimPrinter::new("\t", "\n");
-      for i in 0..self.format.len() {
-        let (fld, rest) = split_first(&line).ok_or_else(|| anyhow!("invalid line"))?;
-        match self.format[i] {
+      let split = line.split("\t");
+      for (fld, fc) in split.zip(&self.format) {
+        match fc {
           ColOp::Skip => (),
           ColOp::String => {
-            debug!("writing string field {}", fld);
             delim.preface(dst)?;
             write_pgencoded(dst, fld.as_bytes())?;
           },
           ColOp::JSON => {
             delim.preface(dst)?;
-            debug!("writing JSON field {}", fld);
             clean_json(&fld, &mut jsbuf);
             write_pgencoded(dst, jsbuf.as_bytes())?;
           }
         }
-        line = rest.to_string();
       }
       delim.end(dst)?;
       n += 1;
     }
+    info!("processed {} lines", n);
     Ok(n)
   }
 }

+ 1
- 3
src/io.rs

@@ -71,17 +71,15 @@ impl <'a> DelimPrinter<'a> {
 
   pub fn preface<W: io::Write>(&mut self, w: &mut W) -> io::Result<bool> {
     if self.first {
-      self.first = true;
+      self.first = false;
       Ok(false)
     } else {
-      debug!("writing preface");
       w.write_all(self.delim)?;
       Ok(true)
     }
   }
 
   pub fn end<W: io::Write>(&mut self, w: &mut W) -> io::Result<()> {
-    debug!("writing end");
     w.write_all(self.end)?;
     self.first = true;
     Ok(())