Chinaunix首页 | 论坛 | 博客
  • 博客访问: 426604
  • 博文数量: 137
  • 博客积分: 5190
  • 博客等级: 大校
  • 技术积分: 997
  • 用 户 组: 普通用户
  • 注册时间: 2010-02-21 16:19
文章存档

2011年(17)

2010年(120)

我的朋友

分类: 数据库开发技术

2011-08-04 11:06:47

关于BSON的部分翻译
2011年05月13日 星期五 15:00

转载自:http://hi.baidu.com/heelenyc/blog/item/98aada01d75bc7603812bb4f.html

官方地址:

Version 1.0

BSON is abinary format in which zero or more key/value pairs are stored as a singleentity. We call this entity a document.

BSON是一种在将0个或多个键值对存储为单个实体是使用的二进制格式,这种单个实体被称为文档(document

The following grammar specifies version1.0 of the BSON standard. We've written the grammar using a pseudo- syntax. 

Valid BSON data is represented by the document non-terminal.

有效的BSON数据由非终结符document描述

Basic Types

The followingbasic types are used as terminals in the rest of the grammar. Each type must beserialized in little-endian format.

如下的基本类型作为终结符在接下来的文法中使用,每种类型必须序列化为小头格式(little-endian,低位低地址,高位高地址)

byte

1 byte (8-bits)

int32

4 bytes (32-bit signed integer)

int64

8 bytes (64-bit signed integer)

double

8 bytes (64-bit IEEE 754 floating point)

Non-terminals

The following specifies the rest of theBSON grammar. Note that quoted strings represent terminals, and should beinterpreted with C semantics (e.g. "\x01" represents the byte0000 0001). Also notethat we use the * operator as shorthand for repetition 

注意带引号的字符串表示终结符,需要用C语义来解释,比如"\x01"表示字节0000 0001,同样注意我们用*表示重复

(e.g.("\x01"*2) is "\x01\x01"). 

When used as a unary operator, * means that therepetition can occur 0 or more times.

*作为一元操作符使用时,表示可重复0或多次

document

::=

int32 e_list "\x00"

BSON Document

e_list

::=

element e_list

Sequence of elements

|

""

element

::=

"\x01" e_name double

Floating point

|

"\x02" e_name string

UTF-8 string

|

"\x03" e_name document

Embedded document

|

"\x04" e_name document

Array

|

"\x05" e_name binary

Binary data

|

"\x06" e_name

Undefined — Deprecated

|

"\x07" e_name (byte*12)

|

"\x08" e_name "\x00"

Boolean "false"

|

"\x08" e_name "\x01"

Boolean "true"

|

"\x09" e_name int64

UTC datetime

|

"\x0A" e_name

Null value

|

"\x0B" e_name cstring cstring

Regular expression

|

"\x0C" e_name string (byte*12)

DBPointer — Deprecated

|

"\x0D" e_name string

JavaScript code

|

"\x0E" e_name string

Symbol

|

"\x0F" e_name code_w_s

JavaScript code w/ scope

|

"\x10" e_name int32

32-bit Integer

|

"\x11" e_name int64

Timestamp

|

"\x12" e_name int64

64-bit integer

|

"\xFF" e_name

Min key

|

"\x7F" e_name

Max key

e_name

::=

cstring

Key name

string

::=

int32 (byte*) "\x00"

String

cstring

::=

(byte*) "\x00"

CString

binary

::=

int32 subtype (byte*)

Binary

subtype

::=

"\x00"

Binary / Generic

|

"\x01"

Function

|

"\x02"

Binary (Old)

|

"\x03"

UUID

|

"\x05"

MD5

|

"\x80"

User defined

code_w_s

::=

int32 string document

Code w/ scope

Examples

The followingare some example documents (in JavaScript / Python style syntax) and theircorresponding BSON representations. Try mousing over them for some usefulcorrelation.

{"hello": "world"}

"\x16\x00\x00\x00

documentint32 表示总长度22个字节

\x02

指定类型是utf-8 str

hello\x00 

键值 包括结尾的\0

\x06\x00\x00\x00

stringint32 表示字符占用六个字节,包括结尾的\0

world\x00

valueworld加结尾的\0

\x00"

doucument的结尾\0

{"BSON": ["awesome", 5.05, 1986]}

"1\x00\x00\x00\x04BSON\x00&\x00 
 \x00\x00
\x020\x00\x08\x00\x00 
 \x00awesome\x00\x011\x00333333 
 \x14@\x102\x00\xc2\x07\x00\x00 
  
 \x00\x00"

 

JSON:javascirpt object notation

BSON [bee ·sahn], short for Bin-ary (), is a bin-ary-en-coded seri-al-iz-a-tion of JSON-like doc-u-ments.Like JSON, BSON sup-ports the em-bed-ding of doc-u-ments and ar-rays with-inoth-er doc-u-ments and ar-rays. BSON also con-tains ex-ten-sions that al-lowrep-res-ent-a-tion of data types that are not part of the JSON spec. For ex-ample,BSON has a Date type and a BinData type.

BSONBinary  JSON的简称,是类JSON文档的一个二进制编码序列化方式,和JSON类似,BSON支持文档和数组在其他文档和数组中的嵌套。同样BSON还容许数据类型表示的扩展,JSON规范没有这个。。。

BSON canbe com-pared to bin-ary inter-change for-mats, like . BSON is more"schema-less" than Proto-col Buf-fers, which can give it an ad-vant-agein flex-ib-il-ity but also a slight dis-ad-vant-age in space ef-fi-ciency (BSONhas over-head for field names with-in the seri-al-ized data).

BSON可比作二进制交换格式,比protocol buffer更“无模式”,带来的优势是灵活性,稍微不足是空间的有效性(在序列化数据中有对域名的overhead

BSON wasde-signed to have the fol-low-ing three char-ac-ter-ist-ics:

BSON的为下面三个特性而设计:

1.   Lightweight轻量

Keep-ing spa-tial over-head to a min-im-umis im-port-ant for any data rep-res-ent-a-tion format, es-pe-cially when usedover the net-work.

2.   Traversable可遍历性

BSON is de-signed to be tra-versed eas-ily.This is a vi-tal prop-erty in its role as the primary data rep-res-ent-a-tionfor .

MongoDB重视这个特性

3.   Efficient有效、高效

En-cod-ing data to BSON and de-cod-ingfrom BSON can be per-formed very quickly in most lan-guages due to the use of Cdata types.

由于使用C类型的缘故,BSON解码编码很容易很快


What is thepoint of BSON when it is no smaller than JSON in many cases?

BSON isdesigned to be efficient in space, but in many cases is not much more efficientthan JSON. In some cases BSON uses even more space than JSON. The reason forthis is another of the BSON design goals: traversability. BSON adds some"extra" information to documents, like length prefixes, that make iteasy and fast to traverse.

BSON is alsodesigned to be fast to encode and decode. For example, integers are stored as32 (or 64) bit integers, so they don't need to be parsed to and from text. Thisuses more space than JSON for small integers, but is much faster to parse.

 

BSON注重于空间的有效性,但是在很多情况下并没有比JSON有效多少。甚至在一些情况下比JSON使用更多的空间。导致这个的原因是BSON的另一个设计目标:可遍历性。BSON增加了一些额外的信息,比如长度前缀,这使得它更容易并快速的遍历

 

BSON的设计同时也注重编码和解码。例如整数被存储为32或者64位整数,所以不必从文本去解析再转换。这相对与JSON会花费更多的空间但是解析快很多。

 

Implementationsof the BSON specification exist for many different languages / environments.Some implementations are currently embedded within  drivers, since MongoDB was the first largeproject to make use of BSON. Over time those libraries will be made morestand-alone, but they should be usable independently of MongoDB in theircurrent state.

 

有多种语言环境下的BSON的实现,由于Mongodb是第一个使用BSON的大项目,一些实现正被嵌入到Mongodb的驱动里
阅读(1956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~