Intro to BoltDB: Painless Performant Persistence

BoltDB is a pure Go persistence solution that saves data to a memory mapped file. I call it a persistence solution and not a database, because the word database has a lot of baggage associated with it that doesn’t apply to bolt. And that lack of baggage is what makes bolt so awesome.

Bolt is just a Go package. There’s nothing you need to install on the system, no configuration to figure out before you can start coding, nothing. You just go get github.com/boltdb/bolt and then import “github.com/boltdb/bolt”.

All you need to fully use bolt as storage is a file name. This is fantastic from both a developer’s point of view, and a user’s point of view. I don’t know about you, but I’ve spent months of work time over my career configuring and setting up databases and debugging configuration problems, users and permissions and all the other crap you get from more traditional databases like Postgres and Mongo. There’s none of that with bolt. No users, no setup, just a file name. This is also a boon for users of your application, because they don’t have to futz with all that crap either.

Bolt is not a relational database. It’s not even a document store, though you can sort of use it that way. It’s really just a key/value store… but don’t worry if you don’t really know what that means or how you’d use that for storage. It’s super simple and it’s incredibly flexible. Let’s take a look.

Storage in bolt is divided into buckets. A bucket is simply a named collection of key/value pairs, just like Go’s map. The name of the bucket, the keys, and the values are all of type []byte. Buckets can contain other buckets, also keyed by a []byte name.

Android: Volley+OkHttp with https

Android Network Simple

This article shows how to use volley with OkHttp and security your api with https. 本文可以在简书浏览。

使用 OkHttp 作为传输层的实现.

Volley 默认根据 Android 系统版本使用不同的 Http 传输协议实现. 3.0 以上使用HttpUrlConnection, 2.3 以下使用 ApacheHttpStack, 参考Android Http Client.

OkHttp 相较于其它的实现有以下的优点:

  • 支持SPDY,允许连接同一主机的所有请求分享一个socket。
  • 如果SPDY不可用,会使用连接池减少请求延迟。
  • 使用GZIP压缩下载内容,且压缩操作对用户是透明的。
  • 利用响应缓存来避免重复的网络请求。
  • 当网络出现问题的时候,OKHttp会依然有效,它将从常见的连接问题当中恢复。
  • 如果你的服务端有多个IP地址,当第一个地址连接失败时,OKHttp会尝试连接其他的地址,这对IPV4和IPV6以及寄宿在多个数据中心的服务而言,是非常有必要的。

庆余年: 又一叶

当年的废话

至于是如何和小眉认识的,已经有些记不清楚了,只记得那天太阳当空照,花儿对我笑,小鸟说操操操,喔,不对,那天好象国际上又出了件什么事儿,于是我们几个兄弟以此为由撮了一顿,中午我喝了太多的酒,头有些昏,腿有些沉,舌自然也有些笨了,一个人到街上的书铺借了本小说便直奔大南门外的烂草地,准备刮刮江风,冒充一下孤独;抽抽小烟,模仿一下绝望。

只是有太久没有出门了,所以没想到七月的太阳竟然会这么毒。

当然,这是一个常识,但现在我的问题就是缺乏常识。于是在石凳上坐了五分钟后,我便决定脱下满身汗臭的格子短袖,做个脱衣的俗人,脱俗之人。

当我的手指伸向短袖的第三个扣子时,不期有一道很清凉,很清凉的声音从我的正前方响了起来:

“天气还真是热呀!”

甜甜的、酸酸的、有营养……我自顾自地笑了笑,第一个反应是,这绝对是一个雌性动物才能拥有的声音,而且一定是个小雌。

我的第二个反应是,赶忙把第二个扣子给扣了起来。

我的第三个反应是,脑袋嗡地一声……世界果然大不同,居然有女子主动找我搭讪。

我把头深深地埋在怀里,用了半分钟的时间,猜度了种种可能,唯一把美女相约的可能排除掉后,勇敢地抬起头来,面对这个脸上可能涂了八层防晒霜的恐龙。

杂谈:指纹歌里的人生百态

“一螺穷,二螺富,三螺四螺开当铺……”童年时代,您可曾唱过指纹歌?童谣未必预示了命运,但却包含了人生百态。

指纹,是在每个人出生第六个月就形成,并跟随人一生一世的身体纹线,每个人的指纹都是独一无二的,世界上绝不会有指纹完全相同的两个人。所以,我国在一千多年前的唐代,就实行了指纹画押。古代的借据就是借户用中指和食指在借据上并列印上指纹叫作“花押”。直到现代,立据签约还仍然使用这种当事人在契约上按手印的方法作为凭信。刑事侦探还把鉴别手纹作为破案的重要手段。

两种最基本的指纹:纹如水流中的漩涡形,或中间的纹成封口的环状,叫涡纹,又叫螺纹;指纹呈流线形脱开,不封口,叫流纹,又叫箕纹。

螺是指螺旋状的环形纹,箕是指簸箕状的开口纹。张爱玲在《谈看书》中提到:十只手指上,螺越多越好,聚得住钱;男人簸箕也好,会赚钱,能够把钱铲回家;女人则是螺好,会积钱,手上没螺,拿东西不牢。

指纹歌的地图:盛行于东南沿海

在一些童谣集中载录了大量的指纹歌,如商务印书馆《各省童谣集》第一集(1923年)就收录了三首,分别采自安徽休宁、浙江杭县、浙江新昌;上海世界书局《绘图童谣大观》(1924年)也收录了三首,分别采自江苏的吴县、无锡和江都。

《各省童谣集》的编者说:“看螺纹定一生贫富贵贱,各省都有这种事,但所说各不相同,可见这事不足深信。”这话听起来,好像如果各地所说一致,就可以深信似的。这首曾经被打入“封建迷信”另册的趣味童谣,正因为异文众多,不足为信,才会给我们的童年生活带来许多相互打趣的快乐。

50 Shades of Go

Go is a simple and fun language, but, like any other language, it has a few gotchas… Many of those gotchas are not entirely Go’s fault. Some of these mistakes are natural traps if you are coming from another language. Others are due to faulty assumptions and missing details.

A lot of these gotchas may seem obvious if you took the time to learn the language reading the official spec, wiki, mailing list discussions, many great posts and presentations by Rob Pike, and the source code. Not everybody starts the same way though and that’s OK. If you are new to Go the information here will save you hours debugging your code.

This post covers Go 1.5 and below.

Total Beginner:

  1. Opening Brace Can’t Be Placed on a Separate Line
  2. Unused Variables
  3. Unused Imports
  4. Short Variable Declarations Can Be Used Only Inside Functions
  5. Redeclaring Variables Using Short Variable Declarations
  6. Can’t Use Short Variable Declarations to Set Field Values
  7. Accidental Variable Shadowing
  8. Can’t Use “nil” to Initialize a Variable Without an Explicit Type
  9. Using “nil” Slices and Maps
  10. Map Capacity

Intro to TOML

Overview of Date and Calendar classes

TOML stands for Tom’s Own Minimal Language. It is a configuration language vaguely similar to YAML or property lists, but far, far better. But before we get into it in detail, let’s look back at what came before.

Long Ago, In A Galaxy Far, Far Away

Since the beginning of computing, people have needed a way to configure their software. On Linux, this generally is done in text files. For simple configurations, good old foo = bar works pretty well. One setting per line, name on the left, value on the right, separated by an equals. Great. But when your configuration gets more complicated, this quickly breaks down. What if you need a value that is more than one line? How do you indicate a value should be parsed as a number instead of a string? How do you namespace related configuration values so you don’t need ridiculously long names to prevent collisions?

The Dark Ages

In the 90’s, we used XML. And it sucked. XML is verbose, it’s hard for humans to read and write, and it still doesn’t solve a lot of the problems above (like how to specify the type of a value). In addition, the XML spec is huge, processing is very complicated, and all the extra features invite abuse and overcomplication.

Java: Date And Calendar

Overview of Date and Calendar classes

In this tutorial we look at the classes in java that are helpful in manipulating dates. Manipulating dates in java can be challenging, especially if you are building applications that cater to multiple timezones. Parsing dates, printing a date in UTC, printing a date in a different timezone, comparing dates, adding time to date… we will be looking at all of these in this tutorial. We first begin with the introduction to the classes and then follow up with examples. You may want to directly jump to the examples, however, we strongly recommend that you understand the classes first, since that will make the examples very easy to understand.

  • java.util.Date - This class is used to represent a specific time with a precision of millisecond. If you create a new Date object, it obtains the current system time using System.currentTimeMillis(). This is the number of milliseconds since epoch time (midnight, January 1, 1970 UTC). Here’s the tricky part, date stores time as number of seconds since epoch, it is a UTC time. However, when you print the date, you get the string representation of the date in the local timezone. Also note that the Date class is not designed to manipulate hour, month, year etc or retrieve them. Use this class only to create data or compare two dates. For all other uses, see the Calendar or GregorianCalendar class.

  • java.util.Calendar - Calendar class allows manipulating data using its various fields such as minute, hour, day etc. As in the java.util.Date object, time is represented as number of milliseconds since epoch time. To get an instance of the Calendar use the Calendar.getInstance() method which returns a locale sensitive Calendar instance. Various set and get methods can be used to set and get time values. The current time is represented in two ways - 1. as number of milliseconds since epoch and 2. as local fields such as YEAR, MONTH, DAY, HOUR, MINUTE etc. The conversion between the two types is achieved by using the timezone offset (getOffset()) and the daylight savings fields.
    the MONTH fields starts from 0 (JANUARY).
    The DAY_OF_MONTH starts from 1
    the DAY_OF_WEEK starts from SUNDAY with a value of 1.
    HOUR starts from 0

  • java.util.GregorialCalendar - A GregorianCalendar is the default implementation of the Abstract Calendar class. Next let us look at examples

Java: org.json

Overview of The API Classes

org.json has classes to parse and write json string. It also converts between json and xml, HTTP header, Cookies, and CDF. The main classes are:

  1. org.json.JSONObject - This class stores unordered key value pairs. The value can be Boolean, JSONArray, Number, String or JSONObject.NULL. It has constructors to take in a json string and store it as key value pairs. It also has constructors that take in a Map, a bean or a String
  2. org.json.JSONTokener - This class parses a JSON string and is also used internally by the JSONObject and JSONArray classes to parse JSON Strings
  3. org.json.JSONArray - This class stores an ordered sequence of values. Externally it represents a JSON Array
  4. org.json.JSONWriter - This class represents method to produce json text. It has an append(String) method to append a string to a json text, key(String) and value(String) method to add key and values to json string. It can also write an array.
  5. org.json.CDL- This class has methods to convert comma delimited text to JSONArray and a JSONArray to a comma delimited text. The array contains rows of comma separed strings, with rows separated by newline. The first row contains names.
  6. org.json.Cookie - This class has method to convert a web browser cookie to a JSONObject and back.
  7. org.json.CookieList - This class has method to convert a list of cookies to JSONObject and back. Lets see some examples

Parse JSON

This examples shows how to parse a JSON string. The JSON string in this example is a list of genres (limited to 2) from freemusicarchive.org