8.9 Explain the data structures and algorithms that you would use to design an in-memory file system. Illustrate with an example in code where possible.

这道题让我们设计一个内存文件系统,咋一听感觉挺吓人啊,像是很底层的东西,但其实只是一道很普通的OOB的题目而已。根据书上所述,在一个简化的文件系统,由文件和目录构成的,而文件类File和目录类Directory都是由一个入口类Entry派生而来的,可参见如下代码:

class Directory;

class Entry {
public:
Entry(string n, Directory *p) {
_name = n;
_parent = p;
_created = getCurrentTimeMillis();
}
static long getCurrentTimeMillis() {
time_t res = time(NULL);
localtime(&res);
return (long)res;
}
bool deleteEntry() {
if (_parent == nullptr) return false;
return _parent->deleteEntry(this);
}
virtual int size() = ;
string getFullPath() {
if (_parent == nullptr) return _name;
else return _parent->getFullPath() + "/" + _name;
}
long getCreationTime() { return _created; }
long getLastUpdatedTime() { return _lastUpdated; }
long getLastAccessedTime() { return _lastAccessed; }
void changeName(string n) { _name = n; }
string getName() { return _name; } protected:
Directory *_parent;
long _created;
long _lastUpdated;
long _lastAccessed;
string _name;
}; class File: public Entry {
public:
File(string n, Directory *p, int sz): Entry(n, p) {
_size = sz;
}
int size() { return _size; }
string getContents() { return _content; }
void setContents(string c) { _content = c; } private:
string _content;
int _size;
}; class Directory: public Entry {
public:
Directory(string n, Directory *p): Entry(n, p) {}
int size() {
int size = ;
for (auto a : _contents) {
size += a->size();
}
return size;
}
int numberOfFiles() {
int cnt = ;
for (auto a : _contents) {
if (Directory *d = dynamic_cast<Directory*>(a)) {
++cnt;
cnt += d->numberOfFiles();
} else if (File *f = dynamic_cast<File*>(a)) {
++cnt;
}
}
return cnt;
}
bool deleteEntry(Entry *entry) {
for (vector<Entry*>::iterator it = _contents.begin(); it != _contents.end(); ++it) {
if (*it == entry) {
_contents.erase(it);
}
}
}
void addEntry(Entry *entry) {
_contents.push_back(entry);
} protected:
vector<Entry*> _contents;
vector<Entry*> getContents() { return _contents; }
};

[CareerCup] 8.9 An In-memory File System 内存文件系统的更多相关文章

  1. NFS - Network File System网络文件系统

    NFS(Network File System/网络文件系统): 设置Linux系统之间的文件共享(Linux与Windows中间文件共享采用SAMBA服务): NFS只是一种文件系统,本身没有传输功 ...

  2. Blazor组件自做十一 : File System Access 文件系统访问 组件

    Blazor File System Access 文件系统访问 组件 Web 应用程序与用户本地设备上的文件进行交互 File System Access API(以前称为 Native File ...

  3. 【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)

    问题描述 如何把开启NFS协议的Azure Blob挂载到Linux虚拟机中呢? [答案]:可以使用 NFS 3.0 协议从基于 Linux 的 Azure 虚拟机 (VM) 或在本地运行的 Linu ...

  4. nfs 是Network File System 网络文件系统

    NFS的基本原刚是容许不同的客户端及服务通过一组PRC分享相同的文件系统,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享.NFS在文件传送过程中依赖于RPC协议.远程过程调用Rem ...

  5. node.js—File System(文件系统模块)

    文件系统模块概述 该模块是核心模块,提供了操作文件的一些API,需要使用require导入后使用,通过 require('fs') 使用该模块 文件 I/O 是由简单封装的标准 POSIX 函数提供的 ...

  6. 小白日记46:kali渗透测试之Web渗透-SqlMap自动注入(四)-sqlmap参数详解- Enumeration,Brute force,UDF injection,File system,OS,Windows Registry,General,Miscellaneous

    sqlmap自动注入 Enumeration[数据枚举] --privileges -U username[CU 当前账号] -D dvwa -T users -C user --columns  [ ...

  7. Unable to chmod /system/build.prop.: Read-only file system

    Unable to chmod /system/build.prop.: Read-only file system 只读文件系统 所以需要更改 使用下面的命令 mount -o remount,rw ...

  8. Linux File System

    目录 . Linux文件系统简介 . 通用文件模型 . VFS相关数据结构 . 处理VFS对象 . 标准函数 1. Linux文件系统简介 Linux系统由数以万计的文件组成,其数据存储在硬盘或者其他 ...

  9. HDFS分布式文件系统(The Hadoop Distributed File System)

    The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to ...

随机推荐

  1. Linux学习书目

    Linux基础 1.<Linux与Unix Shell 编程指南> C语言基础 1.<C Primer Plus,5th Edition>[美]Stephen Prata著 2 ...

  2. python sorted用法

    python列表排序 python字典排序 sorted List的元素可以是各种东西,字符串,字典,自己定义的类等. sorted函数用法如下: sorted(data, cmp=None, key ...

  3. leveldb源码分析--Iterator遍历数据库

    在DBImpl中有一个函数声明为Iterator* DBImpl::NewIterator(const ReadOptions& options) ,他返回一个可以遍历或者搜索数据库的迭代器句 ...

  4. cocos2d-x之Vector与map

    bool HelloWorld::init() { if ( !Layer::init() ) { return false; } Size visibleSize = Director::getIn ...

  5. HTML5客户端数据存储

    HTML5 使在不影响网站性能的情况下存储大量数据成为可能.之前,这些都是由 cookie 完成的,cookie不适合大量数据的存储,因为会影响速度. 举个例子: var obj = {x:1}; / ...

  6. Apache Drill Install and Test

    Drill doc, https://drill.apache.org/docs/hive-storage-plugin/ 发现在国内访问的时候有些标签反应还是很慢,因为它访问了gooleapi的缘故 ...

  7. [转]jquery-confirm

    本文转自:http://craftpip.github.io/jquery-confirm/ Practical Uses These features can practically be used ...

  8. Android+Sqlite 实现古诗阅读应用(一)

    不说网络app,很多本地的app都有一些随机的内容推送,比如随机推送一些小知识,古诗,名言名画什么的,界面制作的好看一点就能看起来特别的文艺范, 最近就是看了这样的一些应用,就想自己实现一下,这种方法 ...

  9. Libfilth(一个滤波器C库)使用

    Libfilth使用说明 winshton 2009年2月 (*本文大部分翻译自libfilth,还有一部分是个人使用实践 *时间水平均有限,翻译的不完整,尤其第二章可以忽略) 版本历史修改记录 版本 ...

  10. noip2008普及组4题题解-rLq

    (啊啊啊终于补到了今天的作业了) 本题地址:http://www.luogu.org/problem/show?pid=1058 题目描述 小渊是个聪明的孩子,他经常会给周围的小朋友们将写自己认为有趣 ...