Skip to content

Commit 42980c9

Browse files
committed
Added dictionary support to Database. Store file and details as objects instead of just key-value.
1 parent 7d6bcce commit 42980c9

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

db.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,27 @@ def set_value(key: str, value: str):
5050
connection = get_connection()
5151
value = value.encode('utf-8')
5252
connection.set(key, value)
53+
54+
55+
def set_object(key: str, field: str, value: str):
56+
"""
57+
`key` is the file identifer. In our case a hash created using the file path.
58+
`field` is the field name. The possible values are `type`,`raw_image_content` and `processed_image_content`.
59+
`value` is the extracted text content after performing the Optical Character Recognition.
60+
HMSET <file_hash> raw_image_content <raw_image_content> processed_image_content <processed_image_content>
61+
"""
62+
connection = get_connection()
63+
value = value.encode('utf-8')
64+
connection.hset(key, field, value)
65+
66+
67+
def get_object(key: str, field: str):
68+
connection = get_connection()
69+
value = connection.hget(key, field)
70+
if value is None:
71+
return value
72+
# Redis stores bytes.
73+
# Application encodes the strings to utf-8 before putting in Redis.
74+
# Hence decode to utf-8 on read.
75+
value = value.decode('utf-8')
76+
return value

0 commit comments

Comments
 (0)