MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / swapdbCommand

Function swapdbCommand

src/db.cpp:1757–1805  ·  view source on GitHub ↗

SWAPDB db1 db2 */

Source from the content-addressed store, hash-verified

1755
1756/* SWAPDB db1 db2 */
1757void swapdbCommand(client *c) {
1758 int id1, id2, oriIdx;
1759
1760 /* Not allowed in cluster mode: we have just DB 0 there. */
1761 if (g_pserver->cluster_enabled) {
1762 addReplyError(c,"SWAPDB is not allowed in cluster mode");
1763 return;
1764 }
1765
1766 /* Get the two DBs indexes. */
1767 if (getIntFromObjectOrReply(c, c->argv[1], &id1,
1768 "invalid first DB index") != C_OK)
1769 return;
1770
1771 if (getIntFromObjectOrReply(c, c->argv[2], &id2,
1772 "invalid second DB index") != C_OK)
1773 return;
1774
1775 // get client's original db's index
1776 for (int idb=0; idb < cserver.dbnum; ++idb) {
1777 if (g_pserver->db[idb]->id == c->db->id) {
1778 oriIdx = idb;
1779 break;
1780 }
1781 }
1782
1783 /* Swap... */
1784 if (dbSwapDatabases(id1,id2) == C_ERR) {
1785 addReplyError(c,"DB index is out of range");
1786 return;
1787 } else {
1788 RedisModuleSwapDbInfo si = {REDISMODULE_SWAPDBINFO_VERSION,(int32_t)id1,(int32_t)id2};
1789 moduleFireServerEvent(REDISMODULE_EVENT_SWAPDB,0,&si);
1790 g_pserver->dirty++;
1791
1792 // set client's db to original db
1793 c->db=g_pserver->db[oriIdx];
1794
1795 // Persist the databse index to dbid mapping into FLASH for later recovery.
1796 if (g_pserver->m_pstorageFactory != nullptr && g_pserver->metadataDb != nullptr) {
1797 std::string dbid_key = "db-" + std::to_string(id1);
1798 g_pserver->metadataDb->insert(dbid_key.c_str(), dbid_key.length(), &g_pserver->db[id1]->id, sizeof(g_pserver->db[id1]->id), true);
1799
1800 dbid_key = "db-" + std::to_string(id2);
1801 g_pserver->metadataDb->insert(dbid_key.c_str(), dbid_key.length(), &g_pserver->db[id2]->id, sizeof(g_pserver->db[id2]->id), true);
1802 }
1803 addReply(c,shared.ok);
1804 }
1805}
1806
1807/*-----------------------------------------------------------------------------
1808 * Expires API

Callers

nothing calls this directly

Calls 6

addReplyErrorFunction · 0.85
getIntFromObjectOrReplyFunction · 0.85
dbSwapDatabasesFunction · 0.85
moduleFireServerEventFunction · 0.85
addReplyFunction · 0.85
insertMethod · 0.45

Tested by

no test coverage detected