Skip to content

Instantly share code, notes, and snippets.

@gurbuzali
Created May 14, 2015 09:11
Show Gist options
  • Save gurbuzali/6cae060b81fab051040f to your computer and use it in GitHub Desktop.
Save gurbuzali/6cae060b81fab051040f to your computer and use it in GitHub Desktop.
/*
* Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
*
* Licensed 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 coh;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.query.PagingPredicate;
import com.hazelcast.query.Predicate;
import com.hazelcast.query.impl.QueryEntry;
import java.io.Serializable;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* TODO add a proper JavaDoc
*/
public class HazMain {
public static final int DATA_COUNT = 200 * 100;
static IMap m;
static MyComparator comparator;
static PagingPredicate pagingPredicate;
public static void main(String[] args) {
Hazelcast.newHazelcastInstance();
Hazelcast.newHazelcastInstance();
HazelcastInstance instance = Hazelcast.newHazelcastInstance();
m = instance.getMap("m");
for (int i = 0; i < HazMain.DATA_COUNT; i++) {
if (i % 25000 == 0) {
System.err.println("asd i: " + i);
}
m.put(i, i);
}
System.err.println("fatal data filled !!!");
sleepSeconds(2);
comparator = new MyComparator();
MyPredicate predicate = new MyPredicate();
pagingPredicate = new PagingPredicate(predicate, comparator, 50);
getPage(0);
getPage(1);
getPage(2);
getPage(10);
getPage(50);
getPage(1500);
}
static void getPage(int page) {
pagingPredicate.setPage(page);
long begin = System.nanoTime();
Collection values = m.values(pagingPredicate);
long elapsed = System.nanoTime() - begin;
System.err.println("fatal page: " + page + " elapsed : " + (elapsed / 1000000));
printSet(values);
}
static void printSet(Collection values) {
for (Object o : values) {
System.err.println("qwe \t " + o);
}
System.err.println("");
}
static void sleepSeconds(int seconds) {
try {
Thread.sleep(seconds * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static class MyPredicate implements Predicate, Serializable {
public MyPredicate() {
}
public boolean apply(Map.Entry mapEntry) {
return true;
}
}
public static class MyComparator implements Comparator, Serializable {
public int compare(Object o1, Object o2) {
Comparable v1 = (Comparable) ((Map.Entry) o1).getValue();
Comparable v2 = (Comparable) ((Map.Entry) o2).getValue();
return (v1).compareTo(v2);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment