pixelplanet/utils/redisCopy.js
HF e95b6ae8d3 Set pixels in redis in batches
finish node-redis update by making sure that everything that needs redis
runs after redis connected
2022-04-07 07:54:36 +02:00

67 lines
1.7 KiB
JavaScript

/* @flow */
//this script just copies chunks from one redis to another
import redis from 'redis';
import {
TILE_SIZE,
THREE_TILE_SIZE,
} from '../src/core/constants';
//ATTENTION Make suer to set the rdis URLs right!!!
const oldurl = "redis://localhost:6380";
const oldredis = redis.createClient({ url: oldurl });
const newurl = "redis://localhost:6379";
const newredis = redis.createClient({ url: newurl });
oldredis.connect();
newredis.connect();
const CANVAS_SIZE = 1024;
const OUR_TILE_SIZE = THREE_TILE_SIZE;
const CHUNKS_XY = CANVAS_SIZE / OUR_TILE_SIZE;
async function copyChunks() {
for (let x = 0; x < CHUNKS_XY; x++) {
for (let y = 0; y < CHUNKS_XY; y++) {
const oldkey = `ch:2:${x}:${y}`;
const newkey = `ch:2:${x}:${y}`;
const chunk = await oldredis.get(oldkey);
if (chunk) {
await newredis.set(newkey, chunk);
console.log("Created Chunk ", newkey);
}
}
}
}
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++) {
for (let y = chunkYMin; y <= chunkYMax; y++) {
const oldkey = `ch:2:${x}:${y}`;
const newkey = `ch:2:${x}:${y}`;
const chunk = await oldredis.get(oldkey);
if (chunk) {
await newredis.set(newkey, chunk);
console.log("Created Chunk ", newkey);
} else {
await newredis.del(newkey);
console.log("Deleted Chunk ", newkey);
}
}
}
}
module.exports = copyChunksByCoords;
// copyChunksByCoords(-160, 60, -60, 160);