TollwaysGraphStorage.java

  1. /*  This file is part of Openrouteservice.
  2.  *
  3.  *  Openrouteservice is free software; you can redistribute it and/or modify it under the terms of the
  4.  *  GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1
  5.  *  of the License, or (at your option) any later version.

  6.  *  This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  7.  *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8.  *  See the GNU Lesser General Public License for more details.

  9.  *  You should have received a copy of the GNU Lesser General Public License along with this library;
  10.  *  if not, see <https://www.gnu.org/licenses/>.
  11.  */
  12. package org.heigit.ors.routing.graphhopper.extensions.storages;

  13. import com.graphhopper.storage.DataAccess;
  14. import com.graphhopper.storage.Directory;
  15. import com.graphhopper.storage.Graph;
  16. import com.graphhopper.storage.GraphExtension;

  17. public class TollwaysGraphStorage implements GraphExtension {
  18.     /* pointer for no entry */
  19.     protected final int efTollways;

  20.     protected DataAccess edges;
  21.     protected int edgeEntryIndex = 0;
  22.     protected int edgeEntryBytes;
  23.     protected int edgesCount;

  24.     public TollwaysGraphStorage() {
  25.         efTollways = nextBlockEntryIndex(1);

  26.         edgeEntryBytes = edgeEntryIndex;
  27.         edgesCount = 0;
  28.     }

  29.     public void init(Graph graph, Directory dir) {
  30.         if (edgesCount > 0)
  31.             throw new AssertionError("The ext_tolls storage must be initialized only once.");

  32.         this.edges = dir.find("ext_tolls");
  33.     }

  34.     protected final int nextBlockEntryIndex(int size) {
  35.         int res = edgeEntryIndex;
  36.         edgeEntryIndex += size;
  37.         return res;
  38.     }

  39.     public TollwaysGraphStorage create(long initBytes) {
  40.         edges.create(initBytes * edgeEntryBytes);
  41.         return this;
  42.     }

  43.     public void flush() {
  44.         edges.setHeader(0, edgeEntryBytes);
  45.         edges.setHeader(4, edgesCount);
  46.         edges.flush();
  47.     }

  48.     public void close() {
  49.         edges.close();
  50.     }

  51.     @Override
  52.     public long getCapacity() {
  53.         return edges.getCapacity();
  54.     }

  55.     public int entries() {
  56.         return edgesCount;
  57.     }

  58.     public boolean loadExisting() {
  59.         if (!edges.loadExisting())
  60.             throw new IllegalStateException("Unable to load storage 'ext_tolls'. corrupt file or directory? ");

  61.         edgeEntryBytes = edges.getHeader(0);
  62.         edgesCount = edges.getHeader(4);
  63.         return true;
  64.     }

  65.     void ensureEdgesIndex(int edgeIndex) {
  66.         edges.ensureCapacity(((long) edgeIndex + 1) * edgeEntryBytes);
  67.     }

  68.     public void setEdgeValue(int edgeId, int value) {
  69.         edgesCount++;
  70.         ensureEdgesIndex(edgeId);

  71.         byte byteValue = (byte) value;

  72.         edges.setByte((long) edgeId * edgeEntryBytes + efTollways, byteValue);
  73.     }

  74.     public int getEdgeValue(int edgeId) {
  75.         byte byteValue = edges.getByte((long) edgeId * edgeEntryBytes + efTollways);
  76.         return byteValue & 0xFF;
  77.     }

  78.     public boolean isRequireNodeField() {
  79.         return true;
  80.     }

  81.     public boolean isRequireEdgeField() {
  82.         // we require the additional field in the graph to point to the first
  83.         // entry in the node table
  84.         return true;
  85.     }

  86.     public int getDefaultNodeFieldValue() {
  87.         return -1;
  88.     }

  89.     public int getDefaultEdgeFieldValue() {
  90.         return -1;
  91.     }

  92.     @Override
  93.     public boolean isClosed() {
  94.         return false;
  95.     }
  96. }