-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
NodeRecordFormat.java
160 lines (141 loc) · 5.87 KB
/
NodeRecordFormat.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
/*
* Copyright (c) 2002-2016 "Neo Technology,"
* Network Engine for Objects in Lund AB [http://neotechnology.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.store.format.highlimit;
import java.io.IOException;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.kernel.impl.store.format.BaseRecordFormat;
import org.neo4j.kernel.impl.store.record.NodeRecord;
import org.neo4j.kernel.impl.store.record.Record;
/**
* LEGEND:
* V: variable between 3B-8B
*
* Record format:
* 1B header
* VB first relationship
* VB first property
* 5B labels
*
* => 12B-22B
*/
class NodeRecordFormat extends BaseHighLimitRecordFormat<NodeRecord>
{
static final int RECORD_SIZE = 16;
private static final long NULL_LABELS = Record.NO_LABELS_FIELD.intValue();
private static final int DENSE_NODE_BIT = 0b0000_1000;
private static final int HAS_RELATIONSHIP_BIT = 0b0001_0000;
private static final int HAS_PROPERTY_BIT = 0b0010_0000;
private static final int HAS_LABELS_BIT = 0b0100_0000;
public NodeRecordFormat()
{
this( RECORD_SIZE );
}
NodeRecordFormat( int recordSize )
{
super( fixedRecordSize( recordSize ), 0 );
}
@Override
public NodeRecord newRecord()
{
return new NodeRecord( -1 );
}
@Override
protected void doReadInternal( NodeRecord record, PageCursor cursor, int recordSize, long headerByte,
boolean inUse )
{
// Interpret the header byte
boolean dense = has( headerByte, DENSE_NODE_BIT );
if ( record.isUseFixedReferences() )
{
byte modifiers = cursor.getByte();
long relModifier = (modifiers & 0b0000_0001L) << 32;
long propModifier = (modifiers & 0b0000_0110L) << 31;
long nextRel = cursor.getInt() & 0xFFFFFFFFL;
long nextProp = cursor.getInt() & 0xFFFFFFFFL;
long lsbLabels = cursor.getInt() & 0xFFFFFFFFL;
long hsbLabels = cursor.getByte() & 0xFF; // so that a negative byte won't fill the "extended" bits with ones.
long labels = lsbLabels | (hsbLabels << 32);
record.initialize( inUse,
BaseRecordFormat.longFromIntAndMod( nextProp, propModifier ), dense,
BaseRecordFormat.longFromIntAndMod( nextRel, relModifier ), labels );
}
else
{
// Now read the rest of the data. The adapter will take care of moving the cursor over to the
// other unit when we've exhausted the first one.
long nextRel = decodeCompressedReference( cursor, headerByte, HAS_RELATIONSHIP_BIT, NULL );
long nextProp = decodeCompressedReference( cursor, headerByte, HAS_PROPERTY_BIT, NULL );
long labelField = decodeCompressedReference( cursor, headerByte, HAS_LABELS_BIT, NULL_LABELS );
record.initialize( inUse, nextProp, dense, nextRel, labelField );
}
}
@Override
public int requiredDataLength( NodeRecord record )
{
return length( record.getNextRel(), NULL ) +
length( record.getNextProp(), NULL ) +
length( record.getLabelField(), NULL_LABELS );
}
@Override
protected byte headerBits( NodeRecord record )
{
byte header = 0;
header = set( header, DENSE_NODE_BIT, record.isDense() );
header = set( header, HAS_RELATIONSHIP_BIT, record.getNextRel(), NULL );
header = set( header, HAS_PROPERTY_BIT, record.getNextProp(), NULL );
header = set( header, HAS_LABELS_BIT, record.getLabelField(), NULL_LABELS );
return header;
}
@Override
protected boolean canUseFixedReferences( NodeRecord record )
{
return (((record.getNextProp() == NULL) || ((record.getNextProp() & 0xFFFF_FFFC_0000_0000L) == 0)) &&
((record.getNextRel() == NULL) || ((record.getNextRel() & 0xFFFF_FFFE_0000_0000L) == 0)));
}
@Override
protected void doWriteInternal( NodeRecord record, PageCursor cursor )
throws IOException
{
if ( record.isUseFixedReferences() )
{
long nextRel = record.getNextRel();
long nextProp = record.getNextProp();
short relModifier = nextRel == Record.NO_NEXT_RELATIONSHIP.intValue() ? 0 : (short)((nextRel & 0x1_0000_0000L) >> 32);
short propModifier = nextProp == Record.NO_NEXT_PROPERTY.intValue() ? 0 : (short) ((nextProp & 0x3_0000_0000L) >> 31);
// [ ,xxxx] higher bits for rel id
// [xxxx, ] higher bits for prop id
short modifiers = (short) ( relModifier | propModifier );
cursor.putByte( (byte) modifiers );
cursor.putInt( (int) nextRel );
cursor.putInt( (int) nextProp );
// lsb of labels
long labelField = record.getLabelField();
cursor.putInt( (int) labelField );
// msb of labels
cursor.putByte( (byte) ((labelField & 0xFF_0000_0000L) >> 32) );
}
else
{
encode( cursor, record.getNextRel(), NULL );
encode( cursor, record.getNextProp(), NULL );
encode( cursor, record.getLabelField(), NULL_LABELS );
}
}
}