move coding from leveldb, fix some code style errors