Graph database_neo4j 底层存储结构分析(5)
3.5 Property 的存储
下面是neo4j graph db 中,Property数据存储对应的文件:
neostore.propertystore.db
neostore.propertystore.db.arrays
neostore.propertystore.db.arrays.id
neostore.propertystore.db.id
neostore.propertystore.db.index
neostore.propertystore.db.index.id
neostore.propertystore.db.index.keys
neostore.propertystore.db.index.keys.id
neostore.propertystore.db.strings
neostore.propertystore.db.strings.id
neo4j 中, Property 的存储是由 PropertyStore, ArrayPropertyStore, StringPropertyStore 和PropertyKeyTokenStore 4种类型的Store配合来完成的.
类PropertyStore对应的存储文件是neostore.propertystore.db, 相应的用来存储 string/array 类型属性值的文件分别是neostore.propertystore.db.strings (StringPropertyStore) 和 neostore.propertystore.db.arrays(ArrayPropertyStore). 其存储模型示意图如下:

其中PropertyStore是Property最主要的存储结构,当Property的Key-Value对的Value 是字符串或数组类型并且要求的存储空间比较大,在PropertyStore中保存不了,则会存在StringPropertyStore/ ArrayPropertyStore这样的DynamicStore 中。如果长度超过一个block ,则分block存储,并将其在StringPropertyStore/ ArrayPropertyStore中的第1个block 的 block_id 保存到 PropertyStore类型文件相应record 的PropertyBlock字段中。
PropertyKeyTokenStore和StringPropertyStore 配合用来存储Propery的Key部分。Propery的Key是编码的,key 的 id 保存在 PropertyKeyTokenStore (即 neostore.propertystore.db.index),key 的字符串名保存在对应的StringPropertyStore类型文件neostore.propertystore.db.index.keys 中。
ArrayPropertyStore的存储格式见< 3.3.2 DynamicStore 类型>,下面分别介绍一下PropertyStore和PropertyKeyTokenStore(PropertyKeyTokenStore)的文件存储格式。
3.5.1 PropertyStore类型的存储格式
neostore.propertystore.db文件存储格式示意图如下,整个文件是有一个 RECORD_SIZE=41 Bytes 的定长数组和一个字符串描述符“PropertyStore v0.A.2”(文件类型描述TYPE_DESCRIPTOR和 neo4j 的 ALL_STORES_VERSION构成)。访问时,可以通过 prop_id 作为数组的下标进行访问。

下面介绍一下 property record 中每个字段的含义:
- highByte(1 Byte):第1字节,共分成2部分
/*
* [pppp,nnnn] previous, next high bits
*/
byte modifiers = buffer.get();
- 第1~4 bit 表示 next 的高4位;
- 第 5~8 bit表示 prev 的高4位
- prev(4 Bytes) : Node或Relationship 的属性是通过双向链表方式组织的,prev 表示本属性在双向链表中的上一个属性的id。第2~5字节是prev property_id的 低32位. 加上highByte字节的第 5~8 bit作为高4位,构成一个完整的36位property_id。
- next(4 Bytes) : next 表示本属性在双向链表中的下一个属性的id。第6~9字节是next property_id的 低32位. 加上highByte字节的第 1~4 bit作为高4位,构成一个完整的36位property_id。
- payload: payload 由block_header(8 Bytes)加3个property_block(8 Bytes)组成,共计 32 Bytes. block_header 分成3部分:
- key_id(24 bits) : 第1 ~24 bit , property 的key 的 id
- type( 4 bits ): 第25 ~28 bit , property 的 value 的类型,支持 string, Interger,Boolean, Float, Long,Double, Byte, Character,Short, array.
- payload(36 bits): 第29 ~64 bit, 共计36bit;对于Interger, Boolean, Float, Byte, Character , Short 类型的值,直接保存在payload;对于long,如果36位可以表示,则直接保存在payload,如果不够,则保存到第1个PropertyBlock中;double 类型,保存到第1个PropertyBlock中;对于 array/string ,如果编码后在 block_header及3个PropertyBlock 能保存,则直接保存;否则,保存到ArrayDynamicStore/StringDynamicStore 中, payload 保存其在ArrayDynamicStore中的数组下表。
3.5.2 String 类型属性值的保存过程
下面的代码片段展示了neo4j 中,比较长的 String 类型属性值的保存处理过程,其是如何分成多个 DynamicBlock 来存储的。

3.5.2.1 encodeValue 函数
encodeValue 函数是 PropertySTore.java 的成员函数, 它实现了不同类型的属性值的编码.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
|
</pre><div>public void encodeValue( PropertyBlock block, int keyId, Object value ){if ( value instanceof String ){ // Try short string first, i.e. inlined in the property blockString string = (String) value;if ( LongerShortString.encode( keyId, string, block, PropertyType.getPayloadSize() ) ){return;}// Fall back to dynamic string storebyte[] encodedString = encodeString( string );Collection valueRecords = allocateStringRecords( encodedString );setSingleBlockValue( block, keyId, PropertyType.STRING, first( valueRecords ).getId() );for ( DynamicRecord valueRecord : valueRecords ){valueRecord.setType( PropertyType.STRING.intValue() );block.addValueRecord( valueRecord );}}else if ( value instanceof Integer ){setSingleBlockValue( block, keyId, PropertyType.INT, ((Integer) value).longValue() );}else if ( value instanceof Boolean ){setSingleBlockValue( block, keyId, PropertyType.BOOL, ((Boolean) value ? 1L : 0L) );}else if ( value instanceof Float ){setSingleBlockValue( block, keyId, PropertyType.FLOAT, Float.floatToRawIntBits( (Float) value ) );}else if ( value instanceof Long ){long keyAndType = keyId | (((long) PropertyType.LONG.intValue()) << 24);if ( ShortArray.LONG.getRequiredBits( (Long) value ) <= 35 ){ // We only need one block for this value, special layout compared to, say, an integerblock.setSingleBlock( keyAndType | (1L << 28) | ((Long) value << 29) );}else{ // We need two blocks for this valueblock.setValueBlocks( new long[]{keyAndType, (Long) value} );}}else if ( value instanceof Double ){block.setValueBlocks( new long[]{keyId | (((long) PropertyType.DOUBLE.intValue()) << 24),Double.doubleToRawLongBits( (Double) value )} );}else if ( value instanceof Byte ){setSingleBlockValue( block, keyId, PropertyType.BYTE, ((Byte) value).longValue() );}else if ( value instanceof Character ){setSingleBlockValue( block, keyId, PropertyType.CHAR, (Character) value );}else if ( value instanceof Short ){setSingleBlockValue( block, keyId, PropertyType.SHORT, ((Short) value).longValue() );}else if ( value.getClass().isArray() ){ // Try short array first, i.e. inlined in the property blockif ( ShortArray.encode( keyId, value, block, PropertyType.getPayloadSize() ) ){return;}// Fall back to dynamic array storeCollection arrayRecords = allocateArrayRecords( value );setSingleBlockValue( block, keyId, PropertyType.ARRAY, first( arrayRecords ).getId() );for ( DynamicRecord valueRecord : arrayRecords ){valueRecord.setType( PropertyType.ARRAY.intValue() );block.addValueRecord( valueRecord );}}else{throw new IllegalArgumentException( "Unknown property type on: " + value + ", " + value.getClass() );}} |
3.5.2.2 allocateStringRecords 函数
allocateStringRecords 函数是 PropertySTore.java 的成员函数.
|
1
2
3
4
5
6
7
8
9
|
</pre><div>private Collection allocateStringRecords( byte[] chars ){return stringPropertyStore.allocateRecordsFromBytes( chars );} |
3.5.2.3 allocateRecordsFromBytes 函数
allocateRecordsFromBytes 函数是 AbstractDynamicStore .java 的成员函数.
|
1
2
3
4
5
6
7
8
9
10
11
|
</pre><div>protected Collection allocateRecordsFromBytes( byte src[] ){return allocateRecordsFromBytes( src, Collections.emptyList().iterator(),recordAllocator );} |
3.5.2.4 allocateRecordsFromBytes 函数
allocateRecordsFromBytes 函数是 AbstractDynamicStore .java 的成员函数.
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
</pre><div>public static Collection allocateRecordsFromBytes(byte src[], Iterator recordsToUseFirst,DynamicRecordAllocator dynamicRecordAllocator ){assert src != null : "Null src argument";List recordList = new LinkedList<>();DynamicRecord nextRecord = dynamicRecordAllocator.nextUsedRecordOrNew( recordsToUseFirst );int srcOffset = 0;int dataSize = dynamicRecordAllocator.dataSize();do{DynamicRecord record = nextRecord;record.setStartRecord( srcOffset == 0 );if ( src.length - srcOffset > dataSize ){byte data[] = new byte[dataSize];System.arraycopy( src, srcOffset, data, 0, dataSize );record.setData( data );nextRecord = dynamicRecordAllocator.nextUsedRecordOrNew( recordsToUseFirst );record.setNextBlock( nextRecord.getId() );srcOffset += dataSize;}else{byte data[] = new byte[src.length - srcOffset];System.arraycopy( src, srcOffset, data, 0, data.length );record.setData( data );nextRecord = null;record.setNextBlock( Record.NO_NEXT_BLOCK.intValue() );}recordList.add( record );assert !record.isLight();assert record.getData() != null;}while ( nextRecord != null );return recordList;} |
3.5.3 ShortArray 类型属性值的保存过程
ShortArray.encode( keyId, value, block, PropertyType.getPayloadSize() ), 它是在 kernel/impl/nioneo/store/ShortArray.java 中实现的,下面是其代码片段。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
</pre><div>public static boolean encode( int keyId, Object array, PropertyBlock target, int payloadSizeInBytes ){/** If the array is huge, we don't have to check anything else.* So do the length check first.*/int arrayLength = Array.getLength( array );if ( arrayLength > 63 )/*because we only use 6 bits for length*/{return false;}ShortArray type = typeOf( array );if ( type == null ){return false;}int requiredBits = type.calculateRequiredBitsForArray( array, arrayLength );if ( !willFit( requiredBits, arrayLength, payloadSizeInBytes ) ){// Too big arrayreturn false;}final int numberOfBytes = calculateNumberOfBlocksUsed( arrayLength, requiredBits ) * 8;if ( Bits.requiredLongs( numberOfBytes ) > PropertyType.getPayloadSizeLongs() ){return false;}Bits result = Bits.bits( numberOfBytes );// [][][ ,bbbb][bbll,llll][yyyy,tttt][kkkk,kkkk][kkkk,kkkk][kkkk,kkkk]writeHeader( keyId, type, arrayLength, requiredBits, result );type.writeAll( array, arrayLength, requiredBits, result );target.setValueBlocks( result.getLongs() );return true;}private static void writeHeader( int keyId, ShortArray type, int arrayLength, int requiredBits, Bits result ){result.put( keyId, 24 );result.put( PropertyType.SHORT_ARRAY.intValue(), 4 );result.put( type.type.intValue(), 4 );result.put( arrayLength, 6 );result.put( requiredBits, 6 );} |
3.5.4 PropertyKeyTokenStore的文件存储格式

类PropertyTypeTokenStore对应的存储文件名是neostore.propertystore.db.index,其对应的存储格式如上图所示: 是一个长度为 RECORD_SIZE=9Bytes 的 record 数组和和一个字符串“PropertyIndexStore v0.A.2”(文件类型描述TYPE_DESCRIPTOR和 neo4j 的 ALL_STORES_VERSION构成)。访问时,可以通过 token_id 作为数组的下标进行访问。
record 是由 in_use(1 Byte) ,prop_count(4 Bytes), name_id(4 Bytes)构成。
Graph database_neo4j 底层存储结构分析(5)的更多相关文章
- Graph database_neo4j 底层存储结构分析(8)
3.8 示例1:neo4j_exam 下面看一个简单的例子,然后看一下几个主要的存储文件,有助于理解<3–neo4j存储结构>描述的neo4j 的存储格式. 3.8.1 neo4j ...
- Graph database_neo4j 底层存储结构分析(7)
3.7 Relationship 的存储 下面是neo4j graph db 中,Relationship数据存储对应的文件: neostore.relationshipgroupstore.db ...
- Graph database_neo4j 底层存储结构分析(6)
3.6 Node 数据存储 neo4j 中, Node 的存储是由 NodeStore 和 ArrayPropertyStore 2中类型配合来完成的. node 的label 内容是存在Array ...
- Graph database_neo4j 底层存储结构分析(1)
1 neo4j 中节点和关系的物理存储模型 1.1 neo4j存储模型 The node records contain only a pointer to their first pr ...
- Graph database_neo4j 底层存储结构分析(4)
3.3.2 DynamicStore 类型 3.3.2.1 AbstractDynamicStore 的存储格式 neo4j 中对于字符串等变长值的保存策略是用一组定长的 block ...
- Graph database_neo4j 底层存储结构分析(3)
3.3 通用的Store 类型 3.3.1 id 类型 下面是 neo4j db 中,每种Store都有自己的ID文件(即后缀.id 文件),它们的格式都是一样的. [test00]$ls - ...
- Graph database_neo4j 底层存储结构分析(2)
3 neo4j存储结构 neo4j 中,主要有4类节点,属性,关系等文件是以数组作为核心存储结构:同时对节点,属性,关系等类型的每个数据项都会分配一个唯一的ID,在存储时以该ID 为数组的 ...
- Redis(一) 数据结构与底层存储 & 事务 & 持久化 & lua
参考文档:redis持久化:http://blog.csdn.net/freebird_lb/article/details/7778981 https://blog.csdn.net/jy69240 ...
- HBase底层存储原理
HBase底层存储原理——我靠,和cassandra本质上没有区别啊!都是kv 列存储,只是一个是p2p另一个是集中式而已! 首先HBase不同于一般的关系数据库, 它是一个适合于非结构化数据存储的数 ...
随机推荐
- 为win7添加ubuntu的启动引导项
利用MBRFix删除ubuntu的开机引导界面,恢复成win7引导之后,为win7添加ubuntu的启动引导项: 直接利用EasyBCD添加一个Grub2的引导项即可 参考:http://mathis ...
- shell脚本调试之工具——bashdb
bash是Unix/Linux操作系统最常用的shell之一,它非常灵活,和awk.c++配合起来异常强大 以下使用一个测试脚本来说明使用bash调试的方法 test.sh #!/bin/bash e ...
- 在CI中集成phpmailer,方便使用SMTP发送邮件
直接使用phpmailer的话,有时候不是很方便,特别你的很多功能都是基于CI完成的时候,要相互依赖就不方便了,所以在想,那是否可以将phpmailer集成到CI中呢,像使用email类这样使用他,功 ...
- Windows命令行提取日期时间
参考: http://elicecn.blog.163.com/blog/static/174017473200931910320556/ SET str="%date:~0,4%%date ...
- powerdesigner奇淫技
在日常开发中数据库的设计常常需要建立模型,而powerdesigner是个不错的选择.但很多时候用powerdesigner生成模型后再去创建表结构,会觉得烦和别扭.那么能不能数据库表建好后再生成模型 ...
- 单个php页面实现301重定向
301重定向的意思是页面永久性移走,实现方式是当用户请求页面时,服务器返回相应http数据流头信息状态码为301,表示本网页永久性转移到另一个地址,301重定向是页面永久性转移,一般用在不打算改变的地 ...
- 学习cocos-js的准备工作
我学习 cocos2d-js 的方向: 学习 cocos2d-js 的 HTML5 版本:即 canvas 渲染. 下载cocos-js 文件 地址: http://www.cocos2d-x.org ...
- CSS对字体单位的总结
国内的设计师大都喜欢用px,而国外的网站大都喜欢用em和rem,那么三者有什么区别,又各自有什么优劣呢? PX特点 1. IE无法调整那些使用px作为单位的字体大小: 2. 国外的大部分网站能够调整的 ...
- easyfinding(codevs 3280)
3280 easyfinding 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 给一个M 行N 列 ...
- 案例(用封装的ajax加载数据库的数据到页面)
本程序主要功能是以表格方式在网页上显示数据库的内容 LoadUsers.htm代码: <head> <title></title> <script src=& ...