kvdb.ino 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. Key Value database
  3. This is a file system based database
  4. that uses foldername as table name,
  5. filename as key and content as value
  6. Folder name and filename are limited to
  7. 5 characters as SDFS requirements.
  8. */
  9. //Root of the db on SD card, **must have tailing slash**
  10. const String DB_root = "/db/";
  11. //Clean the input for any input string
  12. String DBCleanInput(const String& inputString) {
  13. String trimmedString = inputString;
  14. //Replae all the slash that might breaks the file system
  15. trimmedString.replace("/", "");
  16. //Trim off the space before and after the string
  17. trimmedString.trim();
  18. return trimmedString;
  19. }
  20. //Database init create all the required table for basic system operations
  21. void DBInit() {
  22. /* Preference persistent store */
  23. DBNewTable("pref"); //Preference settings
  24. /* User Authentications Tables */
  25. DBNewTable("auth"); //Auth session store
  26. DBNewTable("user"); //User accounts store
  27. DBNewTable("sess"); //Session store
  28. /* Share System Tables */
  29. DBNewTable("shln");//Shared links to filename map
  30. DBNewTable("shfn");//Shared filename to links map
  31. }
  32. //Create a new Database table
  33. void DBNewTable(String tableName) {
  34. tableName = DBCleanInput(tableName);
  35. if (!SD.exists(DB_root + tableName)) {
  36. SD.mkdir(DB_root + tableName);
  37. Serial.println("KVDB table created "+ tableName);
  38. }
  39. }
  40. //Check if a database table exists
  41. bool DBTableExists(String tableName) {
  42. tableName = DBCleanInput(tableName);
  43. return SD.exists(DB_root + tableName);
  44. }
  45. //Write a key to a table, return true if succ
  46. bool DBWrite(String tableName, String key, String value) {
  47. if (!DBTableExists(tableName)) {
  48. Serial.println("KVDB table name "+ tableName + " not exists!");
  49. return false;
  50. }
  51. tableName = DBCleanInput(tableName);
  52. key = DBCleanInput(key);
  53. String fsDataPath = DB_root + tableName + "/" + key;
  54. if (SD.exists(fsDataPath)) {
  55. //Entry already exists. Delete it
  56. SD.remove(fsDataPath);
  57. }
  58. //Write new data to it
  59. File targetEntry = SD.open(fsDataPath, FILE_WRITE);
  60. targetEntry.print(value);
  61. targetEntry.close();
  62. Serial.println("KVDB Entry written to: "+ fsDataPath);
  63. return true;
  64. }
  65. //Read from database, require table name and key
  66. String DBRead(String tableName, String key) {
  67. if (!DBTableExists(tableName)) {
  68. return "";
  69. }
  70. tableName = DBCleanInput(tableName);
  71. key = DBCleanInput(key);
  72. String fsDataPath = DB_root + tableName + "/" + key;
  73. if (!SD.exists(fsDataPath)) {
  74. //Target not exists. Return empty string
  75. return "";
  76. }
  77. String value = "";
  78. File targetEntry = SD.open(fsDataPath, FILE_READ);
  79. while (targetEntry.available()) {
  80. value = value + targetEntry.readString();
  81. }
  82. targetEntry.close();
  83. return value;
  84. }
  85. //Check if a given key exists in the database
  86. bool DBKeyExists(String tableName, String key) {
  87. if (!DBTableExists(tableName)) {
  88. return false;
  89. }
  90. tableName = DBCleanInput(tableName);
  91. key = DBCleanInput(key);
  92. String fsDataPath = DB_root + tableName + "/" + key;
  93. return SD.exists(fsDataPath);
  94. }
  95. //Remove the key-value item from db, return true if succ
  96. bool DBRemove(String tableName, String key) {
  97. if (!DBKeyExists(tableName, key)){
  98. return false;
  99. }
  100. tableName = DBCleanInput(tableName);
  101. key = DBCleanInput(key);
  102. String fsDataPath = DB_root + tableName + "/" + key;
  103. SD.remove(fsDataPath);
  104. return true;
  105. }