Example #1
0
 /**
  * Joins multiple {@link InputStream} suppliers into a single supplier. Streams returned from the
  * supplier will contain the concatenated data from the streams of the underlying suppliers.
  *
  * <p>Only one underlying input stream will be open at a time. Closing the joined stream will
  * close the open underlying stream.
  *
  * <p>Reading from the joined stream will throw a {@link NullPointerException} if any of the
  * suppliers are null or return null.
  *
  * @param suppliers the suppliers to concatenate
  * @return a supplier that will return a stream containing the concatenated stream data
  */
 public static InputSupplier<InputStream> join(
     final Iterable<? extends InputSupplier<? extends InputStream>> suppliers) {
   checkNotNull(suppliers);
   Iterable<ByteSource> sources =
       Iterables.transform(
           suppliers,
           new Function<InputSupplier<? extends InputStream>, ByteSource>() {
             @Override
             public ByteSource apply(InputSupplier<? extends InputStream> input) {
               return asByteSource(input);
             }
           });
   return asInputSupplier(ByteSource.concat(sources));
 }
  @Override
  public String createTask(Graph<Operation, EdgeType> graph, String hostname) {
    logger.info("create task script for host:" + hostname);
    List<ByteSource> byteSources = Lists.newArrayList();
    TreeSet<Operation> imports =
        Sets.newTreeSet(
            new Comparator<Operation>() {
              @Override
              public int compare(Operation o1, Operation o2) {
                return o1.getName().compareTo(o2.getName());
              }
            });
    StringBuilder builder = new StringBuilder();
    String changeLine = System.getProperty("line.separator");

    builder.append(changeLine + "node '" + hostname + "'{" + changeLine);
    String definedContent = opBuilder.createDefineContent(graph, imports, null);
    builder.append(definedContent);
    builder.append(changeLine + "}");

    logger.debug(builder.toString());

    imports.addAll(dependedOperation(imports));
    try {
      for (Operation operation : imports) {
        if (operation.getDefineMd5() != null) {
          byteSources.add(fileService.findFile(operation.getDefineMd5()));
        }
      }
      byteSources.add(ByteSource.wrap(builder.toString().getBytes()));
      String md5Key = fileService.saveFile(ByteSource.concat(byteSources));
      logger.info("the md5key of this task script is" + md5Key);
      logger.info("create task script successful");
      return md5Key;
    } catch (IOException e) {
      logger.error("store task script file failed");
      e.printStackTrace();
    }
    return null;
  }
Example #3
0
  public ByteSource asByteSource(ReadOnlyTransaction txn) throws IOException {
    ByteBuffer snapshotHeader = ByteBuffer.allocate(HEADER_SIZE);

    {
      ByteBuffer header = buildHeader();
      assert header.remaining() == MASTERPAGE_HEADER_SIZE;
      snapshotHeader.put(header);
    }

    // We build a master header with just the current snapshot transaction
    for (int i = 0; i < MASTERPAGE_SLOTS; i++) {
      int position = MASTERPAGE_HEADER_SIZE + (i * MASTERPAGE_SLOT_SIZE);

      ByteBuffer mmap = ByteBuffer.allocate(MASTERPAGE_SLOT_SIZE);
      assert mmap.position() == 0;

      int rootPage = txn.rootPageId;
      int transactionPage = txn.transactionPageId;
      long transactionId = txn.snapshotTransactionId;

      MasterPage.create(mmap, rootPage, transactionPage, transactionId);
      mmap.position(MASTERPAGE_SLOT_SIZE);
      mmap.flip();
      assert mmap.remaining() == MASTERPAGE_SLOT_SIZE;

      snapshotHeader.position(position);
      snapshotHeader.put(mmap);
    }

    snapshotHeader.position(0);

    ByteSource dataByteSource = asByteSource0();
    dataByteSource = dataByteSource.slice(HEADER_SIZE, Long.MAX_VALUE);

    return ByteSource.concat(new ByteBufferByteSource(snapshotHeader), dataByteSource);
  }