test_class.declarator: util
test_class.modifier: private
test_class.original_string: private TajoTestingCluster util;
test_class.type: TajoTestingCluster
test_class.var_name: util
test_class.declarator: conf
test_class.modifier: private
test_class.original_string: private TajoConf conf;
test_class.type: TajoConf
test_class.var_name: conf
test_class.declarator: catalog
test_class.modifier: private
test_class.original_string: private CatalogService catalog;
test_class.type: CatalogService
test_class.var_name: catalog
test_class.declarator: analyzer
test_class.modifier: private
test_class.original_string: private SQLAnalyzer analyzer;
test_class.type: SQLAnalyzer
test_class.var_name: analyzer
test_class.declarator: planner
test_class.modifier: private
test_class.original_string: private LogicalPlanner planner;
test_class.type: LogicalPlanner
test_class.var_name: planner
test_class.declarator: optimizer
test_class.modifier: private
test_class.original_string: private LogicalOptimizer optimizer;
test_class.type: LogicalOptimizer
test_class.var_name: optimizer
test_class.declarator: sm
test_class.modifier: private
test_class.original_string: private AbstractStorageManager sm;
test_class.type: AbstractStorageManager
test_class.var_name: sm
test_class.declarator: schema
test_class.modifier: private
test_class.original_string: private Schema schema;
test_class.type: Schema
test_class.var_name: schema
test_class.declarator: TEST_TUPLE = 10000
test_class.modifier: private static
test_class.original_string: private static int TEST_TUPLE = 10000;
test_class.type: int
test_class.var_name: TEST_TUPLE
test_class.declarator: fs
test_class.modifier: private
test_class.original_string: private FileSystem fs;
test_class.type: FileSystem
test_class.var_name: fs
test_class.declarator: testDir
test_class.modifier: private
test_class.original_string: private Path testDir;
test_class.type: Path
test_class.var_name: testDir
test_class.declarator: SORT_QUERY = {
        "select empId, age from employee order by empId, age",
        "select empId, age from employee order by empId desc, age desc"
    }
test_class.modifier: public
test_class.original_string: public String [] SORT_QUERY = {
        "select empId, age from employee order by empId, age",
        "select empId, age from employee order by empId desc, age desc"
    };
test_class.type: String []
test_class.var_name: SORT_QUERY
test_class.file: tajo-core/src/test/java/org/apache/tajo/worker/TestRangeRetrieverHandler.java
test_class.identifier: TestRangeRetrieverHandler
test_case.body: @Test
    public void testGetFromDescendingOrder() throws Exception {
      Tuple firstTuple = null;
      Tuple lastTuple;
  
      TableMeta meta = CatalogUtil.newTableMeta(StoreType.CSV);
      Path tablePath = StorageUtil.concatPath(testDir, "testGetFromDescendingOrder", "table.csv");
      fs.mkdirs(tablePath.getParent());
      Appender appender = sm.getAppender(meta, schema, tablePath);
      appender.init();
      Tuple tuple = new VTuple(schema.size());
      for (int i = (TEST_TUPLE - 1); i >= 0 ; i--) {
        tuple.put(
            new Datum[] {
                DatumFactory.createInt4(i),
                DatumFactory.createInt4(i + 5)
            });
        appender.addTuple(tuple);
  
        if (firstTuple == null) {
          firstTuple = new VTuple(tuple);
        }
      }
      lastTuple = new VTuple(tuple);
      appender.flush();
      appender.close();
  
      TableDesc employee = new TableDesc(
          CatalogUtil.buildFQName(TajoConstants.DEFAULT_DATABASE_NAME, "employee"), schema, meta, tablePath);
      catalog.createTable(employee);
  
      FileFragment[] frags = sm.splitNG(conf, "default.employee", meta, tablePath, Integer.MAX_VALUE);
  
      TaskAttemptContext
          ctx = new TaskAttemptContext(conf, LocalTajoTestingUtility.newQueryUnitAttemptId(),
          new FileFragment[] {frags[0]}, testDir);
      ctx.setEnforcer(new Enforcer());
      Expr expr = analyzer.parse(SORT_QUERY[1]);
      LogicalPlan plan = planner.createPlan(LocalTajoTestingUtility.createDummySession(), expr);
      LogicalNode rootNode = optimizer.optimize(plan);
  
      PhysicalPlanner phyPlanner = new PhysicalPlannerImpl(conf,sm);
      PhysicalExec exec = phyPlanner.createPlan(ctx, rootNode);
  
      ExternalSortExec sort = null;
      if (exec instanceof ProjectionExec) {
        ProjectionExec projExec = (ProjectionExec) exec;
        sort = projExec.getChild();
      } else if (exec instanceof ExternalSortExec) {
        sort = (ExternalSortExec) exec;
      } else {
        assertTrue(false);
      }
  
      SortSpec[] sortSpecs = sort.getPlan().getSortKeys();
      RangeShuffleFileWriteExec idxStoreExec = new RangeShuffleFileWriteExec(ctx, sm, sort,
          sort.getSchema(), sort.getSchema(), sortSpecs);
  
      exec = idxStoreExec;
      exec.init();
      exec.next();
      exec.close();
  
      Schema keySchema = PlannerUtil.sortSpecsToSchema(sortSpecs);
      TupleComparator comp = new TupleComparator(keySchema, sortSpecs);
      BSTIndex bst = new BSTIndex(conf);
      BSTIndex.BSTIndexReader reader = bst.getIndexReader(
          new Path(testDir, "output/index"), keySchema, comp);
      reader.open();
      TableMeta outputMeta = CatalogUtil.newTableMeta(StoreType.RAW, new Options());
      SeekableScanner scanner = StorageManagerFactory.getSeekableScanner(conf, outputMeta, schema,
          StorageUtil.concatPath(testDir, "output", "output"));
      scanner.init();
      int cnt = 0;
      while(scanner.next() != null) {
        cnt++;
      }
      scanner.reset();
  
      assertEquals(TEST_TUPLE ,cnt);
  
      Tuple keytuple = new VTuple(2);
      for(int i = (TEST_TUPLE - 1) ; i >= 0; i --) {
        keytuple.put(0, DatumFactory.createInt4(i));
        keytuple.put(1, DatumFactory.createInt4(i + 5));
        long offsets = reader.find(keytuple);
        scanner.seek(offsets);
        tuple = scanner.next();
        assertTrue("[seek check " + (i) + " ]" , i == tuple.get(0).asInt4());
      }
  
      TupleRange totalRange = new TupleRange(sortSpecs, firstTuple, lastTuple);
      UniformRangePartition partitioner = new UniformRangePartition(totalRange, sortSpecs, true);
      TupleRange [] partitions = partitioner.partition(25);
  
      File dataFile = new File((new Path(testDir, "output")).toUri());
  
      // The below is for testing RangeRetrieverHandler.
      RangeRetrieverHandler handler = new RangeRetrieverHandler(
          dataFile, keySchema, comp);
  
      List<Long []> offsets = new ArrayList<Long []>();
  
      for (int i = 0; i < partitions.length; i++) {
        FileChunk chunk = getFileChunk(handler, keySchema, partitions[i], i == 0);
        offsets.add(new Long[] {chunk.startOffset(), chunk.length()});
      }
      scanner.close();
  
      long fileLength = new File(dataFile, "data/data").length();
      Long[] previous = null;
      for (Long [] offset : offsets) {
        if (previous == null) {
          assertTrue(offset[0] + offset[1] == fileLength);
          previous = offset;
          continue;
        }
  
        assertTrue(offset[0] + offset[1] == previous[0]);
        previous = offset;
      }
    }
test_case.class_method_signature: TestRangeRetrieverHandler.testGetFromDescendingOrder()
test_case.full_signature: @Test public void testGetFromDescendingOrder()
test_case.identifier: testGetFromDescendingOrder
test_case.invocations: newTableMeta concatPath mkdirs getParent getAppender init size put createInt4 createInt4 addTuple flush close buildFQName createTable splitNG newQueryUnitAttemptId setEnforcer parse createPlan createDummySession optimize createPlan getChild assertTrue getSortKeys getPlan getSchema getSchema init next close sortSpecsToSchema getIndexReader open newTableMeta getSeekableScanner concatPath init next reset assertEquals put createInt4 put createInt4 find seek next assertTrue asInt4 get partition toUri getFileChunk add startOffset length close length assertTrue assertTrue
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testGetFromDescendingOrder()
focal_class.declarator: LOG = LogFactory.getLog(RangeRetrieverHandler.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Log LOG = LogFactory.getLog(RangeRetrieverHandler.class);
focal_class.type: Log
focal_class.var_name: LOG
focal_class.declarator: file
focal_class.modifier: private final
focal_class.original_string: private final File file;
focal_class.type: File
focal_class.var_name: file
focal_class.declarator: idxReader
focal_class.modifier: private final
focal_class.original_string: private final BSTIndex.BSTIndexReader idxReader;
focal_class.type: BSTIndex.BSTIndexReader
focal_class.var_name: idxReader
focal_class.declarator: schema
focal_class.modifier: private final
focal_class.original_string: private final Schema schema;
focal_class.type: Schema
focal_class.var_name: schema
focal_class.declarator: comp
focal_class.modifier: private final
focal_class.original_string: private final TupleComparator comp;
focal_class.type: TupleComparator
focal_class.var_name: comp
focal_class.declarator: decoder
focal_class.modifier: private final
focal_class.original_string: private final RowStoreDecoder decoder;
focal_class.type: RowStoreDecoder
focal_class.var_name: decoder
focal_class.file: tajo-core/src/main/java/org/apache/tajo/worker/RangeRetrieverHandler.java
focal_class.identifier: RangeRetrieverHandler
focal_class.interfaces: implements RetrieverHandler
focal_class.class_method_signature: RangeRetrieverHandler.RangeRetrieverHandler(File outDir, Schema schema, TupleComparator comp)
focal_class.full_signature: public  RangeRetrieverHandler(File outDir, Schema schema, TupleComparator comp)
focal_class.identifier: RangeRetrieverHandler
focal_class.modifiers: public
focal_class.parameters: (File outDir, Schema schema, TupleComparator comp)
focal_class.signature:  RangeRetrieverHandler(File outDir, Schema schema, TupleComparator comp)
focal_class.class_method_signature: RangeRetrieverHandler.get(Map<String, List<String>> kvs)
focal_class.full_signature: @Override public FileChunk get(Map<String, List<String>> kvs)
focal_class.identifier: get
focal_class.modifiers: @Override public
focal_class.parameters: (Map<String, List<String>> kvs)
focal_class.return: FileChunk
focal_class.signature: FileChunk get(Map<String, List<String>> kvs)
focal_method.body: @Override
    public FileChunk get(Map<String, List<String>> kvs) throws IOException {
      // nothing to verify the file because AdvancedDataRetriever checks
      // its validity of the file.
      File data = new File(this.file, "data/data");
      byte [] startBytes = Base64.decodeBase64(kvs.get("start").get(0));
      Tuple start = decoder.toTuple(startBytes);
      byte [] endBytes;
      Tuple end;
      endBytes = Base64.decodeBase64(kvs.get("end").get(0));
      end = decoder.toTuple(endBytes);
      boolean last = kvs.containsKey("final");
  
      if(!comp.isAscendingFirstKey()) {
        Tuple tmpKey = start;
        start = end;
        end = tmpKey;
      }
  
      LOG.info("GET Request for " + data.getAbsolutePath() + " (start="+start+", end="+ end +
          (last ? ", last=true" : "") + ")");
  
      if (idxReader.getFirstKey() == null && idxReader.getLastKey() == null) { // if # of rows is zero
        LOG.info("There is no contents");
        return null;
      }
  
      if (comp.compare(end, idxReader.getFirstKey()) < 0 ||
          comp.compare(idxReader.getLastKey(), start) < 0) {
        LOG.info("Out of Scope (indexed data [" + idxReader.getFirstKey() + ", " + idxReader.getLastKey() +
            "], but request start:" + start + ", end: " + end);
        return null;
      }
  
      long startOffset;
      long endOffset;
      try {
        startOffset = idxReader.find(start);
      } catch (IOException ioe) {
        LOG.error("State Dump (the requested range: "
            + "[" + start + ", " + end+")" + ", idx min: " + idxReader.getFirstKey() + ", idx max: "
            + idxReader.getLastKey());
        throw ioe;
      }
      try {
        endOffset = idxReader.find(end);
        if (endOffset == -1) {
          endOffset = idxReader.find(end, true);
        }
      } catch (IOException ioe) {
        LOG.error("State Dump (the requested range: "
            + "[" + start + ", " + end+")" + ", idx min: " + idxReader.getFirstKey() + ", idx max: "
            + idxReader.getLastKey());
        throw ioe;
      }
  
      // if startOffset == -1 then case 2-1 or case 3
      if (startOffset == -1) { // this is a hack
        // if case 2-1 or case 3
        try {
          startOffset = idxReader.find(start, true);
        } catch (IOException ioe) {
          LOG.error("State Dump (the requested range: "
              + "[" + start + ", " + end+")" + ", idx min: " + idxReader.getFirstKey() + ", idx max: "
              + idxReader.getLastKey());
          throw ioe;
        }
      }
  
      if (startOffset == -1) {
        throw new IllegalStateException("startOffset " + startOffset + " is negative \n" +
            "State Dump (the requested range: "
            + "[" + start + ", " + end+")" + ", idx min: " + idxReader.getFirstKey() + ", idx max: "
            + idxReader.getLastKey());
      }
  
      // if greater than indexed values
      if (last || (endOffset == -1 && comp.compare(idxReader.getLastKey(), end) < 0)) {
        endOffset = data.length();
      }
  
      FileChunk chunk = new FileChunk(data, startOffset, endOffset - startOffset);
      LOG.info("Retrieve File Chunk: " + chunk);
      return chunk;
    }
focal_method.class_method_signature: RangeRetrieverHandler.get(Map<String, List<String>> kvs)
focal_method.full_signature: @Override public FileChunk get(Map<String, List<String>> kvs)
focal_method.identifier: get
focal_method.invocations: decodeBase64 get get toTuple decodeBase64 get get toTuple containsKey isAscendingFirstKey info getAbsolutePath getFirstKey getLastKey info compare getFirstKey compare getLastKey info getFirstKey getLastKey find error getFirstKey getLastKey find find error getFirstKey getLastKey find error getFirstKey getLastKey getFirstKey getLastKey compare getLastKey length info
focal_method.modifiers: @Override public
focal_method.parameters: (Map<String, List<String>> kvs)
focal_method.return: FileChunk
focal_method.signature: FileChunk get(Map<String, List<String>> kvs)