database.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package database
  2. /*
  3. ArOZ Online Database Access Module
  4. author: tobychui
  5. This is an improved Object oriented base solution to the original
  6. aroz online database script.
  7. */
  8. import (
  9. "log"
  10. "runtime"
  11. "imuslab.com/zoraxy/mod/database/dbinc"
  12. )
  13. type Database struct {
  14. Db interface{} //This will be nil on openwrt, leveldb.DB on x64 platforms or bolt.DB on other platforms
  15. BackendType dbinc.BackendType
  16. Backend dbinc.Backend
  17. }
  18. func NewDatabase(dbfile string, backendType dbinc.BackendType) (*Database, error) {
  19. if runtime.GOARCH == "riscv64" {
  20. log.Println("RISCV hardware detected, ignoring the backend type and using FS emulated database")
  21. }
  22. return newDatabase(dbfile, backendType)
  23. }
  24. // Get the recommended backend type for the current system
  25. func GetRecommendedBackendType() dbinc.BackendType {
  26. //Check if the system is running on RISCV hardware
  27. if runtime.GOARCH == "riscv64" {
  28. //RISCV hardware, currently only support FS emulated database
  29. return dbinc.BackendFSOnly
  30. } else if runtime.GOOS == "windows" || (runtime.GOOS == "linux" && runtime.GOARCH == "amd64") {
  31. //Powerful hardware
  32. return dbinc.BackendBoltDB
  33. //return dbinc.BackendLevelDB
  34. }
  35. //Default to BoltDB, the safest option
  36. return dbinc.BackendBoltDB
  37. }
  38. /*
  39. Create / Drop a table
  40. Usage:
  41. err := sysdb.NewTable("MyTable")
  42. err := sysdb.DropTable("MyTable")
  43. */
  44. // Create a new table
  45. func (d *Database) NewTable(tableName string) error {
  46. return d.newTable(tableName)
  47. }
  48. // Check is table exists
  49. func (d *Database) TableExists(tableName string) bool {
  50. return d.tableExists(tableName)
  51. }
  52. // Drop the given table
  53. func (d *Database) DropTable(tableName string) error {
  54. return d.dropTable(tableName)
  55. }
  56. /*
  57. Write to database with given tablename and key. Example Usage:
  58. type demo struct{
  59. content string
  60. }
  61. thisDemo := demo{
  62. content: "Hello World",
  63. }
  64. err := sysdb.Write("MyTable", "username/message",thisDemo);
  65. */
  66. func (d *Database) Write(tableName string, key string, value interface{}) error {
  67. return d.write(tableName, key, value)
  68. }
  69. /*
  70. Read from database and assign the content to a given datatype. Example Usage:
  71. type demo struct{
  72. content string
  73. }
  74. thisDemo := new(demo)
  75. err := sysdb.Read("MyTable", "username/message",&thisDemo);
  76. */
  77. func (d *Database) Read(tableName string, key string, assignee interface{}) error {
  78. return d.read(tableName, key, assignee)
  79. }
  80. /*
  81. Check if a key exists in the database table given tablename and key
  82. if sysdb.KeyExists("MyTable", "username/message"){
  83. log.Println("Key exists")
  84. }
  85. */
  86. func (d *Database) KeyExists(tableName string, key string) bool {
  87. return d.keyExists(tableName, key)
  88. }
  89. /*
  90. Delete a value from the database table given tablename and key
  91. err := sysdb.Delete("MyTable", "username/message");
  92. */
  93. func (d *Database) Delete(tableName string, key string) error {
  94. return d.delete(tableName, key)
  95. }
  96. /*
  97. //List table example usage
  98. //Assume the value is stored as a struct named "groupstruct"
  99. entries, err := sysdb.ListTable("test")
  100. if err != nil {
  101. panic(err)
  102. }
  103. for _, keypairs := range entries{
  104. log.Println(string(keypairs[0]))
  105. group := new(groupstruct)
  106. json.Unmarshal(keypairs[1], &group)
  107. log.Println(group);
  108. }
  109. */
  110. func (d *Database) ListTable(tableName string) ([][][]byte, error) {
  111. return d.listTable(tableName)
  112. }
  113. /*
  114. Close the database connection
  115. */
  116. func (d *Database) Close() {
  117. d.close()
  118. }