IGNITE-5075: Fix to Yardstick in for cache groups.
[ignite.git] / modules / yardstick / src / main / java / org / apache / ignite / yardstick / cache / load / model / ModelUtil.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 package org.apache.ignite.yardstick.cache.load.model;
19
20 import java.util.UUID;
21 import org.apache.ignite.yardstick.cache.load.model.key.Identifier;
22 import org.apache.ignite.yardstick.cache.load.model.key.Mark;
23 import org.apache.ignite.yardstick.cache.load.model.value.Car;
24 import org.apache.ignite.yardstick.cache.load.model.value.Color;
25 import org.apache.ignite.yardstick.cache.load.model.value.Truck;
26 import org.apache.ignite.yardstick.cache.model.Account;
27 import org.apache.ignite.yardstick.cache.model.Organization;
28 import org.apache.ignite.yardstick.cache.model.Person;
29 import org.apache.ignite.yardstick.cache.model.Person1;
30 import org.apache.ignite.yardstick.cache.model.Person2;
31 import org.apache.ignite.yardstick.cache.model.Person8;
32
33 /**
34 * Util class for model.
35 */
36 public class ModelUtil {
37 /**
38 * Classes of keys.
39 */
40 private static Class[] keyClasses = {
41 Integer.class,
42 Double.class,
43 Identifier.class,
44 Mark.class,
45 Integer.class,
46 UUID.class,
47 String.class
48 };
49
50 /**
51 * Classes of values.
52 */
53 private static Class[] valClasses = {
54 // Car.class,
55 // Truck.class,
56 Person.class,
57 Organization.class,
58 Account.class,
59 Person1.class,
60 Person2.class,
61 Person8.class
62 };
63
64 /**
65 * Simple of classes.
66 * Upper approximate size of value 24 bytes.
67 */
68 private static Class[] simpleClasses = {
69 Account.class,
70 Person1.class,
71 Person2.class,
72 };
73
74 /**
75 * Fat of classes.
76 * Upper approximate size of value 128 bytes.
77 */
78 private static Class[] fatClasses = {
79 Person.class,
80 Person8.class,
81 Organization.class
82 };
83
84 /**
85 * @param clazz Checked class.
86 * @return Result.
87 */
88 public static boolean canCreateInstance(Class clazz) {
89 for (Class c: keyClasses) {
90 if (c.equals(clazz))
91 return true;
92 }
93
94 for (Class c: valClasses) {
95 if (c.equals(clazz))
96 return true;
97 }
98
99 return false;
100 }
101
102 /**
103 * @param c model class
104 * @param id object id
105 * @return object from model
106 */
107 public static Object create(Class c, int id) {
108 Object res = null;
109
110 switch (c.getSimpleName()) {
111 case "Double":
112 res = id;
113 break;
114 case "Identifier":
115 res = new Identifier(id, "id " + id);
116 break;
117 case "Mark":
118 res = new Mark(id, UUID.nameUUIDFromBytes(Integer.toString(id).getBytes()));
119 break;
120 case "Integer":
121 res = id;
122 break;
123 case "UUID":
124 res = UUID.nameUUIDFromBytes(Integer.toString(id).getBytes());
125 break;
126 case "Car":
127 int colorCnt = Color.values().length;
128 res = new Car(id, "Mark " + id, id / 2.123 * 100, Color.values()[id % colorCnt]);
129 break;
130 case "Truck":
131 int colors = Color.values().length;
132 res = new Truck(id, "Mark " + id, id / 2.123 * 100, Color.values()[id % colors], id / 4.123 * 100);
133 break;
134 case "Person":
135 res = new Person(id, id+1, "First Name " + id, "Last Name " + id, id / 2.123 * 100);
136 break;
137 case "Organization":
138 res = new Organization(id, "Organization " + id);
139 break;
140 case "Account":
141 res = new Account(id);
142 break;
143 case "Person1":
144 res = new Person1(id);
145 break;
146 case "Person2":
147 res = new Person2(id);
148 break;
149 case "Person8":
150 res = new Person8(id);
151 break;
152 case "String":
153 res = String.valueOf(id);
154 break;
155 default:
156 throw new IllegalArgumentException("Unsupported class: " + c.getSimpleName());
157 }
158
159 return res;
160 }
161
162 /**
163 * @return array of key cache classes
164 */
165 public static Class[] keyClasses() {
166 return keyClasses;
167 }
168
169 /**
170 * @return Array of cache value classes
171 */
172 public static Class[] valueClasses() {
173 return valClasses;
174 }
175
176 /**
177 * @return Array of cache value simple classes.
178 */
179 public static Class[] simpleValueClasses() {
180 return simpleClasses;
181 }
182
183 /**
184 * @return Array of cache value fat classes.
185 */
186 public static Class[] fatValueClasses() {
187 return fatClasses;
188 }
189 }