Monday, April 15, 2019

linux find - OOD, Amazon



请教一道亚麻最近高频的OOD linux find题目
inux的find命令其实还是相当复杂的一个命令,参数种类很多,官方文档在这里:https://www.gnu.org/software/fin ... _mono/find.html#Top

文档里在序章的1.3节介绍了find命令的四大类参数:
options:affect overall operation rather than the processing of a specific file; 
tests:return a true or false value, depending on the file's attributes; 
actions:have side effects and return a true or false value; and 
operators:connect the other arguments and affect when and whether they are evaluated.

这个分类方法稍微有点土,毕竟find是很早以前设计的工具了。现在来看的话find命令本质上是一个针对文件系统的查询引擎(Query Engine)。Options相当于全局的configuration,tests(各种过滤器)和 operations(“与或非”逻辑联结词)可以合称为谓词(predicate,类似于SQL中的where语句),而actions则是一些side effect操作
总体设计上:
(1) 首先要设计对所存储数据的access path,也就你怎么访问/遍历这些数据 —— 对应文件系统就是简单粗暴的walk file tree
(2) 然后需要将各种参数转化为一定的中间表示(IR,intermediate representation),比如说上面提到的option,predicate和action都可以是一个抽象类,然后针对每个参数继承其中一个并实现相应功能
(3) 得到了中间表示,还可以设计优化器对其进行优化。比如说Linux find是可以启动优化功能的(-O1或者-O2或者-O3),会reorder过滤条件的执行顺序。举个简单例子,考虑过滤条件“找到文本中包含Hello world并且后缀为.cpp的文件”,那么我们肯定希望先执行后缀名.cpp的这个过滤,然后在满足后缀的文件中再查找Hello wolrd关键词
(4) 最后需要生成一个执行器,根据优化后的中间表示(一般称为执行计划Execution Plan),walk file tree并且执行相应的filter和action。

关于OOP的部分,首先问一个最根本的问题:我们为什么需要OOP,或者说OOP可以做到什么?

其实OOP好处还是挺多的.. 但最重要的用处之一就是系统的可扩展性(extensibility)。比如说现在系统支持了若干种过滤器,那么当我们想要添加一个新的过滤器的时候,是否需要修改很多处已有的代码?还是只需要在某个地方独立地实现一下该过滤器的逻辑,然后将其注册到过滤器列表(registry)中? —— 我们肯定希望的是后者,并可以通过OOP的继承与动态绑定机制实现。
还有一点题外话.. OOP不是表达抽象、实现多态的唯一/最好的机制,具体要看应用场景。函数式以及C++模板元编程(静多态)都有其非常适用的场景。

下面是一份Java写的参考代码:



Labels

Review (572) System Design (334) System Design - Review (198) Java (189) Coding (75) Interview-System Design (65) Interview (63) Book Notes (59) Coding - Review (59) to-do (45) Linux (43) Knowledge (39) Interview-Java (35) Knowledge - Review (32) Database (31) Design Patterns (31) Big Data (29) Product Architecture (28) MultiThread (27) Soft Skills (27) Concurrency (26) Cracking Code Interview (26) Miscs (25) Distributed (24) OOD Design (24) Google (23) Career (22) Interview - Review (21) Java - Code (21) Operating System (21) Interview Q&A (20) System Design - Practice (20) Tips (19) Algorithm (17) Company - Facebook (17) Security (17) How to Ace Interview (16) Brain Teaser (14) Linux - Shell (14) Redis (14) Testing (14) Tools (14) Code Quality (13) Search (13) Spark (13) Spring (13) Company - LinkedIn (12) How to (12) Interview-Database (12) Interview-Operating System (12) Solr (12) Architecture Principles (11) Resource (10) Amazon (9) Cache (9) Git (9) Interview - MultiThread (9) Scalability (9) Trouble Shooting (9) Web Dev (9) Architecture Model (8) Better Programmer (8) Cassandra (8) Company - Uber (8) Java67 (8) Math (8) OO Design principles (8) SOLID (8) Design (7) Interview Corner (7) JVM (7) Java Basics (7) Kafka (7) Mac (7) Machine Learning (7) NoSQL (7) C++ (6) Chrome (6) File System (6) Highscalability (6) How to Better (6) Network (6) Restful (6) CareerCup (5) Code Review (5) Hash (5) How to Interview (5) JDK Source Code (5) JavaScript (5) Leetcode (5) Must Known (5) Python (5)

Popular Posts