This repository has been archived by the owner on Jan 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #321 from twitter/dependant-gen
Move Dependants common code to graph
- Loading branch information
Showing
3 changed files
with
54 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
summingbird-core/src/main/scala/com/twitter/summingbird/graph/DependantGraph.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
Copyright 2013 Twitter, Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package com.twitter.summingbird.graph | ||
|
||
/** Given Dag and a List of immutable nodes, and a function to get | ||
* dependencies, compute the dependants (reverse the graph) | ||
*/ | ||
abstract class DependantGraph[T] { | ||
def nodes: List[T] | ||
def dependenciesOf(t: T): Iterable[T] | ||
|
||
lazy val allTails: List[T] = nodes.filter { fanOut(_).get == 0 } | ||
private lazy val nodeSet: Set[T] = nodes.toSet | ||
|
||
/** This is the dependants graph. Each node knows who it depends on | ||
* but not who depends on it without doing this computation | ||
*/ | ||
private lazy val graph: NeighborFn[T] = reversed(nodes)(dependenciesOf(_)) | ||
|
||
private lazy val depths: Map[T, Int] = dagDepth(nodes)(dependenciesOf(_)) | ||
|
||
/** The max of zero and 1 + depth of all parents if the node is the graph | ||
*/ | ||
def isNode(p: T): Boolean = nodeSet.contains(p) | ||
def depth(p: T): Option[Int] = depths.get(p) | ||
|
||
def dependantsOf(p: T): Option[List[T]] = | ||
if(isNode(p)) Some(graph(p).toList) else None | ||
|
||
def fanOut(p: T): Option[Int] = dependantsOf(p).map { _.size } | ||
/** | ||
* Return all dependendants of a given node. | ||
* Does not include itself | ||
*/ | ||
def transitiveDependantsOf(p: T): List[T] = depthFirstOf(p)(graph).toList | ||
} |