use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.
the class PostgreSQLCommentNotificationParser method processEdgeLocalCommentNotification.
/**
* Parses the notifications from the database back end for local edge comments by using a regular
* expression. If the regular expression matches the supplied {@link PGNotification} notification,
* it is determined if the edge in question is loaded, and if a
* {@link CommentNotificationContainer} is build with the data from the notification.
*
* @param notification The {@link PGNotification} from the PostgreSQL database server.
* @param provider The {@link SQLProvider} which is used to communicate with the database.
*/
static CommentNotification processEdgeLocalCommentNotification(final PGNotification notification, final SQLProvider provider) {
final Matcher matcher = EDGE_LOCAL_PATTERN.matcher(notification.getParameter());
if (!matcher.find()) {
return null;
}
final Integer edgeId = Integer.parseInt(matcher.group(3));
final Integer commentId = matcher.group(4).equals("null") ? null : Integer.parseInt(matcher.group(4));
final INaviEdge edge = EdgeCache.get(provider).getEdgeById(edgeId);
if (edge == null) {
return null;
}
final CommentOperation operation = commentId == null ? CommentOperation.DELETE : CommentOperation.APPEND;
return new EdgeCommentNotificationContainer(edge, operation, CommentScope.LOCAL, commentId);
}
use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.
the class PostgreSQLEdgeLoader method loadEdges.
/**
* Loads the edges of a view.
*
* @param provider The connection to the database.
* @param view The view whose edges are loaded.
* @param nodeLookup Maps between node IDs and their corresponding node objects.
* @param edgeToGlobalCommentMap Maps between edge IDs and their associated comments.
*
* @return The loaded edges.
*
* @throws CouldntLoadDataException
*/
private static List<INaviEdge> loadEdges(final AbstractSQLProvider provider, final INaviView view, final Map<Integer, INaviViewNode> nodeLookup, final Map<Integer, ArrayList<IComment>> edgeToGlobalCommentMap) throws CouldntLoadDataException {
final String query = "SELECT * FROM load_view_edges(" + view.getConfiguration().getId() + ")";
List<CBend> currentPaths = new ArrayList<CBend>();
final Map<Integer, INaviEdge> commentIdToEdge = new HashMap<Integer, INaviEdge>();
final Map<Integer, INaviEdge> edgeIdToEdge = new HashMap<Integer, INaviEdge>();
try {
final CConnection connection = provider.getConnection();
final PreparedStatement statement = connection.getConnection().prepareStatement(query);
final ResultSet resultSet = statement.executeQuery();
try {
while (resultSet.next()) {
final int edgeId = resultSet.getInt("id");
if (edgeIdToEdge.containsKey(edgeId)) {
final INaviEdge edge = edgeIdToEdge.get(edgeId);
final double pathX = resultSet.getDouble("x");
final double pathY = resultSet.getDouble("y");
if (!resultSet.wasNull()) {
edge.addBend(pathX, pathY);
}
continue;
}
final int sourceNode = resultSet.getInt("source_node_id");
final int targetNode = resultSet.getInt("target_node_id");
Integer localCommentId = resultSet.getInt("comment_id");
if (resultSet.wasNull()) {
localCommentId = null;
}
final double x1 = resultSet.getDouble("x1");
final double y1 = resultSet.getDouble("y1");
final double x2 = resultSet.getDouble("x2");
final double y2 = resultSet.getDouble("y2");
final EdgeType type = EdgeType.valueOf(resultSet.getString("type").toUpperCase());
final Color color = new Color(resultSet.getInt("color"));
final boolean visible = resultSet.getBoolean("visible");
final boolean selected = resultSet.getBoolean("selected");
final INaviViewNode source = nodeLookup.get(sourceNode);
final INaviViewNode target = nodeLookup.get(targetNode);
final double pathX = resultSet.getDouble("x");
final double pathY = resultSet.getDouble("y");
if (!resultSet.wasNull()) {
currentPaths.add(new CBend(pathX, pathY));
}
final CNaviViewEdge edge = new CNaviViewEdge(edgeId, source, target, type, x1, y1, x2, y2, color, selected, visible, null, currentPaths, provider);
if (localCommentId != null) {
commentIdToEdge.put(localCommentId, edge);
}
final ArrayList<IComment> globalComments = edgeToGlobalCommentMap.containsKey(edgeId) ? edgeToGlobalCommentMap.get(edgeId) : null;
if ((globalComments != null) && (globalComments.size() != 0)) {
initializeGlobalComment(edge, globalComments, provider);
}
source.addOutgoingEdge(edge);
target.addIncomingEdge(edge);
edgeIdToEdge.put(edge.getId(), edge);
currentPaths = new ArrayList<CBend>();
}
if (!commentIdToEdge.isEmpty()) {
final HashMap<Integer, ArrayList<IComment>> commentIdToComments = PostgreSQLCommentFunctions.loadMultipleCommentsById(provider, commentIdToEdge.keySet());
for (final Entry<Integer, ArrayList<IComment>> commentIdToComment : commentIdToComments.entrySet()) {
commentIdToEdge.get(commentIdToComment.getKey()).initializeLocalComment(commentIdToComment.getValue());
}
}
} finally {
resultSet.close();
}
} catch (final SQLException exception) {
throw new CouldntLoadDataException("Error: Loading of view edges failed");
}
return Lists.newArrayList(edgeIdToEdge.values());
}
use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.
the class PostgreSQLViewSaver method save.
/**
* Saves a view to the database.
*
* @param provider The SQL provider that provides the connection.
* @param view The view to save to the database.
*
* @throws CouldntSaveDataException Thrown if the view could not be saved to the database.
*/
public static void save(final AbstractSQLProvider provider, final CView view) throws CouldntSaveDataException {
PostgreSQLViewSaver.checkArguments(provider, view);
final CConnection connection = provider.getConnection();
try {
PostgreSQLHelpers.beginTransaction(connection);
final int viewId = view.getConfiguration().getId();
final List<INaviViewNode> nodes = view.getGraph().getNodes();
final List<INaviEdge> edges = view.getGraph().getEdges();
PostgreSQLViewSaver.deleteNodes(connection, viewId);
// Store all nodes
PostgreSQLNodeSaver.writeNodes(provider, viewId, nodes);
// Store all edges
PostgreSQLEdgeSaver.writeEdges(provider, edges);
PostgreSQLHelpers.endTransaction(connection);
} catch (final SQLException exception) {
try {
PostgreSQLHelpers.rollback(connection);
} catch (final SQLException e) {
CUtilityFunctions.logException(e);
}
throw new CouldntSaveDataException(exception);
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.
the class EdgeCache method addEdges.
public void addEdges(final List<INaviEdge> edges) {
final ImmutableMap<Integer, INaviEdge> edgesMap = Maps.uniqueIndex(edges, new Function<INaviEdge, Integer>() {
@Override
public Integer apply(final INaviEdge edge) {
return edge.getId();
}
});
edgesByIdCache.putAll(edgesMap);
for (final INaviEdge edge : edges) {
if (edge.getSource() instanceof IAddressNode && edge.getTarget() instanceof IAddressNode) {
final IAddress sourceAddress = ((IAddressNode) edge.getSource()).getAddress();
final IAddress targetAddress = ((IAddressNode) edge.getTarget()).getAddress();
Integer sourceModuleId = null;
Integer targetModuleId = null;
if (edge.getSource() instanceof INaviCodeNode) {
sourceModuleId = getModuleId((INaviCodeNode) edge.getSource());
} else if (edge.getSource() instanceof INaviFunctionNode) {
sourceModuleId = getModuleId((INaviFunctionNode) edge.getSource());
}
if (edge.getTarget() instanceof INaviCodeNode) {
targetModuleId = getModuleId((INaviCodeNode) edge.getTarget());
} else if (edge.getTarget() instanceof INaviFunctionNode) {
targetModuleId = getModuleId((INaviFunctionNode) edge.getTarget());
}
if (targetModuleId != null && sourceModuleId != null) {
UpdateAddressModuleIdCache(sourceAddress, sourceModuleId, targetAddress, targetModuleId, edge);
}
}
}
}
use of com.google.security.zynamics.binnavi.disassembly.INaviEdge in project binnavi by google.
the class PostgreSQLEdgeSaver method fillEdgesTable.
/**
* Writes the data from the edge objects to the edges table.
*
* @param connection Connection to a PostgreSQL database.
* @param edges The edges to write.
*
* @throws SQLException Thrown if storing the edges failed.
*/
private static void fillEdgesTable(final CConnection connection, final List<INaviEdge> edges) throws SQLException {
final String query = "INSERT INTO " + CTableNames.EDGES_TABLE + "(source_node_id, target_node_id, x1, y1, x2, y2, type, " + "color, visible, selected, comment_id) VALUES " + "( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ";
final PreparedStatement preparedStatement = connection.getConnection().prepareStatement(query, PreparedStatement.RETURN_GENERATED_KEYS);
try {
for (final INaviEdge edge : edges) {
preparedStatement.setInt(1, edge.getSource().getId());
preparedStatement.setInt(2, edge.getTarget().getId());
preparedStatement.setDouble(3, edge.getX1());
preparedStatement.setDouble(4, edge.getY1());
preparedStatement.setDouble(5, edge.getX2());
preparedStatement.setDouble(6, edge.getY2());
preparedStatement.setObject(7, edge.getType().toString().toLowerCase(), Types.OTHER);
preparedStatement.setInt(8, edge.getColor().getRGB());
preparedStatement.setBoolean(9, edge.isVisible());
preparedStatement.setBoolean(10, edge.isSelected());
if (edge.getLocalComment() == null) {
preparedStatement.setNull(11, Types.INTEGER);
} else {
preparedStatement.setInt(11, Iterables.getLast(edge.getLocalComment()).getId());
}
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
final ResultSet resultSet = preparedStatement.getGeneratedKeys();
for (final INaviEdge edge : edges) {
if (resultSet.next()) {
edge.setId(resultSet.getInt(1));
} else {
throw new IllegalStateException("Error: The number of keys generated does not match the number of edges");
}
}
} catch (final SQLException exception) {
CUtilityFunctions.logException(exception);
CUtilityFunctions.logException(exception.getNextException());
} finally {
preparedStatement.close();
}
}
Aggregations