[CareerCup] 8.9 An In-memory File System 内存文件系统
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 内存文件系统的更多相关文章
- NFS - Network File System网络文件系统
NFS(Network File System/网络文件系统): 设置Linux系统之间的文件共享(Linux与Windows中间文件共享采用SAMBA服务): NFS只是一种文件系统,本身没有传输功 ...
- Blazor组件自做十一 : File System Access 文件系统访问 组件
Blazor File System Access 文件系统访问 组件 Web 应用程序与用户本地设备上的文件进行交互 File System Access API(以前称为 Native File ...
- 【Azure 存储服务】如何把开启NFS 3.0协议的Azure Blob挂载在Linux VM中呢?(NFS: Network File System 网络文件系统)
问题描述 如何把开启NFS协议的Azure Blob挂载到Linux虚拟机中呢? [答案]:可以使用 NFS 3.0 协议从基于 Linux 的 Azure 虚拟机 (VM) 或在本地运行的 Linu ...
- nfs 是Network File System 网络文件系统
NFS的基本原刚是容许不同的客户端及服务通过一组PRC分享相同的文件系统,它是独立于操作系统,容许不同硬件及操作系统的系统共同进行文件的分享.NFS在文件传送过程中依赖于RPC协议.远程过程调用Rem ...
- node.js—File System(文件系统模块)
文件系统模块概述 该模块是核心模块,提供了操作文件的一些API,需要使用require导入后使用,通过 require('fs') 使用该模块 文件 I/O 是由简单封装的标准 POSIX 函数提供的 ...
- 小白日记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 [ ...
- Unable to chmod /system/build.prop.: Read-only file system
Unable to chmod /system/build.prop.: Read-only file system 只读文件系统 所以需要更改 使用下面的命令 mount -o remount,rw ...
- Linux File System
目录 . Linux文件系统简介 . 通用文件模型 . VFS相关数据结构 . 处理VFS对象 . 标准函数 1. Linux文件系统简介 Linux系统由数以万计的文件组成,其数据存储在硬盘或者其他 ...
- HDFS分布式文件系统(The Hadoop Distributed File System)
The Hadoop Distributed File System (HDFS) is designed to store very large data sets reliably, and to ...
随机推荐
- 深入理解java虚拟机(2)------垃圾收集器和内存分配策略
GC可谓是java相较于C++语言,最大的不同点之一. 1.GC回收什么? 上一篇讲了内存的分布. 其中程序计数器栈,虚拟机栈,本地方法栈 3个区域随着线程而生,随着线程而死.这些栈的内存,可以理解为 ...
- 使用OLE DB读写Excel
说明: 使用这种技术的好处是无需引用对象,坏处是无法处理类似合并单元格这样的复杂情况 一些更新: 为了使用Office 2010,需要安装Microsoft Access 2010 数据库引擎可再发行 ...
- Jmeter之Http Cookie Manager
一.Http Cookie Manager的作用: 1.自动管理cookie:象浏览器一样的存储和发送Cookie,如果发送一个http请求他的响应中包含Cookie,那么Cookie Manager ...
- Cron 表达式详解和案例
1. cron表达式格式: {秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)} 2. cron表达式各占位符解释: {秒数} ==> 允许值范围: 0~59 ,不允许 ...
- PowerDesigner概述(系统分析与建模)以及如何用PowerDesigner快速的创建出这个数据库
PowerDesigner是Sybase公司推出的一个集成了企业架构,UML(统一建模语言)和数据库的CASE(计算机辅助软件工程)工具.它 不仅可以用于系统设计和开发的不同阶段(即业务分析,概念模型 ...
- Backbone模型
现在进入最关键的组件 - 模型.模型用来存储应用的所有数据,以及直接和数据操作相关的逻辑.Backbone中的模型类是Backbone.Model,它包含了数据存储,数据验证,以及数据发生变动时触发相 ...
- 开启mysql慢查询
Linux查看mysql 安装路径一.查看文件安装路径由于软件安装的地方不止一个地方,所有先说查看文件安装的所有路径(地址).这里以mysql为例.比如说我安装了mysql,但是不知道文件都安装在哪些 ...
- 用Qemu模拟vexpress-a9 (四) --- u-boot引导kernel,用nfs挂载根文件系统
环境介绍 Win7 64 + Vmware 11 + ubuntu14.04 32 u-boot 版本:u-boot-2015-04 Linux kernel版本:linux-3.16.y busyb ...
- 移动web在ios和android下点击元素出现阴影问题
移动web开发经验总结 1.-webkit-tap-highlight-color:rgba(255,255,255,0)可以同时屏蔽ios和android下点击元素时出现的阴影.备注:transp ...
- dipole antenna simulation by HFSS
工作频点为1GHz,新建工程,添加新设计,编辑添加下面的变量 建立天线模型,即两个金属圆柱.编辑完一个振子后,另一半可以用镜像命令产生参数如下设置 ,材料为PEC 两个圆柱间建立一个矩形片,连接两个圆 ...