From 96c9fc129c7ccb79526f377146d6a599c203134c Mon Sep 17 00:00:00 2001 From: Mike Fleetwood Date: Sat, 18 Aug 2012 10:35:21 +0100 Subject: [PATCH] Implement common LVM2_PV_Info cache search and index functions Create common cache search and index functions get_attr_by_name() and get_attr_by_row() as the existing ones, get_pv_attr_by_*() and get_vg_attr_by_*(), only differ from each other by the string vector they use. --- include/LVM2_PV_Info.h | 4 +++ src/LVM2_PV_Info.cc | 79 ++++++++++++++++++------------------------ 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/include/LVM2_PV_Info.h b/include/LVM2_PV_Info.h index 5de6989c..cd531d6f 100644 --- a/include/LVM2_PV_Info.h +++ b/include/LVM2_PV_Info.h @@ -53,6 +53,10 @@ private: Glib::ustring get_pv_attr_by_row( unsigned int row, unsigned int entry ) const ; Glib::ustring get_vg_attr_by_name( const Glib::ustring & vgname, unsigned int entry ) const ; Glib::ustring get_vg_attr_by_row( unsigned int row, unsigned int entry ) const ; + static Glib::ustring get_attr_by_name( const std::vector cache, + const Glib::ustring name, unsigned int entry ) ; + static Glib::ustring get_attr_by_row( const std::vector cache, + unsigned int row, unsigned int entry ) ; static Byte_Value lvm2_pv_attr_to_num( const Glib::ustring str ) ; static bool bit_set( const Glib::ustring & attr, unsigned int bit ) ; static bool lvm2_pv_info_cache_initialized ; diff --git a/src/LVM2_PV_Info.cc b/src/LVM2_PV_Info.cc index 521ef33f..c64fe2ea 100644 --- a/src/LVM2_PV_Info.cc +++ b/src/LVM2_PV_Info.cc @@ -289,54 +289,41 @@ void LVM2_PV_Info::load_lvm2_pv_info_cache() } } -//Return PV's nth attribute. Performs linear search of the cache and -// uses the first matching PV entry. Attributes are numbered 0 upward -// using PV_ATTRIBUTE enumeration. Glib::ustring LVM2_PV_Info::get_pv_attr_by_path( const Glib::ustring & path, unsigned int entry ) const { - for ( unsigned int i = 0 ; i < lvm2_pv_cache .size() ; i ++ ) - { - std::vector pv_attrs ; - Utils::split( lvm2_pv_cache [i], pv_attrs, "," ) ; - if ( PVATTR_PV_NAME < pv_attrs .size() && path == pv_attrs [PVATTR_PV_NAME] ) - { - if ( entry < pv_attrs .size() ) - return pv_attrs [entry] ; - else - break ; - } - } - - return "" ; + return get_attr_by_name( lvm2_pv_cache, path, entry ) ; } -//Return PV's nth attribute from specified cache row. Row is numbered -// 0 upwards and attributes are numbers numbered 0 upwards using -// PV_ATTRIBUTE enumeration. Glib::ustring LVM2_PV_Info::get_pv_attr_by_row( unsigned int row, unsigned int entry ) const { - if ( row >= lvm2_pv_cache .size() ) - return "" ; - std::vector pv_attrs ; - Utils::split( lvm2_pv_cache [row], pv_attrs, "," ) ; - if ( entry < pv_attrs .size() ) - return pv_attrs [entry] ; - return "" ; + return get_attr_by_row( lvm2_pv_cache, row, entry ) ; } -//Return VG's nth attribute. Performs linear search of the cache and -// uses the first matching VG entry. Attributes are numbered 0 upward -// using VG_ATTRIBUTE enumeration. Glib::ustring LVM2_PV_Info::get_vg_attr_by_name( const Glib::ustring & vgname, unsigned int entry ) const { - for ( unsigned int i = 0 ; i < lvm2_vg_cache .size() ; i ++ ) + return get_attr_by_name( lvm2_vg_cache, vgname, entry ) ; +} + +Glib::ustring LVM2_PV_Info::get_vg_attr_by_row( unsigned int row, unsigned int entry ) const +{ + return get_attr_by_row( lvm2_vg_cache, row, entry ) ; +} + +//Performs linear search of the cache and uses the first matching name. +// Return nth attribute. Attributes are numbered 0 upward using +// PV_ATTRIBUTE and VG_ATTRIBUTE enumerations for lvm2_pv_cache and +// lvm2_vg_cache respectively. +Glib::ustring LVM2_PV_Info::get_attr_by_name( const std::vector cache, + const Glib::ustring name, unsigned int entry ) +{ + for ( unsigned int i = 0 ; i < cache .size() ; i ++ ) { - std::vector vg_attrs ; - Utils::split( lvm2_vg_cache [i], vg_attrs, "," ) ; - if ( VGATTR_VG_NAME < vg_attrs .size() && vgname == vg_attrs [VGATTR_VG_NAME] ) + std::vector attrs ; + Utils::split( cache [i], attrs, "," ) ; + if ( attrs .size() >= 1 && attrs [0] == name ) { - if ( entry < vg_attrs .size() ) - return vg_attrs [entry] ; + if ( entry < attrs .size() ) + return attrs [entry] ; else break ; } @@ -345,17 +332,19 @@ Glib::ustring LVM2_PV_Info::get_vg_attr_by_name( const Glib::ustring & vgname, u return "" ; } -//Return VG's nth attribute from specified cache row. Row is numbered -// 0 upwards and attributes are numbers numbered 0 upwards using -// VG_ATTRIBUTE enumeration. -Glib::ustring LVM2_PV_Info::get_vg_attr_by_row( unsigned int row, unsigned int entry ) const +//Lookup row from the cache and return nth attribute. Row is numbered +// 0 upwards and attributes are numbered 0 upwards using PV_ATTRIBUTE +// and VG_ATTRIBUTE enumerations for lvm2_pv_cache and lvm2_vg_cache +// respectively. +Glib::ustring LVM2_PV_Info::get_attr_by_row( const std::vector cache, + unsigned int row, unsigned int entry ) { - if ( row >= lvm2_vg_cache .size() ) + if ( row >= cache .size() ) return "" ; - std::vector vg_attrs ; - Utils::split( lvm2_vg_cache [row], vg_attrs, "," ) ; - if ( entry < vg_attrs .size() ) - return vg_attrs [entry] ; + std::vector attrs ; + Utils::split( cache [row], attrs, "," ) ; + if ( entry < attrs .size() ) + return attrs [entry] ; return "" ; }