text:
#include "config.h"
#include "JSContextRef.h"
#include "JSContextRefPrivate.h"

#include "APICast.h"
#include "InitializeThreading.h"
#include <interpreter/CallFrame.h>
#include <interpreter/Interpreter.h>
#include "JSCallbackObject.h"
#include "JSClassRef.h"
#include "JSGlobalObject.h"
#include "JSObject.h"
#include <wtf/text/StringBuilder.h>
#include <wtf/text/StringHash.h>

#if OS(DARWIN)
#include <mach-o/dyld.h>

static const int32_t webkitFirstVersionWithConcurrentGlobalContexts = 0x2100500; // 528.5.0
#endif

using namespace JSC;

// From the API's perspective, a context group remains alive iff
//     (a) it has been JSContextGroupRetained
//     OR
//     (b) one of its contexts has been JSContextRetained

JSContextGroupRef JSContextGroupCreate()
{
    initializeThreading();
    return toRef(JSGlobalData::createContextGroup().leakRef());
}

JSContextGroupRef JSContextGroupRetain(JSContextGroupRef group)
{
    toJS(group)->ref();
    return group;
}

void JSContextGroupRelease(JSContextGroupRef group)
{
    toJS(group)->deref();
}

// From the API's perspective, a global context remains alive iff it has been JSGlobalContextRetained.

JSGlobalContextRef JSGlobalContextCreate(JSClassRef globalObjectClass)
{
    initializeThreading();

#if OS(DARWIN)
    // If the application was linked before JSGlobalContextCreate was changed to use a unique JSGlobalData,
    // we use a shared one for backwards compatibility.
    if (NSVersionOfLinkTimeLibrary("JavaScriptCore") <= webkitFirstVersionWithConcurrentGlobalContexts) {
        return JSGlobalContextCreateInGroup(toRef(&JSGlobalData::sharedInstance()), globalObjectClass);
    }
#endif // OS(DARWIN)

    return JSGlobalContextCreateInGroup(0, globalObjectClass);
}

JSGlobalContextRef JSGlobalContextCreateInGroup(JSContextGroupRef group, JSClassRef globalObjectClass)
{
    initializeThreading();

    RefPtr<JSGlobalData> globalData = group ? PassRefPtr<JSGlobalData>(toJS(group)) : JSGlobalData::createContextGroup();

    APIEntryShim entryShim(globalData.get(), false);
    globalData->makeUsableFromMultipleThreads();

    if (!globalObjectClass) {
        JSGlobalObject* globalObject = JSGlobalObject::create(*globalData, JSGlobalObject::createStructure(*globalData, jsNull()));
        return JSGlobalContextRetain(toGlobalRef(globalObject->globalExec()));
    }

    JSGlobalObject* globalObject = JSCallbackObject<JSGlobalObject>::create(*globalData, globalObjectClass, JSCallbackObject<JSGlobalObject>::createStructure(*globalData, 0, jsNull()));
    ExecState* exec = globalObject->globalExec();
    JSValue prototype = globalObjectClass->prototype(exec);
    if (!prototype)
        prototype = jsNull();
    globalObject->resetPrototype(*globalData, prototype);
    return JSGlobalContextRetain(toGlobalRef(exec));
}

JSGlobalContextRef JSGlobalContextRetain(JSGlobalContextRef ctx)
{
    ExecState* exec = toJS(ctx);
    APIEntryShim entryShim(exec);

    JSGlobalData& globalData = exec->globalData();
    gcProtect(exec->dynamicGlobalObject());
    globalData.ref();
    return ctx;
}

void JSGlobalContextRelease(JSGlobalContextRef ctx)
{
    IdentifierTable* savedIdentifierTable;
    ExecState* exec = toJS(ctx);
    {
        JSLockHolder lock(exec);

        JSGlobalData& globalData = exec->globalData();
        savedIdentifierTable = wtfThreadData().setCurrentIdentifierTable(globalData.identifierTable);

        bool protectCountIsZero = Heap::heap(exec->dynamicGlobalObject())->unprotect(exec->dynamicGlobalObject());
        if (protectCountIsZero)
            globalData.heap.reportAbandonedObjectGraph();
        globalData.deref();
    }

    wtfThreadData().setCurrentIdentifierTable(savedIdentifierTable);
}

JSObjectRef JSContextGetGlobalObject(JSContextRef ctx)
{
    ExecState* exec = toJS(ctx);
    APIEntryShim entryShim(exec);

    // It is necessary to call toThisObject to get the wrapper object when used with WebCore.
    return toRef(exec->lexicalGlobalObject()->methodTable()->toThisObject(exec->lexicalGlobalObject(), exec));
}

JSContextGroupRef JSContextGetGroup(JSContextRef ctx)
{
    ExecState* exec = toJS(ctx);
    return toRef(&exec->globalData());
}

JSGlobalContextRef JSContextGetGlobalContext(JSContextRef ctx)
{
    ExecState* exec = toJS(ctx);
    APIEntryShim entryShim(exec);

    return toGlobalRef(exec->lexicalGlobalObject()->globalExec());
}

JSStringRef JSContextCreateBacktrace(JSContextRef ctx, unsigned maxStackSize)
{
    ExecState* exec = toJS(ctx);
    JSLockHolder lock(exec);

    unsigned count = 0;
    StringBuilder builder;
    CallFrame* callFrame = exec;
    String functionName;
    if (exec->callee()) {
        if (asObject(exec->callee())->inherits(&InternalFunction::s_info)) {
            functionName = asInternalFunction(exec->callee())->name(exec);
            builder.appendLiteral("#0 ");
            builder.append(functionName);
            builder.appendLiteral("() ");
            count++;
        }
    }
    while (true) {
        ASSERT(callFrame);
        int signedLineNumber;
        intptr_t sourceID;
        String urlString;
        JSValue function;

        exec->interpreter()->retrieveLastCaller(callFrame, signedLineNumber, sourceID, urlString, function);

        if (function)
            functionName = jsCast<JSFunction*>(function)->name(exec);
        else {
            // Caller is unknown, but if frame is empty we should still add the frame, because
            // something called us, and gave us arguments.
            if (count)
                break;
        }
        unsigned lineNumber = signedLineNumber >= 0 ? signedLineNumber : 0;
        if (!builder.isEmpty())
            builder.append('\n');
        builder.append('#');
        builder.appendNumber(count);
        builder.append(' ');
        builder.append(functionName);
        builder.appendLiteral("() at ");
        builder.append(urlString);
        builder.append(':');
        builder.appendNumber(lineNumber);
        if (!function || ++count == maxStackSize)
            break;
        callFrame = callFrame->callerFrame();
    }
    return OpaqueJSString::create(builder.toString()).leakRef();
}


text:
// Copyright (c) 2010 Shardul Deo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package com.googlecode.protobuf.socketrpc;

import java.io.IOException;

import junit.framework.TestCase;

import com.google.protobuf.ByteString;
import com.google.protobuf.MessageLite;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcController;
import com.google.protobuf.MessageLite.Builder;
import com.googlecode.protobuf.socketrpc.RpcConnectionFactory.Connection;
import com.googlecode.protobuf.socketrpc.TestProtos.Request;
import com.googlecode.protobuf.socketrpc.TestProtos.Response;
import com.googlecode.protobuf.socketrpc.TestProtos.TestService;

/**
 * Tests for {@link RpcServer}. Only contains async tests. Rest is tested by
 * {@link SocketRpcServerTest}.
 *
 * @author Shardul Deo
 */
public class RpcServerTest extends TestCase {

  private static final Request REQUEST;
  private static final SocketRpcProtos.Request RPC_REQUEST;

  static {
    REQUEST = Request.newBuilder().setStrData("Request Data").build();
    RPC_REQUEST = createRpcRequest(TestService.getDescriptor().getFullName(),
        TestService.getDescriptor().getMethods().get(0).getName(),
        REQUEST.toByteString());
  }

  private RpcServer rpcServer;
  private FakeServiceImpl fakeServiceImpl;

  @Override
  protected void setUp() throws Exception {
    super.setUp();
    rpcServer = new RpcServer(null, null,
        false /* closeConnectionAfterInvokingService */);
    fakeServiceImpl = new FakeServiceImpl();
  }

  /**
   * Fake Service impl for testing.
   */
  private class FakeServiceImpl extends TestService {

    private RpcCallback<Response> callback;

    @Override
    public void testMethod(RpcController controller, Request request,
        RpcCallback<Response> done) {
      assertEquals(REQUEST, request);
      callback = done;
    }
  }

  /**
   * Fake connection impl for testing.
   */
  private static class FakeConnection implements Connection {

    private final SocketRpcProtos.Request rpcRequest;
    private boolean closed = false;
    private SocketRpcProtos.Response response = null;

    private FakeConnection(SocketRpcProtos.Request rpcRequest) {
      this.rpcRequest = rpcRequest;
    }

    @Override
    public void receiveProtoMessage(Builder messageBuilder) throws IOException {
      messageBuilder.mergeFrom(rpcRequest.toByteArray());
    }

    @Override
    public void sendProtoMessage(MessageLite message) {
      response = (SocketRpcProtos.Response) message;
    }

    @Override
    public void close() {
      closed = true;
    }

    @Override
    public boolean isClosed() {
      return closed;
    }
  }

  private void runHandler(Connection connection) {
    rpcServer.new ConnectionHandler(connection).run();
  }

  /**
   * Test async request/response.
   */
  public void testAsyncRpc_goodResponse() {
    // Create data
    Response response = Response.newBuilder()
        .setStrData("Response Data")
        .build();

    // Create fakes
    FakeConnection fakeConnection = new FakeConnection(RPC_REQUEST);
    rpcServer.registerService(fakeServiceImpl);

    // Call handler
    runHandler(fakeConnection);

    // Check that connection is open and no callback yet
    assertFalse(fakeConnection.closed);
    assertNull(fakeConnection.response);

    // Do delayed callback
    fakeServiceImpl.callback.run(response);

    // Verify result
    assertTrue(fakeConnection.response.getCallback());
    assertEquals(response.toByteString(),
        fakeConnection.response.getResponseProto());
  }

  /**
   * Test async request with null response.
   */
  public void testAsyncRpc_nullResponse() {
    // Create fakes
    FakeConnection fakeConnection = new FakeConnection(RPC_REQUEST);
    rpcServer.registerService(fakeServiceImpl);

    // Call handler
    runHandler(fakeConnection);

    // Check that connection is open and no callback yet
    assertFalse(fakeConnection.closed);
    assertNull(fakeConnection.response);

    // Do delayed callback
    fakeServiceImpl.callback.run(null);

    // Verify result
    assertTrue(fakeConnection.response.getCallback());
    assertFalse(fakeConnection.response.hasResponseProto());
  }

  private static SocketRpcProtos.Request createRpcRequest(String service,
      String method, ByteString request) {
    return SocketRpcProtos.Request.newBuilder()
        .setServiceName(service)
        .setMethodName(method)
        .setRequestProto(request)
        .build();
  }
}

redpajama_set_name: RedPajamaGithub


text:
package com.example.vision;

import com.google.cloud.vision.v1.ImageName;
import com.google.cloud.vision.v1.ProductName;
import com.google.cloud.vision.v1.ProductSearchClient;
import com.google.cloud.vision.v1.ReferenceImage;
import java.io.IOException;
import java.io.PrintStream;
import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.inf.ArgumentParser;
import net.sourceforge.argparse4j.inf.ArgumentParserException;
import net.sourceforge.argparse4j.inf.Namespace;
import net.sourceforge.argparse4j.inf.Subparser;
import net.sourceforge.argparse4j.inf.Subparsers;

/**
 * This application demonstrates how to perform basic operations on Reference Images in Cloud Vision
 * Product Search.
 *
 * <p>For more information, see the tutorial page at
 * https://cloud.google.com/vision/product-search/docs/
 */
public class ReferenceImageManagement {

  // [START vision_product_search_create_reference_image]
  /**
   * Create a reference image.
   *
   * @param projectId - Id of the project.
   * @param computeRegion - Region name.
   * @param productId - Id of the product.
   * @param referenceImageId - Id of the image.
   * @param gcsUri - Google Cloud Storage path of the input image.
   * @throws IOException - on I/O errors.
   */
  public static void createReferenceImage(
      String projectId,
      String computeRegion,
      String productId,
      String referenceImageId,
      String gcsUri)
      throws IOException {
    try (ProductSearchClient client = ProductSearchClient.create()) {

      // Get the full path of the product.
      String formattedParent = ProductName.format(projectId, computeRegion, productId);
      // Create a reference image.
      ReferenceImage referenceImage = ReferenceImage.newBuilder().setUri(gcsUri).build();

      ReferenceImage image =
          client.createReferenceImage(formattedParent, referenceImage, referenceImageId);
      // Display the reference image information.
      System.out.println(String.format("Reference image name: %s", image.getName()));
      System.out.println(String.format("Reference image uri: %s", image.getUri()));
    }
  }
  // [END vision_product_search_create_reference_image]

  // [START vision_product_search_list_reference_images]
  /**
   * List all images in a product.
   *
   * @param projectId - Id of the project.
   * @param computeRegion - Region name.
   * @param productId - Id of the product.
   * @throws IOException - on I/O errors.
   */
  public static void listReferenceImagesOfProduct(
      String projectId, String computeRegion, String productId) throws IOException {
    try (ProductSearchClient client = ProductSearchClient.create()) {

      // Get the full path of the product.
      String formattedParent = ProductName.format(projectId, computeRegion, productId);
      for (ReferenceImage image : client.listReferenceImages(formattedParent).iterateAll()) {
        // Display the reference image information.
        System.out.println(String.format("Reference image name: %s", image.getName()));
        System.out.println(
            String.format(
                "Reference image id: %s",
                image.getName().substring(image.getName().lastIndexOf('/') + 1)));
        System.out.println(String.format("Reference image uri: %s", image.getUri()));
        System.out.println(
            String.format(
                "Reference image bounding polygons: %s \n",
                image.getBoundingPolysList().toString()));
      }
    }
  }
  // [END vision_product_search_list_reference_images]

  // [START vision_product_search_get_reference_image]
  /**
   * Get info about a reference image.
   *
   * @param projectId - Id of the project.
   * @param computeRegion - Region name.
   * @param productId - Id of the product.
   * @param referenceImageId - Id of the image.
   * @throws IOException - on I/O errors.
   */
  public static void getReferenceImage(
      String projectId, String computeRegion, String productId, String referenceImageId)
      throws IOException {
    try (ProductSearchClient client = ProductSearchClient.create()) {

      // Get the full path of the reference image.
      String formattedName =
          ImageName.format(projectId, computeRegion, productId, referenceImageId);
      // Get complete detail of the reference image.
      ReferenceImage image = client.getReferenceImage(formattedName);
      // Display the reference image information.
      System.out.println(String.format("Reference image name: %s", image.getName()));
      System.out.println(
          String.format(
              "Reference image id: %s",
              image.getName().substring(image.getName().lastIndexOf('/') + 1)));
      System.out.println(String.format("Reference image uri: %s", image.getUri()));
      System.out.println(
          String.format(
              "Reference image bounding polygons: %s \n", image.getBoundingPolysList().toString()));
    }
  }
  // [END vision_product_search_get_reference_image]

  // [START vision_product_search_delete_reference_image]
  /**
   * Delete a reference image.
   *
   * @param projectId - Id of the project.
   * @param computeRegion - Region name.
   * @param productId - Id of the product.
   * @param referenceImageId - Id of the image.
   * @throws IOException - on I/O errors.
   */
  public static void deleteReferenceImage(
      String projectId, String computeRegion, String productId, String referenceImageId)
      throws IOException {
    try (ProductSearchClient client = ProductSearchClient.create()) {

      // Get the full path of the reference image.
      String formattedName =
          ImageName.format(projectId, computeRegion, productId, referenceImageId);
      // Delete the reference image.
      client.deleteReferenceImage(formattedName);
      System.out.println("Reference image deleted from product.");
    }
  }
  // [END vision_product_search_delete_reference_image]

  public static void main(String[] args) throws Exception {
    ReferenceImageManagement referenceImageManagement = new ReferenceImageManagement();
    referenceImageManagement.argsHelper(args, System.out);
  }

  public static void argsHelper(String[] args, PrintStream out) throws Exception {
    ArgumentParser parser = ArgumentParsers.newFor("Reference Image Management").build();
    Subparsers subparsers = parser.addSubparsers().dest("command");

    Subparser createReferenceImageParser = subparsers.addParser("create_reference_image");
    createReferenceImageParser.addArgument("productId");
    createReferenceImageParser.addArgument("referenceImageId");
    createReferenceImageParser.addArgument("gcsUri");

    Subparser listReferenceImagesOfProductParser =
        subparsers.addParser("list_reference_images_of_product");
    listReferenceImagesOfProductParser.addArgument("productId");

    Subparser getReferenceImageParser = subparsers.addParser("get_reference_image");
    getReferenceImageParser.addArgument("productId");
    getReferenceImageParser.addArgument("referenceImageId");

    Subparser deleteReferenceImageParser = subparsers.addParser("delete_reference_image");
    deleteReferenceImageParser.addArgument("productId");
    deleteReferenceImageParser.addArgument("referenceImageId");

    String projectId = System.getenv("PROJECT_ID");
    String computeRegion = System.getenv("REGION_NAME");

    Namespace ns = null;
    try {
      ns = parser.parseArgs(args);
      if (ns.get("command").equals("create_reference_image")) {
        createReferenceImage(
            projectId,
            computeRegion,
            ns.getString("productId"),
            ns.getString("referenceImageId"),
            ns.getString("gcsUri"));
      }
      if (ns.get("command").equals("list_reference_images_of_product")) {
        listReferenceImagesOfProduct(projectId, computeRegion, ns.getString("productId"));
      }
      if (ns.get("command").equals("get_reference_image")) {
        getReferenceImage(
            projectId, computeRegion, ns.getString("productId"), ns.getString("referenceImageId"));
      }
      if (ns.get("command").equals("delete_reference_image")) {
        deleteReferenceImage(
            projectId, computeRegion, ns.getString("productId"), ns.getString("referenceImageId"));
      }

    } catch (ArgumentParserException e) {
      parser.handleError(e);
    }
  }
}


text:
void SwizzClassMethod(Class origClass, Class replaceClass, SEL origSel, SEL replaceSel)
{
    Method origMethod = class_getClassMethod(origClass, origSel);
    Method newMethod = class_getClassMethod(replaceClass, replaceSel);

    origClass = object_getClass((id)origClass);

    if(class_addMethod(origClass, origSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(origClass, replaceSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
        method_exchangeImplementations(origMethod, newMethod);
}

void SwizzInstanceMethod(Class origClass, Class replaceClass, SEL origSel, SEL replaceSel)
{
    Method origMethod = class_getInstanceMethod(origClass, origSel);
    Method newMethod = class_getInstanceMethod(replaceClass, replaceSel);
    if(class_addMethod(origClass, origSel, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(origClass, replaceSel, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
    else
        method_exchangeImplementations(origMethod, newMethod);
}


text:
package org.kuali.rice.kim.api.responsibility;

import org.kuali.rice.core.api.mo.common.GloballyUnique;
import org.kuali.rice.core.api.mo.common.Identifiable;
import org.kuali.rice.core.api.mo.common.Versioned;
import org.kuali.rice.core.api.mo.common.active.Inactivatable;
import org.kuali.rice.kim.api.common.template.TemplateContract;

import java.util.Map;

/**
 * This is the contract for a Responsibility. Represents a single Responsibility within the system.
 *
 * Responsibilities are attached to roles. All authorization checks should be done against Responsibilities,
 * never against roles or groups.
 *
 */
public interface ResponsibilityContract extends Versioned, GloballyUnique, Inactivatable, Identifiable{;

    /**
     * The namespace code that this Responsibility belongs too.
     *
     * @return namespaceCode
     */
    String getNamespaceCode();

    /**
     * The name of the Responsibility.
     *
     * @return name
     */
    String getName();

    /**
     * The description of the Responsibility.
     *
     * @return description
     */
	String getDescription();

    /**
     * The Template referenced by the Responsibility.
     *
     * @return templateId
     */
	TemplateContract getTemplate();

   /**
	 * Attributes for a Responsibility.
	 *
	 * @return attributes
	 */
    Map<String, String> getAttributes();
}


text:
require 'spec_helper'

describe 'expect header' do

  it 'should find exact match for header content' do
    mock_get('simple_get', {'Content-Type' => 'application/json'})
    get '/simple_get'
    expect_header(:content_type, 'application/json')
  end

  it 'should find exact match for header content' do
    mock_get('simple_get', {'Content-Type' => 'json'})
    get '/simple_get'
    expect{expect_header(:content_type, 'application/json')}.to raise_error
  end

  it 'should ensure correct headers are present' do
    mock_get('simple_get', {'Content-Type' => 'application/json'})
    get '/simple_get'
    expect{expect_header(:foo, 'bar')}.to raise_error
  end

end


text:
package io.grpc.internal;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Preconditions.checkState;

import com.google.common.annotations.VisibleForTesting;
import io.grpc.Codec;
import io.grpc.Decompressor;
import io.grpc.Status;
import java.io.Closeable;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.DataFormatException;
import javax.annotation.Nullable;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * Deframer for GRPC frames.
 *
 * <p>This class is not thread-safe. Unless otherwise stated, all calls to public methods should be
 * made in the deframing thread.
 */
@NotThreadSafe
public class MessageDeframer implements Closeable, Deframer {
  private static final int HEADER_LENGTH = 5;
  private static final int COMPRESSED_FLAG_MASK = 1;
  private static final int RESERVED_MASK = 0xFE;
  private static final int MAX_BUFFER_SIZE = 1024 * 1024 * 2;

  /**
   * A listener of deframing events. These methods will be invoked from the deframing thread.
   */
  public interface Listener {

    /**
     * Called when the given number of bytes has been read from the input source of the deframer.
     * This is typically used to indicate to the underlying transport that more data can be
     * accepted.
     *
     * @param numBytes the number of bytes read from the deframer's input source.
     */
    void bytesRead(int numBytes);

    /**
     * Called to deliver the next complete message.
     *
     * @param producer single message producer wrapping the message.
     */
    void messagesAvailable(StreamListener.MessageProducer producer);

    /**
     * Called when the deframer closes.
     *
     * @param hasPartialMessage whether the deframer contained an incomplete message at closing.
     */
    void deframerClosed(boolean hasPartialMessage);

    /**
     * Called when a {@link #deframe(ReadableBuffer)} operation failed.
     *
     * @param cause the actual failure
     */
    void deframeFailed(Throwable cause);
  }

  private enum State {
    HEADER, BODY
  }

  private Listener listener;
  private int maxInboundMessageSize;
  private final StatsTraceContext statsTraceCtx;
  private final String debugString;
  private Decompressor decompressor;
  private GzipInflatingBuffer fullStreamDecompressor;
  private byte[] inflatedBuffer;
  private int inflatedIndex;
  private State state = State.HEADER;
  private int requiredLength = HEADER_LENGTH;
  private boolean compressedFlag;
  private CompositeReadableBuffer nextFrame;
  private CompositeReadableBuffer unprocessed = new CompositeReadableBuffer();
  private long pendingDeliveries;
  private boolean inDelivery = false;
  private int currentMessageSeqNo = -1;
  private int inboundBodyWireSize;

  private boolean closeWhenComplete = false;
  private volatile boolean stopDelivery = false;

  /**
   * Create a deframer.
   *
   * @param listener listener for deframer events.
   * @param decompressor the compression used if a compressed frame is encountered, with
   *  {@code NONE} meaning unsupported
   * @param maxMessageSize the maximum allowed size for received messages.
   * @param debugString a string that will appear on errors statuses
   */
  public MessageDeframer(Listener listener, Decompressor decompressor, int maxMessageSize,
      StatsTraceContext statsTraceCtx, String debugString) {
    this.listener = checkNotNull(listener, "sink");
    this.decompressor = checkNotNull(decompressor, "decompressor");
    this.maxInboundMessageSize = maxMessageSize;
    this.statsTraceCtx = checkNotNull(statsTraceCtx, "statsTraceCtx");
    this.debugString = debugString;
  }

  void setListener(Listener listener) {
    this.listener = listener;
  }

  @Override
  public void setMaxInboundMessageSize(int messageSize) {
    maxInboundMessageSize = messageSize;
  }

  @Override
  public void setDecompressor(Decompressor decompressor) {
    checkState(fullStreamDecompressor == null, "Already set full stream decompressor");
    this.decompressor = checkNotNull(decompressor, "Can't pass an empty decompressor");
  }

  @Override
  public void setFullStreamDecompressor(GzipInflatingBuffer fullStreamDecompressor) {
    checkState(decompressor == Codec.Identity.NONE, "per-message decompressor already set");
    checkState(this.fullStreamDecompressor == null, "full stream decompressor already set");
    this.fullStreamDecompressor =
        checkNotNull(fullStreamDecompressor, "Can't pass a null full stream decompressor");
    unprocessed = null;
  }

  @Override
  public void request(int numMessages) {
    checkArgument(numMessages > 0, "numMessages must be > 0");
    if (isClosed()) {
      return;
    }
    pendingDeliveries += numMessages;
    deliver();
  }

  @Override
  public void deframe(ReadableBuffer data) {
    checkNotNull(data, "data");
    boolean needToCloseData = true;
    try {
      if (!isClosedOrScheduledToClose()) {
        if (fullStreamDecompressor != null) {
          fullStreamDecompressor.addGzippedBytes(data);
        } else {
          unprocessed.addBuffer(data);
        }
        needToCloseData = false;

        deliver();
      }
    } finally {
      if (needToCloseData) {
        data.close();
      }
    }
  }

  @Override
  public void closeWhenComplete() {
    if (isClosed()) {
      return;
    } else if (isStalled()) {
      close();
    } else {
      closeWhenComplete = true;
    }
  }

  /**
   * Sets a flag to interrupt delivery of any currently queued messages. This may be invoked outside
   * of the deframing thread, and must be followed by a call to {@link #close()} in the deframing
   * thread. Without a subsequent call to {@link #close()}, the deframer may hang waiting for
   * additional messages before noticing that the {@code stopDelivery} flag has been set.
   */
  void stopDelivery() {
    stopDelivery = true;
  }

  @Override
  public void close() {
    if (isClosed()) {
      return;
    }
    boolean hasPartialMessage = nextFrame != null && nextFrame.readableBytes() > 0;
    try {
      if (fullStreamDecompressor != null) {
        hasPartialMessage = hasPartialMessage || fullStreamDecompressor.hasPartialData();
        fullStreamDecompressor.close();
      }
      if (unprocessed != null) {
        unprocessed.close();
      }
      if (nextFrame != null) {
        nextFrame.close();
      }
    } finally {
      fullStreamDecompressor = null;
      unprocessed = null;
      nextFrame = null;
    }
    listener.deframerClosed(hasPartialMessage);
  }

  /**
   * Indicates whether or not this deframer has been closed.
   */
  public boolean isClosed() {
    return unprocessed == null && fullStreamDecompressor == null;
  }

  /** Returns true if this deframer has already been closed or scheduled to close. */
  private boolean isClosedOrScheduledToClose() {
    return isClosed() || closeWhenComplete;
  }

  private boolean isStalled() {
    if (fullStreamDecompressor != null) {
      return fullStreamDecompressor.isStalled();
    } else {
      return unprocessed.readableBytes() == 0;
    }
  }

  /**
   * Reads and delivers as many messages to the listener as possible.
   */
  private void deliver() {
    // We can have reentrancy here when using a direct executor, triggered by calls to
    // request more messages. This is safe as we simply loop until pendingDelivers = 0
    if (inDelivery) {
      return;
    }
    inDelivery = true;
    try {
      // Process the uncompressed bytes.
      while (!stopDelivery && pendingDeliveries > 0 && readRequiredBytes()) {
        switch (state) {
          case HEADER:
            processHeader();
            break;
          case BODY:
            // Read the body and deliver the message.
            processBody();

            // Since we've delivered a message, decrement the number of pending
            // deliveries remaining.
            pendingDeliveries--;
            break;
          default:
            throw new AssertionError("Invalid state: " + state);
        }
      }

      if (stopDelivery) {
        close();
        return;
      }

      /*
       * We are stalled when there are no more bytes to process. This allows delivering errors as
       * soon as the buffered input has been consumed, independent of whether the application
       * has requested another message.  At this point in the function, either all frames have been
       * delivered, or unprocessed is empty.  If there is a partial message, it will be inside next
       * frame and not in unprocessed.  If there is extra data but no pending deliveries, it will
       * be in unprocessed.
       */
      if (closeWhenComplete && isStalled()) {
        close();
      }
    } finally {
      inDelivery = false;
    }
  }

  /**
   * Attempts to read the required bytes into nextFrame.
   *
   * @return {@code true} if all of the required bytes have been read.
   */
  private boolean readRequiredBytes() {
    int totalBytesRead = 0;
    int deflatedBytesRead = 0;
    try {
      if (nextFrame == null) {
        nextFrame = new CompositeReadableBuffer();
      }

      // Read until the buffer contains all the required bytes.
      int missingBytes;
      while ((missingBytes = requiredLength - nextFrame.readableBytes()) > 0) {
        if (fullStreamDecompressor != null) {
          try {
            if (inflatedBuffer == null || inflatedIndex == inflatedBuffer.length) {
              inflatedBuffer = new byte[Math.min(missingBytes, MAX_BUFFER_SIZE)];
              inflatedIndex = 0;
            }
            int bytesToRead = Math.min(missingBytes, inflatedBuffer.length - inflatedIndex);
            int n = fullStreamDecompressor.inflateBytes(inflatedBuffer, inflatedIndex, bytesToRead);
            totalBytesRead += fullStreamDecompressor.getAndResetBytesConsumed();
            deflatedBytesRead += fullStreamDecompressor.getAndResetDeflatedBytesConsumed();
            if (n == 0) {
              // No more inflated data is available.
              return false;
            }
            nextFrame.addBuffer(ReadableBuffers.wrap(inflatedBuffer, inflatedIndex, n));
            inflatedIndex += n;
          } catch (IOException e) {
            throw new RuntimeException(e);
          } catch (DataFormatException e) {
            throw new RuntimeException(e);
          }
        } else {
          if (unprocessed.readableBytes() == 0) {
            // No more data is available.
            return false;
          }
          int toRead = Math.min(missingBytes, unprocessed.readableBytes());
          totalBytesRead += toRead;
          nextFrame.addBuffer(unprocessed.readBytes(toRead));
        }
      }
      return true;
    } finally {
      if (totalBytesRead > 0) {
        listener.bytesRead(totalBytesRead);
        if (state == State.BODY) {
          if (fullStreamDecompressor != null) {
            // With compressed streams, totalBytesRead can include gzip header and trailer metadata
            statsTraceCtx.inboundWireSize(deflatedBytesRead);
            inboundBodyWireSize += deflatedBytesRead;
          } else {
            statsTraceCtx.inboundWireSize(totalBytesRead);
            inboundBodyWireSize += totalBytesRead;
          }
        }
      }
    }
  }

  /**
   * Processes the GRPC compression header which is composed of the compression flag and the outer
   * frame length.
   */
  private void processHeader() {
    int type = nextFrame.readUnsignedByte();
    if ((type & RESERVED_MASK) != 0) {
      throw Status.INTERNAL.withDescription(
          debugString + ": Frame header malformed: reserved bits not zero")
          .asRuntimeException();
    }
    compressedFlag = (type & COMPRESSED_FLAG_MASK) != 0;

    // Update the required length to include the length of the frame.
    requiredLength = nextFrame.readInt();
    if (requiredLength < 0 || requiredLength > maxInboundMessageSize) {
      throw Status.RESOURCE_EXHAUSTED.withDescription(
          String.format("%s: Frame size %d exceeds maximum: %d. ",
              debugString, requiredLength, maxInboundMessageSize))
          .asRuntimeException();
    }

    currentMessageSeqNo++;
    statsTraceCtx.inboundMessage(currentMessageSeqNo);
    // Continue reading the frame body.
    state = State.BODY;
  }

  /**
   * Processes the GRPC message body, which depending on frame header flags may be compressed.
   */
  private void processBody() {
    // There is no reliable way to get the uncompressed size per message when it's compressed,
    // because the uncompressed bytes are provided through an InputStream whose total size is
    // unknown until all bytes are read, and we don't know when it happens.
    statsTraceCtx.inboundMessageRead(currentMessageSeqNo, inboundBodyWireSize, -1);
    inboundBodyWireSize = 0;
    InputStream stream = compressedFlag ? getCompressedBody() : getUncompressedBody();
    nextFrame = null;
    listener.messagesAvailable(new SingleMessageProducer(stream));

    // Done with this frame, begin processing the next header.
    state = State.HEADER;
    requiredLength = HEADER_LENGTH;
  }

  private InputStream getUncompressedBody() {
    statsTraceCtx.inboundUncompressedSize(nextFrame.readableBytes());
    return ReadableBuffers.openStream(nextFrame, true);
  }

  private InputStream getCompressedBody() {
    if (decompressor == Codec.Identity.NONE) {
      throw Status.INTERNAL.withDescription(
          debugString + ": Can't decode compressed frame as compression not configured.")
          .asRuntimeException();
    }

    try {
      // Enforce the maxMessageSize limit on the returned stream.
      InputStream unlimitedStream =
          decompressor.decompress(ReadableBuffers.openStream(nextFrame, true));
      return new SizeEnforcingInputStream(
          unlimitedStream, maxInboundMessageSize, statsTraceCtx, debugString);
    } catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  /**
   * An {@link InputStream} that enforces the {@link #maxMessageSize} limit for compressed frames.
   */
  @VisibleForTesting
  static final class SizeEnforcingInputStream extends FilterInputStream {
    private final int maxMessageSize;
    private final StatsTraceContext statsTraceCtx;
    private final String debugString;
    private long maxCount;
    private long count;
    private long mark = -1;

    SizeEnforcingInputStream(InputStream in, int maxMessageSize, StatsTraceContext statsTraceCtx,
        String debugString) {
      super(in);
      this.maxMessageSize = maxMessageSize;
      this.statsTraceCtx = statsTraceCtx;
      this.debugString = debugString;
    }

    @Override
    public int read() throws IOException {
      int result = in.read();
      if (result != -1) {
        count++;
      }
      verifySize();
      reportCount();
      return result;
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
      int result = in.read(b, off, len);
      if (result != -1) {
        count += result;
      }
      verifySize();
      reportCount();
      return result;
    }

    @Override
    public long skip(long n) throws IOException {
      long result = in.skip(n);
      count += result;
      verifySize();
      reportCount();
      return result;
    }

    @Override
    public synchronized void mark(int readlimit) {
      in.mark(readlimit);
      mark = count;
      // it's okay to mark even if mark isn't supported, as reset won't work
    }

    @Override
    public synchronized void reset() throws IOException {
      if (!in.markSupported()) {
        throw new IOException("Mark not supported");
      }
      if (mark == -1) {
        throw new IOException("Mark not set");
      }

      in.reset();
      count = mark;
    }

    private void reportCount() {
      if (count > maxCount) {
        statsTraceCtx.inboundUncompressedSize(count - maxCount);
        maxCount = count;
      }
    }

    private void verifySize() {
      if (count > maxMessageSize) {
        throw Status.RESOURCE_EXHAUSTED.withDescription(String.format(
                "%s: Compressed frame exceeds maximum frame size: %d. Bytes read: %d. ",
                debugString, maxMessageSize, count)).asRuntimeException();
      }
    }
  }

  private static class SingleMessageProducer implements StreamListener.MessageProducer {
    private InputStream message;

    private SingleMessageProducer(InputStream message) {
      this.message = message;
    }

    @Nullable
    @Override
    public InputStream next() {
      InputStream messageToReturn = message;
      message = null;
      return messageToReturn;
    }
  }
}


text:
package main

import (
	"hawx.me/code/serve"

	"flag"
	"log"
	"net"
	"net/http"
	"net/http/httputil"
	"path"
	"strings"
)

var (
	socketDir = flag.String("socket-dir", "", "")
	port      = flag.String("port", "8080", "")
)

func unixDial(sock string) func(string, string) (net.Conn, error) {
	return func(proto, addr string) (conn net.Conn, err error) {
		return net.Dial("unix", sock)
	}
}

func director(req *http.Request) {
	req.URL.Scheme = "http"
	req.URL.Host = "dev"
}

func main() {
	flag.Parse()

	if *socketDir == "" {
		log.Fatal("--socket-dir required")
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		log.Println(r.Method, r.Host, r.URL)

		if !strings.HasSuffix(r.Host, ".dev") {
			return
		}

		hostName := strings.TrimSuffix(r.Host, ".dev")
		sockPath := path.Join(*socketDir, hostName+".sock")

		proxy := &httputil.ReverseProxy{
			Director:  director,
			Transport: &http.Transport{Dial: unixDial(sockPath)},
		}

		proxy.ServeHTTP(w, r)
	})

	serve.Port(*port, http.DefaultServeMux)
}


text:
BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Only in standalone mode, import the basic install script.
if [[ "${BASH_SOURCE[0]}" -ef "$0" ]]; then
  source "${BASE_DIR}/../common-libs/nodejs-common/bin/install_functions.sh"
fi

# Solution name.
SOLUTION_NAME="tentacles"

# Project namespace will be used as prefix of the name of Cloud Functions,
# Pub/Sub topics, etc.
# Default project namespace is SOLUTION_NAME.
# Note: only lowercase letters, numbers and dashes(-) are allowed.
PROJECT_NAMESPACE="${SOLUTION_NAME}"

# Project configuration file.
CONFIG_FILE="./config.json"

# Parameter name used by functions to load and save config.
CONFIG_ITEMS=(
  "PROJECT_NAMESPACE"
  "REGION"
  "GCS_BUCKET"
  "OUTBOUND"
  "ENABLE_VISUALIZATION"
  "DATASET"
  "DATASET_LOCATION"
)

# Default name for BigQuery dataset to receive logs.
DATASET="${SOLUTION_NAME}"
# BigQuery table name for logs.
BIGQUERY_LOG_TABLE="winston_log"

# The Google Cloud APIs that will be used in Tentacles.
GOOGLE_CLOUD_APIS["firestore.googleapis.com"]="Cloud Firestore API"
GOOGLE_CLOUD_APIS["cloudfunctions.googleapis.com"]="Cloud Functions API"
GOOGLE_CLOUD_APIS["pubsub.googleapis.com"]="Cloud Pub/Sub API"

# Description of external APIs.
INTEGRATION_APIS_DESCRIPTION=(
  "Google Analytics Measurement Protocol"
  "Google Analytics 4 Measurement Protocol"
  "Google Analytics Data Import"
  "Campaign Manager Conversions Upload"
  "Search Ads 360 Conversions Upload"
  "Google Ads API for Customer Match Upload"
  "Google Ads API for Call Conversions Upload"
  "Google Ads API for Click Conversions Upload"
  "Google Ads API for Enhanced Conversions Upload"
  "Google Ads API for Offline UserData Upload (OfflineUserDataService)"
  "Google Sheets API for Google Ads Conversions Upload based on Google Sheets"
  "SFTP Upload for Search Ads 360 Business Data Upload"
  "Pub/Sub Messages Send"
)

# All build-in external APIs.
INTEGRATION_APIS=(
  "N/A"
  "N/A"
  "analytics.googleapis.com"
  "dfareporting.googleapis.com doubleclicksearch.googleapis.com"
  "doubleclicksearch.googleapis.com"
  "googleads.googleapis.com"
  "googleads.googleapis.com"
  "googleads.googleapis.com"
  "googleads.googleapis.com"
  "googleads.googleapis.com"
  "sheets.googleapis.com"
  "N/A"
  "N/A"  # Pub/Sub will use ADC auth instead of OAuth or JWT.
)

# Code of external APIs. Used to create different Pub/Sub topics and
# subscriptions for different APIs.
INTEGRATION_APIS_CODE=(
  "MP"
  "MP_GA4"
  "GA"
  "CM"
  "SA"
  "ACM"
  "CALL"
  "ACLC"
  "ACA"
  "AOUD"
  "GS"
  "SFTP"
  "PB"
)

# Codes of selected APIs, for creating the topics and subscriptions.
SELECTED_APIS_CODES=()

# Common permissions to install Tentacles.
# https://cloud.google.com/service-usage/docs/access-control
# https://cloud.google.com/storage/docs/access-control/iam-roles
# https://cloud.google.com/pubsub/docs/access-control
# https://cloud.google.com/iam/docs/understanding-roles#service-accounts-roles
# https://cloud.google.com/functions/docs/reference/iam/roles
# https://cloud.google.com/firestore/docs/security/iam#roles
GOOGLE_CLOUD_PERMISSIONS=(
  ["Service Management Administrator"]="servicemanagement.services.bind"
  ["Service Usage Admin"]="serviceusage.services.enable"
  ["Storage Admin"]="storage.buckets.create storage.buckets.list"
  ["Pub/Sub Editor"]="pubsub.subscriptions.create pubsub.topics.create"
  ["Service Account User"]="iam.serviceAccounts.actAs"
  ["Cloud Functions Developer"]="cloudfunctions.functions.create"
  ["Cloud Datastore User"]="appengine.applications.get \
    datastore.databases.get \
    datastore.entities.create \
    resourcemanager.projects.get"
)

#######################################
# Save the codes of selected APIs to 'SELECTED_APIS_CODES'. This will be
# used to create Pub/Sub topics and subscriptions.
# Globals:
#   INTEGRATION_APIS_CODE
#   SELECTED_APIS_CODES
# Arguments:
#   Selected API index
#######################################
extra_operation_for_confirm_api() {
  SELECTED_APIS_CODES+=("${INTEGRATION_APIS_CODE["${1}"]}")
}

#######################################
# Confirm that APIs that Tentacles will integrate.
# Globals:
#   None
# Arguments:
#   None
#######################################
confirm_integration_api() {
  (( STEP += 1 ))
  printf '%s\n' "Step ${STEP}: Confirm the integration with external APIs..."
  confirm_apis "extra_operation_for_confirm_api"
}

#######################################
# Checks if the user want to use visualization feature of Tentacles. This
# feature requires BigQuery.
# Globals:
#   ENABLE_VISUALIZATION
# Arguments:
#   none
#######################################
confirm_visualization() {
  (( STEP += 1 ))
  printf '%s\n' "Step ${STEP}: Confirm enabling the visualization feature..."
  printf '%s' "    Tentacles can provide a structured overview of processed \
files and errors based on BigQuery and Data Studio. \
Do you want to enable it? [Y/n]: "
  local input
  read -r input
  if [[ ${input} == 'n' || ${input} == 'N' ]]; then
    declare -g "ENABLE_VISUALIZATION=false"
    printf '%s\n' "Skipped visualization feature."
  else
    declare -g "ENABLE_VISUALIZATION=true"
    GOOGLE_CLOUD_PERMISSIONS["BigQuery Data Editor"]="bigquery.datasets.create"
    GOOGLE_CLOUD_PERMISSIONS["Logs Writer"]="logging.logEntries.create"
    GOOGLE_CLOUD_PERMISSIONS["Logs Configuration Writer"]=\
"logging.sinks.create"
# Role 'Project IAM Admin' or 'Security Admin' has the permission
# 'bigquery.datasets.setIamPolicy'
    GOOGLE_CLOUD_PERMISSIONS["Project IAM Admin"]=\
"resourcemanager.projects.setIamPolicy"
    printf '%s\n' "OK. Visualization feature has been confirmed."
  fi
}

#######################################
# Create Pub/Sub topics and subscriptions based on the selected APIs.
# Globals:
#   PROJECT_NAMESPACE
#   SELECTED_APIS_CODES
# Arguments:
#   None
# Returns:
#   0 if all topics and subscriptions are created, non-zero on error.
#######################################
create_subscriptions() {
  (( STEP += 1 ))
  cat <<EOF
Step ${STEP}: Creating topics and subscriptions for Pub/Sub...
  Pub/Sub subscribers will not receive messages until subscriptions are created.
EOF

  node -e "require('./index.js').initPubsub(process.argv[1], \
process.argv.slice(2))" "${PROJECT_NAMESPACE}"  "${SELECTED_APIS_CODES[@]}"
  if [[ $? -gt 0 ]]; then
    echo "Failed to create Pub/Sub topics or subscriptions."
    return 1
  else
    echo "OK. Successfully created Pub/Sub topics and subscriptions."
    return 0
  fi
}

#######################################
# Deploy Cloud Functions 'Initiator'.
# Globals:
#   PROJECT_NAMESPACE
#   GCS_BUCKET
#   OUTBOUND
# Arguments:
#   None
#######################################
deploy_cloud_functions_initiator(){
  local cf_flag=()
  cf_flag+=(--entry-point=initiate)
  cf_flag+=(--trigger-bucket="${GCS_BUCKET}")
  cf_flag+=(--set-env-vars=TENTACLES_OUTBOUND="${OUTBOUND}")
  set_cloud_functions_default_settings cf_flag
  printf '%s\n' " 1. '${PROJECT_NAMESPACE}_init' is triggered by new files \
from Cloud Storage bucket [${GCS_BUCKET}]."
  gcloud functions deploy "${PROJECT_NAMESPACE}"_init "${cf_flag[@]}"
  quit_if_failed $?
}

#######################################
# Deploy Cloud Functions 'Transporter'.
# Globals:
#   PROJECT_NAMESPACE
# Arguments:
#   None
#######################################
deploy_cloud_functions_transporter(){
  local cf_flag=()
  cf_flag+=(--entry-point=transport)
  cf_flag+=(--trigger-topic="${PROJECT_NAMESPACE}-trigger")
  set_cloud_functions_default_settings cf_flag
  printf '%s\n' " 2. '${PROJECT_NAMESPACE}_tran' is triggered by new messages \
from Pub/Sub topic [${PROJECT_NAMESPACE}-trigger]."
  gcloud functions deploy "${PROJECT_NAMESPACE}"_tran "${cf_flag[@]}"
  quit_if_failed $?
}

#######################################
# Deploy Cloud Functions 'Api Requester'.
# Globals:
#   PROJECT_NAMESPACE
# Arguments:
#   None
#######################################
deploy_cloud_functions_api_requester(){
  local cf_flag=()
  cf_flag+=(--entry-point=requestApi)
  cf_flag+=(--trigger-topic="${PROJECT_NAMESPACE}-push")
  set_authentication_env_for_cloud_functions cf_flag
  set_cloud_functions_default_settings cf_flag
  printf '%s\n' " 3. '${PROJECT_NAMESPACE}_api' is triggered by new messages \
from Pub/Sub topic [${PROJECT_NAMESPACE}-push]."
  gcloud functions deploy "${PROJECT_NAMESPACE}"_api "${cf_flag[@]}"
  quit_if_failed $?
}

#######################################
# Deploy three Cloud Functions for Tentacles.
# Globals:
#   REGION
#   CF_RUNTIME
#   PROJECT_NAMESPACE
#   GCS_BUCKET
#   OUTBOUND
#   SA_KEY_FILE
# Arguments:
#   None
# Returns:
#   0 if all Cloud Functions deployed, non-zero on error.
#######################################
deploy_tentacles() {
  (( STEP += 1 ))
  printf '%s\n' "Step ${STEP}: Starting to deploy Tentacles..."
  printf '%s\n' "Tentacles is composed of three Cloud Functions."
  printf '%s\n' "The Cloud Functions will be deployed to ${REGION}."

  deploy_cloud_functions_initiator
  deploy_cloud_functions_transporter
  deploy_cloud_functions_api_requester
}

#######################################
# Reselect APIs to integrate.
# Globals:
#   None
# Arguments:
#   None
#######################################
reselect_api() {
  load_config
  check_in_cloud_shell
  prepare_dependencies
  confirm_integration_api
  confirm_auth_method
  enable_apis
  create_subscriptions
  do_authentication
  deploy_cloud_functions_api_requester
  post_installation
}

#######################################
# Generate a default Log Router sink's name based on the project namespace.
# Globals:
#   PROJECT_NAMESPACE
# Arguments:
#   Any string.
# Returns:
#   The default sink name.
#######################################
get_default_sink_name() {
  printf '%s' "${PROJECT_NAMESPACE}_log"
}

#######################################
# Set up visualization features for Tentacles, including:
# 1. Create or update the sink;
# 2. Create or update the dataset;
# 3. Send out a dummy log to trigger the creation of BigQuery table.
# Globals:
#   ENABLE_VISUALIZATION
# Arguments:
#   None
#######################################
prepare_visualization() {
  while [[ -z "${ENABLE_VISUALIZATION}" ]]; do
    confirm_visualization
  done
  if [[ "${ENABLE_VISUALIZATION}" == "false" ]]; then
    return
  fi
  # Create the dataset
  confirm_located_dataset DATASET DATASET_LOCATION REGION
  printf '\n'
  # Create the sink
  create_sink_for_visualization
  # If table exists, confirm to delete it or use it.
  local table_name="${DATASET}.${BIGQUERY_LOG_TABLE}"
  if [[ $(check_existence_in_bigquery "${table_name}") -eq 0 ]]; then
    printf '%s' "  The table [${table_name}] exists. Do you want to delete \
it? [y/N]: "
    local input
    read -r input
    if [[ -z ${input} || ${input} == 'n' || ${input} == 'N' ]]; then
      # Select to use the existing table.
      printf '%s\n' "OK. Continue using the existing table.";
      return 0
    else
      printf '%s\n' "Deleting table: ${table_name}...";
      bq rm -f -t "${table_name}"
    fi
  fi
  send_dummy_log_to_create_sink_table
}

#######################################
# Send a dummy log to create target BigQuery table if it doesn't exist.
# Globals:
#   BIGQUERY_LOG_TABLE
# Arguments:
#   None
#######################################
send_dummy_log_to_create_sink_table(){
  gcloud logging write ${BIGQUERY_LOG_TABLE} '{"message": "[TentaclesFile]"}' \
--payload-type=json --severity=INFO
}

#######################################
# Complete visualization features for Tentacles, including:
# 1. Make sure the log table exists.
# 2. Create or update all views.
# Globals:
#   ENABLE_VISUALIZATION
# Arguments:
#   None
#######################################
complete_visualization() {
  while [[ -z "${ENABLE_VISUALIZATION}" ]]; do
    confirm_visualization
  done
  if [[ "${ENABLE_VISUALIZATION}" == "false" ]]; then
    return
  fi
  (( STEP += 1 ))
  printf '%s\n' "Step ${STEP}: Creating Views for visualization..."
  # Confirm table is ready
  local table_name="${DATASET}.${BIGQUERY_LOG_TABLE}"
  while [[ $(check_existence_in_bigquery "${table_name}") -gt 0 ]]; do
    send_dummy_log_to_create_sink_table
    printf '%s\n' "The table ${table_name} is not ready."
    printf '\n%s' "Press any key to check again..."
    local any
    read -n1 -s any
  done
  create_visualization_views
  printf '%s\n' "OK. Views for visualization have been created."
}

#######################################
# Create Log Export to capture BigQuery events and granted the permission to
# publish message to Cloud Pub/Sub.
# This function requires two permissions beyond the default role 'Editor':
# logging.sinks.create - sample role: Logs Configuration Writer
# resourcemanager.projects.setIamPolicy - sample role: Project IAM Admin
# Globals:
#   GCP_PROJECT
#   PROJECT_NAMESPACE
# Arguments:
#   None
# Returns:
#   1 if failed to create log sink
#######################################
create_sink_for_visualization() {
  (( STEP += 1 ))
  printf '%s\n' "Step ${STEP}: Creating Logs router sink..."
  printf '%s\n' "  Tentacles leverages Logs router to send logs to BigQuery."
  local sinkName logFilter sinkDest
  sinkName=$(get_default_sink_name)
  logFilter='jsonPayload.message=~("TentaclesFile" OR "TentaclesTask" OR
    "TentaclesFailedRecord")'
  sinkDest="bigquery.googleapis.com/projects/${GCP_PROJECT}/datasets/${DATASET}"

  create_or_update_sink ${sinkName} "${logFilter}" \
"${sinkDest} --use-partitioned-tables"
  confirm_sink_service_account_permission ${sinkName} "bigquery.dataEditor" \
"BigQuery Data Editor"
}

#######################################
# Creates all visualization views.
# Globals:
#  GCP_PROJECT
#  DATASET
#  BIGQUERY_LOG_TABLE
# Arguments:
#  none
#######################################
create_visualization_views() {
  create_or_update_view RawJson '
    SELECT
      timestamp,
      REGEXP_EXTRACT(message, r"\[([^]]+)\]")   AS entity,
      REGEXP_EXTRACT(message, r"\[[^]]+\](.*)") AS json
    FROM (
      SELECT
        REPLACE(jsonpayload.message, "\n","")   AS message,
        timestamp
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.'${BIGQUERY_LOG_TABLE}'`
    )
  '

  create_or_update_view RawTask '
    SELECT
      timestamp,
      JSON_VALUE(json, "$.action")         AS action,
      JSON_VALUE(json, "$.fileId")         AS fileId,
      JSON_VALUE(json, "$.id")             AS id,
      JSON_VALUE(json, "$.api")            AS api,
      JSON_VALUE(json, "$.config")         AS config,
      JSON_VALUE(json, "$.start")          AS startIndex,
      JSON_VALUE(json, "$.end")            AS endIndex,
      JSON_VALUE(json, "$.status")         AS status,
      JSON_VALUE(json, "$.dryRun")         AS dryRun,
      JSON_VALUE(json, "$.createdAt")      AS createdAt,
      JSON_VALUE(json, "$.dataMessageId")  AS dataMessageId,
      JSON_VALUE(json, "$.startSending")   AS startSending,
      JSON_VALUE(json, "$.apiMessageId")   AS apiMessageId,
      JSON_VALUE(json, "$.finishedTime")   AS finishedTime,
      JSON_VALUE(json, "$.error")          AS error,
      JSON_VALUE(json, "$.numberOfLines")  AS numberOfLines,
      JSON_VALUE(json, "$.numberOfFailed") AS numberOfFailed
    FROM
      `'${GCP_PROJECT}'.'${DATASET}'.RawJson`
    WHERE
      entity = "TentaclesTask"
    ORDER BY
      timestamp DESC
  '

  create_or_update_view RawFile '
    SELECT
      timestamp,
      JSON_VALUE(json, "$.action")            AS action,
      JSON_VALUE(json, "$.fileId")            AS fileId,
      JSON_VALUE(json, "$.name")              AS name,
      JSON_VALUE(json, "$.size")              AS fileSize,
      JSON_VALUE(json, "$.updated")           AS updated,
      JSON_VALUE(json, "$.error")             AS error,
      JSON_VALUE(json, "$.attributes.api")    AS api,
      JSON_VALUE(json, "$.attributes.config") AS config,
      JSON_VALUE(json, "$.attributes.dryRun") AS dryRun,
      JSON_VALUE(json, "$.attributes.size")   AS splitSize,
      JSON_VALUE(json, "$.attributes.gcs")    AS gcs
    FROM
      `'${GCP_PROJECT}'.'${DATASET}'.RawJson`
    WHERE
      entity = "TentaclesFile"
    ORDER BY
      timestamp DESC
  '

  create_or_update_view RawFailedRecord '
    SELECT
      timestamp                            AS recordUpdatedTime,
      JSON_VALUE(json, "$.taskId")         AS taskId,
      JSON_VALUE(json, "$.error")          AS recordError,
      JSON_VALUE_ARRAY(json, "$.records")  AS records
    FROM
      `'${GCP_PROJECT}'.'${DATASET}'.RawJson`
    WHERE
      entity = "TentaclesFailedRecord"
    ORDER BY
      timestamp DESC
  '

  create_or_update_view TentaclesTask '
    SELECT
      "'${PROJECT_NAMESPACE}'"                           AS namespace,
      "'${GCP_PROJECT}'"                                 AS projectId,
      main.* EXCEPT (error, numberOfLines, numberOfFailed),
      status,
      CASE
        WHEN status = "sending"
          AND DATETIME_DIFF(CURRENT_DATETIME(), startSending, MINUTE) > 9
        THEN "error"
        ELSE status
      END AS taskStatus,
      CASE
        WHEN status = "sending"
          AND DATETIME_DIFF(CURRENT_DATETIME(), startSending, MINUTE) > 9
        THEN "Timeout"
        ELSE error
      END AS taskError,
      DATETIME_DIFF(finishedTime,startSending,SECOND) AS sendingTime,
      DATETIME_DIFF(startSending,createdAt,SECOND)    AS waitingTime,
      CAST(numberOfLines AS INT64)                    AS numberOfLines,
      CAST(numberOfFailed AS INT64)                   AS numberOfFailed
    FROM (
      SELECT
        id                                                       AS taskId,
        ANY_VALUE(fileId)                                        AS fileId,
        ANY_VALUE(api)                                           AS api,
        ANY_VALUE(config)                                        AS config,
        ANY_VALUE(dryRun)                                        AS dryRun,
        ANY_VALUE(error)                                         AS error,
        CAST(ANY_VALUE(startIndex) AS INT64)                     AS startIndex,
        CAST(ANY_VALUE(endIndex) AS INT64)                       AS endIndex,
        PARSE_DATETIME("%FT%H:%M:%E3SZ",ANY_VALUE(createdAt))    AS createdAt,
        PARSE_DATETIME("%FT%H:%M:%E3SZ",ANY_VALUE(startSending)) AS startSending,
        PARSE_DATETIME("%FT%H:%M:%E3SZ",ANY_VALUE(finishedTime)) AS finishedTime,
        ANY_VALUE(dataMessageId)                                 AS dataMessageId,
        ANY_VALUE(apiMessageId)                                  AS apiMessageId,
        MAX(timestamp)                                           AS lastTimestamp,
        ANY_VALUE(numberOfLines)                                 AS numberOfLines,
        ANY_VALUE(numberOfFailed)                                AS numberOfFailed
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.RawTask`
      GROUP BY
        id
      ) AS main
    LEFT JOIN (
      SELECT
        id, timestamp, status
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.RawTask`) AS latest
    ON
      main.taskId=latest.id AND main.lastTimestamp=latest.timestamp
    ORDER BY
      lastTimestamp DESC
  '

  create_or_update_view TentaclesReport '
    SELECT
      "'${PROJECT_NAMESPACE}'"                           AS namespace,
      "'${GCP_PROJECT}'"                                 AS projectId,
      "'${GCS_BUCKET}'"                                  AS bucket,
      file.* EXCEPT (name, fileSize),
      name                                               AS fileFullName,
      REGEXP_REPLACE(file.name, "processed/[^/]*/", "")  AS fileName,
      CAST(file.fileSize AS INT64)                       AS fileSize,
      PARSE_DATETIME("%FT%H:%M:%E3SZ", file.updatedTime) As fileUpdatedTime,
      IFNULL(taskLastUpdatedTime, PARSE_DATETIME("%FT%H:%M:%E3SZ",
        file.updatedTime))                               AS fileLastUpdatedTime,
      taskSummary.* EXCEPT (fileId, failedTask, taskNumber),
      IFNULL(taskNumber, 0)                              AS taskNumber,
      IFNULL(taskError, fileError)                       AS errorMessage,
      CASE
        WHEN (IFNULL(failedTask, fileError)  IS NOT NULL)
        THEN "error"
        WHEN (taskNumber = doneNumber)
        THEN "done"
        ELSE "processing"
      END                                                AS fileStatus,
      task.* EXCEPT (fileId, api, config, dryRun, namespace, projectId),
      failedRecord.* EXCEPT (taskId)
    FROM (
      SELECT
        fileId,
        ANY_VALUE(name)                    AS name,
        ANY_VALUE(fileSize)                AS fileSize,
        ANY_VALUE(updated)                 AS updatedTime,
        ANY_VALUE(api)                     AS api,
        ANY_VALUE(config)                  AS config,
        CAST(ANY_VALUE(dryRun) AS BOOLEAN) AS dryRun,
        ANY_VALUE(error)                   AS fileError
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.RawFile`
      WHERE
        fileId IS NOT NULL
      GROUP BY
        fileId
    ) AS file
    LEFT OUTER JOIN (
      SELECT
        fileId,
        COUNT(taskId)                        AS taskNumber,
        COUNTIF(taskStatus = "done")         AS doneNumber,
        COUNTIF(taskStatus = "queuing")      AS queuingNumber,
        COUNTIF(taskStatus = "sending")      AS sendingNumber,
        COUNTIF(taskStatus = "error")        AS errorNumber,
        COUNTIF(taskStatus = "failed")       AS failedNumber,
        ANY_VALUE(taskError)                 AS failedTask,
        CAST(MAX(lastTimestamp) AS DATETIME) AS taskLastUpdatedTime
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.TentaclesTask`
      GROUP BY
       fileId
    )  AS taskSummary
    ON
      file.fileId = taskSummary.fileId
    LEFT OUTER JOIN (
      SELECT
        *
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.TentaclesTask`
    )  AS task
    ON
      file.fileId = task.fileId
    LEFT OUTER JOIN (
      SELECT
        *
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.RawFailedRecord` as record
    ) AS failedRecord
    ON
      task.taskId = failedRecord.taskId
    WHERE
      file.api IS NOT NULL
    ORDER BY
      updatedTime DESC
  '

  create_or_update_view TentaclesBlockage '
    WITH blockedApis AS (
      SELECT
        api,
        DATETIME_DIFF(CURRENT_TIMESTAMP(), MAX(lastTimestamp), MINUTE) AS minutes
      FROM
        `'${GCP_PROJECT}'.'${DATASET}'.TentaclesTask`
      WHERE
        api IN (
          SELECT
            DISTINCT api
          FROM
            `'${GCP_PROJECT}'.'${DATASET}'.TentaclesTask`
          WHERE
            taskStatus = "queuing"
        )
      GROUP BY
        api
      HAVING
        minutes>9 )
    SELECT
      *
    FROM
      `'${GCP_PROJECT}'.'${DATASET}'.TentaclesTask`
    WHERE
      taskStatus = "queuing" AND api IN (SELECT api FROM blockedApis)
  '
}

#######################################
# Check Firestore status and print next steps information after installation.
# Globals:
#   NEED_SERVICE_ACCOUNT
#   SA_KEY_FILE
# Arguments:
#   None
#######################################
post_installation() {
  (( STEP += 1 ))
  printf '%s\n' "Step ${STEP}: Post-installation actions..."
  if [[ ${NEED_AUTHENTICATION} == 'true' ]]; then
    local account="YOUR_OAUTH_EMAIL"
    if [[ ${NEED_SERVICE_ACCOUNT} == 'true' ]]; then
      account=$(get_service_account)
    fi
    cat <<EOF
Some enabled APIs require authentication. Extra steps are required to grant \
access to the service account's email or your OAuth email in external systems, \
for example, Google Analytics or Campaign Manager.
You need to grant access to the email before Tentacles can send out data to \
the target APIs.

  1. Google Analytics Data Import:
   * Set up dataset for Data Import, see: \
https://support.google.com/analytics/answer/3191417?hl=en
   * Grant the 'Edit' access to [${account}]
  2. Campaign Manager:
   * DCM/DFA Reporting and Trafficking API's Conversions service, see: \
https://developers.google.com/doubleclick-advertisers/guides/conversions_overview
   * Create User Profile for [${account}] and grant the access to 'Insert \
offline conversions'
  3. Google Ads API for Google Ads Click Conversions Upload:
   * Upload click conversions, see: \
https://developers.google.com/adwords/api/docs/guides/conversion-tracking#upload_click_conversions
   * Add [${account}] as a 'Standard access' user of Google Ads.
  4. Google Sheets API for Google Ads Conversions Upload based on Google Sheets:
   * Import conversions from ad clicks into Google Ads, see: \
https://support.google.com/google-ads/answer/7014069
   * Add [${account}] as an Editor of the Google Spreadsheet that Google Ads \
will take conversions from.
EOF
  fi
  cat <<EOF

Follow the document \
https://github.com/GoogleCloudPlatform/cloud-for-marketing/blob/master/marketing-analytics/activation/gmp-googleads-connector/README.md#4-api-details\
 to create a configuration of the integration.
Save the configuration to a JSON file, for example, './config_api.json', and \
then run the following command:
  ./deploy.sh update_api_config
This command updates the configuration of Firestore/Datastore before Tentacles \
can use them.
EOF
}

#######################################
# Upload API configuration in local JSON file to Firestore or Datastore.
# The uploading process adapts to Firestore or Datastore automatically.
# Globals:
#   None
# Arguments:
#   Optional string for the configuration file path and name.
#######################################
update_api_config(){
  check_authentication
  quit_if_failed $?
  check_firestore_existence
  local configFile
  configFile="${1}"
  while [[ ! -s "${configFile}" ]]; do
    local defaultConfigFile='./config_api.json'
    printf '%s' "Enter the configuration file [${defaultConfigFile}]: "
    read -r configFile
    configFile=${configFile:-"${defaultConfigFile}"}
    printf '\n'
  done
  printf '%s\n' "Updating API integration configurations in into Firestore..."
  node -e "require('./index.js').uploadApiConfig(require(process.argv[1]), \
'${PROJECT_NAMESPACE}')" "${configFile}"
}

#######################################
# Trigger Cloud Function 'Transport'
# Globals:
#   PROJECT_NAMESPACE
# Arguments:
#   API name, e.g. MP, CM, etc.
#######################################
trigger_transport(){
  printf '%s\n' "=========================="
  printf '%s\n' "Trigger the Cloud Function 'Transport'."
  check_authentication
  quit_if_failed $?

  local api
  if [[ -n "${1}" ]]; then
    api="${1}"
  else
    printf '%s' "Enter the API to trigger: "
    read -r api
    api="${api}"
    printf '\n'
  fi
  node -e "require('./index.js').triggerTransport('${api}', \
'${PROJECT_NAMESPACE}')"
}

#######################################
# Invoke the Cloud Function 'API requester' directly based on a local file.
# Note: The API configuration is still expected to be on Google Cloud. The API
# configuration needs to be updated to Firestore for any modification.
# Exampales:
# 1. Test based on a GCS file:
#    ./deploy.sh run_test_locally 'file_path_in_gcs' 'bucket_name'
# 2. Test based on a local file (only works for Pub/sub based APIs):
#    ./deploy.sh run_test_locally './loacl_file_path'
# Globals:
#   SA_KEY_FILE
# Arguments:
#   File to be sent out, a path.
#######################################
run_test_locally(){
  printf '%s\n' "=========================="
  cat <<EOF
Invoke the Cloud Function 'API requester' directly based on a local file.
Note: The API configuration is still expected to be on Google Cloud. The API
configuration needs to be updated to Firestore for any modification.
EOF
  check_authentication
  quit_if_failed $?
  check_firestore_existence
  local auth
  if [[ -f "$(pwd)/${OAUTH2_TOKEN_JSON}" ]]; then
    auth="OAUTH2_TOKEN_JSON=$(pwd)/${OAUTH2_TOKEN_JSON}"
  elif [[ -f "$(pwd)/${SA_KEY_FILE}" ]]; then
    auth="API_SERVICE_ACCOUNT=$(pwd)/${SA_KEY_FILE}"
  fi
  printf '%s\n' "  Setting environment variable of auth: ${auth}"
  env "${auth}" node -e "require('./index.js').localApiRequester(\
    '${PROJECT_NAMESPACE}', process.argv[1], process.argv[2])" "$@"
}

#######################################
# Start process by copying a file to target folder.
# Note: The API configuration is still expected to be on Google Cloud. The API
# configuration needs to be updated to Firestore for any modification.
# Globals:
#   CONFIG_FILE
#   OUTBOUND
# Arguments:
#   File to be sent out, a path.
#######################################
copy_file_to_start(){
  printf '%s\n' "=========================="
  cat <<EOF
Copy local file(s) to Cloud Storage to start process.
Note: The API configuration is still expected to be on Google Cloud. The API
configuration needs to be updated to Firestore for any modification.
EOF
  local source bucket folder target
  # gsutil support wildcard name. Use '*' to replace '[' here.
  source="$(printf '%s' "${1}" | sed -r 's/\[/\*/g' )"
  bucket=$(get_value_from_json_file "${CONFIG_FILE}" "GCS_BUCKET")
  folder=$(get_value_from_json_file "${CONFIG_FILE}" "OUTBOUND" | sed -r 's/\/$//')
  target="gs://${bucket}/${folder}"
  printf '%s\n' "  Source: ${1}"
  printf '%s\n' "  Target: ${target}"
  printf '%s' "Confirm? [Y/n]: "
  local input
  read -n1 -s input
  printf '%s\n' "${input}"
  if [[ -z ${input} || ${input} == 'y' || ${input} == 'Y' ]]; then
    copy_to_gcs "${source}" "${target}"
  else
    printf '%s\n' "User cancelled."
  fi
}

# For compatibility to the published doc.
copy_file_to_gcs(){
  copy_file_to_start "$@"
}

DEFAULT_INSTALL_TASKS=(
  "print_welcome Tentacles"
  load_config
  check_in_cloud_shell
  prepare_dependencies
  confirm_project
  confirm_namespace
  confirm_region
  confirm_integration_api
  confirm_auth_method
  confirm_visualization
  check_permissions
  enable_apis
  "confirm_located_bucket GCS_BUCKET BUCKET_LOC REGION"
  "confirm_folder OUTBOUND"
  confirm_firestore
  prepare_visualization
  save_config
  create_subscriptions
  do_authentication
  deploy_tentacles
  complete_visualization
  post_installation
  "print_finished Tentacles"
)

if [[ "${BASH_SOURCE[0]}" -ef "$0" ]]; then
  run_default_function "$@"
else
  printf '%s\n' "Tentacles Bash Library is loaded."
fi


text:
<?php

namespace Acme\BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Acme\BlogBundle\Entity\Author;

class DefaultController extends Controller
{
    public function indexAction()
    {
        $author = new Author();

        $author->setName("d");

        $author->gender = "d";

        $validator = $this->get("validator");
        $errors = $validator->validate($author,array("registration"));

        if (count($errors) > 0) {
            $errorsString = (string) $errors;
            return new Response($errorsString);
        }

        return new Response('The author is valid! Yes!');
    }

    public function updateAction(Request $request)
    {

    //

    }

}


text:
{% include "floybd/header.html" %}
{% load static %}

Cleaning Complete

Weather

                        Weather Concrete Day
                        Weather Predictions
                        Weather Predictions based on stats
                        Weather Stats
                        City Dashboard

Earthquakes

                        Earthquakes
                        Earthquakes Heat Map

GTFS

                    View GTFS
                    Upload New GTFS

{% include "floybd/footer.html" %}


text:
<!DOCTYPE html>

	<title>Layout's cell-based init</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
	<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
	<link rel="stylesheet" type="text/css" href="../../../codebase/fonts/font_roboto/roboto.css"/>
	<link rel="stylesheet" type="text/css" href="../../../codebase/dhtmlx.css"/>


text:
package com.giyeok.dexdio.augmentation.instsem;

import com.giyeok.dexdio.model0.insns.DexInstruction;

public abstract class InstSemStatement implements InstructionSemantic {

	private DexInstruction instruction;

	public InstSemStatement() {
		instruction = null;
	}

	public void setInstruction(DexInstruction instruction) {
		this.instruction = instruction;
	}

	public DexInstruction getInstruction() {
		return instruction;
	}

}


text:
{% extends "buildmanager/base.html" %}
{% load i18n %}
{% block title %}{% trans "Builds for Project: " %}{{ project }}{% endblock %}
{% block buildmanager_content %}

{% trans "Project" %} {{project.name}}

{{project.description}} (View on Build Server)

{% if latest_build %}
   Latest Released Build:
       view |
       download jar |
       download jad

{% endif %}

{% if build_map %}
    {% include "buildmanager/partials/project_builds.html" %}
{% endif %}
{% endblock %}


text:
<?php

namespace Symfony\Component\HttpClient\DataCollector;

use Symfony\Component\HttpClient\TraceableHttpClient;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;

/**
 * @author Jérémy Romey <jeremy@free-agent.fr>
 */
final class HttpClientDataCollector extends DataCollector implements LateDataCollectorInterface
{
    /**
     * @var TraceableHttpClient[]
     */
    private $clients = [];

    public function registerClient(string $name, TraceableHttpClient $client)
    {
        $this->clients[$name] = $client;
    }

    /**
     * {@inheritdoc}
     */
    public function collect(Request $request, Response $response, \Throwable $exception = null)
    {
        $this->reset();

        foreach ($this->clients as $name => $client) {
            [$errorCount, $traces] = $this->collectOnClient($client);

            $this->data['clients'][$name] = [
                'traces' => $traces,
                'error_count' => $errorCount,
            ];

            $this->data['request_count'] += \count($traces);
            $this->data['error_count'] += $errorCount;
        }
    }

    public function lateCollect()
    {
        foreach ($this->clients as $client) {
            $client->reset();
        }
    }

    public function getClients(): array
    {
        return $this->data['clients'] ?? [];
    }

    public function getRequestCount(): int
    {
        return $this->data['request_count'] ?? 0;
    }

    public function getErrorCount(): int
    {
        return $this->data['error_count'] ?? 0;
    }

    /**
     * {@inheritdoc}
     */
    public function getName(): string
    {
        return 'http_client';
    }

    public function reset()
    {
        $this->data = [
            'clients' => [],
            'request_count' => 0,
            'error_count' => 0,
        ];
    }

    private function collectOnClient(TraceableHttpClient $client): array
    {
        $traces = $client->getTracedRequests();
        $errorCount = 0;
        $baseInfo = [
            'response_headers' => 1,
            'redirect_count' => 1,
            'redirect_url' => 1,
            'user_data' => 1,
            'error' => 1,
            'url' => 1,
        ];

        foreach ($traces as $i => $trace) {
            if (400 <= ($trace['info']['http_code'] ?? 0)) {
                ++$errorCount;
            }

            $info = $trace['info'];
            $traces[$i]['http_code'] = $info['http_code'] ?? 0;

            unset($info['filetime'], $info['http_code'], $info['ssl_verify_result'], $info['content_type']);

            if ($trace['method'] === $info['http_method']) {
                unset($info['http_method']);
            }

            if ($trace['url'] === $info['url']) {
                unset($info['url']);
            }

            foreach ($info as $k => $v) {
                if (!$v || (is_numeric($v) && 0 > $v)) {
                    unset($info[$k]);
                }
            }

            $debugInfo = array_diff_key($info, $baseInfo);
            $info = array_diff_key($info, $debugInfo) + ['debug_info' => $debugInfo];
            unset($traces[$i]['info']); // break PHP reference used by TraceableHttpClient
            $traces[$i]['info'] = $this->cloneVar($info);
            $traces[$i]['options'] = $this->cloneVar($trace['options']);
        }

        return [$errorCount, $traces];
    }
}


text:
namespace sim {

class TileGraphic {

public:

    TileGraphic(const Tile* tile, BufferInterface* bufferInterface);

    bool wallDeclared(Direction direction) const;

    void setColor(const Color color);
    void declareWall(Direction direction, bool isWall);
    void undeclareWall(Direction direction);
    void setFogginess(bool foggy);
    void setText(const std::vector<std::string>& rowsOfText);

    void draw() const;
    void updateColor() const;
    void updateWalls() const;
    void updateFog() const;
    void updateText() const;

private:
    const Tile* m_tile;
    BufferInterface* m_bufferInterface;

    Color m_color;
    std::map<Direction, bool> m_declaredWalls;
    bool m_foggy;
    std::vector<std::string> m_rowsOfText;

    void updateWall(Direction direction) const;
    std::pair<Color, float> deduceWallColorAndAlpha(Direction direction) const;
};

} // namespace sim


text:
exports.mergeObjects = function(obj1, obj2){
	var res = obj1;

	for(var field in obj2){
		res[field] = obj2[field];
	}
	return res;
};

exports.cloneObject = function(obj1){
	var res = new Object();

	for(var field in obj1){
		res[field] = obj1[field];
	}
	return res;
};


text:
<!DOCTYPE HTML>

	<meta charset="UTF-8"/>
	 <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Immo chat design</title>

    <link href="css/bootstrap.min.css" rel="stylesheet" type="text/css" >
    <link href="immo.css" rel="stylesheet" type="styles/css" >
	<link rel="stylesheet" href="leaflet.css"/>

Immo Carte

  <button id ="btn" type="button" class="btn">Open chat</button>

      @
      <input type="text" class="form-control" placeholder="Username" aria-describedby="basic-addon1" id ="pseudo" style="width:40%">

  <button id="btn2" style="display : inline-block">Login</button>

    Chat immo  <button type="button" class="close">×</button>

        <form action="/" method="post" id="formulaire_chat">
          <input class="form-control" type="text" name="message" id="message" placeholder="Votre message..." autofocus/>
          <input type="submit" id="envoi_message" value="Envoyer" class="input-group-addon" />
          </form>


text:
module CandlesHelper
end


text:
<?php

/**
 * @namespace
 */

namespace Solarium\Core\Event;

use Symfony\Component\EventDispatcher\Event;
use Solarium\Core\Client\Request;
use Solarium\Core\Client\Response;
use Solarium\Core\Client\Endpoint;

/**
 * PreExecuteRequest event, see Events for details.
 */
class PreExecuteRequest extends Event
{
    /**
     * @var Request
     */
    protected $request;

    /**
     * @var Endpoint
     */
    protected $endpoint;

    /**
     * @var Response
     */
    protected $response;

    /**
     * Event constructor.
     *
     * @param Request  $request
     * @param Endpoint $endpoint
     */
    public function __construct(Request $request, Endpoint $endpoint)
    {
        $this->request = $request;
        $this->endpoint = $endpoint;
    }

    /**
     * Get the endpoint object for this event.
     *
     * @return Endpoint
     */
    public function getEndpoint()
    {
        return $this->endpoint;
    }

    /**
     * Get the request object for this event.
     *
     * @return Request
     */
    public function getRequest()
    {
        return $this->request;
    }

    /**
     * Get the request object for this event.
     *
     * @param Request $request
     */
    public function setRequest($request)
    {
        $this->request = $request;
    }

    /**
     * Get the response object for this event.
     *
     * @return Response
     */
    public function getResponse()
    {
        return $this->response;
    }

    /**
     * Set the response object for this event, overrides default execution.
     *
     * @param Response $response
     */
    public function setResponse($response)
    {
        $this->response = $response;
    }
}


text:
---
layout: post
title: "WeakHashMap导致的内存泄露"
description: "Java"
category: Android
tags: [Binder]

---

#### WeakHashMap原理分析

WeakHashMap与其他 Map 最主要的不同之处在于其 key 是弱引用类型,注意是key是弱引用类型,而不是Value,每次put与get都会更新,如果key被回收了,那么value会被主动清楚,但是如果value显示的引用了key,那就会导致key无法释放。

		/**
	  * The entries in this hash table extend WeakReference, using its main ref
	  * field as the key.
	  */
	 private static class Entry<K,V> extends WeakReference<Object> implements Map.Entry<K,V> {
	     V value;
	     int hash;
	     Entry<K,V> next;
	     /**
	      * Creates new entry.
	      */
	     Entry(Object key, V value,
	           ReferenceQueue<Object> queue,
	           int hash, Entry<K,V> next) {
	         //这里把key传给了父类WeakReference,说明key为弱引用(没有显式的 this.key = key)
	         //所有如果key只有通过弱引用访问时,key会被 GC 清理掉
	         //同时该key所代表的Entry会进入queue中,等待被处理
	         //还可以看到value为强引用(有显式的 this.value = value ),但这并不影响
	         //后面可以看到WeakHashMap.expungeStaleEntries方法是如何清理value的
	         super(key, queue);
	         this.value = value;
	         this.hash  = hash;
	         this.next  = next;
	     }
	     @SuppressWarnings("unchecked")
	     //在获取key时需要unmaskNull,因为对于null的key,是用WeakHashMap的内部成员属性来表示的
	     public K getKey() {
	         return (K) WeakHashMap.unmaskNull(get());
	     }
	     public V getValue() {
	         return value;
	     }
	     public V setValue(V newValue) {
	         V oldValue = value;
	         value = newValue;
	         return oldValue;
	     }
	     public boolean equals(Object o) {
	         if (!(o instanceof Map.Entry))
	             return false;
	         Map.Entry<?,?> e = (Map.Entry<?,?>)o;
	         K k1 = getKey();
	         Object k2 = e.getKey();
	         if (k1 == k2 || (k1 != null && k1.equals(k2))) {
	             V v1 = getValue();
	             Object v2 = e.getValue();
	             if (v1 == v2 || (v1 != null && v1.equals(v2)))
	                 return true;
	         }
	         return false;
	     }
	     public int hashCode() {
	         K k = getKey();
	         V v = getValue();
	         return ((k==null ? 0 : k.hashCode()) ^
	                 (v==null ? 0 : v.hashCode()));
	     }
	     public String toString() {
	         return getKey() + "=" + getValue();
	     }
	 }

	/**
	 * Reference queue for cleared WeakEntries
	 */
	// 所有Entry在构造时都传入该queue
	private final ReferenceQueue<Object> queue = new ReferenceQueue<>();
	/**
	 * Expunges stale entries from the table.
	 */

	private void expungeStaleEntries() {
	    for (Object x; (x = queue.poll()) != null; ) {
	        synchronized (queue) {
	            // e 为要清理的对象
	            @SuppressWarnings("unchecked")
	                Entry<K,V> e = (Entry<K,V>) x;
	            int i = indexFor(e.hash, table.length);
	            Entry<K,V> prev = table[i];
	            Entry<K,V> p = prev;
	            // while 循环遍历冲突链
	            while (p != null) {
	                Entry<K,V> next = p.next;
	                if (p == e) {
	                    if (prev == e)
	                        table[i] = next;
	                    else
	                        prev.next = next;

	                    // Must not null out e.next;
	                    // stale entries may be in use by a HashIterator
	                    // 可以看到这里把value赋值为null,来帮助 GC 回收强引用的value
	                    e.value = null; // Help GC
	                    size--;
	                    break;
	                }
	                prev = p;
	                p = next;
	            }
	        }
	    }
	}
参考文档

<http://blog.csdn.net/lyfi01/article/details/6415726>

<http://liujiacai.net/blog/2015/09/27/java-weakhashmap/>
<http://www.cnblogs.com/skywang12345/p/3311092.html>

####  在Value里面引用Key

> WeakHashMap Implementation note:

出现场景

出现原因:

The value objects in a WeakHashMap are held by ordinary strong references. Thus care should be taken to ensure that value objects do not strongly refer to their own keys, either directly or indirectly, since that will prevent the keys from being discarded.

Note that a value object may refer indirectly to its key via the WeakHashMap itself; that is, a value object may strongly refer to some other key object whose associated value object, in turn, strongly refers to the key of the first value object.

解决方案:

One way to deal with this is to wrap values themselves within WeakReferences before inserting, as in: m.put(key, new WeakReference(value)), and then unwrapping upon each get.


text:
#ifndef _IAS_DS_LMDB_System_H_
#define _IAS_DS_LMDB_System_H_

#include <commonlib/commonlib.h>

#include <ds/api.h>
#include <ds/Impl/System.h>
#include "log/LogLevel.h"

#include <org/invenireaude/qsystem/workers/ds/Parameter.h>
#include <lmdb.h>

namespace IAS {
namespace DS {
namespace Impl {
namespace LMDB {

class ContentManager;

class InputDriver;
class OutputDriver;

/*************************************************************************/
/** The LMDB class.
 *
 */
class System : public Impl::System {
public:

	virtual ~System() throw();

	virtual API::Connection* createConnection(
			const ::org::invenireaude::qsystem::workers::ds::Parameter* dmParameter);

  MDB_env *getEnv(){ return env; }

protected:

	System();

  MDB_env *env;

	friend class Factory<System>;
};
/*************************************************************************/

}
}
}
}

#endif /* _IAS_DS_LMDB_System_H_ */


text:
//
//  NSURL+Query.h
//  DZNCategories
//
//  Created by Ignacio Romero Zurbuchen on 4/19/13.
//  Copyright (c) 2013 DZN Labs. All rights reserved.
//  Licence: MIT-Licence
//  http://opensource.org/licenses/MIT
//

#import <Foundation/Foundation.h>

/*
 * Useful methods to ease NSArray queries.
 */
@interface NSURL (Query)

/*
 *
 */
- (BOOL)hasScheme;

/*
 *
 */
- (NSString *)noun;

/*
 *
 */
- (NSString *)verb;

/*
 *
 */
- (NSDictionary *)parametersString;

@end


text:
package io.cattle.platform.eventing.model;

import io.cattle.platform.eventing.exception.EventExecutionException;
import io.cattle.platform.eventing.model.Event;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.xml.bind.annotation.XmlTransient;

import org.slf4j.MDC;

public class EventVO<T> implements Event {

    String id, name, replyTo, resourceId, resourceType, publisher, transitioning, transitioningMessage, transitioningInternalMessage;
    String[] previousIds, previousNames;
    T data;
    Date time;
    Long timeoutMillis;
    String listenerKey;
    Integer transitioningProgress;
    Map<String, Object> context;

    @SuppressWarnings("unchecked")
    public EventVO() {
        id = UUID.randomUUID().toString();
        time = new Date();
        context = MDC.getMDCAdapter().getCopyOfContextMap();
    }

    public EventVO(Event event) {
        this(event, event.getReplyTo());
    }

    @SuppressWarnings("unchecked")
    public EventVO(Event event, String replyTo) {
        this.replyTo = replyTo;

        this.id = event.getId();
        this.name = event.getName();
        this.previousIds = event.getPreviousIds();
        this.previousNames = event.getPreviousNames();
        this.data = (T) event.getData();
        this.time = event.getTime();
        this.publisher = event.getPublisher();
        this.resourceId = event.getResourceId();
        this.resourceType = event.getResourceType();
        this.transitioning = event.getTransitioning();
        this.transitioningMessage = event.getTransitioningMessage();
        this.transitioningInternalMessage = event.getTransitioningInternalMessage();
        this.transitioningProgress = event.getTransitioningProgress();
        this.context = event.getContext();
        this.timeoutMillis = event.getTimeoutMillis();
    }

    public static EventVO<Object> replyWithException(Event request, Class<? extends EventExecutionException> clz,
                                                     String message) {
        EventVO reply = reply(request);
        reply.setTransitioning(TRANSITIONING_ERROR);
        reply.setTransitioningInternalMessage("class:" + clz.getCanonicalName());
        reply.setTransitioningMessage(message);

        return reply;
    }

    public static EventVO<Object> reply(Event request) {
        String[] previousIds = request.getPreviousIds();
        if (previousIds != null && previousIds.length > 0) {
            String[] newIds = new String[previousIds.length + 1];
            System.arraycopy(previousIds, 0, newIds, 1, previousIds.length);
            newIds[0] = request.getId();

            previousIds = newIds;
        } else {
            previousIds = new String[] { request.getId() };
        }

        EventVO<Object> event = new EventVO<Object>();
        event.setName(request.getReplyTo());
        event.setPreviousNames(prepend(request.getPreviousNames(), request.getName()));
        event.setPreviousIds(prepend(request.getPreviousIds(), request.getId()));
        event.setResourceId(request.getResourceId());
        event.setResourceType(request.getResourceType());

        return event;
    }

    protected static String[] prepend(String[] array, String value) {
        if (array != null && array.length > 0) {
            String[] newIds = new String[array.length + 1];
            System.arraycopy(array, 0, newIds, 1, array.length);
            newIds[0] = value;

            array = newIds;
        } else {
            array = new String[] { value };
        }

        return array;
    }

    public static <T> EventVO<T> newEvent(String name) {
        return new EventVO<T>(name);
    }

    public EventVO(String name) {
        this();
        setName(name);
    }

    @Override
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public EventVO<T> withName(String name) {
        this.name = name;
        return this;
    }

    @Override
    public String[] getPreviousIds() {
        return previousIds;
    }

    public void setPreviousIds(String[] previousIds) {
        this.previousIds = previousIds;
    }

    public EventVO<T> withPreviousIds(String[] previousIds) {
        this.previousIds = previousIds;
        return this;
    }

    @Override
    public String[] getPreviousNames() {
        return previousNames;
    }

    public void setPreviousNames(String[] names) {
        this.previousNames = names;
    }

    public EventVO<T> withPreviousNames(String[] names) {
        this.previousNames = names;
        return this;
    }

    @Override
    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public EventVO<T> withData(T data) {
        this.data = data;
        return this;
    }

    @Override
    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    public EventVO<T> withTime(Date time) {
        this.time = time;
        return this;
    }

    @Override
    public String getPublisher() {
        return publisher;
    }

    public void setPublisher(String publisher) {
        this.publisher = publisher;
    }

    public EventVO<T> withPublisher(String publisher) {
        this.publisher = publisher;
        return this;
    }

    @Override
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public EventVO<T> withId(String id) {
        this.id = id;
        return this;
    }

    @Override
    public String getReplyTo() {
        return replyTo;
    }

    public void setReplyTo(String replyTo) {
        this.replyTo = replyTo;
    }

    public EventVO<T> withReplyTo(String replyTo) {
        this.replyTo = replyTo;
        return this;
    }

    @Override
    public String getResourceId() {
        return resourceId;
    }

    public void setResourceId(String resourceId) {
        this.resourceId = resourceId;
    }

    public EventVO<T> withResourceId(String resourceId) {
        this.resourceId = resourceId;
        return this;
    }

    @Override
    public String getResourceType() {
        return resourceType;
    }

    public void setResourceType(String resourceType) {
        this.resourceType = resourceType;
    }

    public EventVO<T> withResourceType(String resourceType) {
        this.resourceType = resourceType;
        return this;
    }

    @XmlTransient
    public String getListenerKey() {
        return listenerKey;
    }

    public void setListenerKey(String listenerKey) {
        this.listenerKey = listenerKey;
    }

    public EventVO<T> withListenerKey(String listenerKey) {
        this.listenerKey = listenerKey;
        return this;
    }

    @Override
    public String getTransitioning() {
        return transitioning;
    }

    public void setTransitioning(String transitioning) {
        this.transitioning = transitioning;
    }

    public EventVO<T> withTransitioning(String transitioning) {
        this.transitioning = transitioning;
        return this;
    }

    @Override
    public String getTransitioningMessage() {
        return transitioningMessage;
    }

    public void setTransitioningMessage(String transitioningMessage) {
        this.transitioningMessage = transitioningMessage;
    }

    public EventVO<T> withTransitioningMessage(String transitioningMessage) {
        this.transitioningMessage = transitioningMessage;
        return this;
    }

    @Override
    public Integer getTransitioningProgress() {
        return transitioningProgress;
    }

    public void setTransitioningProgress(Integer transitioningProgress) {
        this.transitioningProgress = transitioningProgress;
    }

    public EventVO<T> withTransitioningProgress(Integer transitioningProgress) {
        this.transitioningProgress = transitioningProgress;
        return this;
    }

    @Override
    public String getTransitioningInternalMessage() {
        return transitioningInternalMessage;
    }

    public void setTransitioningInternalMessage(String transitioningInternalMessage) {
        this.transitioningInternalMessage = transitioningInternalMessage;
    }

    public EventVO<T> withTransitioningInternalMessage(String transitioningInternalMessage) {
        this.transitioningInternalMessage = transitioningInternalMessage;
        return this;
    }

    @Override
    public Map<String, Object> getContext() {
        return context;
    }

    public void setContext(Map<String, Object> context) {
        this.context = context;
    }

    @Override
    public Long getTimeoutMillis() {
        return timeoutMillis;
    }

    public void setTimeoutMillis(Long timeout) {
        this.timeoutMillis = timeout;
    }

    public EventVO<T> withTimeoutMillis(Long timeout) {
        this.timeoutMillis = timeout;
        return this;
    }

    public EventVO<T> withContext(Map<String, Object> context) {
        this.context = context;
        return this;
    }

    @Override
    public String toString() {
        return "EventVO [id=" + id + ", name=" + name + ", previousNames=" + Arrays.toString(previousNames) + ", replyTo=" + replyTo + ", resourceId="
                + resourceId + ", resourceType=" + resourceType + ", publisher=" + publisher + ", transitioning=" + transitioning + ", transitioningMessage="
                + transitioningMessage + ", transitioningInternalMessage=" + transitioningInternalMessage + ", previousIds=" + Arrays.toString(previousIds)
                + ", data=" + data + ", time=" + time + ", listenerKey=" + listenerKey + ", transitioningProgress=" + transitioningProgress + "]";
    }

}


text:
/* Problem 18. Grouped by GroupName

    Create a program that extracts all students grouped by GroupName and then prints them to the console.
    Use LINQ.
*/
namespace _18.GroupedByGroupName
{
    using System;
    using System.Linq;
    using _16.Groups;
    class GroupedByGroupName
    {
        static void Main()
        {
            Random rand = new Random();
            Student[] students = new Student[10];
            string[] firstNames = new string[] { "Niki", "Doncho", "Pesho", "Maria", "Stamat", "Ivan", "Ivo", "Gosho", "Evlogi", "Svetlin" };
            string[] lastNames = new string[] { "Kostov", "Minkov", "Peshev", "Ivanov", "Stamatov", "Popov", "Kenov", "Goshev", "Hristov", "Nakov" };
            string[] departmentNames = new string[] { "Web development", "Mobile development", "Quality", "Mathematics" };

            for (int i = 0; i < students.Length; i++)
            {
                string firstName = firstNames[rand.Next(0, 10)];
                string lastName = lastNames[rand.Next(0, 10)];
                Group gr = new Group(rand.Next(1, 7), departmentNames[rand.Next(0, 4)]);
                Student student = new Student(firstName, lastName, gr);
                students[i] = student;
            }

            var groupedStudents = students.GroupBy(st => st.Group.DepartmentName);

            foreach (var group in groupedStudents)
            {
                foreach (var st in group)
                {
                    Console.WriteLine("{0} {1} --> {2}", st.FirstName, st.LastName, st.Group.DepartmentName);
                }
                Console.WriteLine("-------------------------------------------");
            }
        }
    }
}