Coverage for rdflib_ocdm/counter_handler/in_memory_counter_handler.py: 94%
25 statements
« prev ^ index » next coverage.py v7.6.12, created at 2025-11-01 22:02 +0000
« prev ^ index » next coverage.py v7.6.12, created at 2025-11-01 22:02 +0000
1#!/usr/bin/python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2016, Silvio Peroni <essepuntato@gmail.com>
4#
5# Permission to use, copy, modify, and/or distribute this software for any purpose
6# with or without fee is hereby granted, provided that the above copyright notice
7# and this permission notice appear in all copies.
8#
9# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
10# REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
11# FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
12# OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
13# DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14# ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15# SOFTWARE.
17from __future__ import annotations
19from typing import TYPE_CHECKING
21if TYPE_CHECKING: 21 ↛ 22line 21 didn't jump to line 22 because the condition on line 21 was never true
22 from typing import List, Dict
24from rdflib_ocdm.counter_handler.counter_handler import CounterHandler
27class InMemoryCounterHandler(CounterHandler):
28 """A concrete implementation of the ``CounterHandler`` interface that temporarily stores
29 the counter values in the volatile system memory."""
31 def __init__(self) -> None:
32 """
33 Constructor of the ``InMemoryCounterHandler`` class.
34 """
35 self.prov_counters = dict()
37 def set_counter(self, new_value: int, entity_name: str) -> None:
38 """
39 It allows to set the counter value of graph and provenance entities.
41 :param new_value: The new counter value to be set
42 :type new_value: int
43 :param entity_name: The entity name
44 :type entity_name: str
45 :raises ValueError: if ``new_value`` is a negative integer.
46 :return: None
47 """
48 entity_name = str(entity_name)
49 if new_value < 0:
50 raise ValueError("new_value must be a non negative integer!")
51 self.prov_counters[entity_name] = new_value
53 def read_counter(self, entity_name: str) -> int:
54 """
55 It allows to read the counter value of provenance entities.
57 :param entity_name: The entity name
58 :type entity_name: str
59 :return: The requested counter value.
60 """
61 entity_name = str(entity_name)
62 if entity_name in self.prov_counters:
63 return self.prov_counters[entity_name]
64 else:
65 self.prov_counters[entity_name] = 0
66 return 0
68 def increment_counter(self, entity_name: str) -> int:
69 """
70 It allows to increment the counter value of graph and provenance entities by one unit.
72 :param entity_name: The entity name
73 :type entity_name: str
74 :return: The newly-updated (already incremented) counter value.
75 """
76 entity_name = str(entity_name)
77 if entity_name in self.prov_counters:
78 self.prov_counters[entity_name] += 1
79 else:
80 self.prov_counters[entity_name] = 1
81 return self.prov_counters[entity_name]