private void initSet(RedisSchnauzer table, ResultSet rs) { try { rs.first(); String preKey = table.table.SlaveKey; String key = ""; String member = ""; Double score = 0.0; String value = ""; int rowCount = 0; Boolean haveKey = table.table.haveKeyFields(); rs.beforeFirst(); RedisStructure type = table.getType(); while (rs.next()) { switch (type) { case Set: case SortedSet: if (haveKey) { key = rs.getString(1); member = rs.getString(2); score = rs.getDouble(3); } else { key = ""; member = rs.getString(1); score = rs.getDouble(2); } dbhelper.zincrby(preKey + key, score, member); break; case String: key = rs.getString(1); value = rs.getString(2); dbhelper.set(preKey + key, value); break; default: ErrorHelper.errExit(LOGGER.getName() + " '" + table.getType() + "' Not Impliment...."); } rowCount = rowCount + 1; } LOGGER.info("load " + preKey + " OK. rowcount=" + rowCount); rs.close(); } catch (Exception e) { ErrorHelper.errExit( LOGGER.getName() + Infos.Init + "RedisData" + Infos.Failed + ": " + table.table.SlaveKey + ": " + e.getMessage()); } }
private void doInitiate() { try { dbhelper.flushDataDB(); LOGGER.info("Redis flushdb " + Infos.OK); String sql = ""; MySQLDbHelper mhelper = new MySQLDbHelper(masterDb); for (int i = 0; i < tables.size(); i++) { RedisSchnauzer table = tables.get(i); sql = table.getSQL(); if (StrHelp.empty(sql)) ErrorHelper.errExit(Infos.SQLNotFound + table.table.SlaveKey); ResultSet rs = mhelper.getRS(sql); switch (table.getType()) { case Set: case SortedSet: case String: initSet(table, rs); break; default: ErrorHelper.errExit(LOGGER.getName() + " '" + table.getType() + "' Not Impliment...."); } } } catch (Exception e) { ErrorHelper.errExit(Infos.Init + "RedisData" + Infos.Failed + ": " + e.getMessage()); } }
public SchnauzerRedisMaster(DBConnectorConfig master, DBConnectorConfig slave, String args[]) { this.slaveDb = slave; this.masterDb = master; dbhelper = new RedisSlaveDBHelper(slaveDb); dbhelper.noLog = true; registgerTableReplicator(master); // needInitData = (args.length>0) && (args[0].equalsIgnoreCase("ReloadData")); needInitData = true; if (needInitData) { doInitiate(); getTheLastBinlogAndPos(); LOGGER.info("ReloadData" + Infos.OK); } dbhelper.noLog = false; }
@Override public boolean doWrite(ColumnTypeHelper helper, WriteRowsEvent event) { try { boolean doOne = false; for (int i = 0; i < tables.size(); i++) { RedisSchnauzer table = tables.get(i); if (StrHelp.notEqual(helper.databaseName, masterDb.dbname)) continue; if (StrHelp.notEqual(helper.tableName, table.getMasterTableName())) continue; doOne = true; try { innerWrite(helper, event, table); } catch (Exception e) { ErrorHelper.errExit( "[" + table.table.SlaveKey + "]" + Infos.DoInsert + Infos.Failed + e.getMessage()); } } if (!doOne) dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); } catch (Exception e) { ErrorHelper.errExit(Infos.RepFailed + e.getMessage()); } return true; }
private void innerDelete(ColumnTypeHelper helper, DeleteRowsEvent event, RedisSchnauzer table) throws UnsupportedEncodingException { List<Row> rows = event.getRows(); LOGGER.info(rows.size() + Infos.Row); int scoreIndex = table.getMemberIndex(); int memberIndex = table.getScoreIndex(); int valueIndex = table.getValueIndex(); Double score = 0.0; String member = ""; String value = ""; for (int j = 0; j < rows.size(); j++) { Row row = rows.get(j); List<Column> columns = row.getColumns(); if (!table.needReplicate(columns, helper)) continue; switch (table.getType()) { case String: dbhelper.delete(table.getKey(columns)); dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); break; case Set: score = -1.00; value = helper.getColStr( valueIndex, columns.get(valueIndex), (byte) 1, table.masterfields.get(valueIndex)); dbhelper.zincrby(table.getKey(columns), score, value); dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); break; case SortedSet: score = Double.parseDouble( helper.getColStr( scoreIndex, columns.get(scoreIndex), (byte) 1, table.masterfields.get(scoreIndex))) * table.scorefield.multiplier; member = helper.getColStr( memberIndex, columns.get(memberIndex), (byte) 1, table.masterfields.get(memberIndex)); dbhelper.zincrby(table.getKey(columns), -1 * score, member); dbhelper.setBinlogKey( getBinLogName(helper), Long.toString(helper.position), helper.tableName); break; case List: ErrorHelper.errExit( "innerDelete====" + Infos.Illegal + "RedisStructure:" + table.getType().toString()); break; default: ErrorHelper.errExit( "innerDelete====" + Infos.Illegal + "RedisStructure:" + table.getType().toString()); break; } } }