[Rear-users] SF.net SVN: rear:[739] trunk/usr/share/rear/lib/layout-functions.sh

jhoekx at users.sourceforge.net jhoekx at users.sourceforge.net
Wed Nov 23 14:19:47 CET 2011


Revision: 739
          http://rear.svn.sourceforge.net/rear/?rev=739&view=rev
Author:   jhoekx
Date:     2011-11-23 13:19:46 +0000 (Wed, 23 Nov 2011)
Log Message:
-----------
Improve performance of layout tree operations.

This patch rewrites the functions to use bash builtins as much as possible and reduces the number of file reads in the normal case. The logic of the find_parent_components code has also been inversed to bring it more in line with the get_child_components function.

Modified Paths:
--------------
    trunk/usr/share/rear/lib/layout-functions.sh

Modified: trunk/usr/share/rear/lib/layout-functions.sh
===================================================================
--- trunk/usr/share/rear/lib/layout-functions.sh	2011-11-22 09:50:30 UTC (rev 738)
+++ trunk/usr/share/rear/lib/layout-functions.sh	2011-11-23 13:19:46 UTC (rev 739)
@@ -192,68 +192,107 @@
 
 # Mark device $1 as done.
 mark_as_done() {
-    Log "Marking $1 as done."
+    Debug "Marking $1 as done."
     sed -i "s;todo\ $1\ ;done\ $1\ ;" $LAYOUT_TODO
 }
 
 # Mark all devices that depend on $1 as done.
 mark_tree_as_done() {
     for component in $(get_child_components "$1") ; do
-        Log "Marking $component as done (dependent on $1)"
+        Debug "Marking $component as done (dependent on $1)"
         mark_as_done "$component"
     done
 }
 
-# Return all the child components of $1 [filtered by type $2]
+# Return all the (grand-)child components of $1 [filtered by type $2]
 get_child_components() {
-    local devlist testdev dev on type
-    devlist="$1 "
-    while [ -n "$devlist" ] ; do
-        testdev=$(echo "$devlist" | cut -d " " -f "1")
-        while read dev on junk ; do
-            if [ "$on" = "$testdev" ] ; then
-                devlist="$devlist$dev "
-                type=$(get_component_type "$dev")
-                if [ "$type" = "$2" ] || [ -z "$2" ] ; then
-                    echo "$dev"
+    declare -a devlist children
+    declare current child parent
+
+    devlist=( "$1" )
+    while (( ${#devlist[@]} )) ; do
+        current=${devlist[0]}
+
+        ### Find all direct child elements of the current component...
+        while read child parent junk ; do
+            if [[ "$parent" = "$current" ]] ; then
+                ### ...and add them to the list
+                if IsInArray "$child" "${children[@]}" ; then
+                    continue
                 fi
+                devlist+=( "$child" )
+                children+=( "$child" )
             fi
-        done < <(cat $LAYOUT_DEPS)
-        devlist=$(echo "$devlist" | sed -r "s;^$testdev ;;")
+        done < $LAYOUT_DEPS
+
+        ### remove the current element from the array and re-index it
+        unset devlist[0]
+        devlist=( "${devlist[@]}" )
     done
-}
 
-# find_devices <other>
-# Find the disk device(s) component $1 resides on.
-find_disk() {
-    echo "$(get_parent_components $1 "disk")" | sort | uniq
+    ### Filter for the wanted type
+    declare component type
+    for component in "${children[@]}" ; do
+        if [[ "$2" ]] ; then
+            type=$(get_component_type "$component")
+            if [[ "$type" != "$2" ]] ; then
+                continue
+            fi
+        fi
+        echo "$component"
+    done
 }
 
-find_partition() {
-    echo "$(get_parent_components $1 "part")" | sort | uniq
-}
-
-# Find all parent components [of type $2] component $1 resides on.
-# Can/will contain multiples.
+# Return all ancestors of component $1 [ of type $2 ]
 get_parent_components() {
-    local component type
-    local components=$(get_parent_component $1)
+    declare -a ancestors devlist
+    declare current child parent
 
-    for component in $components ; do
-        type=$(get_component_type $component)
-        if [ "$type" = "$2" ] || [ -z "$2" ]; then
-            echo "$component"
-        fi
+    devlist=( "$1" )
+    while (( ${#devlist[@]} )) ; do
+        current=${devlist[0]}
 
-        get_parent_components $component $2
+        ### Find all direct parent elements of the current component...
+        while read child parent junk ; do
+            if [[ "$child" = "$current" ]] ; then
+                ### ...test if we visited them already...
+                if IsInArray "$parent" "${ancestors[@]}" ; then
+                    continue
+                fi
+                ### ...and add them to the list
+                devlist+=( "$parent" )
+                ancestors+=( "$parent" )
+            fi
+        done < $LAYOUT_DEPS
+
+        ### remove the current element from the array and re-index it
+        unset devlist[0]
+        devlist=( "${devlist[@]}" )
     done
+
+    ### Filter the ancestors for the correct type.
+    declare component type
+    for component in "${ancestors[@]}" ; do
+        if [[ "$2" ]] ; then
+            type=$(get_component_type "$component")
+            if [[ "$type" != "$2" ]] ; then
+                continue
+            fi
+        fi
+        echo "$component"
+    done
 }
 
-# Read the parent component(s) from the layout deps list
-get_parent_component() {
-    grep "^$1 " $LAYOUT_DEPS | cut -d " " -f 2 | sort | uniq
+# find_devices <other>
+# Find the disk device(s) component $1 resides on.
+find_disk() {
+    get_parent_components $1 "disk"
 }
 
+find_partition() {
+    get_parent_components $1 "part"
+}
+
 # Get the type of a layout component
 get_component_type() {
     grep -E "^[^ ]+ $1 " $LAYOUT_TODO | cut -d " " -f 3

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.





More information about the rear-users mailing list