Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CAY-2170 MergeToken sorting is highly unstable #159

Merged
merged 2 commits into from Dec 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -22,7 +22,6 @@
import org.apache.cayenne.dbsync.merge.factory.MergerTokenFactory;
import org.apache.cayenne.dbsync.merge.token.EmptyValueForNullProvider;
import org.apache.cayenne.dbsync.merge.token.MergerToken;
import org.apache.cayenne.dbsync.merge.token.TokenComparator;
import org.apache.cayenne.dbsync.merge.token.ValueForNullProvider;
import org.apache.cayenne.dbsync.reverse.filters.FiltersConfig;
import org.apache.cayenne.dbsync.reverse.filters.PatternFilter;
Expand Down Expand Up @@ -76,7 +75,7 @@ private List<MergerToken> createTokens() {
for(AbstractMerger<?, ?> merger : mergerList) {
tokens.addAll(merger.createMergeTokens());
}
Collections.sort(tokens, new TokenComparator());
Collections.sort(tokens);
return tokens;
}

Expand Down
@@ -0,0 +1,59 @@
/*****************************************************************
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
****************************************************************/

package org.apache.cayenne.dbsync.merge.token;

/**
* @since 4.0
*/
public abstract class AbstractMergerToken implements MergerToken {

private final String tokenName;

private final int sortingWeight;

protected AbstractMergerToken(String tokenName, int sortingWeight) {
this.tokenName = tokenName;
this.sortingWeight = sortingWeight;
}

@Override
public final String getTokenName() {
return tokenName;
}

@Override
public int getSortingWeight() {
return sortingWeight;
}

@Override
public int compareTo(MergerToken o) {
return getSortingWeight() - o.getSortingWeight();
}

@Override
public String toString() {
return getTokenName() + ' ' + getTokenValue() + ' ' + getDirection();
}

public boolean isEmpty() {
return false;
}
}
Expand Up @@ -65,4 +65,14 @@ public String getTokenName() {
public String getTokenValue() {
return reverse.getTokenValue();
}

@Override
public int getSortingWeight() {
return 0;
}

@Override
public int compareTo(MergerToken o) {
return -o.getSortingWeight();
}
}
Expand Up @@ -26,12 +26,14 @@
/**
* Represents a minimal atomic synchronization operation between database and Cayenne model.
*/
public interface MergerToken {
public interface MergerToken extends Comparable<MergerToken> {

String getTokenName();

String getTokenValue();

int getSortingWeight();

/**
* The direction of this token. One of {@link MergeDirection#TO_DB} or
* {@link MergeDirection#TO_MODEL}
Expand Down

This file was deleted.

Expand Up @@ -22,6 +22,7 @@
import org.apache.cayenne.dba.DbAdapter;
import org.apache.cayenne.dbsync.merge.context.MergeDirection;
import org.apache.cayenne.dbsync.merge.context.MergerContext;
import org.apache.cayenne.dbsync.merge.token.AbstractMergerToken;
import org.apache.cayenne.dbsync.merge.token.MergerToken;
import org.apache.cayenne.log.JdbcEventLogger;
import org.apache.cayenne.map.DbAttribute;
Expand All @@ -37,22 +38,10 @@
* Common abstract superclass for all {@link MergerToken}s going from the model
* to the database.
*/
public abstract class AbstractToDbToken implements MergerToken, Comparable<MergerToken> {
public abstract class AbstractToDbToken extends AbstractMergerToken {

private final String tokenName;

protected AbstractToDbToken(String tokenName) {
this.tokenName = tokenName;
}

@Override
public final String getTokenName() {
return tokenName;
}

@Override
public final MergeDirection getDirection() {
return MergeDirection.TO_DB;
protected AbstractToDbToken(String tokenName, int sortingWeight) {
super(tokenName, sortingWeight);
}

@Override
Expand All @@ -62,7 +51,7 @@ public void execute(MergerContext mergerContext) {
}
}

protected void executeSql(MergerContext mergerContext, String sql) {
void executeSql(MergerContext mergerContext, String sql) {
JdbcEventLogger logger = mergerContext.getDataNode().getJdbcEventLogger();
logger.log(sql);

Expand All @@ -77,23 +66,19 @@ protected void executeSql(MergerContext mergerContext, String sql) {
}
}

@Override
public String toString() {
return getTokenName() + ' ' + getTokenValue() + ' ' + getDirection();
}
public abstract List<String> createSql(DbAdapter adapter);

public boolean isEmpty() {
return false;
@Override
public final MergeDirection getDirection() {
return MergeDirection.TO_DB;
}

public abstract List<String> createSql(DbAdapter adapter);

abstract static class Entity extends AbstractToDbToken {

private DbEntity entity;
private final DbEntity entity;

public Entity(String tokenName, DbEntity entity) {
super(tokenName);
protected Entity(String tokenName, int sortingWeight, DbEntity entity) {
super(tokenName, sortingWeight);
this.entity = entity;
}

Expand All @@ -104,20 +89,14 @@ public DbEntity getEntity() {
public String getTokenValue() {
return getEntity().getName();
}

public int compareTo(MergerToken o) {
// default order as tokens are created
return 0;
}

}

abstract static class EntityAndColumn extends Entity {

private DbAttribute column;
private final DbAttribute column;

public EntityAndColumn(String tokenName, DbEntity entity, DbAttribute column) {
super(tokenName, entity);
protected EntityAndColumn(String tokenName, int sortingWeight, DbEntity entity, DbAttribute column) {
super(tokenName, sortingWeight, entity);
this.column = column;
}

Expand All @@ -129,6 +108,5 @@ public DbAttribute getColumn() {
public String getTokenValue() {
return getEntity().getName() + "." + getColumn().getName();
}

}
}
Expand Up @@ -33,7 +33,7 @@
public class AddColumnToDb extends AbstractToDbToken.EntityAndColumn {

public AddColumnToDb(DbEntity entity, DbAttribute column) {
super("Add Column", entity, column);
super("Add Column", 50, entity, column);
}

/**
Expand Down Expand Up @@ -64,13 +64,4 @@ public List<String> createSql(DbAdapter adapter) {
public MergerToken createReverse(MergerTokenFactory factory) {
return factory.createDropColumnToModel(getEntity(), getColumn());
}

@Override
public int compareTo(MergerToken o) {
// add all AddRelationshipToDb to the end.
if (o instanceof AddRelationshipToDb) {
return -1;
}
return super.compareTo(o);
}
}
Expand Up @@ -34,7 +34,7 @@ public class AddRelationshipToDb extends AbstractToDbToken.Entity {
private DbRelationship relationship;

public AddRelationshipToDb(DbEntity entity, DbRelationship relationship) {
super("Add foreign key", entity);
super("Add foreign key", 110, entity);
this.relationship = relationship;
}

Expand Down Expand Up @@ -72,13 +72,4 @@ public boolean isEmpty() {
return relationship.isSourceIndependentFromTargetChange();
}

@Override
public int compareTo(MergerToken o) {
// add all AddRelationshipToDb to the end.
if (o instanceof AddRelationshipToDb) {
return super.compareTo(o);
}
return 1;
}

}
Expand Up @@ -35,7 +35,7 @@
public class CreateTableToDb extends AbstractToDbToken.Entity {

public CreateTableToDb(DbEntity entity) {
super("Create Table", entity);
super("Create Table", 40, entity);
}

@Override
Expand Down
Expand Up @@ -32,7 +32,7 @@
public class DropColumnToDb extends AbstractToDbToken.EntityAndColumn {

public DropColumnToDb(DbEntity entity, DbAttribute column) {
super("Drop Column", entity, column);
super("Drop Column", 20, entity, column);
}

@Override
Expand All @@ -50,14 +50,4 @@ public List<String> createSql(DbAdapter adapter) {
public MergerToken createReverse(MergerTokenFactory factory) {
return factory.createAddColumnToModel(getEntity(), getColumn());
}

@Override
public int compareTo(MergerToken o) {
// add all AddRelationshipToDb to the end.
if (o instanceof DropRelationshipToDb) {
return 1;
}
return super.compareTo(o);
}

}
Expand Up @@ -35,7 +35,7 @@ public class DropRelationshipToDb extends AbstractToDbToken.Entity {
private DbRelationship relationship;

public DropRelationshipToDb(DbEntity entity, DbRelationship relationship) {
super("Drop foreign key", entity);
super("Drop foreign key", 10, entity);
this.relationship = relationship;
}

Expand Down Expand Up @@ -73,13 +73,4 @@ public String getTokenValue() {
}
return relationship.getSourceEntity().getName() + "->" + relationship.getTargetEntityName();
}

@Override
public int compareTo(MergerToken o) {
// add all AddRelationshipToDb to the end.
if (o instanceof DropRelationshipToDb) {
return super.compareTo(o);
}
return -1;
}
}
Expand Up @@ -30,7 +30,7 @@
public class DropTableToDb extends AbstractToDbToken.Entity {

public DropTableToDb(DbEntity entity) {
super("Drop Table", entity);
super("Drop Table", 30, entity);
}

@Override
Expand Down
Expand Up @@ -36,7 +36,7 @@
public class SetAllowNullToDb extends AbstractToDbToken.EntityAndColumn {

public SetAllowNullToDb(DbEntity entity, DbAttribute column) {
super("Set Allow Null", entity, column);
super("Set Allow Null", 70, entity, column);
}

@Override
Expand Down