pixelplanet/utils/redisCopy.js

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2020-01-02 16:58:06 +00:00
/* @flow */
2020-03-29 21:16:44 +00:00
//this script just copies chunks from one redis to another
2020-01-02 16:58:06 +00:00
import redis from 'redis';
import bluebird from 'bluebird';
bluebird.promisifyAll(redis.RedisClient.prototype);
bluebird.promisifyAll(redis.Multi.prototype);
2020-03-29 20:42:30 +00:00
import {
TILE_SIZE,
THREE_TILE_SIZE,
} from '../src/core/constants';
2020-01-02 16:58:06 +00:00
//ATTENTION Make suer to set the rdis URLs right!!!
2020-03-29 20:42:30 +00:00
const oldurl = "redis://localhost:6380";
2020-01-02 16:58:06 +00:00
const oldredis = redis.createClient(oldurl, { return_buffers: true });
2020-03-29 20:42:30 +00:00
const newurl = "redis://localhost:6379";
2020-01-02 16:58:06 +00:00
const newredis = redis.createClient(newurl, { return_buffers: true });
2020-03-29 20:42:30 +00:00
const CANVAS_SIZE = 1024;
const OUR_TILE_SIZE = THREE_TILE_SIZE;
const CHUNKS_XY = CANVAS_SIZE / OUR_TILE_SIZE;
2020-01-02 16:58:06 +00:00
async function copyChunks() {
for (let x = 0; x < CHUNKS_XY; x++) {
for (let y = 0; y < CHUNKS_XY; y++) {
2020-03-29 20:42:30 +00:00
const oldkey = `ch:2:${x}:${y}`;
const newkey = `ch:2:${x}:${y}`;
2020-01-02 16:58:06 +00:00
const chunk = await oldredis.getAsync(oldkey);
if (chunk) {
const setNXArgs = [newkey, chunk];
2020-03-29 20:42:30 +00:00
await newredis.sendCommandAsync('SET', setNXArgs);
console.log("Created Chunk ", newkey);
2020-01-02 16:58:06 +00:00
}
}
}
}
2020-03-29 21:16:44 +00:00
function chunkOfCord(cor) {
return Math.floor((cor + CANVAS_SIZE / 2) / OUR_TILE_SIZE);
}
async function copyChunksByCoords(xMin, xMax, yMin, yMax) {
const chunkXMin = chunkOfCord(xMin);
const chunkXMax = chunkOfCord(xMax);
const chunkYMin = chunkOfCord(yMin);
const chunkYMax = chunkOfCord(yMax);
for (let x = chunkXMin; x <= chunkXMax; x++) {
2020-04-11 21:37:24 +00:00
for (let y = chunkYMin; y <= chunkYMax; y++) {
2020-03-29 21:16:44 +00:00
const oldkey = `ch:2:${x}:${y}`;
const newkey = `ch:2:${x}:${y}`;
const chunk = await oldredis.getAsync(oldkey);
if (chunk) {
const setNXArgs = [newkey, chunk];
await newredis.sendCommandAsync('SET', setNXArgs);
console.log("Created Chunk ", newkey);
}
}
}
}
copyChunksByCoords(-160, 60, -60, 160);