package de.mrbesen.youtubecrawler; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.Random; import org.apache.log4j.Logger; import com.mysql.cj.jdbc.exceptions.MysqlDataTruncation; import de.mrbesen.youtubecrawler.Crawler.Video; public class DB implements Runnable { private Connection con; private String server = Config.prop.getProperty("db.host", "localhost"), user = Config.prop.getProperty("db.user", "ytcrawler"), pw = Config.prop.getProperty("db.pw", ""), db = Config.prop.getProperty("db.dbname", "ytcrawler"); private int port = Integer.parseInt(Config.prop.getProperty("db.port", "3306")); private Logger log = Logger.getLogger(DB.class.getName()); private ArrayList randombuffer = new ArrayList<>(100); private Random rand = new Random(); private Thread randomrefill = null; private int dbsize = 0; private StringBuilder tostorebuffer ; private int writebuffersize = 500; private int writebuffercurrentsize = 0; private StringBuilder totempbuffer; private int writetempbuffercurrentsize = 0; public DB() { try { connect(false); //set the database up! boolean found = false; ResultSet set = con.getMetaData().getCatalogs();//does the db exists? while(set.next()) { if(set.getString(1).equalsIgnoreCase(db)) { found = true; con.setCatalog(db); break; } } if(!found) {//DataBase not found, try to create log.warn("Database not found! tring to create!"); //create DB, table: konten / player / Transactions update("CREATE DATABASE `" + db + "` /*!40100 DEFAULT CHARACTER SET latin1*/;"); con.setCatalog(db); update("CREATE TABLE `videos` (`id` varchar(13) NOT NULL,`length` int(11) NOT NULL,`created` int(11) NOT NULL,`langcode` varchar(3) NOT NULL DEFAULT 'en',`category` int(11) DEFAULT NULL, PRIMARY KEY (`id`), UNIQUE KEY `id_UNIQUE` (`id`)) ENGINE=InnoDB DEFAULT CHARSET=latin1;"); update("CREATE TABLE `temp` ( `ytid` varchar(13) NOT NULL COMMENT 'a Table to store Video ids, when they are found to process them later', PRIMARY KEY (`ytid`), UNIQUE KEY `ytid_UNIQUE` (`ytid`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"); log.info("Database is set up!"); } refillbuffer(); //get db size dbsize(); //config data try { writebuffersize = Integer.parseInt(Config.prop.getProperty("db.writebuffersize")); } catch(NumberFormatException e) { log.warn("could not read the number \"" + Config.prop.getProperty("db.writebuffersize") + "\" from the config file. db.writebuffersize"); } tostorebuffer = new StringBuilder(writebuffersize); totempbuffer = new StringBuilder(writebuffersize); } catch (SQLException e) { log.error("Error while connecting to the database! ", e); } } private void dbsize() { try { ResultSet set = query("SELECT count(*) as count FROM `videos`;"); if(set != null) { if(set.next()) { dbsize = set.getInt(1); } } } catch(SQLException e) { e.printStackTrace(); } } public int getDBSize() { return dbsize; } private void connect(boolean selectdb) { try { //verbinden con = DriverManager.getConnection("jdbc:mysql://" + server + ":" + port + "/" + (selectdb ? db : "") + "?serverTimezone=UTC&verifyServerCertificate=false&useSSL=true&useUnicode=true&characterEncoding=utf-8", user, pw); }catch (SQLException e) { log.error("Error while connecting to the database! ", e); } } /** * removes all videos, that are known from the db * @param input * @return */ public List checkvideos(List input) { if(!input.isEmpty()) { StringBuilder ids = new StringBuilder(); for(int i = 0; i < input.size(); i++) { ids.append(',').append(input.get(i)); } String query = "SELECT `id` FROM `videos` WHERE concat('%',`id`,'%') LIKE '" + ids.toString() + "';"; ResultSet res = query(query); try { while(res.next()) { input.remove(res.getString(1)); } } catch(SQLException e) { e.printStackTrace(); } } return input; } /** * save the list of videos to the DB * @param input */ public void addVideos(ArrayList