text:
from django.core.exceptions import ImproperlyConfigured

from storages.backends.s3boto import S3BotoStorage, S3BotoStorageFile, setting

try:
    from boto.gs.connection import GSConnection, SubdomainCallingFormat
    from boto.exception import GSResponseError
    from boto.gs.key import Key as GSKey
except ImportError:
    raise ImproperlyConfigured("Could not load Boto's Google Storage bindings.\n"
                               "See https://github.com/boto/boto")

class GSBotoStorageFile(S3BotoStorageFile):
    buffer_size = setting('GS_FILE_BUFFER_SIZE', 5242880)

class GSBotoStorage(S3BotoStorage):
    connection_class = GSConnection
    connection_response_error = GSResponseError
    file_class = GSBotoStorageFile
    key_class = GSKey

    access_key = setting('GS_ACCESS_KEY_ID')
    secret_key = setting('GS_SECRET_ACCESS_KEY')
    file_overwrite = setting('GS_FILE_OVERWRITE', True)
    headers = setting('GS_HEADERS', {})
    storage_bucket_name = setting('GS_BUCKET_NAME', None)
    auto_create_bucket = setting('GS_AUTO_CREATE_BUCKET', False)
    default_acl = setting('GS_DEFAULT_ACL', 'public-read')
    bucket_acl = setting('GS_BUCKET_ACL', default_acl)
    querystring_auth = setting('GS_QUERYSTRING_AUTH', True)
    querystring_expire = setting('GS_QUERYSTRING_EXPIRE', 3600)
    reduced_redundancy = setting('GS_REDUCED_REDUNDANCY', False)
    location = setting('GS_LOCATION', '')
    custom_domain = setting('GS_CUSTOM_DOMAIN')
    calling_format = setting('GS_CALLING_FORMAT', SubdomainCallingFormat())
    secure_urls = setting('GS_SECURE_URLS', True)
    file_name_charset = setting('GS_FILE_NAME_CHARSET', 'utf-8')
    is_gzipped = setting('GS_IS_GZIPPED', False)
    preload_metadata = setting('GS_PRELOAD_METADATA', False)
    gzip_content_types = setting('GS_GZIP_CONTENT_TYPES', (
        'text/css',
        'application/javascript',
        'application/x-javascript',
    ))
    url_protocol = setting('GS_URL_PROTOCOL', 'http:')


text:
#include "tensorflow/core/kernels/data/range_dataset_op.h"

#include "tensorflow/core/kernels/data/dataset_test_base.h"

namespace tensorflow {
namespace data {
namespace {

constexpr char kNodeName[] = "range_dataset";

class RangeDatasetOpTest : public DatasetOpsTestBase {
 protected:
  // Creates a new RangeDataset op kernel context.
  Status CreateRangeDatasetContext(
      OpKernel* const range_kernel,
      gtl::InlinedVector<TensorValue, 4>* const inputs,
      std::unique_ptr<OpKernelContext>* range_context) {
    TF_RETURN_IF_ERROR(CheckOpKernelInput(*range_kernel, *inputs));
    TF_RETURN_IF_ERROR(
        CreateOpKernelContext(range_kernel, inputs, range_context));
    return Status::OK();
  }
};

struct TestCase {
  int64 start;
  int64 stop;
  int64 step;
  std::vector<Tensor> expected_outputs;
  DataTypeVector expected_output_dtypes;
  std::vector<PartialTensorShape> expected_output_shapes;
  int64 expected_cardinality;
  std::vector<int> breakpoints;
};

TestCase PositiveStepTestCase() {
  return {/*start*/ 0,
          /*stop*/ 10,
          /*step*/ 3,
          /*expected_outputs*/
          {DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {0}),
           DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {3}),
           DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {6}),
           DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {9})},
          /*expected_output_dtypes*/ {DT_INT64},
          /*expected_output_shapes*/ {PartialTensorShape({})},
          /*expected_cardinality*/ 4,
          /*breakpoints*/ {0, 1, 4}};
}

TestCase NegativeStepTestCase() {
  return {/*start*/ 10,
          /*stop*/ 0,
          /*step*/ -3,
          /*expected_outputs*/
          {DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {10}),
           DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {7}),
           DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {4}),
           DatasetOpsTestBase::CreateTensor<int64>(TensorShape({}), {1})},
          /*expected_output_dtypes*/ {DT_INT64},
          /*expected_output_shapes*/ {PartialTensorShape({})},
          /*expected_cardinality*/ 4,
          /*breakpoints*/ {0, 1, 4}};
}

TestCase ZeroStepTestCase() {
  return {/*start*/ 0,
          /*stop*/ 10,
          /*step*/ 0,
          /*expected_outputs*/ {},
          /*expected_output_dtypes*/ {},
          /*expected_output_shapes*/ {},
          /*expected_cardinality*/ 0,
          /*breakpoints*/ {}};
}

class ParameterizedRangeDatasetOpTest
    : public RangeDatasetOpTest,
      public ::testing::WithParamInterface<TestCase> {};

TEST_P(ParameterizedRangeDatasetOpTest, GetNext) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = GetParam();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  std::unique_ptr<IteratorContext> iterator_context;
  TF_ASSERT_OK(
      CreateIteratorContext(range_dataset_context.get(), &iterator_context));
  std::unique_ptr<IteratorBase> iterator;
  TF_ASSERT_OK(range_dataset->MakeIterator(iterator_context.get(), "Iterator",
                                           &iterator));

  bool end_of_sequence = false;
  auto expected_outputs_it = test_case.expected_outputs.begin();
  std::vector<Tensor> out_tensors;
  while (!end_of_sequence) {
    TF_EXPECT_OK(iterator->GetNext(iterator_context.get(), &out_tensors,
                                   &end_of_sequence));
    if (!end_of_sequence) {
      EXPECT_NE(expected_outputs_it, test_case.expected_outputs.end());
      TF_EXPECT_OK(ExpectEqual(out_tensors.back(), *expected_outputs_it));
      expected_outputs_it++;
    }
  }
  EXPECT_EQ(expected_outputs_it, test_case.expected_outputs.end());
}

TEST_F(RangeDatasetOpTest, ZeroStep) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = ZeroStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  EXPECT_EQ(CreateDataset(range_dataset_kernel.get(),
                          range_dataset_context.get(), &range_dataset)
                .code(),
            tensorflow::error::INVALID_ARGUMENT);
}

TEST_F(RangeDatasetOpTest, DatasetNodeName) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  EXPECT_EQ(range_dataset->node_name(), kNodeName);
}

TEST_F(RangeDatasetOpTest, DatasetTypeString) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  EXPECT_EQ(range_dataset->type_string(),
            name_utils::OpName(RangeDatasetOp::kDatasetType));
}

TEST_F(RangeDatasetOpTest, DatasetOutputDtypes) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  TF_EXPECT_OK(VerifyTypesMatch(range_dataset->output_dtypes(),
                                test_case.expected_output_dtypes));
}

TEST_F(RangeDatasetOpTest, DatasetOutputShapes) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  TF_EXPECT_OK(VerifyShapesCompatible(range_dataset->output_shapes(),
                                      test_case.expected_output_shapes));
}

TEST_P(ParameterizedRangeDatasetOpTest, Cardinality) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = GetParam();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  EXPECT_EQ(range_dataset->Cardinality(), test_case.expected_cardinality);
}

TEST_F(RangeDatasetOpTest, DatasetSave) {
  int64 thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  std::unique_ptr<SerializationContext> serialization_context;
  TF_ASSERT_OK(CreateSerializationContext(&serialization_context));

  VariantTensorData data;
  VariantTensorDataWriter writer(&data);
  TF_ASSERT_OK(range_dataset->Save(serialization_context.get(), &writer));
  TF_ASSERT_OK(writer.Flush());
}

TEST_F(RangeDatasetOpTest, IteratorOutputDtypes) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  std::unique_ptr<IteratorContext> iterator_context;
  TF_ASSERT_OK(
      CreateIteratorContext(range_dataset_context.get(), &iterator_context));
  std::unique_ptr<IteratorBase> iterator;
  TF_ASSERT_OK(range_dataset->MakeIterator(iterator_context.get(), "Iterator",
                                           &iterator));

  TF_EXPECT_OK(VerifyTypesMatch(iterator->output_dtypes(),
                                test_case.expected_output_dtypes));
}

TEST_F(RangeDatasetOpTest, IteratorOutputShapes) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  std::unique_ptr<IteratorContext> iterator_context;
  TF_ASSERT_OK(
      CreateIteratorContext(range_dataset_context.get(), &iterator_context));
  std::unique_ptr<IteratorBase> iterator;
  TF_ASSERT_OK(range_dataset->MakeIterator(iterator_context.get(), "Iterator",
                                           &iterator));

  TF_EXPECT_OK(VerifyShapesCompatible(iterator->output_shapes(),
                                      test_case.expected_output_shapes));
}

TEST_F(RangeDatasetOpTest, IteratorOutputPrefix) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = PositiveStepTestCase();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  std::unique_ptr<IteratorContext> iterator_context;
  TF_ASSERT_OK(
      CreateIteratorContext(range_dataset_context.get(), &iterator_context));
  std::unique_ptr<IteratorBase> iterator;
  TF_ASSERT_OK(range_dataset->MakeIterator(iterator_context.get(), "Iterator",
                                           &iterator));

  EXPECT_EQ(iterator->prefix(), name_utils::IteratorPrefix(
                                    RangeDatasetOp::kDatasetType, "Iterator"));
}

TEST_P(ParameterizedRangeDatasetOpTest, Roundtrip) {
  int thread_num = 2, cpu_num = 2;
  TF_ASSERT_OK(InitThreadPool(thread_num));
  TF_ASSERT_OK(InitFunctionLibraryRuntime({}, cpu_num));

  TestCase test_case = GetParam();
  Tensor start = CreateTensor<int64>(TensorShape({}), {test_case.start});
  Tensor stop = CreateTensor<int64>(TensorShape({}), {test_case.stop});
  Tensor step = CreateTensor<int64>(TensorShape({}), {test_case.step});
  gtl::InlinedVector<TensorValue, 4> inputs(
      {TensorValue(&start), TensorValue(&stop), TensorValue(&step)});

  std::unique_ptr<OpKernel> range_dataset_kernel;
  TF_ASSERT_OK(
      CreateRangeDatasetOpKernel<int64>(kNodeName, &range_dataset_kernel));
  std::unique_ptr<OpKernelContext> range_dataset_context;
  TF_ASSERT_OK(CreateRangeDatasetContext(range_dataset_kernel.get(), &inputs,
                                         &range_dataset_context));
  DatasetBase* range_dataset;
  TF_ASSERT_OK(CreateDataset(range_dataset_kernel.get(),
                             range_dataset_context.get(), &range_dataset));
  core::ScopedUnref scoped_unref(range_dataset);

  std::unique_ptr<IteratorContext> iterator_context;
  TF_ASSERT_OK(
      CreateIteratorContext(range_dataset_context.get(), &iterator_context));
  std::unique_ptr<IteratorBase> iterator;
  TF_ASSERT_OK(range_dataset->MakeIterator(iterator_context.get(), "Iterator",
                                           &iterator));

  std::unique_ptr<SerializationContext> serialization_ctx;
  TF_ASSERT_OK(CreateSerializationContext(&serialization_ctx));
  bool end_of_sequence = false;
  std::vector<Tensor> out_tensors;
  int cur_iteration = 0;
  auto expected_outputs_it = test_case.expected_outputs.begin();
  const std::vector<int>& breakpoints = test_case.breakpoints;
  for (int breakpoint : breakpoints) {
    VariantTensorData data;
    VariantTensorDataWriter writer(&data);
    TF_EXPECT_OK(iterator->Save(serialization_ctx.get(), &writer));
    TF_EXPECT_OK(writer.Flush());
    VariantTensorDataReader reader(&data);
    TF_EXPECT_OK(RestoreIterator(iterator_context.get(), &reader, "Iterator",
                                 *range_dataset, &iterator));

    while (cur_iteration <= breakpoint) {
      TF_EXPECT_OK(iterator->GetNext(iterator_context.get(), &out_tensors,
                                     &end_of_sequence));
      if (!end_of_sequence) {
        EXPECT_NE(expected_outputs_it, test_case.expected_outputs.end());
        TF_EXPECT_OK(ExpectEqual(out_tensors.back(), *expected_outputs_it));
        expected_outputs_it++;
      }
      cur_iteration++;
    }

    if (breakpoint >= test_case.expected_cardinality) {
      EXPECT_TRUE(end_of_sequence);
      EXPECT_EQ(expected_outputs_it, test_case.expected_outputs.end());
    } else {
      EXPECT_FALSE(end_of_sequence);
    }
  }
}

INSTANTIATE_TEST_SUITE_P(
    RangeDatasetOpTest, ParameterizedRangeDatasetOpTest,
    ::testing::ValuesIn(std::vector<TestCase>({PositiveStepTestCase(),
                                               NegativeStepTestCase()})));

}  // namespace
}  // namespace data
}  // namespace tensorflow

redpajama_set_name: RedPajamaGithub


text:
DataWriter::DataWriter(const config::FeatureExtractorConfig config) :
  dataset_size_(0) {
  file_ = H5Fcreate(config.output_file().c_str(), H5F_ACC_TRUNC, H5P_DEFAULT,
                    H5P_DEFAULT);
  for (const config::FeatureStreamConfig& feature : config.feature()) {
    stream& s = streams_[feature.layer()];
    s.average_batches = feature.average_batches();
    s.average_online = feature.average_online();
    s.counter = 0;
    s.current_row = 0;
    s.consumed = 0;
  }
}

DataWriter::~DataWriter(){
  for(auto& it : streams_) {
    H5Dclose(it.second.dataset);
  }
  H5Fclose(file_);
}

void DataWriter::SetDataSetSize(int dataset_size) {
  dataset_size_ = dataset_size;
}

void DataWriter::SetNumDims(const string& name, const int num_dims) {
  stream& s = streams_[name];
  s.num_dims = num_dims;
  hsize_t dimsf[2];
  dimsf[0] = dataset_size_/(s.average_online * s.average_batches);
  dimsf[1] = num_dims;
  cout << "Adding Dataspace " << name << " of size " << dimsf[0]
       << " " << dimsf[1] << endl;
  hid_t dataspace = H5Screate_simple(2, dimsf, NULL);
  s.dataset = H5Dcreate(file_, name.c_str(), H5T_NATIVE_FLOAT, dataspace,
                        H5P_DEFAULT, H5P_DEFAULT, H5P_DEFAULT);
  if (s.dataset < 0) {
    cerr << "Could not crete dataset for writing." << endl;
    exit(1);
  }
  H5Sclose(dataspace);
}

// This may not necessarily write to disk, but hold it in a buffer.
void DataWriter::WriteHDF5(Matrix& m, const string& dataset, int numcases, bool transpose) {
  float* data;
  Matrix m_t;
  if (transpose) {
    Matrix::GetTemp(m.GetCols(), m.GetRows(), m_t);
    m.CopyTransposeBig(m_t);
    m_t.CopyToHost();
    data = m_t.GetHostData();
  } else {
    m.CopyToHost();
    data = m.GetHostData();
  }
  stream& s = streams_[dataset];

  hsize_t dimsf[2], start[2];
  dimsf[0] = numcases;
  dimsf[1] = s.num_dims;
  start[0] = s.current_row;
  start[1] = 0;
  hid_t f_dataspace   = H5Dget_space(s.dataset);
  if (f_dataspace < 0) {
    cerr << "Error in datawriter: Could not get file dataspace." << endl;
    exit(1);
  }
  hid_t mem_dataspace = H5Screate_simple(2, dimsf, NULL);
  if (mem_dataspace < 0) {
    cerr << "Error in datawriter: Could not create memory dataspace." << endl;
    exit(1);
  }
  if (H5Sselect_hyperslab(f_dataspace, H5S_SELECT_SET, start, NULL, dimsf, NULL) < 0) {
    cerr << "Error in datawriter: Could not select hyperslab for writing." << endl;
    exit(1);
  }
  if (H5Dwrite(s.dataset, H5T_NATIVE_FLOAT, mem_dataspace, f_dataspace, H5P_DEFAULT, data) < 0) {
    cerr << "Error in datawriter: Could not write to hdf5 file." << endl;
    exit(1);
  }
  if (H5Sclose(mem_dataspace) < 0) {
    cerr << "Error in datawriter: Could not close memory dataspace" << endl;
    exit(1);
  }
  if (H5Sclose(f_dataspace) < 0) {
    cerr << "Error in datawriter: Could not close file dataspace" << endl;
    exit(1);
  }
  s.current_row += numcases;
}

void DataWriter::WriteHDF5SeqBuf(Matrix& m, const string& dataset, int numcases) {
  stream& s = streams_[dataset];
  if (s.average_online == 1) {
    WriteHDF5(m, dataset, numcases, true);
  } else {
    Matrix m_t;
    Matrix::GetTemp(m.GetCols(), m.GetRows(), m_t);
    m.CopyTransposeBig(m_t);

    Matrix& buf = s.seq_buf;
    if (buf.GetNumEls() == 0) {
      buf.AllocateGPUMemory(m_t.GetRows(), 1);
      buf.Set(0);
    }
    int numcases = m_t.GetCols();
    int end = 0, start = 0;

    while(start < numcases && s.current_row * s.average_online < dataset_size_) {
      Matrix slice;
      end = start + s.average_online - s.consumed;
      if (end > numcases) end = numcases;
      m_t.GetSlice(slice, start, end);
      slice.SumCols(buf, 1, 1.0 / s.average_online);
      s.consumed += end - start;
      if (s.consumed == s.average_online) {
        WriteHDF5(buf, dataset, 1, false);
        buf.Set(0);
        s.consumed = 0;
      }
      start = end;
    }
  }
}

void DataWriter::Write(vector<Layer*>& layers, int numcases) {
  for (Layer* l: layers) {
    Matrix& m = l->GetState();
    const string& dataset = l->GetName();
    stream& s = streams_[dataset];
    if(s.average_batches == 1) {
      WriteHDF5SeqBuf(m, dataset, numcases);
    } else {
      Matrix& buf = s.buf;
      if (buf.GetNumEls() == 0) {
        buf.AllocateGPUMemory(m.GetRows(), m.GetCols());
        buf.Set(0);
      }
      buf.Add(m);
      if(++s.counter == s.average_batches) {
        buf.Divide(s.average_batches);
        WriteHDF5SeqBuf(buf, dataset, numcases);
        buf.Set(0);
        s.counter = 0;
      }
    }
  }
}


text:
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include <QEventLoop>
#include <QSignalSpy>
#include "QtStageWebView.h"


text:
(function() {

var VERSION = '0.0.10';

if (Ember.libraries) {
  Ember.libraries.register('Ember Model', VERSION);
}

})();

(function() {

function mustImplement(message) {
  var fn = function() {
    var className = this.constructor.toString();

    throw new Error(message.replace('{{className}}', className));
  };
  fn.isUnimplemented = true;
  return fn;
}

Ember.Adapter = Ember.Object.extend({
  find: mustImplement('{{className}} must implement find'),
  findQuery: mustImplement('{{className}} must implement findQuery'),
  findMany: mustImplement('{{className}} must implement findMany'),
  findAll: mustImplement('{{className}} must implement findAll'),
  createRecord: mustImplement('{{className}} must implement createRecord'),
  saveRecord: mustImplement('{{className}} must implement saveRecord'),
  deleteRecord: mustImplement('{{className}} must implement deleteRecord'),

  load: function(record, id, data) {
    record.load(id, data);
  }
});

})();

(function() {

var get = Ember.get;

Ember.FixtureAdapter = Ember.Adapter.extend({
  _findData: function(klass, id) {
    var fixtures = klass.FIXTURES,
        idAsString = id.toString(),
        primaryKey = get(klass, 'primaryKey'),
        data = Ember.A(fixtures).find(function(el) { return (el[primaryKey]).toString() === idAsString; });

    return data;
  },

  find: function(record, id) {
    var data = this._findData(record.constructor, id);

    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(this, function() {
        Ember.run(record, record.load, id, data);
        resolve(record);
      }, 0);
    });
  },

  findMany: function(klass, records, ids) {
    var fixtures = klass.FIXTURES,
        requestedData = [];

    for (var i = 0, l = ids.length; i < l; i++) {
      requestedData.push(this._findData(klass, ids[i]));
    }

    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(this, function() {
        Ember.run(records, records.load, klass, requestedData);
        resolve(records);
      }, 0);
    });
  },

  findAll: function(klass, records) {
    var fixtures = klass.FIXTURES;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(this, function() {
        Ember.run(records, records.load, klass, fixtures);
        resolve(records);
      }, 0);
    });
  },

  createRecord: function(record) {
    var klass = record.constructor,
        fixtures = klass.FIXTURES;

    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(this, function() {
        fixtures.push(klass.findFromCacheOrLoad(record.toJSON()));
        record.didCreateRecord();
        resolve(record);
      }, 0);
    });
  },

  saveRecord: function(record) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(this, function() {
        record.didSaveRecord();
        resolve(record);
      }, 0);
    });
  },

  deleteRecord: function(record) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.run.later(this, function() {
        record.didDeleteRecord();
        resolve(record);
      }, 0);
    });
  }
});

})();

(function() {

var get = Ember.get,
    set = Ember.set;

Ember.RecordArray = Ember.ArrayProxy.extend(Ember.Evented, {
  isLoaded: false,
  isLoading: Ember.computed.not('isLoaded'),

  load: function(klass, data) {
    set(this, 'content', this.materializeData(klass, data));
    this.notifyLoaded();
  },

  loadForFindMany: function(klass) {
    var content = get(this, '_ids').map(function(id) { return klass.cachedRecordForId(id); });
    set(this, 'content', Ember.A(content));
    this.notifyLoaded();
  },

  notifyLoaded: function() {
    set(this, 'isLoaded', true);
    this.trigger('didLoad');
  },

  materializeData: function(klass, data) {
    return Ember.A(data.map(function(el) {
      return klass.findFromCacheOrLoad(el); // FIXME
    }));
  },

  reload: function() {
    var modelClass = this.get('modelClass'),
        self = this,
        promises;

    set(this, 'isLoaded', false);
    if (modelClass._findAllRecordArray === this) {
      return modelClass.adapter.findAll(modelClass, this);
    } else if (this._query) {
      return modelClass.adapter.findQuery(modelClass, this, this._query);
    } else {
      promises = this.map(function(record) {
        return record.reload();
      });
      return Ember.RSVP.all(promises).then(function(data) {
        self.notifyLoaded();
      });
    }
  }
});

})();

(function() {

var get = Ember.get;

Ember.FilteredRecordArray = Ember.RecordArray.extend({
  init: function() {
    if (!get(this, 'modelClass')) {
      throw new Error('FilteredRecordArrays must be created with a modelClass');
    }
    if (!get(this, 'filterFunction')) {
      throw new Error('FilteredRecordArrays must be created with a filterFunction');
    }
    if (!get(this, 'filterProperties')) {
      throw new Error('FilteredRecordArrays must be created with filterProperties');
    }

    var modelClass = get(this, 'modelClass');
    modelClass.registerRecordArray(this);

    this.registerObservers();
    this.updateFilter();

    this._super();
  },

  updateFilter: function() {
    var self = this,
        results = [];
    get(this, 'modelClass').forEachCachedRecord(function(record) {
      if (self.filterFunction(record)) {
        results.push(record);
      }
    });
    this.set('content', Ember.A(results));
  },

  updateFilterForRecord: function(record) {
    var results = get(this, 'content');
    if (this.filterFunction(record) && !results.contains(record)) {
      results.pushObject(record);
    }
  },

  registerObservers: function() {
    var self = this;
    get(this, 'modelClass').forEachCachedRecord(function(record) {
      self.registerObserversOnRecord(record);
    });
  },

  registerObserversOnRecord: function(record) {
    var self = this,
        filterProperties = get(this, 'filterProperties');

    for (var i = 0, l = get(filterProperties, 'length'); i < l; i++) {
      record.addObserver(filterProperties[i], self, 'updateFilterForRecord');
    }
  }
});

})();

(function() {

var get = Ember.get, set = Ember.set;

Ember.ManyArray = Ember.RecordArray.extend({
  _records: null,
  originalContent: [],

  isDirty: function() {
    var originalContent = get(this, 'originalContent'),
        originalContentLength = get(originalContent, 'length'),
        content = get(this, 'content'),
        contentLength = get(content, 'length');

    if (originalContentLength !== contentLength) { return true; }

    var isDirty = false;

    for (var i = 0, l = contentLength; i < l; i++) {
      if (!originalContent.contains(content[i])) {
        isDirty = true;
        break;
      }
    }

    return isDirty;
  }.property('content.[]', 'originalContent'),

  objectAtContent: function(idx) {
    var content = get(this, 'content');

    if (!content.length) { return; }

    return this.materializeRecord(idx);
  },

  save: function() {
    // TODO: loop over dirty records only
    return Ember.RSVP.all(this.map(function(record) {
      return record.save();
    }));
  },

  replaceContent: function(index, removed, added) {
    added = Ember.EnumerableUtils.map(added, function(record) {
      return record._reference;
    }, this);

    this._super(index, removed, added);
  },

  _contentWillChange: function() {
    var content = get(this, 'content');
    if (content) {
      content.removeArrayObserver(this);
      this._setupOriginalContent(content);
    }
  }.observesBefore('content'),

  _contentDidChange: function() {
    var content = get(this, 'content');
    if (content) {
      content.addArrayObserver(this);
      this.arrayDidChange(content, 0, 0, get(content, 'length'));
    }
  }.observes('content'),

  arrayWillChange: function(item, idx, removedCnt, addedCnt) {},

  arrayDidChange: function(item, idx, removedCnt, addedCnt) {
    var parent = get(this, 'parent'), relationshipKey = get(this, 'relationshipKey'),
        isDirty = get(this, 'isDirty');

    if (isDirty) {
      parent._relationshipBecameDirty(relationshipKey);
    } else {
      parent._relationshipBecameClean(relationshipKey);
    }
  },

  _setupOriginalContent: function(content) {
    content = content || get(this, 'content');
    if (content) {
      set(this, 'originalContent', content.slice());
    }
  },

  init: function() {
    this._super();
    this._setupOriginalContent();
    this._contentDidChange();
  }
});

Ember.HasManyArray = Ember.ManyArray.extend({
  materializeRecord: function(idx) {
    var klass = get(this, 'modelClass'),
        content = get(this, 'content'),
        reference = content.objectAt(idx),
        record;

    if (reference.record) {
      record = reference.record;
    } else {
      record = klass.find(reference.id);
    }

    return record;
  },

  toJSON: function() {
    var ids = [], content = this.get('content');

    content.forEach(function(reference) {
      if (reference.id) {
        ids.push(reference.id);
      }
    });

    return ids;
  }
});

Ember.EmbeddedHasManyArray = Ember.ManyArray.extend({
  create: function(attrs) {
    var klass = get(this, 'modelClass'),
        record = klass.create(attrs);

    this.pushObject(record);

    return record; // FIXME: inject parent's id
  },

  materializeRecord: function(idx) {
    var klass = get(this, 'modelClass'),
        primaryKey = get(klass, 'primaryKey'),
        content = get(this, 'content'),
        reference = content.objectAt(idx),
        attrs = reference.data;

    if (reference.record) {
      return reference.record;
    } else {
      var record = klass.create({ _reference: reference });
      reference.record = record;
      if (attrs) {
        record.load(attrs[primaryKey], attrs);
      }
      return record;
    }
  },

  toJSON: function() {
    return this.map(function(record) {
      return record.toJSON();
    });
  }
});

})();

(function() {

var get = Ember.get,
    set = Ember.set,
    setProperties = Ember.setProperties,
    meta = Ember.meta,
    underscore = Ember.String.underscore;

function contains(array, element) {
  for (var i = 0, l = array.length; i < l; i++) {
    if (array[i] === element) { return true; }
  }
  return false;
}

function concatUnique(toArray, fromArray) {
  var e;
  for (var i = 0, l = fromArray.length; i < l; i++) {
    e = fromArray[i];
    if (!contains(toArray, e)) { toArray.push(e); }
  }
  return toArray;
}

function hasCachedValue(object, key) {
  var objectMeta = meta(object, false);
  if (objectMeta) {
    return key in objectMeta.cache;
  }
}

Ember.run.queues.push('data');

Ember.Model = Ember.Object.extend(Ember.Evented, {
  isLoaded: true,
  isLoading: Ember.computed.not('isLoaded'),
  isNew: true,
  isDeleted: false,
  _dirtyAttributes: null,

  /**
    Called when attribute is accessed.

    @method getAttr
    @param key {String} key which is being accessed
    @param value {Object} value, which will be returned from getter by default
  */
  getAttr: function(key, value) {
    return value;
  },

  isDirty: function() {
    var dirtyAttributes = get(this, '_dirtyAttributes');
    return dirtyAttributes && dirtyAttributes.length !== 0 || false;
  }.property('_dirtyAttributes.length'),

  _relationshipBecameDirty: function(name) {
    var dirtyAttributes = get(this, '_dirtyAttributes');
    if (!dirtyAttributes.contains(name)) { dirtyAttributes.pushObject(name); }
  },

  _relationshipBecameClean: function(name) {
    var dirtyAttributes = get(this, '_dirtyAttributes');
    dirtyAttributes.removeObject(name);
  },

  dataKey: function(key) {
    var camelizeKeys = get(this.constructor, 'camelizeKeys');
    var meta = this.constructor.metaForProperty(key);
    if (meta.options && meta.options.key) {
      return camelizeKeys ? underscore(meta.options.key) : meta.options.key;
    }
    return camelizeKeys ? underscore(key) : key;
  },

  init: function() {
    this._createReference();
    if (!this._dirtyAttributes) {
      set(this, '_dirtyAttributes', []);
    }
    this._super();
  },

  _createReference: function() {
    var reference = this._reference,
        id = this.getPrimaryKey();

    if (!reference) {
      reference = this.constructor._getOrCreateReferenceForId(id);
      reference.record = this;
      this._reference = reference;
    } else if (reference.id !== id) {
      reference.id = id;
      this.constructor._cacheReference(reference);
    }

    if (!reference.id) {
      reference.id = id;
    }

    return reference;
  },

  getPrimaryKey: function() {
    return get(this, get(this.constructor, 'primaryKey'));
  },

  load: function(id, hash) {
    var data = {};
    data[get(this.constructor, 'primaryKey')] = id;
    set(this, '_data', Ember.merge(data, hash));

    // eagerly load embedded data
    var relationships = this.constructor._relationships || [], meta = Ember.meta(this), relationshipKey, relationship, relationshipMeta, relationshipData, relationshipType;
    for (var i = 0, l = relationships.length; i < l; i++) {
      relationshipKey = relationships[i];
      relationship = meta.descs[relationshipKey];
      relationshipMeta = relationship.meta();

      if (relationshipMeta.options.embedded) {
        relationshipType = relationshipMeta.type;
        if (typeof relationshipType === "string") {
          relationshipType = Ember.get(Ember.lookup, relationshipType);
        }

        relationshipData = data[relationshipKey];
        if (relationshipData) {
          relationshipType.load(relationshipData);
        }
      }
    }

    set(this, 'isLoaded', true);
    set(this, 'isNew', false);
    this._createReference();
    this.trigger('didLoad');
  },

  didDefineProperty: function(proto, key, value) {
    if (value instanceof Ember.Descriptor) {
      var meta = value.meta();
      var klass = proto.constructor;

      if (meta.isAttribute) {
        if (!klass._attributes) { klass._attributes = []; }
        klass._attributes.push(key);
      } else if (meta.isRelationship) {
        if (!klass._relationships) { klass._relationships = []; }
        klass._relationships.push(key);
        meta.relationshipKey = key;
      }
    }
  },

  serializeHasMany: function(key, meta) {
    return this.get(key).toJSON();
  },

  serializeBelongsTo: function(key, meta) {
    if (meta.options.embedded) {
      var record = this.get(key);
      return record ? record.toJSON() : null;
    } else {
      var primaryKey = get(meta.getType(), 'primaryKey');
      return this.get(key + '.' + primaryKey);
    }
  },

  toJSON: function() {
    var key, meta,
        json = {},
        attributes = this.constructor.getAttributes(),
        relationships = this.constructor.getRelationships(),
        properties = attributes ? this.getProperties(attributes) : {},
        rootKey = get(this.constructor, 'rootKey');

    for (key in properties) {
      meta = this.constructor.metaForProperty(key);
      if (meta.type && meta.type.serialize) {
        json[this.dataKey(key)] = meta.type.serialize(properties[key]);
      } else if (meta.type && Ember.Model.dataTypes[meta.type]) {
        json[this.dataKey(key)] = Ember.Model.dataTypes[meta.type].serialize(properties[key]);
      } else {
        json[this.dataKey(key)] = properties[key];
      }
    }

    if (relationships) {
      var data, relationshipKey;

      for(var i = 0; i < relationships.length; i++) {
        key = relationships[i];
        meta = this.constructor.metaForProperty(key);
        relationshipKey = meta.options.key || key;

        if (meta.kind === 'belongsTo') {
          data = this.serializeBelongsTo(key, meta);
        } else {
          data = this.serializeHasMany(key, meta);
        }

        json[relationshipKey] = data;

      }
    }

    if (rootKey) {
      var jsonRoot = {};
      jsonRoot[rootKey] = json;
      return jsonRoot;
    } else {
      return json;
    }
  },

  save: function() {
    var adapter = this.constructor.adapter;
    set(this, 'isSaving', true);
    if (get(this, 'isNew')) {
      return adapter.createRecord(this);
    } else if (get(this, 'isDirty')) {
      return adapter.saveRecord(this);
    } else { // noop, return a resolved promise
      var self = this,
          promise = new Ember.RSVP.Promise(function(resolve, reject) {
            resolve(self);
          });
      set(this, 'isSaving', false);
      return promise;
    }
  },

  reload: function() {
    this.getWithDefault('_dirtyAttributes', []).clear();
    return this.constructor.reload(this.get(get(this.constructor, 'primaryKey')));
  },

  revert: function() {
    this.getWithDefault('_dirtyAttributes', []).clear();
    this.notifyPropertyChange('_data');
  },

  didCreateRecord: function() {
    var primaryKey = get(this.constructor, 'primaryKey'),
        id = get(this, primaryKey);

    set(this, 'isNew', false);

    set(this, '_dirtyAttributes', []);
    this.constructor.addToRecordArrays(this);
    this.trigger('didCreateRecord');
    this.didSaveRecord();
  },

  didSaveRecord: function() {
    set(this, 'isSaving', false);
    this.trigger('didSaveRecord');
    if (this.get('isDirty')) { this._copyDirtyAttributesToData(); }
  },

  deleteRecord: function() {
    return this.constructor.adapter.deleteRecord(this);
  },

  didDeleteRecord: function() {
    this.constructor.removeFromRecordArrays(this);
    set(this, 'isDeleted', true);
    this.trigger('didDeleteRecord');
  },

  _copyDirtyAttributesToData: function() {
    if (!this._dirtyAttributes) { return; }
    var dirtyAttributes = this._dirtyAttributes,
        data = get(this, '_data'),
        key;

    if (!data) {
      data = {};
      set(this, '_data', data);
    }
    for (var i = 0, l = dirtyAttributes.length; i < l; i++) {
      // TODO: merge Object.create'd object into prototype
      key = dirtyAttributes[i];
      data[this.dataKey(key)] = this.cacheFor(key);
    }
    set(this, '_dirtyAttributes', []);
  },

  dataDidChange: Ember.observer(function() {
    this._reloadHasManys();
  }, '_data'),

  _registerHasManyArray: function(array) {
    if (!this._hasManyArrays) { this._hasManyArrays = Ember.A([]); }

    this._hasManyArrays.pushObject(array);
  },

  _reloadHasManys: function() {
    if (!this._hasManyArrays) { return; }
    var i, j;
    for (i = 0; i < this._hasManyArrays.length; i++) {
      var array = this._hasManyArrays[i],
          hasManyContent = this._getHasManyContent(get(array, 'key'), get(array, 'modelClass'), get(array, 'embedded'));
        for (j = 0; j < array.get('length'); j++) {
          if (array.objectAt(j).get('isNew')) {
            hasManyContent.addObject(array.objectAt(j)._reference);
          }
        }
      set(array, 'content', hasManyContent);
    }
  },

  _getHasManyContent: function(key, type, embedded) {
    var content = get(this, '_data.' + key);

    if (content) {
      var mapFunction, primaryKey, reference;
      if (embedded) {
        primaryKey = get(type, 'primaryKey');
        mapFunction = function(attrs) {
          reference = type._getOrCreateReferenceForId(attrs[primaryKey]);
          reference.data = attrs;
          return reference;
        };
      } else {
        mapFunction = function(id) { return type._getOrCreateReferenceForId(id); };
      }
      content = Ember.EnumerableUtils.map(content, mapFunction);
    }

    return Ember.A(content || []);
  }
});

Ember.Model.reopenClass({
  primaryKey: 'id',

  adapter: Ember.Adapter.create(),

  _clientIdCounter: 1,

  getAttributes: function() {
    this.proto(); // force class "compilation" if it hasn't been done.
    var attributes = this._attributes || [];
    if (typeof this.superclass.getAttributes === 'function') {
      attributes = this.superclass.getAttributes().concat(attributes);
    }
    return attributes;
  },

  getRelationships: function() {
    this.proto(); // force class "compilation" if it hasn't been done.
    var relationships = this._relationships || [];
    if (typeof this.superclass.getRelationships === 'function') {
      relationships = this.superclass.getRelationships().concat(relationships);
    }
    return relationships;
  },

  fetch: function(id) {
    if (!arguments.length) {
      return this._findFetchAll(true);
    } else if (Ember.isArray(id)) {
      return this._findFetchMany(id, true);
    } else if (typeof id === 'object') {
      return this._findFetchQuery(id, true);
    } else {
      return this._findFetchById(id, true);
    }
  },

  find: function(id) {
    if (!arguments.length) {
      return this._findFetchAll(false);
    } else if (Ember.isArray(id)) {
      return this._findFetchMany(id, false);
    } else if (typeof id === 'object') {
      return this._findFetchQuery(id, false);
    } else {
      return this._findFetchById(id, false);
    }
  },

  findQuery: function(params) {
    return this._findFetchQuery(params, false);
  },

  fetchQuery: function(params) {
    return this._findFetchQuery(params, true);
  },

  _findFetchQuery: function(params, isFetch) {
    var records = Ember.RecordArray.create({modelClass: this, _query: params});

    var promise = this.adapter.findQuery(this, records, params);

    return isFetch ? promise : records;
  },

  findMany: function(ids) {
    return this._findFetchMany(ids, false);
  },

  fetchMany: function(ids) {
    return this._findFetchMany(ids, true);
  },

  _findFetchMany: function(ids, isFetch) {
    Ember.assert("findFetchMany requires an array", Ember.isArray(ids));

    var records = Ember.RecordArray.create({_ids: ids, modelClass: this}),
        deferred;

    if (!this.recordArrays) { this.recordArrays = []; }
    this.recordArrays.push(records);

    if (this._currentBatchIds) {
      concatUnique(this._currentBatchIds, ids);
      this._currentBatchRecordArrays.push(records);
    } else {
      this._currentBatchIds = concatUnique([], ids);
      this._currentBatchRecordArrays = [records];
    }

    if (isFetch) {
      deferred = Ember.Deferred.create();
      Ember.set(deferred, 'resolveWith', records);

      if (!this._currentBatchDeferreds) { this._currentBatchDeferreds = []; }
      this._currentBatchDeferreds.push(deferred);
    }

    Ember.run.scheduleOnce('data', this, this._executeBatch);

    return isFetch ? deferred : records;
  },

  findAll: function() {
    return this._findFetchAll(false);
  },

  fetchAll: function() {
    return this._findFetchAll(true);
  },

  _findFetchAll: function(isFetch) {
    var self = this;

    if (this._findAllRecordArray) {
      if (isFetch) {
        return new Ember.RSVP.Promise(function(resolve) {
          resolve(self._findAllRecordArray);
        });
      } else {
        return this._findAllRecordArray;
      }
    }

    var records = this._findAllRecordArray = Ember.RecordArray.create({modelClass: this});

    var promise = this.adapter.findAll(this, records);

    // Remove the cached record array if the promise is rejected
    if (promise.then) {
      promise.then(null, function() {
        self._findAllRecordArray = null;
        return Ember.RSVP.reject.apply(null, arguments);
      });
    }

    return isFetch ? promise : records;
  },

  findById: function(id) {
    return this._findFetchById(id, false);
  },

  fetchById: function(id) {
    return this._findFetchById(id, true);
  },

  _findFetchById: function(id, isFetch) {
    var record = this.cachedRecordForId(id),
        isLoaded = get(record, 'isLoaded'),
        adapter = get(this, 'adapter'),
        deferredOrPromise;

    if (isLoaded) {
      if (isFetch) {
        return new Ember.RSVP.Promise(function(resolve, reject) {
          resolve(record);
        });
      } else {
        return record;
      }
    }

    deferredOrPromise = this._fetchById(record, id);

    return isFetch ? deferredOrPromise : record;
  },

  _currentBatchIds: null,
  _currentBatchRecordArrays: null,
  _currentBatchDeferreds: null,

  reload: function(id) {
    var record = this.cachedRecordForId(id);
    record.set('isLoaded', false);
    return this._fetchById(record, id);
  },

  _fetchById: function(record, id) {
    var adapter = get(this, 'adapter'),
        deferred;

    if (adapter.findMany && !adapter.findMany.isUnimplemented) {
      if (this._currentBatchIds) {
        if (!contains(this._currentBatchIds, id)) { this._currentBatchIds.push(id); }
      } else {
        this._currentBatchIds = [id];
        this._currentBatchRecordArrays = [];
      }

      deferred = Ember.Deferred.create();

      //Attached the record to the deferred so we can resolove it later.
      Ember.set(deferred, 'resolveWith', record);

      if (!this._currentBatchDeferreds) { this._currentBatchDeferreds = []; }
      this._currentBatchDeferreds.push(deferred);

      Ember.run.scheduleOnce('data', this, this._executeBatch);

      return deferred;
    } else {
      return adapter.find(record, id);
    }
  },

  _executeBatch: function() {
    var batchIds = this._currentBatchIds,
        batchRecordArrays = this._currentBatchRecordArrays,
        batchDeferreds = this._currentBatchDeferreds,
        self = this,
        requestIds = [],
        promise,
        i;

    this._currentBatchIds = null;
    this._currentBatchRecordArrays = null;
    this._currentBatchDeferreds = null;

    for (i = 0; i < batchIds.length; i++) {
      if (!this.cachedRecordForId(batchIds[i]).get('isLoaded')) {
        requestIds.push(batchIds[i]);
      }
    }

    if (batchIds.length === 1) {
      promise = get(this, 'adapter').find(this.cachedRecordForId(batchIds[0]), batchIds[0]);
    } else {
      var recordArray = Ember.RecordArray.create({_ids: batchIds});
      if (requestIds.length === 0) {
        promise = new Ember.RSVP.Promise(function(resolve, reject) { resolve(recordArray); });
        recordArray.notifyLoaded();
      } else {
        promise = get(this, 'adapter').findMany(this, recordArray, requestIds);
      }
    }

    promise.then(function() {
      for (var i = 0, l = batchRecordArrays.length; i < l; i++) {
        batchRecordArrays[i].loadForFindMany(self);
      }

      if (batchDeferreds) {
        for (i = 0, l = batchDeferreds.length; i < l; i++) {
          var resolveWith = Ember.get(batchDeferreds[i], 'resolveWith');
          batchDeferreds[i].resolve(resolveWith);
        }
      }
    }).then(null, function(errorXHR) {
      if (batchDeferreds) {
        for (var i = 0, l = batchDeferreds.length; i < l; i++) {
          batchDeferreds[i].reject(errorXHR);
        }
      }
    });
  },

  getCachedReferenceRecord: function(id){
    var ref = this._getReferenceById(id);
    if(ref) return ref.record;
    return undefined;
  },

  cachedRecordForId: function(id) {
    var record = this.getCachedReferenceRecord(id);

    if (!record) {
      var primaryKey = get(this, 'primaryKey'),
        attrs = {isLoaded: false};
      attrs[primaryKey] = id;
      record = this.create(attrs);
      var sideloadedData = this.sideloadedData && this.sideloadedData[id];
      if (sideloadedData) {
        record.load(id, sideloadedData);
      }
    }

    return record;
  },

  addToRecordArrays: function(record) {
    if (this._findAllRecordArray) {
      this._findAllRecordArray.pushObject(record);
    }
    if (this.recordArrays) {
      this.recordArrays.forEach(function(recordArray) {
        if (recordArray instanceof Ember.FilteredRecordArray) {
          recordArray.registerObserversOnRecord(record);
          recordArray.updateFilter();
        } else {
          recordArray.pushObject(record);
        }
      });
    }
  },

  unload: function (record) {
    this.removeFromRecordArrays(record);
    var primaryKey = record.get(get(this, 'primaryKey'));
    this.removeFromCache(primaryKey);
  },

  clearCache: function () {
    this.sideloadedData = undefined;
    this._referenceCache = undefined;
  },

  removeFromCache: function (key) {
    if (this.sideloadedData && this.sideloadedData[key]) {
      delete this.sideloadedData[key];
    }
    if(this._referenceCache && this._referenceCache[key]) {
      delete this._referenceCache[key];
    }
  },

  removeFromRecordArrays: function(record) {
    if (this._findAllRecordArray) {
      this._findAllRecordArray.removeObject(record);
    }
    if (this.recordArrays) {
      this.recordArrays.forEach(function(recordArray) {
        recordArray.removeObject(record);
      });
    }
  },

  // FIXME
  findFromCacheOrLoad: function(data) {
    var record;
    if (!data[get(this, 'primaryKey')]) {
      record = this.create({isLoaded: false});
    } else {
      record = this.cachedRecordForId(data[get(this, 'primaryKey')]);
    }
    // set(record, 'data', data);
    record.load(data[get(this, 'primaryKey')], data);
    return record;
  },

  registerRecordArray: function(recordArray) {
    if (!this.recordArrays) { this.recordArrays = []; }
    this.recordArrays.push(recordArray);
  },

  unregisterRecordArray: function(recordArray) {
    if (!this.recordArrays) { return; }
    Ember.A(this.recordArrays).removeObject(recordArray);
  },

  forEachCachedRecord: function(callback) {
    if (!this._referenceCache) { return; }
    var ids = Object.keys(this._referenceCache);
    ids.map(function(id) {
      return this._getReferenceById(id).record;
    }, this).forEach(callback);
  },

  load: function(hashes) {
    if (Ember.typeOf(hashes) !== 'array') { hashes = [hashes]; }

    if (!this.sideloadedData) { this.sideloadedData = {}; }

    for (var i = 0, l = hashes.length; i < l; i++) {
      var hash = hashes[i],
          primaryKey = hash[get(this, 'primaryKey')],
          record = this.getCachedReferenceRecord(primaryKey);

      if (record) {
        record.load(primaryKey, hash);
      } else {
        this.sideloadedData[primaryKey] = hash;
      }
    }
  },

  _getReferenceById: function(id) {
    if (!this._referenceCache) { this._referenceCache = {}; }
    return this._referenceCache[id];
  },

  _getOrCreateReferenceForId: function(id) {
    var reference = this._getReferenceById(id);

    if (!reference) {
      reference = this._createReference(id);
    }

    return reference;
  },

  _createReference: function(id) {
    if (!this._referenceCache) { this._referenceCache = {}; }

    Ember.assert('The id ' + id + ' has alread been used with another record of type ' + this.toString() + '.', !id || !this._referenceCache[id]);

    var reference = {
      id: id,
      clientId: this._clientIdCounter++
    };

    this._cacheReference(reference);

    return reference;
  },

  _cacheReference: function(reference) {
    if (!this._referenceCache) { this._referenceCache = {}; }

    // if we're creating an item, this process will be done
    // later, once the object has been persisted.
    if (reference.id) {
      this._referenceCache[reference.id] = reference;
    }
  }
});

})();

(function() {

var get = Ember.get;

Ember.hasMany = function(type, options) {
  options = options || {};

  var meta = { type: type, isRelationship: true, options: options, kind: 'hasMany' },
      key = options.key;

  return Ember.computed(function() {
    if (typeof type === "string") {
      type = Ember.get(Ember.lookup, type);
    }

    return this.getHasMany(key, type, meta);
  }).property().meta(meta);
};

Ember.Model.reopen({
  getHasMany: function(key, type, meta) {
    var embedded = meta.options.embedded,
        collectionClass = embedded ? Ember.EmbeddedHasManyArray : Ember.HasManyArray;

    var collection = collectionClass.create({
      parent: this,
      modelClass: type,
      content: this._getHasManyContent(key, type, embedded),
      embedded: embedded,
      key: key,
      relationshipKey: meta.relationshipKey
    });

    this._registerHasManyArray(collection);

    return collection;
  }
});

})();

(function() {

var get = Ember.get,
    set = Ember.set;

function getType() {
  if (typeof this.type === "string") {
    this.type =  Ember.get(Ember.lookup, this.type);
  }
  return this.type;
}

Ember.belongsTo = function(type, options) {
  options = options || {};

  var meta = { type: type, isRelationship: true, options: options, kind: 'belongsTo', getType: getType },
      relationshipKey = options.key;

  return Ember.computed(function(key, value, oldValue) {
    type = meta.getType();

    var dirtyAttributes = get(this, '_dirtyAttributes'),
        createdDirtyAttributes = false;

    if (!dirtyAttributes) {
      dirtyAttributes = [];
      createdDirtyAttributes = true;
    }

    if (arguments.length > 1) {
      if (value) {
        Ember.assert(Ember.String.fmt('Attempted to set property of type: %@ with a value of type: %@',
                     [value.constructor, type]),
                     value instanceof type);

        if (oldValue !== value) {
          dirtyAttributes.pushObject(key);
        } else {
          dirtyAttributes.removeObject(key);
        }

        if (createdDirtyAttributes) {
          set(this, '_dirtyAttributes', dirtyAttributes);
        }
      }
      return value === undefined ? null : value;
    } else {
      return this.getBelongsTo(relationshipKey, type, meta);
    }
  }).property('_data').meta(meta);
};

Ember.Model.reopen({
  getBelongsTo: function(key, type, meta) {
    var idOrAttrs = get(this, '_data.' + key),
        record;

    if (Ember.isNone(idOrAttrs)) {
      return null;
    }

    if (meta.options.embedded) {
      var primaryKey = get(type, 'primaryKey'),
        id = idOrAttrs[primaryKey];
      record = type.create({ isLoaded: false, id: id });
      record.load(id, idOrAttrs);
    } else {
      record = type.find(idOrAttrs);
    }

    return record;
  }
});

})();

(function() {

var get = Ember.get,
    set = Ember.set,
    meta = Ember.meta;

Ember.Model.dataTypes = {};

Ember.Model.dataTypes[Date] = {
  deserialize: function(string) {
    if (!string) { return null; }
    return new Date(string);
  },
  serialize: function (date) {
    if (!date) { return null; }
    return date.toISOString();
  },
  isEqual: function(obj1, obj2) {
    if (obj1 instanceof Date) { obj1 = this.serialize(obj1); }
    if (obj2 instanceof Date) { obj2 = this.serialize(obj2); }
    return obj1 === obj2;
  }
};

Ember.Model.dataTypes[Number] = {
  deserialize: function(string) {
    if (!string && string !== 0) { return null; }
    return Number(string);
  },
  serialize: function (number) {
    if (!number && number !== 0) { return null; }
    return Number(number);
  }
};

function deserialize(value, type) {
  if (type && type.deserialize) {
    return type.deserialize(value);
  } else if (type && Ember.Model.dataTypes[type]) {
    return Ember.Model.dataTypes[type].deserialize(value);
  } else {
    return value;
  }
}

function serialize(value, type) {
  if (type && type.serialize) {
    return type.serialize(value);
  } else if (type && Ember.Model.dataTypes[type]) {
    return Ember.Model.dataTypes[type].serialize(value);
  } else {
    return value;
  }
}

Ember.attr = function(type, options) {
  return Ember.computed(function(key, value) {
    var data = get(this, '_data'),
        dataKey = this.dataKey(key),
        dataValue = data && get(data, dataKey),
        beingCreated = meta(this).proto === this,
        dirtyAttributes = get(this, '_dirtyAttributes'),
        createdDirtyAttributes = false;

    if (!dirtyAttributes) {
      dirtyAttributes = [];
      createdDirtyAttributes = true;
    }

    if (arguments.length === 2) {
      if (beingCreated) {
        if (!data) {
          data = {};
          set(this, '_data', data);
        }
        dataValue = data[dataKey] = value;
      }

      if (dataValue !== serialize(value, type)) {
        dirtyAttributes.pushObject(key);
      } else {
        dirtyAttributes.removeObject(key);
      }

      if (createdDirtyAttributes) {
        set(this, '_dirtyAttributes', dirtyAttributes);
      }

      return value;
    }

    return this.getAttr(key, deserialize(dataValue, type));
  }).property('_data').meta({isAttribute: true, type: type, options: options});
};

})();

(function() {

var get = Ember.get;

Ember.RESTAdapter = Ember.Adapter.extend({
  find: function(record, id) {
    var url = this.buildURL(record.constructor, id),
        self = this;

    return this.ajax(url).then(function(data) {
      self.didFind(record, id, data);
      return record;
    });
  },

  didFind: function(record, id, data) {
    var rootKey = get(record.constructor, 'rootKey'),
        dataToLoad = rootKey ? data[rootKey] : data;

    record.load(id, dataToLoad);
  },

  findAll: function(klass, records) {
    var url = this.buildURL(klass),
        self = this;

    return this.ajax(url).then(function(data) {
      self.didFindAll(klass, records, data);
      return records;
    });
  },

  didFindAll: function(klass, records, data) {
    var collectionKey = get(klass, 'collectionKey'),
        dataToLoad = collectionKey ? data[collectionKey] : data;

    records.load(klass, dataToLoad);
  },

  findQuery: function(klass, records, params) {
    var url = this.buildURL(klass),
        self = this;

    return this.ajax(url, params).then(function(data) {
      self.didFindQuery(klass, records, params, data);
      return records;
    });
  },

  didFindQuery: function(klass, records, params, data) {
      var collectionKey = get(klass, 'collectionKey'),
          dataToLoad = collectionKey ? data[collectionKey] : data;

      records.load(klass, dataToLoad);
  },

  createRecord: function(record) {
    var url = this.buildURL(record.constructor),
        self = this;

    return this.ajax(url, record.toJSON(), "POST").then(function(data) {
      self.didCreateRecord(record, data);
      return record;
    });
  },

  didCreateRecord: function(record, data) {
    var rootKey = get(record.constructor, 'rootKey'),
        primaryKey = get(record.constructor, 'primaryKey'),
        dataToLoad = rootKey ? data[rootKey] : data;
    record.load(dataToLoad[primaryKey], dataToLoad);
    record.didCreateRecord();
  },

  saveRecord: function(record) {
    var primaryKey = get(record.constructor, 'primaryKey'),
        url = this.buildURL(record.constructor, get(record, primaryKey)),
        self = this;

    return this.ajax(url, record.toJSON(), "PUT").then(function(data) {  // TODO: Some APIs may or may not return data
      self.didSaveRecord(record, data);
      return record;
    });
  },

  didSaveRecord: function(record, data) {
    record.didSaveRecord();
  },

  deleteRecord: function(record) {
    var primaryKey = get(record.constructor, 'primaryKey'),
        url = this.buildURL(record.constructor, get(record, primaryKey)),
        self = this;

    return this.ajax(url, record.toJSON(), "DELETE").then(function(data) {  // TODO: Some APIs may or may not return data
      self.didDeleteRecord(record, data);
    });
  },

  didDeleteRecord: function(record, data) {
    record.didDeleteRecord();
  },

  ajax: function(url, params, method, settings) {
    return this._ajax(url, params, (method || "GET"), settings);
  },

  buildURL: function(klass, id) {
    var urlRoot = get(klass, 'url');
    if (!urlRoot) { throw new Error('Ember.RESTAdapter requires a `url` property to be specified'); }

    if (!Ember.isEmpty(id)) {
      return urlRoot + "/" + id + ".json";
    } else {
      return urlRoot + ".json";
    }
  },

  ajaxSettings: function(url, method) {
    return {
      url: url,
      type: method,
      dataType: "json"
    };
  },

  _ajax: function(url, params, method, settings) {
    if (!settings) {
      settings = this.ajaxSettings(url, method);
    }

    return new Ember.RSVP.Promise(function(resolve, reject) {
      if (params) {
        if (method === "GET") {
          settings.data = params;
        } else {
          settings.contentType = "application/json; charset=utf-8";
          settings.data = JSON.stringify(params);
        }
      }

      settings.success = function(json) {
        Ember.run(null, resolve, json);
      };

      settings.error = function(jqXHR, textStatus, errorThrown) {
        // https://github.com/ebryn/ember-model/issues/202
        if (jqXHR) {
          jqXHR.then = null;
        }

        Ember.run(null, reject, jqXHR);
      };

      Ember.$.ajax(settings);
   });
  }
});

})();

(function() {

var get = Ember.get;

Ember.LoadPromise = Ember.Object.extend(Ember.DeferredMixin, {
  init: function() {
    this._super.apply(this, arguments);

    var target = get(this, 'target');

    if (get(target, 'isLoaded') && !get(target, 'isNew')) {
      this.resolve(target);
    } else {
      target.one('didLoad', this, function() {
        this.resolve(target);
      });
    }
  }
});

Ember.loadPromise = function(target) {
  if (Ember.isNone(target)) {
    return null;
  } else if (target.then) {
    return target;
  } else {
    return Ember.LoadPromise.create({target: target});
  }
};

})();

(function() {

// This is a debug adapter for the Ember Extension, don't let the fact this is called an "adapter" confuse you.
// Most copied from: https://github.com/emberjs/data/blob/master/packages/ember-data/lib/system/debug/debug_adapter.js

if (!Ember.DataAdapter) { return; }

var get = Ember.get, capitalize = Ember.String.capitalize, underscore = Ember.String.underscore;

var DebugAdapter = Ember.DataAdapter.extend({
  getFilters: function() {
    return [
      { name: 'isNew', desc: 'New' },
      { name: 'isModified', desc: 'Modified' },
      { name: 'isClean', desc: 'Clean' }
    ];
  },

  detect: function(klass) {
    return klass !== Ember.Model && Ember.Model.detect(klass);
  },

  columnsForType: function(type) {
    var columns = [], count = 0, self = this;
    type.getAttributes().forEach(function(name, meta) {
        if (count++ > self.attributeLimit) { return false; }
        var desc = capitalize(underscore(name).replace('_', ' '));
        columns.push({ name: name, desc: desc });
    });
    return columns;
  },

  getRecords: function(type) {
    var records = [];
    type.forEachCachedRecord(function(record) { records.push(record); });
    return records;
  },

  getRecordColumnValues: function(record) {
    var self = this, count = 0,
        columnValues = { id: get(record, 'id') };

    record.constructor.getAttributes().forEach(function(key) {
      if (count++ > self.attributeLimit) {
        return false;
      }
      var value = get(record, key);
      columnValues[key] = value;
    });
    return columnValues;
  },

  getRecordKeywords: function(record) {
    var keywords = [], keys = Ember.A(['id']);
    record.constructor.getAttributes().forEach(function(key) {
      keys.push(key);
    });
    keys.forEach(function(key) {
      keywords.push(get(record, key));
    });
    return keywords;
  },

  getRecordFilterValues: function(record) {
    return {
      isNew: record.get('isNew'),
      isModified: record.get('isDirty') && !record.get('isNew'),
      isClean: !record.get('isDirty')
    };
  },

  getRecordColor: function(record) {
    var color = 'black';
    if (record.get('isNew')) {
      color = 'green';
    } else if (record.get('isDirty')) {
      color = 'blue';
    }
    return color;
  },

  observeRecord: function(record, recordUpdated) {
    var releaseMethods = Ember.A(), self = this,
        keysToObserve = Ember.A(['id', 'isNew', 'isDirty']);

    record.constructor.getAttributes().forEach(function(key) {
      keysToObserve.push(key);
    });

    keysToObserve.forEach(function(key) {
      var handler = function() {
        recordUpdated(self.wrapRecord(record));
      };
      Ember.addObserver(record, key, handler);
      releaseMethods.push(function() {
        Ember.removeObserver(record, key, handler);
      });
    });

    var release = function() {
      releaseMethods.forEach(function(fn) { fn(); } );
    };

    return release;
  }
});

Ember.onLoad('Ember.Application', function(Application) {
  Application.initializer({
    name: "dataAdapter",

    initialize: function(container, application) {
      application.register('dataAdapter:main', DebugAdapter);
    }
  });
});

})();


text:
sencha generate app -ext MyApp D:\MyApp

sencha -sdk {SDK路径} generate app {项目路径}

sencha web start


text:
require 'test_helper'

class Api::V1::MapsControllerTest < ActionController::TestCase
  include Devise::Test::ControllerHelpers
  tests Api::V1::MapsController
  Paperclip::DataUriAdapter.register #enable this in the test environment as we use that to mock tests

  setup do
    @map = FactoryGirl.create(:available_map)
    FactoryGirl.create(:original_masking, :map => @map)
    @warped_map = FactoryGirl.create(:warped_map, :upload_file_name => "different.png")
    MapsOcrJob.any_instance.stubs(:google_image_annotate).returns( JSON.parse({responses: [ text_annotations: [ description: "FOOBAR"]]}.to_json, object_class: OpenStruct) )
    MapsOcrJob.any_instance.stubs(:call_google_geocode).returns("dummy text")
  end

  class SingleMapTest < Api::V1::MapsControllerTest

    def cleanup_images(map)
      if File.exists?(map.temp_filename)
        File.delete(map.temp_filename)
      end
      if File.exists?(map.warped_filename)
        File.delete(map.warped_filename)
      end
      if File.exists?(map.warped_png_filename)
        File.delete(map.warped_png_filename)
      end
      if File.exists?(map.warped_png_filename+".aux.xml")
        File.delete(map.warped_png_filename+".aux.xml")
      end

    end

    class LoggedOut < SingleMapTest
      test "get a map" do
        get :show, :id => @map.id, :format => :json
        assert_response :success
        assert_not_nil assigns(:map)
      end

      test "get map with bbox" do
        get :show, :id => @warped_map.id, :format => :json
        assert_response :success
        assert_not_nil assigns(:map)
        body = JSON.parse(response.body)
        assert_not_nil body["data"]["attributes"]["bbox"]
        assert_equal body["data"]["attributes"]["bbox"], @warped_map.bbox
      end

      test "get map with bbox in geojson format" do
        get :show, :id => @warped_map.id, :format => :geojson
        assert_response :success
        assert_not_nil assigns(:map)
        body = JSON.parse(response.body)
        assert_not_nil body["properties"]["bbox"]
        assert_equal body["properties"]["bbox"], @warped_map.bbox
        assert_not_nil body["geometry"]["coordinates"]
        geojson =  body["geometry"]["coordinates"]
        assert_equal 5, geojson[0].size  #ring of coords

        assert_equal @warped_map.bbox.split(",")[0].to_f, geojson[0][0][0]
      end

      test "get map with bbox in geojson format with specific bbox geo param" do
        get :show, :id => @warped_map.id, :format => :geojson, :geo => "bbox"
        assert_response :success
        assert_not_nil assigns(:map)
        body = JSON.parse(response.body)
        assert_not_nil body["properties"]["bbox"]
        assert_equal body["properties"]["bbox"], @warped_map.bbox
        assert_not_nil body["geometry"]["coordinates"]
        geojson =  body["geometry"]["coordinates"]
        assert_equal 5, geojson[0].size  #ring of coords

        assert_equal @warped_map.bbox.split(",")[0].to_f, geojson[0][0][0]
      end

      test "get map with bbox in geojson format with  mask geo param" do
        masking = FactoryGirl.create(:masking, map: @warped_map)
        get :show, :id => @warped_map.id, :format => :geojson, :geo => "mask"
        assert_response :success
        assert_not_nil assigns(:map)
        body = JSON.parse(response.body)
        assert_not_nil body["properties"]["bbox"]
        assert_equal body["properties"]["bbox"], @warped_map.bbox
        assert_not_nil body["geometry"]["coordinates"]
        geojson =  body["geometry"]["coordinates"]
        assert_equal 7, geojson[0].size  #see geojson coords in masking factory

        assert_equal 111263.560446206232882, geojson[0][0][0]
      end

      test "get map gcps" do
        gcp_1 = FactoryGirl.create(:gcp_1, :map => @warped_map)
        FactoryGirl.create(:gcp_2, :map => @warped_map)
        FactoryGirl.create(:gcp_3, :map => @warped_map)
        get :gcps, :id => @warped_map.id, :format => :json
        assert_response :success
        body = JSON.parse(response.body)
        assert_equal 3, body["data"].length
        first = body["data"][0]["attributes"]
        assert_equal @warped_map.id, first["map_id"]
        assert_equal gcp_1.x , first["x"]
      end

      test "get_status" do
        get "status", :id =>@map.id, :format => :json
        assert_response :ok
        assert_equal @map.status.to_s, response.body
      end

      test "cannot see ocr_result and geocode result" do
        Map.any_instance.stubs(:ocr_result).returns("foo")
        Map.any_instance.stubs(:geocode_result).returns("bar")
        get :show, :id => @map.id, :format => :json
        assert_response :ok
        body = JSON.parse(response.body)
        assert_nil body["data"]["attributes"]["ocr_result"]
        assert_nil body["data"]["attributes"]["geocode_result"]
      end
    end

    class LoggedIn < SingleMapTest
      setup do
        @user = FactoryGirl.create(:user)
        request.env["devise.mapping"] = Devise.mappings[:user]
        sign_in @user
        @warped_map = FactoryGirl.create(:warped_map, :upload_file_name => "different2.png", :owner_id => @user.id)
      end

      #editor or owner role needed
      test "update map when they own it" do
        params = {:id => @warped_map.id, :format => :json, 'data' => {'type' => "maps", "attributes"=>{"title"=>"foojson"}} }
        patch 'update', params
        body = JSON.parse(response.body)
        assert_equal body["data"]["attributes"]["title"], "foojson"
      end

      test "not be allowed to update map when they dont own it" do
        params = {:id => @map.id, :format => :json, 'data' => {'type' => "maps", "attributes"=>{"title"=>"foojson"}} }
        patch 'update', params
        body = JSON.parse(response.body)
        assert_response :unauthorized
        assert body["errors"][0]["title"].include?("Unauthorized")
      end

      test "not authorized to publish" do
        Map.any_instance.stubs(:tilestache_seed).returns(true)
        assert_equal :warped, @warped_map.status
        patch "publish", :id => @warped_map.id, :format => :json
        assert_response :unauthorized
        body = JSON.parse(response.body)
        assert body["errors"][0]["title"].include?("Unauthorized")
      end

     test "not create map basic without a title " do

        assert_difference('Map.count', 0) do
          post 'create', :format => :json, 'data' => {'type' => "maps", "attributes"=>{"description"=>"foo", "wrong"=>"new map"}}
        end
        assert_response :unprocessable_entity
        body = JSON.parse(response.body)

        assert body["errors"][1]["source"]["pointer"].include?("title")
        assert body["errors"][1]["detail"].include?("blank")
      end

      test "create map basic without an upload should fail" do
        assert_difference('Map.count', 0) do
          post 'create', :format => :json, 'data' => {'type' => "maps", "attributes"=>{"description"=>"foo", "title"=>"new map"}}
        end
        assert_response :unprocessable_entity
      end

      test "create map from file upload" do
        image_data = Base64.encode64(File.open(File.join(Rails.root, "/test/fixtures/data/100x70map.png"), "rb").read)
        upload = "data:image/png;base64,#{image_data}"

        assert_difference('Map.count', 1) do
          post 'create', :format => :json, 'data' => {'type' => "maps", "attributes"=>{"description"=>"poo", "title"=>"new map", "upload" => upload, "upload_file_name" => "100x70map2.png"}}
        end
        assert_response :created
        body = JSON.parse(response.body)

        assert_equal "new map", body["data"]["attributes"]["title"]
        assert_equal 100, body["data"]["attributes"]["width"]
      end

      test "create map from URL" do

        stub_request(:get, "http://example.com/100x70map.png")
        .to_return(
          status: 200,
          body:  File.read(File.join(Rails.root, "/test/fixtures/data/100x70map.png")),
          headers: {"Content-Type" => 'image/png'}
        )
        assert_difference('Map.count', 1) do
          post 'create', :format => :json, 'data' => {'type' => "maps", "attributes"=>{"description"=>"poo", "title"=>"new map", "upload_url" => "http://example.com/100x70map.png"}}
        end
        assert_response :created
        body = JSON.parse(response.body)
        assert_equal "new map", body["data"]["attributes"]["title"]
        assert_equal 100, body["data"]["attributes"]["width"]
      end

      test "rectify no gcps" do
        patch :rectify, :id => @warped_map.id, :format => :json, :warp => 1
        assert_response :unprocessable_entity
        body = JSON.parse(response.body)
        assert body["errors"][0]["detail"].include?("3 control points")
      end

      test "rectify" do
        assert_equal :available, @map.status
        gcp_1 = FactoryGirl.create(:gcp_1, :map => @map)
        FactoryGirl.create(:gcp_2, :map => @map)
        FactoryGirl.create(:gcp_3, :map => @map)
        patch :rectify, :id => @map.id, :format => :json, :warp => 1
        assert_response :ok
        body = JSON.parse(response.body)
        assert_equal "warped", body["data"]["attributes"]["status"]

        #cleanup from test
        cleanup_images(@map)
      end

      #          post   'mask'   => 'mask'
      #          delete 'mask'   => 'delete_mask'
      #          patch  'crop'   => 'crop'
      #          patch  'mask_rectify' => 'crop_and_rectify'

      test "save_mask" do
        output = '{ "type": "FeatureCollection",  "features": [{ "type": "Feature", "properties": { "gml_id": "OpenLayers.Feature.Vector_207" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1490.037607068606803, -5310.396178794178923 ], [ 3342.488089397089425, -5310.214910602911914 ], [ 3582.659, -5056.446 ], [ 3555.463, -4743.692 ], [ 3637.051, -4417.34 ], [ 4276.157, -3683.048 ], [ 4575.313, -3043.942 ], [ 4546.465124740124338, -1342.519663201662979 ], [ 2417.461553014552464, -1247.354124740124917 ], [ 1431.415054054054053, -1224.932482328482365 ], [ 1447.752538461538734, -2117.807392931393224 ], [ 1434.537536382537155, -4964.563750519751011 ], [ 1490.037607068606803, -5310.396178794178923 ] ] ] } }]}'

        post 'mask', :id => @warped_map.id, :format => :json, :output => output
        assert_response :ok
        assert !@warped_map.masking.original.nil?

      end

      test "delete_mask" do
        output = '{ "type": "FeatureCollection",  "features": [{ "type": "Feature", "properties": { "gml_id": "OpenLayers.Feature.Vector_207" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1490.037607068606803, -5310.396178794178923 ], [ 3342.488089397089425, -5310.214910602911914 ], [ 3582.659, -5056.446 ], [ 3555.463, -4743.692 ], [ 3637.051, -4417.34 ], [ 4276.157, -3683.048 ], [ 4575.313, -3043.942 ], [ 4546.465124740124338, -1342.519663201662979 ], [ 2417.461553014552464, -1247.354124740124917 ], [ 1431.415054054054053, -1224.932482328482365 ], [ 1447.752538461538734, -2117.807392931393224 ], [ 1434.537536382537155, -4964.563750519751011 ], [ 1490.037607068606803, -5310.396178794178923 ] ] ] } }]}'
        post 'mask', :id => @warped_map.id, :format => :json, :output => output
        assert_response :ok
        assert !@warped_map.masking.original.nil?

        delete 'delete_mask', :id => @warped_map.id, :format => :json
        assert_response :ok

        assert @warped_map.reload.masking.original.nil?

        body = JSON.parse(response.body)
        assert_equal "unmasked", body["data"]["attributes"]["mask_status"]
        cleanup_images(@map)
      end

      #clip (apply mask)
      test "clip_mask" do
        FactoryGirl.create(:original_masking, :map => @warped_map)

        patch 'crop', :id =>@warped_map.id, :format => :json

        assert_response :ok

        body = JSON.parse(response.body)
        assert_equal "masked", body["data"]["attributes"]["mask_status"]
      end

      #clip and rectify apply and warp
      test "mask_crop_rectify" do
        FactoryGirl.create(:gcp_1, :map => @map)
        FactoryGirl.create(:gcp_2, :map => @map)
        FactoryGirl.create(:gcp_3, :map => @map)

        assert_equal :available, @map.status
        assert_equal :unmasked, @map.mask_status

        output = '{ "type": "FeatureCollection",  "features": [{ "type": "Feature", "properties": { "gml_id": "OpenLayers.Feature.Vector_207" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 1490.037607068606803, -5310.396178794178923 ], [ 3342.488089397089425, -5310.214910602911914 ], [ 3582.659, -5056.446 ], [ 3555.463, -4743.692 ], [ 3637.051, -4417.34 ], [ 4276.157, -3683.048 ], [ 4575.313, -3043.942 ], [ 4546.465124740124338, -1342.519663201662979 ], [ 2417.461553014552464, -1247.354124740124917 ], [ 1431.415054054054053, -1224.932482328482365 ], [ 1447.752538461538734, -2117.807392931393224 ], [ 1434.537536382537155, -4964.563750519751011 ], [ 1490.037607068606803, -5310.396178794178923 ] ] ] } }]}'
        patch 'mask_crop_rectify', :id => @map.id, :format => :json, :output => output
        assert_response :ok

        body = JSON.parse(response.body)
        assert_equal "masked", body["data"]["attributes"]["mask_status"]
        assert_equal "warped", body["data"]["attributes"]["status"]

        #cleanup
        cleanup_images(@map)
      end

    end
    class LoggedInEditor < SingleMapTest
      setup do
        editor_user_sign_in
      end
      test "update map" do
        params = {:id => @map.id, :format => :json, 'data' => {'type' => "maps", "attributes"=>{"title"=>"foojson"}} }
        patch 'update', params
        assert_response :success
        body = JSON.parse(response.body)
        assert_equal "foojson", body["data"]["attributes"]["title"]
      end

      test "delete map" do
        Map.any_instance.stubs(:delete_images).returns(true)
        assert_difference('Map.count', -1) do
          delete 'destroy',:id => @map.id
        end
        assert_response :success
      end

    end

    class LoggedInAdmin < SingleMapTest
      setup do
        admin_sign_in
      end

      test "can see ocr_result and geocode result" do
        Map.any_instance.stubs(:ocr_result).returns("foo")
        Map.any_instance.stubs(:geocode_result).returns("bar")
        get :show, :id => @map.id, :format => :json
        assert_response :ok
        body = JSON.parse(response.body)

        assert_equal "foo", body["data"]["attributes"]["ocr_result"]
        assert_equal "bar", body["data"]["attributes"]["geocode_result"]
      end

      test "publish" do
        Map.any_instance.stubs(:tilestache_seed).returns(true)
        assert_equal :warped, @warped_map.status
        patch "publish", :id => @warped_map.id, :format => :json
        assert_response :ok
        body = JSON.parse(response.body)
        assert_equal "publishing", body["data"]["attributes"]["status"]
      end

      test "unpublish" do
        @warped_map.status = :published
        @warped_map.save
        patch "unpublish", :id => @warped_map.id, :format => :json
        assert_response :ok
        body = JSON.parse(response.body)
        assert_equal "warped", body["data"]["attributes"]["status"]
      end
    end
    #get layers

  end

  class CollectionMapTest < Api::V1::MapsControllerTest
    setup do
      @index_maps = FactoryGirl.create_list(:index_map, 5)
    end

    test "layers maps" do
      layer = FactoryGirl.create(:layer_with_maps)
      get :index, :layer_id => layer.id, :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal 1, body["data"].length
    end

    test "get list of maps" do
      get :index, :foo=>"bar", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal 7, body["data"].length
    end

    test "search maps" do
      get :index, :query=>"title", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal 2, body["data"].length
      assert_equal "title", body["data"][0]["attributes"]["title"]
    end

    test "sort maps" do
      get :index, :sort_order => "asc", :sort_key => "title", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal 7, body["data"].length
      assert_equal @index_maps.first.title, body["data"][0]["attributes"]["title"]
      assert_equal "title", body["data"][6]["attributes"]["title"]

      get :index, :sort_order => "desc", :sort_key => "title", :format => :json
      assert_response :success
      body = JSON.parse(response.body)
      assert_equal 7, body["data"].length
      assert_equal @index_maps.first.title, body["data"][6]["attributes"]["title"]
      assert_equal "title", body["data"][0]["attributes"]["title"]
    end

    test "sort and query maps" do
      get :index, :query => "map", :sort_order => "asc", :sort_key => "title", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal 5, body["data"].length
      assert_equal @index_maps.first.title, body["data"][0]["attributes"]["title"]
      assert_equal @index_maps.last.title, body["data"][4]["attributes"]["title"]
    end

    test "get warped" do
      get :index, :show_warped => "1", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal 1, body["data"].length
      assert_equal "title", body["data"][0]["attributes"]["title"]
      assert_equal "warped", body["data"][0]["attributes"]["status"]
    end

    test "geosearch" do
      #intersects: POLYGON((26.779899697812 58.421402710855, 26.779213052304 58.328018921793, 26.849250894101 58.329392212808, 26.849937539609 58.422089356363, 26.779899697812 58.421402710855))
      #bbox = 26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855

      #within: POLYGON((26.633644204648 58.428269165933, 26.63295755914 58.302613038003, 26.859550576718 58.301239746988, 26.85886393121 58.428955811441, 26.633644204648 58.428269165933))
      #bbox = 26.633644204648, 58.302613038003, 26.859550576718, 58.428269165933
      #this bbox intersects our map intersects (but does not encompass it)
      get :index, :bbox => "26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855", :operation => "intersect", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal "title", body["data"][0]["attributes"]["title"]
      assert_equal "warped", body["data"][0]["attributes"]["status"]

      #within, so expect 0
      get :index, :bbox => "26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855", :operation => "within", :format => :json
      assert_response :success
      assert_empty assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal [], body["data"]

      # now with the bbox that encompasses it all

      get :index, :bbox => "26.633644204648, 58.302613038003, 26.859550576718, 58.428269165933", :operation => "intersect", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal "title", body["data"][0]["attributes"]["title"]
      assert_equal "warped", body["data"][0]["attributes"]["status"]

      #within, so expect 0
      get :index, :bbox => "26.633644204648, 58.302613038003, 26.859550576718, 58.428269165933", :operation => "within", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal "title", body["data"][0]["attributes"]["title"]
      assert_equal "warped", body["data"][0]["attributes"]["status"]

    end

    test "geosearch with query" do
      get :index, :bbox => "26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855", :query =>"title",:operation => "intersect", :format => :json
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal "title", body["data"][0]["attributes"]["title"]
      assert_equal "warped", body["data"][0]["attributes"]["status"]

      get :index, :bbox => "26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855", :query =>"map",:operation => "intersect", :format => :json
      assert_response :success
      assert_empty assigns(:maps)
      body = JSON.parse(response.body)
      assert_equal [], body["data"]

    end

    test "geosearch with geojson bbox" do
      get :index, :bbox => "26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855", :query =>"title",:operation => "intersect", :format => :geojson
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)

      assert_equal body[0]["properties"]["bbox"], @warped_map.bbox
      assert_not_nil body[0]["geometry"]["coordinates"]
      coords =  body[0]["geometry"]["coordinates"]
      assert_equal 5, coords[0].size  #ring of coords
      assert_equal @warped_map.bbox.split(",")[0].to_f,  coords[0][0][0]
    end

    test "geosearch with geojson mask" do
      masking = FactoryGirl.create(:masking, map: @warped_map)
      get :index, :bbox => "26.849937539609, 58.328018921793, 26.779899697812, 58.421402710855", :query =>"title",:operation => "intersect", :format => :geojson, :geo => "mask"
      assert_response :success
      assert_not_nil assigns(:maps)
      body = JSON.parse(response.body)

      assert_not_nil body[0]["properties"]["bbox"]
      assert_equal body[0]["properties"]["bbox"], @warped_map.bbox
      assert_not_nil body[0]["geometry"]["coordinates"]
      geojson =  body[0]["geometry"]["coordinates"]
      assert_equal 7, geojson[0].size  #see geojson coords in masking factory
      assert_equal 111263.560446206232882, geojson[0][0][0]
    end

  end

end


text:
///////////////////////////////////////////////////////////////////////////
// Copyright © 2016 Esri. All Rights Reserved.
//
// 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.
///////////////////////////////////////////////////////////////////////////
define(["dojo/_base/declare",
    "dojo/dom-class",
    "./SearchComponent",
    "dojo/text!./templates/Paging.html",
    "dojo/i18n!../nls/strings",
    "./util"
  ],
  function(declare, domClass, SearchComponent, template, i18n, util) {

    return declare([SearchComponent], {

      i18n: i18n,
      templateString: template,

      hasLess: false,
      hasMore: false,
      nextStart: -1,
      numPerPage: null,
      previousStart: -1,
      start: 1,

      postCreate: function() {
        this.inherited(arguments);
        if (this.numPerPage === null) {
          this.numPerPage = 30;
        }
        this.enableOrDisable();
      },

      startup: function() {
        if (this._started) {
          return;
        }
        this.inherited(arguments);

        try {
          var numPer = this.getConfig().numPerPage;
          var v = Number(numPer);
          if (typeof v === "number" && !isNaN(v)) {
            v = Math.floor(v);
            if (v >= 1 && v <= 100) {
              this.numPerPage = v;
            }
          }
        } catch (ex) {
          console.warn("Error setting numPerPage:");
          console.warn(ex);
        }
        if (this.numPerPage === null) {
          this.numPerPage = 30;
        }
      },

      enableOrDisable: function() {
        if (this.hasLess) {
          domClass.remove(this.firstButton.parentNode, "disabled");
          domClass.remove(this.previousButton.parentNode, "disabled");
        } else {
          domClass.add(this.firstButton.parentNode, "disabled");
          domClass.add(this.previousButton.parentNode, "disabled");
        }
        if (this.hasMore) {
          domClass.remove(this.nextButton.parentNode, "disabled");
        } else {
          domClass.add(this.nextButton.parentNode, "disabled");
        }
      },

      /* events ========================================================== */

      firstButtonClicked: function() {
        if (this.hasLess) {
          this.start = 1;
          this.search();
        }
      },
      previousButtonClicked: function() {
        if (this.hasLess) {
          this.start = this.previousStart;
          this.search();
        }
      },
      nextButtonClicked: function() {
        if (this.hasMore) {
          this.start = this.nextStart;
          this.search();
        }
      },

      /* SearchComponent API ============================================= */

      appendQueryParams: function(params) {
        params.start = this.start;
        params.num = this.numPerPage;
      },

      processResults: function(searchResponse) {
        this.start = 1;
        var nPer = this.numPerPage;
        var nHits = searchResponse.total;
        var nStart = searchResponse.queryParams.start;
        if (nStart < 1) {
          nStart = 1;
        }

        this.hasLess = false;
        this.previousStart = -1;
        if (nStart > 1) {
          this.hasLess = true;
          this.previousStart = nStart - searchResponse.queryParams.num;
          if (this.previousStart < 1) {
            this.previousStart = 1;
          }
        }

        this.hasMore = false;
        this.nextStart = -1;
        if (searchResponse.nextQueryParams && searchResponse.nextQueryParams.start > 1) {
          this.hasMore = true;
          this.nextStart = searchResponse.nextQueryParams.start;
        }

        var sPage = "";
        if (nHits > nPer) {
          var nPage = 1;
          if (nStart > 1) {
            nPage = Math.floor(nStart / nPer) + 1;
          }
          sPage = this.i18n.search.paging.pagePattern;
          sPage = sPage.replace("{page}", "" + nPage);
        } else {
          sPage = this.i18n.search.paging.pagePattern;
          sPage = sPage.replace("{page}", "" + 1);
        }
        util.setNodeText(this.pageNode, sPage);
        this.enableOrDisable();
      }

    });
  });