/** * Generates new states for all the old states in the provided FSM. Stores the newly created * states into the {@link #newStates} list, and populates the mapping between old state IDs and * new state IDs in {@link #oldToNewStates}. * * @param the {@link FSMPDA} from which the old states are obtained. */ protected void createNewStates(FSMPDA fsm) { LinkedList<StatePDA> oldStatesQueue = new LinkedList<StatePDA>(); oldStatesQueue.add(fsm.getInitialState()); while (oldStatesQueue.size() > 0) { StatePDA anOldState = oldStatesQueue.removeFirst(); if (oldToNewStates.containsKey(anOldState.getIndex())) { // state already converted } else { // new state required SPTBase.State newState = new SPTBase.State(); newStates.add(newState); oldToNewStates.put(anOldState.getIndex(), newStates.size() - 1); // queue all old states reachable from this state for (gate.fsm.Transition anOldTransition : anOldState.getTransitions()) { oldStatesQueue.add((StatePDA) anOldTransition.getTarget()); } // if state is final, set the rule value newState.rule = -1; if (anOldState.isFinal()) { RightHandSide rhs = anOldState.getAction(); for (int i = 0; i < rules.length; i++) { if (rules[i].getRHS() == rhs) { newState.rule = i; break; } } } } } }
public static OpenIntIntHashMap getIntegerMapTweetToPartitionColt( Map<Integer, Connection> cnxMap, OpenIntIntHashMap uidToPartition) throws SQLException { Connection conn = null; // try to connect and load from testmachine so faster String userName = "******"; String password = ""; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://testmachine:3334/"; String dbName = "twitter3"; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url + dbName, userName, password); } catch (Exception e) { System.out.println("could not load from testmachine"); conn = null; } System.out.println("starting to load huge lookup table"); if (conn == null) { conn = cnxMap.values().iterator().next(); } System.out.println("abt to query for tweet lookup table"); Statement stmt = conn.createStatement(); String query = "select uid, id from tweets"; // im not sure why there are nulls... ResultSet rs = stmt.executeQuery(query); System.out.println("got tweet results for lookup table"); OpenIntIntHashMap ret = new OpenIntIntHashMap(); int i = 0; while (rs.next()) { int uid = rs.getInt(1); int tid = rs.getInt(2); int part = uidToPartition.get(uid); assert part != 0 : "partition should not be 0"; // ret.put(tid, part); i++; } System.out.println("added " + i + " mappings to tweet lookup table"); rs.close(); stmt.close(); return ret; }
public static OpenIntIntHashMap getIntegerMapUidToPartitionColt(Map<Integer, Connection> cnxMap) throws SQLException { Connection conn = null; // try to connect and load from testmachine so faster String userName = "******"; String password = ""; String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://testmachine:3334/"; String dbName = "twitter3"; try { Class.forName(driver).newInstance(); conn = DriverManager.getConnection(url + dbName, userName, password); } catch (Exception e) { System.out.println("could not load from testmachine"); conn = null; } System.out.println("starting to load huge lookup table"); if (conn == null) { conn = cnxMap.values().iterator().next(); } String partitionTable = DebugUtil.PARTITION_TABLE; // "user3"; String partCol = DebugUtil.PARTITION_COLUMN; Statement stmt = conn.createStatement(); String query = "select uid, " + partCol + " from " + partitionTable + " where " + partCol + " is not null"; // im not sure why there are nulls... System.out.println("loading partitioning from table: " + partitionTable); ResultSet rs = stmt.executeQuery(query); System.out.println("got big result set"); OpenIntIntHashMap map = new OpenIntIntHashMap(); int i = 0; int unknownId = 0; int[] addTo = new int[5]; addTo[0] = 5; addTo[1] = 7; addTo[2] = 9; addTo[3] = 2; addTo[4] = 4; int whaleCnt = 0; while (rs.next()) { int part = rs.getInt(2); if (part >= 0) { part++; } // -1 ids should be assigned in 1 hop scenario else if (DebugUtil.TWITTER_ROUTER_TYPE.equals(DebugUtil.TwitterRouterType.ONE_HOP)) { part = (whaleCnt % 10) + 1; whaleCnt++; } map.put(rs.getInt(1), part); i++; } // now get ids with null partition id and add them to other partitions query = "select uid from " + partitionTable + " where " + partCol + " is null"; rs.close(); rs = stmt.executeQuery(query); while (rs.next()) { // map.put(rs.getInt(1), unknownId%9+2); if (partitionTable.equals("user")) { map.put(rs.getInt(1), addTo[unknownId % 5]); } else { map.put(rs.getInt(1), unknownId % 9 + 2); } unknownId++; } rs.close(); stmt.close(); System.out.println( "done loading huge lookup table with " + (i + unknownId) + " values in the mapping, " + unknownId + " of which were null and assigned to partitions with fewer tuples"); return map; }
/** * Parses the provided FSMPDA and converts the old transitions to new ones. The {@link #newStates} * list and the {@link #oldToNewStates} mapping should already be populated before this method is * called. * * @param fsm * @throws ResourceInstantiationException */ protected void createNewTransitions(FSMPDA fsm) throws ResourceInstantiationException { LinkedList<StatePDA> oldStatesQueue = new LinkedList<StatePDA>(); oldStatesQueue.add(fsm.getInitialState()); IntArrayList visitedOldStates = new IntArrayList(); while (oldStatesQueue.size() > 0) { StatePDA anOldState = oldStatesQueue.removeFirst(); if (visitedOldStates.contains(anOldState.getIndex())) { // state already processed -> nothing to do } else { if (!oldToNewStates.containsKey(anOldState.getIndex())) { throw new ResourceInstantiationException( "State mapping org.p4535992.mvc.error: " + "old state not associated with a new state!"); } SPTBase.State newState = newStates.get(oldToNewStates.get(anOldState.getIndex())); // now process all transitions List<SPTBase.Transition> newTransitions = new LinkedList<SPTBase.Transition>(); for (gate.fsm.Transition t : anOldState.getTransitions()) { TransitionPDA anOldTransition = (TransitionPDA) t; if (!visitedOldStates.contains(anOldTransition.getTarget().getIndex())) { oldStatesQueue.add((StatePDA) anOldTransition.getTarget()); } if (!oldToNewStates.containsKey(anOldTransition.getTarget().getIndex())) { throw new ResourceInstantiationException( "State mapping org.p4535992.mvc.error: " + "old target state not associated with a new state!"); } int newStateTarget = oldToNewStates.get(anOldTransition.getTarget().getIndex()); SPTBase.Transition newTransition = new SPTBase.Transition(); newTransitions.add(newTransition); newTransition.nextState = newStateTarget; newTransition.type = anOldTransition.getType(); if (newTransition.type != TransitionPDA.TYPE_CONSTRAINT) { continue; } Constraint[] oldConstraints = anOldTransition.getConstraints().getConstraints(); List<int[]> newConstraints = new ArrayList<int[]>(); for (int i = 0; i < oldConstraints.length; i++) { String annType = oldConstraints[i].getAnnotType(); int annTypeInt = annotationTypes.indexOf(annType); if (annTypeInt < 0) { annotationTypes.add(annType); annTypeInt = annotationTypes.size() - 1; } int[] newConstraint = new int[oldConstraints[i].getAttributeSeq().size() + 2]; newConstraints.add(newConstraint); newConstraint[0] = annTypeInt; newConstraint[1] = oldConstraints[i].isNegated() ? -1 : 0; int predId = 2; for (ConstraintPredicate oldPredicate : oldConstraints[i].getAttributeSeq()) { newConstraint[predId++] = convertPredicate(annType, oldPredicate); } } // now save the new constraints newTransition.constraints = new int[newConstraints.size()][]; newTransition.constraints = newConstraints.toArray(newTransition.constraints); } // convert the transitions list to an array newState.transitions = new SPTBase.Transition[newTransitions.size()]; newState.transitions = newTransitions.toArray(newState.transitions); // finally, mark the old state as visited. visitedOldStates.add(anOldState.getIndex()); } } }