use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class JettyRestProcessorAbstractSelfTest method testTypedGet.
/**
* @throws Exception If failed.
*/
public void testTypedGet() throws Exception {
// Test boolean type.
IgniteCache<Boolean, Boolean> cBool = typedCache();
cBool.put(true, false);
cBool.put(false, true);
getTypedValue("boolean", "true", "false");
getTypedValue("java.lang.Boolean", "false", "true");
// Test byte type.
IgniteCache<Byte, Byte> cByte = typedCache();
cByte.put((byte) 77, (byte) 55);
cByte.put((byte) -88, (byte) -10);
getTypedValue("byte", "77", "55");
getTypedValue("java.lang.Byte", "-88", "-10");
// Test short type.
IgniteCache<Short, Short> cShort = typedCache();
cShort.put((short) 2222, (short) 3333);
cShort.put((short) -11111, (short) -12222);
getTypedValue("short", "2222", "3333");
getTypedValue("java.lang.Short", "-11111", "-12222");
// Test integer type.
IgniteCache<Integer, Integer> cInt = typedCache();
cInt.put(65555, 128256);
cInt.put(74555, 200000);
cInt.put(-200, -100000);
getTypedValue("int", "65555", "128256");
getTypedValue("Integer", "74555", "200000");
getTypedValue("java.lang.Integer", "-200", "-100000");
// Test long type.
IgniteCache<Long, Long> cLong = typedCache();
cLong.put(3333333L, 4444444L);
cLong.put(-3333333L, -4444444L);
getTypedValue("long", "3333333", "4444444");
getTypedValue("java.lang.Long", "-3333333", "-4444444");
// Test float type.
IgniteCache<Float, Float> cFloat = typedCache();
cFloat.put(11.5f, 21.5f);
cFloat.put(-71.5f, -81.5f);
getTypedValue("float", "11.5", "21.5");
getTypedValue("java.lang.Float", "-71.5", "-81.5");
// Test double type.
IgniteCache<Double, Double> cDouble = typedCache();
cDouble.put(58.5d, 758.5d);
cDouble.put(-1558.5d, -2558.5d);
getTypedValue("double", "58.5", "758.5");
getTypedValue("java.lang.Double", "-1558.5", "-2558.5");
// Test date type.
IgniteCache<Date, Date> cDate = typedCache();
cDate.put(Date.valueOf("2018-02-18"), Date.valueOf("2017-01-01"));
cDate.put(Date.valueOf("2018-01-01"), Date.valueOf("2017-02-02"));
getTypedValue("Date", "2018-02-18", "2017-01-01");
getTypedValue("java.sql.Date", "2018-01-01", "2017-02-02");
// Test time type.
IgniteCache<Time, Time> cTime = typedCache();
cTime.put(Time.valueOf("01:01:01"), Time.valueOf("02:02:02"));
cTime.put(Time.valueOf("03:03:03"), Time.valueOf("04:04:04"));
getTypedValue("Time", "01:01:01", "02:02:02");
getTypedValue("java.sql.Time", "03:03:03", "04:04:04");
// Test timestamp type.
IgniteCache<Timestamp, String> cTimestamp = typedCache();
cTimestamp.put(Timestamp.valueOf("2018-02-18 01:01:01"), "test1");
cTimestamp.put(Timestamp.valueOf("2018-01-01 01:01:01"), "test2");
getTypedValue("Timestamp", "2018-02-18%2001:01:01", "test1");
getTypedValue("java.sql.timestamp", "2018-01-01%2001:01:01", "test2");
// Test UUID type.
IgniteCache<UUID, UUID> cUUID = typedCache();
UUID k1 = UUID.fromString("121f5ae8-148d-11e8-b642-0ed5f89f718b");
UUID v1 = UUID.fromString("64c6c225-b31c-4000-b136-ef14562ac785");
cUUID.put(k1, v1);
UUID k2 = UUID.randomUUID();
UUID v2 = UUID.randomUUID();
cUUID.put(k2, v2);
getTypedValue("UUID", k1.toString(), v1.toString());
getTypedValue("java.util.UUID", k2.toString(), v2.toString());
// Test IgniteUuid type.
IgniteCache<IgniteUuid, IgniteUuid> cIgniteUUID = typedCache();
IgniteUuid ik1 = IgniteUuid.randomUuid();
IgniteUuid iv1 = IgniteUuid.randomUuid();
cIgniteUUID.put(ik1, iv1);
IgniteUuid ik2 = IgniteUuid.randomUuid();
IgniteUuid iv2 = IgniteUuid.randomUuid();
cIgniteUUID.put(ik2, iv2);
getTypedValue("IgniteUuid", ik1.toString(), iv1.toString());
getTypedValue("org.apache.ignite.lang.IgniteUuid", ik2.toString(), iv2.toString());
// Test tuple.
IgniteCache<Integer, T2<Integer, String>> cTuple = typedCache();
T2<Integer, String> tup = new T2<>(1, "test");
cTuple.put(555, tup);
getTypedValue("int", "555", JSON_MAPPER.writeValueAsString(tup));
// Test enum.
IgniteCache<Integer, CacheMode> cEnum = typedCache();
cEnum.put(888, PARTITIONED);
getTypedValue("int", "888", PARTITIONED.toString());
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class GridCacheNearMultiNodeSelfTest method testMappings.
/**
* Test mappings.
*/
public void testMappings() {
mapDebug = false;
int cnt = 100000;
Map<UUID, T2<Set<Integer>, Set<Integer>>> map = mapKeys(cnt);
for (ClusterNode n : grid(0).cluster().nodes()) {
Set<Integer> primary = map.get(n.id()).get1();
Set<Integer> backups = map.get(n.id()).get2();
if (backups == null)
backups = Collections.emptySet();
info("Grid node [primaries=" + primary.size() + ", backups=" + backups.size() + ']');
assert !F.isEmpty(primary);
assertEquals(backups.size(), cnt - primary.size());
}
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class IgniteCacheExpireAndUpdateConsistencyTest method updateAndEventConsistencyTest.
/**
* @param node Node.
* @param cacheName Cache name.
* @param keyVal Key counter.
* @param nodesEvts Events map.
* @param useTx If {@code true} executes update with explicit transaction.
* @throws Exception If failed.
*/
private void updateAndEventConsistencyTest(final Ignite node, String cacheName, final AtomicInteger keyVal, List<ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>>> nodesEvts, final boolean useTx) throws Exception {
final ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> updates = new ConcurrentHashMap<>();
final int THREADS = 5;
final int KEYS_PER_THREAD = 100;
final IgniteCache<TestKey, TestValue> cache = node.cache(cacheName);
final IgniteCache<TestKey, TestValue> expPlcCache = cache.withExpiryPolicy(new CreatedExpiryPolicy(new Duration(SECONDS, 2)));
GridTestUtils.runMultiThreaded(new IgniteInClosure<Integer>() {
@Override
public void apply(Integer idx) {
List<TestKey> keys = new ArrayList<>();
for (int i = 0; i < KEYS_PER_THREAD; i++) keys.add(new TestKey(keyVal.incrementAndGet()));
for (TestKey key : keys) {
expPlcCache.put(key, new TestValue(0));
List<T2<TestValue, TestValue>> keyUpdates = new ArrayList<>();
keyUpdates.add(new T2<>(new TestValue(0), (TestValue) null));
updates.put(key, keyUpdates);
}
long stopTime = U.currentTimeMillis() + 10_000;
int val = 0;
Set<TestKey> expired = new HashSet<>();
IgniteTransactions txs = node.transactions();
while (U.currentTimeMillis() < stopTime) {
val++;
TestValue newVal = new TestValue(val);
for (TestKey key : keys) {
Transaction tx = useTx ? txs.txStart(PESSIMISTIC, REPEATABLE_READ) : null;
TestValue oldVal = cache.getAndPut(key, newVal);
if (tx != null)
tx.commit();
List<T2<TestValue, TestValue>> keyUpdates = updates.get(key);
keyUpdates.add(new T2<>(newVal, oldVal));
if (oldVal == null)
expired.add(key);
}
if (expired.size() == keys.size())
break;
}
assertEquals(keys.size(), expired.size());
}
}, THREADS, "update-thread");
for (ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> evts : nodesEvts) checkEvents(updates, evts);
nodesEvts.clear();
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class IgniteCacheExpireAndUpdateConsistencyTest method updateAndEventConsistencyTest.
/**
* @param ccfg Cache configuration.
* @throws Exception If failed.
*/
@SuppressWarnings("SynchronizationOnLocalVariableOrMethodParameter")
private void updateAndEventConsistencyTest(CacheConfiguration<TestKey, TestValue> ccfg) throws Exception {
ignite(0).createCache(ccfg);
try {
List<ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>>> nodesEvts = new ArrayList<>();
for (int i = 0; i < NODES; i++) {
Ignite ignite = ignite(i);
IgniteCache<TestKey, TestValue> cache = ignite.cache(ccfg.getName());
ContinuousQuery<TestKey, TestValue> qry = new ContinuousQuery<>();
final ConcurrentMap<TestKey, List<T2<TestValue, TestValue>>> allEvts = new ConcurrentHashMap<>();
qry.setLocalListener(new CacheEntryUpdatedListener<TestKey, TestValue>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends TestKey, ? extends TestValue>> evts) {
for (CacheEntryEvent<? extends TestKey, ? extends TestValue> e : evts) {
List<T2<TestValue, TestValue>> keyEvts = allEvts.get(e.getKey());
if (keyEvts == null) {
List<T2<TestValue, TestValue>> old = allEvts.putIfAbsent(e.getKey(), keyEvts = new ArrayList<>());
assertNull(old);
}
synchronized (keyEvts) {
keyEvts.add(new T2<TestValue, TestValue>(e.getValue(), e.getOldValue()));
}
}
}
});
cache.query(qry);
nodesEvts.add(allEvts);
}
final AtomicInteger keyVal = new AtomicInteger();
for (int i = 0; i < NODES; i++) {
Ignite ignite = ignite(i);
log.info("Test with node: " + ignite.name());
updateAndEventConsistencyTest(ignite, ccfg.getName(), keyVal, nodesEvts, false);
if (ccfg.getAtomicityMode() == TRANSACTIONAL)
updateAndEventConsistencyTest(ignite, ccfg.getName(), keyVal, nodesEvts, true);
}
} finally {
ignite(0).destroyCache(ccfg.getName());
}
}
use of org.apache.ignite.internal.util.typedef.T2 in project ignite by apache.
the class PageIdDistributionTest method _testRealHistory.
/**
* Uncomment and run this test manually to get data to plot histogram for per-element distance from ideal.
* You can use Octave to plot the histogram:
* <pre>
* all = csvread("histo.txt");
* hist(all, 200)
* </pre>
*
* @throws Exception If failed.
*/
public void _testRealHistory() throws Exception {
int capacity = CACHE_IDS.length * PARTS * PAGES;
info("Capacity: " + capacity);
long mem = FullPageIdTable.requiredMemory(capacity);
info(U.readableSize(mem, true));
UnsafeMemoryProvider prov = new UnsafeMemoryProvider(new JavaLogger());
prov.initialize(new long[] { mem });
DirectMemoryRegion region = prov.nextRegion();
try {
long seed = U.currentTimeMillis();
info("Seed: " + seed + "L; //");
Random rnd = new Random(seed);
FullPageIdTable tbl = new FullPageIdTable(region.address(), region.size(), true);
Map<T2<Integer, Integer>, Integer> allocated = new HashMap<>();
for (int i = 0; i < capacity; i++) {
int cacheId = CACHE_IDS[rnd.nextInt(CACHE_IDS.length)];
int partId = rnd.nextInt(PARTS);
T2<Integer, Integer> key = new T2<>(cacheId, partId);
Integer pageIdx = allocated.get(key);
pageIdx = pageIdx == null ? 1 : pageIdx + 1;
if (pageIdx > PAGES)
continue;
tbl.put(cacheId, PageIdUtils.pageId(partId, (byte) 0, pageIdx), 1, 0);
allocated.put(key, pageIdx);
if (i > 0 && i % 100_000 == 0)
info("Done: " + i);
}
int[] scans = new int[capacity];
int cur = 0;
for (T2<Integer, Integer> key : allocated.keySet()) {
Integer alloc = allocated.get(key);
if (alloc != null) {
for (int idx = 1; idx <= alloc; idx++) {
scans[cur] = tbl.distanceFromIdeal(key.get1(), PageIdUtils.pageId(key.get2(), (byte) 0, idx), 0);
assert scans[cur] != -1;
cur++;
}
}
}
try (FileOutputStream out = new FileOutputStream("histo.txt")) {
PrintWriter w = new PrintWriter(new OutputStreamWriter(out));
for (int scan : scans) {
if (scan != 0)
w.println(scan);
}
w.flush();
}
} finally {
prov.shutdown();
}
}
Aggregations