src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RepositoryFiles.java
branchJEP-349-branch
changeset 57640 46a77fccd251
parent 57638 3b41affae2d2
child 57690 9316d02dd4a5
equal deleted inserted replaced
57638:3b41affae2d2 57640:46a77fccd251
    32 import java.util.Collections;
    32 import java.util.Collections;
    33 import java.util.HashMap;
    33 import java.util.HashMap;
    34 import java.util.HashSet;
    34 import java.util.HashSet;
    35 import java.util.List;
    35 import java.util.List;
    36 import java.util.Map;
    36 import java.util.Map;
    37 import java.util.Map.Entry;
       
    38 import java.util.NavigableMap;
    37 import java.util.NavigableMap;
    39 import java.util.Set;
    38 import java.util.Set;
    40 import java.util.SortedMap;
    39 import java.util.SortedMap;
    41 import java.util.TreeMap;
    40 import java.util.TreeMap;
    42 
    41 
    59     public long getTimestamp(Path p) {
    58     public long getTimestamp(Path p) {
    60         return pathLookup.get(p);
    59         return pathLookup.get(p);
    61     }
    60     }
    62 
    61 
    63     public Path lastPath() {
    62     public Path lastPath() {
    64         return nextPath(-1);
    63         // Wait for chunks
       
    64         while (!closed) {
       
    65             try {
       
    66                 if (updatePaths(repository)) {
       
    67                     break;
       
    68                 }
       
    69             } catch (IOException e) {
       
    70                 // ignore, not yet available
       
    71             }
       
    72         }
       
    73         if (closed) {
       
    74             return null;
       
    75         }
       
    76         // Pick the last
       
    77         return pathSet.lastEntry().getValue();
       
    78     }
       
    79 
       
    80     public Path firstPath(long startTimeNanos) {
       
    81         return path(startTimeNanos, true);
    65     }
    82     }
    66 
    83 
    67     public Path nextPath(long startTimeNanos) {
    84     public Path nextPath(long startTimeNanos) {
       
    85         return path(startTimeNanos, false);
       
    86     }
       
    87 
       
    88     private Path path(long timestamp, boolean first) {
    68         while (!closed) {
    89         while (!closed) {
    69             if (startTimeNanos == -1) {
    90             Long time = timestamp;
    70                 Entry<Long, Path> e = pathSet.lastEntry();
    91             if (first) {
    71                 if (e != null) {
    92                 // Pick closest chunk before timestamp
    72                     return e.getValue();
    93                 time = pathSet.floorKey(timestamp);
    73                 }
       
    74             }
    94             }
    75             Long f = pathSet.floorKey(startTimeNanos);
    95             if (time != null) {
    76             if (f != null) {
    96                 SortedMap<Long, Path> after = pathSet.tailMap(time);
    77                 SortedMap<Long, Path> after = pathSet.tailMap(f);
       
    78                 if (!after.isEmpty()) {
    97                 if (!after.isEmpty()) {
    79                     Path path = after.get(after.firstKey());
    98                     Path path = after.get(after.firstKey());
    80                     Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + startTimeNanos);
    99                     Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + timestamp);
    81                     return path;
   100                     return path;
    82                 }
   101                 }
    83             }
   102             }
    84             try {
   103             try {
    85                 if (updatePaths(repository)) {
   104                 if (updatePaths(repository)) {