Skip to content

Commit

Permalink
update: logic of loading data
Browse files Browse the repository at this point in the history
  • Loading branch information
alikia2x committed Jul 11, 2024
1 parent 815544e commit e548326
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
32 changes: 19 additions & 13 deletions src/lib/server/database/loadData.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
import fs from 'fs';
import { globalMemoryStorage, songData, songNameCache } from '$lib/server/cache.js';
import { getDirectoryHash } from '../dirHash';

const dataPath = './data/song/';

export async function loadData() {
const LastLoaded: number | undefined = globalMemoryStorage.get("lastLoadData");
const LastLoaded: number | undefined = globalMemoryStorage.get('lastLoadData');
const LastHash: string | undefined = globalMemoryStorage.get('lastDataDirHash');
const currentTime = new Date().getTime();
const currentHash = getDirectoryHash(dataPath);
// Already loaded.
if (LastLoaded && currentTime - LastLoaded < 3600) {
if (LastLoaded && LastHash && currentTime - LastLoaded < 120 * 1000 && currentHash === LastHash) {
return;
}

const dataPath = "./data/song/";
const songList = fs.readdirSync(dataPath)
.map(fileName => {
if (fileName.endsWith(".json"))
return fileName.slice(0, fileName.length - 5);
const songList = fs
.readdirSync(dataPath)
.map((fileName) => {
if (fileName.endsWith('.json')) return fileName.slice(0, fileName.length - 5);
else return null;
})
.filter(fileName => fileName !== null);
.filter((fileName) => fileName !== null);
songData.flushAll();
songNameCache.flushAll();
for (const songID of songList) {
try {
const fileContentString = fs.readFileSync(dataPath + songID + ".json").toString();
const fileContentString = fs.readFileSync(dataPath + songID + '.json').toString();
const data = JSON.parse(fileContentString);
songData.set(songID, data);
const metadata: MusicMetadata = data;
songNameCache.set(metadata.name, metadata);
}
catch {
} catch {
console.error(`[load-song-data] Could not load song ID ${songID}`);
}
}
globalMemoryStorage.set("lastLoadData", new Date().getTime());
}
globalMemoryStorage.set('lastLoadData', new Date().getTime());
globalMemoryStorage.set('lastDataDirHash', getDirectoryHash(dataPath));
}
36 changes: 36 additions & 0 deletions src/lib/server/dirHash.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';

// Function to get hash for a given directory
export function getDirectoryHash(dir: string): string {
const fileDetails: string[] = [];

// Recursive function to traverse the directory
function traverseDirectory(currentDir: string): void {
const files = fs.readdirSync(currentDir);

files.forEach(file => {
const filePath = path.join(currentDir, file);
const stats = fs.statSync(filePath);

if (stats.isDirectory()) {
traverseDirectory(filePath);
} else {
// Collect file path and last modification time
fileDetails.push(`${filePath}:${stats.mtimeMs}`);
}
});
}

traverseDirectory(dir);

// Sort file details to ensure consistent hash
fileDetails.sort();

// Create hash from file details
const hash = crypto.createHash('sha256');
hash.update(fileDetails.join('|'));

return hash.digest('hex');
}

0 comments on commit e548326

Please sign in to comment.