Enhanced iterator with a filter option in order to copy the whole object data only when it is required (improves performance a little bit on huge databases 1000+)

This commit is contained in:
Jean Jacques Avril 2022-02-17 00:48:11 +01:00
parent fc44a3fd8d
commit b6b4786b87
7 changed files with 53 additions and 69 deletions

View File

@ -1,5 +1,5 @@
{ {
"THEMEID": 1, "THEMEID": 1,
"SSID":"DoorLock", "SSID":"DoorLock",
"PASS":"geheim" "PASS":"geheim123"
} }

View File

@ -1 +0,0 @@
test

View File

@ -1,10 +0,0 @@
1,Max1,Muster,RFID,12341,1
2,Max2,Muster,RFID,12342,1
3,Max3,Muster,RFID,12343,1
4,Max4,Muster,RFID,12344,1
5,Max5,Muster,RFID,12345,1
6,Max6,Muster,RFID,12346,1
7,Max7,Muster,RFID,12347,1
8,Max8,Muster,RFID,12348,1
9,Max9,Muster,RFID,12349,1
10,Max20,Muster,RFID,12340,1

View File

@ -1,34 +0,0 @@
[
{
"ID": 1,
"FIRST_NAME": "Duplicate",
"LAST_NAME": "Test",
"RFID_UID": "",
"USER_PIN": 2323,
"ENABLED": true
},
{
"ID": 5,
"FIRST_NAME": "JSON",
"LAST_NAME": "User1",
"RFID_UID": "",
"USER_PIN": 123123,
"ENABLED": true
},
{
"ID": 6,
"FIRST_NAME": "JSON2",
"LAST_NAME": "User2",
"RFID_UID": "",
"USER_PIN": 321,
"ENABLED": true
},
{
"ID": 7,
"FIRST_NAME": "JSON3",
"LAST_NAME": "User3",
"RFID_UID": "",
"USER_PIN": 147,
"ENABLED": true
}
]

View File

@ -8,12 +8,12 @@ UserDb::UserDb()
User UserDb::user_by_pin(String cmp) User UserDb::user_by_pin(String cmp)
{ {
Serial.println("Stearching for user with pin number " + cmp); Serial.println("Searching for user with pin number " + cmp);
Iterator it = begin(); Iterator it = begin_with_filter(&cmp,USERATTRIBUTES::USER_PIN);
do do
{ {
User temp = *it; User temp = *it;
if (temp.user_pin == cmp) if (temp.enabled)
return temp; return temp;
++it; ++it;
} while (it.has_next()); } while (it.has_next());
@ -21,12 +21,12 @@ User UserDb::user_by_pin(String cmp)
} }
User UserDb::user_by_rfid(String cmp) User UserDb::user_by_rfid(String cmp)
{ {
Serial.println("Stearching for user with rfid " + cmp); Serial.println("Searching for user with rfid " + cmp);
Iterator it = begin(); Iterator it = begin_with_filter(&cmp,USERATTRIBUTES::RFID_UID);
do do
{ {
User temp = *it; User temp = *it;
if (temp.rfid_uid == cmp) if (temp.enabled)
return temp; return temp;
++it; ++it;
} while (it.has_next()); } while (it.has_next());
@ -34,7 +34,7 @@ User UserDb::user_by_rfid(String cmp)
} }
User UserDb::user_by_uid(unsigned long cmp){ User UserDb::user_by_uid(unsigned long cmp){
Serial.println("Stearching for user with user id " + String(cmp)); Serial.println("Searching for user with user id " + String(cmp));
Iterator it = begin(); Iterator it = begin();
do do
{ {

View File

@ -15,7 +15,8 @@ namespace userdb
LAST_NAME, LAST_NAME,
RFID_UID, RFID_UID,
USER_PIN, USER_PIN,
ENABLED ENABLED,
NONE
}; };
struct User struct User
{ {
@ -46,7 +47,7 @@ namespace userdb
User user_by_pin(String); User user_by_pin(String);
User user_by_rfid(String); User user_by_rfid(String);
User user_by_uid(unsigned long); User user_by_uid(unsigned long);
static User read_csv_line(String instring, int line = -1) static User read_csv_line(String &instring, int line, String *match, USERATTRIBUTES attr)
{ {
int locations[5]; int locations[5];
int location_index = 0; int location_index = 0;
@ -60,11 +61,28 @@ namespace userdb
} }
} }
User res; User res;
if (attr == RFID_UID)
{
res.rfid_uid = instring.substring(locations[2] + 1, locations[3]);
if (!res.rfid_uid.equals(*match))
return res;
res.user_pin = instring.substring(locations[3] + 1, locations[4]);
}
else if (attr == USER_PIN)
{
res.user_pin = instring.substring(locations[3] + 1, locations[4]);
if (!res.user_pin.equals(*match))
return res;
res.rfid_uid = instring.substring(locations[2] + 1, locations[3]);
}
else
{
res.rfid_uid = instring.substring(locations[2] + 1, locations[3]);
res.user_pin = instring.substring(locations[3] + 1, locations[4]);
}
res.uid = instring.substring(0, locations[0]).toInt(); res.uid = instring.substring(0, locations[0]).toInt();
res.first_name = instring.substring(locations[0] + 1, locations[1]); res.first_name = instring.substring(locations[0] + 1, locations[1]);
res.last_name = instring.substring(locations[1] + 1, locations[2]); res.last_name = instring.substring(locations[1] + 1, locations[2]);
res.rfid_uid = instring.substring(locations[2] + 1, locations[3]);
res.user_pin = instring.substring(locations[3] + 1, locations[4]);
res.enabled = instring.charAt(locations[4] + 1) == '1' ? true : false; res.enabled = instring.charAt(locations[4] + 1) == '1' ? true : false;
res.line = line; res.line = line;
return res; return res;
@ -78,8 +96,16 @@ namespace userdb
using reference = User; using reference = User;
using difference_type = std::ptrdiff_t; using difference_type = std::ptrdiff_t;
Iterator(File data) : db_file(data) Iterator(File data) : Iterator(data, nullptr, NONE)
{ {
}
Iterator(File data, String *m, USERATTRIBUTES attr) : db_file(data), filter_attr(attr), match(m)
{
if (filter_attr == NONE)
Serial.println("Started user iterator with ");
else
Serial.println("Started filtered user iterator with " + String(filter_attr) + " query: " + *match);
if (!data) if (!data)
{ {
Serial.println("Failed to open userdb while creating an iterator"); Serial.println("Failed to open userdb while creating an iterator");
@ -113,7 +139,7 @@ namespace userdb
if (db_file.available()) if (db_file.available())
{ {
String temp = db_file.readStringUntil('\n'); String temp = db_file.readStringUntil('\n');
current = read_csv_line(temp, line); current = read_csv_line(temp, line, match, filter_attr);
line++; line++;
} }
else else
@ -122,14 +148,17 @@ namespace userdb
current = User{}; current = User{};
db_file.close(); db_file.close();
} }
}; }
private: private:
File db_file; File db_file;
User current; User current;
USERATTRIBUTES filter_attr;
String *match;
bool available = true; bool available = true;
unsigned int line = 0; unsigned int line = 0;
}; };
Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); } Iterator begin() { return Iterator(LittleFS.open("userdb.csv", "r")); }
Iterator begin_with_filter(String *match, USERATTRIBUTES attr) { return Iterator(LittleFS.open("userdb.csv", "r"), match, attr); }
}; };
} }

View File

@ -34,12 +34,12 @@ void setup()
Persistence::Configuration config = persistence.loadConfig(); Persistence::Configuration config = persistence.loadConfig();
// users.ImportFromPersistence(); // users.ImportFromPersistence();
Serial.begin(9600); Serial.begin(9600);
Serial.print("Starting"); Serial.println("Starting System");
Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!"); Serial.println("\t1. Network config: " + WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
if (strlen(config.PASS) > 0) if (strlen(config.PASS) > 0)
Serial.println(WiFi.softAP(config.SSID, config.PASS) ? "Ready" : "Failed!"); Serial.println("\t1. AP setup with password: " + WiFi.softAP(config.SSID, config.PASS) ? "Ready" : "Failed!");
else else
Serial.println(WiFi.softAP(config.SSID) ? "Ready" : "Failed!"); Serial.println("\t1. AP setup without password: " +WiFi.softAP(config.SSID) ? "Ready" : "Failed!");
delay(250); delay(250);
#ifdef DEBUG #ifdef DEBUG
userdatabase.print_to_serial(); userdatabase.print_to_serial();