Design data structures and algorithms for in-memory file system
Last Updated :
10 Apr, 2025
Explain the data structures and algorithms that you would use to design an in-memory file system. Illustrate with an example in the code logic where possible.
Asked In: Amazon
A file system, in its most simplistic version, consists of Files and Directories. Each Directory contains a set of Files and Directories. Since Files and Directories share so many characteristics, we've implemented them such that they inherit from the same class, Entry.
Implemented Main logic in Java
Java
// Entry is superclass for both File and Directory
public abstract class Entry
{
protected Directory parent;
protected long created;
protected long lastUpdated;
protected long lastAccessed;
protected String name;
public Entry(String n, Directory p)
{
name = n;
parent = p;
created= System.currentTimeMillis();
lastUpdated = System.currentTimeMillis();
lastAccessed = System.currentTimeMillis();
}
public boolean delete()
{
if (parent == null)
return false;
return parent.deleteEntry(this);
}
public abstract int size();
/* Getters and setters. */
public long getcreationTime()
{
return created;
}
public long getLastUpdatedTime()
{
return lastUpdated;
}
public long getLastAccessedTime()
{
return lastAccessed;
}
public void changeName(String n)
{
name = n;
}
public String getName()
{
return name;
}
}
// A class to represent a File (Inherits
// from Entry)
public class File extends Entry
{
private String content;
private int size;
public File(String n, Directory p, int sz)
{
super(n, p);
size = sz;
}
public int size()
{
return size;
}
public String getContents()
{
return content;
}
public void setContents(String c)
{
content = c;
}
}
// A class to represent a Directory (Inherits
// from Entry)
public class Directory extends Entry
{
protected Arraylist<Entry> contents;
public Directory(String n, Directory p)
{
super(n, p);
contents = new Arraylist<Entry>();
}
public int size()
{
int size = 0;
for (Entry e : contents)
size += e.size();
return size;
}
public int numberOfFiles()
{
int count = 0;
for (Entry e : contents)
{
if (e instanceof Directory)
{
count++; // Directory counts as a file
Directory d = (Directory) e;
count += d. numberOfFiles ();
}
else if (e instanceof File)
count++;
}
return count;
}
public boolean deleteEntry(Entry entry)
{
return contents.remove(entry);
}
public void addEntry(Entry entry)
{
contents.add(entry);
}
protected ArrayList<Entry> getContents()
{
return contents;
}
}
Alternatively, we could have implemented Directory such that it contains separate lists for files and subdirectories. This makes the nurnberOfFiles () method a bit cleaner, since it doesn't need to use the instanceof operator, but it does prohibit us from cleanly sorting files and directories by dates or names. For data block allocation, we can use bitmask vector and linear search (see “Practical File System Design”) or B+ trees (see Reference or Wikipedia).
References:
https://stackoverflow.com/questions/14126575/data-structures-used-to-build-file-systems This article is contributed by
Mr. Somesh Awasthi
. If you like GeeksforGeeks and would like to contribute, you can also write an article using
write.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Similar Reads
File and Database Storage Systems in System Design File and database storage systems are important to the effective management and arrangement of data in system design. These systems offer a structure for data organization, retrieval, and storage in applications while guaranteeing data accessibility and integrity. Database systems provide structured
4 min read
Block, Object, and File Storage in System Design Storage is a key part of system design, and understanding the types of storage can help you build efficient systems. Block, object, and file storage are three common methods, each suited for specific use cases. Block storage is like building blocks for structured data, object storage handles large,
6 min read
Blob vs. File System in System Design In system design, choosing the right method for storing and managing data is crucial for performance, scalability, and maintenance. Two common approaches are using Blobs (Binary Large Objects) and traditional file systems. Each has its own set of advantages and challenges, making them suitable for d
7 min read
Storage Concepts in System Design In system design, storage concepts play an important role in ensuring data reliability, accessibility, and scalability. From traditional disk-based systems to modern cloud storage solutions, understanding the fundamentals of storage architecture is crucial for designing efficient and resilient syste
9 min read
Shared Disk Architecture - System Design Shared Disk Architecture is a system design approach where multiple computers access the same storage disk simultaneously. Unlike Shared Nothing Architecture, which partitions data across independent nodes, Shared Disk allows all nodes to read and write to a common storage pool. This architecture is
9 min read
Centralized Storage - System Design Centralized storage plays a critical role in system design by consolidating data in a single location or server, making it easier to manage, secure, and maintain. In this model, all clients and systems access the data over a network, allowing for centralized control and streamlined operations. This
11 min read