跳至主要内容

博文

目前显示的是 十一月, 2016的博文

List Implementation

Today, we implement a sorted linked-list and try to feel how to write beautiful code. Question Implement a sorted linked-list with max possible value known and following declaration: Class SortedList { public SortedList ( int maxValue) { //... } public void insert ( int i) { } } Simple Version What comes to my mind, at the beginning, is some code like the following: public SortedList0 ( int max) { } public void insert ( int i) { if (head == null ) { head = new ListNode(i, null ); } else { ListNode prev = null , next = head; while (next != null && next.getVal() < i) { prev = next; next = next.getNext(); } if (prev == null ) { head = new ListNode(i, head); } else { prev.setNext( new ListNode(i, next)); } } } This version works, but we can see there are some special cases need to handle which make o

LevelDB Source Reading (4): Concurrent Access

In this thread, we come to the issue of concurrent access of LevelDB. As a database, it can be concurrently accessed by users. But, it wouldn’t be easy to provide high throughput under product load. What effort does LevelDB make to achieve this goal both in design and implementation? Goal of Design From this github issue , we can see LevelDB is designed for not allowing multi-process access. this (supporting multiple processes) doesn’t seem like a good feature for LevelDB to implement. They believe let multiple process running would be impossible to share memory/buffer/cache, which may affect the performance of LevelDB. In the case of multiple read-only readers without altering the code base, you could simply copy the file for each reader. Yes, it will be inefficient (though not on file systems that dedupe data), but then again, so would having multiple leveldb processes running as they wouldn’t be able to share their memory/buffer/etc. They achieve it by adding a l

Java Nested Class Implementation

Before we started, we have to review the definition of nested class: Nested classes are divided into two categories: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. Features Nested class is very common in Java code, and provides some benefits as oracle document says: It is a way of logically grouping classes that are only used in one place; It increases encapsulation; It can lead to more readable and maintainable code. And here, we are going to focus on the features and convenience that nested class bring to us. The following listed items are some of them: They can access each other’s private members directly Inner class can access outer class’s field directly Secret of Access Private Member Now, we move to a simple example of static nested class to see how Java make it. As the comment in the code pointed out, nested class and outer class reference