Skip to content

SharedFileView is a node add-on that leverages shared memory segments to provide fast access to large amounts of data. By loading a text file into a read-only shared memory segment, multiple processes can access it simultaneously without expensive disk reads or memory duplication.

License

Notifications You must be signed in to change notification settings

monteiz/shared-file-view

Repository files navigation

Shared File View

test passing NPM version

SharedFileView is a Node.js add-on that loads a file into a shared memory segment, allowing multiple processes to access the same memory data quickly and efficiently, as a JavaScript array. The returned array is read-only.

SharedFileView is designed for speed and minimal memory usage. By using shared memory, SharedFileView eliminates the need to copy data between processes, resulting in blazing fast performance. Additionally, SharedFileView uses the minimum memory allocation possible to load the text file into memory, ensuring that your system resources are used efficiently.

Requirements

It is necessary to have the Boost libraries version 1.81 or higher installed on the system. Failure to have the required libraries installed will result in installation failure.

To install the Boost libraries, please follow the instructions outlined in this guide. Ensure that the environment variables BOOST_ROOT and BOOST_LIBRARYDIR are correctly configured for the installation to proceed smoothly.

Installation

npm install shared-file-view

Usage

Creating a SharedFileView

First create a SharedFileView for an existing file, using the SharedFileView.Create static method. This method is asynchronous, so you can pass a callback function as the second argument to be called when the SharedFileView is actually created.

const { SharedFileView } = require("shared-file-view");

SharedFileView.Create("/path/to/file.txt", (err) => {
	if (err) {
		console.error(err);
	} else {
		console.log("SharedFileView created");
	}
});

Reading from an existing SharedFileView

To retrieve an array of lines from a file, use the SharedFileView.ArrayFrom constructor. This method returns a JavaScript array that you can use to access any line of the file.

const { SharedFileView } = require("shared-file-view");

const filePath = "/path/to/file.txt";
const sharedFileView = new SharedFileView.ArrayFrom(filePath);

console.log(sharedFileView[0]); // Prints the first line of the file
console.log(sharedFileView[1]); // Prints the second line of the file

Checking if a SharedFileView Exists

To check if a SharedFileView for a file has already been created, use the SharedFileView.Exists static method. This method returns a boolean indicating whether the SharedFileView exists.

const { SharedFileView } = require("shared-file-view");

const filePath = "/path/to/file.txt";
const exists = SharedFileView.Exists(filePath);

console.log(exists); // Prints true if a SharedFileView exists, false otherwise

Removing a SharedFileView

To remove a SharedFileView from memory and free up system resources, use the SharedFileView.Remove static method.

const { SharedFileView } = require("shared-file-view");

const filePath = "/path/to/file.txt";
SharedFileView.Remove(filePath, (err) => {
	if (err) {
		console.error(err);
	} else {
		console.log("SharedFileView removed");
	}
});

License

SharedFileView is licensed under the MIT License. See the LICENSE file for details.

Credits

I would like to thank the following individuals for their contributions to SharedFileView:

  • Seth Heeren (@sehe), for providing the original approach that underlie SharedFileView.
  • Allen Luce (@allenluce), for creating the mmap-object package that inspired SharedFileView.

About

SharedFileView is a node add-on that leverages shared memory segments to provide fast access to large amounts of data. By loading a text file into a read-only shared memory segment, multiple processes can access it simultaneously without expensive disk reads or memory duplication.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published