77#include " ../cluster.h"
88#include " rocksdbfactor_internal.h"
99
10+ template class std ::basic_string<char >;
11+
1012static const char keyprefix[] = INTERNAL_KEY_PREFIX;
1113
1214rocksdb::Options DefaultRocksDBOptions ();
@@ -24,8 +26,9 @@ bool FInternalKey(const char *key, size_t cch)
2426
2527std::string getPrefix (unsigned int hashslot)
2628{
27- char *hash_char = (char *)&hashslot;
28- return std::string (hash_char + (sizeof (unsigned int ) - 2 ), 2 );
29+ HASHSLOT_PREFIX_TYPE slot = HASHSLOT_PREFIX_ENDIAN ((HASHSLOT_PREFIX_TYPE)hashslot);
30+ char *hash_char = (char *)&slot;
31+ return std::string (hash_char, HASHSLOT_PREFIX_BYTES);
2932}
3033
3134std::string prefixKey (const char *key, size_t cchKey)
@@ -184,7 +187,7 @@ bool RocksDBStorageProvider::enumerate(callback fn) const
184187 if (FInternalKey (it->key ().data (), it->key ().size ()))
185188 continue ;
186189 ++count;
187- bool fContinue = fn (it->key ().data ()+2 , it->key ().size ()-2 , it->value ().data (), it->value ().size ());
190+ bool fContinue = fn (it->key ().data ()+HASHSLOT_PREFIX_BYTES , it->key ().size ()-HASHSLOT_PREFIX_BYTES , it->value ().data (), it->value ().size ());
188191 if (!fContinue )
189192 break ;
190193 }
@@ -203,17 +206,17 @@ bool RocksDBStorageProvider::enumerate_hashslot(callback fn, unsigned int hashsl
203206 std::string prefix = getPrefix (hashslot);
204207 std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator (ReadOptions (), m_spcolfamily.get ()));
205208 size_t count = 0 ;
206- for (it->Seek (prefix. c_str () ); it->Valid (); it->Next ()) {
209+ for (it->Seek (prefix); it->Valid (); it->Next ()) {
207210 if (FInternalKey (it->key ().data (), it->key ().size ()))
208211 continue ;
209- if (strncmp ( it->key ().data (),prefix. c_str (), 2 ) != 0 )
212+ if (HASHSLOT_PREFIX_RECOVER (*(HASHSLOT_PREFIX_TYPE *) it->key ().data ()) != hashslot )
210213 break ;
211214 ++count;
212- bool fContinue = fn (it->key ().data ()+2 , it->key ().size ()-2 , it->value ().data (), it->value ().size ());
215+ bool fContinue = fn (it->key ().data ()+HASHSLOT_PREFIX_BYTES , it->key ().size ()-HASHSLOT_PREFIX_BYTES , it->value ().data (), it->value ().size ());
213216 if (!fContinue )
214217 break ;
215218 }
216- bool full_iter = !it->Valid () || (strncmp ( it->key ().data (),prefix. c_str (), 2 ) != 0 );
219+ bool full_iter = !it->Valid () || (HASHSLOT_PREFIX_RECOVER (*(HASHSLOT_PREFIX_TYPE *) it->key ().data ()) != hashslot );
217220 if (full_iter && count != g_pserver->cluster ->slots_keys_count [hashslot])
218221 {
219222 printf (" WARNING: rocksdb hashslot count mismatch" );
@@ -227,7 +230,8 @@ void RocksDBStorageProvider::setExpire(const char *key, size_t cchKey, long long
227230{
228231 rocksdb::Status status;
229232 std::unique_lock<fastlock> l (m_lock);
230- std::string prefix ((const char *)&expire,sizeof (long long ));
233+ long long beExpire = htobe64 (expire);
234+ std::string prefix ((const char *)&beExpire,sizeof (long long ));
231235 std::string strKey (key, cchKey);
232236 if (m_spbatch != nullptr )
233237 status = m_spbatch->Put (m_spexpirecolfamily.get (), rocksdb::Slice (prefix + strKey), rocksdb::Slice (strKey));
@@ -241,7 +245,8 @@ void RocksDBStorageProvider::removeExpire(const char *key, size_t cchKey, long l
241245{
242246 rocksdb::Status status;
243247 std::unique_lock<fastlock> l (m_lock);
244- std::string prefix ((const char *)&expire,sizeof (long long ));
248+ long long beExpire = htobe64 (expire);
249+ std::string prefix ((const char *)&beExpire,sizeof (long long ));
245250 std::string strKey (key, cchKey);
246251 std::string fullKey = prefix + strKey;
247252 if (!FExpireExists (fullKey))
@@ -278,7 +283,7 @@ std::vector<std::string> RocksDBStorageProvider::getEvictionCandidates(unsigned
278283 for (it->Seek (randomHashSlot ()); it->Valid () && result.size () < count; it->Next ()) {
279284 if (FInternalKey (it->key ().data (), it->key ().size ()))
280285 continue ;
281- result.emplace_back (it->key ().data () + 2 , it->key ().size () - 2 );
286+ result.emplace_back (it->key ().data () + HASHSLOT_PREFIX_BYTES , it->key ().size () - HASHSLOT_PREFIX_BYTES );
282287 }
283288 } else {
284289 std::unique_ptr<rocksdb::Iterator> it = std::unique_ptr<rocksdb::Iterator>(m_spdb->NewIterator (ReadOptions (), m_spexpirecolfamily.get ()));
0 commit comments