-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
SchemaDescriptor.java
128 lines (116 loc) · 5.04 KB
/
SchemaDescriptor.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
/*
* 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.internal.kernel.api.schema;
import java.util.function.Predicate;
import org.neo4j.internal.kernel.api.TokenNameLookup;
import org.neo4j.storageengine.api.lock.ResourceType;
/**
* Internal representation of one schema unit, for example a label-property pair.
*
* Even when only supporting a small set of different schemas, the number of common methods is very small. This
* interface therefore supports a visitor type access pattern, results can be computed using the {#compute} method, and
* side-effect type logic performed using the processWith method. This means that when implementing this interface
* with a new concrete type, the compute and processWith method implementations need to be added similarly to
* how this is done in eg. LabelSchemaDescriptor, and the SchemaProcessor and SchemaComputer interfaces need to be
* extended with methods taking the new concrete type as argument.
*/
public interface SchemaDescriptor extends SchemaDescriptorSupplier
{
/**
* Computes some value by feeding this object into the given SchemaComputer.
*
* Note that implementers of this method just need to call `return computer.compute( this );`.
*
* @param computer The SchemaComputer that hold the logic for the computation
* @param <R> The return type
* @return The result of the computation
*/
<R> R computeWith( SchemaComputer<R> computer );
/**
* Performs some side-effect type logic by processing this object using the given SchemaProcessor.
*
* Note that implementers of this method just need to call `return processor.process( this );`.
*
* @param processor The SchemaProcessor that hold the logic for the computation
*/
void processWith( SchemaProcessor processor );
/**
* @param tokenNameLookup used for looking up names for token ids.
* @return a user friendly description of what this index indexes.
*/
String userDescription( TokenNameLookup tokenNameLookup );
/**
* Translate the schema key to a key name using the given {@link TokenNameLookup}.
* @param tokenNameLookup used for looking up names for token ids.
* @return The string name of the key token.
*/
String keyName( TokenNameLookup tokenNameLookup );
/**
* This method return the property ids that are relevant to this Schema Descriptor.
*
* Putting this method here is a convenience that will break if/when we introduce more complicated schema
* descriptors like paths, but until that point it is very useful.
*
* @return the property ids
*/
int[] getPropertyIds();
/**
* Assume that this schema descriptor describes a schema that includes a single property id, and return that id.
*
* @return The presumed single property id of this schema.
* @throws IllegalStateException if this schema does not have exactly one property.
*/
default int getPropertyId()
{
int[] propertyIds = getPropertyIds();
if ( propertyIds.length != 1 )
{
throw new IllegalStateException(
"Single property schema requires one property but had " + propertyIds.length );
}
return propertyIds[0];
}
/**
* Id of underlying schema descriptor key.
* Key is part of schema unit that determines which resources with specified properties are applicable.
* @return id of underlying key
*/
int keyId();
/**
* Type of underlying schema descriptor key.
* Key is part of schema unit that determines which resources with specified properties are applicable.
* @return type of underlying key
*/
ResourceType keyType();
/**
* Create a predicate that checks whether a schema descriptor Supplier supplies the given schema descriptor.
* @param descriptor The schema descriptor to check equality with.
* @return A predicate that returns {@code true} if it is given a schema descriptor supplier that supplies the
* same schema descriptor as the given schema descriptor.
*/
static <T extends SchemaDescriptorSupplier> Predicate<T> equalTo( SchemaDescriptor descriptor )
{
return supplier -> descriptor.equals( supplier.schema() );
}
interface Supplier
{
SchemaDescriptor schema();
}
}