1
1
import json
2
2
from typing import Mapping , List
3
3
4
- from .document import Document
4
+ from .document import Document , JsonArray
5
5
from .fileio import FileIO
6
6
7
7
14
14
15
15
16
16
class Collection :
17
- def __init__ (self , col_name : str , binary_file : FileIO ) -> None :
17
+ def __init__ (self , col_name : str , file_handler : FileIO ) -> None :
18
18
self ._col_name = col_name
19
- self ._binary_file = binary_file
19
+ self ._file_handler = file_handler
20
20
21
21
# Get the data of existing Database or empty database.
22
- self ._database = self ._binary_file . read ()
22
+ self ._database = self ._get_database ()
23
23
24
- self ._cursor : int = 0
25
-
26
- # Initiate a default Collection.
27
- # Check the Collection is already exists or no.
28
- if self ._col_name in self ._database .keys ():
24
+ # Initiating Collecting
25
+ self ._collection : JsonArray = self ._get_collection ()
29
26
30
- # Get the existing Collection
31
- self ._collection : list = self ._database [self ._col_name ]
32
-
33
- else :
34
- # Create new Collection
35
- self ._database [self ._col_name ] = []
36
- self ._collection : list = self ._database [self ._col_name ]
27
+ # Cursor
28
+ self ._cursor : int = 0
37
29
38
30
def insert (self , document : Mapping ) -> str :
39
31
"""
40
32
Inserts a single Document into the Database.
41
33
42
34
Document should be JSON Object.
43
35
44
- :param document: Document to insert into database
45
- :return: None
36
+ :param document: Document to insert into the database.
37
+ :return: Document ID.
46
38
"""
39
+
47
40
# Make sure the document implements the ``Mapping`` interface
48
41
if not isinstance (document , Mapping ):
49
42
raise ValueError ('Document is not a Dictionary' )
@@ -52,30 +45,33 @@ def insert(self, document: Mapping) -> str:
52
45
if "_id_" in document .keys ():
53
46
raise KeyError (f"You are not allowed to modify key `_id_`" )
54
47
48
+ # getting Database
49
+ _database = self ._get_database ()
50
+
55
51
# Create a Document
56
52
_document = Document (document )
57
53
58
- # id of Document
59
- _doc_id : str = _document .id
54
+ # ID of Document
55
+ _doc_id : str = str ( _document .id )
60
56
61
57
# check Document is already exist or not
62
- if not self ._doc_is_exists (_document .id ):
58
+ if not self ._doc_is_exists (str ( _document .id ) ):
63
59
64
60
# Append the document into the Collection
65
61
self ._collection .append (_document )
66
62
67
63
# Add modified Collection to Database
68
- self . _database [self ._col_name ] = self ._collection
64
+ _database [self ._col_name ] = self ._collection
69
65
70
66
# print(self._database)
71
67
# Write current state of Database into the Database-file
72
- self ._binary_file .write (self . _database )
68
+ self ._file_handler .write (_database )
73
69
74
70
return _doc_id
75
71
else :
76
72
raise ValueError (f"Document id `{ _document .id } ` is already exists" )
77
73
78
- def insert_all (self , document_list : List [Mapping ]) -> List [ str ] :
74
+ def insert_all (self , document_list : List [Mapping ]) -> JsonArray :
79
75
"""
80
76
Inserts a single ``Document`` into the ``Database``.
81
77
@@ -97,43 +93,9 @@ def insert_all(self, document_list: List[Mapping]) -> List[str]:
97
93
# insert every single document in Database & increment ``doc_count``.
98
94
_doc_id .append (self .insert (document ))
99
95
100
- return _doc_id
101
-
102
- """ FIND_ONE
103
- def __find_one(self, query: Mapping = None) -> Document | None:
104
- "
105
- Finds a single ``Document`` of ``Collection``.
106
-
107
- If ``query`` is None then returns all the ``Documents`` of ``Collection``.
108
-
109
- If ``query`` is not None then returns only the first occurrence.
96
+ return JsonArray (_doc_id )
110
97
111
- :param query: Condition to search Document
112
- :return: Document
113
- "
114
- # Default result
115
- _result = {}
116
-
117
- # Make sure the query implements the ``Mapping`` interface.
118
- if not isinstance(query, Mapping | None):
119
- raise ValueError('Document is not a Dictionary')
120
-
121
-
122
-
123
- # Check if has ``query`` or not
124
- if query is None:
125
- _result = self._collection[self._cursor]
126
- self._reset_cursor()
127
- else:
128
- print(self._cursor)
129
- _result = self._find_document_by_query(query)
130
- self._reset_cursor()
131
- _result = _result[self._cursor]
132
-
133
- return _result
134
- """
135
-
136
- def find (self , query = None , limit = None ) -> List [Document ]:
98
+ def find (self , query = None , limit = None ) -> JsonArray :
137
99
"""
138
100
Finds all ``Document`` of ``Collection``.
139
101
@@ -160,7 +122,7 @@ def find(self, query=None, limit=None) -> List[Document]:
160
122
raise ValueError ('Document is not a Tuple' )
161
123
162
124
# if limit, Check everything ok
163
- _limit_start = _limit_end = None
125
+ _limit_start = _limit_end = 0
164
126
165
127
if limit and type (limit ) == type ((1 , 3 )):
166
128
if len (limit ) == 2 :
@@ -189,7 +151,7 @@ def find(self, query=None, limit=None) -> List[Document]:
189
151
else :
190
152
_result = self ._collection
191
153
192
- return _result
154
+ return JsonArray ( _result )
193
155
194
156
elif query is not None and type (query ) == type ({}):
195
157
if limit :
@@ -208,7 +170,7 @@ def find(self, query=None, limit=None) -> List[Document]:
208
170
self ._reset_cursor ()
209
171
210
172
# check if lower limit is valid or not
211
- if _limit_start >= len (_result ):
173
+ if _limit_start >= len (_result ) and _limit_start != 0 :
212
174
raise ValueError (f"lower limit should be smaller than length of result" )
213
175
else :
214
176
# Travers limited result
@@ -225,9 +187,9 @@ def find(self, query=None, limit=None) -> List[Document]:
225
187
226
188
self ._reset_cursor ()
227
189
228
- return _result
190
+ return JsonArray ( _result )
229
191
230
- def delete (self , query = None ) -> List [ str ] :
192
+ def delete (self , query = None ) -> JsonArray :
231
193
"""
232
194
Delete single or multiple Document when meet the Conditions or ``query``.
233
195
@@ -248,11 +210,11 @@ def delete(self, query=None) -> List[str]:
248
210
self ._collection .remove (_doc )
249
211
_doc_id .append (_doc ["_id_" ])
250
212
251
- self ._binary_file .write (self ._database )
213
+ self ._file_handler .write (self ._database )
252
214
253
- return _doc_id
215
+ return JsonArray ( _doc_id )
254
216
255
- def update (self , document : Mapping , query = None ) -> List [ str ] :
217
+ def update (self , document : Mapping , query = None ) -> JsonArray :
256
218
"""
257
219
Fetch all the Documents mathc the conditions and update them.
258
220
@@ -284,30 +246,105 @@ def update(self, document: Mapping, query=None) -> List[str]:
284
246
_doc_id .append (_doc ["_id_" ])
285
247
286
248
# Write current state of Database
287
- self ._binary_file .write (self ._database )
249
+ self ._file_handler .write (self ._database )
288
250
289
- return _doc_id
251
+ return JsonArray ( _doc_id )
290
252
291
- def count (self , query = None , limit : tuple = None ) -> int :
253
+ def rename (self , new_name : str ) -> int :
292
254
"""
293
- Return amount of Document found.
255
+ This method used to change the name of collection.
256
+ Takes current name & new name to change name of the collection.
294
257
295
- :param query: Condition to search Document.
296
- :param limit: If there is any limit.
297
- :return: (int) amount of Document found.
258
+ :param new_name: New name for collection.
259
+ :return: Amount of affected collection.
298
260
"""
299
- count = len (self .find (query = query , limit = limit ))
261
+
262
+ # Initiating counter
263
+ count = 0
264
+
265
+ # Checking the collection is already exist or not
266
+ if new_name not in self ._database .keys ():
267
+ # Creating new collection and
268
+ # Putting old data into new collection
269
+ self ._database [new_name ] = self ._collection
270
+
271
+ # Writing Current database status into the file
272
+ self ._file_handler .write (self ._database )
273
+
274
+ # Remove old collection
275
+ self .drop ()
276
+
277
+ # Increasing counter
278
+ count += 1
279
+
280
+ return count
281
+
282
+ def drop (self ) -> int :
283
+ """
284
+ Deletes the selected collection from the database
285
+
286
+ :return: Amount of affected collection
287
+ """
288
+
289
+ # Initiating counter
290
+ count = 0
291
+
292
+ # Getting database
293
+ _database = self ._file_handler .read ()
294
+
295
+ # Check database has the collection or not
296
+ if self ._col_name in _database .keys ():
297
+ # Removing collection from database
298
+ _database .pop (self ._col_name )
299
+
300
+ # Writing current status of database into the file system.
301
+ self ._file_handler .write (_database )
302
+
303
+ # Increasing counter
304
+ count += 1
300
305
301
306
return count
302
307
308
+ # ----------------------------------------------------------------#
309
+ def _get_database (self ) -> Document :
310
+ """
311
+ Getting Database
312
+
313
+ :return: Database
314
+ """
315
+ # Get the data of existing Database or empty database.
316
+ database = Document (self ._file_handler .read (), False )
317
+
318
+ return database
319
+
320
+ def _get_collection (self ) -> JsonArray :
321
+ """
322
+ Getting Collection
323
+
324
+ :return: Collection
325
+ """
326
+ # Initiate a default Collection.
327
+ # Check the Collection is already exists or no.
328
+ if self ._col_name in self ._database .keys ():
329
+
330
+ # Get the existing Collection
331
+ _collection : JsonArray = self ._database [self ._col_name ]
332
+
333
+ else :
334
+ # Create new Collection
335
+ self ._database [self ._col_name ] = JsonArray ([])
336
+ _collection = self ._database [self ._col_name ]
337
+
338
+ return JsonArray (_collection )
339
+
303
340
def _reset_cursor (self ) -> None :
304
341
"""
305
342
Reset Cursor Pointer to 0th index
306
343
:return: None
307
344
"""
308
345
self ._cursor = 0
309
346
310
- def _find_document_by_query (self , query : Mapping ) -> List :
347
+ def _find_document_by_query (self , query : Mapping ) -> JsonArray | None :
311
348
"""
312
349
Finds a single ``Document`` of ``Collection``.
313
350
@@ -371,20 +408,12 @@ def _find_document_by_query(self, query: Mapping) -> List:
371
408
else :
372
409
return None
373
410
374
- return result
411
+ return JsonArray ( result )
375
412
376
- # ======================== #
377
413
def _doc_is_exists (self , doc_id : str ) -> bool :
378
414
# Iterate over all Documents of Collection
379
415
for doc in self ._collection :
380
416
if doc ["_id_" ] == doc_id :
381
417
return True
382
418
383
419
return False
384
-
385
- def _find_document_by_id (self , doc_id ) -> Document :
386
- for doc in self ._collection :
387
- if doc ["_id_" ] == doc_id :
388
- return doc
389
- else :
390
- return None
0 commit comments