-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature(RDB): add support to RDB, Generate RDB by SAVE command is rea…
…dy, refactor in the methods of the SharedStorage, add Thread workers for RDB lifeCycle
- Loading branch information
Showing
23 changed files
with
258 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/ | ||
.idea/ | ||
.idea/ | ||
dumps/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import ICommand from "../../interfaces/command.interface"; | ||
import SharedStorage from "../../storage/SharedStorage"; | ||
import {subject} from "../../workers/worker.observer"; | ||
|
||
const SAVE = (): ICommand => { | ||
subject.send('RDB.SAVE', SharedStorage.copyAll()); | ||
|
||
return <ICommand>{ | ||
type: 'string', | ||
data: 'OK' | ||
} | ||
}; | ||
|
||
export default SAVE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './worker.config'; | ||
export * from './args.config'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import SharedEnv from './storage/SharedEnv'; | ||
import SharedEnv from '../storage/SharedEnv'; | ||
|
||
( | ||
() => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import {WorkerObserver, subject} from "../workers/worker.observer"; | ||
|
||
const rdbUrl = new URL('../workers/rdb/rdb.worker.ts', import.meta.url).pathname; | ||
const RDBWorker = new WorkerObserver('RDBWorker', rdbUrl); | ||
subject.attach(RDBWorker); | ||
|
||
export {RDBWorker} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import generateRDB from "./utils/generateRDB"; | ||
declare var self: Worker; | ||
|
||
self.onmessage = (event: MessageEvent) => { | ||
const message: string = event.data.signal; | ||
|
||
switch (message) { | ||
case 'RDB.SAVE': | ||
const result = generateRDB(event.data.content); | ||
postMessage(result); | ||
break; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import unixTimeEncoder from "./unixTimeEncoder"; | ||
import stringEncoder from "./stringEncoder"; | ||
import fs from "fs"; | ||
|
||
enum OP_CODES { | ||
REDIS_WITH_MAGIC_NUMBER = 'REDIS0007', | ||
REDIS_VERSION = '6.0.16', | ||
METADATA = 'FA', | ||
DATA_SECTION = 'FE', | ||
HASH_INFORMATION_SECTION = 'FB', | ||
END_OF_FILE_SECTION = 'FF', | ||
} | ||
|
||
enum DATA_TYPES { | ||
STRING = '00', | ||
} | ||
|
||
const generateRDB = (data: Map<string, any>): string => { | ||
const folderPath = process.cwd() + '/dumps'; | ||
const fileName = 'dump.rdb'; | ||
|
||
if (!fs.existsSync(folderPath)) fs.mkdirSync(folderPath); | ||
|
||
// this will generate the header | ||
const header = Buffer.from(OP_CODES.REDIS_WITH_MAGIC_NUMBER).toString('hex'); | ||
|
||
// this will generate the metadata section | ||
const metadata = | ||
OP_CODES.METADATA + | ||
Buffer.from('redis-ver').toString('hex') + | ||
Buffer.from(OP_CODES.REDIS_VERSION).toString('hex'); | ||
|
||
// this will generate the data section | ||
let dataSection = OP_CODES.DATA_SECTION + '00' + OP_CODES.HASH_INFORMATION_SECTION | ||
|
||
// get the encoding size | ||
dataSection += (data.size + '').padStart(2, '0'); | ||
dataSection += Array.from(data.values()) | ||
.filter((item: any) => item.expiredAt !== undefined) | ||
.length.toString(16).padStart(2, '0'); | ||
|
||
// for each key-value pair, generate the data section | ||
data.forEach((value, key) => { | ||
const type = value.type; | ||
const expiredAt = value.expiredAt; | ||
const data = value.value; | ||
|
||
let result: string | null = null; | ||
if (type === 'string') result = DATA_TYPES.STRING; | ||
else return; | ||
|
||
result += stringEncoder(key); | ||
result += stringEncoder(data); | ||
|
||
if (expiredAt) result += 'FC' + unixTimeEncoder(expiredAt); | ||
dataSection += result; | ||
}); | ||
|
||
// this will generate the end of the file section | ||
dataSection += OP_CODES.END_OF_FILE_SECTION; | ||
|
||
// Temporary CRC64 checksum | ||
const CRC64CheckSum = '0000000000000000'; | ||
dataSection += CRC64CheckSum; | ||
|
||
const buffer = Buffer.from(header + metadata + dataSection, 'hex'); | ||
fs.writeFileSync(folderPath + '/' + fileName, buffer); | ||
|
||
return `RDB file generated ${fileName}`; | ||
}; | ||
|
||
export default generateRDB; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* Encodes a string to a hex string with a length prefix. | ||
* @param str | ||
* @returns string | ||
* @example | ||
* stringEncoder('foobar'); | ||
* // => '06 66 6F 6F 62 61 72' | ||
*/ | ||
const stringEncoder = (str: string): string => { | ||
const length = (str.length + '').padStart(2, '0'); | ||
const encoded = Buffer.from(str).toString('hex'); | ||
|
||
return length + encoded; | ||
} | ||
|
||
export default stringEncoder; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* Encodes a unix time into a 8-byte unsigned long, in little-endian. | ||
* @param time | ||
* @returns string | ||
* @example | ||
* unixTimeEncoder(1713824559637); | ||
* // => '15 72 E7 07 8F 01 00 00' | ||
*/ | ||
const unixTimeEncoder = (time: number): string => { | ||
return time.toString(16).padStart(16, '0'); | ||
}; | ||
|
||
export default unixTimeEncoder; |
Oops, something went wrong.