/******************************************************************************* * Copyright (c) 2012 IT Just working. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IT Just working - initial API and implementation *******************************************************************************/ package com.minres.scviewer.database.text; import java.nio.charset.CharsetDecoder; import java.util.Collection; import java.util.zip.GZIPInputStream import org.codehaus.groovy.ast.stmt.CatchStatement import org.mapdb.DB import org.mapdb.DBMaker import groovy.io.FileType import com.minres.scviewer.database.AssociationType import com.minres.scviewer.database.DataType import com.minres.scviewer.database.IWaveform import com.minres.scviewer.database.IWaveformDb import com.minres.scviewer.database.IWaveformDbLoader import com.minres.scviewer.database.RelationType import com.minres.scviewer.database.tx.ITxGenerator public class TextDbLoader implements IWaveformDbLoader{ private Long maxTime; IWaveformDb db; def streams = [] def relationTypes=[:] DB mapDb public TextDbLoader() { } @Override public Long getMaxTime() { return maxTime; } @Override public Collection getAllWaves() { return streams; } public Map getGeneratorsById() { TreeMap res = new TreeMap(); streams.each{TxStream stream -> stream.generators.each{res.put(it.id, id)} } return res; } static final byte[] x = "scv_tr_stream".bytes @Override boolean load(IWaveformDb db, File file) throws Exception { if(file.isDirectory() || !file.exists()) return false; this.db=db this.streams=[] try { def gzipped = isGzipped(file) if(isTxfile(gzipped?new GZIPInputStream(new FileInputStream(file)):new FileInputStream(file))){ def mapDbFile = File.createTempFile("."+file.name, null /*"tmp"*/, null /*file.parentFile*/) mapDbFile.delete() mapDbFile.deleteOnExit() this.mapDb = DBMaker .fileDB(mapDbFile) .fileMmapEnableIfSupported() .fileMmapPreclearDisable() .cleanerHackEnable() .allocateStartSize(64*1024*1024) .allocateIncrement(64*1024*1024) .make() // NPE here ---> parseInput(gzipped?new GZIPInputStream(new FileInputStream(file)):new FileInputStream(file)) streams.each{ TxStream stream -> stream.getWidth() } return true } } catch (IndexOutOfBoundsException e) { return false } catch (IllegalArgumentException e) { return false } catch (NumberFormatException e) { return false } catch(EOFException e) { return true; } catch(Exception e) { System.out.println("---->>> Exception "+e.toString()+" caught while loading database"); //System.out.println("---->>> Exception "+e.toString()+" caught while loading database. StackTrace following... "); //e.printStackTrace() } catch(Error e) { System.out.println("---->>> Exception "+e.toString()+" caught while loading database. StackTrace following... "); e.printStackTrace() } return false; } private static boolean isTxfile(InputStream istream) { byte[] buffer = new byte[x.size()] def readCnt = istream.read(buffer, 0, x.size()) istream.close() if(readCnt==x.size()){ for(int i=0; i def tokens = line.split(/\s+/) as ArrayList switch(tokens[0]){ case "scv_tr_stream": if ((matcher = line =~ /^scv_tr_stream\s+\(ID (\d+),\s+name\s+"([^"]+)",\s+kind\s+"([^"]+)"\)$/)) { def id = Integer.parseInt(matcher[0][1]) def stream = new TxStream(this, id, matcher[0][2], matcher[0][3]) streams<transaction.beginTime?maxTime:transaction.beginTime endTransaction=false break case "tx_end"://matcher = line =~ /^tx_end\s+(\d+)\s+(\d+)\s+(\d+)\s+([munpf]?s)/ def id = Integer.parseInt(tokens[1]) transaction = transactionsById[id] assert Integer.parseInt(tokens[2])==transaction.generator.id transaction.endTime = Long.parseLong(tokens[3])*stringToScale(tokens[4]) transaction.generator.end_attrs_idx=0; maxTime = maxTime>transaction.endTime?maxTime:transaction.endTime endTransaction=true break case "tx_record_attribute"://matcher = line =~ /^tx_record_attribute\s+(\d+)\s+"([^"]+)"\s+(\S+)\s*=\s*(.+)$/ def id = Integer.parseInt(tokens[1]) def name = tokens[2][1..-2] def type = tokens[3] as DataType def remaining = tokens.size()>5?tokens[5..-1].join(' '):"" transactionsById[id].attributes< getAllRelationTypes(){ return relationTypes.values(); } }