cengal.data_containers.dynamic_tag_tree.versions.v_0.tag_db_interface

Module Docstring Docstrings: http://www.python.org/dev/peps/pep-0257/

  1#!/usr/bin/env python
  2# coding=utf-8
  3
  4# Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>
  5# 
  6# Licensed under the Apache License, Version 2.0 (the "License");
  7# you may not use this file except in compliance with the License.
  8# You may obtain a copy of the License at
  9# 
 10#     http://www.apache.org/licenses/LICENSE-2.0
 11# 
 12# Unless required by applicable law or agreed to in writing, software
 13# distributed under the License is distributed on an "AS IS" BASIS,
 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15# See the License for the specific language governing permissions and
 16# limitations under the License.
 17
 18
 19"""
 20Module Docstring
 21Docstrings: http://www.python.org/dev/peps/pep-0257/
 22"""
 23
 24
 25__author__ = "ButenkoMS <gtalk@butenkoms.space>"
 26__copyright__ = "Copyright © 2012-2024 ButenkoMS. All rights reserved. Contacts: <gtalk@butenkoms.space>"
 27__credits__ = ["ButenkoMS <gtalk@butenkoms.space>", ]
 28__license__ = "Apache License, Version 2.0"
 29__version__ = "4.4.1"
 30__maintainer__ = "ButenkoMS <gtalk@butenkoms.space>"
 31__email__ = "gtalk@butenkoms.space"
 32# __status__ = "Prototype"
 33__status__ = "Development"
 34# __status__ = "Production"
 35
 36
 37__author__ = 'Mikhail Butenko <gtalk@mikhail-butenko.in.ua>'
 38
 39
 40# import TagDB
 41# from TagDB import USUAL_TREE_TYPE as USUAL_TREE_TYPE
 42# from TagDB import SMART_TREE_TYPE as SMART_TREE_TYPE
 43# from TagDB import FULL_TREE_TYPE as FULL_TREE_TYPE
 44# from TagDB import PLAIN_PSEUDO_TREE_TYPE as PLAIN_PSEUDO_TREE_TYPE
 45from .TagDB import *
 46import copy
 47from cengal.web_tools.request_cache import RequestCache
 48import time
 49
 50
 51class tag_db_interface:
 52
 53    def __init__(self):
 54        super().__init__()
 55        self.__db = TagDB()
 56        self.cache = RequestCache(300000)
 57
 58    def get_db(self):
 59        return self.__db
 60
 61    def add_item(self, binItem, binTags):
 62        binItem = copy.deepcopy(binItem)
 63        binTags = copy.deepcopy(binTags)
 64
 65        result = self.__db.add_item(binItem, binTags)
 66
 67        result = copy.deepcopy(result)
 68        self.cache.clear()
 69        return result
 70
 71    def remove_item(self, binTags, binItem):
 72        binTags = copy.deepcopy(binTags)
 73        binItem = copy.deepcopy(binItem)
 74
 75        result = self.__db.remove_item(binItem, binTags)
 76
 77        result = copy.deepcopy(result)
 78        self.cache.clear()
 79        return result
 80
 81    def get_items(self, binTags, treeType=USUAL_TREE_TYPE):
 82        binTags = copy.deepcopy(binTags)
 83        treeType = copy.deepcopy(treeType)
 84
 85        # request = ('get_items', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
 86        request = ('get_items', frozenset(binTags), treeType)
 87        result = self.cache.try_to_get_data_for_request(request)
 88        if result is None:
 89            result = self.__db.get_items_from_tags(binTags, treeType=treeType)
 90            self.cache.put_new_request(request, result)
 91
 92        result = copy.deepcopy(result)
 93        return result
 94
 95    def get_tags(self, binTags, treeType=USUAL_TREE_TYPE):
 96        binTags = copy.deepcopy(binTags)
 97        treeType = copy.deepcopy(treeType)
 98
 99        # request = ('get_tags', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
100        request = ('get_tags', frozenset(binTags), treeType)
101        result = self.cache.try_to_get_data_for_request(request)
102        if result is None:
103            result = self.__db.get_tags_from_tags(binTags, treeType=treeType)
104            self.cache.put_new_request(request, result)
105
106        result = copy.deepcopy(result)
107        return result
108
109    def get_all(self, binTags, treeType=USUAL_TREE_TYPE):
110        binTags = copy.deepcopy(binTags)
111        treeType = copy.deepcopy(treeType)
112
113        # request = ('get_all', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
114        request = ('get_all', frozenset(binTags), treeType)
115        result = self.cache.try_to_get_data_for_request(request)
116        if result is None:
117            result = self.__db.get_all_from_tags(binTags, treeType=treeType)
118            self.cache.put_new_request(request, result)
119
120        result = copy.deepcopy(result)
121        return result
122
123    def get_tags_for_a_smart_redirection(self, binTags):
124        binTags = copy.deepcopy(binTags)
125
126        # request = ('is_smart_redirection_for_a_tag_path_reduction_needed'
127        #            , tuple(self.__db.sort_tag_list_by_hash(binTags)))
128        request = ('is_smart_redirection_for_a_tag_path_reduction_needed', frozenset(binTags))
129        result = self.cache.try_to_get_data_for_request(request)
130        if result is None:
131            result = self.__db.get_tags_for_a_smart_redirection(binTags)
132            self.cache.put_new_request(request, result)
133
134        result = copy.deepcopy(result)
135        return result
136
137    def fill_cache(self, max_answer_time_in_miliseconds=500, path=list()):
138        current_path = list(path)
139        startTime = time.time()
140
141        tags_and_items_inside_curent_path = self.get_all(current_path, SMART_TREE_TYPE)
142        internal_tags = tags_and_items_inside_curent_path[0]
143        self.get_items(current_path, PLAIN_PSEUDO_TREE_TYPE)
144
145        endTime = time.time()
146        resultTime = endTime - startTime
147        if (resultTime*1000) >= max_answer_time_in_miliseconds:
148            for additional_tag in internal_tags:
149                new_path = current_path + [additional_tag]
150                self.fill_cache(max_answer_time_in_miliseconds, new_path)
class tag_db_interface:
 52class tag_db_interface:
 53
 54    def __init__(self):
 55        super().__init__()
 56        self.__db = TagDB()
 57        self.cache = RequestCache(300000)
 58
 59    def get_db(self):
 60        return self.__db
 61
 62    def add_item(self, binItem, binTags):
 63        binItem = copy.deepcopy(binItem)
 64        binTags = copy.deepcopy(binTags)
 65
 66        result = self.__db.add_item(binItem, binTags)
 67
 68        result = copy.deepcopy(result)
 69        self.cache.clear()
 70        return result
 71
 72    def remove_item(self, binTags, binItem):
 73        binTags = copy.deepcopy(binTags)
 74        binItem = copy.deepcopy(binItem)
 75
 76        result = self.__db.remove_item(binItem, binTags)
 77
 78        result = copy.deepcopy(result)
 79        self.cache.clear()
 80        return result
 81
 82    def get_items(self, binTags, treeType=USUAL_TREE_TYPE):
 83        binTags = copy.deepcopy(binTags)
 84        treeType = copy.deepcopy(treeType)
 85
 86        # request = ('get_items', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
 87        request = ('get_items', frozenset(binTags), treeType)
 88        result = self.cache.try_to_get_data_for_request(request)
 89        if result is None:
 90            result = self.__db.get_items_from_tags(binTags, treeType=treeType)
 91            self.cache.put_new_request(request, result)
 92
 93        result = copy.deepcopy(result)
 94        return result
 95
 96    def get_tags(self, binTags, treeType=USUAL_TREE_TYPE):
 97        binTags = copy.deepcopy(binTags)
 98        treeType = copy.deepcopy(treeType)
 99
100        # request = ('get_tags', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
101        request = ('get_tags', frozenset(binTags), treeType)
102        result = self.cache.try_to_get_data_for_request(request)
103        if result is None:
104            result = self.__db.get_tags_from_tags(binTags, treeType=treeType)
105            self.cache.put_new_request(request, result)
106
107        result = copy.deepcopy(result)
108        return result
109
110    def get_all(self, binTags, treeType=USUAL_TREE_TYPE):
111        binTags = copy.deepcopy(binTags)
112        treeType = copy.deepcopy(treeType)
113
114        # request = ('get_all', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
115        request = ('get_all', frozenset(binTags), treeType)
116        result = self.cache.try_to_get_data_for_request(request)
117        if result is None:
118            result = self.__db.get_all_from_tags(binTags, treeType=treeType)
119            self.cache.put_new_request(request, result)
120
121        result = copy.deepcopy(result)
122        return result
123
124    def get_tags_for_a_smart_redirection(self, binTags):
125        binTags = copy.deepcopy(binTags)
126
127        # request = ('is_smart_redirection_for_a_tag_path_reduction_needed'
128        #            , tuple(self.__db.sort_tag_list_by_hash(binTags)))
129        request = ('is_smart_redirection_for_a_tag_path_reduction_needed', frozenset(binTags))
130        result = self.cache.try_to_get_data_for_request(request)
131        if result is None:
132            result = self.__db.get_tags_for_a_smart_redirection(binTags)
133            self.cache.put_new_request(request, result)
134
135        result = copy.deepcopy(result)
136        return result
137
138    def fill_cache(self, max_answer_time_in_miliseconds=500, path=list()):
139        current_path = list(path)
140        startTime = time.time()
141
142        tags_and_items_inside_curent_path = self.get_all(current_path, SMART_TREE_TYPE)
143        internal_tags = tags_and_items_inside_curent_path[0]
144        self.get_items(current_path, PLAIN_PSEUDO_TREE_TYPE)
145
146        endTime = time.time()
147        resultTime = endTime - startTime
148        if (resultTime*1000) >= max_answer_time_in_miliseconds:
149            for additional_tag in internal_tags:
150                new_path = current_path + [additional_tag]
151                self.fill_cache(max_answer_time_in_miliseconds, new_path)
cache
def get_db(self):
59    def get_db(self):
60        return self.__db
def add_item(self, binItem, binTags):
62    def add_item(self, binItem, binTags):
63        binItem = copy.deepcopy(binItem)
64        binTags = copy.deepcopy(binTags)
65
66        result = self.__db.add_item(binItem, binTags)
67
68        result = copy.deepcopy(result)
69        self.cache.clear()
70        return result
def remove_item(self, binTags, binItem):
72    def remove_item(self, binTags, binItem):
73        binTags = copy.deepcopy(binTags)
74        binItem = copy.deepcopy(binItem)
75
76        result = self.__db.remove_item(binItem, binTags)
77
78        result = copy.deepcopy(result)
79        self.cache.clear()
80        return result
def get_items(self, binTags, treeType=3):
82    def get_items(self, binTags, treeType=USUAL_TREE_TYPE):
83        binTags = copy.deepcopy(binTags)
84        treeType = copy.deepcopy(treeType)
85
86        # request = ('get_items', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
87        request = ('get_items', frozenset(binTags), treeType)
88        result = self.cache.try_to_get_data_for_request(request)
89        if result is None:
90            result = self.__db.get_items_from_tags(binTags, treeType=treeType)
91            self.cache.put_new_request(request, result)
92
93        result = copy.deepcopy(result)
94        return result
def get_tags(self, binTags, treeType=3):
 96    def get_tags(self, binTags, treeType=USUAL_TREE_TYPE):
 97        binTags = copy.deepcopy(binTags)
 98        treeType = copy.deepcopy(treeType)
 99
100        # request = ('get_tags', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
101        request = ('get_tags', frozenset(binTags), treeType)
102        result = self.cache.try_to_get_data_for_request(request)
103        if result is None:
104            result = self.__db.get_tags_from_tags(binTags, treeType=treeType)
105            self.cache.put_new_request(request, result)
106
107        result = copy.deepcopy(result)
108        return result
def get_all(self, binTags, treeType=3):
110    def get_all(self, binTags, treeType=USUAL_TREE_TYPE):
111        binTags = copy.deepcopy(binTags)
112        treeType = copy.deepcopy(treeType)
113
114        # request = ('get_all', tuple(self.__db.sort_tag_list_by_hash(binTags)), treeType)
115        request = ('get_all', frozenset(binTags), treeType)
116        result = self.cache.try_to_get_data_for_request(request)
117        if result is None:
118            result = self.__db.get_all_from_tags(binTags, treeType=treeType)
119            self.cache.put_new_request(request, result)
120
121        result = copy.deepcopy(result)
122        return result
def get_tags_for_a_smart_redirection(self, binTags):
124    def get_tags_for_a_smart_redirection(self, binTags):
125        binTags = copy.deepcopy(binTags)
126
127        # request = ('is_smart_redirection_for_a_tag_path_reduction_needed'
128        #            , tuple(self.__db.sort_tag_list_by_hash(binTags)))
129        request = ('is_smart_redirection_for_a_tag_path_reduction_needed', frozenset(binTags))
130        result = self.cache.try_to_get_data_for_request(request)
131        if result is None:
132            result = self.__db.get_tags_for_a_smart_redirection(binTags)
133            self.cache.put_new_request(request, result)
134
135        result = copy.deepcopy(result)
136        return result
def fill_cache(self, max_answer_time_in_miliseconds=500, path=[]):
138    def fill_cache(self, max_answer_time_in_miliseconds=500, path=list()):
139        current_path = list(path)
140        startTime = time.time()
141
142        tags_and_items_inside_curent_path = self.get_all(current_path, SMART_TREE_TYPE)
143        internal_tags = tags_and_items_inside_curent_path[0]
144        self.get_items(current_path, PLAIN_PSEUDO_TREE_TYPE)
145
146        endTime = time.time()
147        resultTime = endTime - startTime
148        if (resultTime*1000) >= max_answer_time_in_miliseconds:
149            for additional_tag in internal_tags:
150                new_path = current_path + [additional_tag]
151                self.fill_cache(max_answer_time_in_miliseconds, new_path)