2
2
"""SQLite-based attribute container store."""
3
3
4
4
import ast
5
- import collections
6
5
import itertools
6
+ import json
7
7
import os
8
8
import pathlib
9
9
import sqlite3
@@ -122,7 +122,8 @@ def DeserializeValue(self, data_type, value):
122
122
elif data_type not in self ._MAPPINGS :
123
123
serializer = schema_helper .SchemaHelper .GetAttributeSerializer (
124
124
data_type , 'json' )
125
- value = serializer .DeserializeValue (value )
125
+ json_dict = json .loads (value )
126
+ value = serializer .DeserializeValue (json_dict )
126
127
127
128
return value
128
129
@@ -160,12 +161,14 @@ def SerializeValue(self, data_type, value):
160
161
if isinstance (value , set ):
161
162
value = list (value )
162
163
163
- return serializer .SerializeValue (value )
164
+ json_dict = serializer .SerializeValue (value )
165
+ return json .dumps (json_dict )
164
166
165
167
return value
166
168
167
169
168
- class SQLiteAttributeContainerStore (interface .AttributeContainerStore ):
170
+ class SQLiteAttributeContainerStore (
171
+ interface .AttributeContainerStoreWithReadCache ):
169
172
"""SQLite-based attribute container store.
170
173
171
174
Attributes:
@@ -205,15 +208,11 @@ class SQLiteAttributeContainerStore(interface.AttributeContainerStore):
205
208
_INSERT_METADATA_VALUE_QUERY = (
206
209
'INSERT INTO metadata (key, value) VALUES (?, ?)' )
207
210
208
- # The maximum number of cached attribute containers
209
- _MAXIMUM_CACHED_CONTAINERS = 32 * 1024
210
-
211
211
_MAXIMUM_WRITE_CACHE_SIZE = 50
212
212
213
213
def __init__ (self ):
214
214
"""Initializes a SQLite attribute container store."""
215
215
super (SQLiteAttributeContainerStore , self ).__init__ ()
216
- self ._attribute_container_cache = collections .OrderedDict ()
217
216
self ._connection = None
218
217
self ._cursor = None
219
218
self ._is_open = False
@@ -224,20 +223,6 @@ def __init__(self):
224
223
self .format_version = self ._FORMAT_VERSION
225
224
self .serialization_format = 'json'
226
225
227
- def _CacheAttributeContainerByIndex (self , attribute_container , index ):
228
- """Caches a specific attribute container.
229
-
230
- Args:
231
- attribute_container (AttributeContainer): attribute container.
232
- index (int): attribute container index.
233
- """
234
- if len (self ._attribute_container_cache ) >= self ._MAXIMUM_CACHED_CONTAINERS :
235
- self ._attribute_container_cache .popitem (last = True )
236
-
237
- lookup_key = f'{ attribute_container .CONTAINER_TYPE :s} .{ index :d} '
238
- self ._attribute_container_cache [lookup_key ] = attribute_container
239
- self ._attribute_container_cache .move_to_end (lookup_key , last = False )
240
-
241
226
def _CacheAttributeContainerForWrite (
242
227
self , container_type , column_names , values ):
243
228
"""Caches an attribute container for writing.
@@ -515,26 +500,6 @@ def _GetAttributeContainersWithFilter(
515
500
if self ._storage_profiler :
516
501
self ._storage_profiler .StopTiming ('get_containers' )
517
502
518
- def _GetCachedAttributeContainer (self , container_type , index ):
519
- """Retrieves a specific cached attribute container.
520
-
521
- Args:
522
- container_type (str): attribute container type.
523
- index (int): attribute container index.
524
-
525
- Returns:
526
- AttributeContainer: attribute container or None if not available.
527
-
528
- Raises:
529
- IOError: when there is an error querying the attribute container store.
530
- OSError: when there is an error querying the attribute container store.
531
- """
532
- lookup_key = f'{ container_type :s} .{ index :d} '
533
- attribute_container = self ._attribute_container_cache .get (lookup_key , None )
534
- if attribute_container :
535
- self ._attribute_container_cache .move_to_end (lookup_key , last = False )
536
- return attribute_container
537
-
538
503
def _HasTable (self , table_name ):
539
504
"""Determines if a specific table exists.
540
505
@@ -835,7 +800,7 @@ def Close(self):
835
800
OSError: if the attribute container store is already closed.
836
801
"""
837
802
if not self ._is_open :
838
- raise IOError ('Storage file already closed.' )
803
+ raise IOError ('Attribute container store already closed.' )
839
804
840
805
if self ._connection :
841
806
self ._Flush ()
@@ -1034,7 +999,7 @@ def Open(self, path=None, read_only=True, **unused_kwargs): # pylint: disable=a
1034
999
ValueError: if path is missing.
1035
1000
"""
1036
1001
if self ._is_open :
1037
- raise IOError ('Storage file already opened.' )
1002
+ raise IOError ('Attribute container store already opened.' )
1038
1003
1039
1004
if not path :
1040
1005
raise ValueError ('Missing path.' )
0 commit comments