text:
package javatutorial;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Iterators {

    public static void main(String[] args) {

        List<Integer> nums = new ArrayList<>();
        nums.add(4);
        nums.add(-7);
        nums.add(5);
        nums.add(-19);

        Iterator<Integer> iterator = nums.iterator();
        while (iterator.hasNext()) {
            if (iterator.next() < 0) {
                // Removes from the last element returned by this iterator
                iterator.remove();
            }
        }
        System.out.println(nums);

    }

}


text:
My notes on Protocol Inheritance<!--more-->

insert class hierarchy diagram here.

## Rational

Say you have 3 classes that needs to call the same method with the same code. But you cant extend a new BaseClass. Because the BaseClass is different in each 3 classes. You need to override the same method in all classes "scroll" And you want to be able to call the "super" There is no super with pop (protocol oriented programming)

```swift
/**
 * This setup describes how classes may call methods and the methods call their "super" method.
 * Inheritance with protocols. retaining the ability to call "super" and the ability to override
 */
protocol Scrollable{
    func scroll()
}
extension Scrollable{
    func scroll(){
        defaulScroll()
    }
    func defaulScroll(){
        Swift.print("a")
    }
}
class A:Scrollable{}
protocol BScrollable:Scrollable{}
extension BScrollable{
    func scroll() {
        bScroll()
    }
    func bScroll(){
        Swift.print("b")
        defaulScroll()
    }
}
class B:BScrollable{}
//how do we make c call b call a
protocol CScrollable:BScrollable{}
extension CScrollable{
    func scroll() {
        cScroll()
    }
    func cScroll(){
        Swift.print("c")
        bScroll()
    }
}
class C:CScrollable{}

let a = A()
a.scroll()//a
print("")
let b = B()
b.scroll()//b, a
print("")
let c = C()
c.scroll()//c , b , a

```

## An alternative is to :

```swift
protocol X{}
protocol Y{}
protocol Z{}
extension X{func render(print("x")){}}
extension Y{func render(print("y")){}}
extension Z{func render(print("z")){}}
class A:X,Y,Z{
    func render(){//can be called from a Super class
        (self as X).render()
        (self as Y).render()
        (self as Z).render()
    }
}
let a = A()
a.render()//z,x,y
```

redpajama_set_name: RedPajamaGithub


text:
'use strict';

module.exports = require('../../../index')(module);


text:
from sys import path

# Setuptools imports
from setuptools import setup, find_packages

# owls-common imports
path.append('common/modules')
from version_check import owls_python_version_check

# Check that this version of Python is supported
owls_python_version_check()

# Setup owls-cache
setup(
    # Basic installation information
    name = 'owls-hep',
    version = '0.0.2',
    packages = find_packages(exclude = ['common', 'testing']),

    # Dependencies
    install_requires = [
        'six >= 1.7.3',
        'owls-cache >= 0.0.2',
        'owls-parallel >= 0.0.2',
    ],

    # Metadata for PyPI
    author = 'Henrik Öhman',
    author_email = 'speeph@gmail.com',
    description = 'Modular analysis toolkit - HEP module',
    license = 'MIT',
    keywords = 'python big data analysis',
    url = 'https://github.com/spiiph/owls-hep'
)


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rip.rip_ui.core.popups;

import java.util.Optional;
import javafx.scene.control.Alert;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.control.ButtonBar.ButtonData;
import javafx.scene.control.ButtonType;

/**
 *
 * Pop ups to warn when deleting a resource
 */
public class DeleteResourceWarning {

    private boolean isOk = false;

    private final String DELETE_RESOURCE = "Delete Resource";

    private final String DELETE_WARNING = "Deleting this Resource may loose all"
                                           + "paths \nand parameter information "
                                           + "related to it";

    private final String DELETE_CONFIMATION = "Are you sure you want to delete "
                                               + "\n";

    private final String YES_BTN = "Yes";

    private final String NO_BTN = "No";

    private final String CANCEL = "Cancel";

    public DeleteResourceWarning(String resource) {

        Alert alert = new Alert(AlertType.WARNING);
        alert.setTitle(DELETE_RESOURCE);
        alert.setHeaderText(DELETE_WARNING);
        alert.setContentText(DELETE_CONFIMATION+resource+" ?");

        ButtonType yesButton = new ButtonType(YES_BTN);
        ButtonType noButton = new ButtonType(NO_BTN);
        ButtonType cancelButton = new ButtonType(CANCEL, ButtonData.CANCEL_CLOSE);

        alert.getButtonTypes().setAll(yesButton, noButton, cancelButton);

        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == yesButton) {
            isOk = true;
        }
    }

    public boolean getUserResponse(){
        return isOk;
    }

}


text:
namespace Microsoft.Azure.Management.Synapse.Models
{
    using Newtonsoft.Json;
    using System.Linq;

    /// <summary>
    /// A class that contains the optimized auto scale definition.
    /// </summary>
    public partial class OptimizedAutoscale
    {
        /// <summary>
        /// Initializes a new instance of the OptimizedAutoscale class.
        /// </summary>
        public OptimizedAutoscale()
        {
            CustomInit();
        }

        /// <summary>
        /// Initializes a new instance of the OptimizedAutoscale class.
        /// </summary>
        /// <param name="version">The version of the template defined, for
        /// instance 1.</param>
        /// <param name="isEnabled">A boolean value that indicate if the
        /// optimized autoscale feature is enabled or not.</param>
        /// <param name="minimum">Minimum allowed instances count.</param>
        /// <param name="maximum">Maximum allowed instances count.</param>
        public OptimizedAutoscale(int version, bool isEnabled, int minimum, int maximum)
        {
            Version = version;
            IsEnabled = isEnabled;
            Minimum = minimum;
            Maximum = maximum;
            CustomInit();
        }

        /// <summary>
        /// An initialization method that performs custom operations like setting defaults
        /// </summary>
        partial void CustomInit();

        /// <summary>
        /// Gets or sets the version of the template defined, for instance 1.
        /// </summary>
        [JsonProperty(PropertyName = "version")]
        public int Version { get; set; }

        /// <summary>
        /// Gets or sets a boolean value that indicate if the optimized
        /// autoscale feature is enabled or not.
        /// </summary>
        [JsonProperty(PropertyName = "isEnabled")]
        public bool IsEnabled { get; set; }

        /// <summary>
        /// Gets or sets minimum allowed instances count.
        /// </summary>
        [JsonProperty(PropertyName = "minimum")]
        public int Minimum { get; set; }

        /// <summary>
        /// Gets or sets maximum allowed instances count.
        /// </summary>
        [JsonProperty(PropertyName = "maximum")]
        public int Maximum { get; set; }

        /// <summary>
        /// Validate the object.
        /// </summary>
        /// <exception cref="Rest.ValidationException">
        /// Thrown if validation fails
        /// </exception>
        public virtual void Validate()
        {
            //Nothing to validate
        }
    }
}


text:
import { take, fork } from "redux-saga/effects";
import { DefaultGraphStrategyLoadContentRequest, DEFAULT_GRAPH_STRATEGY_LOAD_CONTENT, EVALUATE_DEPENDENCY, EvaluateDependencyRequest, DependencyGraph, Dependency } from "aerial-sandbox2";
import * as path from "path";
import detective = require("detective");
import { request, takeRequest, weakMemo } from "aerial-common2";
import { Script, createContext } from "vm";

export function createCommonJSSaga(mimeType = "application/javascript") {
  return function*() {
    yield fork(handleLoadCommonJS, mimeType);
    yield fork(handleEvaluateCommonJS, mimeType);
  };
}

function* handleLoadCommonJS(mimeType: string) {
  while(true) {
    yield takeRequest((request: DefaultGraphStrategyLoadContentRequest) => request.type === DEFAULT_GRAPH_STRATEGY_LOAD_CONTENT && request.contentType === mimeType, ({ content, contentType }: DefaultGraphStrategyLoadContentRequest) => {
      const dependencies = detective(String(content));
      return {
        content,
        contentType,
        importedDependencyUris: dependencies
      };
    });
  }
}

function* handleEvaluateCommonJS(mimeType: string) {
  while(true) {
    yield takeRequest((request: EvaluateDependencyRequest) => request.type === EVALUATE_DEPENDENCY && request.entry.contentType === mimeType, ({ context: globalContext, entry, graph }: EvaluateDependencyRequest) => {

      const moduleContexts = globalContext.$$moduleContexts = {};
      globalContext.global = globalContext;
      globalContext.console = console;

      const evaluate = (dep: Dependency) => {
        if (moduleContexts[dep.hash]) return moduleContexts[dep.hash].module.exports;

        const script = compile(dep.hash, dep.uri, String(dep.content));

        const context = moduleContexts[dep.hash] = {
          __dirname: path.dirname(dep.uri),
          __filename: dep.uri,
          require(depPath) {
            const hash = dep.importedDependencyHashes[dep.importedDependencyUris.indexOf(depPath)];
            return evaluate(graph.allDependencies[hash]);
          },
          module: {
            exports: {}
          }
        };

        script.runInContext(globalContext);

        return context.module.exports;
      }

      return evaluate(entry);
    });
  }
}

const compile = weakMemo((hash: string, uri: string, content: string) => {
  return new Script(`
    with($$moduleContexts["${hash}"]) {
      ${content}
    }
  `, {
    filename: uri,
    displayErrors: true
  });
});


text:
package com.xiongyingqi.json;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xiongyingqi.vo.User;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author qi
 * @version 2015-09-02 14:07
 */
public class JSonTest {
    public static final Logger logger = LoggerFactory.getLogger(JSonTest.class);

    @Test
    public void testObject() throws Exception {
        User user = new User();
        user.setUserId(1);
        user.setUserName("a");
        user.setAge(10);
        ObjectMapper mapper = new ObjectMapper();
        System.out.println(mapper.writeValueAsString(user));
        logger.error("呵呵");
    }
//    @Test
//    public void testStream() throws Exception {
//        User user = new User();
//        user.setUserId(1);
//        user.setUserName("a");
//        user.setAge(10);
//        User user2 = new User();
//        user2.setUserId(2);
//        user2.setUserName("b");
//        user2.setAge(12);
//
//        List<User> userList = new ArrayList<User>();
//        userList.add(user);
//        userList.add(user2);
//        int ages = userList.stream()
//                .filter(x -> x.getAge() != null)
//                .mapToInt(User::getAge)
//                .sum();
//        logger.error("age sum: {}", ages);
//    }
}


text:
package com.greenbits.binlog;

import com.github.shyiko.mysql.binlog.event.Event;

public interface RowEventListener {
    void startup(ServerVersion version);

    void shutdown();

    void beginTransaction();

    void onUpdate(UpdateEvent updateEvent);

    void onWrite(WriteEvent writeEvent);

    void onDelete(DeleteEvent deleteEvent);

    void onQuery(String sql);

    void onError(Throwable error, Event event);

    void commitTransaction();
}


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BooliNET.Examples
{
    class Program
    {
        const string CallerId = "yourbooliid"; // Supplied from booli, http://www.booli.se/api/key
        const string Key = "P9rfkeXvKOXgHIvXZ1npRXVGG2kHLmXpd5NIetHS"; // Supplied from booli, http://www.booli.se/api/key

        static void Main(string[] args)
        {
            var examples = new Examples.BooliExamples(CallerId, Key);
            examples.RunSimpleListingsExample();

            Console.WriteLine();
            examples.CenterDimListingsExample();

            Console.WriteLine();
            examples.BboxListingsExample();

            Console.WriteLine();
            examples.AreaIdListingsExample();

            Console.WriteLine();
            examples.RunSimpleSoldExample();

            Console.WriteLine();
            examples.PriceSoldExample();

            Console.WriteLine();
            examples.RunSimpleAreaExample();

            Console.WriteLine();
            examples.RunSimpleSoldIdExample();

            Console.ReadKey();
        }
    }
}


text:
import ServiceClient = require('azure-sb/lib/serviceclient');

declare class ServiceBusServiceClient extends ServiceClient {
    constructor(accessKey?: string,
                issuer?: string,
                sharedAccessKeyName?: string,
                sharedAccessKeyValue?: string,
                host?: string,
                acsHost?: string,
        authenticationProvider?: object);
}

export = ServiceBusServiceClient;


text:
#ifndef MD5_H
#define MD5_H

#include <string>
#include <fstream>

/* Type define */
typedef unsigned char byte;
typedef unsigned int uint32;

using std::string;
using std::ifstream;

/* MD5 declaration. */
class MD5 {
public:
	MD5();
	MD5(const void* input, size_t length);
	MD5(const string& str);
	MD5(ifstream& in);
	void update(const void* input, size_t length);
	void update(const string& str);
	void update(ifstream& in);
	const byte* digest();
	string toString();
	void reset();

private:
	void update(const byte* input, size_t length);
	void final();
	void transform(const byte block[64]);
	void encode(const uint32* input, byte* output, size_t length);
	void decode(const byte* input, uint32* output, size_t length);
	string bytesToHexString(const byte* input, size_t length);

	/* class uncopyable */
	MD5(const MD5&);
	MD5& operator=(const MD5&);

private:
	uint32 _state[4];	/* state (ABCD) */
	uint32 _count[2];	/* number of bits, modulo 2^64 (low-order word first) */
	byte _buffer[64];	/* input buffer */
	byte _digest[16];	/* message digest */
	bool _finished;		/* calculate finished ? */

	static const byte PADDING[64];	/* padding for calculate */
	static const char HEX[16];
	enum { BUFFER_SIZE = 1024 };
};

#endif /*MD5_H*/


text:
class CreateGroupingsTags < ActiveRecord::Migration
  def up
    create_table :groupings_tags, id: false do |t|
      t.integer :tag_id, null: false
      t.integer :grouping_id, null: false
    end

    add_index :groupings_tags, [:tag_id, :grouping_id], unique: true
  end

  def down
    remove_index :groupings_tags, column: [:tag_id, :grouping_id]
    drop_table :groupings_tags
  end
end


text: Kicking peaches out your gills.


text:
package myra.rule.archive;

import static myra.Config.CONFIG;
import static myra.datamining.Algorithm.RANDOM_GENERATOR;
import static myra.datamining.Dataset.NOT_COVERED;

import java.io.InputStreamReader;
import java.util.Random;

import junit.framework.TestCase;
import myra.Archive;
import myra.classification.rule.ClassificationRule;
import myra.datamining.ARFFReader;
import myra.datamining.Dataset;
import myra.datamining.Dataset.Instance;
import myra.datamining.VariableArchive;
import myra.rule.Graph.Entry;
import myra.rule.Heuristic;
import myra.rule.Rule;

/**
 * @author Fernando Esteban Barril Otero
 */
public class ArchiveRuleFactoryTest extends TestCase {
    /**
     * The dataset for the test.
     */
    private Dataset dataset;

    /**
     * The graph for the test.
     */
    private Graph graph;

    @Override
    protected void setUp() throws Exception {
        CONFIG.set(Archive.ARCHIVE_SIZE, 5);
        CONFIG.set(VariableArchive.PRECISION, 2.0);
        CONFIG.set(RANDOM_GENERATOR, new Random(System.currentTimeMillis()));
        CONFIG.set(Rule.DEFAULT_RULE, ClassificationRule.class);

        ARFFReader reader = new ARFFReader();
        dataset = reader.read(new InputStreamReader(getClass()
                .getResourceAsStream("/weather.arff")));

        graph = new Graph(dataset);
        Entry[][] matrix = graph.matrix();

        for (int i = 0; i < graph.size(); i++) {
            for (int j = 0; j < graph.size(); j++) {
                if (matrix[i][j] != null) {
                    matrix[i][j] = new Entry(1.0, 1.0);
                }
            }
        }
    }

    public void testCreate() {
        Instance[] instances = Instance.newArray(dataset.size());
        Instance.markAll(instances, NOT_COVERED);

        Entry[] heuristic =
                new Heuristic.None().compute(graph, dataset, instances);

        Rule rule = new ArchiveRuleFactory()
                .create(0, graph, heuristic, dataset, instances);
        assertTrue(!rule.isEmpty());
    }
}


text:
import { ENV } from '@ember/-internals/environment';
import { assign } from '@ember/polyfills';

/**
  Set `EmberENV.FEATURES` in your application's `config/environment.js` file
  to enable canary features in your application.

  See the [feature flag guide](https://guides.emberjs.com/release/configuring-ember/feature-flags/)
  for more details.

  @module @ember/canary-features
  @public
*/

export const DEFAULT_FEATURES = {
  EMBER_LIBRARIES_ISREGISTERED: null,
  EMBER_IMPROVED_INSTRUMENTATION: null,
  EMBER_GLIMMER_NAMED_ARGUMENTS: true,
  EMBER_ROUTING_ROUTER_SERVICE: true,
  EMBER_ENGINES_MOUNT_PARAMS: true,
  EMBER_MODULE_UNIFICATION: null,
  GLIMMER_CUSTOM_COMPONENT_MANAGER: true,
  GLIMMER_MODIFIER_MANAGER: null,
  EMBER_METAL_TRACKED_PROPERTIES: null,
  EMBER_GLIMMER_ANGLE_BRACKET_INVOCATION: true,
  EMBER_GLIMMER_ARRAY_HELPER: null,
};

/**
  The hash of enabled Canary features. Add to this, any canary features
  before creating your application.

  @class FEATURES
  @static
  @since 1.1.0
  @public
*/
export const FEATURES = assign(DEFAULT_FEATURES, ENV.FEATURES);

/**
  Determine whether the specified `feature` is enabled. Used by Ember's
  build tools to exclude experimental features from beta/stable builds.

  You can define the following configuration options:

  * `EmberENV.ENABLE_OPTIONAL_FEATURES` - enable any features that have not been explicitly
    enabled/disabled.

  @method isEnabled
  @param {String} feature The feature to check
  @return {Boolean}
  @since 1.1.0
  @public
*/
export function isEnabled(feature: string): boolean {
  let featureValue = FEATURES[feature];

  if (featureValue === true || featureValue === false) {
    return featureValue;
  } else if (ENV.ENABLE_OPTIONAL_FEATURES) {
    return true;
  } else {
    return false;
  }
}

function featureValue(value: null | boolean) {
  if (ENV.ENABLE_OPTIONAL_FEATURES && value === null) {
    return true;
  }

  return value;
}

export const EMBER_LIBRARIES_ISREGISTERED = featureValue(FEATURES.EMBER_LIBRARIES_ISREGISTERED);
export const EMBER_IMPROVED_INSTRUMENTATION = featureValue(FEATURES.EMBER_IMPROVED_INSTRUMENTATION);
export const EMBER_GLIMMER_NAMED_ARGUMENTS = featureValue(FEATURES.EMBER_GLIMMER_NAMED_ARGUMENTS);
export const EMBER_ROUTING_ROUTER_SERVICE = featureValue(FEATURES.EMBER_ROUTING_ROUTER_SERVICE);
export const EMBER_ENGINES_MOUNT_PARAMS = featureValue(FEATURES.EMBER_ENGINES_MOUNT_PARAMS);
export const EMBER_MODULE_UNIFICATION = featureValue(FEATURES.EMBER_MODULE_UNIFICATION);
export const EMBER_METAL_TRACKED_PROPERTIES = featureValue(FEATURES.EMBER_METAL_TRACKED_PROPERTIES);
export const GLIMMER_CUSTOM_COMPONENT_MANAGER = featureValue(
  FEATURES.GLIMMER_CUSTOM_COMPONENT_MANAGER
);
export const EMBER_GLIMMER_ANGLE_BRACKET_INVOCATION = featureValue(
  FEATURES.EMBER_GLIMMER_ANGLE_BRACKET_INVOCATION
);
export const GLIMMER_MODIFIER_MANAGER = featureValue(FEATURES.GLIMMER_MODIFIER_MANAGER);
export const EMBER_GLIMMER_ARRAY_HELPER = featureValue(FEATURES.EMBER_GLIMMER_ARRAY_HELPER);


text:
DELIMITER /
INSERT INTO KRIM_ROLE_PERM_ID_BS_S VALUES(NULL)
/
INSERT INTO KRIM_ROLE_PERM_T (ROLE_PERM_ID,ROLE_ID,PERM_ID,ACTV_IND,OBJ_ID,VER_NBR)
VALUES ((SELECT (MAX(ID)) FROM KRIM_ROLE_PERM_ID_BS_S),(SELECT ROLE_ID FROM KRIM_ROLE_T WHERE NMSPC_CD = 'KR-SYS' AND ROLE_NM = 'Technical Administrator'),(SELECT PERM_ID FROM KRIM_PERM_T WHERE NMSPC_CD = 'KUALI' AND NM = 'Add Message to Route Log'),'Y',UUID(),1)
/
DELIMITER ;


text:
DECLARE_bool(cfile_write_checksums);
DECLARE_bool(cfile_verify_checksums);
DECLARE_string(block_cache_type);
DECLARE_bool(force_block_cache_capacity);
DECLARE_int64(block_cache_capacity_mb);
DECLARE_string(nvm_cache_path);
DECLARE_bool(nvm_cache_simulate_allocation_failure);

METRIC_DECLARE_counter(block_cache_hits_caching);

METRIC_DECLARE_entity(server);

using kudu::fs::BlockManager;
using kudu::fs::CountingReadableBlock;
using kudu::fs::CreateCorruptBlock;
using kudu::fs::ReadableBlock;
using kudu::fs::WritableBlock;
using std::shared_ptr;
using std::string;
using std::unique_ptr;
using std::vector;
using strings::Substitute;

namespace kudu {
namespace cfile {

class TestCFile : public CFileTestBase {
 protected:
  template <class DataGeneratorType>
  void TestReadWriteFixedSizeTypes(EncodingType encoding) {
    BlockId block_id;
    DataGeneratorType generator;

    WriteTestFile(&generator, encoding, NO_COMPRESSION, 10000, SMALL_BLOCKSIZE, &block_id);

    unique_ptr<ReadableBlock> block;
    ASSERT_OK(fs_manager_->OpenBlock(block_id, &block));
    unique_ptr<CFileReader> reader;
    ASSERT_OK(CFileReader::Open(std::move(block), ReaderOptions(), &reader));

    BlockPointer ptr;
    unique_ptr<CFileIterator> iter;
    ASSERT_OK(reader->NewIterator(&iter, CFileReader::CACHE_BLOCK, nullptr));

    ASSERT_OK(iter->SeekToOrdinal(5000));
    ASSERT_EQ(5000u, iter->GetCurrentOrdinal());

    // Seek to last key exactly, should succeed.
    ASSERT_OK(iter->SeekToOrdinal(9999));
    ASSERT_EQ(9999u, iter->GetCurrentOrdinal());

    // Seek to after last key. Should result in not found.
    ASSERT_TRUE(iter->SeekToOrdinal(10000).IsNotFound());

    // Seek to start of file
    ASSERT_OK(iter->SeekToOrdinal(0));
    ASSERT_EQ(0u, iter->GetCurrentOrdinal());

    // Fetch all data.
    ScopedColumnBlock<DataGeneratorType::kDataType> out(10000);
    size_t n = 10000;
    SelectionVector sel(10000);
    ColumnMaterializationContext out_ctx = CreateNonDecoderEvalContext(&out, &sel);
    ASSERT_OK(iter->CopyNextValues(&n, &out_ctx));
    ASSERT_EQ(10000, n);

    DataGeneratorType data_generator_pre;

    for (int i = 0; i < 10000; i++) {
      if (out[i] != data_generator_pre.BuildTestValue(0,i)) {
        FAIL() << "mismatch at index " << i
               << " expected: " << data_generator_pre.BuildTestValue(0,i)
               << " got: " << out[i];
      }
      out[i] = 0;
    }

    // Fetch all data using small batches of only a few rows.
    // This should catch edge conditions like a batch lining up exactly
    // with the end of a block.
    unsigned int seed = time(nullptr);
    LOG(INFO) << "Using random seed: " << seed;
    srand(seed);
    ASSERT_OK(iter->SeekToOrdinal(0));
    size_t fetched = 0;
    while (fetched < 10000) {
      ColumnBlock advancing_block(out.type_info(), nullptr,
                                  out.data() + (fetched * out.stride()),
                                  out.nrows() - fetched, out.arena());
      ColumnMaterializationContext adv_ctx = CreateNonDecoderEvalContext(&advancing_block, &sel);
      ASSERT_TRUE(iter->HasNext());
      size_t batch_size = random() % 5 + 1;
      size_t n = batch_size;
      ASSERT_OK(iter->CopyNextValues(&n, &adv_ctx));
      ASSERT_LE(n, batch_size);
      fetched += n;
    }
    ASSERT_FALSE(iter->HasNext());

    DataGeneratorType data_generator_post;

    // Re-verify
    for (int i = 0; i < 10000; i++) {
      if (out[i] != data_generator_post.BuildTestValue(0,i)) {
        FAIL() << "mismatch at index " << i
               << " expected: " << data_generator_post.BuildTestValue(0,i)
               << " got: " << out[i];
      }
      out[i] = 0;
    }

    TimeReadFile(fs_manager_.get(), block_id, &n);
    ASSERT_EQ(10000, n);
  }

  template <class DataGeneratorType>
  void TimeSeekAndReadFileWithNulls(DataGeneratorType* generator,
                                    const BlockId& block_id, size_t num_entries) {
    unique_ptr<ReadableBlock> block;
    ASSERT_OK(fs_manager_->OpenBlock(block_id, &block));
    unique_ptr<CFileReader> reader;
    ASSERT_OK(CFileReader::Open(std::move(block), ReaderOptions(), &reader));
    ASSERT_EQ(DataGeneratorType::kDataType, reader->type_info()->type());

    unique_ptr<CFileIterator> iter;
    ASSERT_OK(reader->NewIterator(&iter, CFileReader::CACHE_BLOCK, nullptr));

    Arena arena(8192);
    ScopedColumnBlock<DataGeneratorType::kDataType> cb(10);

    SelectionVector sel(10);
    ColumnMaterializationContext ctx = CreateNonDecoderEvalContext(&cb, &sel);
    const int kNumLoops = AllowSlowTests() ? num_entries : 10;
    for (int loop = 0; loop < kNumLoops; loop++) {
      // Seek to a random point in the file,
      // or just try each entry as starting point if you're running SlowTests
      int target = AllowSlowTests() ? loop : (random() % (num_entries - 1));
      SCOPED_TRACE(target);
      ASSERT_OK(iter->SeekToOrdinal(target));
      ASSERT_TRUE(iter->HasNext());

      // Read and verify several ColumnBlocks from this point in the file.
      int read_offset = target;
      for (int block = 0; block < 3 && iter->HasNext(); block++) {
        size_t n = cb.nrows();
        ASSERT_OK_FAST(iter->CopyNextValues(&n, &ctx));
        ASSERT_EQ(n, std::min(num_entries - read_offset, cb.nrows()));

        // Verify that the block data is correct.
        generator->Build(read_offset, n);
        for (size_t j = 0; j < n; ++j) {
          bool expected_null = generator->TestValueShouldBeNull(read_offset + j);
          ASSERT_EQ(expected_null, cb.is_null(j));
          if (!expected_null) {
            ASSERT_EQ((*generator)[j], cb[j]);
          }
        }
        cb.arena()->Reset();
        read_offset += n;
      }
    }
  }

  template <class DataGeneratorType>
  void TestNullTypes(DataGeneratorType* generator, EncodingType encoding,
                     CompressionType compression) {
    BlockId block_id;
    WriteTestFile(generator, encoding, compression, 10000, SMALL_BLOCKSIZE, &block_id);

    size_t n;
    TimeReadFile(fs_manager_.get(), block_id, &n);
    ASSERT_EQ(n, 10000);

    generator->Reset();
    TimeSeekAndReadFileWithNulls(generator, block_id, n);
  }

  void TestReadWriteRawBlocks(CompressionType compression, int num_entries) {
    // Test Write
    unique_ptr<WritableBlock> sink;
    ASSERT_OK(fs_manager_->CreateNewBlock({}, &sink));
    BlockId id = sink->id();
    WriterOptions opts;
    opts.write_posidx = true;
    opts.write_validx = false;
    opts.storage_attributes.cfile_block_size = FLAGS_cfile_test_block_size;
    opts.storage_attributes.compression = compression;
    opts.storage_attributes.encoding = PLAIN_ENCODING;
    CFileWriter w(opts, GetTypeInfo(STRING), false, std::move(sink));
    ASSERT_OK(w.Start());
    for (uint32_t i = 0; i < num_entries; i++) {
      vector<Slice> slices;
      slices.emplace_back("Head");
      slices.emplace_back("Body");
      slices.emplace_back("Tail");
      slices.emplace_back(reinterpret_cast<uint8_t *>(&i), 4);
      ASSERT_OK(w.AppendRawBlock(slices, i, nullptr, Slice(), "raw-data"));
    }
    ASSERT_OK(w.Finish());

    // Test Read
    unique_ptr<ReadableBlock> source;
    ASSERT_OK(fs_manager_->OpenBlock(id, &source));
    unique_ptr<CFileReader> reader;
    ASSERT_OK(CFileReader::Open(std::move(source), ReaderOptions(), &reader));

    ASSERT_EQ(reader->footer().compression(), compression);
    if (FLAGS_cfile_write_checksums) {
      ASSERT_TRUE(reader->footer().incompatible_features() & IncompatibleFeatures::CHECKSUM);
    } else {
      ASSERT_FALSE(reader->footer().incompatible_features() & IncompatibleFeatures::CHECKSUM);
    }

    unique_ptr<IndexTreeIterator> iter;
    iter.reset(IndexTreeIterator::Create(nullptr, reader.get(), reader->posidx_root()));
    ASSERT_OK(iter->SeekToFirst());

    uint8_t data[16];
    Slice expected_data(data, 16);
    memcpy(data, "HeadBodyTail", 12);

    uint32_t count = 0;
    do {
      BlockHandle dblk_data;
      BlockPointer blk_ptr = iter->GetCurrentBlockPointer();
      ASSERT_OK(reader->ReadBlock(nullptr, blk_ptr, CFileReader::CACHE_BLOCK, &dblk_data));

      memcpy(data + 12, &count, 4);
      ASSERT_EQ(expected_data, dblk_data.data());

      count++;
    } while (iter->Next().ok());
    ASSERT_EQ(num_entries, count);
  }

  void TestReadWriteStrings(EncodingType encoding) {
    TestReadWriteStrings(encoding, [](size_t val) {
        return StringPrintf("hello %04zd", val);
      });
  }

  void TestReadWriteStrings(EncodingType encoding,
                            std::function<string(size_t)> formatter);

#ifdef NDEBUG
  void TestWrite100MFileStrings(EncodingType encoding) {
    BlockId block_id;
    LOG_TIMING(INFO, "writing 100M strings") {
      LOG(INFO) << "Starting writefile";
      StringDataGenerator<false> generator([](size_t idx) {
          char buf[kFastToBufferSize];
          FastHex64ToBuffer(idx, buf);
          return string(buf);
        });
      WriteTestFile(&generator, encoding, NO_COMPRESSION, 100000000, NO_FLAGS, &block_id);
      LOG(INFO) << "Done writing";
    }

    LOG_TIMING(INFO, "reading 100M strings") {
      LOG(INFO) << "Starting readfile";
      size_t n;
      TimeReadFile(fs_manager_.get(), block_id, &n);
      ASSERT_EQ(100000000, n);
      LOG(INFO) << "End readfile";
    }
  }
#endif

  void TestWriteDictEncodingLowCardinalityStrings(int64_t num_rows) {
    BlockId block_id;
    LOG_TIMING(INFO, Substitute("writing $0 strings with dupes", num_rows)) {
      LOG(INFO) << "Starting writefile";
      // The second parameter specify how many distinct strings are there
      DuplicateStringDataGenerator<false> generator("hello %zu", 256);
      WriteTestFile(&generator, DICT_ENCODING, NO_COMPRESSION, num_rows, NO_FLAGS, &block_id);
      LOG(INFO) << "Done writing";
    }

    LOG_TIMING(INFO, Substitute("reading $0 strings with dupes", num_rows)) {
      LOG(INFO) << "Starting readfile";
      size_t n;
      TimeReadFile(fs_manager_.get(), block_id, &n);
      ASSERT_EQ(num_rows, n);
      LOG(INFO) << "End readfile";
    }
  }

  Status CorruptAndReadBlock(const BlockId block_id, const uint64_t corrupt_offset,
                             uint8_t flip_bit) {
    BlockId new_id;
    RETURN_NOT_OK(CreateCorruptBlock(
        fs_manager_.get(), block_id, corrupt_offset, flip_bit, &new_id));

    // Open and read the corrupt block with the CFileReader
    unique_ptr<ReadableBlock> corrupt_source;
    RETURN_NOT_OK(fs_manager_->OpenBlock(new_id, &corrupt_source));
    unique_ptr<CFileReader> reader;
    ReaderOptions opts;
    const fs::IOContext io_context({ "corrupted-dummy-tablet" });
    opts.io_context = &io_context;
    RETURN_NOT_OK(CFileReader::Open(std::move(corrupt_source), std::move(opts), &reader));
    unique_ptr<IndexTreeIterator> iter;
    iter.reset(IndexTreeIterator::Create(&io_context, reader.get(), reader->posidx_root()));
    RETURN_NOT_OK(iter->SeekToFirst());

    do {
      BlockHandle dblk_data;
      BlockPointer blk_ptr = iter->GetCurrentBlockPointer();
      RETURN_NOT_OK(reader->ReadBlock(&io_context, blk_ptr,
          CFileReader::DONT_CACHE_BLOCK, &dblk_data));
    } while (iter->Next().ok());

    return Status::OK();
  }
};

// Subclass of TestCFile which is parameterized on the block cache type.
// Tests that use TEST_P(TestCFileBothCacheMemoryTypes, ...) will run twice --
// once for each cache memory type (DRAM, NVM).
class TestCFileBothCacheMemoryTypes :
    public TestCFile,
    public ::testing::WithParamInterface<Cache::MemoryType> {
 public:
  void SetUp() OVERRIDE {
    // The NVM cache can run using any directory as its path -- it doesn't have
    // a lot of practical use outside of an actual NVM device, but for testing
    // purposes, we'll point it at our test dir, unless otherwise specified.
    if (google::GetCommandLineFlagInfoOrDie("nvm_cache_path").is_default) {
      FLAGS_nvm_cache_path = GetTestPath("nvm-cache");
      ASSERT_OK(Env::Default()->CreateDir(FLAGS_nvm_cache_path));
    }
    switch (GetParam()) {
      case Cache::MemoryType::DRAM:
        FLAGS_block_cache_type = "DRAM";
        break;
      case Cache::MemoryType::NVM:
        FLAGS_block_cache_type = "NVM";
        break;
      default:
        LOG(FATAL) << "Unknown block cache type: '"
                   << static_cast<int16_t>(GetParam());
    }
    CFileTestBase::SetUp();
  }

  void TearDown() OVERRIDE {
    Singleton<BlockCache>::UnsafeReset();
  }
};

INSTANTIATE_TEST_CASE_P(CacheMemoryTypes, TestCFileBothCacheMemoryTypes,
                        ::testing::Values(Cache::MemoryType::DRAM,
                                          Cache::MemoryType::NVM));

template<DataType type>
void CopyOne(CFileIterator *it,
             typename TypeTraits<type>::cpp_type *ret,
             Arena *arena) {
  ColumnBlock cb(GetTypeInfo(type), nullptr, ret, 1, arena);
  SelectionVector sel(1);
  ColumnMaterializationContext ctx(0, nullptr, &cb, &sel);
  ctx.SetDecoderEvalNotSupported();
  size_t n = 1;
  ASSERT_OK(it->CopyNextValues(&n, &ctx));
  ASSERT_EQ(1, n);
}

#ifdef NDEBUG
// Only run the 100M entry tests in non-debug mode.
// They take way too long with debugging enabled.

TEST_P(TestCFileBothCacheMemoryTypes, TestWrite100MFileInts) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  BlockId block_id;
  LOG_TIMING(INFO, "writing 100m ints") {
    LOG(INFO) << "Starting writefile";
    Int32DataGenerator<false> generator;
    WriteTestFile(&generator, BIT_SHUFFLE, NO_COMPRESSION, 100000000, NO_FLAGS, &block_id);
    LOG(INFO) << "Done writing";
  }

  LOG_TIMING(INFO, "reading 100M ints") {
    LOG(INFO) << "Starting readfile";
    size_t n;
    TimeReadFile(fs_manager_.get(), block_id, &n);
    ASSERT_EQ(100000000, n);
    LOG(INFO) << "End readfile";
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestWrite100MFileNullableInts) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  BlockId block_id;
  LOG_TIMING(INFO, "writing 100m nullable ints") {
    LOG(INFO) << "Starting writefile";
    Int32DataGenerator<true> generator;
    WriteTestFile(&generator, PLAIN_ENCODING, NO_COMPRESSION, 100000000, NO_FLAGS, &block_id);
    LOG(INFO) << "Done writing";
  }

  LOG_TIMING(INFO, "reading 100M nullable ints") {
    LOG(INFO) << "Starting readfile";
    size_t n;
    TimeReadFile(fs_manager_.get(), block_id, &n);
    ASSERT_EQ(100000000, n);
    LOG(INFO) << "End readfile";
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestWrite100MFileStringsPrefixEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestWrite100MFileStrings(PREFIX_ENCODING);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestWrite100MUniqueStringsDictEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestWrite100MFileStrings(DICT_ENCODING);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestWrite100MLowCardinalityStringsDictEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestWriteDictEncodingLowCardinalityStrings(100 * 1e6);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestWrite100MFileStringsPlainEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestWrite100MFileStrings(PLAIN_ENCODING);
}

#endif

// Write and Read 1 million unique strings with dictionary encoding
TEST_P(TestCFileBothCacheMemoryTypes, TestWrite1MUniqueFileStringsDictEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  BlockId block_id;
  LOG_TIMING(INFO, "writing 1M unique strings") {
    LOG(INFO) << "Starting writefile";
    StringDataGenerator<false> generator("hello %zu");
    WriteTestFile(&generator, DICT_ENCODING, NO_COMPRESSION, 1000000, NO_FLAGS, &block_id);
    LOG(INFO) << "Done writing";
  }

  LOG_TIMING(INFO, "reading 1M strings") {
    LOG(INFO) << "Starting readfile";
    size_t n;
    TimeReadFile(fs_manager_.get(), block_id, &n);
    ASSERT_EQ(1000000, n);
    LOG(INFO) << "End readfile";
  }
}

// Write and Read 1 million strings, which contains duplicates with dictionary encoding
TEST_P(TestCFileBothCacheMemoryTypes, TestWrite1MLowCardinalityStringsDictEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestWriteDictEncodingLowCardinalityStrings(1000000);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteUInt32) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  for (auto enc : { PLAIN_ENCODING, RLE }) {
    TestReadWriteFixedSizeTypes<UInt32DataGenerator<false>>(enc);
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteInt32) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  for (auto enc : { PLAIN_ENCODING, RLE }) {
    TestReadWriteFixedSizeTypes<Int32DataGenerator<false>>(enc);
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteUInt64) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  for (auto enc : { PLAIN_ENCODING, RLE, BIT_SHUFFLE }) {
    TestReadWriteFixedSizeTypes<UInt64DataGenerator<false>>(enc);
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteInt64) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  for (auto enc : { PLAIN_ENCODING, RLE, BIT_SHUFFLE }) {
    TestReadWriteFixedSizeTypes<Int64DataGenerator<false>>(enc);
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteInt128) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestReadWriteFixedSizeTypes<Int128DataGenerator<false>>(PLAIN_ENCODING);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestFixedSizeReadWritePlainEncodingFloat) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestReadWriteFixedSizeTypes<FPDataGenerator<FLOAT, false>>(PLAIN_ENCODING);
}
TEST_P(TestCFileBothCacheMemoryTypes, TestFixedSizeReadWritePlainEncodingDouble) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestReadWriteFixedSizeTypes<FPDataGenerator<DOUBLE, false>>(PLAIN_ENCODING);
}

// Test for BitShuffle builder for UINT8, INT8, UINT16, INT16, UINT32, INT32,
// UINT64, INT64, INT128, FLOAT, DOUBLE
template <typename T>
class BitShuffleTest : public TestCFile {
  public:
    void TestBitShuffle() {
      TestReadWriteFixedSizeTypes<T>(BIT_SHUFFLE);
    }
};
typedef ::testing::Types<UInt8DataGenerator<false>,
                         Int8DataGenerator<false>,
                         UInt16DataGenerator<false>,
                         Int16DataGenerator<false>,
                         UInt32DataGenerator<false>,
                         Int32DataGenerator<false>,
                         UInt64DataGenerator<false>,
                         Int64DataGenerator<false>,
                         Int128DataGenerator<false>,
                         FPDataGenerator<FLOAT, false>,
                         FPDataGenerator<DOUBLE, false> > MyTypes;
TYPED_TEST_CASE(BitShuffleTest, MyTypes);
TYPED_TEST(BitShuffleTest, TestFixedSizeReadWriteBitShuffle) {
  this->TestBitShuffle();
}

void EncodeStringKey(const Schema &schema, const Slice& key,
                     unique_ptr<EncodedKey> *encoded_key) {
  EncodedKeyBuilder kb(&schema);
  kb.AddColumnKey(&key);
  encoded_key->reset(kb.BuildEncodedKey());
}

void TestCFile::TestReadWriteStrings(EncodingType encoding,
                                     std::function<string(size_t)> formatter) {
  Schema schema({ ColumnSchema("key", STRING) }, 1);

  const int nrows = 10000;
  BlockId block_id;
  StringDataGenerator<false> generator(formatter);
  WriteTestFile(&generator, encoding, NO_COMPRESSION, nrows,
                SMALL_BLOCKSIZE | WRITE_VALIDX, &block_id);

  unique_ptr<ReadableBlock> block;
  ASSERT_OK(fs_manager_->OpenBlock(block_id, &block));
  unique_ptr<CFileReader> reader;
  ASSERT_OK(CFileReader::Open(std::move(block), ReaderOptions(), &reader));

  rowid_t reader_nrows;
  ASSERT_OK(reader->CountRows(&reader_nrows));
  ASSERT_EQ(nrows, reader_nrows);

  BlockPointer ptr;

  unique_ptr<CFileIterator> iter;
  ASSERT_OK(reader->NewIterator(&iter, CFileReader::CACHE_BLOCK, nullptr));

  Arena arena(1024);

  ASSERT_OK(iter->SeekToOrdinal(5000));
  ASSERT_EQ(5000u, iter->GetCurrentOrdinal());
  Slice s;

  CopyOne<STRING>(iter.get(), &s, &arena);
  ASSERT_EQ(formatter(5000), s.ToString());

  // Seek to last key exactly, should succeed
  ASSERT_OK(iter->SeekToOrdinal(9999));
  ASSERT_EQ(9999u, iter->GetCurrentOrdinal());

  // Seek to after last key. Should result in not found.
  ASSERT_TRUE(iter->SeekToOrdinal(10000).IsNotFound());

  ////////
  // Now try some seeks by the value instead of position
  /////////

  unique_ptr<EncodedKey> encoded_key;
  bool exact;

  // Seek in between each key.
  // (seek to "hello 0000.5" through "hello 9999.5")
  string buf;
  for (int i = 1; i < 10000; i++) {
    arena.Reset();
    buf = formatter(i - 1);
    buf.append(".5");
    s = Slice(buf);
    EncodeStringKey(schema, s, &encoded_key);
    ASSERT_OK(iter->SeekAtOrAfter(*encoded_key, &exact));
    ASSERT_FALSE(exact);
    ASSERT_EQ(i, iter->GetCurrentOrdinal());
    CopyOne<STRING>(iter.get(), &s, &arena);
    ASSERT_EQ(formatter(i), s.ToString());
  }

  // Seek exactly to each key
  // (seek to "hello 0000" through "hello 9999")
  for (int i = 0; i < 9999; i++) {
    arena.Reset();
    buf = formatter(i);
    s = Slice(buf);
    EncodeStringKey(schema, s, &encoded_key);
    ASSERT_OK(iter->SeekAtOrAfter(*encoded_key, &exact));
    ASSERT_TRUE(exact);
    ASSERT_EQ(i, iter->GetCurrentOrdinal());
    Slice read_back;
    CopyOne<STRING>(iter.get(), &read_back, &arena);
    ASSERT_EQ(read_back.ToString(), s.ToString());
  }

  // after last entry
  // (seek to "hello 9999.x")
  buf = formatter(9999) + ".x";
  s = Slice(buf);
  EncodeStringKey(schema, s, &encoded_key);
  EXPECT_TRUE(iter->SeekAtOrAfter(*encoded_key, &exact).IsNotFound());

  // before first entry
  // (seek to "hello 000", which falls before "hello 0000")
  buf = formatter(0);
  buf.resize(buf.size() - 1);
  s = Slice(buf);
  EncodeStringKey(schema, s, &encoded_key);
  ASSERT_OK(iter->SeekAtOrAfter(*encoded_key, &exact));
  EXPECT_FALSE(exact);
  EXPECT_EQ(0u, iter->GetCurrentOrdinal());
  CopyOne<STRING>(iter.get(), &s, &arena);
  EXPECT_EQ(formatter(0), s.ToString());

  // Seek to start of file by ordinal
  ASSERT_OK(iter->SeekToFirst());
  ASSERT_EQ(0u, iter->GetCurrentOrdinal());
  CopyOne<STRING>(iter.get(), &s, &arena);
  ASSERT_EQ(formatter(0), s.ToString());

  // Reseek to start and fetch all data.
  // We fetch in 10 smaller chunks to avoid using too much RAM for the
  // case where the values are large.
  SelectionVector sel(10000);
  ASSERT_OK(iter->SeekToFirst());
  for (int i = 0; i < 10; i++) {
    ScopedColumnBlock<STRING> cb(10000);
    ColumnMaterializationContext cb_ctx = CreateNonDecoderEvalContext(&cb, &sel);
    size_t n = 1000;
    ASSERT_OK(iter->CopyNextValues(&n, &cb_ctx));
    ASSERT_EQ(1000, n);
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteStringsPrefixEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestReadWriteStrings(PREFIX_ENCODING);
}

// Read/Write test for dictionary encoded blocks
TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteStringsDictEncoding) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestReadWriteStrings(DICT_ENCODING);
}

// Regression test for properly handling cells that are larger
// than the index block and/or data block size.
//
// This test is disabled in TSAN because it's single-threaded anyway
// and runs extremely slowly with TSAN enabled.
#ifndef THREAD_SANITIZER
TEST_P(TestCFileBothCacheMemoryTypes, TestReadWriteLargeStrings) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  // Pad the values out to a length of ~65KB.
  // We use this method instead of just a longer sprintf format since
  // this is much more CPU-efficient (speeds up the test).
  auto formatter = [](size_t val) {
    string ret(66000, '0');
    StringAppendF(&ret, "%010zd", val);
    return ret;
  };
  TestReadWriteStrings(PLAIN_ENCODING, formatter);
  if (AllowSlowTests()) {
    TestReadWriteStrings(DICT_ENCODING, formatter);
    TestReadWriteStrings(PREFIX_ENCODING, formatter);
  }
}
#endif

// Test that metadata entries stored in the cfile are persisted.
TEST_P(TestCFileBothCacheMemoryTypes, TestMetadata) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  BlockId block_id;

  // Write the file.
  {
    unique_ptr<WritableBlock> sink;
    ASSERT_OK(fs_manager_->CreateNewBlock({}, &sink));
    block_id = sink->id();
    WriterOptions opts;
    CFileWriter w(opts, GetTypeInfo(INT32), false, std::move(sink));

    w.AddMetadataPair("key_in_header", "header value");
    ASSERT_OK(w.Start());

    uint32_t val = 1;
    ASSERT_OK(w.AppendEntries(&val, 1));

    w.AddMetadataPair("key_in_footer", "footer value");
    ASSERT_OK(w.Finish());
  }

  // Read the file and ensure metadata is present.
  {
    unique_ptr<ReadableBlock> source;
    ASSERT_OK(fs_manager_->OpenBlock(block_id, &source));
    unique_ptr<CFileReader> reader;
    ASSERT_OK(CFileReader::Open(std::move(source), ReaderOptions(), &reader));
    string val;
    ASSERT_TRUE(reader->GetMetadataEntry("key_in_header", &val));
    ASSERT_EQ(val, "header value");
    ASSERT_TRUE(reader->GetMetadataEntry("key_in_footer", &val));
    ASSERT_EQ(val, "footer value");
    ASSERT_FALSE(reader->GetMetadataEntry("not a key", &val));

    // Test that, even though we didn't specify an encoding or compression, the
    // resulting file has them explicitly set.
    ASSERT_EQ(BIT_SHUFFLE, reader->type_encoding_info()->encoding_type());
    ASSERT_EQ(NO_COMPRESSION, reader->footer().compression());
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestDefaultColumnIter) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  const int kNumItems = 64;
  uint8_t non_null_bitmap[BitmapSize(kNumItems)];
  uint32_t data[kNumItems];

  // Test Int Default Value
  uint32_t int_value = 15;
  DefaultColumnValueIterator iter(GetTypeInfo(UINT32), &int_value);
  ColumnBlock int_col(GetTypeInfo(UINT32), nullptr, data, kNumItems, nullptr);
  SelectionVector sel(kNumItems);
  ColumnMaterializationContext int_ctx = CreateNonDecoderEvalContext(&int_col, &sel);
  ASSERT_OK(iter.Scan(&int_ctx));
  for (size_t i = 0; i < int_col.nrows(); ++i) {
    ASSERT_EQ(int_value, *reinterpret_cast<const uint32_t *>(int_col.cell_ptr(i)));
  }

  // Test Int Nullable Default Value
  int_value = 321;
  DefaultColumnValueIterator nullable_iter(GetTypeInfo(UINT32), &int_value);
  ColumnBlock nullable_col(GetTypeInfo(UINT32), non_null_bitmap, data, kNumItems, nullptr);
  ColumnMaterializationContext nullable_ctx = CreateNonDecoderEvalContext(&nullable_col, &sel);
  ASSERT_OK(nullable_iter.Scan(&nullable_ctx));
  for (size_t i = 0; i < nullable_col.nrows(); ++i) {
    ASSERT_FALSE(nullable_col.is_null(i));
    ASSERT_EQ(int_value, *reinterpret_cast<const uint32_t *>(nullable_col.cell_ptr(i)));
  }

  // Test NULL Default Value
  DefaultColumnValueIterator null_iter(GetTypeInfo(UINT32),  nullptr);
  ColumnBlock null_col(GetTypeInfo(UINT32), non_null_bitmap, data, kNumItems, nullptr);
  ColumnMaterializationContext null_ctx = CreateNonDecoderEvalContext(&null_col, &sel);
  ASSERT_OK(null_iter.Scan(&null_ctx));
  for (size_t i = 0; i < null_col.nrows(); ++i) {
    ASSERT_TRUE(null_col.is_null(i));
  }

  // Test String Default Value
  Slice str_data[kNumItems];
  Slice str_value("Hello");
  Arena arena(32*1024);
  DefaultColumnValueIterator str_iter(GetTypeInfo(STRING), &str_value);
  ColumnBlock str_col(GetTypeInfo(STRING), nullptr, str_data, kNumItems, &arena);
  ColumnMaterializationContext str_ctx = CreateNonDecoderEvalContext(&str_col, &sel);
  ASSERT_OK(str_iter.Scan(&str_ctx));
  for (size_t i = 0; i < str_col.nrows(); ++i) {
    ASSERT_EQ(str_value, *reinterpret_cast<const Slice *>(str_col.cell_ptr(i)));
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestAppendRaw) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  TestReadWriteRawBlocks(NO_COMPRESSION, 1000);
  TestReadWriteRawBlocks(SNAPPY, 1000);
  TestReadWriteRawBlocks(LZ4, 1000);
  TestReadWriteRawBlocks(ZLIB, 1000);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestChecksumFlags) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  for (bool write_checksums : {false, true}) {
    for (bool verify_checksums : {false, true}) {
      FLAGS_cfile_write_checksums = write_checksums;
      FLAGS_cfile_verify_checksums = verify_checksums;
      TestReadWriteRawBlocks(NO_COMPRESSION, 1000);
      TestReadWriteRawBlocks(SNAPPY, 1000);
    }
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestDataCorruption) {
  RETURN_IF_NO_NVM_CACHE(GetParam());
  FLAGS_cfile_write_checksums = true;
  FLAGS_cfile_verify_checksums = true;

  // Write some data
  unique_ptr<WritableBlock> sink;
  ASSERT_OK(fs_manager_->CreateNewBlock({}, &sink));
  BlockId id = sink->id();
  WriterOptions opts;
  opts.write_posidx = true;
  opts.write_validx = false;
  opts.storage_attributes.cfile_block_size = FLAGS_cfile_test_block_size;
  opts.storage_attributes.encoding = PLAIN_ENCODING;
  CFileWriter w(opts, GetTypeInfo(STRING), false, std::move(sink));
  w.AddMetadataPair("header_key", "header_value");
  ASSERT_OK(w.Start());
  vector<Slice> slices;
  slices.emplace_back("HelloWorld");
  ASSERT_OK(w.AppendRawBlock(slices, 1, nullptr, Slice(), "raw-data"));
  ASSERT_OK(w.Finish());

  // Get the final size of the data
  unique_ptr<ReadableBlock> source;
  ASSERT_OK(fs_manager_->OpenBlock(id, &source));
  uint64_t file_size;
  ASSERT_OK(source->Size(&file_size));

  // Corrupt each bit and verify a corruption status is returned
  for (size_t i = 0; i < file_size; i++) {
    for (uint8_t flip = 0; flip < 8; flip++) {
      Status s = CorruptAndReadBlock(id, i, flip);
      ASSERT_TRUE(s.IsCorruption());
      ASSERT_STR_MATCHES(s.ToString(), "block [0-9]+");
    }
  }
}

TEST_P(TestCFileBothCacheMemoryTypes, TestNullInts) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  UInt32DataGenerator<true> generator;
  TestNullTypes(&generator, PLAIN_ENCODING, NO_COMPRESSION);
  TestNullTypes(&generator, PLAIN_ENCODING, LZ4);
  TestNullTypes(&generator, BIT_SHUFFLE, NO_COMPRESSION);
  TestNullTypes(&generator, BIT_SHUFFLE, LZ4);
  TestNullTypes(&generator, RLE, NO_COMPRESSION);
  TestNullTypes(&generator, RLE, LZ4);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestNullFloats) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  FPDataGenerator<FLOAT, true> generator;
  TestNullTypes(&generator, PLAIN_ENCODING, NO_COMPRESSION);
  TestNullTypes(&generator, BIT_SHUFFLE, NO_COMPRESSION);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestNullPrefixStrings) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  StringDataGenerator<true> generator("hello %zu");
  TestNullTypes(&generator, PLAIN_ENCODING, NO_COMPRESSION);
  TestNullTypes(&generator, PLAIN_ENCODING, LZ4);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestNullPlainStrings) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  StringDataGenerator<true> generator("hello %zu");
  TestNullTypes(&generator, PREFIX_ENCODING, NO_COMPRESSION);
  TestNullTypes(&generator, PREFIX_ENCODING, LZ4);
}

// Test for dictionary encoding
TEST_P(TestCFileBothCacheMemoryTypes, TestNullDictStrings) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  StringDataGenerator<true> generator("hello %zu");
  TestNullTypes(&generator, DICT_ENCODING, NO_COMPRESSION);
  TestNullTypes(&generator, DICT_ENCODING, LZ4);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestReleaseBlock) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  unique_ptr<WritableBlock> sink;
  ASSERT_OK(fs_manager_->CreateNewBlock({}, &sink));
  ASSERT_EQ(WritableBlock::CLEAN, sink->state());
  WriterOptions opts;
  CFileWriter w(opts, GetTypeInfo(STRING), false, std::move(sink));
  ASSERT_OK(w.Start());
  BlockManager* bm = fs_manager_->block_manager();
  unique_ptr<fs::BlockCreationTransaction> transaction = bm->NewCreationTransaction();
  ASSERT_OK(w.FinishAndReleaseBlock(transaction.get()));
  ASSERT_OK(transaction->CommitCreatedBlocks());
}

TEST_P(TestCFileBothCacheMemoryTypes, TestLazyInit) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  // Create a small test file.
  BlockId block_id;
  {
    const int nrows = 1000;
    StringDataGenerator<false> generator("hello %04d");
    WriteTestFile(&generator, PREFIX_ENCODING, NO_COMPRESSION, nrows,
                  SMALL_BLOCKSIZE | WRITE_VALIDX, &block_id);
  }

  shared_ptr<MemTracker> tracker = MemTracker::CreateTracker(-1, "test");
  int64_t initial_mem_usage = tracker->consumption();

  // Open it using a "counting" readable block.
  unique_ptr<ReadableBlock> block;
  ASSERT_OK(fs_manager_->OpenBlock(block_id, &block));
  size_t bytes_read = 0;
  unique_ptr<ReadableBlock> count_block(
      new CountingReadableBlock(std::move(block), &bytes_read));
  ASSERT_EQ(initial_mem_usage, tracker->consumption());

  // Lazily opening the cfile should not trigger any reads.
  ReaderOptions opts;
  opts.parent_mem_tracker = tracker;
  unique_ptr<CFileReader> reader;
  ASSERT_OK(CFileReader::OpenNoInit(std::move(count_block), opts, &reader));
  ASSERT_EQ(0, bytes_read);
  int64_t lazy_mem_usage = tracker->consumption();
  ASSERT_GT(lazy_mem_usage, initial_mem_usage);

  // But initializing it should (only the first time), and the reader's
  // memory usage should increase.
  ASSERT_OK(reader->Init(nullptr));
  ASSERT_GT(bytes_read, 0);
  size_t bytes_read_after_init = bytes_read;
  ASSERT_OK(reader->Init(nullptr));
  ASSERT_EQ(bytes_read_after_init, bytes_read);
  ASSERT_GT(tracker->consumption(), lazy_mem_usage);

  // And let's test non-lazy open for good measure; it should yield the
  // same number of bytes read.
  ASSERT_OK(fs_manager_->OpenBlock(block_id, &block));
  bytes_read = 0;
  count_block.reset(new CountingReadableBlock(std::move(block), &bytes_read));
  ASSERT_OK(CFileReader::Open(std::move(count_block), ReaderOptions(), &reader));
  ASSERT_EQ(bytes_read_after_init, bytes_read);
}

// Tests that the block cache keys used by CFileReaders are stable. That is,
// different reader instances operating on the same block should use the same
// block cache keys.
TEST_P(TestCFileBothCacheMemoryTypes, TestCacheKeysAreStable) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  // Set up block cache instrumentation.
  MetricRegistry registry;
  scoped_refptr<MetricEntity> entity(METRIC_ENTITY_server.Instantiate(&registry, "test_entity"));
  BlockCache* cache = BlockCache::GetSingleton();
  cache->StartInstrumentation(entity);

  // Create a small test file.
  BlockId block_id;
  {
    const int nrows = 1000;
    StringDataGenerator<false> generator("hello %04d");
    WriteTestFile(&generator, PREFIX_ENCODING, NO_COMPRESSION, nrows,
                  SMALL_BLOCKSIZE | WRITE_VALIDX, &block_id);
  }

  // Open and read from it twice, checking the block cache statistics.
  for (int i = 0; i < 2; i++) {
    unique_ptr<ReadableBlock> source;
    ASSERT_OK(fs_manager_->OpenBlock(block_id, &source));
    unique_ptr<CFileReader> reader;
    ASSERT_OK(CFileReader::Open(std::move(source), ReaderOptions(), &reader));

    unique_ptr<IndexTreeIterator> iter;
    iter.reset(IndexTreeIterator::Create(nullptr, reader.get(), reader->posidx_root()));
    ASSERT_OK(iter->SeekToFirst());

    BlockHandle bh;
    ASSERT_OK(reader->ReadBlock(nullptr, iter->GetCurrentBlockPointer(),
                                CFileReader::CACHE_BLOCK,
                                &bh));

    // The first time through, we miss in the seek and in the ReadBlock().
    // But the second time through, both are hits, because we've got the same
    // cache keys as before.
    ASSERT_EQ(i * 2, down_cast<Counter*>(
        entity->FindOrNull(METRIC_block_cache_hits_caching).get())->value());
  }
}

// Inject failures in nvm allocation and ensure that we can still read a file.
TEST_P(TestCFileBothCacheMemoryTypes, TestNvmAllocationFailure) {
  if (GetParam() != Cache::MemoryType::NVM) return;
  RETURN_IF_NO_NVM_CACHE(GetParam());

  FLAGS_nvm_cache_simulate_allocation_failure = true;
  TestReadWriteFixedSizeTypes<UInt32DataGenerator<false> >(PLAIN_ENCODING);
}

TEST_P(TestCFileBothCacheMemoryTypes, TestValidateBlockCacheCapacity) {
  RETURN_IF_NO_NVM_CACHE(GetParam());

  FLAGS_force_block_cache_capacity = false;
  FLAGS_block_cache_capacity_mb = 100000000; // very big number (100TB)
  switch (GetParam()) {
    case Cache::MemoryType::DRAM:
      ASSERT_FALSE(kudu::cfile::ValidateBlockCacheCapacity());
      break;
    case Cache::MemoryType::NVM:
      ASSERT_TRUE(kudu::cfile::ValidateBlockCacheCapacity());
      break;
    default:
      LOG(FATAL) << "Unknown block cache type: "
                 << static_cast<int16_t>(GetParam());
  }
}

class TestCFileDifferentCodecs : public TestCFile,
                                 public testing::WithParamInterface<CompressionType> {
};

INSTANTIATE_TEST_CASE_P(Codecs, TestCFileDifferentCodecs,
                        ::testing::Values(NO_COMPRESSION, SNAPPY, LZ4, ZLIB));

// Read/write a file with uncompressible data (random int32s)
TEST_P(TestCFileDifferentCodecs, TestUncompressible) {
  auto codec = GetParam();
  const size_t nrows = 1000000;
  BlockId block_id;
  size_t rdrows;

  // Generate a plain-encoded file with random (uncompressible) data.
  // This exercises the code path which short-circuits compression
  // when the codec is not able to be effective on the input data.
  {
    RandomInt32DataGenerator int_gen;
    WriteTestFile(&int_gen, PLAIN_ENCODING, codec, nrows,
                  NO_FLAGS, &block_id);
    TimeReadFile(fs_manager_.get(), block_id, &rdrows);
    ASSERT_EQ(nrows, rdrows);
  }
}

} // namespace cfile
} // namespace kudu


text:
package de.bluewhale.captcha.service;

import de.bluewhale.captcha.configs.AppConfig;
import org.junit.Assert;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * Functional and quality tests of the captcha validation
 *
 * @author Stefan Schubert
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(webEnvironment = WebEnvironment.DEFINED_PORT)
@ContextConfiguration(classes = AppConfig.class)
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class CheckerTest {

    final String BASE_API_URL = "http://localhost:8081/captcha/api";

    @Autowired
    public Environment env;

    @Value("${challenge.throttle.per.minute}")
    private int MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE;

    private long configuredTTL;

    @PostConstruct // env won't be initialized earlier
    public void lazyInit() {
        String ttl = env.getProperty("token.TTL");
        configuredTTL = Long.parseLong(ttl);
    }

    @Before
    public void resetThresholdMeter(){
        ChallengeRequestThrottle.resetAPICounter();
        ChallengeRequestThrottle.setThrottleThreshold(MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE);
    }

    /**
     * As other tests may overwrite the TTL you must ensure, that this is the first test running.
     *
     * @throws Exception
     */
    @Test
    public void testTTLInit() throws Exception {
        // Given

        // When
        long ttl = ValidationCache.getTTL();

        // Then
        Assert.assertTrue("application.properties from testclass not accessed?", configuredTTL > 0);
        Assert.assertTrue("application.properties from ValidationCache not accessed?", ttl > 0);
        Assert.assertEquals("Different than configured TTL?", configuredTTL, ttl);
    }

    @Test
    public void validateAnswer() throws Exception {
        // Given
        String validToken = "jhk7786";
        ValidationCache.registerToken(validToken);

        // When
        Assert.assertTrue("Register of token failed", ValidationCache.knowsCode(validToken));
        ValidationCache.invalidateCode(validToken);

        // Then
        Assert.assertFalse("Token was not consumed", ValidationCache.knowsCode(validToken));
    }

    @Test
    public void validateAnswerViaRestCall() throws Exception {
        // This test demonstrates the code a client needs to send a check request

        // Given (Simulate a previous taken challenge request)
        String checkURI = BASE_API_URL + "/check/{code}";
        String validToken = "GreenTea";
        ValidationCache.registerToken(validToken);

        // When (Client check)
        RestTemplate restTemplate = new RestTemplate();
        Map params = new HashMap<String, String>(1);
        params.put("code", validToken);
        final String checkresult = restTemplate.getForObject(checkURI, String.class, params);

        // Then (be Happy)
        Assert.assertEquals("Ouch - Token was not recognized", "Accepted", checkresult);
    }

    @Test
    public void testAPIThrottle() throws Exception {
        // Demonstrating effective throttleing
        long too_many_requests = 0;

        // Given
        String checkURI = BASE_API_URL + "/challenge/de";
        RestTemplate restTemplate = new RestTemplate();

        // When (Client check)
        while (too_many_requests <= MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE) {
            too_many_requests++;
            try {
                String forObject = restTemplate.getForObject(checkURI, String.class);
            } catch (RestClientException e) {
                // 429 - TOO MANY Request Exception
                Assert.assertTrue(e.getMessage().startsWith("429"));
            }
        }

        // Then (be Happy)
        Assert.assertEquals("Throttle ignition too early.", MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE + 1, too_many_requests);
    }

    @Test
    public void testResetAPIThrottle() throws Exception {
        // This test demonstrates releasing the throttle after a minute

        // Given
        int requestCount = MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE + 1;
        String checkURI = BASE_API_URL + "/challenge/de";
        RestTemplate restTemplate = new RestTemplate();
        ChallengeRequestThrottle.resetAPICounter();

        // When (exceeding the limit)
        for (int i = 0; i <= MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE * 2; i++) {
            try {
                restTemplate.getForObject(checkURI, String.class);
            } catch (RestClientException e) {
                // 429 - TOO MANY Request Exception
                Assert.assertTrue(e.getMessage().startsWith("429"));
                break;
            }
        }

        // Wait a good minute to relax the threshold counter
        Thread.sleep(1000 * 61);

        // Then (should work without throwing excepting)
        try {
            String object = restTemplate.getForObject(checkURI, String.class);
            Assert.assertTrue("Did not received a valid captcha.", object.length() > 10);
        } catch (Exception e) {
            Assert.fail("Last call should have been accepted.");
        }
    }

    @Test
    public void testThrottleMechanism() throws Exception {

        // Given
        String checkURI = BASE_API_URL + "/challenge/de";
        RestTemplate restTemplate = new RestTemplate();

        // When (exceeding the limit)
        for (int i = 0; i <= MAX_CHALLENGE_REQUEST_THROUGHPUT_PER_MINUTE * 2; i++) {
            try {
                restTemplate.getForObject(checkURI, String.class);
            } catch (RestClientException e) {
                // 429 - TOO MANY Request Exception
                Assert.assertTrue(e.getMessage().startsWith("429"));
                break;
            }
        }

        // Then
        Assert.assertFalse("Throttel not activated.", ChallengeRequestThrottle.requestAllowed());

    }

    @Test
    public void testCleanUpOfStaleTokens() throws Exception {
        // Given
        long old_ttl = ValidationCache.getTTL();
        ValidationCache.setTTL(100l); // 100ms
        String staleToken = "staleToken7z987";
        ValidationCache.registerToken(staleToken);

        // When register new one after expired TTL and Cachesize > Cleanup Threshold Policy
        for (int i = 0; i <= ValidationCache.CLEANUP_THRESHOLD; i++) {
            ValidationCache.registerToken(staleToken + i);
        }
        Thread.sleep(300l);
        String newToken = "nextToken";
        ValidationCache.registerToken(newToken);

        ValidationCache.setTTL(old_ttl); // restablish to avoid conflicts with other tests,
        // Because of the expired TTL  the stale one should be unknown by now
        Assert.assertFalse("Cache cleanup failed", ValidationCache.knowsCode(staleToken));
    }

}


text:
'use strict';

// Declare app level module which depends on views, and components
var app = angular.module('app', ['ngRoute', 'ngMaterial']);

app.run(['$rootScope', function($rootScope) {
    $rootScope.loggedIn = false;
}]);

app.config(['$locationProvider', '$routeProvider', function($locationProvider, $routeProvider) {
  $locationProvider.hashPrefix('!');

  $routeProvider.when('/home', {
    templateUrl: 'public/pages/home.html',
    resolve: {
      'check': function ($location, $rootScope) {
        if (!$rootScope.loggedIn) {
          $location.path('/login')
        }
      }
    }
  })
  .when('/login', {
    templateUrl: 'public/pages/login.html'
  }).otherwise({redirectTo: '/home'});

}]);

app.factory('appManager', ['$rootScope', function ($rootScope) {
  return {
    update: function () {
      console.log('Updating!');
      if (!$rootScope.$$phase) {
        $rootScope.$apply();
      }
    }
  };
}]);


text:
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package org.jitsi.sctp4j;

import java.io.*;

/**
 * Interface used by {@link SctpSocket} for sending network packets.
 *
 * FIXME: introduce offset and length parameters in order to be able to
 *        re-use single buffer instance
 *
 * @author Pawel Domas
 */
public interface NetworkLink
{
    /**
     * Callback triggered by <tt>SctpSocket</tt> whenever it wants to send some
     * network packet.
     * @param s source <tt>SctpSocket</tt> instance.
     * @param packet network packet buffer.
     *
     * @throws java.io.IOException in case of transport error.
     */
    public void onConnOut(final SctpSocket s, final byte[] packet)
        throws IOException;
}


text:
package com.bazaarvoice.emodb.event;

import com.google.inject.BindingAnnotation;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

/**
 * Guice binding annotation for identifying the host discovery the dedup event store should use.
 */
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD }) @Retention(RUNTIME)
public @interface EventStoreHostDiscovery {
}


text:
package org.camunda.bpm.extension.reactor.projectreactor.registry;

import org.camunda.bpm.extension.reactor.projectreactor.selector.ObjectSelector;
import org.camunda.bpm.extension.reactor.projectreactor.selector.Selector;
import org.camunda.bpm.extension.reactor.projectreactor.selector.Selectors;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import static org.junit.Assert.assertEquals;

public final class CachingRegistryTest
{

	private final AtomicInteger    cacheMisses     = new AtomicInteger();
	private final Registry<Object, Object> cachingRegistry = new CacheMissCountingCachingRegistry<Object>(cacheMisses);

	@Test
	public void registrationsWithTheSameSelectorAreOrderedByInsertionOrder() {
		String key = "selector";
		Selector<Object> selector = Selectors.$(key);

		this.cachingRegistry.register(selector, "echo");
		this.cachingRegistry.register(selector, "bravo");
		this.cachingRegistry.register(selector, "alpha");
		this.cachingRegistry.register(selector, "charlie");
		this.cachingRegistry.register(selector, "delta");

		Iterable<Registration<Object, ? extends Object>> registrations = this.cachingRegistry.select(key);
		List<Object> objects = new ArrayList<Object>();
		for (Registration<?, ? extends Object> registration : registrations) {
			if (null != registration) {
				objects.add(registration.getObject());
			}
		}

		assertEquals(Arrays.asList("echo", "bravo", "alpha", "charlie", "delta"), objects);
	}

	@Test
	@SuppressWarnings("unchecked")
	public void nonEmptyResultsAreCached() {
		String key = "/**/selector";
		Selector<Object> selector = Selectors.uri(key);

		this.cachingRegistry.register(selector, "alpha");

		this.cachingRegistry.select("/test/selector");
		this.cachingRegistry.select("/test/selector");

		assertEquals(1, this.cacheMisses.get());
	}

	@Test
	public void emptyResultsAreCached() {
		this.cachingRegistry.register(Selectors.$("another-key"), "alpha");
		this.cachingRegistry.select("key");
		this.cachingRegistry.select("key");

		assertEquals(1, this.cacheMisses.get());
	}

	@Test
	public void emptyResultsAreCachedWhenThereAreNoRegistrations() {
		this.cachingRegistry.select("key");
		this.cachingRegistry.select("key");

		assertEquals(1, this.cacheMisses.get());
	}

	@Test
	public void cacheIsRefreshedWhenANewRegistrationWithTheSameSelectorIsMade() {
		String key = "selector";
		Selector<Object> selector = Selectors.$(key);

		this.cachingRegistry.register(selector, "alpha");

		this.cachingRegistry.select(key);
		this.cachingRegistry.select(key);

		assertEquals(1, this.cacheMisses.get());

		this.cachingRegistry.register(selector, "bravo");

		this.cachingRegistry.select(key);
		this.cachingRegistry.select(key);

		assertEquals(2, this.cacheMisses.get());
	}

	//@Test
	public void cacheIsRefreshedWhenANewRegistrationWithADifferentSelectorIsMade() {
		String key1 = "selector";
		Selector<Object> selector1 = Selectors.$(key1);

		this.cachingRegistry.register(selector1, "alpha");

		this.cachingRegistry.select(key1);
		this.cachingRegistry.select(key1);

		assertEquals(1, this.cacheMisses.get());

		String key2 = "selector2";
		Selector<Object> selector2 = Selectors.$(key2);

		this.cachingRegistry.register(selector2, "bravo");

		this.cachingRegistry.select(key1);
		this.cachingRegistry.select(key1);

		assertEquals(2, this.cacheMisses.get());
	}

	//Issue : https://github.com/eventBus/eventBus/issues/237
	@Test
	public void invokeConsumersWithCustomSelector() {

		Subscription sub1 = new Subscription("client1", "test");
		Selector<Object> s1 = new MySelector(sub1);

		// consumer1
		this.cachingRegistry.register(s1, "pseudo-consumer-1");

		// notify1
		List<Registration<Object, ?>> registrations = this.cachingRegistry.select("test");

		assertEquals("number of consumers incorrect", 1, registrations.size());

		// consumer2
		this.cachingRegistry.register(s1, "pseudo-consumer-2");

		// consumer3
		Subscription sub2 = new Subscription("client2", "test");
		Selector<Object> s2 = new MySelector(sub2);
		this.cachingRegistry.register(s2, "pseudo-consumer-3");

		//consumer 4
		Subscription sub3 = new Subscription("client2", "test2");
		Selector<Object> s3 = new MySelector(sub3);
		this.cachingRegistry.register(s3, "pseudo-consumer-4");

		//prepopulate and add another consumer
		this.cachingRegistry.select("test2");
		this.cachingRegistry.register(s3, "pseudo-consumer-5");

		// notify2
		registrations = this.cachingRegistry.select("test");
		assertEquals("number of consumers incorrect", 3, registrations.size());

		registrations = this.cachingRegistry.select("test2");
		assertEquals("number of consumers incorrect", 2, registrations.size());

		/*for(Registration<?> registration : registrations){
			System.out.println (registration.getObject());
		}*/
	}

	private static final class CacheMissCountingCachingRegistry<T> extends CachingRegistry<Object, T> {
		private final AtomicInteger cacheMisses;

		public CacheMissCountingCachingRegistry(AtomicInteger cacheMisses) {
			super(true, true, null);
			this.cacheMisses = cacheMisses;
		}

		@Override
		protected void cacheMiss(Object key) {
			this.cacheMisses.incrementAndGet();
		}
	}

	public static class Subscription {
		public final String clientId;
		public final String topic;

		public Subscription(String clientId, String topic) {
			this.clientId = clientId;
			this.topic = topic;
		}
	}

	static class MySelector extends ObjectSelector<Object, Subscription>
  {
		public MySelector(Subscription subscription) {
			super(subscription);
		}

		@Override
		public boolean matches(Object key) {
			if(!(key instanceof String)) return false;
			return key.equals(getObject().topic);
		}
	}

}


text:
class Game < ActiveRecord::Base

  has_many :ownerships
  has_many :owners, through: :ownerships, source: :users
  has_many :game_ratings
  has_many :raters, through: :game_ratings, source: :users

  validates :title, :image, :thumbnail, :playing_time, :player_count_min, :player_count_max, presence: true

  def calculate_average_rating
    ratings_of_game = []
    self.game_ratings.each do |rating|
      ratings_of_game << rating.value
    end
    if ratings_of_game.length < 1
      @message = ["Be the first to rate this game"]
    else
      total = ratings_of_game.reduce(0, :+)
      average = total/ratings_of_game.length
    end
    return average
  end
end


text:
<?php

require_once '../../csrest_lists.php';

$wrap = new CS_REST_Lists('List ID', 'Your API Key');

$result = $wrap->get_custom_fields();

echo "Result of GET /api/v3/lists/{ID}/customfields\n
";
if($result->was_successful()) {
    echo "Got custom fields\n

";
    var_dump($result->response);
} else {
    echo 'Failed with code '.$result->http_status_code."\n

";
    var_dump($result->response);
}
echo '
';


text:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Kubility;
using System.Text;
using System.Threading;
using System.Net.Sockets;
using System.Net;
using System;
using System.IO;
using System.Runtime.InteropServices;
using ProtoBuf;

public class StructTest
{
	public int vid = 12;
	public string mm = "saasd";
	public long sda = 1234;
	public float sdags = 23.123f;
	public double mka = 45.1239f;
	public string value = "dashfalsfllfabksdbfkabsbfdffF";
	public ushort asda = 23;
	public char da = 'd';
	public List<int> val = new List<int> ();
}

public class Manager : MonoBehaviour
{

	KTcpClient client;
	KTcpServer server;

	bool isDone = false;
	float value = 0;
	HttpClient http;

	// Use this for initialization
	void Start ()
	{

		var buffer = new NetByteBuffer (1024);
		buffer += 1.452f;
		buffer += (uint)12;
		buffer += "sda1asd";

		var v1 = (float)buffer;
		var v2 = (int)buffer;
		var v3 = (string)buffer;

		LogMgr.Log("v1 = "+ v1.ToString() +" v2 ="+ v2.ToString() +" v3 ="+ v3.ToString());
		FactoryUtils.InitFactory();

		http = new HttpClient ();
		KThread.StartTask (ThreadListener);

		client = new KTcpClient ();
		client.Init ("127.0.0.1", 11000);

	}

	void ThreadListener ()
	{
		AsynchronousSocketListener.StartListening ();
	}

	static int couter =0;

	static int rc =0;

	void OnGUI ()
	{
		GUILayout.Label ("process = " + value.ToString (), GUILayout.Width (150));

		GUILayout.Space (10);
		GUILayout.Label ("state = " + isDone, GUILayout.Width (150));
		GUILayout.Label ("HttpDownload :" , GUILayout.Width (150));
		GUILayout.Label ("Socket : " , GUILayout.Width (150));
		GUILayout.Label("发送数据包数量 : "+couter +" 成功接受数据包数量 ="+ rc,GUILayout.Width (150));

		GUILayout.BeginHorizontal();
		if (GUILayout.Button ("Pause", GUILayout.Width (150))) {
			http.Pause ();
		}

		if(GUILayout.Button("Close Socket",GUILayout.Width(150)))
		{

			client.CloseConnect();
		}

		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();

		if (GUILayout.Button ("Resume", GUILayout.Width (150))) {
			http.Resume ();
		}

		if (GUILayout.Button ("Send Struct Message", GUILayout.Width (150)))
		{
			MessageInfo.MessageType = MessageDataType.Struct;
			HeartBeatStructData mess;
			mess.boolValue = true;
			mess.charValue = 2;
			mess.doubleValue = 2.3;
			mess.floatValue = 1.5f;
			mess.info = "i'm fine @ ~~~ 你好";
			mess.msgID = 102;
			var head = new StructMessageHead ();
			head.CMD = 102;
			head.bodyLen = (int)Marshal.SizeOf (mess);

			var message = StructMessage.Create (head, mess);

			client.Send<HeartBeatStructData> (message, delegate(HeartBeatStructData obj) {
				LogMgr.Log ("Struct Message Callback");
				rc++;
				KTool.Dump (obj);
			});

			couter++;
		}

		if (GUILayout.Button ("Send Json Message", GUILayout.Width (150)))
		{
			MessageInfo.MessageType = MessageDataType.Json;
			var test = new StructTest ();
			test.val.Add (123);
			test.val.Add (246);
			test.val.Add (45);
			var Jsonhead = new JsonMessageHead ();
			var jsonMessage = JsonMessage.Create<StructTest> (test, Jsonhead);
			client.Send<StructTest> (jsonMessage, delegate(StructTest obj) {
				LogMgr.Log ("Json Message Callback");
				rc++;
				KTool.Dump (obj);

			});
			couter++;

		}

		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();

		if (GUILayout.Button ("start", GUILayout.Width (150))) {

			string rootpath = Application.persistentDataPath;
			http.BeginDownLoadFileFlushToFile ("http://118.192.69.207:8083/monster.apk",rootpath + "/mons.assetbundle",
			delegate(byte[] arg1, float arg2, bool arg3)
			{
					value = arg2;
					isDone = arg3;
				LogMgr.Log ("FlushToFile Onprocess " + "  float  =" + arg2.ToString () + " isDone " + arg3.ToString ());
				});

			http.StartConnect ();

//			http.BeginDownLoadFileFlushToMemory ("http://118.192.69.207:8083/monster.apk",rootpath + "/mons.apk",
//			delegate(byte[] arg1, float arg2, bool arg3)
//			{
//				value = arg2;
//				isDone = arg3;
//				LogMgr.Log ("FlushToMemory Onprocess " + "  float  =" + arg2.ToString () + " isDone " + arg3.ToString ());
//			});
//
//			http.StartConnect ();
		}

		if(GUILayout.Button("Test Protobuf Serialize",GUILayout.Width(150)))
		{
			KprotobufMessage pmessage = new KprotobufMessage(){Id = 1235,data =new List<string>(new string[]{"hello","my name is cjsjy!"," welcome  中国!"})};
			string str= ParseUtils.ProtoBuf_Serialize<KprotobufMessage>(pmessage);
			LogMgr.Log("protobuf : "+str );
			var result = ParseUtils.ProtoBuf_Deserialize<KprotobufMessage>(str);

			LogMgr.Log(result);

		}

		if(GUILayout.Button("Send Protobuf ",GUILayout.Width(150)))
		{
			MessageInfo.MessageType = MessageDataType.ProtoBuf;
			KprotobufMessage pmessage = new KprotobufMessage(){Id = 1235,data =new List<string>(new string[]{"hello","my name is cjsjy!"," welcome  中国!"})};

			var Protbufhead = new ProtobufMessageHead ();
			var ProbufMessage = ProtobufMessage.Create<KprotobufMessage>(Protbufhead,pmessage);

			client.Send<KprotobufMessage>(ProbufMessage,delegate(KprotobufMessage obj) {
				LogMgr.Log ("Protobuf Message Callback");
				rc++;
				LogMgr.Log(obj);
			});

			couter++;

		}

		GUILayout.EndHorizontal();

		GUILayout.BeginHorizontal();

		GUILayout.Label("",GUILayout.Width(150));

		if(GUILayout.Button("Reconnect Socket",GUILayout.Width(150)))
		{

			client.Reconnect();

		}

		GUILayout.EndHorizontal();
	}
	public class ProtobufHelper
	{

	}

	void OnApplicationQuit ()
	{
		LogMgr.Log ("OnApplicationQuit   = ");
		AsynchronousSocketListener.allDone.Set ();

		AsynchronousSocketListener.stop = true;

		if (client != null) {
			client.Close ();
		}

		if (http != null) {
			http.Close ();
		}

		KObject.Dump ();
	}

}