-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
PropertyRecordFormat.java
168 lines (155 loc) · 6.16 KB
/
PropertyRecordFormat.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
164
165
166
167
168
/*
* 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 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.neo4j.kernel.impl.store.format.standard;
import org.neo4j.io.pagecache.PageCursor;
import org.neo4j.io.pagecache.PagedFile;
import org.neo4j.kernel.impl.store.PropertyType;
import org.neo4j.kernel.impl.store.format.BaseRecordFormat;
import org.neo4j.kernel.impl.store.record.PropertyBlock;
import org.neo4j.kernel.impl.store.record.PropertyRecord;
import org.neo4j.kernel.impl.store.record.Record;
import org.neo4j.kernel.impl.store.record.RecordLoad;
public class PropertyRecordFormat extends BaseRecordFormat<PropertyRecord>
{
public static final int DEFAULT_DATA_BLOCK_SIZE = 120;
public static final int DEFAULT_PAYLOAD_SIZE = 32;
public static final int RECORD_SIZE = 1/*next and prev high bits*/
+ 4/*next*/
+ 4/*prev*/
+ DEFAULT_PAYLOAD_SIZE /*property blocks*/;
// = 41
public PropertyRecordFormat()
{
super( fixedRecordSize( RECORD_SIZE ), 0, StandardFormatSettings.PROPERTY_RECORD_MAXIMUM_ID_BITS );
}
@Override
public PropertyRecord newRecord()
{
return new PropertyRecord( -1 );
}
@Override
public void read( PropertyRecord record, PageCursor cursor, RecordLoad mode, int recordSize, PagedFile storeFile )
{
int offsetAtBeginning = cursor.getOffset();
/*
* [pppp,nnnn] previous, next high bits
*/
byte modifiers = cursor.getByte();
long prevMod = (modifiers & 0xF0L) << 28;
long nextMod = (modifiers & 0x0FL) << 32;
long prevProp = cursor.getInt() & 0xFFFFFFFFL;
long nextProp = cursor.getInt() & 0xFFFFFFFFL;
record.initialize( false,
BaseRecordFormat.longFromIntAndMod( prevProp, prevMod ),
BaseRecordFormat.longFromIntAndMod( nextProp, nextMod ) );
while ( cursor.getOffset() - offsetAtBeginning < RECORD_SIZE )
{
long block = cursor.getLong();
PropertyType type = PropertyType.getPropertyTypeOrNull( block );
if ( type == null )
{
// We assume that storage is defragged
break;
}
record.setInUse( true );
record.addLoadedBlock( block );
int numberOfBlocksUsed = type.calculateNumberOfBlocksUsed( block );
if ( numberOfBlocksUsed == PropertyType.BLOCKS_USED_FOR_BAD_TYPE_OR_ENCODING )
{
cursor.setCursorException( "Invalid type or encoding of property block: " + block + " (type = " + type + ")" );
return;
}
int additionalBlocks = numberOfBlocksUsed - 1;
if ( additionalBlocks * Long.BYTES > RECORD_SIZE - (cursor.getOffset() - offsetAtBeginning) )
{
cursor.setCursorException( "PropertyRecord claims to have more property blocks than can fit in a record" );
return;
}
while ( additionalBlocks --> 0 )
{
record.addLoadedBlock( cursor.getLong() );
}
}
}
@Override
public void write( PropertyRecord record, PageCursor cursor, int recordSize, PagedFile storeFile )
{
if ( record.inUse() )
{
// Set up the record header
short prevModifier = record.getPrevProp() == Record.NO_NEXT_RELATIONSHIP.intValue() ? 0
: (short) ((record.getPrevProp() & 0xF00000000L) >> 28);
short nextModifier = record.getNextProp() == Record.NO_NEXT_RELATIONSHIP.intValue() ? 0
: (short) ((record.getNextProp() & 0xF00000000L) >> 32);
byte modifiers = (byte) (prevModifier | nextModifier);
/*
* [pppp,nnnn] previous, next high bits
*/
cursor.putByte( modifiers );
cursor.putInt( (int) record.getPrevProp() );
cursor.putInt( (int) record.getNextProp() );
// Then go through the blocks
int longsAppended = 0; // For marking the end of blocks
for ( PropertyBlock block : record )
{
long[] propBlockValues = block.getValueBlocks();
for ( long propBlockValue : propBlockValues )
{
cursor.putLong( propBlockValue );
}
longsAppended += propBlockValues.length;
}
if ( longsAppended < PropertyType.getPayloadSizeLongs() )
{
cursor.putLong( 0 );
}
}
else
{
// skip over the record header, nothing useful there
cursor.setOffset( cursor.getOffset() + 9 );
cursor.putLong( 0 );
}
}
@Override
public long getNextRecordReference( PropertyRecord record )
{
return record.getNextProp();
}
/**
* For property records there's no "inUse" byte and we need to read the whole record to
* see if there are any PropertyBlocks in use in it.
*/
@Override
public boolean isInUse( PageCursor cursor )
{
cursor.setOffset( cursor.getOffset() /*skip...*/ + 1/*mod*/ + 4/*prev*/ + 4/*next*/ );
int blocks = PropertyType.getPayloadSizeLongs();
for ( int i = 0; i < blocks; i++ )
{
long block = cursor.getLong();
if ( PropertyType.getPropertyTypeOrNull( block ) != null )
{
return true;
}
}
return false;
}
}