1 2 12 package com.versant.core.server; 13 14 import com.versant.core.common.OID; 15 import com.versant.core.common.State; 16 import com.versant.core.metadata.ModelMetaData; 17 import com.versant.core.util.IntArray; 18 19 import javax.jdo.JDOFatalUserException; import java.util.Date ; 21 22 28 public final class PersistGraphFullSort extends PersistGraph { 29 30 private int[] oidLevels; 31 private int[] oidDepth; 32 33 public PersistGraphFullSort(ModelMetaData jmd, int maxSize) { 34 super(jmd, maxSize); 35 oidDepth = new int[maxSize]; 36 } 37 38 41 public void clear() { 42 super.clear(); 43 oidLevels = null; 44 } 45 46 55 public void sort() { 56 int size = this.size; 57 int[] oidEdgeStart = new int[size]; 58 int[] oidEdgeCount = new int[size]; 59 boolean[] rootVertex = new boolean[size]; 60 boolean[] isVisited = new boolean[size]; 61 boolean[] isInserted = new boolean[size]; 62 63 for (int i = 0; i < size; i++) { 64 rootVertex[i] = true; 65 isVisited[i] = false; 66 isInserted[i] = false; 67 } 68 69 int[] oidEdges = findEdges(size, oidEdgeStart, oidEdgeCount, 70 rootVertex); 71 72 for (int i = 0; i < size; i++) { 73 if (rootVertex[i]) { 74 topsort(i, oidEdges, oidEdgeStart, oidEdgeCount, 75 isVisited, isInserted); 76 } 77 } 78 79 super.sort(); 80 81 oidLevels = new int[oidDepth[size - 1] + 1]; 82 for (int i = 0; i < oidLevels.length; oidLevels[i++] = 0) ; 83 oidLevels[0] = 0; 84 85 for (int i = 0, j = 0; i < size; i++) { 86 if (j != oidDepth[i]) oidLevels[++j] = i; 87 } 88 89 oidIndexMap.clear(); } 91 92 98 private int topsort(int index, int[] oidEdges, int[] oidEdgeStart, 99 int[] oidEdgeCount, boolean[] isVisited, 100 boolean[] isInserted) throws JDOFatalUserException { 101 if (isVisited[index]) return 0; 102 int edgeCount = oidEdgeCount[index]; 103 if (edgeCount == 0) { if (!isInserted[index]) { 105 oidDepth[index] = 0; 106 isInserted[index] = true; 107 } 108 return 0; 109 } 110 isVisited[index] = true; int depth = 0; 112 int t = 0; 113 while (edgeCount > 0) { 114 t = topsort(oidEdges[oidEdgeStart[index] + (--edgeCount)], 115 oidEdges, oidEdgeStart, oidEdgeCount, 116 isVisited, isInserted); 117 depth = t > depth ? t : depth; 118 } 119 depth = depth + 1; if (!isInserted[index]) { 121 oidDepth[index] = depth; 122 isInserted[index] = true; 123 } 124 isVisited[index] = false; return depth; 126 } 127 128 131 private int[] findEdges(int size, int[] oidEdgeStart, 132 int[] oidEdgeCount, boolean[] rootVertex) { 133 IntArray edges = new IntArray(size); 134 int start; 135 int fin; 136 Date now = new Date (); 137 for (int i = 0; i < size; i++) { 138 start = oidEdgeStart[i] = edges.size(); 139 State ns = newStates[i]; 140 OID oid = oids[i]; 141 if (oid.isNew()) { 142 ns.updateAutoSetFieldsCreated(now); 143 } else { 144 ns.updateAutoSetFieldsModified(now, oldStates[i]); 145 } 146 ns.findDirectEdges(this, edges); 147 fin = edges.size(); 148 oidEdgeCount[i] = fin - start; 149 for (int j = start; j < fin; j++) { 150 rootVertex[edges.get(j)] = false; 151 } 152 } 153 return edges.toArray(); 154 } 155 156 162 protected int compare(int a, int b) { 163 int diff = oidDepth[a] - oidDepth[b]; 164 if (diff != 0) return diff; 165 return super.compare(a, b); 166 } 167 168 171 protected void swap(int index1, int index2) { 172 super.swap(index1, index2); 173 int tempDepth = oidDepth[index1]; 174 oidDepth[index1] = oidDepth[index2]; 175 oidDepth[index2] = tempDepth; 176 } 177 } 178 | Popular Tags |