11# -*- coding: utf-8 -*-
2+ import argparse
3+ from typing import Dict , List , Union
4+
25from findex_dict import FindexDict
36from findex_redis import FindexRedis
47from findex_sqlite import FindexSQLite
5- from cloudproof_py import findex
6- from typing import Union
7- import argparse
8+
9+ from cloudproof_py .findex import MasterKey , Label , IndexedValue , Findex , utils
810
911# Simple database containing the firstname and lastname of each user.
1012# Each line has a corresponding UID: 1, 2 or 3.
@@ -19,13 +21,13 @@ def main(backend: str = "Dict"):
1921 print ("Database to index:" , data )
2022
2123 # Initialize a symmetric key
22- master_key = findex . MasterKey .random ()
24+ master_key = MasterKey .random ()
2325
2426 # Initialize a random label
25- label = findex . Label .random ()
27+ label = Label .random ()
2628
2729 # Instance the class implementing the required callbacks
28- findex_interface : Union [findex . Findex .FindexUpsert , findex . Findex .FindexSearch ]
30+ findex_interface : Union [Findex .FindexUpsert , Findex .FindexSearch ]
2931 if backend == "Redis" :
3032 findex_interface = FindexRedis ()
3133 elif backend == "SQLite" :
@@ -37,7 +39,7 @@ def main(backend: str = "Dict"):
3739 indexed_values_and_keywords = {}
3840 for uid , keywords in data .items ():
3941 # Convert database UIDs to IndexedValue expected by Findex
40- location = findex . IndexedValue .from_location (uid .encode ("utf-8" ))
42+ location = IndexedValue .from_location (uid .encode ("utf-8" ))
4143 # This location has 2 keywords associated: the firstname and lastname
4244 indexed_values_and_keywords [location ] = keywords
4345
@@ -60,21 +62,21 @@ def main(backend: str = "Dict"):
6062
6163 # Create the alias `Joe` for `John`
6264 alias_graph = {
63- findex . IndexedValue .from_keyword (b"John" ): ["Joe" ],
65+ IndexedValue .from_keyword (b"John" ): ["Joe" ],
6466 }
6567 findex_interface .upsert (alias_graph , master_key , label )
6668
6769 # Now searching `Joe` will return the same location as `John`
68- print ("Search for Joe :" )
70+ print ("Search with aliases :" )
6971 print ("\t " , findex_interface .search (["Joe" ], master_key , label ))
7072
7173 # Generate an auto-completion graph:
7274 # For example, with the word `Wilkins`, one could upsert the following aliases:
7375 # ["Wil" => "Wilk", "Wilk" => "Wilki", "Wilki" => "Wilkin", "Wilkin" => "Wilkins"].
74- # A search for the Keyword Thi will then return the Location of `Wilkins`.
76+ # A search for the Keyword "Wil" will then return the Location of `Wilkins`.
7577
7678 # CloudProof provides a helper function to generate such graph
77- auto_completion_graph = findex . utils .generate_auto_completion (
79+ auto_completion_graph = utils .generate_auto_completion (
7880 ["Martin" , "Martial" , "Wilkins" ]
7981 )
8082 findex_interface .upsert (auto_completion_graph , master_key , label )
@@ -87,6 +89,17 @@ def main(backend: str = "Dict"):
8789 # `Mar` points to both Martin's and Martial's locations.
8890 # `Wil` only points to Wilkins' location.
8991
92+ print ("Search using the `progress_callback`: " )
93+
94+ def echo_progress_callback (res : Dict [str , List [IndexedValue ]]) -> bool :
95+ print ("\t Partial results:" , res )
96+ return True
97+
98+ found_locations = findex_interface .search (
99+ ["Mar" ], master_key , label , progress_callback = echo_progress_callback
100+ )
101+ print ("\t Final results:" , found_locations )
102+
90103
91104if __name__ == "__main__" :
92105
@@ -111,5 +124,5 @@ def main(backend: str = "Dict"):
111124 print ("Using in-memory SQLite" )
112125 main ("SQLite" )
113126 else :
114- print ("Using in-memory dictionnaries " )
127+ print ("Using in-memory dictionaries " )
115128 main ("Dict" )
0 commit comments