-
Notifications
You must be signed in to change notification settings - Fork 98
Connection models
NetflixGraph includes the optional concept of “connection models”. You can use NetflixGraph without connection models, but they may be useful for some use cases.
NetflixGraph is extremely efficient at encoding connections between nodes. There is a very small, but nonzero overhead cost for each node in the graph which has outgoing connections. Sometimes, you may be interested in creating multiple graphs over the same set of nodes. You could solve this by creating multiple NFGraphs. However, this will multiply the overhead cost for the nodes in your graph. Even worse, it may add complexity to your code. You can mitigate both of these undesirable effects by using connection models. A “connection model” is a set of connections over the nodes in a graph.
We can define a property as being segregated into connection models with the NFPropertySpec.MODEL_SPECIFIC flag. For such properties, connections should be added to an NFBuildGraph with the API methods which specify a “connectionModel” parameter. Connections added for a connection model will be visible only for that model.
Expanding on the example from the Usage page, the movie “The Matrix” is rated “R” in the United States. However, in Australia, it is rated “M”. In this case, you can think of each country as a “connection model”. You can define the NFGraphSpec:
import static com.netflix.nfgraph.NFPropertySpec.SINGLE import static com.netflix.nfgraph.NFPropertySpec.MODEL_SPECIFICNFGraphSpec spec = new NFGraphSpec( new NFNodeSpec( "Movie", new NFPropertySpec("rated", "Rating", MODEL_SPECIFIC | SINGLE) ), new NFNodeSpec("Rating") );
When you add connections to the buildGraph, you add them to their respective connection models:
buildGraph.addConnection("United States", "Movie", matrixOrdinal, "rated", ratedROrdinal);
buildGraph.addConnection("Australia", "Movie", matrixOrdinal, "rated", ratedMOrdinal);And then when you retrieve the connections, you must specify the model as well:
int ratedROrdinal = buildGraph.getConnection("United States", "Movie", matrixOrdinal, "rated");
int ratedMOrdinal = buildGraph.getConnection("Australia", "Movie", matrixOrdinal, "rated");Use of multiple connection models will add a minimum of one byte per model-specific connection set per node. As a result, this feature should be used only when the number of connection models is and will remain low, or when the populated connection sets for all models among all MODEL_SPECIFIC connections is not sparse. For the same reason, if multiple properties in your NFGraphSpec are specified as MODEL_SPECIFIC, then each property should ideally be segregated into the same set of models.