Search in sources :

Example 1 with TLongList

use of gnu.trove.list.TLongList in project atlasdb by palantir.

the class SweepableCellFilter method getCellToSweep.

// Decide if the candidate cell needs to be swept, and if so, for which timestamps.
@Nullable
private CellToSweep getCellToSweep(CandidateCellForSweeping candidate, CommitTsLoader commitTss) {
    Preconditions.checkArgument(candidate.sortedTimestamps().size() > 0);
    TLongList timestampsToSweep = new TLongArrayList();
    TLongList uncommittedTimestamps = new TLongArrayList();
    long maxStartTs = TransactionConstants.FAILED_COMMIT_TS;
    boolean maxStartTsIsCommitted = false;
    for (long startTs : candidate.sortedTimestamps()) {
        long commitTs = commitTss.load(startTs);
        if (startTs > maxStartTs && commitTs < sweepTs) {
            maxStartTs = startTs;
            maxStartTsIsCommitted = commitTs != TransactionConstants.FAILED_COMMIT_TS;
        }
        // passing condition (1)
        if (commitTs > 0 && commitTs < sweepTs) {
            timestampsToSweep.add(startTs);
        } else if (commitTs == TransactionConstants.FAILED_COMMIT_TS) {
            uncommittedTimestamps.add(startTs);
        }
    }
    boolean needsSentinel = sweeper.shouldAddSentinels() && timestampsToSweep.size() > 1;
    boolean shouldSweepLastCommitted = sweeper.shouldSweepLastCommitted() && candidate.isLatestValueEmpty() && maxStartTsIsCommitted;
    if (!timestampsToSweep.isEmpty() && !shouldSweepLastCommitted) {
        timestampsToSweep.removeAt(timestampsToSweep.size() - 1);
    }
    timestampsToSweep.addAll(uncommittedTimestamps);
    if (timestampsToSweep.isEmpty()) {
        return null;
    } else {
        return ImmutableCellToSweep.builder().cell(candidate.cell()).sortedTimestamps(timestampsToSweep).needsSentinel(needsSentinel).build();
    }
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongList(gnu.trove.list.TLongList) Nullable(javax.annotation.Nullable)

Example 2 with TLongList

use of gnu.trove.list.TLongList in project Osmand by osmandapp.

the class MapRenderRepositories method unifyIncompletedRings.

private void unifyIncompletedRings(List<TLongList> toProcces, List<TLongList> completedRings, int leftX, int rightX, int bottomY, int topY, long dbId, int zoom) {
    int mask = 0xffffffff;
    List<TLongList> uncompletedRings = new ArrayList<TLongList>(toProcces);
    toProcces.clear();
    Set<Integer> nonvisitedRings = new LinkedHashSet<Integer>();
    for (int j = 0; j < uncompletedRings.size(); j++) {
        TLongList i = uncompletedRings.get(j);
        int x = (int) (i.get(i.size() - 1) >> 32);
        int y = (int) (i.get(i.size() - 1) & mask);
        int sx = (int) (i.get(0) >> 32);
        int sy = (int) (i.get(0) & mask);
        boolean st = y == topY || x == rightX || y == bottomY || x == leftX;
        boolean end = sy == topY || sx == rightX || sy == bottomY || sx == leftX;
        // that's why these exceptions could be replaced with return; statement.
        if (!end || !st) {
            float dx = (float) MapUtils.get31LongitudeX(x);
            float dsx = (float) MapUtils.get31LongitudeX(sx);
            float dy = (float) MapUtils.get31LatitudeY(y);
            float dsy = (float) MapUtils.get31LatitudeY(sy);
            String str;
            if (!end) {
                // $NON-NLS-1$
                str = " Starting point (to close) not found : end_x = {0}, end_y = {1}, start_x = {2}, start_y = {3} : bounds {4} {5} - {6} {7}";
                System.err.println(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
                MessageFormat.format(dbId + str, dx, dy, dsx, dsy, leftX + "", topY + "", rightX + "", bottomY + ""));
            }
            if (!st) {
                // $NON-NLS-1$
                str = " End not found : end_x = {0}, end_y = {1}, start_x = {2}, start_y = {3} : bounds {4} {5} - {6} {7}";
                System.err.println(// $NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$//$NON-NLS-4$
                MessageFormat.format(dbId + str, dx, dy, dsx, dsy, leftX + "", topY + "", rightX + "", bottomY + ""));
            }
            toProcces.add(i);
        } else {
            nonvisitedRings.add(j);
        }
    }
    for (int j = 0; j < uncompletedRings.size(); j++) {
        TLongList i = uncompletedRings.get(j);
        if (!nonvisitedRings.contains(j)) {
            continue;
        }
        int x = (int) (i.get(i.size() - 1) >> 32);
        int y = (int) (i.get(i.size() - 1) & mask);
        // 31 - (zoom + 8)
        int EVAL_DELTA = 6 << (23 - zoom);
        int UNDEFINED_MIN_DIFF = -1 - EVAL_DELTA;
        while (true) {
            // st already checked to be one of the four
            int st = 0;
            if (y == topY) {
                st = 0;
            } else if (x == rightX) {
                st = 1;
            } else if (y == bottomY) {
                st = 2;
            } else if (x == leftX) {
                st = 3;
            }
            int nextRingIndex = -1;
            // BEGIN go clockwise around rectangle
            for (int h = st; h < st + 4; h++) {
                // BEGIN find closest nonvisited start (including current)
                int mindiff = UNDEFINED_MIN_DIFF;
                for (Integer ni : nonvisitedRings) {
                    TLongList cni = uncompletedRings.get(ni);
                    int csx = (int) (cni.get(0) >> 32);
                    int csy = (int) (cni.get(0) & mask);
                    if (h % 4 == 0) {
                        // top
                        if (csy == topY && csx >= safelyAddDelta(x, -EVAL_DELTA)) {
                            if (mindiff == UNDEFINED_MIN_DIFF || (csx - x) <= mindiff) {
                                mindiff = (csx - x);
                                nextRingIndex = ni;
                            }
                        }
                    } else if (h % 4 == 1) {
                        // right
                        if (csx == rightX && csy >= safelyAddDelta(y, -EVAL_DELTA)) {
                            if (mindiff == UNDEFINED_MIN_DIFF || (csy - y) <= mindiff) {
                                mindiff = (csy - y);
                                nextRingIndex = ni;
                            }
                        }
                    } else if (h % 4 == 2) {
                        // bottom
                        if (csy == bottomY && csx <= safelyAddDelta(x, EVAL_DELTA)) {
                            if (mindiff == UNDEFINED_MIN_DIFF || (x - csx) <= mindiff) {
                                mindiff = (x - csx);
                                nextRingIndex = ni;
                            }
                        }
                    } else if (h % 4 == 3) {
                        // left
                        if (csx == leftX && csy <= safelyAddDelta(y, EVAL_DELTA)) {
                            if (mindiff == UNDEFINED_MIN_DIFF || (y - csy) <= mindiff) {
                                mindiff = (y - csy);
                                nextRingIndex = ni;
                            }
                        }
                    }
                }
                // we found start point
                if (mindiff != UNDEFINED_MIN_DIFF) {
                    break;
                } else {
                    if (h % 4 == 0) {
                        // top
                        y = topY;
                        x = rightX;
                    } else if (h % 4 == 1) {
                        // right
                        y = bottomY;
                        x = rightX;
                    } else if (h % 4 == 2) {
                        // bottom
                        y = bottomY;
                        x = leftX;
                    } else if (h % 4 == 3) {
                        y = topY;
                        x = leftX;
                    }
                    i.add((((long) x) << 32) | ((long) y));
                }
            }
            // END go clockwise around rectangle
            if (nextRingIndex == -1) {
            // it is impossible (current start should always be found)
            } else if (nextRingIndex == j) {
                i.add(i.get(0));
                nonvisitedRings.remove(j);
                break;
            } else {
                i.addAll(uncompletedRings.get(nextRingIndex));
                nonvisitedRings.remove(nextRingIndex);
                // get last point and start again going clockwise
                x = (int) (i.get(i.size() - 1) >> 32);
                y = (int) (i.get(i.size() - 1) & mask);
            }
        }
        completedRings.add(i);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) TLongList(gnu.trove.list.TLongList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList)

Example 3 with TLongList

use of gnu.trove.list.TLongList in project Osmand by osmandapp.

the class MapRenderRepositories method processCoastlines.

// / MULTI POLYGONS (coastline)
// returns true if coastlines were added!
private boolean processCoastlines(List<BinaryMapDataObject> coastLines, int leftX, int rightX, int bottomY, int topY, int zoom, boolean doNotAddIfIncompleted, boolean addDebugIncompleted, List<BinaryMapDataObject> result) {
    List<TLongList> completedRings = new ArrayList<TLongList>();
    List<TLongList> uncompletedRings = new ArrayList<TLongList>();
    MapIndex mapIndex = null;
    long dbId = 0;
    for (BinaryMapDataObject o : coastLines) {
        int len = o.getPointsLength();
        if (len < 2) {
            continue;
        }
        mapIndex = o.getMapIndex();
        dbId = o.getId() >> 1;
        TLongList coordinates = new TLongArrayList(o.getPointsLength() / 2);
        int px = o.getPoint31XTile(0);
        int py = o.getPoint31YTile(0);
        int x = px;
        int y = py;
        boolean pinside = leftX <= x && x <= rightX && y >= topY && y <= bottomY;
        if (pinside) {
            coordinates.add(combine2Points(x, y));
        }
        for (int i = 1; i < len; i++) {
            x = o.getPoint31XTile(i);
            y = o.getPoint31YTile(i);
            boolean inside = leftX <= x && x <= rightX && y >= topY && y <= bottomY;
            boolean lineEnded = calculateLineCoordinates(inside, x, y, pinside, px, py, leftX, rightX, bottomY, topY, coordinates);
            if (lineEnded) {
                combineMultipolygonLine(completedRings, uncompletedRings, coordinates);
                // create new line if it goes outside
                coordinates = new TLongArrayList();
            }
            px = x;
            py = y;
            pinside = inside;
        }
        combineMultipolygonLine(completedRings, uncompletedRings, coordinates);
    }
    if (completedRings.size() == 0 && uncompletedRings.size() == 0) {
        return false;
    }
    if (uncompletedRings.size() > 0) {
        unifyIncompletedRings(uncompletedRings, completedRings, leftX, rightX, bottomY, topY, dbId, zoom);
    }
    long mask = 0xffffffffL;
    // draw uncompleted for debug purpose
    for (int i = 0; i < uncompletedRings.size(); i++) {
        TLongList ring = uncompletedRings.get(i);
        int[] coordinates = new int[ring.size() * 2];
        for (int j = 0; j < ring.size(); j++) {
            coordinates[j * 2] = (int) (ring.get(j) >> 32);
            coordinates[j * 2 + 1] = (int) (ring.get(j) & mask);
        }
        BinaryMapDataObject o = new BinaryMapDataObject(dbId, coordinates, new int[0][], RenderingRulesStorage.POLYGON_RULES, true, new int[] { mapIndex.coastlineBrokenEncodingType }, null);
        o.setMapIndex(mapIndex);
        result.add(o);
    }
    if (!doNotAddIfIncompleted && uncompletedRings.size() > 0) {
        return false;
    }
    boolean clockwiseFound = false;
    for (int i = 0; i < completedRings.size(); i++) {
        TLongList ring = completedRings.get(i);
        int[] coordinates = new int[ring.size() * 2];
        for (int j = 0; j < ring.size(); j++) {
            coordinates[j * 2] = (int) (ring.get(j) >> 32);
            coordinates[j * 2 + 1] = (int) (ring.get(j) & mask);
        }
        boolean clockwise = MapAlgorithms.isClockwiseWay(ring);
        clockwiseFound = clockwiseFound || clockwise;
        BinaryMapDataObject o = new BinaryMapDataObject(dbId, coordinates, new int[0][], RenderingRulesStorage.POLYGON_RULES, true, new int[] { clockwise ? mapIndex.coastlineEncodingType : mapIndex.landEncodingType }, null);
        o.setMapIndex(mapIndex);
        o.setArea(true);
        result.add(o);
    }
    if (!clockwiseFound && uncompletedRings.size() == 0) {
        // add complete water tile
        BinaryMapDataObject o = new BinaryMapDataObject(dbId, new int[] { leftX, topY, rightX, topY, rightX, bottomY, leftX, bottomY, leftX, topY }, new int[0][], RenderingRulesStorage.POLYGON_RULES, true, new int[] { mapIndex.coastlineEncodingType }, null);
        o.setMapIndex(mapIndex);
        // $NON-NLS-1$
        log.info("!!! Isolated islands !!!");
        result.add(o);
    }
    return true;
}
Also used : BinaryMapDataObject(net.osmand.binary.BinaryMapDataObject) TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongList(gnu.trove.list.TLongList) TIntArrayList(gnu.trove.list.array.TIntArrayList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) MapIndex(net.osmand.binary.BinaryMapIndexReader.MapIndex)

Example 4 with TLongList

use of gnu.trove.list.TLongList in project Osmand by osmandapp.

the class MapRenderRepositories method combineMultipolygonLine.

private void combineMultipolygonLine(List<TLongList> completedRings, List<TLongList> incompletedRings, TLongList coordinates) {
    if (coordinates.size() > 0) {
        if (eq(coordinates.get(0), coordinates.get(coordinates.size() - 1))) {
            completedRings.add(coordinates);
        } else {
            boolean add = true;
            for (int k = 0; k < incompletedRings.size(); ) {
                boolean remove = false;
                TLongList i = incompletedRings.get(k);
                if (eq(coordinates.get(0), i.get(i.size() - 1))) {
                    i.addAll(coordinates.subList(1, coordinates.size()));
                    remove = true;
                    coordinates = i;
                } else if (eq(coordinates.get(coordinates.size() - 1), i.get(0))) {
                    coordinates.addAll(i.subList(1, i.size()));
                    remove = true;
                }
                if (remove) {
                    incompletedRings.remove(k);
                } else {
                    k++;
                }
                if (eq(coordinates.get(0), coordinates.get(coordinates.size() - 1))) {
                    completedRings.add(coordinates);
                    add = false;
                    break;
                }
            }
            if (add) {
                incompletedRings.add(coordinates);
            }
        }
    }
}
Also used : TLongList(gnu.trove.list.TLongList)

Example 5 with TLongList

use of gnu.trove.list.TLongList in project spf4j by zolyfarkas.

the class TSDBQuery method getTimeSeries.

public static TimeSeries getTimeSeries(final File tsdbFile, final long[] tableIds, final long startTimeMillis, final long endTimeMillis) throws IOException {
    TLongList timestamps = new TLongArrayList();
    List<long[]> metrics = new ArrayList<>();
    try (TSDBReader reader = new TSDBReader(tsdbFile, 8192)) {
        Either<TableDef, DataBlock> read;
        while ((read = reader.read()) != null) {
            if (read.isRight()) {
                DataBlock data = read.getRight();
                long baseTs = data.baseTimestamp;
                for (DataRow row : data.getValues()) {
                    for (long tableId : tableIds) {
                        if (tableId == row.tableDefId) {
                            final long ts = baseTs + row.relTimeStamp;
                            if (ts >= startTimeMillis && ts <= endTimeMillis) {
                                timestamps.add(ts);
                                metrics.add(Longs.toArray(row.data));
                            }
                        }
                    }
                }
            }
        }
    }
    return new TimeSeries(timestamps.toArray(), metrics.toArray(new long[metrics.size()][]));
}
Also used : TLongArrayList(gnu.trove.list.array.TLongArrayList) TLongList(gnu.trove.list.TLongList) ArrayList(java.util.ArrayList) TLongArrayList(gnu.trove.list.array.TLongArrayList) TableDef(org.spf4j.tsdb2.avro.TableDef) DataRow(org.spf4j.tsdb2.avro.DataRow) DataBlock(org.spf4j.tsdb2.avro.DataBlock)

Aggregations

TLongList (gnu.trove.list.TLongList)5 TLongArrayList (gnu.trove.list.array.TLongArrayList)4 ArrayList (java.util.ArrayList)3 TIntArrayList (gnu.trove.list.array.TIntArrayList)2 LinkedHashSet (java.util.LinkedHashSet)1 Nullable (javax.annotation.Nullable)1 BinaryMapDataObject (net.osmand.binary.BinaryMapDataObject)1 MapIndex (net.osmand.binary.BinaryMapIndexReader.MapIndex)1 DataBlock (org.spf4j.tsdb2.avro.DataBlock)1 DataRow (org.spf4j.tsdb2.avro.DataRow)1 TableDef (org.spf4j.tsdb2.avro.TableDef)1