/
CacheSetExample.java
193 lines (154 loc) · 6.06 KB
/
CacheSetExample.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.gridgain.examples.datagrid.datastructures;
import org.apache.ignite.*;
import org.apache.ignite.cache.datastructures.*;
import org.apache.ignite.lang.*;
import org.gridgain.examples.datagrid.*;
import java.util.*;
/**
* Grid cache distributed set example.
* <p>
* Remote nodes should always be started with special configuration file which
* enables P2P class loading: {@code 'ggstart.{sh|bat} examples/config/example-cache.xml'}.
* <p>
* Alternatively you can run {@link CacheNodeStartup} in another JVM which will
* start GridGain node with {@code examples/config/example-cache.xml} configuration.
*/
public class CacheSetExample {
/** Cache name. */
private static final String CACHE_NAME = "partitioned_tx";
/** Set instance. */
private static GridCacheSet<String> set;
/**
* Executes example.
*
* @param args Command line arguments, none required.
* @throws IgniteCheckedException If example execution failed.
*/
public static void main(String[] args) throws IgniteCheckedException {
try (Ignite g = Ignition.start("examples/config/example-cache.xml")) {
System.out.println();
System.out.println(">>> Cache set example started.");
// Make set name.
String setName = UUID.randomUUID().toString();
set = initializeSet(g, setName);
writeToSet(g);
clearAndRemoveSet(g);
}
System.out.println("Cache set example finished.");
}
/**
* Initialize set.
*
* @param g Grid.
* @param setName Name of set.
* @return Set.
* @throws IgniteCheckedException If execution failed.
*/
private static GridCacheSet<String> initializeSet(Ignite g, String setName) throws IgniteCheckedException {
// Initialize new set.
GridCacheSet<String> set = g.cache(CACHE_NAME).dataStructures().set(setName, false, true);
// Initialize set items.
for (int i = 0; i < 10; i++)
set.add(Integer.toString(i));
System.out.println("Set size after initializing: " + set.size());
return set;
}
/**
* Write items into set.
*
* @param g Grid.
* @throws IgniteCheckedException If failed.
*/
private static void writeToSet(Ignite g) throws IgniteCheckedException {
final String setName = set.name();
// Write set items on each node.
g.compute().broadcast(new SetClosure(CACHE_NAME, setName));
System.out.println("Set size after writing [expected=" + (10 + g.cluster().nodes().size() * 5) +
", actual=" + set.size() + ']');
System.out.println("Iterate over set.");
// Iterate over set.
for (String item : set)
System.out.println("Set item: " + item);
// Set API usage examples.
if (!set.contains("0"))
throw new RuntimeException("Set should contain '0' among its elements.");
if (set.add("0"))
throw new RuntimeException("Set should not allow duplicates.");
if (!set.remove("0"))
throw new RuntimeException("Set should correctly remove elements.");
if (set.contains("0"))
throw new RuntimeException("Set should not contain '0' among its elements.");
if (!set.add("0"))
throw new RuntimeException("Set should correctly add new elements.");
}
/**
* Clear and remove set.
*
* @param g Grid.
* @throws IgniteCheckedException If execution failed.
*/
private static void clearAndRemoveSet(Ignite g) throws IgniteCheckedException {
System.out.println("Set size before clearing: " + set.size());
// Clear set.
set.clear();
System.out.println("Set size after clearing: " + set.size());
// Remove set from cache.
g.cache(CACHE_NAME).dataStructures().removeSet(set.name());
System.out.println("Set was removed: " + set.removed());
// Try to work with removed set.
try {
set.contains("1");
}
catch (IgniteException expected) {
System.out.println("Expected exception - " + expected.getMessage());
}
}
/**
* Closure to populate the set.
*/
private static class SetClosure implements IgniteRunnable {
/** Cache name. */
private final String cacheName;
/** Set name. */
private final String setName;
/**
* @param cacheName Cache name.
* @param setName Set name.
*/
SetClosure(String cacheName, String setName) {
this.cacheName = cacheName;
this.setName = setName;
}
/** {@inheritDoc} */
@Override public void run() {
try {
GridCacheSet<String> set = Ignition.ignite().cache(cacheName).dataStructures().set(setName, false, true);
UUID locId = Ignition.ignite().cluster().localNode().id();
for (int i = 0; i < 5; i++) {
String item = locId + "_" + Integer.toString(i);
set.add(item);
System.out.println("Set item has been added: " + item);
}
}
catch (IgniteCheckedException e) {
throw new RuntimeException(e);
}
}
}
}