nodejs eventloop 和libuv

NodeJS and Chrome eventloop Node.js and Chrome do not use the same event loop implementation. Chrome/Chromium uses libevent, while node.js uses libuv. Node’s API provides a kind of asynchronous no-op, setImmediate. For that function, the “some operation” I’ve mention above is “do nothing”, after which an item is immediately added to the end of the…

Java8 中的Optional类详解

在Java8中,引入了一个新的类java.util.Optional 。这个类专门用来解决空引用的问题。有了它,对于一个方法的返回值什么的,都不需要再判断是不是null了。同时,这个类和lambda表达式和函数式编程也可以比较好的整合在一起使用。 这里就来看一下这个类的使用。 知道Scala的同学可以类比一下 scala.Option[T] ,或者Haskell的Data.Maybe。都有类似的功能。 Optional是一个容器,里面放着我们的返回值(就是真正要用的东西),但是这个真正要用的东西可能是null呀,所以每次都要判断,但是Optional不可能是null。所以不要在判断是否为null了。 1 2 3 4 5 6 public void print(String s) { System.out.println(s); }String x = //… Optional<String> opt = //… 上面代码中,x可能是null,opt不可能是null。 建立Optional对象 首先让我们来看一下如何构造Optional对象。Optional对象有一系列静态工厂方法创建出来。 1 2 3 opt = Optional.of(notNull); opt = Optional.ofNullable(mayBeNull); opt = Optional.empty(); 以上3个方法,第一个of()从一个非null的对象创建Optional,如果传给他null,就有异常NullPointerException。 ofNullable()可以由任意对象(包括null)创建Optional。empty方法总是返回一个空的Optional。 ifPresent()方法:真的存在吗? Optional在使用的时候,有一些主要的方法,首先是ifPresent。顾名思义,即使对象是否存在的意思。就是说里面装的东西是不是null,这个和判断对象是否为null基本上也是等价的。 1 2 3 if (x != null) { print(x);…

angularjs $scope form undefined

Was developing some form with angular, things work perfect until I add a angular-ui bootstrap tab to enclose 2 different form in each tab. when i try to access the form with $scope.formName, the formName is undefined. It turns out that the angularUI tab directive uses transclude which will create a child scope to the…

execution time of JavaScript code in page load processde

In that short period of time between you wanting to load a page and your page loading, many relevant and interesting stuff happen that you need to know more about. One example of a relevant and interesting stuff that happens is that any code specified on the page will run. When exactly the code runs…

display formatted json in the page

To Display formatted json we need to use <pre> tag and stringify the js object. Use JSON.Stringify JSON.stringify(value[, replacer [, space]]) Parameters value The value to convert to a JSON string. replacer If a function, transforms values and properties encountered while stringifying; if an array, specifies the set of properties included in objects in the…

PESSIMISTIC_READ and PESSIMISTIC_WRITE

Readers–writer lock A database system is a highly concurrent environment, therefore many concurrency theory idioms apply to database access as well. Concurrent changes must be serialized to preserve data integrity, so most database systems use a two-phase locking strategy, even if it’s usually supplemented by a Multiversion concurrency control mechanism. Because a mutual exclusion locking…

Spring transaction isolation level

Introduction Transaction isolation level is a concept that is not exclusive to the Spring framework. It is applied to transactions in general and is directly related with the ACID transaction properties. Isolation level defines how the changes made to some data repository by one transaction affect other simultaneous concurrent transactions, and also how and when…

angularjs filter

I think it’s best to understand what we even want to learn. To do that, we need to understand what filters really are and how we use them. For me, there are four types of filters. Yes, four, but there of course can be other variants. Let’s rattle through them: Filter 1: Static (single) use…

MongoDB Queries and Projections

Methods, Queries, and Projections In any MongoDB call—often referred to as a “MongoDB query”—we have four parts: Collection Name – The set of documents we’d like to look at. Collection Method – The MongoDB method that we’ll use to look for documents. Query – A document describing what we’re looking for in our collection. Projection…

nosql modeling notes

When designing a MongoDB schema, you need to start with a question that you’d never consider when using SQL: what is the cardinality of the relationship? Put less formally: you need to characterize your “One-to-N” relationship with a bit more nuance: is it “one-to-few”, “one-to-many”, or “one-to-squillions”? Depending on which one it is, you’d use…

Apache Camel notes

Apache Camel 2.15  Spring 4 Example Some other official examples : 1 , 2