View Javadoc
1   /*
2    * Copyright 2016 The Lannister Project
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.anyflow.lannister.cluster;
17  
18  import java.util.Set;
19  import java.util.stream.Collectors;
20  
21  import javax.cache.Cache;
22  
23  import org.apache.ignite.IgniteCache;
24  import org.apache.ignite.cache.query.ScanQuery;
25  
26  public class IgniteMap<K, V> implements Map<K, V> {
27  
28  	private IgniteCache<K, V> engine;
29  
30  	public IgniteMap(String name) {
31  		engine = Ignite.INSTANCE.getCache(name);
32  	}
33  
34  	@Override
35  	public void put(K key, V value) {
36  		engine.put(key, value);
37  	}
38  
39  	@Override
40  	public V get(K key) {
41  		return engine.get(key);
42  	}
43  
44  	@Override
45  	public V remove(K key) {
46  		return engine.getAndRemove(key);
47  	}
48  
49  	@Override
50  	public Set<K> keySet() {
51  		return engine.query(new ScanQuery<K, V>()).getAll().stream().map(Cache.Entry::getKey)
52  				.collect(Collectors.toSet());
53  	}
54  
55  	@Override
56  	public int size() {
57  		return engine.size();
58  	}
59  
60  	@Override
61  	public void dispose() {
62  		engine.destroy();
63  	}
64  
65  	@Override
66  	public boolean containsKey(K key) {
67  		return engine.containsKey(key);
68  	}
69  }