X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FClusterChangeManager.java;fp=bundles%2Forg.simantics.db.procore%2Fsrc%2Ffi%2Fvtt%2Fsimantics%2Fprocore%2Finternal%2FClusterChangeManager.java;h=9f203c8af5a1402853fa10593fcaeb5049a1001d;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterChangeManager.java b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterChangeManager.java new file mode 100644 index 000000000..9f203c8af --- /dev/null +++ b/bundles/org.simantics.db.procore/src/fi/vtt/simantics/procore/internal/ClusterChangeManager.java @@ -0,0 +1,65 @@ +package fi.vtt.simantics.procore.internal; + +import gnu.trove.set.hash.THashSet; + +import java.util.Collection; +import java.util.LinkedList; + +public class ClusterChangeManager { + + final private THashSet clusterChanges = new THashSet(); + final private LinkedList removeList = new LinkedList(); + private int changeCounter = 0; + private int byteLimit = 65536; + + public void checkFlush() { + if(changeCounter < 50) return; + ClusterChange test = removeList.poll(); + if(test == null) return; + if(test.byteIndex > byteLimit) { + test.flushCollect(null); + } else { + removeList.add(test); + } + } + + public void updateChangeCounters() { + changeCounter = clusterChanges.size(); + byteLimit = (100-changeCounter)*ClusterChange.MAX_FIXED_BYTES; + } + + void addChange(ClusterChange change) { + clusterChanges.add(change); + updateChangeCounters(); + } + + Collection get() { + return clusterChanges; + } + + void add(ClusterChange change) { + if(clusterChanges.add(change)) { + removeList.add(change); + } else { + new Exception("trying to add change that already exists").printStackTrace(); + } + updateChangeCounters(); + } + + void remove(Collection changes) { + clusterChanges.removeAll(changes); + removeList.removeAll(changes); + updateChangeCounters(); + } + + int size() { + return clusterChanges.size(); + } + + void clear() { + clusterChanges.clear(); + removeList.clear(); + updateChangeCounters(); + } + +}