MVCC in Database

2016 Dec 23

Multiversion concurrency control(MVCC)是数据库实现并发访问的一种方法。 在MVCC下一个row会有多个版本。 比如对一个row做UPDATE操作时,不会修改原始的row,而是作用在这个row的一个新的拷贝上。 MVCC在实现并发时不需要引入锁。 读、写操作不会相互阻塞。

很多数据库都实现了MVCC,比如PostgreSQL和Oracle。

更多参考

Multiversion concurrency control

PostgreSQL Concurrency with MVCC

Mvcc Unmasked - Bruce Momjian

Bit Twiddling Hacks

2016 Dec 20

Java的Integer类有个highestOneBit方法,可以返回一个整数的二进制表示的最左边的1的位置。

public static int highestOneBit(int i) {
	// HD, Figure 3-1
	i |= (i >>  1);
	i |= (i >>  2);
	i |= (i >>  4);
	i |= (i >>  8);
	i |= (i >> 16);
	return i - (i >>> 1);
}

那么上面的实现到底是在干什么?!😨

Integer类的Javadoc上说highestOneBit的实现参考自书籍Hacker’s Delight

Implementation note: The implementations of the “bit twiddling” methods (such as highestOneBit and numberOfTrailingZeros) are based on material from Henry S. Warren, Jr.’s Hacker’s Delight, (Addison Wesley, 2002).

或许面试前可以看看这边书,让面试官惊讶一下。

另外,google一下“Bit Twiddling Hacks”,也可以发现很多参考资料。比如这篇pdf,以及这个网站

Java支持的位操作运算有:~&^|<<>>>>>

The unsigned right shift operator “»>” shifts a zero into the leftmost position, while the leftmost position after “»” depends on sign extension.

>>>会在最左边补0>>运算后,最左边是0还是1取决于整数的正负号。

来自Thoughtworks的技术雷达

2016 Dec 3

Thoughtworks对一些技术和工具(包括新的和老的)的评价和看法。 会推荐一些新的技术和工具,也会评估已存在的工具和技术是不是还能继续适用。 Technology Radar会定期更新,一般一年有两到三期。

没事可以看一看,起码可以了解一堆名词

SAM type和Lambda

2016 Nov 23

这是一份Java 8 Lambda的早期设计文档。 文档里Lambda的语法和最终的实现有一些细微的区别,但是不影响阅读。 最终的Lambda设计文档见这里

Lambda也叫“闭包(closure)”。 在Java 8之前,anonymous inner class可以认为是一种闭包——它可以capture enclosing class里定义的变量,封装一段或者几段代码以便稍后执行。 anonymous inner classes的缺点是语法太啰嗦、this不是lexically scoped、不能capture非final变量等。 Java 8引入Lambda是为了解决这些问题。

Java 8在引入Lambda时,并没有引入新的function type;而是借助SAM type来实现Lambda。 SAM(single abstract method) type是指只有一个抽象方法的接口。

Lambda expressions can only appear in context where it will be converted to a variable of SAM type.

Lambda表达式都会转化为一个SAM类型的变量。 凡是能接收SAM类型的地方,比如方法的参数,都可以接收一个Lambda表达式。

Comparator<String> c = (String s1, String s2) -> s1.compareToIgnoreCase(s2);

Java 8的Lambda表达式和anonymous inner class的一个不同之处是它们对this的解读。 Lambda表达式中的this指的是enclosing class中的this。 anonymous inner class中的this指的是inner class自己的this

Hash Join

2016 Oct 13

Hash join是数据库实现join操作的一种算法。

  • First prepare a hash table of the smaller relation. The hash table entries consist of the join attribute and its row.
  • Once the hash table is built, scan the larger relation and find the relevant rows from the smaller relation by looking in the hash table.

3种基本的join实现算法:

Three fundamental algorithms for performing a join operation exist: nested loop join, sort-merge join and hash join.

Nested loop join就是两个简单粗暴的for loop。