Search in sources :

Example 1 with OGremlinResult

use of org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResult in project open-kilda by telstra.

the class OrientDbFlowMeterRepository method findFirstUnassignedMeter.

@Override
public Optional<MeterId> findFirstUnassignedMeter(SwitchId switchId, MeterId lowestMeterId, MeterId highestMeterId) {
    String switchIdAsStr = SwitchIdConverter.INSTANCE.toGraphProperty(switchId);
    Long lowestMeterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(lowestMeterId);
    Long highestMeterIdAsLong = MeterIdConverter.INSTANCE.toGraphProperty(highestMeterId);
    try (OGremlinResultSet results = graphSupplier.get().querySql(format("SELECT FROM (SELECT difference(unionAll($init_meter, $next_to_meters).meter, " + "$meters.meter) as meter " + "LET $init_meter = (SELECT %sL as meter), " + "$meters = (SELECT %s.asLong() as meter FROM %s WHERE %s = ? " + "AND %s >= ? AND %s < ?), " + "$next_to_meters = (SELECT %s.asLong() + 1 as meter FROM %s WHERE %s = ? " + "AND %s >= ? AND %s < ?) " + "UNWIND meter) ORDER by meter LIMIT 1", lowestMeterIdAsLong, FlowMeterFrame.METER_ID_PROPERTY, FlowMeterFrame.FRAME_LABEL, FlowMeterFrame.SWITCH_PROPERTY, FlowMeterFrame.METER_ID_PROPERTY, FlowMeterFrame.METER_ID_PROPERTY, FlowMeterFrame.METER_ID_PROPERTY, FlowMeterFrame.FRAME_LABEL, FlowMeterFrame.SWITCH_PROPERTY, FlowMeterFrame.METER_ID_PROPERTY, FlowMeterFrame.METER_ID_PROPERTY), switchIdAsStr, lowestMeterIdAsLong, highestMeterIdAsLong, switchIdAsStr, lowestMeterIdAsLong, highestMeterIdAsLong)) {
        Iterator<OGremlinResult> it = results.iterator();
        if (it.hasNext()) {
            Number meter = it.next().getProperty("meter");
            if (meter != null) {
                return Optional.of(MeterIdConverter.INSTANCE.toEntityAttribute(meter.longValue()));
            }
        }
        return Optional.empty();
    }
}
Also used : OGremlinResult(org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResult) OGremlinResultSet(org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet)

Aggregations

OGremlinResult (org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResult)1 OGremlinResultSet (org.apache.tinkerpop.gremlin.orientdb.executor.OGremlinResultSet)1