| 1 |
# Copyright (C) 2015 Ericsson AB |
| 2 |
# Copyright (c) 2015 Gigamon |
| 3 |
# |
| 4 |
# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 |
# not use this file except in compliance with the License. You may obtain |
| 6 |
# a copy of the License at |
| 7 |
# |
| 8 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
# |
| 10 |
# Unless required by applicable law or agreed to in writing, software |
| 11 |
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 |
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 |
# License for the specific language governing permissions and limitations |
| 14 |
# under the License. |
| 15 |
|
| 16 |
|
| 17 |
# |
| 18 |
# This class implements a simple key-value manager that can support |
| 19 |
# the following relationships. |
| 20 |
# |
| 21 |
# - Multiple values may be affiliated with a key. |
| 22 |
# - A value may be affiliated with multiple keys. |
| 23 |
# - A value may be affiliated with a key multiple times. |
| 24 |
# |
| 25 |
class key_value_mgr: |
| 26 |
# |
| 27 |
# Initializes internal state for specified # keys |
| 28 |
# |
| 29 |
def __init__(self, nr_keys): |
| 30 |
self.key_list = [] |
| 31 |
for i in range(nr_keys): |
| 32 |
self.key_list.append([]) |
| 33 |
|
| 34 |
# |
| 35 |
# Returns specified key-value affilation, if it exists. |
| 36 |
# |
| 37 |
def _find_affiliation(self, key, value): |
| 38 |
aff_list = self.key_list[key] |
| 39 |
|
| 40 |
for aff in aff_list: |
| 41 |
if aff['value'] == value: |
| 42 |
return aff |
| 43 |
|
| 44 |
return None |
| 45 |
|
| 46 |
# |
| 47 |
# Adds an affiliation of 'value' with 'key' |
| 48 |
# |
| 49 |
def affiliate(self, key, value): |
| 50 |
# Locate key-value affiliation |
| 51 |
aff = self._find_affiliation(key, value) |
| 52 |
if aff is None: |
| 53 |
# Create a (new) key-value affiliation |
| 54 |
aff = { |
| 55 |
'value': value, |
| 56 |
'refcnt': 0, |
| 57 |
} |
| 58 |
aff_list = self.key_list[key] |
| 59 |
aff_list.append(aff) |
| 60 |
|
| 61 |
# Increment affiliation reference count |
| 62 |
aff['refcnt'] += 1 |
| 63 |
|
| 64 |
# |
| 65 |
# Removes an affiliation of 'value' with 'key' |
| 66 |
# |
| 67 |
def unaffiliate(self, key, value): |
| 68 |
# Locate key-value affiliation |
| 69 |
aff = self._find_affiliation(key, value) |
| 70 |
if aff is None: |
| 71 |
return |
| 72 |
|
| 73 |
# Decrement affiliation reference count |
| 74 |
aff['refcnt'] -= 1 |
| 75 |
|
| 76 |
# Destroy affiliation iff no outstanding references |
| 77 |
if aff['refcnt'] <= 0: |
| 78 |
aff_list = self.key_list[key] |
| 79 |
aff_list.remove(aff) |
| 80 |
|
| 81 |
return |
| 82 |
|
| 83 |
# |
| 84 |
# Lists all values affiliated with 'key' |
| 85 |
# |
| 86 |
# Note: The returned list is a set (contains no duplicates) |
| 87 |
# |
| 88 |
def list_affiliations(self, key): |
| 89 |
aff_list = self.key_list[key] |
| 90 |
|
| 91 |
value_list = [] |
| 92 |
for aff in aff_list: |
| 93 |
value_list.append(aff['value']) |
| 94 |
|
| 95 |
return value_list |