-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
AbstractBaseRecord.java
210 lines (185 loc) · 5.97 KB
/
AbstractBaseRecord.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
* 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.record;
import java.util.function.Predicate;
import org.neo4j.helpers.CloneableInPublic;
/**
* {@link AbstractBaseRecord records} are intended to be reusable. Created with a zero-arg constructor
* and initialized with the public {@code initialize} method exposed by the specific record implementations,
* or {@link #clear() cleared} if reading a record that isn't in use.
*/
public abstract class AbstractBaseRecord implements CloneableInPublic
{
public static final int NO_ID = -1;
private long id;
// Used for the "record unit" feature where one logical record may span two physical records,
// as to still keep low and fixed record size, but support occasionally bigger records.
private long secondaryUnitId;
// This flag is for when a record required a secondary unit, was changed, as a result of that change
// no longer requires that secondary unit and gets updated. In that scenario we still want to know
// about the secondary unit id so that we can free it when the time comes to apply the record to store.
private boolean requiresSecondaryUnit;
private boolean inUse;
private boolean created;
protected AbstractBaseRecord( long id )
{
this.id = id;
clear();
}
protected AbstractBaseRecord initialize( boolean inUse )
{
this.inUse = inUse;
this.created = false;
this.secondaryUnitId = NO_ID;
this.requiresSecondaryUnit = false;
return this;
}
/**
* Clears this record to its initial state. Initializing this record with an {@code initialize-method}
* doesn't require clear the record first, either initialize or clear suffices.
* Subclasses, most specific subclasses only, implements this method by calling initialize with
* zero-like arguments.
*/
public void clear()
{
inUse = false;
created = false;
secondaryUnitId = NO_ID;
requiresSecondaryUnit = false;
}
public long getId()
{
return id;
}
public int getIntId()
{
return Math.toIntExact( id );
}
public void setId( long id )
{
this.id = id;
}
/**
* Sets a secondary record unit ID for this record. If this is set to something other than {@link #NO_ID}
* then {@link #requiresSecondaryUnit()} will return {@code true}.
* Setting this id is separate from setting {@link #requiresSecondaryUnit()} since this secondary unit id
* may be used to just free that id at the time of updating in the store if a record goes from two to one unit.
*/
public void setSecondaryUnitId( long id )
{
this.secondaryUnitId = id;
}
public boolean hasSecondaryUnitId()
{
return secondaryUnitId != NO_ID;
}
/**
* @return secondary record unit ID set by {@link #setSecondaryUnitId(long)}.
*/
public long getSecondaryUnitId()
{
return this.secondaryUnitId;
}
public void setRequiresSecondaryUnit( boolean requires )
{
this.requiresSecondaryUnit = requires;
}
/**
* @return whether or not a secondary record unit ID has been assigned.
*/
public boolean requiresSecondaryUnit()
{
return requiresSecondaryUnit;
}
public final boolean inUse()
{
return inUse;
}
public void setInUse( boolean inUse )
{
this.inUse = inUse;
}
public final void setCreated()
{
this.created = true;
}
public final boolean isCreated()
{
return created;
}
@Override
public int hashCode()
{
return (int) (( id >>> 32 ) ^ id );
}
@Override
public boolean equals( Object obj )
{
if ( this == obj )
return true;
if ( obj == null )
return false;
if ( getClass() != obj.getClass() )
return false;
AbstractBaseRecord other = (AbstractBaseRecord) obj;
if ( id != other.id )
return false;
return true;
}
@Override
public AbstractBaseRecord clone()
{
throw new UnsupportedOperationException();
}
@SuppressWarnings( "rawtypes" )
private static final Predicate IN_USE_FILTER = new Predicate<AbstractBaseRecord>()
{
@Override
public boolean test( AbstractBaseRecord item )
{
return item.inUse();
}
};
@SuppressWarnings( "rawtypes" )
private static final Predicate NOT_IN_USE_FILTER = new Predicate<AbstractBaseRecord>()
{
@Override
public boolean test( AbstractBaseRecord item )
{
return !item.inUse();
}
};
/**
* @return {@link Predicate filter} which only records that are {@link #inUse() in use} passes.
*/
@SuppressWarnings( "unchecked" )
public static <RECORD extends AbstractBaseRecord> Predicate<RECORD> inUseFilter()
{
return IN_USE_FILTER;
}
/**
* @return {@link Predicate filter} which only records that are {@link #inUse() NOT in use} passes.
*/
@SuppressWarnings( "unchecked" )
public static <RECORD extends AbstractBaseRecord> Predicate<RECORD> notInUseFilter()
{
return NOT_IN_USE_FILTER;
}
}