Skip to content

Commit

Permalink
Implemented tracking of changes in DB relation-based containers.
Browse files Browse the repository at this point in the history
  • Loading branch information
nmihajlovski committed Dec 28, 2014
1 parent 7dbb124 commit 73366f4
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 51 deletions.
@@ -0,0 +1,51 @@
package org.rapidoid.db.impl;

/*
* #%L
* rapidoid-db
* %%
* Copyright (C) 2014 Nikolche Mihajlovski
* %%
* Licensed 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.
* #L%
*/

import java.util.Set;

import org.rapidoid.util.U;

public class DbRelChangesTracker {

private final Set<Long> addedRelations = U.set();

private final Set<Long> removedRelations = U.set();

public void addedRelTo(long id) {
addedRelations.add(id);
removedRelations.remove(id);
}

public void removedRelTo(long id) {
removedRelations.add(id);
addedRelations.remove(id);
}

public Set<Long> getAddedRelations() {
return addedRelations;
}

public Set<Long> getRemovedRelations() {
return removedRelations;
}

}
129 changes: 78 additions & 51 deletions rapidoid-db/src/main/java/org/rapidoid/db/impl/DbRelsCommons.java
Expand Up @@ -21,6 +21,7 @@
*/ */


import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;


Expand All @@ -37,34 +38,22 @@ public abstract class DbRelsCommons<E> implements DbRelationInternals {


private final Collection<Long> ids; private final Collection<Long> ids;


protected final Set<Long> addedRelations = U.set(); protected final DbRelChangesTracker tracker = new DbRelChangesTracker();

protected final Set<Long> removedRelations = U.set();


public DbRelsCommons(Db db, String relation, Collection<Long> ids) { public DbRelsCommons(Db db, String relation, Collection<Long> ids) {
this.db = db; this.db = db;
this.relation = relation; this.relation = relation;
this.ids = ids; this.ids = ids;
} }


protected void addedRelTo(long id) {
addedRelations.add(id);
removedRelations.remove(id);
}

protected void removedRelTo(long id) {
removedRelations.add(id);
addedRelations.remove(id);
}

@Override @Override
public Set<Long> getAddedRelations() { public Set<Long> getAddedRelations() {
return addedRelations; return tracker.getAddedRelations();
} }


@Override @Override
public Set<Long> getRemovedRelations() { public Set<Long> getRemovedRelations() {
return removedRelations; return tracker.getRemovedRelations();
} }


@Override @Override
Expand Down Expand Up @@ -105,10 +94,6 @@ public boolean equals(Object obj) {
return true; return true;
} }


protected Collection<Long> getIds() {
return ids;
}

protected long getSingleId() { protected long getSingleId() {
U.must(ids.size() <= 1); U.must(ids.size() <= 1);
return !ids.isEmpty() ? ids.iterator().next() : -1; return !ids.isEmpty() ? ids.iterator().next() : -1;
Expand All @@ -119,10 +104,6 @@ public Object serialized() {
return U.map("relation", relation, "ids", ids); return U.map("relation", relation, "ids", ids);
} }


public void clear() {
ids.clear();
}

public boolean isEmpty() { public boolean isEmpty() {
return ids.isEmpty(); return ids.isEmpty();
} }
Expand All @@ -131,25 +112,11 @@ public int size() {
return ids.size(); return ids.size();
} }


@Override
public boolean addId(long id) {
return ids.add(id);
}

@Override
public boolean removeId(long id) {
return ids.remove(id);
}

@Override @Override
public boolean hasId(long id) { public boolean hasId(long id) {
return ids.contains(id); return ids.contains(id);
} }


protected boolean retainIds(Collection<Long> ids) {
return ids.retainAll(ids);
}

private List<Long> getIdsAsList() { private List<Long> getIdsAsList() {
return (List<Long>) ids; return (List<Long>) ids;
} }
Expand All @@ -170,32 +137,92 @@ protected long getIdAt(int index) {
return getIdsAsList().get(index); return getIdsAsList().get(index);
} }


protected void addIdAt(int index, long id) { protected int indexOfId(long id) {
getIdsAsList().add(index, id); return getIdsAsList().indexOf(id);
} }


protected boolean addIdsAt(int index, Collection<Long> items) { protected int lastIndexOfId(long id) {
return getIdsAsList().addAll(index, items); return getIdsAsList().lastIndexOf(id);
} }


protected long removeIdAt(int index) { protected List<Long> getIdSublist(int fromIndex, int toIndex) {
return getIdsAsList().remove(index); // TODO make it modifiable (solve the problem of tracking changes through the sublist)
return Collections.unmodifiableList(getIdsAsList().subList(fromIndex, toIndex));
} }


protected long setIdAt(int index, long id) { /*
return getIdsAsList().set(index, id); * THE FOLLOWING METHODS CHANGE THE IDs (IT IS IMPORTANT TO TRACK THE CHANGES):
*/

public void clear() {
for (long id : ids) {
tracker.removedRelTo(id);
}
ids.clear();
} }


protected List<Long> getIdSublist(int fromIndex, int toIndex) { @Override
return getIdsAsList().subList(fromIndex, toIndex); public boolean addId(long id) {
boolean changed = ids.add(id);

if (changed) {
tracker.addedRelTo(id);
}

return changed;
} }


protected int indexOfId(long id) { @Override
return getIdsAsList().indexOf(id); public boolean removeId(long id) {
boolean changed = ids.remove(id);

if (changed) {
tracker.removedRelTo(id);
}

return changed;
} }


protected int lastIndexOfId(long id) { protected boolean retainIds(Collection<Long> retainIds) {
return getIdsAsList().lastIndexOf(id); for (Long id : ids) {
if (!retainIds.contains(id)) {
tracker.removedRelTo(id);
}
}

return retainIds.retainAll(retainIds);
}

protected void addIdAt(int index, long id) {
getIdsAsList().add(index, id);
tracker.addedRelTo(id);
}

protected boolean addIdsAt(int index, Collection<Long> idsToAdd) {
boolean changed = getIdsAsList().addAll(index, idsToAdd);

for (Long id : idsToAdd) {
tracker.addedRelTo(id);
}

return changed;
}

protected long removeIdAt(int index) {
long removedId = getIdsAsList().remove(index);
tracker.removedRelTo(removedId);
return removedId;
}

protected long setIdAt(int index, long id) {
long removedId = getIdsAsList().set(index, id);

if (id != removedId) {
tracker.addedRelTo(id);
tracker.removedRelTo(removedId);
}

return removedId;
} }


} }

0 comments on commit 73366f4

Please sign in to comment.