Re: [PATCHv2 1/2] nwfilter: utility function virNWFilterVarValueEqual

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 29.11.2012 19:30, Laine Stump wrote:
> From: Stefan Berger <stefanb@xxxxxxxxxx>
> 
> To detect if an interface's nwfilter has changed, we need to also
> compare the filterparams, which is a hashtable of virNWFilterVarValue.
> virHashEqual can do this nicely, but requires a pointer to a function
> that will compare two of the items being stored in the hashes.
> ---
>  src/conf/nwfilter_params.c | 31 +++++++++++++++++++++++++++++++
>  src/conf/nwfilter_params.h |  2 ++
>  src/libvirt_private.syms   |  1 +
>  3 files changed, 34 insertions(+)
> 
> diff --git a/src/conf/nwfilter_params.c b/src/conf/nwfilter_params.c
> index 6dc4baa..3ac1303 100644
> --- a/src/conf/nwfilter_params.c
> +++ b/src/conf/nwfilter_params.c
> @@ -189,6 +189,37 @@ virNWFilterVarValueGetCardinality(const virNWFilterVarValuePtr val)
>      return 0;
>  }
>  
> +bool
> +virNWFilterVarValueEqual(const virNWFilterVarValuePtr a,
> +                         const virNWFilterVarValuePtr b)
> +{
> +    unsigned int card, i, j;
> +    const char *s;
> +
> +    if (!a || !b)
> +        return false;
> +
> +    card = virNWFilterVarValueGetCardinality(a);
> +    if (card != virNWFilterVarValueGetCardinality(b))
> +        return false;
> +
> +    /* brute force O(n^2) comparison */
> +    for (i = 0; i < card; i++) {
> +        bool eq = false;
> +
> +        s = virNWFilterVarValueGetNthValue(a, i);
> +        for (j = 0; j < card; j++) {
> +            if (STREQ_NULLABLE(s, virNWFilterVarValueGetNthValue(b, j))) {
> +                 eq = true;
> +                 break;
> +            }
> +        }
> +        if (!eq)
> +            return false;
> +    }
> +    return true;
> +}
> +

Seems reasonable. The quadratic time complexity could be avoided if @a
and @b items are sorted. And since this is just a callback to
virHashEqual() we shouldn't be doing anything here but comparing.

>  int
>  virNWFilterVarValueAddValue(virNWFilterVarValuePtr val, char *value)
>  {
> diff --git a/src/conf/nwfilter_params.h b/src/conf/nwfilter_params.h
> index cedf9cd..96d3033 100644
> --- a/src/conf/nwfilter_params.h
> +++ b/src/conf/nwfilter_params.h
> @@ -57,6 +57,8 @@ const char *virNWFilterVarValueGetSimple(const virNWFilterVarValuePtr val);
>  const char *virNWFilterVarValueGetNthValue(virNWFilterVarValuePtr val,
>                                             unsigned int idx);
>  unsigned int virNWFilterVarValueGetCardinality(const virNWFilterVarValuePtr);
> +bool virNWFilterVarValueEqual(const virNWFilterVarValuePtr a,
> +                              const virNWFilterVarValuePtr b);
>  int virNWFilterVarValueAddValue(virNWFilterVarValuePtr val, char *value);
>  int virNWFilterVarValueDelValue(virNWFilterVarValuePtr val, const char *value);
>  
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 2573b8a..ada73fb 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -968,6 +968,7 @@ virNWFilterVarValueCopy;
>  virNWFilterVarValueCreateSimple;
>  virNWFilterVarValueCreateSimpleCopyValue;
>  virNWFilterVarValueDelValue;
> +virNWFilterVarValueEqual;
>  virNWFilterVarValueFree;
>  virNWFilterVarValueGetCardinality;
>  virNWFilterVarValueGetNthValue;
> 

ACK

Michal

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]