@Override public Map<String, Object> serializeBean() { return U.map("relation", name, "ids", ids); }
public static Database db() { assert U.must(db != null, "Database not initialized!"); return db; }
@Override public T get() { if (freeN == 0) { try { instancesN++; return factory.call(); } catch (Exception e) { throw U.rte(e); } } else { T obj = free[--freeN]; assert obj != null; return obj; } }
public DbColumn<?> column(Method method) { U.must(DbColumn.class.isAssignableFrom(method.getReturnType())); String name = method.getName(); DbColumn<?> res = columns.get(name); if (res == null) { Class<Object> colType = Cls.clazz(Cls.generic(method.getGenericReturnType()).getActualTypeArguments()[0]); DbColumn<Object> value = DB.column(values, method.getName(), colType); DbColumn<?> old = columns.putIfAbsent(name, value); return U.or(old, value); } return res; }
@Override public Object entity(String rql, Object... args) { String[] parts = rql.split(" "); String entityName = U.capitalized(parts[0]); Class<?> entityType = getEntityType(entityName); U.must(entityType != null, "Cannot find entity '%s'!", entityName); Map<String, Object> properties = U.map(); if (parts.length > 1) { String[] props = rql.substring(entityName.length() + 1).split("\\s*\\,\\s*"); int argIndex = 0; for (String prop : props) { String[] kv = prop.trim().split("\\s*=\\s*"); String key = kv[0]; Object value; if (kv.length > 1) { value = kv[1].equals("?") ? args[argIndex++] : kv[1]; } else { value = true; } properties.put(key, value); } } return entity(entityType, properties); }
public Map<String, Object> loadMetadata(InputStream in) { globalLock(); try { BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = reader.readLine(); U.must(line != null, "Missing meta-data at the first line in the database file!"); Map<String, Object> meta = U.map(); data.serializer.deserialize(line.getBytes(), meta); reader.close(); return meta; } catch (IOException e) { throw new RuntimeException("Cannot load meta-data from database!", e); } finally { globalUnlock(); } }
public static Date date(String value) { if (U.isEmpty(value)) { return null; } String[] parts = value.split("(\\.|-|/)"); int a = parts.length > 0 ? U.num(parts[0]) : -1; int b = parts.length > 1 ? U.num(parts[1]) : -1; int c = parts.length > 2 ? U.num(parts[2]) : -1; switch (parts.length) { case 3: if (isDay(a) && isMonth(b) && isYear(c)) { return date(a, b, c); } else if (isYear(a) && isMonth(b) && isDay(c)) { return date(c, b, a); } break; case 2: if (isDay(a) && isMonth(b)) { return date(a, b, thisYear()); } break; default: } throw U.rte("Invalid date: " + value); }
public static void initRelations(Object target) { for (Prop prop : Beany.propertiesOf(target.getClass()).select(ANNOTATED_DB_REL_PROPS)) { Rel rel = prop.getAnnotation(Rel.class); U.must(!U.isEmpty(rel.value()), "Relation name must be specified!"); Object value = prop.getRaw(target); if (value == null && !prop.isReadOnly()) { Class<?> type = prop.getType(); if (DbList.class.equals(type)) { prop.setRaw(target, DB.list(target, rel.value())); } else if (DbSet.class.equals(type)) { prop.setRaw(target, DB.set(target, rel.value())); } else if (DbRef.class.equals(type)) { prop.setRaw(target, DB.ref(target, rel.value())); } } } }
Long oldVersion = U.or(Beany.getPropValueOfType(entity, VERSION, Long.class, null), 0L); Long recordVersion = U.or(Beany.getPropValueOfType(record, VERSION, Long.class, null), 0L); occErrorIf(!U.eq(oldVersion, recordVersion), "Concurrent modification occured while updating the object with ID=%s!", id); U.secure(canUpdate, "Not enough privileges to update any column of %s!", entity.getClass().getSimpleName());
public static int indexOf(Object[] arr, Object value) { for (int i = 0; i < arr.length; i++) { if (U.eq(arr[i], value)) { return i; } } return -1; }
@SuppressWarnings("unchecked") @Override public synchronized <T> List<T> _list(Object key) { _extras(); if (!extras.containsKey(key)) { extras.put(key, U.list()); } return (List<T>) extras.get(key); }
byte[] bytes = line.getBytes(); U.must(line != null, "Missing meta-data at the first line in the database file!"); Map<String, Object> meta = U.map(); data.serializer.deserialize(bytes, meta); Map<String, Object> map = U.map(); data.serializer.deserialize(bytes, map); U.must(idNum != null, "Found DB record without ID: %s", line); long id = idNum.longValue(); String className = ((String) map.get("_class"));
@Override public boolean eq(Object obj) { return U.eq(get(), obj); }
@Override protected List<Item> data() { List<T> all = DB.find(beanType, match, orderBy); List<Item> records = U.list(); for (T t : all) { records.add(Models.item(t)); } return records; }
@Override public synchronized Map<Object, Object> _extras() { if (extras == null) { extras = Collections.synchronizedMap(U.map()); } return extras; }
public static <T> T[] subarray(T[] arr, int from, int to) { int start = from >= 0 ? from : arr.length + from; int end = to >= 0 ? to : arr.length + to; if (start < 0) { start = 0; } if (end > arr.length - 1) { end = arr.length - 1; } U.must(start <= end, "Invalid range: expected form <= to!"); int size = end - start + 1; T[] part = Arrays.copyOf(arr, size); System.arraycopy(arr, start, part, 0, size); return part; }
@SuppressWarnings("unchecked") @Override public <E> Class<E> getEntityTypeFor(Class<E> clazz) { if (IEntity.class.isAssignableFrom(clazz)) { if (Proxy.class.isAssignableFrom(clazz)) { for (Class<?> interf : clazz.getInterfaces()) { if (IEntity.class.isAssignableFrom(interf)) { return (Class<E>) interf; } } throw U.rte("Cannot find entity type for: %s!", clazz); } } return clazz; }
@SuppressWarnings("unchecked") @Override public void deserializeBean(Map<String, Object> serialized) { if (serialized != null) { name = (String) serialized.get("relation"); U.notNull(name, "relation"); Collection<? extends Number> initialIds = (Collection<? extends Number>) serialized.get("ids"); U.notNull(initialIds, "ids"); initIds(initialIds); } }