#include "db-leveldb.h" #include #include inline int64_t stoi64(const std::string &s) { std::stringstream tmp(s); long long t; tmp >> t; return t; } inline std::string i64tos(int64_t i) { std::ostringstream o; o< DBLevelDB::getBlockPos() { loadPosCache(); return posCache; } void DBLevelDB::loadPosCache() { if (posCacheLoaded) { return; } leveldb::Iterator * it = db->NewIterator(leveldb::ReadOptions()); for (it->SeekToFirst(); it->Valid(); it->Next()) { posCache.push_back(stoi64(it->key().ToString())); } delete it; posCacheLoaded = true; } DBBlockList DBLevelDB::getBlocksOnZ(int zPos) { DBBlockList blocks; std::string datastr; leveldb::Status status; int64_t psMin = (zPos * 16777216L) - 0x800000; int64_t psMax = (zPos * 16777216L) + 0x7fffff; for (std::vector::iterator it = posCache.begin(); it != posCache.end(); ++it) { int64_t i = *it; if (i < psMin || i > psMax) { continue; } status = db->Get(leveldb::ReadOptions(), i64tos(i), &datastr); if (status.ok()) { blocks.push_back( DBBlock(i, std::basic_string((const unsigned char*) datastr.data(), datastr.size()) ) ); } } return blocks; }