Friday, May 17, 2024
 Popular · Latest · Hot · Upcoming
181
rated 0 times [  182] [ 1]  / answers: 1 / hits: 31436  / 10 Years ago, fri, may 23, 2014, 12:00:00

I'm doing some file manipulation work with node.js and many of the packages that I use require a path to be sent so that they can open the file, do some work, and so on.



But I'm parsing millions of files and rather than actually store them on disk, I'd like to store them in memory. The contents of the files are all in my database and I'd hate to write them to disk, just to do my insanely awesome work on them.



So is such a thing possible?


More From » node.js

 Answers
12

If you are using a Linux system then you can merely create your own tmpfs (RAM) via the following nodejs code. This creates a tmpfs at /mnt/myramdisk as per the node code. The directory /mnt/myramdisk must be manually created beforehand obviously via mkdir /mnt/myramdisk.



var MountPoint='/mnt/myramdisk';
var TextFile='/MyTextFileInRAM.txt';
var RAM_Size='512m';

const fs = require('fs');

const { exec } = require('child_process');
exec(awk '{print $2}' /proc/mounts | grep +MountPoint, (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
//console.log(err);
console.log(MountPoint+' is Not Mounted yet. Im mounting it now:n');
NotMountedYetSoMountIt();
return;
}
// the *entire* stdout and stderr (buffered)
if(stdout)
{
console.log(MountPoint+' is Already Mounted');
TextToWriteToFileOnTMPFS();
}
});

function NotMountedYetSoMountIt()
{
const { exec } = require('child_process');
exec('df -h && echo && mount -t tmpfs -o size='+RAM_Size+' tmpfs '+MountPoint+' && echo && df -h', (err, stdout, stderr) => {
if (err) {
// node couldn't execute the command
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
TextToWriteToFileOnTMPFS();
console.log(`stderr: ${stderr}`);
});
}

function TextToWriteToFileOnTMPFS()
{

let TextToWrite = 'Hellon' +
'world @'+CurrentTime();

fs.writeFile(MountPoint+TextFile, TextToWrite, (err) => {
// throws an error, you could also catch it here
if (err) throw err;
// success case, the file was saved
console.log('saved!');
});
}

function addZero(i) {
if (i < 10) {
i = 0 + i;
}
return i;
}
function CurrentTime()
{
var d = new Date();
var h = addZero(d.getHours());
var m = addZero(d.getMinutes());
var s = addZero(d.getSeconds());
return h + : + m + : + s;
}


Output:



root@box:/daemons#node tmpfs_test.js && cat /mnt/myramdisk/MyTextFileInRAM.txt
/mnt/myramdisk is Not Mounted yet. Im mounting it now:

stdout: Filesystem Size Used Avail Use% Mounted on
udev 7.8G 0 7.8G 0% /dev
tmpfs 1.6G 1.4M 1.6G 1% /run
/dev/sda2 938G 436G 454G 49% /
tmpfs 7.8G 449M 7.4G 6% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/loop1 90M 90M 0 100% /snap/core/8213
/dev/sda1 511M 6.1M 505M 2% /boot/efi
/dev/loop2 90M 90M 0 100% /snap/core/8268


Filesystem Size Used Avail Use% Mounted on
udev 7.8G 0 7.8G 0% /dev
tmpfs 1.6G 1.4M 1.6G 1% /run
/dev/sda2 938G 436G 454G 49% /
tmpfs 7.8G 449M 7.4G 6% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 7.8G 0 7.8G 0% /sys/fs/cgroup
/dev/loop1 90M 90M 0 100% /snap/core/8213
/dev/sda1 511M 6.1M 505M 2% /boot/efi
/dev/loop2 90M 90M 0 100% /snap/core/8268
tmpfs 512M 0 512M 0% /mnt/myramdisk

stderr:
saved!
Hello
world @23:09:15



root@box:/daemons# node tmpfs_test.js && cat /mnt/myramdisk/MyTextFileInRAM.txt
/mnt/myramdisk is Already Mounted
saved!
Hello
world @23:09:19

[#70876] Thursday, May 22, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
latrelllloydb

Total Points: 449
Total Questions: 92
Total Answers: 100

Location: French Polynesia
Member since Tue, Jul 7, 2020
4 Years ago
;