Skip to content

Commit d625427

Browse files
committed
Single & Multi-Insertion Done
1 parent c83d428 commit d625427

File tree

2 files changed

+88
-10
lines changed

2 files changed

+88
-10
lines changed

filexdb/collection.py

Lines changed: 66 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
from typing import Mapping, List
23

34
from .document import Document
45
from .fileio import FileIO, BinaryFileIO
@@ -25,7 +26,7 @@ def __init__(self, col_name: str, binary_file: FileIO) -> None:
2526
self._database[self._col_name] = []
2627
self._collection: list = self._database[self._col_name]
2728

28-
def insert(self, document: dict) -> None:
29+
def insert(self, document: Mapping) -> int:
2930
"""
3031
Inserts a single Document into the Database.
3132
@@ -34,14 +35,71 @@ def insert(self, document: dict) -> None:
3435
:param document: Document to insert into database
3536
:return: None
3637
"""
38+
# Make sure the document implements the ``Mapping`` interface
39+
if not isinstance(document, Mapping):
40+
raise ValueError('Document is not a Dictionary')
3741

38-
# Append the document into the Collection
39-
self._collection.append(document)
42+
# Create a Document
43+
_document = Document(document)
4044

41-
# Add modified Collection to Database
42-
self._database[self._col_name] = self._collection
43-
# print(self._database)
44-
# Write current state of Database into the Database-file
45-
self._binary_file.write(self._database)
45+
# Numbers of Document
46+
doc_count: int = 0
47+
48+
# check Document is already exist or not
49+
if not self._doc_is_exists(_document.id):
50+
51+
# Append the document into the Collection
52+
self._collection.append(_document)
53+
54+
# Add modified Collection to Database
55+
self._database[self._col_name] = self._collection
56+
57+
# print(self._database)
58+
# Write current state of Database into the Database-file
59+
self._binary_file.write(self._database)
60+
61+
return doc_count + 1
62+
else:
63+
raise ValueError(f"Document id `{_document.id}` is already exists")
64+
65+
def insert_all(self, document_list: List[Mapping]) -> int:
66+
"""
67+
Inserts a single ``Document`` into the ``Database``.
68+
69+
Document should be a ``List`` of ``JSON Object``.
70+
71+
:param document_list: List of Documents to insert into Database
72+
:return: Inserted row(s) count
73+
"""
74+
75+
# Make sure the document_list implements the ``List`` interface.
76+
if not isinstance(document_list, List):
77+
raise ValueError('Document is not a List of Dictionary')
78+
79+
# Numbers of Document
80+
doc_count: int = 0
81+
82+
# Iterate over all Documents of document_list & Insert one by one.
83+
for document in document_list:
84+
85+
# insert every single document in Database & increment ``doc_count``.
86+
doc_count += self.insert(document)
87+
88+
return doc_count
89+
90+
91+
# ======================== #
92+
def _doc_is_exists(self, doc_id: str | int) -> bool:
93+
# Iterate over all Documents of Collection
94+
for doc in self._collection:
95+
if doc["_id_"] == doc_id:
96+
return True
4697

98+
return False
4799

100+
def _get_document_by_id(self, doc_id) -> Document | str:
101+
for doc in self._collection:
102+
if doc["_id_"] == doc_id:
103+
return doc
104+
else:
105+
return "none"

filexdb/document.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
1+
from typing import Mapping
2+
import uuid
3+
4+
5+
def _get_id():
6+
_id = uuid.uuid1()
7+
return _id.hex
18

29

310
class Document(dict):
4-
def __init__(self) -> None:
5-
super().__init__()
11+
def __init__(self, value: Mapping) -> None:
12+
self.id = None
13+
14+
_id_obj = {
15+
"_id_": _get_id()
16+
}
17+
18+
if "_id_" in value.keys():
19+
self._doc = value
20+
self.id = value["_id_"]
21+
else:
22+
self._doc = (_id_obj | value)
23+
self.id = self._doc["_id_"]
24+
25+
super().__init__(self._doc)

0 commit comments

Comments
 (0)