Design data structures and algorithms for in-memory file system - GeeksforGeeks

20 Jun, 2017
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
// Entry is superclass for both File and Directorypublic 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://www.careercup.com/question?id=13618661https://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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Amazon
Design Pattern
Amazon
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
SDE SHEET - A Complete Guide for SDE Preparation
Factory method design pattern in Java
Unified Modeling Language (UML) | An Introduction
Introduction of Programming Paradigms
MVC Design Pattern
Builder Design Pattern
Composite Design Pattern
Unified Modeling Language (UML) | State Diagrams
Unified Modeling Language (UML) | Activity Diagrams
Abstract Factory Pattern