@Override public Map<Long, Long> findStoragePoolHostsByDriver(long clusterId, Long storageDriverId) { final Map<Long, Long> result = new HashMap<>(); create().select(HOST.ID, STORAGE_POOL.ID) .from(HOST) .leftOuterJoin(STORAGE_POOL_HOST_MAP) .on(STORAGE_POOL_HOST_MAP.HOST_ID.eq(HOST.ID)) .leftOuterJoin(STORAGE_POOL) .on(STORAGE_POOL_HOST_MAP.STORAGE_POOL_ID.eq(STORAGE_POOL.ID) .and(STORAGE_POOL.STORAGE_DRIVER_ID.eq(storageDriverId))) .where(STORAGE_POOL.REMOVED.isNull() .and(STORAGE_POOL_HOST_MAP.REMOVED.isNull()) .and(HOST.CLUSTER_ID.eq(clusterId)) .and(HOST.REMOVED.isNull())) .fetchInto((RecordHandler<Record2<Long, Long>>) record -> { Long hostId = record.getValue(HOST.ID); Long storagePoolId = record.getValue(STORAGE_POOL.ID); if (!result.containsKey(hostId) || storagePoolId != null) { result.put(hostId, storagePoolId); } }); return result; }
@Override public Map<Long, List<Object>> findHostsForPools(List<Long> ids, final IdFormatter idFormatter) { final Map<Long, List<Object>> result = new HashMap<>(); create().select(STORAGE_POOL_HOST_MAP.STORAGE_POOL_ID, STORAGE_POOL_HOST_MAP.HOST_ID) .from(STORAGE_POOL_HOST_MAP) .where(STORAGE_POOL_HOST_MAP.REMOVED.isNull() .and(STORAGE_POOL_HOST_MAP.STORAGE_POOL_ID.in(ids))) .fetchInto(new RecordHandler<Record2<Long, Long>>() { @Override public void next(Record2<Long, Long> record) { Long hostId = record.getValue(STORAGE_POOL_HOST_MAP.HOST_ID); Long storagePoolId = record.getValue(STORAGE_POOL_HOST_MAP.STORAGE_POOL_ID); List<Object> pools = result.get(storagePoolId); if (pools == null) { pools = new ArrayList<>(); result.put(storagePoolId, pools); } pools.add(idFormatter.formatId(HostConstants.TYPE, hostId)); } }); return result; }
@Override public Map<Long, List<Object>> findVolumesForPools(List<Long> ids, final IdFormatter idFormatter) { final Map<Long, List<Object>> result = new HashMap<>(); create().select(VOLUME.STORAGE_POOL_ID, VOLUME.ID) .from(VOLUME) .join(STORAGE_POOL) .on(STORAGE_POOL.ID.eq(VOLUME.STORAGE_POOL_ID)) .where(STORAGE_POOL.KIND.ne("docker") .and(VOLUME.STORAGE_POOL_ID.in(ids))) .fetchInto(new RecordHandler<Record2<Long, Long>>() { @Override public void next(Record2<Long, Long> record) { Long volumeId = record.getValue(VOLUME.ID); Long storagePoolId = record.getValue(VOLUME.STORAGE_POOL_ID); List<Object> pools = result.get(storagePoolId); if (pools == null) { pools = new ArrayList<>(); result.put(storagePoolId, pools); } pools.add(idFormatter.formatId(VolumeConstants.TYPE, volumeId)); } }); return result; }
@Override public Map<Long, List<Object>> getInstancesPerHost(List<Long> hosts, final IdFormatter idFormatter) { final Map<Long, List<Object>> result = new HashMap<>(); create().select(INSTANCE.ID, INSTANCE.HOST_ID) .from(INSTANCE) .where(INSTANCE.HOST_ID.in(hosts) .and(INSTANCE.REMOVED.isNull())) .fetchInto((RecordHandler<Record2<Long, Long>>) record -> { Long hostId = record.getValue(INSTANCE.HOST_ID); Long instanceId = record.getValue(INSTANCE.ID); List<Object> list = result.get(hostId); if (list == null) { list = new ArrayList<>(); result.put(hostId, list); } list.add(idFormatter.formatId(InstanceConstants.TYPE, instanceId)); }); return result; }
@Override public Map<Long, Pair<String, String>> getServiceIdToServiceStackName(long accountId) { final Map<Long, Pair<String, String>> toReturn = new HashMap<>(); create().select(SERVICE.ID, SERVICE.NAME, STACK.NAME) .from(SERVICE) .join(STACK) .on(STACK.ID.eq(SERVICE.STACK_ID)) .where(SERVICE.ACCOUNT_ID.eq(accountId)) .and(SERVICE.REMOVED.isNull()) .fetchInto(new RecordHandler<Record3<Long, String, String>>() { @Override public void next(Record3<Long, String, String> record) { toReturn.put(record.getValue(SERVICE.ID), Pair.of(record.getValue(SERVICE.NAME), record.getValue(STACK.NAME))); } }); return toReturn; }
@Override public Map<Long, List<Object>> getServicesForStack(List<Long> ids, final IdFormatter idFormatter) { final Map<Long, List<Object>> result = new HashMap<>(); create().select(SERVICE.ID, SERVICE.STACK_ID) .from(SERVICE) .where(SERVICE.STACK_ID.in(ids) .and(SERVICE.REMOVED.isNull())) .fetchInto((RecordHandler<Record2<Long, Long>>) record -> { Long id = record.getValue(SERVICE.ID); Long stackId = record.getValue(SERVICE.STACK_ID); List<Object> list = result.get(stackId); if (list == null) { list = new ArrayList<>(); result.put(stackId, list); } list.add(idFormatter.formatId(ServiceConstants.KIND_SERVICE, id)); }); return result; }
@Override public Set<Long> findHostsWithVolumeInUse(long volumeId) { Set<Long> result = new HashSet<>(); create() .select(HOST.ID) .from(INSTANCE) .join(MOUNT) .on(MOUNT.INSTANCE_ID.eq(INSTANCE.ID).and(MOUNT.VOLUME_ID.eq(volumeId))) .where(INSTANCE.REMOVED.isNull() .and(INSTANCE.HOST_ID.isNotNull()) .and((INSTANCE.HEALTH_STATE.isNull().or(INSTANCE.HEALTH_STATE.eq(HealthcheckConstants.HEALTH_STATE_HEALTHY))))) .fetchInto((RecordHandler<Record1<Long>>) record -> result.add(record.getValue(HOST.ID))); return result; }
@Override public List<ProcessReference> pendingTasks() { final List<ProcessReference> result = new ArrayList<>(); create() .select(PROCESS_INSTANCE.ID, PROCESS_INSTANCE.PROCESS_NAME, PROCESS_INSTANCE.RESOURCE_TYPE, PROCESS_INSTANCE.RESOURCE_ID, PROCESS_INSTANCE.ACCOUNT_ID, PROCESS_INSTANCE.CLUSTER_ID) .from(PROCESS_INSTANCE) .where(PROCESS_INSTANCE.END_TIME.isNull() .and(PROCESS_INSTANCE.RUN_AFTER.isNull().or(PROCESS_INSTANCE.RUN_AFTER.le(new Date())))) .limit(BATCH.get()) .fetchInto((RecordHandler<Record6<Long, String, String, String, Long, Long>>) record -> { ProcessReference ref = new ProcessReference( record.getValue(PROCESS_INSTANCE.ID), record.getValue(PROCESS_INSTANCE.PROCESS_NAME), record.getValue(PROCESS_INSTANCE.RESOURCE_TYPE), record.getValue(PROCESS_INSTANCE.RESOURCE_ID), record.getValue(PROCESS_INSTANCE.ACCOUNT_ID), record.getValue(PROCESS_INSTANCE.CLUSTER_ID)); result.add(ref); }); return result; }
@Override public Map<Long, String> getInstanceIdToInstanceName(long accountId) { final Map<Long, String> toReturn = new HashMap<>(); create().select(INSTANCE.ID, INSTANCE.NAME) .from(INSTANCE) .where(INSTANCE.ACCOUNT_ID.eq(accountId)) .and(INSTANCE.REMOVED.isNull()) .fetchInto(new RecordHandler<Record2<Long, String>>() { @Override public void next(Record2<Long, String> record) { toReturn.put(record.getValue(INSTANCE.ID), record.getValue(INSTANCE.NAME)); } }); return toReturn; }
@Override public Map<Long, String> getCertificateIdToCertificate(long accountId) { final Map<Long, String> toReturn = new HashMap<>(); create().select(CERTIFICATE.ID, CERTIFICATE.NAME) .from(CERTIFICATE) .where(CERTIFICATE.ACCOUNT_ID.eq(accountId)) .and(CERTIFICATE.REMOVED.isNull()) .fetchInto(new RecordHandler<Record2<Long, String>>() { @Override public void next(Record2<Long, String> record) { toReturn.put(record.getValue(CERTIFICATE.ID), record.getValue(CERTIFICATE.NAME)); } }); return toReturn; }
@Override public Map<Long, Collection<MountEntry>> getMountsForInstances(List<Long> ids, final IdFormatter idF) { ListValuedMap<Long, MountEntry> result = new ArrayListValuedHashMap<>(); create().select(INSTANCE.NAME, VOLUME.NAME, VOLUME.ID, MOUNT.PERMISSIONS, MOUNT.PATH, MOUNT.INSTANCE_ID) .from(MOUNT) .join(VOLUME) .on(VOLUME.ID.eq(MOUNT.VOLUME_ID)) .join(INSTANCE) .on(INSTANCE.ID.eq(MOUNT.INSTANCE_ID)) .where(MOUNT.REMOVED.isNull() .and(VOLUME.REMOVED.isNull()) .and(MOUNT.INSTANCE_ID.in(ids))) .fetchInto((RecordHandler<Record6<String, String, Long, String, String, Long>>) record -> { Long instanceId = record.getValue(MOUNT.INSTANCE_ID); MountEntry mount = new MountEntry(); mount.setInstanceName(record.getValue(INSTANCE.NAME)); mount.setInstanceId(idF.formatId(InstanceConstants.TYPE, instanceId)); mount.setPath(record.getValue(MOUNT.PATH)); mount.setPermission(record.getValue(MOUNT.PERMISSIONS)); mount.setVolumeId(idF.formatId(VolumeConstants.TYPE, record.getValue(VOLUME.ID))); mount.setVolumeName(record.getValue(VOLUME.NAME)); result.put(instanceId, mount); }); return result.asMap(); }
@Override public Map<Long, Collection<MountEntry>> getMountsForVolumes(List<Long> ids, final IdFormatter idF) { ListValuedMap<Long, MountEntry> result = new ArrayListValuedHashMap<>(); create().select(VOLUME.NAME, MOUNT.PERMISSIONS, MOUNT.PATH, MOUNT.INSTANCE_ID, MOUNT.VOLUME_ID, INSTANCE.NAME) .from(MOUNT) .join(INSTANCE) .on(INSTANCE.ID.eq(MOUNT.INSTANCE_ID)) .join(VOLUME) .on(VOLUME.ID.eq(MOUNT.VOLUME_ID)) .where(INSTANCE.REMOVED.isNull() .and(VOLUME.REMOVED.isNull()) .and(MOUNT.VOLUME_ID.in(ids))) .fetchInto((RecordHandler<Record6<String, String, String, Long, Long, String>>) record -> { Long volumeId = record.getValue(MOUNT.VOLUME_ID); MountEntry mount = new MountEntry(); mount.setInstanceName(record.getValue(INSTANCE.NAME)); mount.setInstanceId(idF.formatId(InstanceConstants.TYPE, record.getValue(MOUNT.INSTANCE_ID))); mount.setPath(record.getValue(MOUNT.PATH)); mount.setPermission(record.getValue(MOUNT.PERMISSIONS)); mount.setVolumeId(idF.formatId(VolumeConstants.TYPE, volumeId)); mount.setVolumeName(record.getValue(VOLUME.NAME)); result.put(volumeId, mount); }); return result.asMap(); }
@SuppressWarnings("unchecked") @Override public Set<String> getMappedUuids(String uuid, Class<?> resourceType, Class<?> mappingType, Class<?> otherType) { Table<?> resourceTable = JooqUtils.getTable(schemaFactory, resourceType); Table<?> mappingTable = JooqUtils.getTable(schemaFactory, mappingType); Table<?> otherTable = JooqUtils.getTable(schemaFactory, otherType); TableField<Record,Field<Long>> resourceTableField = getReferenceField(mappingTable, resourceTable); TableField<Record,Field<Long>> otherTableField = getReferenceField(mappingTable, otherTable); final Set<String> result = new HashSet<String>(); create() .select((Field<String>)otherTable.field(ObjectMetaDataManager.UUID_FIELD)) .from(resourceTable) .join(mappingTable) .on(resourceTableField.eq((Field<Long>)resourceTable.field(ObjectMetaDataManager.ID_FIELD))) .join(otherTable) .on(otherTableField.eq((Field<Long>)otherTable.field(ObjectMetaDataManager.ID_FIELD))) .where( ((Field<String>)resourceTable.field(ObjectMetaDataManager.UUID_FIELD)).eq(uuid)) .fetchInto(new RecordHandler<Record1<String>>() { @Override public void next(Record1<String> record) { result.add(record.value1()); } }); return result; }
protected Map<Long,String[]> readKeys() { final Map<Long,String[]> keys = new LinkedHashMap<Long, String[]>(); create() .select(DATA.NAME, DATA.VALUE) .from(DATA) .where( DATA.NAME.like("ssh-client-key%")) .orderBy(DATA.NAME.asc()) .fetchInto(new RecordHandler<Record2<String,String>>() { @Override public void next(Record2<String,String> record) { Matcher m = KEY_NAME_PATTERN.matcher(record.value1()); if ( ! m.matches() ) { return; } Long key = new Long(m.group(1)); String type = m.group(2); String[] value = keys.get(key); if ( value == null ) { value = new String[2]; keys.put(key, value); } int idx = type.equals("public") ? 0 : 1; value[idx] = record.value2(); } }); return keys; }
@Override public List<ProcessSummary> getProcessSummary() { final Map<String, ProcessSummary> processSummary = new TreeMap<>(); final Field<Boolean> running = DSL.field(PROCESS_INSTANCE.RUNNING_PROCESS_SERVER_ID.isNotNull()).as("running"); final Field<Boolean> delayed = DSL.field(PROCESS_INSTANCE.RUN_AFTER.greaterThan(new Date())).as("foo"); final Field<Integer> count = PROCESS_INSTANCE.PROCESS_NAME.count().as("count"); create() .select(PROCESS_INSTANCE.PROCESS_NAME, running, delayed, count) .from(PROCESS_INSTANCE) .where(PROCESS_INSTANCE.END_TIME.isNull()) .groupBy(PROCESS_INSTANCE.PROCESS_NAME, running, delayed) .fetchInto(new RecordHandler<Record4<String, Boolean, Boolean, Integer>>() { @Override public void next(Record4<String, Boolean, Boolean, Integer> record) { String name = record.getValue(PROCESS_INSTANCE.PROCESS_NAME); int c = record.getValue(count); boolean r = record.getValue(running); Boolean d = record.getValue(delayed); ProcessSummary summary = processSummary.get(name); if (summary == null) { summary = new ProcessSummary(); summary.setProcessName(name); processSummary.put(name, summary); } if (r) { summary.setRunning(summary.getRunning() + c); } else if (d == null || !d) { summary.setReady(summary.getReady() + c); } else { summary.setDelay(summary.getDelay() + c); } } }); return new ArrayList<>(processSummary.values()); }
@Override public List<Long> pendingTasks(String resourceType, String resourceId) { final List<Long> result = new ArrayList<Long>(); /* I know I should and can do this unique logic in SQL, but I couldn't * figure out a simple way to do it in HSQLDB. So if you're reading this * and can find a query that does this across all the supported DB, please * let someone know. * * Here's what I wanted to do * * select * min(PROCESS_INSTANCE.ID) * from PROCESS_INSTANCE * where * PROCESS_INSTANCE.END_TIME is null * group by PROCESS_INSTANCE.RESOURCE_TYPE, PROCESS_INSTANCE.RESOURCE_ID * order by PROCESS_INSTANCE.START_TIME asc * limit 10000 offset 0 * * But you can't order by something that is not in the group by. So how * do I get a unique pair of resource_type, resource_id, but still order by * id or start_time */ final Set<String> seen = new HashSet<String>(); create() .select(PROCESS_INSTANCE.ID, PROCESS_INSTANCE.RESOURCE_TYPE, PROCESS_INSTANCE.RESOURCE_ID) .from(PROCESS_INSTANCE) .where(processCondition(resourceType, resourceId)) .orderBy(PROCESS_INSTANCE.PRIORITY.desc(), PROCESS_INSTANCE.ID.asc()) .limit(PROCESS_REPLAY_BATCH.get()) .fetchInto(new RecordHandler<Record3<Long,String,String>>() { @Override public void next(Record3<Long,String,String> record) { String resource = String.format("%s:%s", record.value2(), record.value3()); if ( seen.contains(resource) ) { return; } seen.add(resource); result.add(record.value1()); } }); return result; }