public int clear(String topology, long currentTimeMillis) { Connection c = getConnection(topology); int deletes = 0; try { deleteBooleanEvent.setLong(1, currentTimeMillis); deleteCountEvent.setLong(1, currentTimeMillis); deleteDistributionEvent.setLong(1, currentTimeMillis); deleteEmitEvent.setLong(1, currentTimeMillis); deleteNumberEvent.setLong(1, currentTimeMillis); deleteReceiveEvent.setLong(1, currentTimeMillis); deleteTimeEvent.setLong(1, currentTimeMillis); deletes = deleteBooleanEvent.executeUpdate(); deletes += deleteCountEvent.executeUpdate(); deletes += deleteDistributionEvent.executeUpdate(); deletes += deleteEmitEvent.executeUpdate(); deletes += deleteNumberEvent.executeUpdate(); deletes += deleteReceiveEvent.executeUpdate(); deletes += deleteTimeEvent.executeUpdate(); c.commit(); Logger.info(this.getClass(), "clear", "Deleted " + deletes + " events"); } catch (Exception e) { Logger.error(this.getClass(), "clear", e.getLocalizedMessage()); } return deletes; }
private Connection getConnection(String topology) { Logger.debug(this.getClass(), "getConnection", "enter"); if (conn == null) { try { File folder = new File(this.location + "/" + topology); boolean exists = folder.exists(); Class.forName("org.h2.Driver"); String url = "jdbc:h2:" + this.location + "/" + topology + "/db"; conn = DriverManager.getConnection(url, "sa", ""); if (!exists) { Statement statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_BOOLEAN); statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_COUNT); statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_DIST); statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_EMIT); statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_NUMBER); statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_RECEIVE); statement = conn.createStatement(); statement.execute(H2DataModel.CREATE_TIME); Logger.info(this.getClass(), "init", "Created Data Bases"); } /** Prepared statements */ this.insertBooleanEvent = conn.prepareStatement(H2DataModel.INSERT_BOOLEAN); this.insertCountEvent = conn.prepareStatement(H2DataModel.INSERT_COUNT); this.insertEmitEvent = conn.prepareStatement(H2DataModel.INSERT_EMIT); this.insertReceiveEvent = conn.prepareStatement(H2DataModel.INSERT_RECEIVE); this.insertNumberEvent = conn.prepareStatement(H2DataModel.INSERT_NUMBER); this.insertTimeEvent = conn.prepareStatement(H2DataModel.INSERT_TIME); this.insertDistributionEvent = conn.prepareStatement(H2DataModel.INSERT_DIST); this.deleteBooleanEvent = conn.prepareStatement(H2DataModel.DELETE_BOOLEAN); this.deleteCountEvent = conn.prepareStatement(H2DataModel.DELETE_COUNT); this.deleteReceiveEvent = conn.prepareStatement(H2DataModel.DELETE_RECEIVE); this.deleteEmitEvent = conn.prepareStatement(H2DataModel.DELETE_EMIT); this.deleteNumberEvent = conn.prepareStatement(H2DataModel.DELETE_NUMBER); this.deleteTimeEvent = conn.prepareStatement(H2DataModel.DELETE_TIME); this.deleteDistributionEvent = conn.prepareStatement(H2DataModel.DELETE_DIST); Logger.debug(this.getClass(), "getConnection", "Connected to " + url); } catch (Exception e) { Logger.error(this.getClass(), "getConnection", e.getLocalizedMessage()); } } return conn; }
public int save(Events e) { Connection c = getConnection(e.getTopology()); int inserts = 0; try { ArrayList<BooleanEvent> booleanEvents = e.getBools(); for (BooleanEvent event : booleanEvents) { insertBooleanEvent.setLong(1, e.getTimestamp()); insertBooleanEvent.setString(2, event.getServer()); insertBooleanEvent.setString(3, event.getComponent()); insertBooleanEvent.setString(4, event.getInstance()); insertBooleanEvent.setString(5, event.getAspect()); insertBooleanEvent.setLong(6, event.getTrues()); insertBooleanEvent.setLong(7, event.getFalses()); int done = insertBooleanEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert BooleanEvent"); } inserts += done; } ArrayList<CountEvent> countEvents = e.getCounts(); for (CountEvent event : countEvents) { insertCountEvent.setLong(1, e.getTimestamp()); insertCountEvent.setString(2, event.getServer()); insertCountEvent.setString(3, event.getComponent()); insertCountEvent.setString(4, event.getInstance()); insertCountEvent.setString(5, event.getAspect()); insertCountEvent.setLong(6, event.getCount()); int done = insertCountEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert CountEvent"); } inserts += done; } ArrayList<EmitEvent> emitEvents = e.getEmits(); for (EmitEvent event : emitEvents) { insertEmitEvent.setLong(1, e.getTimestamp()); insertEmitEvent.setString(2, event.getServer()); insertEmitEvent.setString(3, event.getComponent()); insertEmitEvent.setString(4, event.getInstance()); insertEmitEvent.setString(5, event.getAspect()); insertEmitEvent.setLong(6, event.getCount()); int done = insertEmitEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert EmitEvent"); } inserts += done; } ArrayList<ReceiveEvent> receiveEvents = e.getReceives(); for (ReceiveEvent event : receiveEvents) { insertReceiveEvent.setLong(1, e.getTimestamp()); insertReceiveEvent.setString(2, event.getServer()); insertReceiveEvent.setString(3, event.getComponent()); insertReceiveEvent.setString(4, event.getInstance()); insertReceiveEvent.setString(5, event.getAspect()); insertReceiveEvent.setLong(6, event.getCount()); int done = insertReceiveEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert ReceiveEvent"); } inserts += done; } ArrayList<NumberEvent> numberEvents = e.getNumbers(); for (NumberEvent event : numberEvents) { insertNumberEvent.setLong(1, e.getTimestamp()); insertNumberEvent.setString(2, event.getServer()); insertNumberEvent.setString(3, event.getComponent()); insertNumberEvent.setString(4, event.getInstance()); insertNumberEvent.setString(5, event.getAspect()); insertNumberEvent.setLong(6, event.getCount()); insertNumberEvent.setDouble(7, event.getValue()); int done = insertNumberEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert NumberEvent"); } inserts += done; } ArrayList<TimeEvent> timEvents = e.getTime(); for (TimeEvent event : timEvents) { insertTimeEvent.setLong(1, e.getTimestamp()); insertTimeEvent.setString(2, event.getServer()); insertTimeEvent.setString(3, event.getComponent()); insertTimeEvent.setString(4, event.getInstance()); insertTimeEvent.setString(5, event.getAspect()); insertTimeEvent.setLong(6, event.getCount()); insertTimeEvent.setLong(7, event.getNanos()); int done = insertTimeEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert NumberEvent"); } inserts += done; } ArrayList<DistributionEvent> distEvents = e.getDistributions(); for (DistributionEvent event : distEvents) { HashMap<Double, Long> dist = event.getDistribution(); for (Double value : dist.keySet()) { insertDistributionEvent.setLong(1, e.getTimestamp()); insertDistributionEvent.setString(2, event.getServer()); insertDistributionEvent.setString(3, event.getComponent()); insertDistributionEvent.setString(4, event.getInstance()); insertDistributionEvent.setString(5, event.getAspect()); insertDistributionEvent.setDouble(6, value); insertDistributionEvent.setLong(7, dist.get(value)); int done = insertDistributionEvent.executeUpdate(); if (done == 0) { Logger.error(this.getClass(), "clear", "Did not insert NumberEvent"); } inserts += done; } } c.commit(); Logger.info(this.getClass(), "save", "Added " + inserts + " events"); } catch (Exception ex) { Logger.error(this.getClass(), "save", ex.getLocalizedMessage()); } return inserts; }