Skip to content

Commit 511116c

Browse files
committed
limit number of rows different per table
1 parent 4ec6322 commit 511116c

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ The tool compares md5 of the row, simply speaking `select concat(pk1, pk2) PK, m
1212

1313
The tool being optimized for tables very similar in content, it retrieves 1 row alternatively from master then slave table. As data is sorted by PK, it keeps a sorted list of differences (pk and md5 value) in memory. As soon as there is a match, any value lower than the match pair is immediately reported and removed from memory. Memory footprint shall then remain small.
1414

15+
In the case the tool finds more than N rows (N=100 by default) different, the tool proceeds to analyze the next table.
16+
1517
## Build and Test
1618

1719
Build only requires Maven and Java 7.

src/main/java/com/geckotechnology/mySqlDataCompare/MySQLTableDataComparer.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
public class MySQLTableDataComparer {
1212

13+
private static final int MAX_DIFFERENCES_PER_TABLE = 100;
14+
1315
private ArrayList<SchemaDifference> dataDifferences = new ArrayList<SchemaDifference>();
1416
private MySQLSchemaRetriever masterSchemaReader;
1517
private MySQLSchemaRetriever slaveSchemaReader;
@@ -41,6 +43,7 @@ public void compareTable(Table table) throws Exception {
4143
ArrayList<OneRow> unmatchedSlaveRows = new ArrayList<OneRow>();
4244
boolean hasMasterResultSetNext = true;
4345
boolean hasSlaveResultSetNext = true;
46+
int dataDifferencesSizeAtStart = dataDifferences.size();
4447

4548
while(hasMasterResultSetNext && hasSlaveResultSetNext) {
4649
//Get 1 row from master DB
@@ -57,6 +60,27 @@ public void compareTable(Table table) throws Exception {
5760
}
5861
else
5962
hasSlaveResultSetNext = false;
63+
//check now there are not too many differences
64+
if((dataDifferences.size() - dataDifferencesSizeAtStart) > MAX_DIFFERENCES_PER_TABLE) {
65+
dataDifferences.add(new SchemaDifference(Criticality.ERROR,
66+
table.getTableName(),
67+
DifferenceType.DATA_TOO_MANY_DIFFERENCES,
68+
"max rows:" + MAX_DIFFERENCES_PER_TABLE
69+
));
70+
break;
71+
}
72+
if(unmatchedMasterRows.size() > MAX_DIFFERENCES_PER_TABLE ||
73+
unmatchedSlaveRows.size() > MAX_DIFFERENCES_PER_TABLE) {
74+
dataDifferences.add(new SchemaDifference(Criticality.ERROR,
75+
table.getTableName(),
76+
DifferenceType.DATA_TOO_MANY_UNMATCHED_ROWS,
77+
"max rows:" + MAX_DIFFERENCES_PER_TABLE
78+
));
79+
//clearing the rows as it is not sure they are unmatched
80+
unmatchedMasterRows.clear();
81+
unmatchedSlaveRows.clear();
82+
break;
83+
}
6084
}
6185

6286
//rows not found in slave table

src/main/java/com/geckotechnology/mySqlDataCompare/SchemaDifference.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ enum DifferenceType {
2121
COLUMN_DIFFERENT_PRIMARY_KEY,
2222
DATA_ROW_DIFFERENT_MD5,
2323
DATA_ROW_MISSING_IN_SLAVE_TABLE,
24-
DATA_ROW_EXCESS_IN_SLAVE_TABLE
24+
DATA_ROW_EXCESS_IN_SLAVE_TABLE,
25+
DATA_TOO_MANY_UNMATCHED_ROWS,
26+
DATA_TOO_MANY_DIFFERENCES
2527
}
2628

2729
private Criticality criticality;

0 commit comments

Comments
 (0)