kvdb.ino 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. }
  38. }
  39. //Check if a database table exists
  40. bool DBTableExists(String tableName) {
  41. tableName = DBCleanInput(tableName);
  42. return SD.exists(DB_root + tableName);
  43. }
  44. //Write a key to a table, return true if succ
  45. bool DBWrite(String tableName, String key, String value) {
  46. if (!DBTableExists(tableName)) {
  47. return false;
  48. }
  49. tableName = DBCleanInput(tableName);
  50. key = DBCleanInput(key);
  51. String fsDataPath = DB_root + tableName + "/" + key;
  52. if (SD.exists(fsDataPath)) {
  53. //Entry already exists. Delete it
  54. SD.remove(fsDataPath);
  55. }
  56. //Write new data to it
  57. File targetEntry = SD.open(fsDataPath, FILE_WRITE);
  58. targetEntry.print(value);
  59. targetEntry.close();
  60. return true;
  61. }
  62. //Read from database, require table name and key
  63. String DBRead(String tableName, String key) {
  64. if (!DBTableExists(tableName)) {
  65. return "";
  66. }
  67. tableName = DBCleanInput(tableName);
  68. key = DBCleanInput(key);
  69. String fsDataPath = DB_root + tableName + "/" + key;
  70. if (!SD.exists(fsDataPath)) {
  71. //Target not exists. Return empty string
  72. return "";
  73. }
  74. String value = "";
  75. File targetEntry = SD.open(fsDataPath, FILE_READ);
  76. while (targetEntry.available()) {
  77. value = value + targetEntry.readString();
  78. }
  79. targetEntry.close();
  80. return value;
  81. }
  82. //Check if a given key exists in the database
  83. bool DBKeyExists(String tableName, String key) {
  84. if (!DBTableExists(tableName)) {
  85. return false;
  86. }
  87. tableName = DBCleanInput(tableName);
  88. key = DBCleanInput(key);
  89. String fsDataPath = DB_root + tableName + "/" + key;
  90. return SD.exists(fsDataPath);
  91. }
  92. //Remove the key-value item from db, return true if succ
  93. bool DBRemove(String tableName, String key) {
  94. if (!DBKeyExists(tableName, key)){
  95. return false;
  96. }
  97. tableName = DBCleanInput(tableName);
  98. key = DBCleanInput(key);
  99. String fsDataPath = DB_root + tableName + "/" + key;
  100. SD.remove(fsDataPath);
  101. return true;
  102. }