database-leveldb.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3.0 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "config.h"
  17. #if USE_LEVELDB
  18. #include "database-leveldb.h"
  19. #include "log.h"
  20. #include "filesys.h"
  21. #include "exceptions.h"
  22. #include "util/string.h"
  23. #include "leveldb/db.h"
  24. #define ENSURE_STATUS_OK(s) \
  25. if (!(s).ok()) { \
  26. throw DatabaseException(std::string("LevelDB error: ") + \
  27. (s).ToString()); \
  28. }
  29. Database_LevelDB::Database_LevelDB(const std::string &savedir)
  30. {
  31. leveldb::Options options;
  32. options.create_if_missing = true;
  33. leveldb::Status status = leveldb::DB::Open(options,
  34. savedir + DIR_DELIM + "map.db", &m_database);
  35. ENSURE_STATUS_OK(status);
  36. }
  37. Database_LevelDB::~Database_LevelDB()
  38. {
  39. delete m_database;
  40. }
  41. bool Database_LevelDB::saveBlock(const v3s16 &pos, const std::string &data)
  42. {
  43. leveldb::Status status = m_database->Put(leveldb::WriteOptions(),
  44. i64tos(getBlockAsInteger(pos)), data);
  45. if (!status.ok()) {
  46. warningstream << "saveBlock: LevelDB error saving block "
  47. << PP(pos) << ": " << status.ToString() << std::endl;
  48. return false;
  49. }
  50. return true;
  51. }
  52. void Database_LevelDB::loadBlock(const v3s16 &pos, std::string *block)
  53. {
  54. std::string datastr;
  55. leveldb::Status status = m_database->Get(leveldb::ReadOptions(),
  56. i64tos(getBlockAsInteger(pos)), &datastr);
  57. *block = (status.ok()) ? datastr : "";
  58. }
  59. bool Database_LevelDB::deleteBlock(const v3s16 &pos)
  60. {
  61. leveldb::Status status = m_database->Delete(leveldb::WriteOptions(),
  62. i64tos(getBlockAsInteger(pos)));
  63. if (!status.ok()) {
  64. warningstream << "deleteBlock: LevelDB error deleting block "
  65. << PP(pos) << ": " << status.ToString() << std::endl;
  66. return false;
  67. }
  68. return true;
  69. }
  70. void Database_LevelDB::listAllLoadableBlocks(std::vector<v3s16> &dst)
  71. {
  72. leveldb::Iterator* it = m_database->NewIterator(leveldb::ReadOptions());
  73. for (it->SeekToFirst(); it->Valid(); it->Next()) {
  74. dst.push_back(getIntegerAsBlock(stoi64(it->key().ToString())));
  75. }
  76. ENSURE_STATUS_OK(it->status()); // Check for any errors found during the scan
  77. delete it;
  78. }
  79. #endif // USE_LEVELDB