text:
package apiai

import (
	"encoding/json"
	"fmt"
	"net/http"
)

type Data struct {
	Text        string `json:"text"`
	Meta        string `json:"meta"`
	Alias       string `json:"alias"`
	UserDefined bool   `json:"userDefined"`
}

type UserSays struct {
	Id         string `json:"id"`
	Data       []Data `json:"data"`
	IsTemplate bool   `json:"isTemplate"`
	Count      int    `json:"count"`
}

type IntentParameter struct {
	Name         string   `json:"name"`
	Value        string   `json:"value"`
	DefaultValue string   `json:"defaultValue"`
	Required     bool     `json:"required"`
	DataType     string   `json:"dataType"`
	Prompts      []string `json:"prompts"`
	IsList       bool     `json:"isList"`
}

type IntentResponse struct {
	Action           string            `json:"action"`
	ResetContexts    bool              `json:"resetContexts"`
	AffectedContexts []Context         `json:"affectedContexts"`
	Params           []IntentParameter `json:"parameters"`
	Messages         []Message         `json:"messages"`
}

type CortanaCommand struct {
	NavigateOrService string `json:"navigateOrService"`
	Target            string `json:"target"`
}

type Intent struct {
	Id                    string           `json:"id"`
	Name                  string           `json:"name"`
	Auto                  bool             `json:"auto"`
	Contexts              []string         `json:"contexts"`
	Templates             []string         `json:"templates"`
	UserSays              []UserSays       `json:"userSays"`
	Responses             []IntentResponse `json:"responses"`
	Priority              int              `json:"priority"`
	WebhookUsed           bool             `json:"webhookUsed"`
	WebhookForSlotFilling bool             `json:"webhookForSlotFilling"`
	FallbackIntent        bool             `json:"fallbackIntent"`
	CortanaCommand        CortanaCommand   `json:"cortanaCommand"`
	Events                []Event          `json:"events"`
}

type IntentDescription struct {
	Id             string            `json:"id"`
	Name           string            `json:"name"`
	ContextIn      []string          `json:"contextIn"`
	ContextOut     []Context         `json:"contextOut"`
	Actions        []string          `json:"actions"`
	Params         []IntentParameter `json:"parameters"`
	Priority       int               `json:"priority"`
	FallbackIntent bool              `json:"fallbackIntent"`
}

func (c *ApiClient) GetIntents() ([]IntentDescription, error) {

	resp, err := c.getApiaiResponse(http.MethodGet, "intents", nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var intents []IntentDescription
	switch resp.StatusCode {
	case http.StatusOK:
		decoder := json.NewDecoder(resp.Body)
		err = decoder.Decode(&intents)
		if err != nil {
			return nil, err
		}
		return intents, nil
	default:
		return nil, fmt.Errorf(DefaultErrorMsg, resp.StatusCode)
	}
}

func (c *ApiClient) GetIntent(id string) (*Intent, error) {

	resp, err := c.getApiaiResponse(http.MethodGet, "intents/"+id, nil, nil)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var intent *Intent
	switch resp.StatusCode {
	case http.StatusOK:
		decoder := json.NewDecoder(resp.Body)
		err = decoder.Decode(&intent)
		if err != nil {
			return nil, err
		}
		return intent, nil
	default:
		return nil, fmt.Errorf(DefaultErrorMsg, resp.StatusCode)
	}
}

func (c *ApiClient) CreateIntent(intent Intent) (*CreationResponse, error) {

	resp, err := c.getApiaiResponse(http.MethodPost, "intents", nil, intent)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	var cr *CreationResponse
	switch resp.StatusCode {
	case http.StatusOK:
		decoder := json.NewDecoder(resp.Body)
		err = decoder.Decode(&cr)
		if err != nil {
			return nil, err
		}
		return cr, nil
	default:
		return nil, fmt.Errorf(DefaultErrorMsg, resp.StatusCode)
	}
}

func (c *ApiClient) UpdateIntent(id string, intent Intent) error {

	resp, err := c.getApiaiResponse(http.MethodPut, "intents/"+id, nil, intent)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	switch resp.StatusCode {
	case http.StatusOK:
		return nil
	default:
		return fmt.Errorf(DefaultErrorMsg, resp.StatusCode)
	}
}

func (c *ApiClient) DeleteIntent(id string) error {

	resp, err := c.getApiaiResponse(http.MethodDelete, "intents/"+id, nil, nil)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	switch resp.StatusCode {
	case http.StatusOK:
		return nil
	default:
		return fmt.Errorf(DefaultErrorMsg, resp.StatusCode)
	}
}


text:
package org.apache.tinkerpop.gremlin.structure.io.gryo;

import org.junit.Test;

import static org.junit.Assert.assertNotSame;

/**
 * @author Stephen Mallette (http://stephen.genoprime.com)
 */
public class GryoMapperTest {
    @Test
    public void shouldGetMostRecentVersion() {
        final GryoMapper.Builder b = GryoMapper.build();
        assertNotSame(b, GryoMapper.build());
    }
}

redpajama_set_name: RedPajamaGithub


text:
from django.conf import settings
from django.core.cache import cache
from django.dispatch import receiver
from django.test import TestCase, Client
from django.core.urlresolvers import reverse

import hashlib, hmac

from ..models import HelloSignRequest, HelloSignLog
from ..views import HelloSignWebhookEventHandler
from ..signals import hellosign_webhook_event_recieved

from .data import HELLOSIGN_200_RESPONSE, HELLOSIGN_WEBHOOK_EVENT_DATA
from .models import TestMonkeyModel

import json

@receiver(hellosign_webhook_event_recieved)
def on_hellosign_webhook_event_recieved(**kwargs):
    """
    Test signal listner to handle the signal fired event
    """
    cache.set('hellosign_webhook_event_recieved_keys', kwargs.keys())

def _generated_event_hash_to_match_test_api_key(HELLOSIGN_API_KEY, event_time, event_type):
    """
    In order to process webhooks an event_hash is passed from hellosign
    we need to simulate this event_hash based on our current settings
    """
    return hmac.new(HELLOSIGN_API_KEY, (event_time + event_type), hashlib.sha256).hexdigest()

class HelloSignWebhookEventHandlerTest(TestCase):
    """
    Test the handleing of HelloSign webhook events

    as per: http://www.hellosign.com/api/gettingStarted "Handling callbacks"

    NOTE: Your endpoint will need to return a 200 HTTP code and a response body
    containing the following text: "Hello API Event Received". Otherwise,
    the callback will be considered a failure and will be retried later.
    """
    subject = HelloSignWebhookEventHandler
    client = Client

    def setUp(self):
        super(HelloSignWebhookEventHandlerTest, self).setUp()
        self.monkey = TestMonkeyModel.objects.create(slug='test-monkey')
        self.monkey_content_object = self.monkey.get_content_type_object()

        self.signature_request_id = HELLOSIGN_WEBHOOK_EVENT_DATA['SIGNATURE_REQUEST_SENT']['signature_request'].get('signature_request_id')

        self.request = HelloSignRequest.objects.create(content_object_type=self.monkey_content_object,
                                                       object_id=self.monkey.pk,
                                                       signature_request_id=self.signature_request_id,
                                                       data=HELLOSIGN_200_RESPONSE)

    def get_webhook_event_post_data(self):
        """
        NB: HelloSign wraps the json data in another json object called "json"
        i.e. HelloSign sends a post object with key "json" that is set to an actual
        string of JSON
        """
        data = HELLOSIGN_WEBHOOK_EVENT_DATA['SIGNATURE_REQUEST_SENT']

        event_time = data['event']['event_time']
        event_type = data['event']['event_type']
        api_key = settings.HELLOSIGN_API_KEY

        # get a matching event hash
        data['event']['event_hash'] = _generated_event_hash_to_match_test_api_key(HELLOSIGN_API_KEY=api_key, event_time=event_time, event_type=event_type)

        return { 'json': json.dumps(data) }

    def get_hellosign_post_response(self):
        return self.client.post(reverse('hellosign_webhook_event'), self.get_webhook_event_post_data())

    def test_response_contains_required_text(self):
        # emulate a HelloSign Post
        resp = self.get_hellosign_post_response()

        self.assertEqual(resp.status_code, 200)
        self.assertEqual(resp.content, 'Hello API Event Received')

    def test_hellosign_webhook_event_recieved_fired(self):
        # emulate a HelloSign Post
        resp = self.get_hellosign_post_response()
        self.assertEqual(cache.get('hellosign_webhook_event_recieved_keys'), ["hellosign_log", "signal", "signature_request_id", "hellosign_request", "event_type", "data", "sender"])

    def test_response_creates_correct_objects(self):
        # emulate a HelloSign Post
        resp = self.get_hellosign_post_response()

        hsrequest_object = HelloSignRequest.objects.get(content_object_type=self.monkey_content_object,
                                     object_id=self.monkey.pk)
        hs_logs = hsrequest_object.hellosignlog_set.all()
        log = hs_logs[0]

        self.assertEqual(hs_logs.count(), 1)
        self.assertEqual(log.event_type, 'signature_request_sent')
        self.assertEqual(log.data, HELLOSIGN_WEBHOOK_EVENT_DATA['SIGNATURE_REQUEST_SENT'])


text:
namespace printing {

// Keys for a dictionary specifying a custom vendor capability. See
// settings/advanced_settings/advanced_settings_item.js in
// chrome/browser/resources/print_preview.
const char kOptionKey[] = "option";
const char kResetToDefaultKey[] = "reset_to_default";
const char kSelectCapKey[] = "select_cap";
const char kSelectString[] = "SELECT";
const char kTypeKey[] = "type";

// TODO(thestig): Consolidate duplicate constants.
const char kDpiCapabilityKey[] = "dpi";
const char kHorizontalDpi[] = "horizontal_dpi";
const char kVerticalDpi[] = "vertical_dpi";

// The dictionary key for the CDD item containing custom vendor capabilities.
const char kVendorCapabilityKey[] = "vendor_capability";

namespace {

base::Value::List PrintersToValues(const PrinterList& printer_list) {
  base::Value::List results;
  for (const PrinterBasicInfo& printer : printer_list) {
    base::Value::Dict printer_info;
    printer_info.Set(kSettingDeviceName, printer.printer_name);

    printer_info.Set(kSettingPrinterName, printer.display_name);
    printer_info.Set(kSettingPrinterDescription, printer.printer_description);

    base::Value::Dict options;
    for (const auto& opt_it : printer.options)
      options.SetByDottedPath(opt_it.first, opt_it.second);

#if BUILDFLAG(IS_CHROMEOS_ASH)
    printer_info.Set(
        kCUPSEnterprisePrinter,
        base::Contains(printer.options, kCUPSEnterprisePrinter) &&
            printer.options.at(kCUPSEnterprisePrinter) == kValueTrue);
#endif  // BUILDFLAG(IS_CHROMEOS_ASH)

    printer_info.Set(kSettingPrinterOptions, std::move(options));

    results.Append(std::move(printer_info));

    VLOG(1) << "Found printer " << printer.display_name << " with device name "
            << printer.printer_name;
  }
  return results;
}

template <typename Predicate>
base::Value::List GetFilteredList(const base::Value::List& list,
                                  Predicate pred) {
  auto out_list = list.Clone();
  out_list.EraseIf(pred);
  return out_list;
}

bool ValueIsNull(const base::Value& val) {
  return val.is_none();
}

bool DpiCapabilityInvalid(const base::Value& val) {
  if (!val.is_dict())
    return true;
  const auto& dict = val.GetDict();
  absl::optional<int> horizontal_dpi = dict.FindInt(kHorizontalDpi);
  if (horizontal_dpi.value_or(0) <= 0)
    return true;
  absl::optional<int> vertical_dpi = dict.FindInt(kVerticalDpi);
  if (vertical_dpi.value_or(0) <= 0)
    return true;
  return false;
}

bool VendorCapabilityInvalid(const base::Value& val) {
  if (!val.is_dict())
    return true;
  const auto& dict = val.GetDict();
  const std::string* option_type = dict.FindString(kTypeKey);
  if (!option_type)
    return true;
  if (*option_type != kSelectString)
    return false;
  const base::Value::Dict* select_cap = dict.FindDict(kSelectCapKey);
  if (!select_cap)
    return true;
  const base::Value::List* options_list = select_cap->FindList(kOptionKey);
  if (!options_list || options_list->empty() ||
      GetFilteredList(*options_list, ValueIsNull).empty()) {
    return true;
  }
  return false;
}

void SystemDialogDone(const base::Value& error) {
  // intentional no-op
}

}  // namespace

base::Value::Dict ValidateCddForPrintPreview(base::Value::Dict cdd) {
  base::Value::Dict* caps = cdd.FindDict(kPrinter);
  if (!caps)
    return cdd;

  base::Value::Dict out_caps;
  for (auto capability : *caps) {
    const auto& key = capability.first;
    base::Value& value = capability.second;
    base::Value::List* list_value = nullptr;
    if (value.is_list())
      list_value = &value.GetList();
    if (value.is_dict())
      list_value = value.GetDict().FindList(kOptionKey);

    if (!list_value) {
      out_caps.Set(key, std::move(value));
      continue;
    }

    bool is_vendor_capability = key == kVendorCapabilityKey;
    bool is_dpi_capability = key == kDpiCapabilityKey;
    if (is_vendor_capability) {
      list_value->EraseIf(VendorCapabilityInvalid);
    } else if (is_dpi_capability) {
      list_value->EraseIf(DpiCapabilityInvalid);
    } else {
      list_value->EraseIf(ValueIsNull);
    }
    if (list_value->empty())  // leave out empty lists.
      continue;

    if (is_vendor_capability) {
      // Need to also filter the individual capability lists.
      for (auto& vendor_option : *list_value) {
        if (!vendor_option.is_dict())
          continue;

        auto& vendor_dict = vendor_option.GetDict();
        const std::string* type = vendor_dict.FindString(kTypeKey);
        if (!type || *type != kSelectString)
          continue;

        auto* select_cap_dict = vendor_dict.FindDict(kSelectCapKey);
        if (select_cap_dict) {
          auto* option_list = select_cap_dict->FindList(kOptionKey);
          if (option_list)
            option_list->EraseIf(ValueIsNull);
        }
      }
    }
    if (value.is_dict()) {
      base::Value::Dict option_dict;
      option_dict.Set(kOptionKey, std::move(*list_value));
      absl::optional<bool> reset_to_default =
          value.GetDict().FindBool(kResetToDefaultKey);
      if (reset_to_default.has_value())
        option_dict.Set(kResetToDefaultKey, reset_to_default.value());
      out_caps.Set(key, std::move(option_dict));
    } else {
      out_caps.Set(key, std::move(*list_value));
    }
  }
  cdd.Set(kPrinter, std::move(out_caps));
  return cdd;
}

base::Value::Dict UpdateCddWithDpiIfMissing(base::Value::Dict cdd) {
  base::Value::Dict* printer = cdd.FindDict(kPrinter);
  if (!printer)
    return cdd;

  if (!printer->FindDict(kDpiCapabilityKey)) {
    base::Value::Dict default_dpi;
    default_dpi.Set(kHorizontalDpi, kDefaultPdfDpi);
    default_dpi.Set(kVerticalDpi, kDefaultPdfDpi);
    base::Value::List dpi_options;
    dpi_options.Append(std::move(default_dpi));
    base::Value::Dict dpi_capability;
    dpi_capability.Set(kOptionKey, std::move(dpi_options));
    printer->Set(kDpiCapabilityKey, std::move(dpi_capability));
  }
  return cdd;
}

void ConvertPrinterListForCallback(
    PrinterHandler::AddedPrintersCallback callback,
    PrinterHandler::GetPrintersDoneCallback done_callback,
    const PrinterList& printer_list) {
  base::Value::List printers = PrintersToValues(printer_list);

  VLOG(1) << "Enumerate printers finished, found " << printers.size()
          << " printers";
  if (!printers.empty())
    callback.Run(std::move(printers));
  std::move(done_callback).Run();
}

void StartLocalPrint(base::Value::Dict job_settings,
                     scoped_refptr<base::RefCountedMemory> print_data,
                     content::WebContents* preview_web_contents,
                     PrinterHandler::PrintCallback callback) {
  // Get print view manager.
  PrintPreviewDialogController* dialog_controller =
      PrintPreviewDialogController::GetInstance();
  content::WebContents* initiator =
      dialog_controller ? dialog_controller->GetInitiator(preview_web_contents)
                        : nullptr;
  PrintViewManager* print_view_manager =
      initiator ? PrintViewManager::FromWebContents(initiator) : nullptr;
  if (!print_view_manager) {
    std::move(callback).Run(base::Value("Initiator closed"));
    return;
  }

  if (job_settings.FindBool(kSettingShowSystemDialog).value_or(false) ||
      job_settings.FindBool(kSettingOpenPDFInPreview).value_or(false)) {
    // Run the callback early, or the modal dialogs will prevent the preview
    // from closing until they do.
    std::move(callback).Run(base::Value());
    callback = base::BindOnce(&SystemDialogDone);
  }
  print_view_manager->PrintForPrintPreview(
      std::move(job_settings), std::move(print_data),
      preview_web_contents->GetPrimaryMainFrame(), std::move(callback));
}

bool ParseSettings(const base::Value::Dict& settings,
                   std::string* out_destination_id,
                   std::string* out_capabilities,
                   gfx::Size* out_page_size,
                   base::Value* out_ticket) {
  const std::string* ticket_opt = settings.FindString(kSettingTicket);
  const std::string* capabilities_opt =
      settings.FindString(kSettingCapabilities);
  out_page_size->SetSize(settings.FindInt(kSettingPageWidth).value_or(0),
                         settings.FindInt(kSettingPageHeight).value_or(0));
  if (!ticket_opt || !capabilities_opt || out_page_size->IsEmpty()) {
    NOTREACHED();
    return false;
  }
  absl::optional<base::Value> ticket_value =
      base::JSONReader::Read(*ticket_opt);
  if (!ticket_value)
    return false;

  *out_destination_id = *settings.FindString(kSettingDeviceName);
  *out_capabilities = *capabilities_opt;
  *out_ticket = std::move(*ticket_value);
  return true;
}

}  // namespace printing


text:
#import <UIKit/UIKit.h>

@class DBAccount;

/** A generic block type used for observing changes throughout the Sync API */
typedef void (^DBObserver)();

/** An observer for the <linkedAccount> property */
typedef void (^DBAccountManagerObserver)(DBAccount *account);

/** The account manager is responsible for linking new users and persisting account information
 across runs of your app. You typically create an account manager at app startup with your
 app key and secret and keep it until your app terminates.
 */

@interface DBAccountManager : NSObject

/** @name Creating an account manager */

/** Create a new account manager with your app's app key and secret. You can register your app or
 find your key at the [apps](https://www.dropbox.com/developers/apps) page. */
- (id)initWithAppKey:(NSString *)key secret:(NSString *)secret;

/** A convenient place to store your app's account manager. */
+ (void)setSharedManager:(DBAccountManager *)sharedManager;

/** A convenient place to get your app's account manager. */
+ (DBAccountManager *)sharedManager;

/** @name Linking new accounts */

/** This method begins the process for linking new accounts.

    @param rootController the topmost view controller in your controller hierarchy.
 */
- (void)linkFromController:(UIViewController *)rootController;

/** You must call this method in your app delegate's
 `-application:openURL:sourceApplication:annotation:` method in order to complete the link process.

 @returns The [account](DBAccount) object if the link was successful, or `nil` if the user
 cancelled.
 */
- (DBAccount *)handleOpenURL:(NSURL *)url;

/** @name Getting the current state */

/** The currently linked account, or `nil` if there are no accounts currently linked.

 If your app needs to link multiple accounts at the same time, you should always use the
 <linkedAccounts> property. */
@property (nonatomic, readonly) DBAccount *linkedAccount;

/** All currently linked accounts, or `nil` if there are no accounts currently linked.

 The accounts are ordered from the least recently to the most recently linked. */
@property (nonatomic, readonly) NSArray *linkedAccounts;

/** @name Watching for changes */

/** Add `block` as an observer to get called whenever a new account is linked or an existing
 account is unlinked.

 @param observer this is only used as a handle to unregister blocks with the <removeObserver:> method.
 */
- (void)addObserver:(id)observer block:(DBAccountManagerObserver)block;

/** Use this method to remove all blocks associated with `observer`.

 @param observer the same value you provided to the <addObserver:block:> method.
 */
- (void)removeObserver:(id)observer;

@end


text:
package cli

import (
	"errors"
	"gopkg.in/alecthomas/kingpin.v2"
	"strings"
)

var (
	errGeneric = errors.New("Errors were encountered")
)

// CLIApp represents a cli application instance
type CLIApp struct {
	*kingpin.Application
	ClientName string
	AccountId  string
	Config     *config
	Client     *Client
}

// New initializes the brightbox cli application
func New() *CLIApp {
	a := new(CLIApp)
	a.Application = kingpin.New("brightbox", "Bleh")
	a.Flag("client", "client to authenticate with.").OverrideDefaultFromEnvar("CLIENT").StringVar(&a.ClientName)
	a.Flag("account", "id of account to limit queries to").OverrideDefaultFromEnvar("ACCOUNT").StringVar(&a.AccountId)

	configureServersCommand(a)
	configureConfigCommand(a)
	configureAccountsCommand(a)
	configureServerGroupsCommand(a)
	configureTokenCommand(a)
	configureImagesCommand(a)
	configureCloudIPsCommand(a)
	configureEventsCommand(a)
	configureLoginCommand(a)
	return a
}

func (c *CLIApp) Configure() error {
	cfg, err := newConfig()
	if err != nil {
		return err
	}
	c.Config = cfg

	clientName := c.ClientName
	if clientName == "" {
		clientName = cfg.defaultClientName
	}
	if clientName == "" {
		return nil
	}
	err = cfg.setClient(clientName)
	if err != nil {
		return err
	}
	err = cfg.CurrentClient().Setup(c.AccountId)
	if err != nil {
		return err
	}
	c.Client = cfg.CurrentClient()
	return nil
}

// Try to get an account id for the connection, either as specified in the
// config or by looking up the api client id
func (c *CLIApp) accountId() string {
	if c.AccountId != "" {
		return c.AccountId
	}
	if strings.HasPrefix(c.Client.ClientID, "cli-") {
		apiClient, err := c.Client.ApiClient(c.Client.ClientID)
		if err == nil {
			return apiClient.Account.Id
		}
	}
	return ""
}


text:
package com.how_old.app;

import java.io.ByteArrayOutputStream;

import org.apache.http.HttpRequest;
import org.json.JSONObject;

import com.facepp.error.FaceppParseException;
import com.facepp.http.HttpRequests;
import com.facepp.http.PostParameters;

import android.graphics.Bitmap;
import android.hardware.Camera.Parameters;
import android.util.Log;

public class FaceDetect {

	public interface CallBack{

		void success(JSONObject result);
		void error(FaceppParseException exception);
	}
	public static void detect(final Bitmap bitMap,final CallBack callBack){

		new Thread(new Runnable() {

			@Override
			public void run() {
				// TODO Auto-generated method stub
				try {
					HttpRequests httpRequests = new HttpRequests(Content.Key,Content.Secret,true,true);
					Bitmap bmSmall =  Bitmap.createBitmap(bitMap, 0, 0, bitMap.getWidth(), bitMap.getHeight());
					ByteArrayOutputStream stream = new ByteArrayOutputStream();
					bmSmall.compress(Bitmap.CompressFormat.JPEG, 100, stream);
					byte[] arrays = stream.toByteArray();

					PostParameters params = new PostParameters();
					params.setImg(arrays);
					JSONObject result = httpRequests.detectionDetect(params);

					Log.e("TAG", result.toString());

					if(callBack != null){
						callBack.success(result);

					}
				} catch (FaceppParseException e) {
					// TODO: handle exception
					if(callBack != null){
						callBack.error(e);
					}
				}

			}
		}).start();
	}
}


text:
title: "Network Buffer Tuning"
weight: 100
type: docs
aliases:
  - /deployment/memory/network_mem_tuning.html
---
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License.
-->

# Network memory tuning guide

## Overview

Each record in Flink is sent to the next subtask compounded with other records in a *network buffer*,
the smallest unit for communication between subtasks. In order to maintain consistent high throughput,
Flink uses *network buffer queues* (also known as *in-flight data*) on the input and output side of the transmission process.

Each subtask has an input queue waiting to consume data and an output queue
waiting to send data to the next subtask. Having a larger amount of in-flight data means that Flink can provide higher and more resilient throughput in the pipeline.  This will, however, cause longer checkpoint times.

Checkpoints in Flink can only finish once all the subtasks receive all of the injected checkpoint
barriers. In [aligned checkpoints]({{< ref "docs/concepts/stateful-stream-processing" >}}#checkpointing), those checkpoint barriers are traveling throughout the job graph along with
the network buffers. The larger the amount of in-flight data, the longer the checkpoint barrier propagation time. In [unaligned checkpoints]({{< ref "docs/concepts/stateful-stream-processing" >}}#unaligned-checkpointing), the larger the amount of in-flight data, the larger the checkpoint size will be because all of the captured in-flight data has to be persisted as part of the checkpoint.

## The Buffer Debloating Mechanism

Previously, the only way to configure the amount of in-flight data was to specify both the amount and the buffer size. However, ideal values can be difficult to choose since they are different for every
deployment. The buffer debloating mechanism added in Flink 1.14 attempts to address this issue by automatically adjusting the amount of in-flight data to reasonable values.

The buffer debloating feature calculates the maximum possible throughput for the subtask (in the scenario that it is always busy) and adjusts the amount of in-flight data such that the consumption time of those in-flight data will be equal to the configured value.

The buffer debloat mechanism can be enabled by setting the property `taskmanager.network.memory.buffer-debloat.enabled` to `true`.
The targeted time to consume the in-flight data can be configured by setting `taskmanager.network.memory.buffer-debloat.target` to `duration`.
The default value of the debloat target should be good enough for most cases.

This feature uses past throughput data to predict the time required to consume the remaining
in-flight data. If the predictions are incorrect, the debloating mechanism can fail in one of two ways:
* There will not be enough buffered data to provide full throughput.
* There will be too many buffered in-flight data which will negatively affect the aligned checkpoint barriers propagation time or the unaligned checkpoint size.

If you have a varying load in your Job (i.e. sudden spikes of incoming records, periodically
firing windowed aggregations or joins), you might need to adjust the following settings:

* `taskmanager.network.memory.buffer-debloat.period` - This is the minimum time period between buffer size recalculation. The shorter the period, the faster the reaction time of the debloating mechanism but the higher the CPU overhead for the necessary calculations.

* `taskmanager.network.memory.buffer-debloat.samples` - This adjusts the number of samples over which throughput measurements are averaged out. The frequency of the collected samples can be adjusted via `taskmanager.network.memory.buffer-debloat.period`. The fewer the samples, the faster the reaction time of the debloating mechanism, but a higher chance of a sudden spike or drop of the throughput which can cause the buffer debloating mechanism to miscalculate the optimal amount of in-flight data.

* `taskmanager.network.memory.buffer-debloat.threshold-percentages` - An optimization for preventing frequent buffer size changes (i.e. if the new size is not much different compared to the old size).

Consult the [configuration]({{< ref "docs/deployment/config" >}}#full-taskmanageroptions) documentation for more details and additional parameters.

Here are [metrics]({{< ref "docs/ops/metrics" >}}#io) you can use to monitor the current buffer size:
* `estimatedTimeToConsumeBuffersMs` - total time to consume data from all input channels
* `debloatedBufferSize` - current buffer size

### Limitations

Currently, there are a few cases that are not handled automatically by the buffer debloating mechanism.

#### Large records

If your record size exceeds the [minimum memory segment size]({{< ref "docs/deployment/config" >}}#taskmanager-memory-min-segment-size), buffer debloating can potentially shrink the buffer size so much, that the network stack will require more than one buffer to transfer a single record. This can have adverse effects on the throughput, without actually reducing the amount of in-flight data.

#### Multiple inputs and unions

Currently, the throughput calculation and buffer debloating happen on the subtask level.

If your subtask has multiple different inputs or it has a single but unioned input, buffer debloating can cause the input of the low throughput to have too much buffered in-flight data, while the input of the high throughput might have buffers that are too small to sustain that throughput. This might be particularly visible if the different inputs have vastly different throughputs. We recommend paying special attention to such subtasks when testing this feature.

#### Buffer size and number of buffers

Currently, buffer debloating only caps at the maximal used buffer size. The actual buffer size and the number of buffers remain unchanged. This means that the debloating mechanism cannot reduce the memory usage of your job. You would have to manually reduce either the amount or the size of the buffers.

Furthermore, if you want to reduce the amount of buffered in-flight data below what buffer debloating currently allows, you might want to manually configure the number of buffers.

## Network buffer lifecycle

Flink has several local buffer pools - one for the output stream and one for each input gate.
Each of those pools is limited to at most

`#channels * taskmanager.network.memory.buffers-per-channel + taskmanager.network.memory.floating-buffers-per-gate`

The size of the buffer can be configured by setting `taskmanager.memory.segment-size`.

### Input network buffers

Buffers in the input channel are divided into exclusive and floating buffers.  Exclusive buffers can be used by only one particular channel.  A channel can request additional floating buffers from a buffer pool shared across all channels belonging to the given input gate. The remaining floating buffers are optional and are acquired only if there are enough resources available.

In the initialization phase:
- Flink will try to acquire the configured amount of exclusive buffers for each channel
- all exclusive buffers must be fulfilled or the job will fail with an exception
- a single floating buffer has to be allocated for Flink to be able to make progress

### Output network buffers

Unlike the input buffer pool, the output buffer pool has only one type of buffer which it shares among all subpartitions.

In order to avoid excessive data skew, the number of buffers for each subpartition is limited by the `taskmanager.network.memory.max-buffers-per-channel` setting.

Like the input buffer pool, the configured amount of exclusive buffers and floating buffers is only treated as recommended values. If there are not enough buffers available, Flink can make progress with only a single exclusive buffer per output subpartition and zero floating buffers.

## The number of in-flight buffers

The default settings for exclusive buffers and floating buffers should be sufficient for the maximum throughput.  If the minimum of in-flight data needs to be set, the exclusive buffers can be set to `0` and the memory segment size can be decreased.

### Selecting the buffer size

The buffer collects records in order to optimize network overhead when sending the data portion to the next subtask. The next subtask should receive all parts of the record before consuming it.

If the buffer size is too small (i.e. less than one record), this can lead to low throughput since the overhead is still pretty large.

If the buffer size is too large, this can lead to:
- high memory usage
- huge checkpoint data (for unaligned checkpoints)
- long checkpoint time (for aligned checkpoints)
- inefficient use of allocated memory with a small `execution.buffer-timeout` because flushed buffers would only be sent partially filled

### Selecting the buffer count

The number of buffers is configured by the `taskmanager.network.memory.buffers-per-channel` and `taskmanager.network.memory.floating-buffers-per-gate` settings.

For best throughput, we recommend using the default values for the number of exclusive
and floating buffers. If the amount of in-flight data is causing issues, enabling
[buffer debloating]({{< ref "docs/deployment/memory/network_mem_tuning" >}}#the-buffer-debloating-mechanism) is recommended.

You can tune the number of network buffers manually, but consider the following:

1. You should adjust the number of buffers according to your expected throughput (in `bytes/second`).
Assigning credits and sending buffers takes some time (around two roundtrip messages between two nodes). The latency also depends on your network.

Using the buffer roundtrip time (around `1ms` in a healthy local network), [the buffer size]({{< ref "docs/deployment/config" >}}#taskmanager-memory-segment-size), and the expected throughput, you can calculate the number of buffers required to sustain the throughput by using this formula:
```
number_of_buffers = expected_throughput * buffer_roundtrip / buffer_size
```
For example, with an expected throughput of `320MB/s`, roundtrip latency of `1ms`, and the default memory segment size, 10 is the number of actively used buffers needed to achieve the expected throughput:
```
number_of_buffers = 320MB/s * 1ms / 32KB = 10
```
2. The purpose of floating buffers is to handle data skew scenarios. Ideally, the number of floating buffers (default: 8) and the exclusive buffers (default: 2) that belong to that channel should be able to saturate the network throughput. But this is not always feasible or necessary. It is very rare that only a single channel among all the subtasks in the task manager is being used.

3. The purpose of exclusive buffers is to provide a fluent throughput. While one buffer is in transit, the other is being filled up. With high throughput setups, the number of exclusive buffers is the main factor that defines the amount of in-flight data Flink uses.

In the case of backpressure in low throughput setups, you should consider reducing the number of [exclusive buffers]({{< ref "docs/deployment/config" >}}#taskmanager-network-memory-buffers-per-channel).

## Summary

Memory configuration tuning for the network in Flink can be simplified by enabling the buffer debloating mechanism. You may have to tune it.

If this does not work, you can disable the buffer debloating mechanism and manually configure the memory segment size and the number of buffers. For this second scenario, we recommend:
- using the default values for max throughput
- reducing the memory segment size and/or number of exclusive buffers to speed up checkpointing and reduce the memory consumption of the network stack

{{< top >}}


text:
package smartag

//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
//
// Code generated by Alibaba Cloud SDK Code Generator.
// Changes may cause incorrect behavior and will be lost if the code is regenerated.

import (
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
)

// ListAccessPointNetworkQualities invokes the smartag.ListAccessPointNetworkQualities API synchronously
func (client *Client) ListAccessPointNetworkQualities(request *ListAccessPointNetworkQualitiesRequest) (response *ListAccessPointNetworkQualitiesResponse, err error) {
	response = CreateListAccessPointNetworkQualitiesResponse()
	err = client.DoAction(request, response)
	return
}

// ListAccessPointNetworkQualitiesWithChan invokes the smartag.ListAccessPointNetworkQualities API asynchronously
func (client *Client) ListAccessPointNetworkQualitiesWithChan(request *ListAccessPointNetworkQualitiesRequest) (<-chan *ListAccessPointNetworkQualitiesResponse, <-chan error) {
	responseChan := make(chan *ListAccessPointNetworkQualitiesResponse, 1)
	errChan := make(chan error, 1)
	err := client.AddAsyncTask(func() {
		defer close(responseChan)
		defer close(errChan)
		response, err := client.ListAccessPointNetworkQualities(request)
		if err != nil {
			errChan <- err
		} else {
			responseChan <- response
		}
	})
	if err != nil {
		errChan <- err
		close(responseChan)
		close(errChan)
	}
	return responseChan, errChan
}

// ListAccessPointNetworkQualitiesWithCallback invokes the smartag.ListAccessPointNetworkQualities API asynchronously
func (client *Client) ListAccessPointNetworkQualitiesWithCallback(request *ListAccessPointNetworkQualitiesRequest, callback func(response *ListAccessPointNetworkQualitiesResponse, err error)) <-chan int {
	result := make(chan int, 1)
	err := client.AddAsyncTask(func() {
		var response *ListAccessPointNetworkQualitiesResponse
		var err error
		defer close(result)
		response, err = client.ListAccessPointNetworkQualities(request)
		callback(response, err)
		result <- 1
	})
	if err != nil {
		defer close(result)
		callback(nil, err)
		result <- 0
	}
	return result
}

// ListAccessPointNetworkQualitiesRequest is the request struct for api ListAccessPointNetworkQualities
type ListAccessPointNetworkQualitiesRequest struct {
	*requests.RpcRequest
	ResourceOwnerId      requests.Integer `position:"Query" name:"ResourceOwnerId"`
	ResourceOwnerAccount string           `position:"Query" name:"ResourceOwnerAccount"`
	OwnerAccount         string           `position:"Query" name:"OwnerAccount"`
	OwnerId              requests.Integer `position:"Query" name:"OwnerId"`
	SmartAGId            string           `position:"Query" name:"SmartAGId"`
}

// ListAccessPointNetworkQualitiesResponse is the response struct for api ListAccessPointNetworkQualities
type ListAccessPointNetworkQualitiesResponse struct {
	*responses.BaseResponse
	RequestId                   string                      `json:"RequestId" xml:"RequestId"`
	AccessPointNetworkQualities []AccessPointNetworkQuality `json:"AccessPointNetworkQualities" xml:"AccessPointNetworkQualities"`
}

// CreateListAccessPointNetworkQualitiesRequest creates a request to invoke ListAccessPointNetworkQualities API
func CreateListAccessPointNetworkQualitiesRequest() (request *ListAccessPointNetworkQualitiesRequest) {
	request = &ListAccessPointNetworkQualitiesRequest{
		RpcRequest: &requests.RpcRequest{},
	}
	request.InitWithApiInfo("Smartag", "2018-03-13", "ListAccessPointNetworkQualities", "smartag", "openAPI")
	request.Method = requests.POST
	return
}

// CreateListAccessPointNetworkQualitiesResponse creates a response to parse from ListAccessPointNetworkQualities response
func CreateListAccessPointNetworkQualitiesResponse() (response *ListAccessPointNetworkQualitiesResponse) {
	response = &ListAccessPointNetworkQualitiesResponse{
		BaseResponse: &responses.BaseResponse{},
	}
	return
}


text:
<?php
namespace Phine\Framework\Progress;
use Phine\Framework\Progress\Interfaces\IReporter;
abstract class Progressor
{

    private $reporters = array();
    function AddReporter(IReporter $reporter)
    {
        $this->reporters[] = $reporter;
    }
    function ClearReporters()
    {
        $this->reporters = array();
    }
    function NotifyReporters($progress, $progressCount)
    {
        foreach ($this->reporters as $reporter)
        {
            $reporter->Report($progress, $progressCount);
        }
    }
}


text:
"""
Train shadow net script
"""
import os
import tensorflow as tf
import os.path as ops
import time
import numpy as np
import argparse
import sys
sys.path.append('/data/code')

from crnnmodel import crnn_model
from local_utils import data_utils, log_utils
from global_configuration import config
from uaitrain.arch.tensorflow import uflag

tf.app.flags.DEFINE_string('dataset_dir','/data/data','data path')
tf.app.flags.DEFINE_string('weights_path',None,'weight path')
FLAGS = tf.app.flags.FLAGS

logger = log_utils.init_logger()

'''def init_args():
    """
    :return:
    """
    parser = argparse.ArgumentParser()
    parser.add_argument('--dataset_dir', type=str, help='Where you store the dataset')
    parser.add_argument('--weights_path', type=str, help='Where you store the pretrained weights')

    return parser.parse_args()
'''
def train_shadownet():
    """

    :param dataset_dir:
    :param weights_path:
    :return:
    """
    # decode the tf records to get the training data
    decoder = data_utils.TextFeatureIO().reader
    images, labels, imagenames = decoder.read_features(ops.join(FLAGS.dataset_dir, 'train_feature.tfrecords'),
                                                       num_epochs=None)
    inputdata, input_labels, input_imagenames = tf.train.shuffle_batch(
        tensors=[images, labels, imagenames], batch_size=32, capacity=1000+2*32, min_after_dequeue=100, num_threads=1)

    inputdata = tf.cast(x=inputdata, dtype=tf.float32)

    # initializa the net model
    shadownet = crnn_model.ShadowNet(phase='Train', hidden_nums=256, layers_nums=2, seq_length=25, num_classes=37)
    #print('initializa the net model')
    with tf.variable_scope('shadow', reuse=False):
        net_out = shadownet.build_shadownet(inputdata=inputdata)
    #print('data input finish')
    cost = tf.reduce_mean(tf.nn.ctc_loss(labels=input_labels, inputs=net_out, sequence_length=25*np.ones(32)))

    decoded, log_prob = tf.nn.ctc_beam_search_decoder(net_out, 25*np.ones(32), merge_repeated=False)

    sequence_dist = tf.reduce_mean(tf.edit_distance(tf.cast(decoded[0], tf.int32), input_labels))

    global_step = tf.Variable(0, name='global_step', trainable=False)

    starter_learning_rate = config.cfg.TRAIN.LEARNING_RATE
    learning_rate = tf.train.exponential_decay(starter_learning_rate, global_step,
                                               config.cfg.TRAIN.LR_DECAY_STEPS, config.cfg.TRAIN.LR_DECAY_RATE,
                                               staircase=True)
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)

    with tf.control_dependencies(update_ops):
        optimizer = tf.train.AdadeltaOptimizer(learning_rate=learning_rate).minimize(loss=cost, global_step=global_step)

    # Set tf summary
    tboard_save_path = FLAGS.output_dir+'/log'
    if not ops.exists(tboard_save_path):
        os.makedirs(tboard_save_path)
    tf.summary.scalar(name='Cost', tensor=cost)
    tf.summary.scalar(name='Learning_Rate', tensor=learning_rate)
    tf.summary.scalar(name='Seq_Dist', tensor=sequence_dist)
    merge_summary_op = tf.summary.merge_all()

    # Set saver configuration
    saver = tf.train.Saver()
    model_save_dir = FLAGS.output_dir
    if not ops.exists(model_save_dir):
        os.makedirs(model_save_dir)
        print('makedir savepath')
    train_start_time = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
    model_name = 'shadownet_{:s}.ckpt'.format(str(train_start_time))
    model_save_path = ops.join(model_save_dir, model_name)

    # Set sess configuration
    sess_config = tf.ConfigProto()
    sess_config.allow_soft_placement = True ###################
    sess_config.gpu_options.per_process_gpu_memory_fraction = config.cfg.TRAIN.GPU_MEMORY_FRACTION
    sess_config.gpu_options.allow_growth = config.cfg.TRAIN.TF_ALLOW_GROWTH

    sess = tf.Session(config=sess_config)

    summary_writer = tf.summary.FileWriter(tboard_save_path)
    summary_writer.add_graph(sess.graph)

    # Set the training parameters
    train_epochs = config.cfg.TRAIN.EPOCHS
    weights_path=FLAGS.weights_path
    with sess.as_default():
        if weights_path is None:
            logger.info('Training from scratch')
            init = tf.global_variables_initializer()
            sess.run(init)
        else:
            logger.info('Restore model from {:s}'.format(weights_path))
            saver.restore(sess=sess, save_path=weights_path)

        coord = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coord)

        for epoch in range(train_epochs):
            logger.info('epoch is {:d}'.format(epoch))
            _, c, seq_distance, preds, gt_labels, summary = sess.run(
                [optimizer, cost, sequence_dist, decoded, input_labels, merge_summary_op])

            # calculate the precision
            logger.info('calculate the precision')
            preds = decoder.sparse_tensor_to_str(preds[0])
            gt_labels = decoder.sparse_tensor_to_str(gt_labels)
            accuracy = []

            for index, gt_label in enumerate(gt_labels):
                pred = preds[index]
                totol_count = len(gt_label)
                correct_count = 0
                try:
                    for i, tmp in enumerate(gt_label):
                        if tmp == pred[i]:
                            correct_count += 1
                except IndexError:
                    continue
                finally:
                    try:
                        accuracy.append(correct_count / totol_count)
                    except ZeroDivisionError:
                        if len(pred) == 0:
                            accuracy.append(1)
                        else:
                            accuracy.append(0)
            accuracy = np.mean(np.array(accuracy).astype(np.float32), axis=0)
            #
            if epoch % config.cfg.TRAIN.DISPLAY_STEP == 0:
                logger.info('Epoch: {:d} cost= {:9f} seq distance= {:9f} train accuracy= {:9f}'.format(
                    epoch + 1, c, seq_distance, accuracy))

            summary_writer.add_summary(summary=summary, global_step=epoch)
            saver.save(sess=sess, save_path=model_save_path, global_step=epoch)
            logger.info('model saved')
        coord.request_stop()
        coord.join(threads=threads)

    sess.close()

    return

def main(_):
    train_shadownet()

if __name__ == '__main__':
    # init args
    #args = init_args()

    #if not ops.exists(args.dataset_dir):
    #    raise ValueError('{:s} doesn\'t exist'.format(args.dataset_dir))

    #train_shadownet(args.dataset_dir, args.weights_path)
    tf.app.run()
    print('Done')


text:
/* server.cpp */

#include "../../testsuite/test.hpp"

void ServerError(SSL_CTX* ctx, SSL* ssl, SOCKET_T& sockfd, const char* msg)
{
    SSL_CTX_free(ctx);
    SSL_free(ssl);
    tcp_close(sockfd);
    err_sys(msg);
}

#ifdef NON_BLOCKING
    void NonBlockingSSL_Accept(SSL* ssl, SSL_CTX* ctx, SOCKET_T& clientfd)
    {
        int ret = SSL_accept(ssl);
        int err = SSL_get_error(ssl, 0);
        while (ret != SSL_SUCCESS && (err == SSL_ERROR_WANT_READ ||
                                      err == SSL_ERROR_WANT_WRITE)) {
            if (err == SSL_ERROR_WANT_READ)
                printf("... server would read block\n");
            else
                printf("... server would write block\n");
            #ifdef _WIN32
                Sleep(1000);
            #else
                sleep(1);
            #endif
            ret = SSL_accept(ssl);
            err = SSL_get_error(ssl, 0);
        }
        if (ret != SSL_SUCCESS)
            ServerError(ctx, ssl, clientfd, "SSL_accept failed");
    }
#endif

THREAD_RETURN YASSL_API server_test(void* args)
{
#ifdef _WIN32
    WSADATA wsd;
    WSAStartup(0x0002, &wsd);
#endif

    SOCKET_T sockfd   = 0;
    SOCKET_T clientfd = 0;
    int      argc     = 0;
    char**   argv     = 0;

    set_args(argc, argv, *static_cast<func_args*>(args));
    tcp_accept(sockfd, clientfd, *static_cast<func_args*>(args));

    tcp_close(sockfd);

    SSL_METHOD* method = TLSv1_server_method();
    SSL_CTX*    ctx = SSL_CTX_new(method);

    //SSL_CTX_set_cipher_list(ctx, "RC4-SHA:RC4-MD5");
    SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 0);
    set_serverCerts(ctx);
    DH* dh = set_tmpDH(ctx);

    SSL* ssl = SSL_new(ctx);
    SSL_set_fd(ssl, clientfd);

#ifdef NON_BLOCKING
    NonBlockingSSL_Accept(ssl, ctx, clientfd);
#else
    if (SSL_accept(ssl) != SSL_SUCCESS)
        ServerError(ctx, ssl, clientfd, "SSL_accept failed");
#endif

    showPeer(ssl);
    printf("Using Cipher Suite: %s\n", SSL_get_cipher(ssl));

    char command[1024];
    int input = SSL_read(ssl, command, sizeof(command));
    if (input > 0) {
        command[input] = 0;
        printf("First client command: %s\n", command);
    }

    char msg[] = "I hear you, fa shizzle!";
    if (SSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
        ServerError(ctx, ssl, clientfd, "SSL_write failed");

    DH_free(dh);
    SSL_CTX_free(ctx);
    SSL_shutdown(ssl);
    SSL_free(ssl);

    tcp_close(clientfd);

    ((func_args*)args)->return_code = 0;
    return 0;
}

#ifndef NO_MAIN_DRIVER

    int main(int argc, char** argv)
    {
        func_args args;

        args.argc = argc;
        args.argv = argv;

        server_test(&args);
        yaSSL_CleanUp();

        return args.return_code;
    }

#endif // NO_MAIN_DRIVER


text:
'use strict';

describe('Posts E2E Tests:', function () {
  describe('Test Posts page', function () {
    it('Should report missing credentials', function () {
      browser.get('http://localhost:3001/posts');
      expect(element.all(by.repeater('post in posts')).count()).toEqual(0);
    });
  });
});


text:
package com.mall.portal.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.dto.MallResult;
import com.mall.common.utils.ExceptionUtil;
import com.mall.portal.service.StaticPageService;

@Controller
public class StaticPageController {

	private final Logger logger = LoggerFactory.getLogger(StaticPageController.class);

	@Autowired
	private StaticPageService staticPageService;

	@RequestMapping("/gen/item/{itemId}")
	@ResponseBody
	public MallResult genItemPage(@PathVariable Long itemId) {
		try {
			return staticPageService.genHtml(itemId);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			return MallResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}


text:
namespace GameObjects.Models.Pets
{
    using Abstract;
    using Contracts;

    public class Turtle : Pet, IPet
    {
        private const int KnowledgeBoost = 15;

        public Turtle(string name) : base(name)
        {
        }

        public override string HelpMe(IKnowledge knowledge)
        {
            knowledge.AddKnowledge(KnowledgeBoost);
            return $"{this.ToString()} gives the student it's wisdom!!! The student receives {Turtle.KnowledgeBoost} knowledge!";
        }
    }
}


text:
using System.Collections.Generic;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace Citrina
{
    public class NewsfeedItemNoteNotes
    {
        /// <summary>
        /// Notes number.
        /// </summary>
        public int? Count { get; set; }

        public IEnumerable<NewsfeedNewsfeedNote> Items { get; set; }
    }
}


text:
namespace ombt {

// discrete uniform distribution
class DiscreteUniform: public BaseObject
{
public:
    // ctors and dtor
    DiscreteUniform();
    DiscreteUniform(double start, double end, const Random &rng);
    DiscreteUniform(const DiscreteUniform &src);
    ~DiscreteUniform();

    // assignment
    DiscreteUniform &operator=(const DiscreteUniform &rhs);

    // get a random value
    double nextValue();

protected:
    // data
    double start_;
    double end_;
    Random rng_;
};

}

#endif


text:
using MacroBot.Protocol;
using System.Collections.Generic;
using System.Linq;
using static MacroBot.Helpers;

namespace MacroBot
{
    abstract class SolarMovementStrategy
    {
        protected readonly Fleet fleet;

        protected SolarMovementStrategy(Fleet fleet)
        {
            this.fleet = fleet;
        }

        public abstract void GetMovementsAndConquers(
            GameState gameState,
            Player mePlayer,
            out List<GameResponseMoveToPlanet> movements,
            out List<GameResponseConquer> conquers);
    }
}


text:
package controllers;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

import java.util.Map;

import models.Person;
import ninja.NinjaTest;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.google.common.collect.Maps;

public class PersonControllerTest extends NinjaTest {

    @Test
    public void testPostPersonJson() throws Exception {
        Person person = new Person();
        person.name = "zeeess name - and some utf8 => öäü";

        String response = ninjaTestBrowser.postJson(getServerAddress()
                + "api/person.json", person);

        Person result = new ObjectMapper().readValue(response, Person.class);
        assertEquals(person.name, result.name);
    }

    @Test
    public void testPostPersonXml() throws Exception {
        Person person = new Person();
        person.name = "zeeess name - and some utf8 => öäü";

        String response = ninjaTestBrowser
                .postXml(getServerAddress() + "api/person.xml", person);

        System.out.println("j: " + response);

        Person result = new XmlMapper().readValue(response, Person.class);
        assertEquals(person.name, result.name);
    }

    @Test
    public void testThatContentNegotiationWithoutFallbackWorks() throws Exception {

        Map<String, String> headers = Maps.newHashMap();

        headers.put("Accept", "text/html");
        String response = ninjaTestBrowser
                .makeRequest(getServerAddress() + "api/person", headers);
        assertThat(response, equalTo("Person is: zeeess name - and some utf8 => öäü"));

        headers.put("Accept", "application/json");
        response = ninjaTestBrowser
                .makeRequest(getServerAddress() + "api/person", headers);
        assertThat(response, equalTo("{\"name\":\"zeeess name - and some utf8 => öäü\"}"));

        headers.put("Accept", "application/xml");
        response = ninjaTestBrowser
            .makeRequest(getServerAddress() + "api/person", headers);
        assertThat(response, equalTo("<Person xmlns=\"\"><name>zeeess name - and some utf8 => öäü</name></Person>"));

        //not supported => expecting error result:
        headers.put("Accept", "text/plain");
        response = ninjaTestBrowser
                .makeRequest(getServerAddress() + "api/person", headers);
        assertThat(response.contains("Oops. That's a bad request and all we know."), equalTo(true));

    }

    @Test
    public void testThatContentNegotiationWithFallbackWorks() throws Exception {

        Map<String, String> headers = Maps.newHashMap();

        headers.put("Accept", "text/html");
        String response = ninjaTestBrowser
                .makeRequest(getServerAddress() + "api/person_with_content_negotiation_fallback", headers);
        assertThat(response, equalTo("Person is: zeeess name - and some utf8 => öäü"));

        headers.put("Accept", "application/xml");
        response = ninjaTestBrowser
                .makeRequest(getServerAddress() + "api/person_with_content_negotiation_fallback", headers);
        assertThat(response, equalTo("Person is: zeeess name - and some utf8 => öäü"));

        headers.put("Accept", "application/unknown_content_type");
        response = ninjaTestBrowser
                .makeRequest(getServerAddress() + "api/person_with_content_negotiation_fallback", headers);
        assertThat(response, equalTo("Person is: zeeess name - and some utf8 => öäü"));

    }
}


text:
import * as v2beta from './v2beta';

const WorkflowsServiceV2BetaClient = v2beta.WorkflowsServiceV2BetaClient;
type WorkflowsServiceV2BetaClient = v2beta.WorkflowsServiceV2BetaClient;

export {v2beta, WorkflowsServiceV2BetaClient};
export default {v2beta, WorkflowsServiceV2BetaClient};
import * as protos from '../protos/protos';
export {protos};


text:
namespace MVVMLight.Messages.Messages
{
    public class StartAnimationMessage
    {
    }
}


text:
package com.simplifynowsoftware.flickrdemo.display;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.simplifynowsoftware.flickrdemo.FlickrImageUrl;
import com.simplifynowsoftware.flickrdemo.R;
import com.simplifynowsoftware.flickrdemo.retrofit.response.photosets.Photoset;
import com.simplifynowsoftware.flickrdemo.retrofit.response.photosets.Photosets;
import com.squareup.picasso.Picasso;

import java.util.List;

import static android.widget.ImageView.ScaleType.CENTER_CROP;

public class PhotosetAdapter extends BaseAdapter {
    protected final Context context;

    protected final List<Photoset> mPhotosets;

    public PhotosetAdapter(Context context, final Photosets photosets) {
        this.context = context;

        mPhotosets = photosets.getPhotosets().getPhotoset();
    }

    @Override public View getView(int position, View convertView, ViewGroup parent) {
        Photoset photoset = mPhotosets.get(position);

        final String url = FlickrImageUrl.getUrl(photoset);

        SquaredImageView view = (SquaredImageView) convertView;
        if (view == null) {
            view = new SquaredImageView(context);
            view.setScaleType(CENTER_CROP);
        }

        // Trigger the download of the URL asynchronously into the image view.
        Picasso.with(context)
                .load(url)
                .placeholder(R.drawable.ic_action_picture)
                .error(R.drawable.ic_action_warning)
                .fit()
                .tag(context)
                .into(view);

        return view;
    }

    @Override public int getCount() {
        return mPhotosets.size();
    }

    @Override public Photoset getItem(int position) {
     return mPhotosets.get(position);
    }

    @Override public long getItemId(int position) {
        return position;
    }
}


text:
import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
from google.protobuf import descriptor_pb2
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()

DESCRIPTOR = _descriptor.FileDescriptor(
  name='lopq_model.proto',
  package='com.flickr.vision.lopq',
  serialized_pb=_b('\n\x10lopq_model.proto\x12\x16\x63om.flickr.vision.lopq\"\x1c\n\x06Vector\x12\x12\n\x06values\x18\x01 \x03(\x02\x42\x02\x10\x01\"+\n\x06Matrix\x12\x12\n\x06values\x18\x01 \x03(\x02\x42\x02\x10\x01\x12\r\n\x05shape\x18\x02 \x03(\r\"\x80\x02\n\x0fLOPQModelParams\x12\t\n\x01\x44\x18\x01 \x01(\r\x12\t\n\x01V\x18\x02 \x01(\r\x12\t\n\x01M\x18\x03 \x01(\r\x12\x19\n\x11num_subquantizers\x18\x04 \x01(\r\x12*\n\x02\x43s\x18\x05 \x03(\x0b\x32\x1e.com.flickr.vision.lopq.Matrix\x12*\n\x02Rs\x18\x06 \x03(\x0b\x32\x1e.com.flickr.vision.lopq.Matrix\x12+\n\x03mus\x18\x07 \x03(\x0b\x32\x1e.com.flickr.vision.lopq.Vector\x12,\n\x04subs\x18\x08 \x03(\x0b\x32\x1e.com.flickr.vision.lopq.MatrixB/\n\x16\x63om.flickr.vision.lopqB\x13LOPQModelParametersH\x01')
)
_sym_db.RegisterFileDescriptor(DESCRIPTOR)

_VECTOR = _descriptor.Descriptor(
  name='Vector',
  full_name='com.flickr.vision.lopq.Vector',
  filename=None,
  file=DESCRIPTOR,
  containing_type=None,
  fields=[
    _descriptor.FieldDescriptor(
      name='values', full_name='com.flickr.vision.lopq.Vector.values', index=0,
      number=1, type=2, cpp_type=6, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=_descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\020\001'))),
  ],
  extensions=[
  ],
  nested_types=[],
  enum_types=[
  ],
  options=None,
  is_extendable=False,
  extension_ranges=[],
  oneofs=[
  ],
  serialized_start=44,
  serialized_end=72,
)

_MATRIX = _descriptor.Descriptor(
  name='Matrix',
  full_name='com.flickr.vision.lopq.Matrix',
  filename=None,
  file=DESCRIPTOR,
  containing_type=None,
  fields=[
    _descriptor.FieldDescriptor(
      name='values', full_name='com.flickr.vision.lopq.Matrix.values', index=0,
      number=1, type=2, cpp_type=6, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=_descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\020\001'))),
    _descriptor.FieldDescriptor(
      name='shape', full_name='com.flickr.vision.lopq.Matrix.shape', index=1,
      number=2, type=13, cpp_type=3, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
  ],
  extensions=[
  ],
  nested_types=[],
  enum_types=[
  ],
  options=None,
  is_extendable=False,
  extension_ranges=[],
  oneofs=[
  ],
  serialized_start=74,
  serialized_end=117,
)

_LOPQMODELPARAMS = _descriptor.Descriptor(
  name='LOPQModelParams',
  full_name='com.flickr.vision.lopq.LOPQModelParams',
  filename=None,
  file=DESCRIPTOR,
  containing_type=None,
  fields=[
    _descriptor.FieldDescriptor(
      name='D', full_name='com.flickr.vision.lopq.LOPQModelParams.D', index=0,
      number=1, type=13, cpp_type=3, label=1,
      has_default_value=False, default_value=0,
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='V', full_name='com.flickr.vision.lopq.LOPQModelParams.V', index=1,
      number=2, type=13, cpp_type=3, label=1,
      has_default_value=False, default_value=0,
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='M', full_name='com.flickr.vision.lopq.LOPQModelParams.M', index=2,
      number=3, type=13, cpp_type=3, label=1,
      has_default_value=False, default_value=0,
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='num_subquantizers', full_name='com.flickr.vision.lopq.LOPQModelParams.num_subquantizers', index=3,
      number=4, type=13, cpp_type=3, label=1,
      has_default_value=False, default_value=0,
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='Cs', full_name='com.flickr.vision.lopq.LOPQModelParams.Cs', index=4,
      number=5, type=11, cpp_type=10, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='Rs', full_name='com.flickr.vision.lopq.LOPQModelParams.Rs', index=5,
      number=6, type=11, cpp_type=10, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='mus', full_name='com.flickr.vision.lopq.LOPQModelParams.mus', index=6,
      number=7, type=11, cpp_type=10, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
    _descriptor.FieldDescriptor(
      name='subs', full_name='com.flickr.vision.lopq.LOPQModelParams.subs', index=7,
      number=8, type=11, cpp_type=10, label=3,
      has_default_value=False, default_value=[],
      message_type=None, enum_type=None, containing_type=None,
      is_extension=False, extension_scope=None,
      options=None),
  ],
  extensions=[
  ],
  nested_types=[],
  enum_types=[
  ],
  options=None,
  is_extendable=False,
  extension_ranges=[],
  oneofs=[
  ],
  serialized_start=120,
  serialized_end=376,
)

_LOPQMODELPARAMS.fields_by_name['Cs'].message_type = _MATRIX
_LOPQMODELPARAMS.fields_by_name['Rs'].message_type = _MATRIX
_LOPQMODELPARAMS.fields_by_name['mus'].message_type = _VECTOR
_LOPQMODELPARAMS.fields_by_name['subs'].message_type = _MATRIX
DESCRIPTOR.message_types_by_name['Vector'] = _VECTOR
DESCRIPTOR.message_types_by_name['Matrix'] = _MATRIX
DESCRIPTOR.message_types_by_name['LOPQModelParams'] = _LOPQMODELPARAMS

Vector = _reflection.GeneratedProtocolMessageType('Vector', (_message.Message,), dict(
  DESCRIPTOR = _VECTOR,
  __module__ = 'lopq_model_pb2'
  # @@protoc_insertion_point(class_scope:com.flickr.vision.lopq.Vector)
  ))
_sym_db.RegisterMessage(Vector)

Matrix = _reflection.GeneratedProtocolMessageType('Matrix', (_message.Message,), dict(
  DESCRIPTOR = _MATRIX,
  __module__ = 'lopq_model_pb2'
  # @@protoc_insertion_point(class_scope:com.flickr.vision.lopq.Matrix)
  ))
_sym_db.RegisterMessage(Matrix)

LOPQModelParams = _reflection.GeneratedProtocolMessageType('LOPQModelParams', (_message.Message,), dict(
  DESCRIPTOR = _LOPQMODELPARAMS,
  __module__ = 'lopq_model_pb2'
  # @@protoc_insertion_point(class_scope:com.flickr.vision.lopq.LOPQModelParams)
  ))
_sym_db.RegisterMessage(LOPQModelParams)

DESCRIPTOR.has_options = True
DESCRIPTOR._options = _descriptor._ParseOptions(descriptor_pb2.FileOptions(), _b('\n\026com.flickr.vision.lopqB\023LOPQModelParametersH\001'))
_VECTOR.fields_by_name['values'].has_options = True
_VECTOR.fields_by_name['values']._options = _descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\020\001'))
_MATRIX.fields_by_name['values'].has_options = True
_MATRIX.fields_by_name['values']._options = _descriptor._ParseOptions(descriptor_pb2.FieldOptions(), _b('\020\001'))
# @@protoc_insertion_point(module_scope)


text:
//-----------------------------------------------------------------------
// <copyright file="ProductFacetsViewModel.cs" company="Sitecore Corporation">
//     Copyright (c) Sitecore Corporation 1999-2015
// </copyright>
// <summary>Defines the ProductFacetsViewModel class.</summary>
//-----------------------------------------------------------------------
// Copyright 2015 Sitecore Corporation A/S
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain a copy of the License at
//       http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.
// -------------------------------------------------------------------------------------------

namespace Sitecore.Commerce.Storefront.Models.Storefront
{
    using Sitecore.Commerce.Connect.CommerceServer.Search.Models;
    using Sitecore.Mvc.Presentation;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    /// <summary>
    /// Used to represent a product facet list
    /// </summary>
    public class ProductFacetsViewModel : Sitecore.Mvc.Presentation.RenderingModel
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ProductFacetsViewModel"/> class.
        /// </summary>
        public ProductFacetsViewModel()
        {
            this.ChildProductFacets = new List<CommerceQueryFacet>();
            this.ActiveFacets = new List<CommerceQueryFacet>();
        }

        /// <summary>
        /// Gets or sets the list of product facets
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is the desired behavior")]
        public IEnumerable<CommerceQueryFacet> ChildProductFacets
        {
            get;
            protected set;
        }

        /// <summary>
        /// Gets or sets the list of active facets
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly", Justification = "This is the desired behavior")]
        public IEnumerable<CommerceQueryFacet> ActiveFacets
        {
            get;
            protected set;
        }

        /// <summary>
        /// Initializes the view model
        /// </summary>
        /// <param name="rendering">The rendering</param>
        /// <param name="products">The list of child products</param>
        /// <param name="searchOptions">Any search options used to find products in this category</param>
        public void Initialize(Rendering rendering, ProductSearchResults products, CommerceSearchOptions searchOptions)
        {
            base.Initialize(rendering);

            if (products != null)
                this.ChildProductFacets = products.Facets;

            this.ActiveFacets = searchOptions.FacetFields;
        }
    }
}


text:
<!---
FrozenIsBool True
-->

##Example

the each operation in that case is


text:
require_relative "condition/condition"
require_relative "condition/condition_simple"
require_relative "condition/condition_format"
require_relative "condition/condition_print"
require_relative "condition/condition_regexp"
require_relative "condition/condition_or"
require_relative "condition/condition_in"
require_relative "condition/condition_set_type"
require_relative "condition/condition_nickname"
Dir["#{__dir__}/condition/condition_*.rb"].sort.each do |path| require_relative path end
require_relative "query_tokenizer"

class QueryParser
  def parse(query_string)
    str = query_string.strip
    if str =~ /\A(\+\+)?!(.*)\z/
      if $1 == "++"
        metadata = {ungrouped: true}
      else
        metadata = {}
      end
      name = $2
      # These cards need special treatment:
      # * "Ach! Hans, Run!"
      # * Look at Me, I'm R&D
      # * R&D's Secret Lair
      # * Sword of Dungeons & Dragons
      # * "Rumors of My Death . . ."
      # * Minsc & Boo, Timeless Heroes
      unless name =~ /Ach.*Hans.*Run/i or name =~ /Rumors.*of.*My.*Death/i
        name = name.sub(/\A"(.*)"\z/) { $1 }
      end
      if name =~ %r[&|/] && name !~ /R&D|Dungeons\s*&\s*Dragons|Minsc\s*&|&\s*Boo/i
        cond = ConditionExactMultipart.new(name)
      else
        cond = ConditionExact.new(name)
      end
      [cond, metadata, []]
    else
      @tokens, @warnings = QueryTokenizer.new.tokenize(str)
      @metadata = {}
      query = parse_query
      [query, @metadata, @warnings]
    end
  end

private

  def conds_to_query(conds)
    if conds.empty?
      nil
    elsif conds.size == 1
      conds[0]
    else
      ConditionAnd.new(*conds)
    end
  end

  def parse_query
    old_time, @time = @time, nil
    cond = parse_cond_list
    if @time
      printed_early = ConditionPrint.new("<=", @time)
      cond = conds_to_query([cond, printed_early])
      cond.metadata! :time, @time
      cond
    else
      cond
    end
  ensure
    @time = old_time
  end

  def parse_cond_list
    conds = []
    until @tokens.empty?
      case @tokens[0][0]
      when :close
        # Do not eat token
        break
      when :or
        @tokens.shift
        # This is optimization,
        #   Condition.new(:or, [conds, right_conds])
        # would work just as well
        if conds.empty?
          # Ignore
        else
          right_query = parse_cond_list
          if right_query
            return ConditionOr.new(conds_to_query(conds), right_query)
          else
            break
          end
        end
      when :slash_slash
        @tokens.shift
        # This is semantically meaningful
        left_query = conds_to_query(conds)
        right_query = parse_cond_list
        if left_query and right_query
          return ConditionPart.new(ConditionAnd.new(left_query, ConditionOther.new(right_query)))
        else
          query = left_query || right_query
          if query
            return ConditionPart.new(query)
          else
            return ConditionIsMultipart.new
          end
        end
      # includes open / not
      else
        subquery = parse_cond
        if subquery
          conds << subquery
        else
          break
        end
      end
    end
    conds_to_query(conds)
  end

  def parse_cond
    return nil if @tokens.empty?
    case @tokens[0][0]
    when :open
      @tokens.shift
      subquery = parse_query
      @tokens.shift if @tokens[0] == [:close] # Ignore mismatched
      subquery
    when :close
      return
    when :not, :other, :part, :related, :alt
      tok, = @tokens.shift
      cond = parse_cond
      if cond
        klass = Kernel.const_get("Condition#{tok.capitalize}")
        klass.new(cond)
      else
        # Parse error like "-)" or final "-"
        nil
      end
    when :or
      # Parse error like "- or"
      @tokens.shift
      parse_cond
    when :test
      @tokens.shift[1]
    when :metadata
      # Quietly eat it
      @metadata.merge!(@tokens.shift[1])
      parse_cond
    when :time
      # Quietly eat it, for now
      @warnings << "Multiple time: clauses in same subquery" if @time
      @time = @tokens.shift[1]
      parse_cond
    else
      @warnings << "Unknown token type #{@tokens[0]}"
    end
  end
end


text:
namespace paddle {
namespace operators {

class RollOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    auto data_type = OperatorWithKernel::IndicateVarDataType(ctx, "X");
    return framework::OpKernelType(data_type, ctx.device_context());
  }
};

class RollGradOp : public framework::OperatorWithKernel {
 public:
  using framework::OperatorWithKernel::OperatorWithKernel;

  void InferShape(framework::InferShapeContext* ctx) const override {
    PADDLE_ENFORCE_EQ(ctx->HasInput(framework::GradVarName("Out")),
                      true,
                      platform::errors::InvalidArgument(
                          "Input(Out@GRAD) should be not null."));
    PADDLE_ENFORCE_EQ(ctx->HasOutput(framework::GradVarName("X")),
                      true,
                      platform::errors::InvalidArgument(
                          "Output(X@GRAD) should be not null."));

    ctx->SetOutputDim(framework::GradVarName("X"), ctx->GetInputDim("X"));
  }

 protected:
  framework::OpKernelType GetExpectedKernelType(
      const framework::ExecutionContext& ctx) const override {
    return framework::OpKernelType(OperatorWithKernel::IndicateVarDataType(
                                       ctx, framework::GradVarName("Out")),
                                   ctx.device_context());
  }
};

class RollOpMaker : public framework::OpProtoAndCheckerMaker {
 public:
  void Make() override {
    AddInput("X", "(Tensor) the input tensor.");
    AddOutput("Out", "(Tensor), the output tensor.");
    AddAttr<std::vector<int64_t>>("shifts",
                                  "The number of places by which the elements "
                                  "of the tensor are shifted.")
        .SetDefault({});
    AddInput("ShiftsTensor",
             "The number of places by which the elements of the tensor "
             "are shifted.")
        .AsDispensable();
    AddAttr<std::vector<int64_t>>(
        "axis",
        "Axis along which to roll. It must have the same size "
        "with shifts or size == 0")
        .SetDefault({});
    AddComment(R"DOC(
    Roll the tensor along the given dimension(s).
    Elements that are shifted beyond the last position
    are re-introduced at the first position. If a dimension
    is not specified, the tensor will be flattened before
    rolling and then restored to the original shape.
    )DOC");
  }
};

template <typename T>
class RollGradMaker : public framework::SingleGradOpMaker<T> {
 public:
  using framework::SingleGradOpMaker<T>::SingleGradOpMaker;

 protected:
  void Apply(GradOpPtr<T> op) const override {
    op->SetType("roll_grad");
    op->SetInput("X", this->Input("X"));
    if (this->HasInput("ShiftsTensor")) {
      op->SetInput("ShiftsTensor", this->Input("ShiftsTensor"));
    }
    op->SetInput(framework::GradVarName("Out"), this->OutputGrad("Out"));
    op->SetOutput(framework::GradVarName("X"), this->InputGrad("X"));
    op->SetAttrMap(this->Attrs());
  }
};

DECLARE_NO_NEED_BUFFER_VARS_INFERER(RollGradNoNeedBufferVarsInferer, "X");
}  // namespace operators
}  // namespace paddle

namespace ops = paddle::operators;
DECLARE_INFER_SHAPE_FUNCTOR(roll,
                            RollInferShapeFunctor,
                            PD_INFER_META(phi::RollInferMeta));

REGISTER_OPERATOR(roll,
                  ops::RollOp,
                  ops::RollOpMaker,
                  ops::RollGradMaker<paddle::framework::OpDesc>,
                  ops::RollGradMaker<paddle::imperative::OpBase>,
                  RollInferShapeFunctor);
REGISTER_OPERATOR(roll_grad,
                  ops::RollGradOp,
                  ops::RollGradNoNeedBufferVarsInferer);

REGISTER_OP_VERSION(roll)
    .AddCheckpoint(
        R"ROC(
      Upgrade roll add 1 attribute [axis], delete 1 attribute[dims].
    )ROC",
        paddle::framework::compatible::OpVersionDesc()
            .NewAttr("axis",
                     "(std::vector<int64_t>) Axis along which to roll. "
                     "It must have the same size with shifts, or size = 0.",
                     std::vector<int64_t>())
            .DeleteAttr("dims",
                        "(std::vector<int64_t>) Dims along which to roll. "
                        "It must have the same size with shifts, or size = 0."))
    .AddCheckpoint(
        R"ROC(Upgrade roll add a dispensable input "ShiftsTensor".)ROC",
        paddle::framework::compatible::OpVersionDesc().NewInput(
            "ShiftsTensor",
            "The number of places by which the elements of"
            "the tensor are shifted."));


text:
namespace BoardgameSimulator.SQLiteDB
{
    using Repositories;

    public class BoardgameSimulatorSqLiteData : IBoardgameSimulatorSqLiteData
    {
        private readonly IBoardgameSimulatorSqLiteDbContext context;

        public BoardgameSimulatorSqLiteData(IBoardgameSimulatorSqLiteDbContext context)
        {
            this.context = context;

            this.UnitsCosts = new BoardgameSimulatorSqLiteUnitsCostsRepository(this.context);
        }

        public BoardgameSimulatorSqLiteData()
            : this(new BoardgameSimulatorSqLiteDbContext())
        {
        }

        public IBoardgameSimulatorSqLiteUnitsCostsRepository UnitsCosts { get; private set; }
    }
}


text:
<?php

namespace League\OAuth2\Client\Test\Provider;

use League\OAuth2\Client\Provider\Google as GoogleProvider;

use Mockery as m;

class GoogleTest extends \PHPUnit_Framework_TestCase
{
    protected $provider;

    protected function setUp()
    {
        $this->provider = new GoogleProvider([
            'clientId' => 'mock_client_id',
            'clientSecret' => 'mock_secret',
            'redirectUri' => 'none',
            'hostedDomain' => 'mock_domain',
            'accessType' => 'mock_access_type'
        ]);
    }

    public function tearDown()
    {
        m::close();
        parent::tearDown();
    }

    public function testAuthorizationUrl()
    {
        $url = $this->provider->getAuthorizationUrl();
        $uri = parse_url($url);
        parse_str($uri['query'], $query);

        $this->assertArrayHasKey('client_id', $query);
        $this->assertArrayHasKey('redirect_uri', $query);
        $this->assertArrayHasKey('state', $query);
        $this->assertArrayHasKey('scope', $query);
        $this->assertArrayHasKey('response_type', $query);
        $this->assertArrayHasKey('approval_prompt', $query);
        $this->assertArrayHasKey('hd', $query);
        $this->assertArrayHasKey('access_type', $query);

        $this->assertEquals('mock_access_type', $query['access_type']);
        $this->assertEquals('mock_domain', $query['hd']);

        $this->assertContains('email', $query['scope']);
        $this->assertContains('profile', $query['scope']);
        $this->assertContains('openid', $query['scope']);

        $this->assertAttributeNotEmpty('state', $this->provider);
    }

    public function testBaseAccessTokenUrl()
    {
        $url = $this->provider->getBaseAccessTokenUrl([]);
        $uri = parse_url($url);

        $this->assertEquals('/o/oauth2/token', $uri['path']);
    }

    public function testResourceOwnerDetailsUrl()
    {
        $token = m::mock('League\OAuth2\Client\Token\AccessToken', [['access_token' => 'mock_access_token']]);

        $url = $this->provider->getResourceOwnerDetailsUrl($token);
        $uri = parse_url($url);

        $this->assertEquals('/plus/v1/people/me', $uri['path']);
        $this->assertNotContains('mock_access_token', $url);

    }

    public function testResourceOwnerDetailsUrlCustomFields()
    {
        $provider = new GoogleProvider([
            'clientId' => 'mock_client_id',
            'clientSecret' => 'mock_secret',
            'redirectUri' => 'none',
            'userFields' => [
                'domain',
                'gender',
                'verified',
            ],
        ]);

        $token = m::mock('League\OAuth2\Client\Token\AccessToken', [['access_token' => 'mock_access_token']]);

        $url = $provider->getResourceOwnerDetailsUrl($token);
        $uri = parse_url($url);
        parse_str($uri['query'], $query);

        $this->assertArrayHasKey('fields', $query);
        $this->assertArrayHasKey('alt', $query);

        // Always JSON for consistency
        $this->assertEquals('json', $query['alt']);

        $fields = explode(',', $query['fields']);

        // Default values
        $this->assertContains('displayName', $fields);
        $this->assertContains('emails/value', $fields);
        $this->assertContains('image/url', $fields);

        // Configured values
        $this->assertContains('domain', $fields);
        $this->assertContains('gender', $fields);
        $this->assertContains('verified', $fields);
    }

    public function testUserData()
    {
        $response = json_decode('{"emails": [{"value": "mock_email"}],"id": "12345","displayName": "mock_name","name": {"familyName": "mock_last_name","givenName": "mock_first_name"},"image": {"url": "mock_image_url"}}', true);

        $provider = m::mock('League\OAuth2\Client\Provider\Google[fetchResourceOwnerDetails]')
            ->shouldAllowMockingProtectedMethods();

        $provider->shouldReceive('fetchResourceOwnerDetails')
            ->times(1)
            ->andReturn($response);

        $token = m::mock('League\OAuth2\Client\Token\AccessToken');
        $user = $provider->getResourceOwner($token);

        $this->assertInstanceOf('League\OAuth2\Client\Provider\ResourceOwnerInterface', $user);

        $this->assertEquals(12345, $user->getId());
        $this->assertEquals('mock_name', $user->getName());
        $this->assertEquals('mock_first_name', $user->getFirstName());
        $this->assertEquals('mock_last_name', $user->getLastName());
        $this->assertEquals('mock_email', $user->getEmail());
        $this->assertEquals('mock_image_url', $user->getAvatar());

        $user = $user->toArray();

        $this->assertArrayHasKey('id', $user);
        $this->assertArrayHasKey('displayName', $user);
        $this->assertArrayHasKey('emails', $user);
        $this->assertArrayHasKey('image', $user);
        $this->assertArrayHasKey('name', $user);
    }

    /**
     * @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
     */
    public function testErrorResponse()
    {
        $response = m::mock('GuzzleHttp\Psr7\Response');

        $response->shouldReceive('getHeader')
            ->with('content-type')
            ->andReturn(['application/json']);

        $response->shouldReceive('getBody')
            ->andReturn('{"error": {"code": 400, "message": "I am an error"}}');

        $provider = m::mock('League\OAuth2\Client\Provider\Google[sendRequest]')
            ->shouldAllowMockingProtectedMethods();

        $provider->shouldReceive('sendRequest')
            ->times(1)
            ->andReturn($response);

        $token = m::mock('League\OAuth2\Client\Token\AccessToken');
        $user = $provider->getResourceOwner($token);
    }
}


text:
package org.apache.carbondata.core.datastore.impl;

import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.carbondata.core.constants.CarbonCommonConstants;
import org.apache.carbondata.core.datastore.FileHolder;

public class FileHolderImpl implements FileHolder {
  /**
   * cache to hold filename and its stream
   */
  private Map<String, FileChannel> fileNameAndStreamCache;
  private String queryId;

  /**
   * FileHolderImpl Constructor
   * It will create the cache
   */
  public FileHolderImpl() {
    this.fileNameAndStreamCache =
        new HashMap<String, FileChannel>(CarbonCommonConstants.DEFAULT_COLLECTION_SIZE);
  }

  public FileHolderImpl(int capacity) {
    this.fileNameAndStreamCache = new HashMap<String, FileChannel>(capacity);
  }

  /**
   * This method will be used to read the byte array from file based on offset
   * and length(number of bytes) need to read
   *
   * @param filePath fully qualified file path
   * @param offset   reading start position,
   * @param length   number of bytes to be read
   * @return read byte array
   */
  @Override public byte[] readByteArray(String filePath, long offset, int length)
      throws IOException {
    FileChannel fileChannel = updateCache(filePath);
    ByteBuffer byteBffer = read(fileChannel, length, offset);
    return byteBffer.array();
  }

  /**
   * This method will be used to close all the streams currently present in the cache
   */
  @Override public void finish() throws IOException {
    for (Entry<String, FileChannel> entry : fileNameAndStreamCache.entrySet()) {
      FileChannel channel = entry.getValue();
      if (null != channel) {
        channel.close();
      }
    }
  }

  /**
   * This method will be used to read int from file from postion(offset), here
   * length will be always 4 bacause int byte size if 4
   *
   * @param filePath fully qualified file path
   * @param offset   reading start position,
   * @return read int
   */
  @Override public int readInt(String filePath, long offset) throws IOException {
    FileChannel fileChannel = updateCache(filePath);
    ByteBuffer byteBffer = read(fileChannel, CarbonCommonConstants.INT_SIZE_IN_BYTE, offset);
    return byteBffer.getInt();
  }

  /**
   * This method will be used to read int from file from postion(offset), here
   * length will be always 4 bacause int byte size if 4
   *
   * @param filePath fully qualified file path
   * @return read int
   */
  @Override public int readInt(String filePath) throws IOException {
    FileChannel fileChannel = updateCache(filePath);
    ByteBuffer byteBffer = read(fileChannel, CarbonCommonConstants.INT_SIZE_IN_BYTE);
    return byteBffer.getInt();
  }

  /**
   * This method will be used to read int from file from postion(offset), here
   * length will be always 4 bacause int byte size if 4
   *
   * @param filePath fully qualified file path
   * @param offset   reading start position,
   * @return read int
   */
  @Override public long readDouble(String filePath, long offset) throws IOException {
    FileChannel fileChannel = updateCache(filePath);
    ByteBuffer byteBffer = read(fileChannel, CarbonCommonConstants.LONG_SIZE_IN_BYTE, offset);
    return byteBffer.getLong();
  }

  /**
   * This method will be used to check whether stream is already present in
   * cache or not for filepath if not present then create it and then add to
   * cache, other wise get from cache
   *
   * @param filePath fully qualified file path
   * @return channel
   */
  private FileChannel updateCache(String filePath) throws FileNotFoundException {
    FileChannel fileChannel = fileNameAndStreamCache.get(filePath);
    if (null == fileChannel) {
      FileInputStream stream = new FileInputStream(filePath);
      fileChannel = stream.getChannel();
      fileNameAndStreamCache.put(filePath, fileChannel);
    }
    return fileChannel;
  }

  /**
   * This method will be used to read from file based on number of bytes to be read and position
   *
   * @param channel file channel
   * @param size    number of bytes
   * @param offset  position
   * @return byte buffer
   */
  private ByteBuffer read(FileChannel channel, int size, long offset) throws IOException {
    ByteBuffer byteBffer = ByteBuffer.allocate(size);
    channel.position(offset);
    channel.read(byteBffer);
    byteBffer.rewind();
    return byteBffer;
  }

  /**
   * This method will be used to read from file based on number of bytes to be read and position
   *
   * @param channel file channel
   * @param size    number of bytes
   * @return byte buffer
   */
  private ByteBuffer read(FileChannel channel, int size) throws IOException {
    ByteBuffer byteBffer = ByteBuffer.allocate(size);
    channel.read(byteBffer);
    byteBffer.rewind();
    return byteBffer;
  }

  /**
   * This method will be used to read the byte array from file based on length(number of bytes)
   *
   * @param filePath fully qualified file path
   * @param length   number of bytes to be read
   * @return read byte array
   */
  @Override public byte[] readByteArray(String filePath, int length) throws IOException {
    FileChannel fileChannel = updateCache(filePath);
    ByteBuffer byteBffer = read(fileChannel, length);
    return byteBffer.array();
  }

  /**
   * This method will be used to read long from file from postion(offset), here
   * length will be always 8 bacause int byte size is 8
   *
   * @param filePath fully qualified file path
   * @param offset   reading start position,
   * @return read long
   */
  @Override public long readLong(String filePath, long offset) throws IOException {
    FileChannel fileChannel = updateCache(filePath);
    ByteBuffer byteBffer = read(fileChannel, CarbonCommonConstants.LONG_SIZE_IN_BYTE, offset);
    return byteBffer.getLong();
  }

  @Override public ByteBuffer readByteBuffer(String filePath, long offset, int length)
      throws IOException {
    ByteBuffer byteBuffer = ByteBuffer.allocate(length);
    FileChannel fileChannel = updateCache(filePath);
    fileChannel.position(offset);
    fileChannel.read(byteBuffer);
    byteBuffer.rewind();
    return byteBuffer;
  }

  @Override public void setQueryId(String queryId) {
    this.queryId = queryId;
  }

  @Override public String getQueryId() {
    return queryId;
  }

  @Override public DataInputStream getDataInputStream(String filePath, long offset)
      throws IOException {
    FileInputStream stream = new FileInputStream(filePath);
    stream.skip(offset);
    return new DataInputStream(new BufferedInputStream(stream));
  }
}


text:
package com.datatorrent.benchmark.algo;

import org.apache.hadoop.conf.Configuration;

import com.datatorrent.lib.algo.UniqueCounter;
import com.datatorrent.lib.converter.MapToKeyHashValuePairConverter;
import com.datatorrent.lib.io.ConsoleOutputOperator;
import com.datatorrent.common.partitioner.StatelessPartitioner;

import com.datatorrent.lib.stream.Counter;
import com.datatorrent.lib.testbench.RandomEventGenerator;

import com.datatorrent.api.Context;
import com.datatorrent.api.DAG;
import com.datatorrent.api.DAG.Locality;
import com.datatorrent.api.StreamingApplication;
import com.datatorrent.api.annotation.ApplicationAnnotation;

/**
 * Application to demonstrate PartitionableUniqueCount operator. <br>
 * The input operator generate random keys, which is sent to
 * PartitionableUniqueCount operator initially partitioned into three partitions to
 * test unifier functionality, and output of the operator is sent to verifier to verify
 * that it generates correct result.
 *
 * @since 1.0.2
 */
@ApplicationAnnotation(name = "UniqueCountBenchmark")
public class UniqueValueCountBenchmarkApplication implements StreamingApplication
{

  @Override
  public void populateDAG(DAG dag, Configuration entries)
  {
    dag.setAttribute(dag.APPLICATION_NAME, "UniqueValueCountDemo");
    dag.setAttribute(dag.DEBUG, true);

    /* Generate random key-value pairs */
    RandomEventGenerator randGen = dag.addOperator("randomgen", new RandomEventGenerator());
    randGen.setMaxvalue(999999);
    randGen.setTuplesBlastIntervalMillis(50);
    dag.setAttribute(randGen, Context.OperatorContext.PARTITIONER, new StatelessPartitioner<RandomEventGenerator>(3));

    /* Initialize with three partition to start with */
    UniqueCounter<Integer> uniqCount = dag.addOperator("uniqevalue", new UniqueCounter<Integer>());
    MapToKeyHashValuePairConverter<Integer, Integer> converter = dag.addOperator("converter", new MapToKeyHashValuePairConverter());

    dag.setAttribute(uniqCount, Context.OperatorContext.PARTITIONER, new StatelessPartitioner<UniqueCounter<Integer>>(3));
    dag.setInputPortAttribute(uniqCount.data, Context.PortContext.PARTITION_PARALLEL, true);
    uniqCount.setCumulative(false);

    Counter counter = dag.addOperator("count", new Counter());
    ConsoleOutputOperator output = dag.addOperator("output", new ConsoleOutputOperator());

    dag.addStream("datain", randGen.integer_data, uniqCount.data);
    dag.addStream("convert", uniqCount.count, converter.input).setLocality(Locality.THREAD_LOCAL);
    dag.addStream("consoutput", converter.output, counter.input);
    dag.addStream("final", counter.output, output.input);
  }
}


text:
package com.lengchuan.springBoot.exceptionHandler;

import org.springframework.web.bind.annotation.ControllerAdvice;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author lengchuan <lishuijun1992@gmail.com>
 * @date 17-3-27
 */
@ControllerAdvice()
public class ExceptionHandler {

    /**
     * 全局异常处理
     *
     * @param response
     * @param e
     */
    @org.springframework.web.bind.annotation.ExceptionHandler(value = Exception.class)
    public void ExceptionHandler(HttpServletResponse response, Exception e) {
        System.out.println("-------全局默认异常处理-----");
        try {
            response.getOutputStream().write("全局默认异常处理".getBytes());
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}


text:
'use strict';

const simpleQueryValidator = require('./generic/simpleQuery.validator');

exports.execute = execute;

function execute() {
    return simpleQueryValidator
        .executeInScope('sys_documentation',
            'element=NULL^hint=NULL',
            'Found tables without hints');
}


text:
@interface KSFrameworkStatsTest : SenTestCase
@end

@implementation KSFrameworkStatsTest

- (void)testBasic {
  STAssertNil([KSFrameworkStats sharedStats], nil);

  KSStatsCollection *stats = [KSStatsCollection statsCollectionWithPath:@"/dev/null"
                                                        autoSynchronize:NO];
  STAssertNotNil(stats, nil);

  [KSFrameworkStats setSharedStats:stats];

  STAssertNotNil([KSFrameworkStats sharedStats], nil);
  STAssertTrue([KSFrameworkStats sharedStats] == stats, nil);

  [KSFrameworkStats setSharedStats:nil];
  STAssertNil([KSFrameworkStats sharedStats], nil);
}

@end