parent
a64fba0cb1
commit
013a0b4565
@ -0,0 +1,12 @@ |
||||
package me.bearns.fias.service; |
||||
|
||||
import me.bearns.fias.util.CatalogItem; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
||||
import java.io.InputStream; |
||||
import java.util.function.Predicate; |
||||
|
||||
public interface StreamSaver { |
||||
|
||||
public void process(InputStream is, CatalogItem conf, Predicate filter); |
||||
} |
@ -0,0 +1,98 @@ |
||||
package me.bearns.fias.service; |
||||
|
||||
import me.bearns.fias.util.CatalogItem; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.xml.bind.JAXBContext; |
||||
import javax.xml.bind.JAXBException; |
||||
import javax.xml.bind.Unmarshaller; |
||||
import javax.xml.stream.XMLEventReader; |
||||
import javax.xml.stream.XMLInputFactory; |
||||
import javax.xml.stream.XMLStreamException; |
||||
import javax.xml.stream.events.Attribute; |
||||
import javax.xml.stream.events.StartElement; |
||||
import javax.xml.stream.events.XMLEvent; |
||||
import java.io.InputStream; |
||||
import java.util.Iterator; |
||||
import java.util.function.Predicate; |
||||
|
||||
@Component |
||||
public class StreamSaverImpl implements StreamSaver{ |
||||
@Override |
||||
public void process(InputStream is, CatalogItem conf, Predicate filter) { |
||||
|
||||
try { |
||||
// create xml event reader for input stream
|
||||
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); |
||||
final XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(is); |
||||
|
||||
|
||||
// initialize jaxb
|
||||
final JAXBContext context = JAXBContext.newInstance(conf.getParentCls()); |
||||
final Unmarshaller unmarshaller = context.createUnmarshaller(); |
||||
|
||||
XMLEvent e = null; |
||||
|
||||
long countRead=0, countWrite=0, countErrors=0; |
||||
|
||||
// loop though the xml stream
|
||||
while (true) { |
||||
|
||||
if (!((e = xmlEventReader.peek()) != null)) break; |
||||
|
||||
countRead++; |
||||
|
||||
// check the event is a Document start element
|
||||
if (e.isStartElement() && ((StartElement) e).getName().equals(conf.getQName())) { |
||||
|
||||
Object obj; |
||||
try { |
||||
// unmarshall the document
|
||||
obj = unmarshaller.unmarshal(xmlEventReader, conf.getCls()).getValue(); |
||||
|
||||
} catch (Exception ex) { |
||||
countErrors++; |
||||
//log.error("Unmarshalling error {} in {}", ex.getMessage(), file.getName());
|
||||
|
||||
/*final Iterator attributes = ((StartElement) e).getAttributes(); |
||||
attributes.forEachRemaining( |
||||
(a) -> log.error("Object dump: {} {}", ((Attribute) a).getName(), ((Attribute) a).getValue()) |
||||
);*/ |
||||
|
||||
continue; |
||||
} |
||||
|
||||
//if(countRead%100000==0) log.info("Read {} item from {}", countRead, file.getName());
|
||||
|
||||
//Region filter
|
||||
if(filter != null && !filter.test(obj)) continue; |
||||
|
||||
//Save to repository
|
||||
conf.getRepository().save(obj); |
||||
|
||||
/*if(++countWrite%10000==0) { |
||||
repository.flush(); |
||||
log.info("Write {} items to repository.", countWrite); |
||||
}*/ |
||||
|
||||
} else { |
||||
xmlEventReader.next(); |
||||
} |
||||
} |
||||
} catch (XMLStreamException ex) { |
||||
ex.printStackTrace(); //createXMLEventReader & peak
|
||||
} catch (JAXBException ex) { |
||||
ex.printStackTrace(); //createUnmarshaller();
|
||||
} |
||||
|
||||
//write to DB from cache
|
||||
conf.getRepository().flush(); |
||||
|
||||
/*log.info("SUCSESS: read={}, write={}", countRead, countWrite); |
||||
if(countErrors > 0) { |
||||
log.warn("Unmarshalling errors suppressed: {}", countErrors); |
||||
log.warn("Check data consistency in {}", file.getName()); |
||||
}*/ |
||||
} |
||||
} |
@ -0,0 +1,75 @@ |
||||
package me.bearns.fias.util; |
||||
|
||||
import me.bearns.fias.repository.FiasVersionRepository; |
||||
import org.springframework.beans.factory.annotation.Autowired; |
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import javax.xml.namespace.QName; |
||||
import java.util.HashMap; |
||||
|
||||
@Component |
||||
public class Catalog { |
||||
|
||||
private final HashMap<String,Item> prefixMap = new HashMap<>(); |
||||
|
||||
public Catalog(FiasVersionRepository FIASJPA_REPOSITORY) { |
||||
prefixMap.put( |
||||
"AS_ADDROBJ_", |
||||
new Item( |
||||
null, |
||||
new QName("","Object"), |
||||
null, |
||||
null |
||||
) |
||||
); |
||||
prefixMap.put( |
||||
"AS_HOUSE_", |
||||
new Item( |
||||
null, |
||||
new QName("","House"), |
||||
null, |
||||
null |
||||
) |
||||
); |
||||
} |
||||
|
||||
public Item getByPrefix(String s){ |
||||
return prefixMap.get(s); |
||||
} |
||||
|
||||
public class Item implements CatalogItem{ |
||||
private Item(JpaRepository repository, QName qName, Class cls, Class parentCls) { |
||||
this.repository = repository; |
||||
this.qName = qName; |
||||
this.cls = cls; |
||||
this.parentCls = parentCls; |
||||
} |
||||
|
||||
private final JpaRepository repository; |
||||
private final QName qName; |
||||
private final Class cls; |
||||
private final Class parentCls; |
||||
|
||||
|
||||
@Override |
||||
public JpaRepository getRepository() { |
||||
return repository; |
||||
} |
||||
|
||||
@Override |
||||
public QName getQName() { |
||||
return qName; |
||||
} |
||||
|
||||
@Override |
||||
public Class getCls() { |
||||
return cls; |
||||
} |
||||
|
||||
@Override |
||||
public Class getParentCls() { |
||||
return parentCls; |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,12 @@ |
||||
package me.bearns.fias.util; |
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository; |
||||
|
||||
import javax.xml.namespace.QName; |
||||
|
||||
public interface CatalogItem { |
||||
public JpaRepository getRepository(); |
||||
public QName getQName(); |
||||
public Class getCls(); |
||||
public Class getParentCls(); |
||||
} |
Loading…
Reference in new issue