-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Cluster.java
167 lines (140 loc) · 3.82 KB
/
Cluster.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
/*
* 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.cluster.client;
import org.apache.commons.lang3.RandomStringUtils;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Nonnull;
import static java.util.Objects.requireNonNull;
public class Cluster
{
private final String name;
private final List<Member> members = new ArrayList<>();
/**
* Creates a cluster with a random name.
* The name is used in deciding which directory the store files are placed in.
*/
public Cluster()
{
this( RandomStringUtils.randomAlphanumeric( 8 ) );
}
/**
* Creates a cluster with a specified name.
* The name is used in deciding which directory the store files are placed in.
*/
public Cluster( @Nonnull String name )
{
requireNonNull( name );
this.name = name;
}
public String getName()
{
return name;
}
public List<Member> getMembers()
{
return members;
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
Cluster cluster = (Cluster) o;
return name.equals( cluster.name ) && members.equals( cluster.members );
}
@Override
public int hashCode()
{
int result = name.hashCode();
result = 31 * result + members.hashCode();
return result;
}
public boolean contains( URI serverId )
{
for ( Member member : members )
{
if ( serverId.toString().contains( member.getHost() ) )
{
return true;
}
}
return false;
}
/**
* Represents a member tag in the discovery XML file.
*/
public static class Member
{
private String host;
private boolean fullHaMember;
public Member( int port, boolean fullHaMember )
{
this( localhost() + ":" + port, fullHaMember );
}
public Member( String host )
{
this( host, true );
}
public Member( String host, boolean fullHaMember )
{
this.host = host;
this.fullHaMember = fullHaMember;
}
public boolean isFullHaMember()
{
return fullHaMember;
}
private static String localhost()
{
return "localhost";
}
public String getHost()
{
return host;
}
@Override
public boolean equals( Object o )
{
if ( this == o )
{
return true;
}
if ( o == null || getClass() != o.getClass() )
{
return false;
}
Member member = (Member) o;
return host.equals( member.host );
}
@Override
public int hashCode()
{
return host.hashCode();
}
}
}