11import dataclasses
22import logging
3- import threading
43from pathlib import Path
54from typing import BinaryIO , Optional , List
65
@@ -56,7 +55,6 @@ def __init__(self, session: DbSession):
5655 self .session = session
5756 self .logger = logger .get ()
5857
59- self .__lock = threading .Lock ()
6058 self .__active : Optional [_ActivePack ] = None
6159 self .__new_pack_paths : List [Path ] = []
6260 self .__created_pack_size = 0
@@ -84,62 +82,57 @@ def __should_write_dedicated(size: int) -> bool:
8482 return size >= pack_constants .PACK_DEDICATED_ENTRY_MIN_SIZE
8583
8684 def __write_active (self , data : bytes ) -> PackEntryLocation :
87- with self .__lock :
88- active = self .__get_active_for_write_no_lock ()
89- result = active .append_bytes (data )
90- self .__created_pack_size += len (data )
91- return result
85+ active = self .__get_active_for_write ()
86+ result = active .append_bytes (data )
87+ self .__created_pack_size += len (data )
88+ return result
9289
9390 def __write_active_reader (self , reader : SupportsReadBytes , size : int ) -> PackEntryLocation :
94- with self .__lock :
95- active = self .__get_active_for_write_no_lock ()
96- result = active .append_reader (reader , size )
97- self .__created_pack_size += size
98- return result
91+ active = self .__get_active_for_write ()
92+ result = active .append_reader (reader , size )
93+ self .__created_pack_size += size
94+ return result
9995
10096 def __write_dedicated (self , data : bytes ) -> PackEntryLocation :
101- with self .__lock :
102- dedicated_pack = self .__create_new_pack_no_lock ()
103- result = dedicated_pack .append_bytes (data )
104- self .__created_pack_size += len (data )
105- dedicated_pack .close ()
97+ dedicated_pack = self .__create_new_pack ()
98+ result = dedicated_pack .append_bytes (data )
99+ self .__created_pack_size += len (data )
100+ dedicated_pack .close ()
106101 if self .logger .isEnabledFor (logging .DEBUG ):
107102 self .logger .debug (f'Wrote dedicated pack id={ dedicated_pack .pack .id } file_name={ pack_utils .get_pack_file_name (dedicated_pack .pack .id )} size={ len (data )} ' )
108103 return result
109104
110105 def __write_dedicated_reader (self , reader : SupportsReadBytes , size : int ) -> PackEntryLocation :
111- with self .__lock :
112- dedicated_pack = self .__create_new_pack_no_lock ()
113- result = dedicated_pack .append_reader (reader , size )
114- self .__created_pack_size += size
115- dedicated_pack .close ()
106+ dedicated_pack = self .__create_new_pack ()
107+ result = dedicated_pack .append_reader (reader , size )
108+ self .__created_pack_size += size
109+ dedicated_pack .close ()
116110 if self .logger .isEnabledFor (logging .DEBUG ):
117111 self .logger .debug (f'Wrote dedicated pack id={ dedicated_pack .pack .id } file_name={ pack_utils .get_pack_file_name (dedicated_pack .pack .id )} size={ size } ' )
118112 return result
119113
120- def __get_active_for_write_no_lock (self ) -> _ActivePack :
121- if self .__active is None or self .__should_rotate_active_no_lock ():
122- self .__open_new_active_no_lock ()
114+ def __get_active_for_write (self ) -> _ActivePack :
115+ if self .__active is None or self .__should_rotate_active ():
116+ self .__open_new_active ()
123117 assert self .__active is not None
124118 return self .__active
125119
126- def __should_rotate_active_no_lock (self ) -> bool :
120+ def __should_rotate_active (self ) -> bool :
127121 assert self .__active is not None
128122 return (
129123 self .__active .pack .size >= pack_constants .PACK_MAX_SIZE or
130124 self .__active .pack .entry_count >= pack_constants .PACK_MAX_COUNT
131125 )
132126
133127 def close (self ):
134- with self .__lock :
135- self .__close_no_lock ()
128+ self .__close ()
136129
137- def __close_no_lock (self ):
130+ def __close (self ):
138131 if self .__active is not None :
139132 self .__active .close ()
140133 self .__active = None
141134
142- def __create_new_pack_no_lock (self ) -> _ActivePack :
135+ def __create_new_pack (self ) -> _ActivePack :
143136 pack_utils .prepare_pack_store ()
144137 new_pack = self .session .create_and_add_pack (
145138 size = 0 ,
@@ -155,10 +148,10 @@ def __create_new_pack_no_lock(self) -> _ActivePack:
155148 self .__new_pack_paths .append (pack_path )
156149 return _ActivePack (new_pack , fh )
157150
158- def __open_new_active_no_lock (self ):
159- self .__close_no_lock ()
151+ def __open_new_active (self ):
152+ self .__close ()
160153
161- new_pack = self .__create_new_pack_no_lock ()
154+ new_pack = self .__create_new_pack ()
162155 self .__active = new_pack
163156 if self .logger .isEnabledFor (logging .DEBUG ):
164157 self .logger .debug (f'Opened new active pack id={ new_pack .pack .id } file_name={ pack_utils .get_pack_file_name (new_pack .pack .id )} ' )
0 commit comments