text:
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/optimization_guide/optimization_guide_store.h"

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/metrics/histogram_functions.h"
#include "base/metrics/histogram_macros.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/public/proto_database_provider.h"
#include "components/leveldb_proto/public/shared_proto_database_client_list.h"
#include "components/optimization_guide/memory_hint.h"
#include "components/optimization_guide/optimization_guide_prefs.h"
#include "components/optimization_guide/proto/hint_cache.pb.h"

namespace optimization_guide {

namespace {

// Enforce that StoreEntryType enum is synced with the StoreEntryType proto
// (components/previews/content/proto/hint_cache.proto)
static_assert(
    proto::StoreEntryType_MAX ==
        static_cast<int>(OptimizationGuideStore::StoreEntryType::kMaxValue),
    "mismatched StoreEntryType enums");

// The amount of data to build up in memory before converting to a sorted on-
// disk file.
constexpr size_t kDatabaseWriteBufferSizeBytes = 128 * 1024;

// Delimiter that appears between the sections of a store entry key.
//  Examples:
//    "[StoreEntryType::kMetadata]_[MetadataType]"
//    "[StoreEntryType::kComponentHint]_[component_version]_[host]"
constexpr char kKeySectionDelimiter = '_';

// Realistic minimum length of a host suffix.
const int kMinHostSuffix = 6;  // eg., abc.tv

// Enumerates the possible outcomes of loading metadata. Used in UMA histograms,
// so the order of enumerators should not be changed.
//
// Keep in sync with OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult
// in tools/metrics/histograms/enums.xml.
enum class OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult {
  kSuccess = 0,
  kLoadMetadataFailed = 1,
  kSchemaMetadataMissing = 2,
  kSchemaMetadataWrongVersion = 3,
  kComponentMetadataMissing = 4,
  kFetchedMetadataMissing = 5,
  kComponentAndFetchedMetadataMissing = 6,
  kMaxValue = kComponentAndFetchedMetadataMissing,
};

// Util class for recording the result of loading the metadata. The result is
// recorded when it goes out of scope and its destructor is called.
class ScopedLoadMetadataResultRecorder {
 public:
  ScopedLoadMetadataResultRecorder()
      : result_(OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
                    kSuccess) {}
  ~ScopedLoadMetadataResultRecorder() {
    UMA_HISTOGRAM_ENUMERATION(
        "OptimizationGuide.HintCacheLevelDBStore.LoadMetadataResult", result_);
  }

  void set_result(
      OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult result) {
    result_ = result;
  }

 private:
  OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult result_;
};

void RecordStatusChange(OptimizationGuideStore::Status status) {
  UMA_HISTOGRAM_ENUMERATION("OptimizationGuide.HintCacheLevelDBStore.Status",
                            status);
}

// Returns true if |key_prefix| is a prefix of |key|.
bool DatabasePrefixFilter(const std::string& key_prefix,
                          const std::string& key) {
  return base::StartsWith(key, key_prefix, base::CompareCase::SENSITIVE);
}

// Returns true if |key| is in |keys_to_remove|.
bool ExpiredKeyFilter(const base::flat_set<std::string>& keys_to_remove,
                      const std::string& key) {
  return keys_to_remove.find(key) != keys_to_remove.end();
}

}  // namespace

OptimizationGuideStore::OptimizationGuideStore(
    leveldb_proto::ProtoDatabaseProvider* database_provider,
    const base::FilePath& database_dir,
    scoped_refptr<base::SequencedTaskRunner> store_task_runner) {
  database_ = database_provider->GetDB<proto::StoreEntry>(
      leveldb_proto::ProtoDbType::HINT_CACHE_STORE, database_dir,
      store_task_runner);

  RecordStatusChange(status_);
}

OptimizationGuideStore::OptimizationGuideStore(
    std::unique_ptr<leveldb_proto::ProtoDatabase<proto::StoreEntry>> database)
    : database_(std::move(database)) {
  RecordStatusChange(status_);
}

OptimizationGuideStore::~OptimizationGuideStore() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void OptimizationGuideStore::Initialize(bool purge_existing_data,
                                        base::OnceClosure callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  UpdateStatus(Status::kInitializing);

  // Asynchronously initialize the store and run the callback once
  // initialization completes. Initialization consists of the following steps:
  //   1. Initialize the database.
  //   2. If |purge_existing_data| is set to true, unconditionally purge
  //      database and skip to step 6.
  //   3. Otherwise, retrieve the metadata entries (e.g. Schema and Component).
  //   4. If schema is the wrong version, purge database and skip to step 6.
  //   5. Otherwise, load all hint entry keys.
  //   6. Run callback after purging database or retrieving hint entry keys.

  leveldb_env::Options options = leveldb_proto::CreateSimpleOptions();
  options.write_buffer_size = kDatabaseWriteBufferSizeBytes;
  database_->Init(options,
                  base::BindOnce(&OptimizationGuideStore::OnDatabaseInitialized,
                                 weak_ptr_factory_.GetWeakPtr(),
                                 purge_existing_data, std::move(callback)));
}

std::unique_ptr<StoreUpdateData>
OptimizationGuideStore::MaybeCreateUpdateDataForComponentHints(
    const base::Version& version) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(version.IsValid());

  if (!IsAvailable()) {
    return nullptr;
  }

  // Component updates are only permitted when the update version is newer than
  // the store's current one.
  if (component_version_.has_value() && version <= component_version_) {
    return nullptr;
  }

  // Create and return a StoreUpdateData object. This object has
  // hints from the component moved into it and organizes them in a format
  // usable by the store; the object will returned to the store in
  // StoreComponentHints().
  return StoreUpdateData::CreateComponentStoreUpdateData(version);
}

std::unique_ptr<StoreUpdateData>
OptimizationGuideStore::CreateUpdateDataForFetchedHints(
    base::Time update_time) const {
  // Create and returns a StoreUpdateData object. This object has has hints
  // from the GetHintsResponse moved into and organizes them in a format
  // usable by the store. The object will be store with UpdateFetchedData().
  return StoreUpdateData::CreateFetchedStoreUpdateData(update_time);
}

void OptimizationGuideStore::UpdateComponentHints(
    std::unique_ptr<StoreUpdateData> component_data,
    base::OnceClosure callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(component_data);
  DCHECK(component_data->component_version());

  if (!IsAvailable()) {
    std::move(callback).Run();
    return;
  }

  // If this isn't a newer component version than the store's current one, then
  // the simply return. There's nothing to update.
  if (component_version_.has_value() &&
      component_data->component_version() <= component_version_) {
    std::move(callback).Run();
    return;
  }

  // Set the component version prior to requesting the update. This ensures that
  // a second update request for the same component version won't be allowed. In
  // the case where the update fails, the store will become unavailable, so it's
  // safe to treat component version in the update as the new one.
  SetComponentVersion(*component_data->component_version());

  // The current keys are about to be removed, so clear out the keys available
  // within the store. The keys will be populated after the component data
  // update completes.
  entry_keys_.reset();

  // Purge any component hints that are missing the new component version
  // prefix.
  EntryKeyPrefix retain_prefix =
      GetComponentHintEntryKeyPrefix(component_version_.value());
  EntryKeyPrefix filter_prefix = GetComponentHintEntryKeyPrefixWithoutVersion();

  // Add the new component data and purge any old component hints from the db.
  // After processing finishes, OnUpdateStore() is called, which loads
  // the updated hint entry keys from the database.
  database_->UpdateEntriesWithRemoveFilter(
      component_data->TakeUpdateEntries(),
      base::BindRepeating(
          [](const EntryKeyPrefix& retain_prefix,
             const EntryKeyPrefix& filter_prefix, const std::string& key) {
            return key.compare(0, retain_prefix.length(), retain_prefix) != 0 &&
                   key.compare(0, filter_prefix.length(), filter_prefix) == 0;
          },
          retain_prefix, filter_prefix),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::UpdateFetchedHints(
    std::unique_ptr<StoreUpdateData> fetched_hints_data,
    base::OnceClosure callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(fetched_hints_data);
  DCHECK(fetched_hints_data->update_time());

  if (!IsAvailable()) {
    std::move(callback).Run();
    return;
  }

  fetched_update_time_ = *fetched_hints_data->update_time();

  entry_keys_.reset();

  // This will remove the fetched metadata entry and insert all the entries
  // currently in |leveldb_fetched_hints_data|.
  database_->UpdateEntriesWithRemoveFilter(
      fetched_hints_data->TakeUpdateEntries(),
      base::BindRepeating(&DatabasePrefixFilter,
                          GetMetadataTypeEntryKey(MetadataType::kFetched)),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::PurgeExpiredFetchedHints() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!IsAvailable())
    return;

  // Load all the fetched hints to check their expiry times.
  database_->LoadKeysAndEntriesWithFilter(
      base::BindRepeating(&DatabasePrefixFilter,
                          GetFetchedHintEntryKeyPrefix()),
      base::BindOnce(&OptimizationGuideStore::OnLoadEntriesToPurgeExpired,
                     weak_ptr_factory_.GetWeakPtr()));
}

void OptimizationGuideStore::PurgeExpiredHostModelFeatures() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!IsAvailable())
    return;

  // Load all the host model features to check their expiry times.
  database_->LoadKeysAndEntriesWithFilter(
      base::BindRepeating(&DatabasePrefixFilter,
                          GetHostModelFeaturesEntryKeyPrefix()),
      base::BindOnce(&OptimizationGuideStore::OnLoadEntriesToPurgeExpired,
                     weak_ptr_factory_.GetWeakPtr()));
}

void OptimizationGuideStore::OnLoadEntriesToPurgeExpired(
    bool success,
    std::unique_ptr<EntryMap> entries) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!success)
    return;

  EntryKeySet expired_keys_to_remove;
  int64_t now_since_epoch =
      base::Time::Now().ToDeltaSinceWindowsEpoch().InSeconds();

  for (const auto& entry : *entries) {
    if (entry.second.has_expiry_time_secs() &&
        entry.second.expiry_time_secs() <= now_since_epoch) {
      expired_keys_to_remove.insert(entry.first);
    }
  }

  entry_keys_.reset();

  auto empty_entries = std::make_unique<EntryVector>();

  database_->UpdateEntriesWithRemoveFilter(
      std::move(empty_entries),
      base::BindRepeating(&ExpiredKeyFilter, std::move(expired_keys_to_remove)),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing::Once()));
}

bool OptimizationGuideStore::FindHintEntryKey(
    const std::string& host,
    EntryKey* out_hint_entry_key) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Search for kFetched hint entry keys first, fetched hints should be
  // fresher and preferred.
  if (FindEntryKeyForHostWithPrefix(host, out_hint_entry_key,
                                    GetFetchedHintEntryKeyPrefix())) {
    return true;
  }

  // Search for kComponent hint entry keys next.
  DCHECK(!component_version_.has_value() ||
         component_hint_entry_key_prefix_ ==
             GetComponentHintEntryKeyPrefix(component_version_.value()));
  if (FindEntryKeyForHostWithPrefix(host, out_hint_entry_key,
                                    component_hint_entry_key_prefix_))
    return true;

  return false;
}

bool OptimizationGuideStore::FindEntryKeyForHostWithPrefix(
    const std::string& host,
    EntryKey* out_entry_key,
    const EntryKeyPrefix& entry_key_prefix) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(out_entry_key);

  // Look for longest host name suffix that has a hint. No need to continue
  // lookups and substring work once get to a root domain like ".com" or
  // ".co.in" (MinHostSuffix length check is a heuristic for that).
  std::string host_suffix(host);
  while (host_suffix.length() >= kMinHostSuffix) {
    // Attempt to find an entry key associated with the current host suffix.
    *out_entry_key = entry_key_prefix + host_suffix;
    if (entry_keys_ &&
        entry_keys_->find(*out_entry_key) != entry_keys_->end()) {
      return true;
    }

    size_t pos = host_suffix.find_first_of('.');
    if (pos == std::string::npos) {
      break;
    }
    host_suffix = host_suffix.substr(pos + 1);
  }
  return false;
}

void OptimizationGuideStore::LoadHint(const EntryKey& hint_entry_key,
                                      HintLoadedCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!IsAvailable()) {
    std::move(callback).Run(hint_entry_key, nullptr);
    return;
  }

  database_->GetEntry(hint_entry_key,
                      base::BindOnce(&OptimizationGuideStore::OnLoadHint,
                                     weak_ptr_factory_.GetWeakPtr(),
                                     hint_entry_key, std::move(callback)));
}

base::Time OptimizationGuideStore::GetFetchedHintsUpdateTime() const {
  // If the store is not available, the metadata entries have not been loaded
  // so there are no fetched hints.
  if (!IsAvailable())
    return base::Time();
  return fetched_update_time_;
}

base::Time OptimizationGuideStore::GetHostModelFeaturesUpdateTime() const {
  // If the store is not available, the metadata entries have not been loaded
  // so there are no host model features.
  if (!IsAvailable())
    return base::Time();
  return host_model_features_update_time_;
}

// static
const char OptimizationGuideStore::kStoreSchemaVersion[] = "1";

// static
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetMetadataEntryKeyPrefix() {
  return base::NumberToString(static_cast<int>(
             OptimizationGuideStore::StoreEntryType::kMetadata)) +
         kKeySectionDelimiter;
}

// static
OptimizationGuideStore::EntryKey
OptimizationGuideStore::GetMetadataTypeEntryKey(MetadataType metadata_type) {
  return GetMetadataEntryKeyPrefix() +
         base::NumberToString(static_cast<int>(metadata_type));
}

// static
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetComponentHintEntryKeyPrefixWithoutVersion() {
  return base::NumberToString(static_cast<int>(
             OptimizationGuideStore::StoreEntryType::kComponentHint)) +
         kKeySectionDelimiter;
}

// static
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetComponentHintEntryKeyPrefix(
    const base::Version& component_version) {
  return GetComponentHintEntryKeyPrefixWithoutVersion() +
         component_version.GetString() + kKeySectionDelimiter;
}

// static
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetFetchedHintEntryKeyPrefix() {
  return base::NumberToString(static_cast<int>(
             OptimizationGuideStore::StoreEntryType::kFetchedHint)) +
         kKeySectionDelimiter;
}

// static
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetPredictionModelEntryKeyPrefix() {
  return base::NumberToString(static_cast<int>(
             OptimizationGuideStore::StoreEntryType::kPredictionModel)) +
         kKeySectionDelimiter;
}

// static
OptimizationGuideStore::EntryKeyPrefix
OptimizationGuideStore::GetHostModelFeaturesEntryKeyPrefix() {
  return base::NumberToString(static_cast<int>(
             OptimizationGuideStore::StoreEntryType::kHostModelFeatures)) +
         kKeySectionDelimiter;
}

void OptimizationGuideStore::UpdateStatus(Status new_status) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // Status::kUninitialized can only transition to Status::kInitializing.
  DCHECK(status_ != Status::kUninitialized ||
         new_status == Status::kInitializing);
  // Status::kInitializing can only transition to Status::kAvailable or
  // Status::kFailed.
  DCHECK(status_ != Status::kInitializing || new_status == Status::kAvailable ||
         new_status == Status::kFailed);
  // Status::kAvailable can only transition to kStatus::Failed.
  DCHECK(status_ != Status::kAvailable || new_status == Status::kFailed);
  // The status can never transition from Status::kFailed.
  DCHECK(status_ != Status::kFailed || new_status == Status::kFailed);

  // If the status is not changing, simply return; the remaining logic handles
  // status changes.
  if (status_ == new_status) {
    return;
  }

  status_ = new_status;
  RecordStatusChange(status_);

  if (status_ == Status::kFailed) {
    database_->Destroy(
        base::BindOnce(&OptimizationGuideStore::OnDatabaseDestroyed,
                       weak_ptr_factory_.GetWeakPtr()));
    ClearComponentVersion();
    entry_keys_.reset();
  }
}

bool OptimizationGuideStore::IsAvailable() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return status_ == Status::kAvailable;
}

void OptimizationGuideStore::PurgeDatabase(base::OnceClosure callback) {
  // When purging the database, update the schema version to the current one.
  EntryKey schema_entry_key = GetMetadataTypeEntryKey(MetadataType::kSchema);
  proto::StoreEntry schema_entry;
  schema_entry.set_version(kStoreSchemaVersion);

  auto entries_to_save = std::make_unique<EntryVector>();
  entries_to_save->emplace_back(schema_entry_key, schema_entry);

  database_->UpdateEntriesWithRemoveFilter(
      std::move(entries_to_save),
      base::BindRepeating(
          [](const std::string& schema_entry_key, const std::string& key) {
            return key.compare(0, schema_entry_key.length(),
                               schema_entry_key) != 0;
          },
          schema_entry_key),
      base::BindOnce(&OptimizationGuideStore::OnPurgeDatabase,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::SetComponentVersion(
    const base::Version& component_version) {
  DCHECK(component_version.IsValid());
  component_version_ = component_version;
  component_hint_entry_key_prefix_ =
      GetComponentHintEntryKeyPrefix(component_version_.value());
}

void OptimizationGuideStore::ClearComponentVersion() {
  component_version_.reset();
  component_hint_entry_key_prefix_.clear();
}

void OptimizationGuideStore::ClearFetchedHintsFromDatabase() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  base::UmaHistogramBoolean(
      "OptimizationGuide.ClearFetchedHints.StoreAvailable", IsAvailable());
  if (!IsAvailable())
    return;

  auto entries_to_save = std::make_unique<EntryVector>();

  // TODO(mcrouse): Add histogram to record the number of hints being removed.
  entry_keys_.reset();

  // Removes all |kFetchedHint| store entries. OnUpdateStore will handle
  // updating status and re-filling entry_keys with the entries still in the
  // store.
  database_->UpdateEntriesWithRemoveFilter(
      std::move(entries_to_save),  // this should be empty.
      base::BindRepeating(&DatabasePrefixFilter,
                          GetFetchedHintEntryKeyPrefix()),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing::Once()));
}

void OptimizationGuideStore::MaybeLoadEntryKeys(base::OnceClosure callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // If the database is unavailable  don't load the hint keys. Simply run the
  // callback.
  if (!IsAvailable()) {
    std::move(callback).Run();
    return;
  }

  // Create a new KeySet object. This is populated by the store's keys as the
  // filter is run with each key on the DB's background thread. The filter
  // itself always returns false, ensuring that no entries are ever actually
  // loaded by the DB. Ownership of the KeySet is passed into the
  // LoadKeysAndEntriesCallback callback, guaranteeing that the KeySet has a
  // lifespan longer than the filter calls.
  std::unique_ptr<EntryKeySet> entry_keys(std::make_unique<EntryKeySet>());
  EntryKeySet* raw_entry_keys_pointer = entry_keys.get();
  database_->LoadKeysAndEntriesWithFilter(
      base::BindRepeating(
          [](EntryKeySet* entry_keys, const std::string& filter_prefix,
             const std::string& entry_key) {
            if (entry_key.compare(0, filter_prefix.length(), filter_prefix) !=
                0) {
              entry_keys->insert(entry_key);
            }
            return false;
          },
          raw_entry_keys_pointer, GetMetadataEntryKeyPrefix()),
      base::BindOnce(&OptimizationGuideStore::OnLoadEntryKeys,
                     weak_ptr_factory_.GetWeakPtr(), std::move(entry_keys),
                     std::move(callback)));
}

size_t OptimizationGuideStore::GetEntryKeyCount() const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  return entry_keys_ ? entry_keys_->size() : 0;
}

void OptimizationGuideStore::OnDatabaseInitialized(
    bool purge_existing_data,
    base::OnceClosure callback,
    leveldb_proto::Enums::InitStatus status) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (status != leveldb_proto::Enums::InitStatus::kOK) {
    UpdateStatus(Status::kFailed);
    std::move(callback).Run();
    return;
  }

  // If initialization is set to purge all existing data, then simply trigger
  // the purge and return. There's no need to load metadata entries that'll
  // immediately be purged.
  if (purge_existing_data) {
    PurgeDatabase(std::move(callback));
    return;
  }

  // Load all entries from the DB with the metadata key prefix.
  database_->LoadKeysAndEntriesWithFilter(
      leveldb_proto::KeyFilter(), leveldb::ReadOptions(),
      GetMetadataEntryKeyPrefix(),
      base::BindOnce(&OptimizationGuideStore::OnLoadMetadata,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::OnDatabaseDestroyed(bool /*success*/) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
}

void OptimizationGuideStore::OnLoadMetadata(
    base::OnceClosure callback,
    bool success,
    std::unique_ptr<EntryMap> metadata_entries) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(metadata_entries);

  // Create a scoped load metadata result recorder. It records the result when
  // its destructor is called.
  ScopedLoadMetadataResultRecorder result_recorder;

  if (!success) {
    result_recorder.set_result(
        OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
            kLoadMetadataFailed);

    UpdateStatus(Status::kFailed);
    std::move(callback).Run();
    return;
  }

  // If the schema version in the DB is not the current version, then purge
  // the database.
  auto schema_entry =
      metadata_entries->find(GetMetadataTypeEntryKey(MetadataType::kSchema));
  if (schema_entry == metadata_entries->end() ||
      !schema_entry->second.has_version() ||
      schema_entry->second.version() != kStoreSchemaVersion) {
    if (schema_entry == metadata_entries->end()) {
      result_recorder.set_result(
          OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
              kSchemaMetadataMissing);
    } else {
      result_recorder.set_result(
          OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
              kSchemaMetadataWrongVersion);
    }

    PurgeDatabase(std::move(callback));
    return;
  }

  // If the component metadata entry exists, then use it to set the component
  // version.
  bool component_metadata_missing = false;
  auto component_entry =
      metadata_entries->find(GetMetadataTypeEntryKey(MetadataType::kComponent));
  if (component_entry != metadata_entries->end()) {
    DCHECK(component_entry->second.has_version());
    SetComponentVersion(base::Version(component_entry->second.version()));
  } else {
    result_recorder.set_result(
        OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
            kComponentMetadataMissing);
    component_metadata_missing = true;
  }

  auto fetched_entry =
      metadata_entries->find(GetMetadataTypeEntryKey(MetadataType::kFetched));
  if (fetched_entry != metadata_entries->end()) {
    DCHECK(fetched_entry->second.has_update_time_secs());
    fetched_update_time_ = base::Time::FromDeltaSinceWindowsEpoch(
        base::TimeDelta::FromSeconds(fetched_entry->second.update_time_secs()));
  } else {
    if (component_metadata_missing) {
      result_recorder.set_result(
          OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
              kComponentAndFetchedMetadataMissing);
    } else {
      result_recorder.set_result(
          OptimizationGuideHintCacheLevelDBStoreLoadMetadataResult::
              kFetchedMetadataMissing);
    }
    fetched_update_time_ = base::Time();
  }

  auto host_model_features_entry = metadata_entries->find(
      GetMetadataTypeEntryKey(MetadataType::kHostModelFeatures));
  bool host_model_features_metadata_loaded = false;
  host_model_features_update_time_ = base::Time();
  if (host_model_features_entry != metadata_entries->end()) {
    DCHECK(host_model_features_entry->second.has_update_time_secs());
    host_model_features_update_time_ =
        base::Time::FromDeltaSinceWindowsEpoch(base::TimeDelta::FromSeconds(
            host_model_features_entry->second.update_time_secs()));
    host_model_features_metadata_loaded = true;
  }
  // TODO(crbug/1001194): Metrics should be separated so that stores maintaining
  // different information types only record metrics for the types of entries
  // they store.
  UMA_HISTOGRAM_BOOLEAN(
      "OptimizationGuide.PredictionModelStore."
      "HostModelFeaturesLoadMetadataResult",
      host_model_features_metadata_loaded);

  UpdateStatus(Status::kAvailable);
  MaybeLoadEntryKeys(std::move(callback));
}

void OptimizationGuideStore::OnPurgeDatabase(base::OnceClosure callback,
                                             bool success) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  // The database can only be purged during initialization.
  DCHECK_EQ(status_, Status::kInitializing);

  UpdateStatus(success ? Status::kAvailable : Status::kFailed);
  std::move(callback).Run();
}

void OptimizationGuideStore::OnUpdateStore(base::OnceClosure callback,
                                           bool success) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!success) {
    UpdateStatus(Status::kFailed);
    std::move(callback).Run();
    return;
  }
  MaybeLoadEntryKeys(std::move(callback));
}

void OptimizationGuideStore::OnLoadEntryKeys(
    std::unique_ptr<EntryKeySet> hint_entry_keys,
    base::OnceClosure callback,
    bool success,
    std::unique_ptr<EntryMap> /*unused*/) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!success) {
    UpdateStatus(Status::kFailed);
    std::move(callback).Run();
    return;
  }

  // If the store was set to unavailable after the request was started, then the
  // loaded keys should not be considered valid. Reset the keys so that they are
  // cleared.
  if (!IsAvailable())
    hint_entry_keys.reset();

  entry_keys_ = std::move(hint_entry_keys);

  std::move(callback).Run();
}

void OptimizationGuideStore::OnLoadHint(
    const std::string& entry_key,
    HintLoadedCallback callback,
    bool success,
    std::unique_ptr<proto::StoreEntry> entry) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // If either the request failed, the store was set to unavailable after the
  // request was started, or there's an in-flight component data update, which
  // means the entry is about to be invalidated, then the loaded hint should
  // not be considered valid. Reset the entry so that no hint is returned to
  // the requester.
  if (!success || !IsAvailable()) {
    entry.reset();
  }

  if (!entry || !entry->has_hint()) {
    std::unique_ptr<MemoryHint> memory_hint;
    std::move(callback).Run(entry_key, std::move(memory_hint));
    return;
  }

  if (entry->has_expiry_time_secs() &&
      entry->expiry_time_secs() <=
          base::Time::Now().ToDeltaSinceWindowsEpoch().InSeconds()) {
    // An expired hint should be loaded rarely if the user is regularly fetching
    // and storing fresh hints. Expired fetched hints are removed each time
    // fresh hints are fetched and placed into the store. In the future, the
    // expired hints could be asynchronously removed if necessary.
    // An empty hint is returned instead of the expired one.
    LOCAL_HISTOGRAM_BOOLEAN(
        "OptimizationGuide.HintCacheStore.OnLoadHint.FetchedHintExpired", true);
    std::unique_ptr<MemoryHint> memory_hint(nullptr);
    std::move(callback).Run(entry_key, std::move(memory_hint));
    return;
  }

  StoreEntryType store_entry_type =
      static_cast<StoreEntryType>(entry->entry_type());
  UMA_HISTOGRAM_ENUMERATION("OptimizationGuide.HintCache.HintType.Loaded",
                            store_entry_type);

  base::Optional<base::Time> expiry_time;
  if (entry->has_expiry_time_secs()) {
    expiry_time = base::Time::FromDeltaSinceWindowsEpoch(
        base::TimeDelta::FromSeconds(entry->expiry_time_secs()));
    LOCAL_HISTOGRAM_CUSTOM_TIMES(
        "OptimizationGuide.HintCache.FetchedHint.TimeToExpiration",
        *expiry_time - base::Time::Now(), base::TimeDelta::FromHours(1),
        base::TimeDelta::FromDays(15), 50);
  }
  std::move(callback).Run(
      entry_key,
      std::make_unique<MemoryHint>(
          expiry_time, std::unique_ptr<proto::Hint>(entry->release_hint())));
}

std::unique_ptr<StoreUpdateData>
OptimizationGuideStore::CreateUpdateDataForPredictionModels() const {
  // Create and returns a StoreUpdateData object. This object has prediction
  // models from the GetModelsResponse moved into and organizes them in a format
  // usable by the store. The object will be stored with
  // UpdatePredictionModels().
  return StoreUpdateData::CreatePredictionModelStoreUpdateData();
}

void OptimizationGuideStore::UpdatePredictionModels(
    std::unique_ptr<StoreUpdateData> prediction_models_update_data,
    base::OnceClosure callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(prediction_models_update_data);

  if (!IsAvailable()) {
    std::move(callback).Run();
    return;
  }

  std::unique_ptr<EntryVector> entry_vectors =
      prediction_models_update_data->TakeUpdateEntries();

  database_->UpdateEntries(
      std::move(entry_vectors), std::make_unique<leveldb_proto::KeyVector>(),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

bool OptimizationGuideStore::FindPredictionModelEntryKey(
    proto::OptimizationTarget optimization_target,
    OptimizationGuideStore::EntryKey* out_prediction_model_entry_key) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!entry_keys_)
    return false;
  *out_prediction_model_entry_key =
      GetPredictionModelEntryKeyPrefix() +
      base::NumberToString(static_cast<int>(optimization_target));
  if (entry_keys_->find(*out_prediction_model_entry_key) != entry_keys_->end())
    return true;
  return false;
}

void OptimizationGuideStore::LoadPredictionModel(
    const EntryKey& prediction_model_entry_key,
    PredictionModelLoadedCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!IsAvailable()) {
    std::move(callback).Run(nullptr);
    return;
  }

  database_->GetEntry(
      prediction_model_entry_key,
      base::BindOnce(&OptimizationGuideStore::OnLoadPredictionModel,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::OnLoadPredictionModel(
    PredictionModelLoadedCallback callback,
    bool success,
    std::unique_ptr<proto::StoreEntry> entry) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // If either the request failed or the store was set to unavailable after the
  // request was started, then the loaded model should not be considered valid.
  // Reset the entry so that nothing is returned to
  // the requester.
  if (!success || !IsAvailable()) {
    entry.reset();
  }

  if (!entry || !entry->has_prediction_model()) {
    std::unique_ptr<proto::PredictionModel> loaded_prediction_model(nullptr);
    std::move(callback).Run(std::move(loaded_prediction_model));
    return;
  }

  std::unique_ptr<proto::PredictionModel> loaded_prediction_model(
      entry->release_prediction_model());
  std::move(callback).Run(std::move(loaded_prediction_model));
}

std::unique_ptr<StoreUpdateData>
OptimizationGuideStore::CreateUpdateDataForHostModelFeatures(
    base::Time host_model_features_update_time,
    base::Time expiry_time) const {
  // Create and returns a StoreUpdateData object. This object has host model
  // features from the GetModelsResponse moved into and organizes them in a
  // format usable by the store. The object will be stored with
  // UpdateHostModelFeatures().
  return StoreUpdateData::CreateHostModelFeaturesStoreUpdateData(
      host_model_features_update_time, expiry_time);
}

void OptimizationGuideStore::UpdateHostModelFeatures(
    std::unique_ptr<StoreUpdateData> host_model_features_update_data,
    base::OnceClosure callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
  DCHECK(host_model_features_update_data->update_time());

  if (!IsAvailable()) {
    std::move(callback).Run();
    return;
  }

  host_model_features_update_time_ =
      *host_model_features_update_data->update_time();

  entry_keys_.reset();

  // This will remove the host model features metadata entry and insert all the
  // entries currently in |host_model_features_update_data|.
  database_->UpdateEntriesWithRemoveFilter(
      host_model_features_update_data->TakeUpdateEntries(),
      base::BindRepeating(
          &DatabasePrefixFilter,
          GetMetadataTypeEntryKey(MetadataType::kHostModelFeatures)),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

bool OptimizationGuideStore::FindHostModelFeaturesEntryKey(
    const std::string& host,
    OptimizationGuideStore::EntryKey* out_host_model_features_entry_key) const {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!entry_keys_)
    return false;

  return FindEntryKeyForHostWithPrefix(host, out_host_model_features_entry_key,
                                       GetHostModelFeaturesEntryKeyPrefix());
}

void OptimizationGuideStore::LoadAllHostModelFeatures(
    AllHostModelFeaturesLoadedCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!IsAvailable()) {
    std::move(callback).Run({});
    return;
  }

  // Load all the host model features within the store.
  database_->LoadEntriesWithFilter(
      base::BindRepeating(&DatabasePrefixFilter,
                          GetHostModelFeaturesEntryKeyPrefix()),
      base::BindOnce(&OptimizationGuideStore::OnLoadAllHostModelFeatures,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::LoadHostModelFeatures(
    const EntryKey& host_model_features_entry_key,
    HostModelFeaturesLoadedCallback callback) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  if (!IsAvailable()) {
    std::move(callback).Run({});
    return;
  }

  // Load all the host model features within the store.
  database_->GetEntry(
      host_model_features_entry_key,
      base::BindOnce(&OptimizationGuideStore::OnLoadHostModelFeatures,
                     weak_ptr_factory_.GetWeakPtr(), std::move(callback)));
}

void OptimizationGuideStore::OnLoadHostModelFeatures(
    HostModelFeaturesLoadedCallback callback,
    bool success,
    std::unique_ptr<proto::StoreEntry> entry) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // If either the request failed or the store was set to unavailable after the
  // request was started, then the loaded host model features should not
  // be considered valid. Reset the entry so that nothing is returned to the
  // requester.
  if (!success || !IsAvailable()) {
    entry.reset();
  }
  if (!entry || !entry->has_host_model_features()) {
    std::move(callback).Run(nullptr);
    return;
  }

  std::unique_ptr<proto::HostModelFeatures> loaded_host_model_features(
      entry->release_host_model_features());
  std::move(callback).Run(std::move(loaded_host_model_features));
}

void OptimizationGuideStore::OnLoadAllHostModelFeatures(
    AllHostModelFeaturesLoadedCallback callback,
    bool success,
    std::unique_ptr<std::vector<proto::StoreEntry>> entries) {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  // If either the request failed or the store was set to unavailable after the
  // request was started, then the loaded host model features should not
  // be considered valid. Reset the entry so that nothing is returned to the
  // requester.
  if (!success || !IsAvailable()) {
    entries.reset();
  }

  if (!entries || entries->size() == 0) {
    std::unique_ptr<std::vector<proto::HostModelFeatures>>
        loaded_host_model_features(nullptr);
    std::move(callback).Run(std::move(loaded_host_model_features));
    return;
  }

  std::unique_ptr<std::vector<proto::HostModelFeatures>>
      loaded_host_model_features =
          std::make_unique<std::vector<proto::HostModelFeatures>>();
  for (auto& entry : *entries.get()) {
    if (!entry.has_host_model_features())
      continue;
    loaded_host_model_features->emplace_back(entry.host_model_features());
  }
  std::move(callback).Run(std::move(loaded_host_model_features));
}

void OptimizationGuideStore::ClearHostModelFeaturesFromDatabase() {
  DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);

  base::UmaHistogramBoolean(
      "OptimizationGuide.ClearHostModelFeatures.StoreAvailable", IsAvailable());
  if (!IsAvailable())
    return;

  auto entries_to_save = std::make_unique<EntryVector>();

  entry_keys_.reset();

  // Removes all |kHostModelFeatures| store entries. OnUpdateStore will handle
  // updating status and re-filling entry_keys with the entries still in the
  // store.
  database_->UpdateEntriesWithRemoveFilter(
      std::move(entries_to_save),  // this should be empty.
      base::BindRepeating(&DatabasePrefixFilter,
                          GetHostModelFeaturesEntryKeyPrefix()),
      base::BindOnce(&OptimizationGuideStore::OnUpdateStore,
                     weak_ptr_factory_.GetWeakPtr(), base::DoNothing::Once()));
}

}  // namespace optimization_guide


text:
usb4java
========

USB library for Java based on libusb 1.0
----------------------------------------

Copyright (C) 2011 Klaus Reimer, k@ailis.de
Copyright (C) 2013 Luca Longinotti, l@longi.li

See LICENSE.md for licensing information.

This library can be used to access USB devices in Java. It is based on the
native libusb 1.0 shared library and reflects this API as complete as
possible. usb4java also implements the javax-usb (JSR80) API. So you can choose
if you want to access the USB devices via the low-level libusb 1.0 API or
via the high level javax-usb API.

The native libraries are included in pre-compiled form for Linux, Windows and
Mac OS X in 32 and 64 bit versions. The libraries are automatically extracted
and loaded so you don't need to care about them.

For more detailed information please visit the [usb4java website](http://usb4java.org/).

[![Build Status](https://ci.ailis.de/job/usb4java/badge/icon)](https://ci.ailis.de/job/usb4java/)

redpajama_set_name: RedPajamaGithub


text:
FROM debian:bullseye

ENV LANG C.UTF-8
ENV LANGUAGE C.UTF-8
ENV LC_ALL C.UTF-8

ARG USERID=1000
RUN adduser elektra --uid ${USERID} \
    && adduser elektra sudo

ENV ELEKTRA_ROOT=/opt/elektra/
RUN mkdir -p ${ELEKTRA_ROOT}
COPY ./*.deb ${ELEKTRA_ROOT}
COPY ./*.ddeb ${ELEKTRA_ROOT}

RUN apt-get update \
    && apt-get -y install \
        sudo \
        vim \
    && apt-get -y install ${ELEKTRA_ROOT}/* \
    && rm -rf /var/lib/apt/lists/*

RUN rm -rf ${ELEKTRA_ROOT}

RUN kdb mount-info \
    && mkdir -p `kdb sget system:/info/elektra/constants/cmake/KDB_DB_SPEC .` || true \
    && chown -R ${USERID} `kdb sget system:/info/elektra/constants/cmake/KDB_DB_SPEC .` \
    && chown -R ${USERID} `kdb sget system:/info/elektra/constants/cmake/KDB_DB_SYSTEM .` \
    && chown -R ${USERID} `kdb sget system:/info/elektra/constants/cmake/BUILTIN_DATA_FOLDER .`

RUN --mount=type=tmpfs,target=/tmp \
    --mount=type=tmpfs,target=/etc/kdb \
    --mount=type=tmpfs,target=/root/.cache/elektra \
    --mount=type=tmpfs,target=/root/.config \
    kdb run_all

RUN echo "%sudo ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers

USER ${USERID}
WORKDIR /home/elektra

CMD ["/bin/bash","-l"]


text:
package ro.pub.cs.aipi.lab09.general;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Utilities {

    public static String tableNameToMenuEntry(String tableName) {
        return tableName.replaceAll("_", " ");
    }

    public static String menuEntryToTableName(String menuEntry) {
        return menuEntry.replaceAll(" ", "_");
    }

    public static String compress(List<String> record) {
        String result = record.get(0);
        for (int position = 1; position < record.size(); position++) {
            result += " # " + record.get(position);
        }
        return result;
    }

    public static List<String> decompress(String record) {
        List<String> result = new ArrayList<>(Arrays.asList(record.split("#")));
        for (int position = 0; position < result.size(); position++) {
            result.set(position, result.get(position).trim().replace("'", "\\'"));
        }
        return result;
    }

    public static String escape(String text) {
        return text.replace("'", "\\'");
    }
}


text:
{% extends "admin/change_form.html" %}
{% load i18n admin_static admin_modify cms_admin %}

{% block title %}{% trans "Change a page" %}{% endblock %}

{% block breadcrumbs %}

    {% trans "Home" %} ›
    {{ app_label|capfirst|escape }} › {{ opts.verbose_name_plural|capfirst|escape }}

{% endblock %}

{% block extrahead %}
{{ block.super }}

{% endblock %}

{% block content %}

{% block object-tools %}{% endblock %}

<form {% if has_file_field %}enctype="multipart/form-data" {% endif %}action="?language={{ language }}{%if request.GET.target %}&target={{ request.GET.target }}{% endif %}{%if request.GET.position %}&position={{ request.GET.position }}{% endif %}{% if request.GET.copy_target %}&copy_target={{ request.GET.copy_target }}{% endif %}{% if request.GET.add_page_type %}&add_page_type={{ request.GET.add_page_type }}{% endif %}" method="post" id="page_form">
{% csrf_token %}
{% block form_top %}{% endblock %}
{{ adminForm.fields.parent }}

<input type="hidden" name="language" value="{{language}}" id="id_language"/>

{% if show_language_tabs and not show_permissions %}

    {% for lang_code, lang_name in language_tabs %}
        <input type="button" onclick="CMS.API.changeLanguage('./?language={{lang_code}}');"
            class="language_button {% if lang_code == language %}selected{% else %}{% if not lang_code in filled_languages %} notfilled{% endif %}{% endif %}"
            id="{{lang_code}}button" name="{{lang_code}}" value="{{lang_name}}" />
    {% endfor %}

{% endif %}

{% if is_popup %}<input type="hidden" name="_popup" value="1" />{% endif %}
{% if save_on_top %}{% submit_row %}{% endif %}
{% if errors %}

{% blocktrans count errors|length as counter %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}

{% for error in adminform.form.non_field_errors %}
{{ error }}
{% endfor %}

{% endif %}

{% for fieldset in adminform %}
    {% include "admin/cms/page/includes/fieldset.html" %}
{% endfor %}

{% for inline_admin_formset in inline_admin_formsets %}
    {% include inline_admin_formset.opts.template %}
{% endfor %}

{% if show_permissions %}
    {% if CMS_PERMISSION and can_change_permissions %}

            <fieldset id="inherited_permissions" class="module aligned collapse">

{% trans 'All permissions' %}

{% trans 'Loading...' %}

            </fieldset>

    {% endif %}
{% endif %}

{% block after_related_objects %}{% endblock %}

{% if add %}

        <input type="submit" name="_save" class="default" value="{% trans 'Save' %}" {{ onclick_attrib }}/>
        <input type="submit" name="_continue" value="{% trans 'Save and continue editing' %}" {{ onclick_attrib }}/>

{% else %}
    {% page_submit_row %}
{% endif %}

</form>

{% for url in unihandecode_urls %}{% endfor %}
{% if unihandecode_urls %}

{% endif %}

{# JavaScript for prepopulated fields #}
{% prepopulated_fields_js %}

{% endblock %}


text:
package cmpl

import "github.com/codegangsta/cli"

func MetricCreate(c *cli.Context) {
	GenericMulti(c, []string{`unit`, `description`}, []string{`package`})
}

// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix


text:
Developer Specification for Script Based Extensions
================================================================================

This document summarizes what information should be included in scripts to extend the autograder.

Build
--------------------------------------------------------------------------------

### Behavior

The script should execute a build

### Input

No input is expected

### Output

No output is expected

### Errors

If there is a problem the script should return non-zero.

Clean
--------------------------------------------------------------------------------

### Behavior

The script should remove temporary files and other necessary tasks to prepare
for a new run

### Input

No input is expected

### Output

No output is expected

### Errors

If there is a problem the script should return non-zero.

Update
--------------------------------------------------------------------------------

### Behavior

The script should update the source to the latest version to be tested

### Input

No input is expected

### Output

A json dictionary with the following keys:

+ changed - a boolean representing if there were updates or not

### Errors

If there is a problem the script should return non-zero.

Clone
--------------------------------------------------------------------------------

### Behavior

This script should clone new source code repositories if they do not exist

### Input

No input is expected

### Output

No output is expected

### Errors

If there is a problem the script should return non-zero.

Run
--------------------------------------------------------------------------------

### Behavior

This should run automated unit/integration tests against the source code.  This
is the only code that you may assume will have access to the same file system as
the running tests.

### Input

Arbitrary text

### Output

Arbitrary text

### Errors

This script should set a non-zero return code on exit

Parse
--------------------------------------------------------------------------------

### Behavior

This should parse the stdout of the run command in order to aid in scoring.

### Input

Arbitrary text

### Output

JSON formatted dictionary with the following keys should be outputted to stdout:

+ passed - integer representing the number of tests passed
+ failed - integer representing the number of tests failed
+ skipped - integer representing the number of tests skipped
+ errors - integer representing the number of errors that occurred
+ total - integer representing the sum of the previous 4 keys

### Errors

If there is a problem the script should return non-zero.

Score
--------------------------------------------------------------------------------

### Behavior

This should determine the number of points to assign to a given set of tests

### Input

A JSON dictionary that contains the following structure:

### Output

A JSON dictionary that contains the following keys should be outputted to
stdout:

+ points\_earned - integer that represents the total number of points earned
+ points\_possible - integer that represents the total number of points possible

### Errors

If there is a problem the script should return non-zero.


text:
package org.get.oxicam.clinicalguide.xml;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.get.oxicam.clinicalguide.ClinicalGuideActivity;
import org.get.oxicam.clinicalguide.xml.stats.AbstractStatsQuestion;
import org.get.oxicam.clinicalguide.xml.stats.Stats;
import org.get.oxicam.clinicalguide.xml.stats.StatsQuestionFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

public class CGStatsParser {

	private final Document mDom;
	private Context clinicalGuideContext;
	private String statsName;

	public CGStatsParser(Context context) {
		Document document = null;
		this.clinicalGuideContext = context;

		String filename = "clinicalguidestats.xml";

		InputStream inputStream = XMLHandler.getDecryptedXMLInputStream(context,
				filename);
		if (inputStream != null) {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			try {
				DocumentBuilder db = dbf.newDocumentBuilder();
				document = db.parse(inputStream);
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				try {
					inputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

		if (document == null) {
			Log.e("Parser", "failed loading XML file");
		}
		mDom = document;
	}

	/**
	 * Gets all the stats tags inside the clinicalguidestats.xml
	 *
	 * @return A NodeList containing all form nodes inside clinicalguidestas.xml
	 */
	private NodeList getStats() {
		return mDom.getElementsByTagName("stats");
	}

	/**
	 * Populates the stats using a specific form id.
	 *
	 * @param id The id of the stats to be populated.
	 */
	public String populateStats(String id) {
		Element e = mDom.getElementById(id);
		String retVal = "";
		if (!e.getTagName().equals("stats")) {
			Toast.makeText(clinicalGuideContext,
					"Error! " + id + " is not an id of a <stats> tag",
					Toast.LENGTH_SHORT).show();
			return retVal;
		}

		statsName = ParserHelper.requiredAttributeGetter(e, "name");

		if (statsName != null) {
			Stats stats = new Stats(getQuestions(e), e.getAttribute("name"));
			StatsGenerator sqb = new StatsGenerator(clinicalGuideContext, stats);
			retVal = sqb.getStatsString();
		}

		return retVal;
	}

	private ArrayList<AbstractStatsQuestion> getQuestions(Element e) {
		ArrayList<AbstractStatsQuestion> retVal = new ArrayList<AbstractStatsQuestion>();
		NodeList nl = e.getElementsByTagName("question");
		int len = nl.getLength();
		for (int i = 0; i < len; i++) {
			retVal.add(StatsQuestionFactory.createQuestion((Element) nl.item(i), (ClinicalGuideActivity)clinicalGuideContext));
		}
		return retVal;
	}

	/**
	 * Gets all stats names and its corresponding id that is inside
	 * clinicalguidestats.xml
	 *
	 * @return A HashMap<String,String> containing stats IDs as the keys and form
	 *         names as the values.
	 */
	public HashMap<String, String> getStatsNamesAndIds() {
		NodeList nl = getStats();
		HashMap<String, String> id_name = new LinkedHashMap<String, String>();
		int len = nl.getLength();
		for (int i = 0; i < len; i++) {
			String id = ParserHelper.requiredAttributeGetter((Element) nl.item(i), "id");
			String name = ParserHelper.requiredAttributeGetter((Element) nl.item(i), "name");
			if (id != null && name != null) {
				id_name.put(id, name);
			}
		}
		return id_name;
	}

	/**
	 * Gets the stats ids that is inside clinicalguidestats.xml
	 *
	 * @return Arraylist of String containing the ids of each stats in the clinicalguidestats.xml
	 *         file.
	 */
	public ArrayList<String> getStatsIds() {
		ArrayList<String> arr = new ArrayList<String>();
		NodeList nl = getStats();
		int len = nl.getLength();
		String str = "";
		for (int i = 0; i < len; i++) {
			String id = ParserHelper.requiredAttributeGetter((Element) nl.item(i), "id");
			if (id != null) {
				arr.add(id);
				str += (id + "\n");
			}
		}
		return arr;
	}

	/**
	 * Gets the stats names that is inside clinicalguidestats.xml
	 *
	 * @return Arraylist of String containing the names of each stats in the xml
	 *         file.
	 */
	public ArrayList<String> getStatsNames() {
		ArrayList<String> arr = new ArrayList<String>();
		NodeList nl = getStats();
		int len = nl.getLength();
		for (int i = 0; i < len; i++) {
			String name = ParserHelper.requiredAttributeGetter((Element) nl.item(i), "name");
			if (name != null) {
				arr.add(name);
			}
		}
		return arr;
	}

}


text:
import React from 'react';
import {Col, Form, Modal, Row} from 'react-bootstrap';
import {UseMutationResult} from 'react-query';
import {useTranslation} from 'react-i18next';
import * as yup from 'yup';
import {Formik} from 'formik';

import CancelButton from '../../controllers/Buttons/CancelButton';
import SubmitButton from '../../controllers/Buttons/SubmitButton';
import {UserRole, USER_ROLES} from '../../types/user';

interface UserValues {
    password?: string | number | string[];
    username: string,
    email: string,
    role: UserRole,
}

export type UpdateUserValues = UserValues;

export interface CreateUserValues extends UserValues {
    password: string,
}

interface UserRequest {
    username: string,
    email: string,
    role: UserRole,
}

export type UpdateUserRequest = UserRequest;

export interface CreateUserRequest extends UserRequest {
    password: string,
}

export interface CreateUserResponse {
    user: string,
}

// @ts-ignore
const UpdateSchema: yup.SchemaOf<UserValues> = yup.object()
    .noUnknown()
    .shape(
        {
            username: yup.string()
                .required()
                .max(128),
            email: yup.string()
                .required()
                .max(128)
                .email(),
            role: yup.string()
                .required()
                .max(16)
                .oneOf([...USER_ROLES]),
        }
    );

// @ts-ignore
const CreateSchema = UpdateSchema.shape({
    password: yup.string()
        .required()
        .max(4096),
});

interface UserFromProps<TValues extends object, TData, TRes, TError> {
    createUser: boolean,
    submitBtn?: string | null,
    onCancel?: () => void,
    initialValues: TValues,
    validationSchema?: yup.SchemaOf<any>,
    onSubmit: UseMutationResult<TRes, TError, TData>,
}

function UserForm<TValues extends UserValues, TData extends UserRequest>(
    {
        createUser,
        onSubmit,
        submitBtn,
        onCancel,
        ...rest
    }: UserFromProps<TValues, TData, any, any>
): React.ReactElement {
    const {t} = useTranslation();
    return (
        <Formik
            validationSchema={createUser ? CreateSchema : UpdateSchema}
            onSubmit={((values) => {
                // @ts-ignore
                onSubmit.mutate(values);
            })}
            {...rest}>
            {({
                  handleSubmit,
                  handleChange,
                  values,
                  errors,
                  isSubmitting
              }) => (
                <Form noValidate onSubmit={handleSubmit}>
                    <Modal.Body>
                        <Row className="mb-3">

                            <Form.Group as={Col} md="12" controlId="validationFormik01">
                                <Form.Label>{t('user.username')}</Form.Label>
                                <Form.Control
                                    type="text"
                                    name="username"
                                    value={values.username}
                                    onChange={handleChange}
                                    isInvalid={!!errors.username}/>

                                <Form.Control.Feedback type="invalid">
                                    {errors.username}
                                </Form.Control.Feedback>
                            </Form.Group>

                            <Form.Group as={Col} md="12" controlId="validationFormik02">
                                <Form.Label>{t('user.email')}</Form.Label>
                                <Form.Control
                                    type="email"
                                    name="email"
                                    value={values.email}
                                    onChange={handleChange}
                                    isInvalid={!!errors.email}/>
                                <Form.Control.Feedback type="invalid">
                                    {errors.email}
                                </Form.Control.Feedback>
                            </Form.Group>

                            <Form.Group as={Col} md="12" controlId="validationFormik03">
                                <Form.Label>{t('user.role')}</Form.Label>
                                <Form.Select
                                    name="role"
                                    value={values.role}
                                    onChange={handleChange}
                                    isInvalid={!!errors.role}>
                                    {USER_ROLES.map((r) => {
                                        return (
                                            <option key={r} value={r}>{t(`user.roles.${r}`)}</option>
                                        );
                                    })}
                                </Form.Select>
                                <Form.Control.Feedback type="invalid">
                                    {errors.role}
                                </Form.Control.Feedback>
                            </Form.Group>

                            {createUser && (<Form.Group as={Col} md="12" controlId="validationFormik04">
                                <Form.Label>{t('user.password')}</Form.Label>

                                {/* @ts-ignore */}
                                <Form.Control
                                    type="text"
                                    name="password"
                                    value={values.password}
                                    onChange={handleChange}
                                    isInvalid={!!errors.password}/>

                                <Form.Control.Feedback type="invalid">
                                    {errors.password}
                                </Form.Control.Feedback>
                            </Form.Group>)}

                        </Row>
                    </Modal.Body>
                    <Modal.Footer>
                        <CancelButton  onClick={() => onCancel()}/>

                        <SubmitButton label={submitBtn}
                                      disabled={isSubmitting && !onSubmit.isError}/>
                    </Modal.Footer>
                </Form>
            )}
        </Formik>
    );
}

UserForm.defaultProps = {};

export default UserForm;


text:
from __future__ import absolute_import

from .url import URLValidator

DEFAULT_VALIDATORS = {
    'url': [URLValidator],
}


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Emzi0767.Ada.Extensions;

namespace Emzi0767.Ada.Plugins
{
    internal class AdaPluginManager
    {
        public int PluginCount { get { return this.RegisteredPlugins.Count; } }
        internal IEnumerable<Assembly> PluginAssemblies { get { return this.LoadedAssemblies.Select(xkvp => xkvp.Value); } }
        internal Assembly MainAssembly { get; private set; }
        private Dictionary<string, AdaPlugin> RegisteredPlugins { get; set; }
        private Dictionary<string, Assembly> LoadedAssemblies { get; set; }

        public AdaPluginManager()
        {
            L.W("ADA PLG", "Initializing Plugin manager");
            this.RegisteredPlugins = new Dictionary<string, AdaPlugin>();
            L.W("ADA PLG", "Initializer");
        }

        public void LoadAssemblies()
        {
            L.W("ADA PLG", "Loading all plugin assemblies");
            this.LoadedAssemblies = new Dictionary<string, Assembly>();
            var a = typeof(AdaPlugin).GetTypeInfo().Assembly;
            this.LoadedAssemblies.Add(a.GetName().Name, a);
            this.MainAssembly = a;
            var l = a.Location;
            l = Path.GetDirectoryName(l);

            var r = Path.Combine(l, "references");
            if (Directory.Exists(r))
            {
                var x = Directory.GetFiles(r, "*.dll", SearchOption.TopDirectoryOnly);
                foreach (var xx in x)
                {
                    L.W("ADA PLG", "Loaded reference file '{0}'", xx);
                    var xa = FrameworkAssemblyLoader.LoadFile(xx);
                    this.LoadedAssemblies.Add(xa.GetName().Name, xa);
                }
            }

            l = Path.Combine(l, "plugins");
            if (Directory.Exists(l))
            {
                var x = Directory.GetFiles(l, "*.dll", SearchOption.TopDirectoryOnly);
                foreach (var xx in x)
                {
                    L.W("ADA PLG", "Loaded file '{0}'", xx);
                    var xa = FrameworkAssemblyLoader.LoadFile(xx);
                    this.LoadedAssemblies.Add(xa.GetName().Name, xa);
                }
            }
            L.W("ADA PLG", "Registering plugin dependency resolver");
            FrameworkAssemblyLoader.ResolvingAssembly += ResolvePlugin;
            L.W("ADA PLG", "Done");
        }

        public void Initialize()
        {
            L.W("ADA PLG", "Registering and initializing plugins");
            var @as = this.PluginAssemblies;
            var ts = @as.SelectMany(xa => xa.DefinedTypes);
            var pt = typeof(IAdaPlugin);
            foreach (var t in ts)
            {
                if (!pt.IsAssignableFrom(t.AsType()) || !t.IsClass || t.IsAbstract)
                    continue;

                L.W("ADA PLG", "Type {0} is a plugin", t.ToString());
                var iplg = (IAdaPlugin)Activator.CreateInstance(t.AsType());
                var plg = new AdaPlugin { Plugin = iplg };
                this.RegisteredPlugins.Add(plg.Name, plg);
                L.W("ADA PLG", "Registered plugin '{0}'", plg.Name);
                plg.Plugin.Initialize();
                plg.Plugin.LoadConfig(AdaBotCore.ConfigManager.GetConfig(iplg));
                L.W("ADA PLG", "Plugin '{0}' initialized", plg.Name);
            }
            this.UpdateAllConfigs();
            L.W("ADA PLG", "Registered and initialized {0:#,##0} plugins", this.RegisteredPlugins.Count);
        }

        internal void UpdateAllConfigs()
        {
            foreach (var plg in this.RegisteredPlugins)
            {
                AdaBotCore.ConfigManager.UpdateConfig(plg.Value.Plugin);
            }
        }

        private Assembly ResolvePlugin(string assembly_name)
        {
            if (this.LoadedAssemblies.ContainsKey(assembly_name))
                return this.LoadedAssemblies[assembly_name];
            return null;
        }
    }
}


text:
NAME

dh_python3 - calculates Python dependencies, adds maintainer scripts to byte compile files, etc.

\$1 \n[an-margin] level \n[rst2man-indent-level] level margin: \n[rst2man-indent\n[rst2man-indent-level]] - \n[rst2man-indent0] \n[rst2man-indent1] \n[rst2man-indent2]

SYNOPSIS

dh_python3 -p PACKAGE [-V [X.Y][-][A.B]] DIR [-X REGEXPR]

DESCRIPTION

QUICK GUIDE FOR MAINTAINERS

\(bu 2

if necessary, describe supported Python 3 versions via X-Python3-Version field in debian/control,

\(bu 2

build-depend on python3 or python3-all or python3-all-dev,

\(bu 2

build module/application using its standard build system, remember to build extensions for all supported Python 3 versions (loop over py3versions -vr),

\(bu 2

install files to the standard locations, add --install-layout=deb to setup.py(aqs install command if your package is using distutils,

\(bu 2

add python3 to dh(aqs --with option, or:

\(bu 2

include /usr/share/cdbs/1/class/python-distutils.mk in debian/rules and depend on cdbs (>= 0.4.90), or:

\(bu 2

call dh_python3 in the binary-* target,

\(bu 2

add ${python3:Depends} to Depends

NOTES

dependencies

dh_python3 tries to translate Python dependencies from requires.txt file to Debian dependencies. Use debian/py3dist-overrides or --no-guessing-deps option to override it if the guess is incorrect. If you want dh_python3 to generate more strict dependencies (f.e. to avoid ABI problems) create debian/python3-foo.pydist file. See /usr/share/doc/dh-python/README.PyDist for more information. If the pydist file contains PEP386 flag or set of (uscan like) rules, dh_python3 will make the depedency versioned (version requirements are ignored by default).
private dirs

/usr/share/foo, /usr/share/games/foo, /usr/lib/foo and /usr/lib/games/foo private directories are scanned for Python files by default (where foo is binary package name). If your package is shipping Python files in some other directory, add another dh_python3 call in debian/rules with directory name as an argument - you can use different set of options in this call. If you need to change options (f.e. a list of supported Python 3 versions) for a private directory that is checked by default, invoke dh_python3 with --skip-private option and add another call with a path to this directory and new options.
debug packages

In binary packages which name ends with -dbg, all files in /usr/lib/python3/dist-packages/ directory that have extensions different than so or h are removed by default. Use --no-dbg-cleaning option to disable this feature.
pyinstall files

Files listed in debian/pkg.pyinstall file will be installed as public modules (i.e. into .../dist-packages/ directory) for all requested Python versions.

Syntax: path/to/file [VERSION_RANGE] [NAMESPACE]

debian directory is automatically removed from the path, so you can place your files in debian/ directory and install them from this location (if you want to install them in "debian" namespace, set NAMESPACE to debian). If NAMESPACE is set, all listed files will be installed in .../dist-packages/NAMESPACE/ directory.

Examples:

\(bu 2

foo.py installs .../dist-packages/foo.py for all supported Python versions

\(bu 2

foo/bar.py 3.3- installs .../dist-packages/foo/bar.py for versions >= 3.3

\(bu 2

foo/bar.py spam installs .../dist-packages/spam/bar.py

\(bu 2

debian/*.py spam.egg 3.2 installs .../python3.2/dist-packages/spam/egg/*.py files

pyremove files

If you want to remove some public modules (i.e. files in .../dist-packages/ directory) installed by build system (from all supported Python versions or only from a subset of these versions), add them to debian/pkg.pyremove file.

Examples:

\(bu 2

*.pth removes .pth files from .../dist-packages/

\(bu 2

bar/baz.py 3.2 removes .../python3.2/dist-packages/bar/baz.py

overriding supported / default Python versions

If you want to override system(aqs list of supported Python versions or the default one (f.e. to build a package that includes symlinks for older version of Python or compile .py files only for given interpreter version), you can do that via DEBPYTHON3_SUPPORTED and/or DEBPYTHON3_DEFAULT env. variables.

Example: 3.2,3.3 limits the list of supported Python versions to Python 3.2 and Python 3.3.
OPTIONS

--version show program(aqs version number and exit

-h,\fB  --help show help message and exit

--no-guessing-deps disable guessing dependencies

--no-dbg-cleaning do not remove any files from debug packages

--no-ext-rename do not add magic tags nor multiarch tuples to extension file names

--no-shebang-rewrite do not rewrite shebangs

--skip-private don(aqt check private directories

-v,\fB  --verbose turn verbose mode on

-i,\fB  --indep act on architecture independent packages

-a,\fB  --arch act on architecture dependent packages

-q,\fB  --quiet be quiet

-p \ PACKAGE,\fB \ --package\fB= PACKAGE act on the package named PACKAGE

-N \ NO_PACKAGE,\fB \ --no-package\fB= NO_PACKAGE do not act on the specified package

-V \ VRANGE specify list of supported Python 3 versions. See py3compile(1) for examples

-X \ REGEXPR,\fB \ --exclude\fB= REGEXPR exclude items that match given REGEXPR. You may use this option multiple times to build up a list of things to exclude.

--compile-all compile all files from given private directory in postinst/rtupdate not just the ones provided by the package (i.e. do not pass the --package parameter to py3compile/py3clean)

--depends\fB= DEPENDS translate given requirements into Debian dependencies and add them to ${python3:Depends}. Use it for missing items in requires.txt

--recommends\fB= RECOMMENDS translate given requirements into Debian dependencies and add them to ${python3:Recommends}

--suggests\fB= SUGGESTS translate given requirements into Debian dependencies and add them to ${python3:Suggests}

--requires\fB= FILENAME translate requirements from given file(s) into Debian dependencies and add them to ${python3:Depends}

--shebang\fB= COMMAND use given command as shebang in scripts

--ignore-shebangs do not translate shebangs into Debian dependencies

SEE ALSO

\(bu 2

/usr/share/doc/python/python-policy.txt.gz

\(bu 2

/usr/share/doc/dh-python/README.PyDist

\(bu 2

pybuild(1)

\(bu 2

py3compile(1), py3clean(1)

\(bu 2

dh_python2(1), pycompile(1), pyclean(1)

\(bu 2

%http://deb.li/dhp3 - most recent version of this document

AUTHOR

Piotr Ożarowski, 2012-2013


text:
function Get-ModuleReleaseNotes
{
    param(
        [Parameter(Mandatory=$true)]
        [string]
        # Path to the module's manifest.
        $ManifestPath,

        [Parameter(Mandatory=$true)]
        [string]
        # Path to the releaes notes file.
        $ReleaseNotesPath
    )

    Set-StrictMode -Version 'Latest'

    $Version = Test-ModuleManifest -Path $ManifestPath | Select-Object -ExpandProperty 'Version'
    if( -not $Version )
    {
        Write-Error -Message ('Version not found in module manifest ''{0}''.' -f $ManifestPath)
        return
    }

    $foundVersion = $false
    $versionReleaseNotes = Get-Content -Path $ReleaseNotesPath |
                            Where-Object {
                                $line = $_
                                if( -not $foundVersion )
                                {
                                    if( $line -match ('^#\s+{0}' -f [regex]::Escape($version)) )
                                    {
                                        $foundVersion = $true
                                        return
                                    }
                                }
                                else
                                {
                                    if( $line -match ('^#\s+(?!{0})' -f [regex]::Escape($version)) )
                                    {
                                        $foundVersion = $false
                                    }
                                }
                                return( $foundVersion )
                            }
    if( -not $versionReleaseNotes )
    {
        Write-Error -Message ('There are no release notes for version {0} in ''{1}''.' -f $Version,$ReleaseNotesPath)
        return
    }

    $versionReleaseNotes -join [Environment]::NewLine

}


text:
<?php
namespace Aviogram\InfluxDB\Entity\Admin;

use Aviogram\Common\AbstractEntity;

class User extends AbstractEntity
{
    /**
     * @var string
     */
    protected $name;

    /**
     * @var boolean
     */
    protected $admin;

    /**
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @param string $name
     *
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * @return boolean
     */
    public function isAdmin()
    {
        return $this->admin;
    }

    /**
     * @param boolean $admin
     *
     * @return User
     */
    public function setAdmin($admin)
    {
        $this->admin = $admin;

        return $this;
    }
}


text:
using Microsoft.Kinect;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Media.Imaging;

namespace LightBuzz.Vitruvius.WPF
{
    /// <summary>
    /// Provides some common functionality for manipulating color frames.
    /// </summary>
    public static class ColorExtensions
    {
        #region Members

        /// <summary>
        /// The bitmap source.
        /// </summary>
        static WriteableBitmap _bitmap = null;

        /// <summary>
        /// Frame width.
        /// </summary>
        static int _width;

        /// <summary>
        /// Frame height.
        /// </summary>
        static int _height;

        /// <summary>
        /// The RGB pixel values.
        /// </summary>
        static byte[] _pixels = null;

        #endregion

        #region Public methods

        /// <summary>
        /// Converts a color frame to a System.Media.ImageSource.
        /// </summary>
        /// <param name="frame">A ColorImageFrame generated from a Kinect sensor.</param>
        /// <returns>The specified frame in a System.media.ImageSource format.</returns>
        public static BitmapSource ToBitmap(this ColorImageFrame frame)
        {
            if (_bitmap == null)
            {
                _width = frame.Width;
                _height = frame.Height;
                _pixels = new byte[_width * _height * Constants.BYTES_PER_PIXEL];
                _bitmap = new WriteableBitmap(_width, _height, Constants.DPI, Constants.DPI, Constants.FORMAT, null);
            }

            frame.CopyPixelDataTo(_pixels);

            _bitmap.Lock();

            Marshal.Copy(_pixels, 0, _bitmap.BackBuffer, _pixels.Length);
            _bitmap.AddDirtyRect(new Int32Rect(0, 0, _width, _height));

            _bitmap.Unlock();

            return _bitmap;
        }

        #endregion
    }
}


text:
#define PY_SSIZE_T_CLEAN
#include <Python.h>

#include <stdint.h>

#if defined(__x86_64__) || defined(__amd64__) || defined(_M_IX86) || \
    defined(_M_X64)
#define PLATFORM_IS_X86
#endif

#if defined(_WIN32)
#define PLATFORM_WINDOWS
#endif

// SIMD extension querying is only available on x86.
#ifdef PLATFORM_IS_X86

#ifdef PLATFORM_WINDOWS
#if defined(_MSC_VER)
#include <intrin.h>
#endif

// Visual Studio defines a builtin function for CPUID, so use that if possible.
#define GETCPUID(a, b, c, d, a_inp, c_inp) \
  {                                        \
    int cpu_info[4] = {-1};                \
    __cpuidex(cpu_info, a_inp, c_inp);     \
    a = cpu_info[0];                       \
    b = cpu_info[1];                       \
    c = cpu_info[2];                       \
    d = cpu_info[3];                       \
  }

// Visual Studio defines a builtin function, so use that if possible.
static int GetXCR0EAX() { return _xgetbv(0); }

#else

// Otherwise use gcc-format assembler to implement the underlying instructions.
#define GETCPUID(a, b, c, d, a_inp, c_inp) \
  asm("mov %%rbx, %%rdi\n"                 \
      "cpuid\n"                            \
      "xchg %%rdi, %%rbx\n"                \
      : "=a"(a), "=D"(b), "=c"(c), "=d"(d) \
      : "a"(a_inp), "2"(c_inp))

static int GetXCR0EAX() {
  int eax, edx;
  asm("XGETBV" : "=a"(eax), "=d"(edx) : "c"(0));
  return eax;
}

#endif
#endif

// TODO(phawkins): technically we should build this module without AVX support
// and use configure-time tests instead of __AVX__, since there is a
// possibility that the compiler will use AVX instructions before we reach this
// point.
#ifdef PLATFORM_IS_X86

static void ReportMissingCpuFeature(const char* name) {
  PyErr_Format(
      PyExc_RuntimeError,
      "This version of jaxlib was built using %s instructions, which your "
      "CPU and/or operating system do not support. You may be able work around "
      "this issue by building jaxlib from source.", name);
}

static PyObject *CheckCpuFeatures(PyObject *self, PyObject *args) {
  uint32_t eax, ebx, ecx, edx;

  // To get general information and extended features we send eax = 1 and
  // ecx = 0 to cpuid.  The response is returned in eax, ebx, ecx and edx.
  // (See Intel 64 and IA-32 Architectures Software Developer's Manual
  // Volume 2A: Instruction Set Reference, A-M CPUID).
  GETCPUID(eax, ebx, ecx, edx, 1, 0);
  const uint64_t xcr0_xmm_mask = 0x2;
  const uint64_t xcr0_ymm_mask = 0x4;
  const uint64_t xcr0_avx_mask = xcr0_xmm_mask | xcr0_ymm_mask;
  const _Bool have_avx =
      // Does the OS support XGETBV instruction use by applications?
      ((ecx >> 27) & 0x1) &&
      // Does the OS save/restore XMM and YMM state?
      ((GetXCR0EAX() & xcr0_avx_mask) == xcr0_avx_mask) &&
      // Is AVX supported in hardware?
      ((ecx >> 28) & 0x1);
  const _Bool have_fma = have_avx && ((ecx >> 12) & 0x1);

  // Get standard level 7 structured extension features (issue CPUID with
  // eax = 7 and ecx= 0), which is required to check for AVX2 support as
  // well as other Haswell (and beyond) features.  (See Intel 64 and IA-32
  // Architectures Software Developer's Manual Volume 2A: Instruction Set
  // Reference, A-M CPUID).
  GETCPUID(eax, ebx, ecx, edx, 7, 0);
  const _Bool have_avx2 = have_avx && ((ebx >> 5) & 0x1);

#ifdef __AVX__
  if (!have_avx) {
    ReportMissingCpuFeature("AVX");
    return NULL;
  }
#endif  // __AVX__

#ifdef __AVX2__
  if (!have_avx2) {
    ReportMissingCpuFeature("AVX2");
    return NULL;
  }
#endif  // __AVX2__

#ifdef __FMA__
  if (!have_fma) {
    ReportMissingCpuFeature("FMA");
    return NULL;
  }
#endif  // __FMA__

  Py_INCREF(Py_None);
  return Py_None;
}

#else  // PLATFORM_IS_X86

static PyObject *CheckCpuFeatures(PyObject *self, PyObject *args) {
  Py_INCREF(Py_None);
  return Py_None;
}

#endif  // PLATFORM_IS_X86

static PyMethodDef cpu_feature_guard_methods[] = {
    {"check_cpu_features", CheckCpuFeatures, METH_NOARGS,
     "Throws an exception if the CPU is missing instructions used by jaxlib."},
    {NULL, NULL, 0, NULL}};

static struct PyModuleDef cpu_feature_guard_module = {
    PyModuleDef_HEAD_INIT, "cpu_feature_guard", /* name of module */
    NULL, -1, /* size of per-interpreter state of the module,
                 or -1 if the module keeps state in global variables. */
    cpu_feature_guard_methods};

#if defined(WIN32) || defined(_WIN32)
#define EXPORT_SYMBOL __declspec(dllexport)
#else
#define EXPORT_SYMBOL __attribute__ ((visibility("default")))
#endif

EXPORT_SYMBOL PyMODINIT_FUNC PyInit_cpu_feature_guard(void) {
  return PyModule_Create(&cpu_feature_guard_module);
}


text:
// Gruntfile

var path = require('path');

module.exports = function (grunt) {
    // Read in the replacements file for structuring the sample.html
    var replaceJSON = grunt.file.readJSON('replacements.json'),
        samplePatterns = [],
        tumblrPatterns = [],
        key;

    function getRandomInt(min, max) {
        min = Math.ceil(min);
        max = Math.floor(max);
        return Math.floor(Math.random() * (max - min + 1)) + min;
    }

    /**
     * @desc Parses an input array into a function that returns each element sequentially when called
     *       If the input is anything else, return it unmutated
     * @param {*|Array} input
     * @returns {*|Function}
     */
    function arrayFactory(input) {
        // Check if the replacement is an array of strings
        if (Array.isArray(input)) {
            return (function () {
                // Return an anonymous function with closured values

                var i = 0,
                    strings = input;

                return function () {
                    var string = strings[i++];
                    if (i === strings.length) i = 0;
                    return string;
                };
            })();
        }

        return input;
    }

    function duplicateFactory(options) {
        var countLocation = parseInt(options.count.substr(1), 10),
            contentLocation;

        if (options.content === '$&') {
            contentLocation = 0;
        } else {
            contentLocation = parseInt(options.content.substr(1), 10);
        }

        return function () {
            var countString = arguments[countLocation],
                countSplit = countString.split('-'),
                count,
                content = arguments[contentLocation],
                string = '';

            if (countSplit.length === 1) {
                count = parseInt(countString, 10);
            } else {
                count = getRandomInt(parseInt(countSplit[0], 10), parseInt(countSplit[1], 10));
            }

            for (var i = 0; i < count; i++) {
                string += content;
            }

            return string;
        };
    }

    // Read in the replacement data, importing the match as regex, and using the duplicateFactory replacement
    // match: {Regex}
    // replacement: {Function}
    for (key in replaceJSON.sample.duplicate) {
        samplePatterns.push({
            match: new RegExp(key, 'g'),
            replacement: duplicateFactory(replaceJSON.sample.duplicate[key])
        });
    }

    // Read in the replacement data, importing the match as regex
    // match: {Regex}
    // replacement: {String|Function}
    for (key in replaceJSON.sample.regex) {
        samplePatterns.push({
            match: new RegExp(key, 'g'),
            replacement: arrayFactory(replaceJSON.sample.regex[key])
        });
    }

    // Read in the replacement data structure and convert it into an array of
    // match: {String}
    // replacement: {String|Function}
    for (key in replaceJSON.sample.text) {
        samplePatterns.push({
            match: key,
            replacement: arrayFactory(replaceJSON.sample.text[key])
        });
    }

    // Same as above, but for tumblr replacements
    for (key in replaceJSON.tumblr.text) {
        tumblrPatterns.push({
            match: key,
            replacement: arrayFactory(replaceJSON.tumblr.text[key])
        });
    }

    for (key in replaceJSON.tumblr.regex) {
        tumblrPatterns.push({
            match: new RegExp(key, 'g'),
            replacement: arrayFactory(replaceJSON.tumblr.regex[key])
        });
    }

    var scriptsBuild = require('./src/scripts/build.js');

    function prefixArray(prefix, array) {
        return array.map(function (d) {
            return path.normalize(prefix + d);
        });
    }

    // Load all the included grunt tasks, defined by the init
    require('load-grunt-tasks')(grunt);

    grunt.initConfig({
        pkg: grunt.file.readJSON('package.json'),

        // Configure watched files and tasks to fire
        watch: {
            // Watch for changes to the grunt configuration
            config: {
                files: ['Gruntfile.js', 'replacements.json']
            },

            // Check for changes in any files and run compile
            views: {
                files: ['src/views/**/*.pug', 'replacements.json'],
                tasks: ['views']
            },
            scripts: {
                files: ['src/scripts/**/*.js'],
                tasks: ['scripts']
            },
            styles: {
                files: ['src/styles/**/*.scss'],
                tasks: ['styles']
            }
        },

        notify: {
            sample: {
                options: {
                    title: 'Sample compiled',
                    message: 'sample.html has finished compiling'
                }
            },
            tumblr: {
                options: {
                    title: 'Theme compiled',
                    message: 'theme.html has fininshed compiling'
                }
            },
            publish: {
                options: {
                    title: 'Theme uploaded',
                    message: 'theme.html has been successfully uploaded to Tumblr'
                }
            }
        },

        // Compile pug to html
        pug: {
            views: {
                src: 'src/views/theme.pug',
                dest: '.grunt/temp.html'
            }
        },

        // Lints the Javascript before compilation
        eslint: {
            target: './src/**/*.js'
        },

        // Concatenates and minifies the Javascript
        uglify: {
            scripts: {
                files: {
                    // We use a build file so we can define the order
                    '.grunt/theme.min.js': prefixArray('src/scripts/', scriptsBuild.concat)
                }
            }
        },

        // Compile sass to css
        sass: {
            options: {
                quiet: false,
                outputStyle: 'compressed'
            },
            styles: {
                files: {
                    '.grunt/theme.css': 'src/styles/theme.scss'
                }
            }
        },

        // Post process CSS to add vendor prefixes
        postcss: {
            options: {
                processors: [
                    require('autoprefixer')({
                        browsers: 'last 2 versions'
                    })
                ]
            },
            styles: {
                src: '.grunt/theme.css'
            }
        },

        // Create the distibutable copies
        copy: {
            compiled: {
                src: '.grunt/temp.html',
                dest: '.grunt/theme.html'
            }
        },

        // Insert all the JS and CSS into the single html file
        insert: {
            scripts: {
                src: '.grunt/theme.min.js',
                dest: '.grunt/theme.html',
                match: '<!-- @import scripts-->'
            },
            styles: {
                src: '.grunt/theme.css',
                dest: '.grunt/theme.html',
                match: '<!-- @import styles-->'
            }
        },

        // Replace all the tags
        // sample replaces comment tags with sample content
        // tumblr removes the comment wrapping from tags
        // This is the last operation, so it puts the file in the final location
        replace: {
            sample: {
                options: {
                    usePrefix: false,
                    preserveOrder: true,
                    // Use the constructed patterns array for replacements
                    patterns: samplePatterns
                },
                src: '.grunt/theme.html',
                dest: 'dist/sample.html'
            },
            tumblr: {
                options: {
                    usePrefix: false,
                    preserveOrder: true,
                    patterns: tumblrPatterns
                },
                src: '.grunt/theme.html',
                dest: 'dist/theme.html'
            }
        },

        // Send the latest theme to Tumblr
        http: {
            postTumblr: {
                options: {
                    url: 'https://www.tumblr.com/customize_api/blog/tremblrtheme',
                    headers: {
                        "Cookie": "rxx=1xcxwccjk91.1g0vbf4&v=1; tmgioct=596997d8cf03d20576528200; anon_id=MUHHYGSHVXYWGKZZJYZLFBUNIUMESLUZ; logged_in=1; use_own_posts=1; nts=true; capture=cnzOgtunXPuMvf6PGb48RReeIRI; language=%2Cen_US; pfp=W1vym2kQDME7VF1oDWW3tqomisU0evn40PLdrVs3; pfs=cmZBl40YfEpKLBgrFltyHD2t5U; pfe=1507871622; pfu=38855025; pfx=1c2fc9024519ac291b0dead44cd2a9ebc8f2a7267b955df6cc9236adbbe491dc%230%234271846327; devicePixelRatio=1; documentWidth=1903; __utma=189990958.1533710215.1406201325.1501922164.1501924784.89; __utmc=189990958; __utmz=189990958.1500725688.77.57.utmcsr=google|utmccn=(organic)|utmcmd=organic|utmctr=(not%20provided); _ga=GA1.2.1533710215.1406201325; _gid=GA1.2.1161574138.1501906587; yx=4n4816g9fsscq%26o%3D3%26f%3Dze; customize_word_wrap=1"
                    },
                    method: 'POST',
                    json: true,
                    body: {
                        "custom_css": "",
                        "description": "This is a blog to show off the new Tremblr theme",
                        "enable_url_slugs": 1,
                        "name": "tremblrtheme",
                        "open_links_in_new_window": 0,
                        "posts_per_page": "15",
                        "theme_id": 0,
                        "title": "Tremblr Theme",
                        "truncate_feed": 0,
                        "global_params": {
                            "avatar_shape": "square",
                            "background_color": "#FAFAFA",
                            "body_font": "Helvetica Neue",
                            "header_bounds": 0,
                            "header_image": "http://assets.tumblr.com/images/default_header/optica_pattern_02.png?_v=b976ee00195b1b7806c94ae285ca46a7",
                            "header_image_dimens": "",
                            "header_image_focused": "http://assets.tumblr.com/images/default_header/optica_pattern_02.png?_v=b976ee00195b1b7806c94ae285ca46a7",
                            "header_image_scaled": "http://assets.tumblr.com/images/default_header/optica_pattern_02.png?_v=b976ee00195b1b7806c94ae285ca46a7",
                            "header_stretch": true,
                            "link_color": "#529ECC",
                            "show_avatar": true,
                            "show_description": true,
                            "show_header_image": true,
                            "show_title": true,
                            "title_color": "#444444",
                            "title_font": "Gibson",
                            "title_font_weight": "bold"
                        },
                        "custom_params": {
                            "if:Endless scrolling": true,
                            "if:Related Posts": true,
                            "if:Show navigation": true,
                            "if:Sliding header": true,
                            "if:Syntax highlighting": false,
                            "select:Layout": "regular",
                            "text:Disqus shortname": "",
                            "text:Google analytics ID": ""
                        },
                        "custom_theme": grunt.file.read('dist/theme.html'),
                        "purchased_theme_ids": ["39927"],
                        "enable_mobile_interface": false,
                        "enable_google_amp": true,
                        "brag": true,
                        "url": "https://tremblrtheme.tumblr.com/",
                        "theme_author_prompt": false,
                        "PortraitURL-16": "https://assets.tumblr.com/images/default_avatar/cube_closed_16.png",
                        "PortraitURL-24": "https://assets.tumblr.com/images/default_avatar/cube_closed_24.png",
                        "PortraitURL-30": "https://assets.tumblr.com/images/default_avatar/cube_closed_30.png",
                        "PortraitURL-40": "https://assets.tumblr.com/images/default_avatar/cube_closed_40.png",
                        "PortraitURL-48": "https://assets.tumblr.com/images/default_avatar/cube_closed_48.png",
                        "PortraitURL-64": "https://assets.tumblr.com/images/default_avatar/cube_closed_64.png",
                        "PortraitURL-96": "https://assets.tumblr.com/images/default_avatar/cube_closed_96.png",
                        "PortraitURL-128": "https://assets.tumblr.com/images/default_avatar/cube_closed_128.png",
                        "avatar_url": "https://assets.tumblr.com/images/default_avatar/cube_closed_128.png",
                        "id": "tremblrtheme",
                        "user_form_key": "cnzOgtunXPuMvf6PGb48RReeIRI",
                        "secure_form_key": "!331501925758|uXhYGCJ51IhCh0NZR3EM8SdxM"
                    }
                }
            }
        }
    });

    grunt.registerTask('default', ['watch']);

    grunt.registerTask('views', ['pug', 'prepare-sample']);
    grunt.registerTask('scripts', ['eslint', 'uglify', 'prepare-sample']);
    grunt.registerTask('styles', ['sass', 'postcss', 'prepare-sample']);

    grunt.registerTask('prepare-sample', ['copy', 'insert', 'replace:sample', 'notify:sample']);
    grunt.registerTask('prepare-tumblr', ['copy', 'insert', 'replace:tumblr', 'notify:tumblr']);
    grunt.registerTask('prepare-publish', ['copy', 'insert', 'replace:tumblr', 'http', 'notify:publish']);

    // Compile to create a locally viewable version
    // Tumblr to create a a Tumblr-ready version
    // Publish to upload the Tumblr-ready version directly to Tumblr
    grunt.registerTask('compile', ['pug', 'eslint', 'uglify', 'sass', 'postcss', 'prepare-sample']);
    grunt.registerTask('tumblr', ['pug', 'eslint', 'uglify', 'sass', 'postcss', 'prepare-tumblr']);
    grunt.registerTask('publish', ['pug', 'eslint', 'uglify', 'sass', 'postcss', 'prepare-publish']);
};


text:
import * as path from 'path';

export const Path = {
    root: (...paths: string[]) => path.join(__dirname, '..', '..', ...paths),
    current: (...paths: string[]) => path.join(__dirname, '.', ...paths),
    relative: (...paths: string[]) => path.join(...paths),
};


text:
package graphene.web.annotations;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
 * Security annotation for Tapestry Pages. Enables anonymous access to pages, so
 * the user does not have to be logged in.
 *
 * @version 1.0
 */
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface AnonymousAccess {

}


text:
package org.wildfly.extension.camel.config;

public interface ConfigPlugin {

    void applyDomainConfigChange(ConfigContext context, boolean enable);

    void applyStandaloneConfigChange(ConfigContext context, boolean enable);

}


text:
<!DOCTYPE html>

  <meta charset="utf-8">
	<title>SCORE</title>

	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
	<link rel="icon" type="image/png" sizes="174x174" href="./style/favicon.png">

	<link rel="stylesheet" type="text/css" href="./style/examples.css">

Tone.Transport

			Tone.Transport
			is the application-wide timekeeper. It uses an OscillatorNode
			as it's clock source which enables sample-accurate scheduling as well as
			tempo-curves and automation. This example uses Tone.Transport.setInterval
			to invoke a callback every 16th note.

		<canvas nx="matrix"></canvas>


text:
Generate thumbs

Access Forbiden

		<button md-button (click)="genThumbs()">
			GENERATE THUMBS
		</button>

		<md-list>
			<md-list-item *ngFor="let photoThumb of listPhotoThumb">

				<md-icon md-list-icon
					[color]="photoThumb.thumbExist ? 'primary' : 'accent'"
					[innerHTML]="photoThumb.thumbExist ? 'check_circle' : 'block'">
				</md-icon>
			</md-list-item>
		</md-list>


text:
package com.opdar.gulosity.serializable;

import com.opdar.gulosity.event.base.BinlogEvent;
import com.opdar.gulosity.utils.BufferUtils;

import java.nio.ByteBuffer;

/**
 * Created by Shey on 2016/8/27.
 */
public class NumberSerializable extends JavaSerializable<Number> {

    public NumberSerializable(int type) {
        super(type);
    }

    @Override
    public Number getValue(int meta, ByteBuffer buffer) {
        if (type == BinlogEvent.TINY) {
            return buffer.get();
        }
        if (type == BinlogEvent.SHORT) {
            return buffer.getShort();
        }
        if (type == BinlogEvent.INT24) {
            return BufferUtils.readLong(buffer, 3);
        }
        if (type == BinlogEvent.LONGLONG) {
            return buffer.getLong();
        }
        if (type == BinlogEvent.FLOAT) {
            return buffer.getFloat();
        }
        if (type == BinlogEvent.DOUBLE) {
            return buffer.getDouble();
        }
        return buffer.getInt();
    }
}


text:
package com.amazonaws.services.iot.model.transform;

import java.math.*;

import javax.annotation.Generated;

import com.amazonaws.services.iot.model.*;
import com.amazonaws.transform.SimpleTypeJsonUnmarshallers.*;
import com.amazonaws.transform.*;

import static com.fasterxml.jackson.core.JsonToken.*;

/**
 * SetV2LoggingLevelResult JSON Unmarshaller
 */
@Generated("com.amazonaws:aws-java-sdk-code-generator")
public class SetV2LoggingLevelResultJsonUnmarshaller implements Unmarshaller<SetV2LoggingLevelResult, JsonUnmarshallerContext> {

    public SetV2LoggingLevelResult unmarshall(JsonUnmarshallerContext context) throws Exception {
        SetV2LoggingLevelResult setV2LoggingLevelResult = new SetV2LoggingLevelResult();

        return setV2LoggingLevelResult;
    }

    private static SetV2LoggingLevelResultJsonUnmarshaller instance;

    public static SetV2LoggingLevelResultJsonUnmarshaller getInstance() {
        if (instance == null)
            instance = new SetV2LoggingLevelResultJsonUnmarshaller();
        return instance;
    }
}