-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
BaseRecordFormats.java
160 lines (141 loc) · 5.55 KB
/
BaseRecordFormats.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-2018 "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;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import org.neo4j.kernel.impl.store.format.standard.MetaDataRecordFormat;
import org.neo4j.kernel.impl.store.record.MetaDataRecord;
import static java.util.stream.Collectors.toSet;
import static org.neo4j.helpers.ArrayUtil.contains;
/**
* Base class for simpler implementation of {@link RecordFormats}.
*/
public abstract class BaseRecordFormats implements RecordFormats
{
private final int generation;
private final Capability[] capabilities;
private final String storeVersion;
private final String introductionVersion;
protected BaseRecordFormats( String storeVersion, String introductionVersion, int generation,
Capability... capabilities )
{
this.storeVersion = storeVersion;
this.generation = generation;
this.capabilities = capabilities;
this.introductionVersion = introductionVersion;
}
@Override
public String storeVersion()
{
return storeVersion;
}
@Override
public String introductionVersion()
{
return introductionVersion;
}
@Override
public RecordFormat<MetaDataRecord> metaData()
{
return new MetaDataRecordFormat();
}
@Override
public boolean equals( Object obj )
{
if ( obj == null || !(obj instanceof RecordFormats) )
{
return false;
}
RecordFormats other = (RecordFormats) obj;
return node().equals( other.node() ) &&
relationship().equals( other.relationship() ) &&
relationshipGroup().equals( other.relationshipGroup() ) &&
property().equals( other.property() ) &&
labelToken().equals( other.labelToken() ) &&
relationshipTypeToken().equals( other.relationshipTypeToken() ) &&
propertyKeyToken().equals( other.propertyKeyToken() ) &&
dynamic().equals( other.dynamic() );
}
@Override
public int hashCode()
{
int hashCode = 17;
hashCode = 31 * hashCode + node().hashCode();
hashCode = 31 * hashCode + relationship().hashCode();
hashCode = 31 * hashCode + relationshipGroup().hashCode();
hashCode = 31 * hashCode + property().hashCode();
hashCode = 31 * hashCode + labelToken().hashCode();
hashCode = 31 * hashCode + relationshipTypeToken().hashCode();
hashCode = 31 * hashCode + propertyKeyToken().hashCode();
hashCode = 31 * hashCode + dynamic().hashCode();
return hashCode;
}
@Override
public String toString()
{
return "RecordFormat:" + getClass().getSimpleName() + "[" + storeVersion() + "]";
}
@Override
public int generation()
{
return generation;
}
@Override
public Capability[] capabilities()
{
return capabilities;
}
@Override
public boolean hasCapability( Capability capability )
{
return contains( capabilities(), capability );
}
public static boolean hasCompatibleCapabilities( RecordFormats one, RecordFormats other, CapabilityType type )
{
Set<Capability> myFormatCapabilities = Stream.of( one.capabilities() )
.filter( capability -> capability.isType( type ) ).collect( toSet() );
Set<Capability> otherFormatCapabilities = Stream.of( other.capabilities() )
.filter( capability -> capability.isType( type ) ).collect( toSet() );
if ( myFormatCapabilities.equals( otherFormatCapabilities ) )
{
// If they have the same capabilities then of course they are compatible
return true;
}
Set<Capability> removedCapabilities =
new HashSet<>( myFormatCapabilities ).stream().filter( capability -> !otherFormatCapabilities.contains( capability ) ).collect( toSet() );
Set<Capability> addedCapabilities =
new HashSet<>( otherFormatCapabilities ).stream().filter( capability -> !myFormatCapabilities.contains( capability ) ).collect( toSet() );
boolean allAddedAreAdditive = addedCapabilities.stream().allMatch( Capability::isAdditive );
if ( removedCapabilities.isEmpty() && allAddedAreAdditive )
{
// Even if capabilities of the two aren't the same then there's a special case where if the additional
// capabilities of the other format are all additive then they are also compatible because no data
// in the existing store needs to be migrated.
return true;
}
return false;
}
@Override
public boolean hasCompatibleCapabilities( RecordFormats other, CapabilityType type )
{
return hasCompatibleCapabilities( this, other, type );
}
}