text:
Given the [deprecation of `stable`](https://github.com/helm/charts#deprecation-timeline), future CockroachDB Helm charts are now located at [cockroachdb/charts](https://github.com/cockroachdb/helm-charts/).

The CockroachDB charts repository is already included in the Hubs. Updated installation instructions are included in the new [source code repository README](https://github.com/cockroachdb/helm-charts/blob/master/README.md).

To update an exisiting _stable_ deployment with a chart hosted in the CockroachDB repository, you can run:

```bash
$ helm repo add cockroachdb https://charts.cockroachdb.com/
$ helm upgrade my-release cockroachdb/cockroachdb
```

## Documentation

Below is a brief overview of operating the CockroachDB Helm Chart and some specific implementation details. For additional information on deploying CockroachDB, please see:
> https://www.cockroachlabs.com/docs/stable/orchestrate-cockroachdb-with-kubernetes.html

Note that the documentation requires Helm 3.0 or higher.

## Prerequisites Details

* Kubernetes 1.8
* PV support on the underlying infrastructure (only if using `storage.persistentVolume`). [Docker for windows hostpath provisioner is not supported](https://github.com/cockroachdb/docs/issues/3184).
* If you want to secure your cluster to use TLS certificates for all network communication, [Helm must be installed with RBAC privileges](https://github.com/kubernetes/helm/blob/master/docs/rbac.md) or else you will get an "attempt to grant extra privileges" error.

## StatefulSet Details

* http://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/

## StatefulSet Caveats

* http://kubernetes.io/docs/concepts/abstractions/controllers/statefulsets/#limitations

## Chart Details

This chart will do the following:

* Set up a dynamically scalable CockroachDB cluster using a Kubernetes StatefulSet.

## Installing the Chart

To install the chart with the release name `my-release`:

```shell
helm install my-release stable/cockroachdb
```

Note that for a production cluster, you will likely want to override the following parameters in [`values.yaml`](values.yaml) with your own values.

- `statefulset.resources.requests.memory` and `statefulset.resources.limits.memory` allocate memory resources to CockroachDB pods in your cluster.
- `conf.cache` and `conf.max-sql-memory` are memory limits that we recommend setting to 1/4 of the above resource allocation. When running CockroachDB, you must set these limits explicitly to avoid running out of memory.
- `storage.persistentVolume.size` defaults to `100Gi` of disk space per pod, which you may increase or decrease for your use case.
- `storage.persistentVolume.storageClass` uses the default storage class for your environment. We strongly recommend that you specify a storage class which uses an SSD.
- `tls.enabled` must be set to `yes`/`true` to deploy in secure mode.

For more information on overriding the `values.yaml` parameters, please see:
> https://www.cockroachlabs.com/docs/stable/orchestrate-cockroachdb-with-kubernetes.html#step-2-start-cockroachdb

If you are running in secure mode (with configuration parameter `tls.enabled` set to `yes`/`true`) and `tls.certs.provided` set to `no`/`false`), then you will have to manually approve the cluster's security certificates as the pods are created. You can see the pending CSRs (certificate signing requests) by running `kubectl get csr`, and approve them by running `kubectl certificate approve <csr-name>`. You'll have to approve one certificate for each CockroachDB node (e.g., `default.node.my-release-cockroachdb-0` and one client certificate for the job that initializes the cluster (e.g., `default.node.root`).

When `tls.certs.provided` is set to `yes`/`true`, this chart will use certificates created outside of Kubernetes. You may want to use this if you want to use a different certificate authority from the one being used by Kubernetes or if your Kubernetes cluster doesn't fully support certificate-signing requests. To use this, first set up your certificates and load them into your Kubernetes cluster as Secrets using the commands below:
```
mkdir certs
mkdir my-safe-directory
cockroach cert create-ca --certs-dir=certs --ca-key=my-safe-directory/ca.key
cockroach cert create-client root --certs-dir=certs --ca-key=my-safe-directory/ca.key
kubectl create secret generic cockroachdb-root --from-file=certs
cockroach cert create-node --certs-dir=certs --ca-key=my-safe-directory/ca.key localhost 127.0.0.1 eerie-horse-cockroachdb-public eerie-horse-cockroachdb-public.default eerie-horse-cockroachdb-public.default.svc.cluster.local *.eerie-horse-cockroachdb *.eerie-horse-cockroachdb.default *.eerie-horse-cockroachdb.default.svc.cluster.local
kubectl create secret generic cockroachdb-node --from-file=certs
```

Set `tls.certs.tlsSecret` to `yes/true` if you make use of [cert-manager][3] in your cluster.

[cert-manager][3] stores generated certificates in dedicated TLS secrets. Thus, they are always named:
* `ca.crt`
* `tls.crt`
* `tls.key`

On the other hand, CockroachDB also demands dedicated certificate filenames:
* `ca.crt`
* `node.crt`
* `node.key`
* `client.root.crt`
* `client.root.key`

By activating `tls.certs.tlsSecret` we benefit from projected secrets and convert the TLS secret filenames to their according CockroachDB filenames.

If you are running in secure mode, then you will have to manually approve the cluster's security certificates as the pods are created. You can see the pending CSRs (certificate signing requests) by running `kubectl get csr`, and approve them by running `kubectl certificate approve <csr-name>`. You'll have to approve one certificate for each CockroachDB node (e.g., `default.node.my-release-cockroachdb-0` and one client certificate for the job that initializes the cluster (e.g., `default.node.root`).

Confirm that all pods are `Running` successfully and init has been completed:
```shell
kubectl get pods
```
```
NAME                                READY     STATUS      RESTARTS   AGE
my-release-cockroachdb-0            1/1       Running     0          1m
my-release-cockroachdb-1            1/1       Running     0          1m
my-release-cockroachdb-2            1/1       Running     0          1m
my-release-cockroachdb-init-k6jcr   0/1       Completed   0          1m
```

Confirm that persistent volumes are created and claimed for each pod:
```shell
kubectl get pv
```
```
NAME                                       CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM                                      STORAGECLASS   REASON    AGE
pvc-64878ebf-f3f0-11e8-ab5b-42010a8e0035   100Gi      RWO            Delete           Bound     default/datadir-my-release-cockroachdb-0   standard                 51s
pvc-64945b4f-f3f0-11e8-ab5b-42010a8e0035   100Gi      RWO            Delete           Bound     default/datadir-my-release-cockroachdb-1   standard                 51s
pvc-649d920d-f3f0-11e8-ab5b-42010a8e0035   100Gi      RWO            Delete           Bound     default/datadir-my-release-cockroachdb-2   standard                 51s
```

## Upgrading the cluster

### Chart version 3.0.0 and after

Launch a temporary interactive pod and start the built-in SQL client:
```shell
kubectl run cockroachdb --rm -it \
--image=cockroachdb/cockroach \
--restart=Never \
-- sql --insecure --host=my-release-cockroachdb-public
```

> If you are running in secure mode, you will have to provide a client certificate to the cluster in order to authenticate, so the above command will not work. See [here](https://github.com/cockroachdb/cockroach/blob/master/cloud/kubernetes/client-secure.yaml) for an example of how to set up an interactive SQL shell against a secure cluster or [here](https://github.com/cockroachdb/cockroach/blob/master/cloud/kubernetes/example-app-secure.yaml) for an example application connecting to a secure cluster.

Set `cluster.preserve_downgrade_option`, where `$current_version` is the CockroachDB version currently running (e.g., `19.2`):
```sql
> SET CLUSTER SETTING cluster.preserve_downgrade_option = '$current_version';
```

Exit the shell and delete the temporary pod:
```sql
> \q
```

Kick off the upgrade process by changing the new Docker image, where `$new_version` is the CockroachDB version to which you are upgrading:
```shell
kubectl delete job my-release-cockroachdb-init
```
```shell
helm upgrade my-release stable/cockroachdb \
--set image.tag=$new_version \
--reuse-values
```

Kubernetes will carry out a safe [rolling upgrade](https://kubernetes.io/docs/tutorials/stateful-application/basic-stateful-set/#updating-statefulsets) of your CockroachDB nodes one-by-one. Monitor the cluster's pods until all have been successfully restarted:
```shell
kubectl get pods
```
```
NAME                                READY     STATUS              RESTARTS   AGE
my-release-cockroachdb-0            1/1       Running             0          2m
my-release-cockroachdb-1            1/1       Running             0          3m
my-release-cockroachdb-2            1/1       Running             0          3m
my-release-cockroachdb-3            0/1       ContainerCreating   0          25s
my-release-cockroachdb-init-nwjkh   0/1       ContainerCreating   0          6s
```
```shell
kubectl get pods \
-o jsonpath='{range .items[*]}{.metadata.name}{"\t"}{.spec.containers[0].image}{"\n"}'
```
```
my-release-cockroachdb-0    cockroachdb/cockroach:v19.2.5
my-release-cockroachdb-1    cockroachdb/cockroach:v19.2.5
my-release-cockroachdb-2    cockroachdb/cockroach:v19.2.5
my-release-cockroachdb-3    cockroachdb/cockroach:v19.2.5
```

Resume normal operations. Once you are comfortable that the stability and performance of the cluster is what you'd expect post-upgrade, finalize the upgrade:
```shell
kubectl run cockroachdb --rm -it \
--image=cockroachdb/cockroach \
--restart=Never \
-- sql --insecure --host=my-release-cockroachdb-public
```
```sql
> RESET CLUSTER SETTING cluster.preserve_downgrade_option;
> \q
```

### Chart versions prior to 3.0.0

Due to a change in the label format in version 3.0.0 of this chart, upgrading requires that you delete the StatefulSet. Luckily there is a way to do it without actually deleting all the resources managed by the StatefulSet. Use the workaround below to upgrade from charts versions previous to 3.0.0:

Get the new labels from the specs rendered by Helm:
```shell
helm template -f deploy.vals.yml stable/cockroachdb -x templates/statefulset.yaml \
| yq r - spec.template.metadata.labels
```
```
app.kubernetes.io/name: cockroachdb
app.kubernetes.io/instance: my-release
app.kubernetes.io/component: cockroachdb
```

Place the new labels on all pods of the StatefulSet (change `my-release-cockroachdb-0` to the name of each pod):
```shell
kubectl label pods my-release-cockroachdb-0 \
app.kubernetes.io/name=cockroachdb \
app.kubernetes.io/instance=my-release \
app.kubernetes.io/component=cockroachdb
```

Delete the StatefulSet without deleting pods:
```shell
kubectl delete statefulset my-release-cockroachdb --cascade=false
```

Verify that no pod is deleted and then upgrade as normal. A new StatefulSet will be created, taking over the management of the existing pods and upgrading them if needed.

## Configuration

The following table lists the configurable parameters of the CockroachDB chart and their default values.
For details see the [`values.yaml`](values.yaml) file.

| Parameter                                | Description                                                     | Default                                          |
| ---------                                | -----------                                                     | -------                                          |
| `clusterDomain`                          | Cluster's default DNS domain                                    | `cluster.local`                                  |
| `conf.attrs`                             | CockroachDB node attributes                                     | `[]`                                             |
| `conf.cache`                             | Size of CockroachDB's in-memory cache                           | `25%`                                            |
| `conf.cluster-name`                      | Name of CockroachDB cluster                                     | `""`                                             |
| `conf.disable-cluster-name-verification` | Disable CockroachDB cluster name verification                   | `no`                                             |
| `conf.join`                              | List of already-existing CockroachDB instances                  | `[]`                                             |
| `conf.max-disk-temp-storage`             | Max storage capacity for temp data                              | `0`                                              |
| `conf.max-offset`                        | Max allowed clock offset for CockroachDB cluster                | `500ms`                                          |
| `conf.max-sql-memory`                    | Max memory to use processing SQL querie                         | `25%`                                            |
| `conf.locality`                          | Locality attribute for this deployment                          | `""`                                             |
| `conf.single-node`                       | Disable CockroachDB clustering (standalone mode)                | `no`                                             |
| `conf.sql-audit-dir`                     | Directory for SQL audit log                                     | `""`                                             |
| `conf.port`                              | CockroachDB primary serving port in Pods                        | `26257`                                          |
| `conf.http-port`                         | CockroachDB HTTP port in Pods                                   | `8080`                                           |
| `image.repository`                       | Container image name                                            | `cockroachdb/cockroach`                          |
| `image.tag`                              | Container image tag                                             | `v19.2.5`                                        |
| `image.pullPolicy`                       | Container pull policy                                           | `IfNotPresent`                                   |
| `image.credentials`                      | `registry`, `user` and `pass` credentials to pull private image | `{}`                                             |
| `statefulset.replicas`                   | StatefulSet replicas number                                     | `3`                                              |
| `statefulset.updateStrategy`             | Update strategy for StatefulSet Pods                            | `{"type": "RollingUpdate"}`                      |
| `statefulset.podManagementPolicy`        | `OrderedReady`/`Parallel` Pods creation/deletion order          | `Parallel`                                       |
| `statefulset.budget.maxUnavailable`      | k8s PodDisruptionBudget parameter                               | `1`                                              |
| `statefulset.args`                       | Extra command-line arguments                                    | `[]`                                             |
| `statefulset.env`                        | Extra env vars                                                  | `[]`                                             |
| `statefulset.secretMounts`               | Additional Secrets to mount at cluster members                  | `[]`                                             |
| `statefulset.labels`                     | Additional labels of StatefulSet and its Pods                   | `{"app.kubernetes.io/component": "cockroachdb"}` |
| `statefulset.annotations`                | Additional annotations of StatefulSet Pods                      | `{}`                                             |
| `statefulset.nodeAffinity`               | [Node affinity rules][2] of StatefulSet Pods                    | `{}`                                             |
| `statefulset.podAffinity`                | [Inter-Pod affinity rules][1] of StatefulSet Pods               | `{}`                                             |
| `statefulset.podAntiAffinity`            | [Anti-affinity rules][1] of StatefulSet Pods                    | auto                                             |
| `statefulset.podAntiAffinity.type`       | Type of auto [anti-affinity rules][1]                           | `soft`                                           |
| `statefulset.podAntiAffinity.weight`     | Weight for `soft` auto [anti-affinity rules][1]                 | `100`                                            |
| `statefulset.nodeSelector`               | Node labels for StatefulSet Pods assignment                     | `{}`                                             |
| `statefulset.tolerations`                | Node taints to tolerate by StatefulSet Pods                     | `[]`                                             |
| `statefulset.resources`                  | Resource requests and limits for StatefulSet Pods               | `{}`                                             |
| `service.ports.grpc.external.port`       | CockroachDB primary serving port in Services                    | `26257`                                          |
| `service.ports.grpc.external.name`       | CockroachDB primary serving port name in Services               | `grpc`                                           |
| `service.ports.grpc.internal.port`       | CockroachDB inter-communication port in Services                | `26257`                                          |
| `service.ports.grpc.internal.name`       | CockroachDB inter-communication port name in Services           | `grpc-internal`                                  |
| `service.ports.http.port`                | CockroachDB HTTP port in Services                               | `8080`                                           |
| `service.ports.http.name`                | CockroachDB HTTP port name in Services                          | `http`                                           |
| `service.public.type`                    | Public Service type                                             | `ClusterIP`                                      |
| `service.public.labels`                  | Additional labels of public Service                             | `{"app.kubernetes.io/component": "cockroachdb"}` |
| `service.public.annotations`             | Additional annotations of public Service                        | `{}`                                             |
| `service.discovery.labels`               | Additional labels of discovery Service                          | `{"app.kubernetes.io/component": "cockroachdb"}` |
| `service.discovery.annotations`          | Additional annotations of discovery Service                     | `{}`                                             |
| `storage.hostPath`                       | Absolute path on host to store data                             | `""`                                             |
| `storage.persistentVolume.enabled`       | Whether to use PersistentVolume to store data                   | `yes`                                            |
| `storage.persistentVolume.size`          | PersistentVolume size                                           | `100Gi`                                          |
| `storage.persistentVolume.storageClass`  | PersistentVolume class                                          | `""`                                             |
| `storage.persistentVolume.labels`        | Additional labels of PersistentVolumeClaim                      | `{}`                                             |
| `storage.persistentVolume.annotations`   | Additional annotations of PersistentVolumeClaim                 | `{}`                                             |
| `init.labels`                            | Additional labels of init Job and its Pod                       | `{"app.kubernetes.io/component": "init"}`        |
| `init.annotations`                       | Additional labels of the Pod of init Job                        | `{}`                                             |
| `init.affinity`                          | [Affinity rules][2] of init Job Pod                             | `{}`                                             |
| `init.nodeSelector`                      | Node labels for init Job Pod assignment                         | `{}`                                             |
| `init.tolerations`                       | Node taints to tolerate by init Job Pod                         | `[]`                                             |
| `init.resources`                         | Resource requests and limits for the Pod of init Job            | `{}`                                             |
| `tls.enabled`                            | Whether to run securely using TLS certificates                  | `no`                                             |
| `tls.serviceAccount.create`              | Whether to create a new RBAC service account                    | `yes`                                            |
| `tls.serviceAccount.name`                | Name of RBAC service account to use                             | `""`                                             |
| `tls.certs.provided`                     | Bring your own certs scenario, i.e certificates are provided    | `no`                                             |
| `tls.certs.clientRootSecret`             | If certs are provided, secret name for client root cert         | `cockroachdb-root`                               |
| `tls.certs.nodeSecret`                   | If certs are provided, secret name for node cert                | `cockroachdb-node`                               |
| `tls.certs.tlsSecret`                    | Own certs are stored in TLS secret                              | `no`                                             |
| `tls.init.image.repository`              | Image to use for requesting TLS certificates                    | `cockroachdb/cockroach-k8s-request-cert`         |
| `tls.init.image.tag`                     | Image tag to use for requesting TLS certificates                | `0.4`                                            |
| `tls.init.image.pullPolicy`              | Requesting TLS certificates container pull policy               | `IfNotPresent`                                   |
| `tls.init.image.credentials`             | `registry`, `user` and `pass` credentials to pull private image | `{}`                                             |
| `networkPolicy.enabled`                  | Enable NetworkPolicy for CockroachDB's Pods                     | `no`                                             |
| `networkPolicy.ingress.grpc`             | Whitelist resources to access gRPC port of CockroachDB's Pods   | `[]`                                             |
| `networkPolicy.ingress.http`             | Whitelist resources to access gRPC port of CockroachDB's Pods   | `[]`                                             |

Override the default parameters using the `--set key=value[,key=value]` argument to `helm install`.

Alternatively, a YAML file that specifies custom values for the parameters can be provided while installing the chart. For example:
```shell
helm install my-release -f my-values.yaml stable/cockroachdb
```

> **Tip**: You can use the default [values.yaml](values.yaml)

## Deep dive

### Connecting to the CockroachDB cluster

Once you've created the cluster, you can start talking to it by connecting to its `-public` Service. CockroachDB is PostgreSQL wire protocol compatible, so there's a [wide variety of supported clients](https://www.cockroachlabs.com/docs/install-client-drivers.html). As an example, we'll open up a SQL shell using CockroachDB's built-in shell and play around with it a bit, like this (likely needing to replace `my-release-cockroachdb-public` with the name of the `-public` Service that was created with your installed chart):
```shell
kubectl run cockroach-client --rm -it \
--image=cockroachdb/cockroach \
--restart=Never \
-- sql --insecure --host my-release-cockroachdb-public
```
```
Waiting for pod default/cockroach-client to be running, status is Pending,
pod ready: false
If you don't see a command prompt, try pressing enter.
root@my-release-cockroachdb-public:26257> SHOW DATABASES;
+--------------------+
|      Database      |
+--------------------+
| information_schema |
| pg_catalog         |
| system             |
+--------------------+
(3 rows)
root@my-release-cockroachdb-public:26257> CREATE DATABASE bank;
CREATE DATABASE
root@my-release-cockroachdb-public:26257> CREATE TABLE bank.accounts (id INT
PRIMARY KEY, balance DECIMAL);
CREATE TABLE
root@my-release-cockroachdb-public:26257> INSERT INTO bank.accounts VALUES
(1234, 10000.50);
INSERT 1
root@my-release-cockroachdb-public:26257> SELECT * FROM bank.accounts;
+------+---------+
|  id  | balance |
+------+---------+
| 1234 | 10000.5 |
+------+---------+
(1 row)
root@my-release-cockroachdb-public:26257> \q
Waiting for pod default/cockroach-client to terminate, status is Running
pod "cockroach-client" deleted
```

> If you are running in secure mode, you will have to provide a client certificate to the cluster in order to authenticate, so the above command will not work. See [here](https://github.com/cockroachdb/cockroach/blob/master/cloud/kubernetes/client-secure.yaml) for an example of how to set up an interactive SQL shell against a secure cluster or [here](https://github.com/cockroachdb/cockroach/blob/master/cloud/kubernetes/example-app-secure.yaml) for an example application connecting to a secure cluster.

### Cluster health

Because our pod spec includes regular health checks of the CockroachDB processes, simply running `kubectl get pods` and looking at the `STATUS` column is sufficient to determine the health of each instance in the cluster.

If you want more detailed information about the cluster, the best place to look is the Admin UI.

### Accessing the Admin UI

If you want to see information about how the cluster is doing, you can try pulling up the CockroachDB Admin UI by port-forwarding from your local machine to one of the pods (replacing `my-release-cockroachdb-0` with the name of one of your pods:
```shell
kubectl port-forward my-release-cockroachdb-0 8080
```

You should then be able to access the Admin UI by visiting http://localhost:8080/ in your web browser.

### Failover

If any CockroachDB member fails, it is restarted or recreated automatically by the Kubernetes infrastructure, and will re-join the cluster automatically when it comes back up. You can test this scenario by killing any of the CockroachDB pods:
```shell
kubectl delete pod my-release-cockroachdb-1
```
```shell
kubectl get pods -l "app.kubernetes.io/instance=my-release,app.kubernetes.io/component=cockroachdb"
```
```
NAME                      READY     STATUS        RESTARTS   AGE
my-release-cockroachdb-0  1/1       Running       0          5m
my-release-cockroachdb-2  1/1       Running       0          5m
```

After a while:
```shell
kubectl get pods -l "app.kubernetes.io/instance=my-release,app.kubernetes.io/component=cockroachdb"
```
```
NAME                      READY     STATUS        RESTARTS   AGE
my-release-cockroachdb-0  1/1       Running       0          5m
my-release-cockroachdb-1  1/1       Running       0          20s
my-release-cockroachdb-2  1/1       Running       0          5m
```

You can check the state of re-joining from the new pod's logs:
```shell
kubectl logs my-release-cockroachdb-1
```
```
[...]
I161028 19:32:09.754026 1 server/node.go:586  [n1] node connected via gossip and
verified as part of cluster {"35ecbc27-3f67-4e7d-9b8f-27c31aae17d6"}
[...]
cockroachdb-0.my-release-cockroachdb.default.svc.cluster.local:26257
build:      beta-20161027-55-gd2d3c7f @ 2016/10/28 19:27:25 (go1.7.3)
admin:      http://0.0.0.0:8080
sql:
postgresql://root@my-release-cockroachdb-1.my-release-cockroachdb.default.svc.cluster.local:26257?sslmode=disable
logs:       cockroach-data/logs
store[0]:   path=cockroach-data
status:     restarted pre-existing node
clusterID:  {35ecbc27-3f67-4e7d-9b8f-27c31aae17d6}
nodeID:     2
[...]
```

### NetworkPolicy

To enable NetworkPolicy for CockroachDB, install [a networking plugin that implements the Kubernetes NetworkPolicy spec](https://kubernetes.io/docs/tasks/administer-cluster/declare-network-policy#before-you-begin), and set `networkPolicy.enabled` to `yes`/`true`.

For Kubernetes v1.5 & v1.6, you must also turn on NetworkPolicy by setting the `DefaultDeny` Namespace annotation. Note: this will enforce policy for _all_ pods in the Namespace:
```shell
kubectl annotate namespace default "net.beta.kubernetes.io/network-policy={\"ingress\":{\"isolation\":\"DefaultDeny\"}}"
```

For more precise policy, set `networkPolicy.ingress.grpc` and `networkPolicy.ingress.http` rules. This will only allow pods that match the provided rules to connect to CockroachDB.

### Scaling

Scaling should be managed via the `helm upgrade` command. After resizing your cluster on your cloud environment (e.g., GKE or EKS), run the following command to add a pod. This assumes you scaled from 3 to 4 nodes:

```shell
helm upgrade \
my-release \
stable/cockroachdb \
--set statefulset.replicas=4 \
--reuse-values
```

Note, that if you are running in secure mode (`tls.enabled` is `yes`/`true`) and increase the size of your cluster, you will also have to approve the CSR (certificate-signing request) of each new node (using `kubectl get csr` and `kubectl certificate approve`).

[1]: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#inter-pod-affinity-and-anti-affinity
[2]: https://kubernetes.io/docs/concepts/configuration/assign-pod-node/#node-affinity
[3]: https://cert-manager.io/


text:
public class Service{
  public void foo() {}
}

redpajama_set_name: RedPajamaGithub


text:
using System;
using System.IO;

namespace Public.Common.Lib.Visuals.IO.Serialization
{
    /// <summary>
    /// Serializes RGB float images to binary files.
    /// </summary>
    /// <remarks>
    /// The format used is direct:
    /// * (int, 4-bytes) number of rows.
    /// * (int, 4-bytes) number of columns.
    /// * N = rows * columns * 3 color channel (float, 4-bytes) values.
    /// </remarks>
    public class RgbFloatImageBinarySerializer : IRgbFloatImageSerializer
    {
        #region Static

        public static RgbFloatImage Deserialize(string imageFilePath)
        {
            // Determine the size of the image and get the image byte data.
            int rows;
            int cols;
            byte[] intBuffer = new byte[sizeof(Int32)];
            byte[] valuesByteBuffer;
            using (FileStream fStream = new FileStream(imageFilePath, FileMode.Open))
            {
                fStream.Read(intBuffer, 0, sizeof(Int32));
                rows = BitConverter.ToInt32(intBuffer, 0);

                fStream.Read(intBuffer, 0, sizeof(Int32));
                cols = BitConverter.ToInt32(intBuffer, 0);

                int numBytes = rows * cols * RgbFloatImage.NumberOfRgbColorChannels * sizeof(float);
                valuesByteBuffer = new byte[numBytes];
                fStream.Read(valuesByteBuffer, 0, numBytes);
            }

            // Create the output image.
            RgbFloatImage output = new RgbFloatImage(rows, cols);
            float[] values = output.Data;

            // Fill the output image.
            int numValues = values.Length;
            int iByte = 0;
            for (int iValue = 0; iValue < numValues; iValue++)
            {
                float value = BitConverter.ToSingle(valuesByteBuffer, iByte);
                values[iValue] = value;

                iByte += sizeof(float);
            }

            return output;
        }

        public static void Serialize(string imageFilePath, RgbFloatImage image, bool overwrite = true)
        {
            FileMode fileMode = FileMode.Create;
            if(!overwrite)
            {
                fileMode = FileMode.CreateNew;
            }

            using (FileStream fStream = new FileStream(imageFilePath, fileMode))
            {
                fStream.Write(BitConverter.GetBytes(image.Rows), 0, sizeof(Int32));
                fStream.Write(BitConverter.GetBytes(image.Columns), 0, sizeof(Int32));

                float[] values = image.Data;
                int numValues = values.Length;
                for (int iValue = 0; iValue < numValues; iValue++)
                {
                    float value = values[iValue];
                    fStream.Write(BitConverter.GetBytes(value), 0, sizeof(float));
                }
            }
        }

        #endregion

        public ImageFormat[] SupportedImageFormats => new ImageFormat[] { ImageFormat.Dat };
        public RgbFloatImage this[string imageFilePath, bool overwrite = true]
        {
            get
            {
                RgbFloatImage output = RgbFloatImageBinarySerializer.Deserialize(imageFilePath);
                return output;
            }
            set
            {
                RgbFloatImageBinarySerializer.Serialize(imageFilePath, value);
            }
        }
    }
}


text:
in_header: true
layout: page
title: About
permalink: /about/
---

## So... what IS Abacus?
It's a calculator. Obviously. But what makes it better than
what already exists? There's a few things. Abacus is:
* Programmable, and not in TI Basic.
* Precise. With the "precise" option, Abacus can keep up to 50 significant figures.
* Capable. Ever wonder what 2700 is? How about 8!!? Abacus can tell you!
* Offline. While Wolfram Alpha can do powerful math, it needs internet connection!
* Built for the desktop. Why use buttons on the screen when there's buttons on the keyboard?
* Open source. Don't like something? Help is always welcome!

## Why was Abacus made?
The initial project was proposed for the [Northwest Advanced Programming Workshop](http://nwapw.org/about/).
You can read the project proposal on the main GitHub page, although the idea has
changed quite a bit, mostly in shifting from "fast" to "precise".

## What is Abacus made with?
Java and Kotlin. Java provides a good layer of abstraction and a great standard
library, while Kotlin allows for the reduction of boilerplate code and null
safety. Using JVM-based languages also allows Abacus to expose its entire
API to plugins, and load them at runtime.


text:
{% requireAdmin %}

{% extends "settings/users/_layout" %}
{% set selectedNavItem = 'fields' %}

{% block content %}
    <form method="post" accept-charset="UTF-8" data-saveshortcut data-confirm-unload>
        <input type="hidden" name="action" value="users/save-field-layout">
        {{ redirectInput('settings') }}
        {{ csrfInput() }}

        {% include "_includes/fieldlayoutdesigner" with {
            fieldLayout: craft.app.fields.getLayoutByType('craft\\elements\\User')
        } only %}

        <div class="buttons">
            <input type="submit" class="btn submit" value="{{ 'Save'|t('app') }}">
        </div>
    </form>
{% endblock %}


text:
<?php

/**
 * DokuWiki Media Manager Popup
 *
 * @author   Andreas Gohr <andi@splitbrain.org>
 * @license  GPL 2 (http://www.gnu.org/licenses/gpl.html)
 */
// must be run from within DokuWiki
if (!defined('DOKU_INC')) die();
header('X-UA-Compatible: IE=edge,chrome=1');

?><!DOCTYPE html>
<html lang="<?php echo $conf['lang']?>" dir="<?php echo $lang['direction'] ?>" class="popup no-js">

    <meta charset="utf-8" />
    <title>
        <?php echo hsc($lang['mediaselect'])?>
        [<?php echo strip_tags($conf['title'])?>]
    </title>

    <?php tpl_metaheaders()?>
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <?php echo tpl_favicon(array('favicon', 'mobile')) ?>
    <?php tpl_includeFile('meta.html') ?>

        <?php html_msgarea() ?>

<?php echo hsc($lang['mediaselect'])?>

            <?php /* keep the id! additional elements are inserted via JS here */?>

            <?php tpl_mediaTree() ?>

            <?php tpl_mediaContent() ?>


text:
#include "config.h"
#include "core/dom/UserTypingGestureIndicator.h"

#include "core/dom/Document.h"
#include "core/dom/Element.h"
#include "core/page/Frame.h"

namespace WebCore {

static bool s_processingUserTypingGesture;
bool UserTypingGestureIndicator::processingUserTypingGesture()
{
    return s_processingUserTypingGesture;
}

static RefPtr<Element>& focusedElement()
{
    DEFINE_STATIC_LOCAL(RefPtr<Element>, element, ());
    return element;
}

Element* UserTypingGestureIndicator::focusedElementAtGestureStart()
{
    return focusedElement().get();
}

UserTypingGestureIndicator::UserTypingGestureIndicator(Frame* frame)
    : m_previousProcessingUserTypingGesture(s_processingUserTypingGesture)
    , m_previousFocusedElement(focusedElement())
{
    s_processingUserTypingGesture = true;
    focusedElement() = frame->document() ? frame->document()->focusedElement() : 0;
}

UserTypingGestureIndicator::~UserTypingGestureIndicator()
{
    s_processingUserTypingGesture = m_previousProcessingUserTypingGesture;
    focusedElement() = m_previousFocusedElement;
}

} // namespace WebCore


text:
import math

import numpy as np
from scipy.stats import kurtosis

from ..utils import logger, verbose, check_random_state

@verbose
def infomax(data, weights=None, l_rate=None, block=None, w_change=1e-12,
            anneal_deg=60., anneal_step=0.9, extended=False, n_subgauss=1,
            kurt_size=6000, ext_blocks=1, max_iter=200,
            random_state=None, verbose=None):
    """Run the (extended) Infomax ICA decomposition on raw data

    based on the publications of Bell & Sejnowski 1995 (Infomax)
    and Lee, Girolami & Sejnowski, 1999 (extended Infomax)

    Parameters
    ----------
    data : np.ndarray, shape (n_samples, n_features)
        The data to unmix.
    w_init : np.ndarray, shape (n_features, n_features)
        The initialized unmixing matrix. Defaults to None. If None, the
        identity matrix is used.
    l_rate : float
        This quantity indicates the relative size of the change in weights.
        Note. Smaller learining rates will slow down the procedure.
        Defaults to 0.010d / alog(n_features ^ 2.0)
    block : int
        The block size of randomly chosen data segment.
        Defaults to floor(sqrt(n_times / 3d))
    w_change : float
        The change at which to stop iteration. Defaults to 1e-12.
    anneal_deg : float
        The angle at which (in degree) the learning rate will be reduced.
        Defaults to 60.0
    anneal_step : float
        The factor by which the learning rate will be reduced once
        ``anneal_deg`` is exceeded:
            l_rate *= anneal_step
        Defaults to 0.9
    extended : bool
        Wheather to use the extended infomax algorithm or not. Defaults to
        True.
    n_subgauss : int
        The number of subgaussian components. Only considered for extended
        Infomax.
    kurt_size : int
        The window size for kurtosis estimation. Only considered for extended
        Infomax.
    ext_blocks : int
        The number of blocks after which to recompute Kurtosis.
        Only considered for extended Infomax.
    max_iter : int
        The maximum number of iterations. Defaults to 200.
    verbose : bool, str, int, or None
        If not None, override default verbose level (see mne.verbose).

    Returns
    -------
    unmixing_matrix : np.ndarray of float, shape (n_features, n_features)
        The linear unmixing operator.
    """
    rng = check_random_state(random_state)

    # define some default parameter
    max_weight = 1e8
    restart_fac = 0.9
    min_l_rate = 1e-10
    blowup = 1e4
    blowup_fac = 0.5
    n_small_angle = 20
    degconst = 180.0 / np.pi

    # for extended Infomax
    extmomentum = 0.5
    signsbias = 0.02
    signcount_threshold = 25
    signcount_step = 2
    if ext_blocks > 0:  # allow not to recompute kurtosis
        n_subgauss = 1  # but initialize n_subgauss to 1 if you recompute

    # check data shape
    n_samples, n_features = data.shape
    n_features_square = n_features ** 2

    # check input parameter
    # heuristic default - may need adjustment for
    # large or tiny data sets
    if l_rate is None:
        l_rate = 0.01 / math.log(n_features ** 2.0)

    if block is None:
        block = int(math.floor(math.sqrt(n_samples / 3.0)))

    logger.info('computing%sInfomax ICA' % ' Extended ' if extended is True
                else ' ')

    # collect parameter
    nblock = n_samples // block
    lastt = (nblock - 1) * block + 1

    # initialize training
    if weights is None:
        # initialize weights as identity matrix
        weights = np.identity(n_features, dtype=np.float64)

    BI = block * np.identity(n_features, dtype=np.float64)
    bias = np.zeros((n_features, 1), dtype=np.float64)
    onesrow = np.ones((1, block), dtype=np.float64)
    startweights = weights.copy()
    oldweights = startweights.copy()
    step = 0
    count_small_angle = 0
    wts_blowup = False
    blockno = 0
    signcount = 0

    # for extended Infomax
    if extended is True:
        signs = np.identity(n_features)
        signs.flat[slice(0, n_features * n_subgauss, n_features)]
        kurt_size = min(kurt_size, n_samples)
        old_kurt = np.zeros(n_features, dtype=np.float64)
        oldsigns = np.zeros((n_features, n_features))

    # trainings loop
    olddelta, oldchange = 1., 0.
    while step < max_iter:

        # shuffle data at each step
        rng.seed(step)  # --> permutation is fixed but differs at each step
        permute = list(range(n_samples))
        rng.shuffle(permute)

        # ICA training block
        # loop across block samples
        for t in range(0, lastt, block):
            u = np.dot(data[permute[t:t + block], :], weights)
            u += np.dot(bias, onesrow).T

            if extended is True:
                # extended ICA update
                y = np.tanh(u)
                weights += l_rate * np.dot(weights,
                                           BI - np.dot(np.dot(u.T, y), signs) -
                                           np.dot(u.T, u))
                bias += l_rate * np.reshape(np.sum(y, axis=0,
                                            dtype=np.float64) * -2.0,
                                            (n_features, 1))

            else:
                # logistic ICA weights update
                y = 1.0 / (1.0 + np.exp(-u))
                weights += l_rate * np.dot(weights,
                                           BI + np.dot(u.T, (1.0 - 2.0 * y)))
                bias += l_rate * np.reshape(np.sum((1.0 - 2.0 * y), axis=0,
                                            dtype=np.float64), (n_features, 1))

            # check change limit
            max_weight_val = np.max(np.abs(weights))
            if max_weight_val > max_weight:
                wts_blowup = True

            blockno += 1
            if wts_blowup:
                break

            # ICA kurtosis estimation
            if extended is True:

                n = np.fix(blockno / ext_blocks)

                if np.abs(n) * ext_blocks == blockno:
                    if kurt_size < n_samples:
                        rp = np.floor(rng.uniform(0, 1, kurt_size) *
                                      (n_samples - 1))
                        tpartact = np.dot(data[rp.astype(int), :], weights).T
                    else:
                        tpartact = np.dot(data, weights).T

                    # estimate kurtosis
                    kurt = kurtosis(tpartact, axis=1, fisher=True)

                    if extmomentum != 0:
                        kurt = (extmomentum * old_kurt +
                                (1.0 - extmomentum) * kurt)
                        old_kurt = kurt

                    # estimate weighted signs
                    signs.flat[::n_features + 1] = ((kurt + signsbias) /
                                                    np.abs(kurt + signsbias))

                    ndiff = ((signs.flat[::n_features + 1] -
                              oldsigns.flat[::n_features + 1]) != 0).sum()
                    if ndiff == 0:
                        signcount += 1
                    else:
                        signcount = 0
                    oldsigns = signs

                    if signcount >= signcount_threshold:
                        ext_blocks = np.fix(ext_blocks * signcount_step)
                        signcount = 0

        # here we continue after the for
        # loop over the ICA training blocks
        # if weights in bounds:
        if not wts_blowup:
            oldwtchange = weights - oldweights
            step += 1
            angledelta = 0.0
            delta = oldwtchange.reshape(1, n_features_square)
            change = np.sum(delta * delta, dtype=np.float64)
            if step > 1:
                angledelta = math.acos(np.sum(delta * olddelta) /
                                       math.sqrt(change * oldchange))
                angledelta *= degconst

            # anneal learning rate
            oldweights = weights.copy()
            if angledelta > anneal_deg:
                l_rate *= anneal_step    # anneal learning rate
                # accumulate angledelta until anneal_deg reached l_rates
                olddelta = delta
                oldchange = change
                count_small_angle = 0  # reset count when angle delta is large
            else:
                if step == 1:  # on first step only
                    olddelta = delta  # initialize
                    oldchange = change
                count_small_angle += 1
                if count_small_angle > n_small_angle:
                    max_iter = step

            # apply stopping rule
            if step > 2 and change < w_change:
                step = max_iter
            elif change > blowup:
                l_rate *= blowup_fac

        # restart if weights blow up
        # (for lowering l_rate)
        else:
            step = 0  # start again
            wts_blowup = 0  # re-initialize variables
            blockno = 1
            l_rate *= restart_fac  # with lower learning rate
            weights = startweights.copy()
            oldweights = startweights.copy()
            olddelta = np.zeros((1, n_features_square), dtype=np.float64)
            bias = np.zeros((n_features, 1), dtype=np.float64)

            # for extended Infomax
            if extended:
                signs = np.identity(n_features)
                signs.flat[slice(0, n_features * n_subgauss, n_features)]
                oldsigns = np.zeros((n_features, n_features))

            if l_rate > min_l_rate:
                if verbose:
                    logger.info('... lowering learning rate to %g'
                                '\n... re-starting...' % l_rate)
            else:
                raise ValueError('Error in Infomax ICA: unmixing_matrix matrix'
                                 'might not be invertible!')

    # prepare return values
    return weights.T


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

namespace QS.Fx.Interface.ConstraintClasses
{
    [QS.Fx.Reflection.InterfaceConstraintClass(QS.Fx.Reflection.InterfaceConstraintClasses.Some)]
    public sealed class Some : QS.Fx.Reflection.IInterfaceConstraint<Some>
    {
        #region Constructor

        public Some()
        {
        }

        #endregion

        #region Fields

        private int _level;

        #endregion

        #region IInterfaceConstraint<Some> Members

        void QS.Fx.Reflection.IInterfaceConstraint<Some>.Initialize(string _spec, QS.Fx.Reflection.IInterfaceClass _class)
        {
            this._level = Int32.Parse(_spec);
        }

        bool QS.Fx.Reflection.IInterfaceConstraint<Some>.Weaker(Some _other)
        {
            return this._level <= _other._level;
        }

        #endregion
    }
}


text:
<?php

namespace App\Services\Request\Factory;

use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\RequestStack;

abstract class AbstractRequestFactory
{
    /**
     * @var ParameterBag
     */
    private $requestParameters;

    /**
     * @param RequestStack $requestStack
     */
    public function __construct(RequestStack $requestStack)
    {
        $this->requestParameters = $requestStack->getCurrentRequest()->request;
    }

    /**
     * @param string $key
     *
     * @return string
     */
    protected function getStringValueFromRequestParameters($key)
    {
        return trim($this->requestParameters->get($key));
    }

    /**
     * @param string $key
     *
     * @return bool
     */
    protected function getBooleanValueFromRequestParameters($key)
    {
        return (bool)$this->requestParameters->get($key);
    }
}


text:
package com.betterweather.app.db;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class BetterWeatherOpenHelper extends SQLiteOpenHelper {

	/**
	 * Province±í´´½¨Óï¾ä
	 */
	public static final String CREATE_PROVINCE = "create table Province("
			+ "id integer primary key autoincrement, "
			+ "province_name text, "
			+ "province_code text) ";

	/**
	 * City±í´´½¨Óï¾ä
	 */
	public static final String CREATE_CITY = "create table City("
			+ "id integer primary key autoincrement, "
			+ "city_name text, "
			+ "city_code text, "
			+ "province_id integer) ";

	/**
	 * Country±í´´½¨Óï¾ä
	 */
	public static final String CREATE_COUNTRY = "create table Country ("
			+ "id integer primary key autoincrement, "
			+ "country_name text, "
			+ "country_code text, "
			+ "city_id integer) ";

	public BetterWeatherOpenHelper(Context context, String name,
			CursorFactory factory, int version) {
		super(context, name, factory, version);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL(CREATE_PROVINCE);//´´½¨province±í
		db.execSQL(CREATE_CITY);//´´½¨city±í
		db.execSQL(CREATE_COUNTRY);//´´½¨country±í
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub

	}

}


text:
package com.spotify.spydra.submitter.runner;

import static org.junit.Assert.assertTrue;

import com.spotify.spydra.model.ClusterType;
import com.spotify.spydra.model.SpydraArgument;
import com.spotify.spydra.util.SpydraArgumentUtil;
import org.junit.Test;

public class RunnerTest {

  @Test
  public void testIsStaticInvocation() {
    SpydraArgument arguments = new SpydraArgument();
    arguments.submit.options.put(SpydraArgument.OPTION_CLUSTER, "my_cluster");
    assertTrue(SpydraArgumentUtil.isStaticInvocation(arguments));
  }

  @Test
  public void testIsOnPremiseInvocation() {
    SpydraArgument arguments = new SpydraArgument();
    arguments.setClusterType(ClusterType.ON_PREMISE);
    assertTrue(SpydraArgumentUtil.isOnPremiseInvocation(arguments));
  }
}


text:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    style="@style/Benchit.ListView.ListItem">

    <TextView
        android:id="@+id/item_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/Benchit.ListView.ListItem.Text"/>
</LinearLayout>


text:
package org.wso2.siddhi.core.query.output.ratelimit.event;

import org.wso2.siddhi.core.event.ComplexEvent;
import org.wso2.siddhi.core.event.ComplexEventChunk;
import org.wso2.siddhi.core.query.output.ratelimit.OutputRateLimiter;
import org.wso2.siddhi.core.query.selector.QuerySelector;

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

public class FirstGroupByPerEventOutputRateLimiter extends OutputRateLimiter {
    private final Integer value;
    private String id;
    private List<ComplexEvent> complexEventList;
    private volatile int counter = 0;
    List<String> groupByKeys = new ArrayList<String>();

    public FirstGroupByPerEventOutputRateLimiter(String id, Integer value) {
        this.id = id;
        this.value = value;
        complexEventList = new ArrayList<ComplexEvent>();
    }

    @Override
    public OutputRateLimiter clone(String key) {
        return new FirstGroupByPerEventOutputRateLimiter(id + key, value);
    }

    @Override
    public void process(ComplexEventChunk complexEventChunk) {
        ComplexEventChunk<ComplexEvent> eventChunk = new ComplexEventChunk<ComplexEvent>();
        for (ComplexEvent complexEvent : complexEventList) {
            eventChunk.add(complexEvent);
        }
        complexEventList.clear();
        sendToCallBacks(eventChunk);
    }

    @Override
    public void add(ComplexEvent complexEvent) {
        String groupByKey = QuerySelector.getThreadLocalGroupByKey();
        if (!groupByKeys.contains(groupByKey)) {
            groupByKeys.add(groupByKey);
            complexEventList.add(complexEvent);
        }
        if (++counter == value) {
            counter = 0;
            groupByKeys.clear();
        }

    }

    @Override
    public void start() {
        //Nothing to start
    }

    @Override
    public void stop() {
        //Nothing to stop
    }

    @Override
    public Object[] currentState() {
        return new Object[]{complexEventList, groupByKeys, counter};
    }

    @Override
    public void restoreState(Object[] state) {
        complexEventList = (List<ComplexEvent>) state[0];
        groupByKeys = (List<String>) state[1];
        counter = (Integer) state[2];
    }

}


text:
{-# LANGUAGE TemplateHaskell, CPP #-}
import Test
-- import Test.LeanCheck -- already exported by Test
import Test.LeanCheck.Derive
import System.Exit (exitFailure)
import Data.List (elemIndices,sort)
import Test.LeanCheck.Utils

data D0       = D0                    deriving Show
data D1 a     = D1 a                  deriving Show
data D2 a b   = D2 a b                deriving Show
data D3 a b c = D3 a b c              deriving Show
data C1 a     =           C11 a | C10 deriving Show
data C2 a b   = C22 a b | C21 a | C20 deriving Show
data I a b    = a :+ b                deriving Show

deriveListable ''D0
deriveListable ''D1
deriveListable ''D2
deriveListable ''D3
deriveListable ''C1
deriveListable ''C2
deriveListable ''I

-- recursive datatypes
data Peano = Zero | Succ Peano deriving Show
data List a = a :- List a | Nil deriving Show
data Bush a = Bush a :-: Bush a | Leaf a deriving (Show, Eq)
data Tree a = Node (Tree a) a (Tree a) | Null deriving (Show, Eq)

deriveListable ''Peano
deriveListable ''List
deriveListable ''Bush
deriveListable ''Tree

-- Nested datatype cascade
data Nested = Nested N0 (N1 Int) (N2 Int Int)
data N0     = R0 Int
data N1 a   = R1 a
data N2 a b = R2 a b
deriveListableCascading ''Nested

-- Recursive nested datatype cascade
data RN      = RN RN0 (RN1 Int) (RN2 Int RN)
data RN0     = Nest0 Int | Recurse0 RN
data RN1 a   = Nest1 a   | Recurse1 RN
data RN2 a b = Nest2 a b | Recurse2 RN
deriveListableCascading ''RN

-- Type synonyms
data Pair a = Pair a a
type Alias a = Pair a
-- deriveListable ''Alias -- this will fail
deriveListableCascading ''Alias
deriveListableIfNeeded ''Alias -- only works because instance already exists

-- Nested type synonyms
data Triple a = Triple a a a
type Tralias a = Triple a
data Pairiple a = Pairriple (Tralias a) (Tralias a)
deriveListableCascading ''Pairiple

-- Those should have no effect (instance already exists):
{- uncommenting those should generate warnings
deriveListable ''Bool
deriveListable ''Maybe
deriveListable ''Either
-}

-- Those should not generate warnings
deriveListableIfNeeded ''Bool
deriveListableIfNeeded ''Maybe
deriveListableIfNeeded ''Either

main :: IO ()
main  =  do
  max <- getMaxTestsFromArgs 200
  case elemIndices False (tests max) of
    [] -> putStrLn "Tests passed!"
    is -> do putStrLn ("Failed tests:" ++ show is)
             exitFailure

tests :: Int -> [Bool]
tests n  =
  [ True

  , map unD0 list =| n |= list
  , map unD1 list =| n |= (list :: [Int])
  , map unD2 list =| n |= (list :: [(Int,Int)])
  , map unD3 list =| n |= (list :: [(Int,Int,Int)])

  , map unD1 list == (list :: [()])
  , map unD2 list == (list :: [((),())])
  , map unD3 list == (list :: [((),(),())])

  , map unD1 list == (list :: [Bool])
  , map unD2 list == (list :: [(Bool,Bool)])
  , map unD3 list == (list :: [(Bool,Bool,Bool)])

  , map peanoToNat list =| n |= list
  , map listToList list =| n |= (list :: [[Bool]])
  , map listToList list =| n |= (list :: [[Int]])

  , mapT peanoToNat tiers =| 6 |= tiers
  , mapT listToList tiers =| 6 |= (tiers :: [[ [Bool] ]])
  , mapT listToList tiers =| 6 |= (tiers :: [[ [Int] ]])

  , take 6 (list :: [Bush Bool])
    == [ Leaf False
       , Leaf True
       , Leaf False :-: Leaf False
       , Leaf False :-: Leaf True
       , Leaf True :-: Leaf False
       , Leaf True :-: Leaf True
       ]

  , take 6 (list :: [Tree Bool])
    == [ Null
       , Node Null False Null
       , Node Null True Null
       , Node Null False (Node Null False Null)
       , Node Null False (Node Null True Null)
       , Node Null True (Node Null False Null)
       ]

  , (tiers :: [[ Bool       ]]) =| 6 |= $(deriveTiers ''Bool)
  , (tiers :: [[ [Int]      ]]) =| 6 |= $(deriveTiers ''[])
  , (tiers :: [[ [Bool]     ]]) =| 6 |= $(deriveTiers ''[])
  , (tiers :: [[ Maybe Int  ]]) =| 6 |= $(deriveTiers ''Maybe)
  , (tiers :: [[ Maybe Bool ]]) =| 6 |= $(deriveTiers ''Maybe)
  , ([]:tiers :: [[Either Bool Int]]) =$ map sort . take 6 $= $(deriveTiers ''Either)

  , (list :: [ Bool       ]) =| n |= $(deriveList ''Bool)
  , (list :: [ [Int]      ]) =| n |= $(deriveList ''[])
  , (list :: [ [Bool]     ]) =| n |= $(deriveList ''[])
  , (list :: [ Maybe Int  ]) =| n |= $(deriveList ''Maybe)
  , (list :: [ Maybe Bool ]) =| n |= $(deriveList ''Maybe)
  ]
  where
  unD0 (D0)       = ()
  unD1 (D1 x)     = (x)
  unD2 (D2 x y)   = (x,y)
  unD3 (D3 x y z) = (x,y,z)

peanoToNat :: Peano -> Nat
peanoToNat Zero = 0
peanoToNat (Succ n) = 1 + peanoToNat n

listToList :: List a -> [a]
listToList Nil = []
listToList (x :- xs) = x : listToList xs


text:
import json

import requests as r

from typing import Mapping, Optional, Tuple

RESOURCES = {
    'login': '/api/login/',
    'document': '/api/document/',
}

HEADERS = {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
}

def json_or_none(response:r.Response) -> Optional[Mapping[str, str]]:
    """
    Returns a valid JSON, when the response if not a valid JSON, returns `None`.
    """
    try:
        return response.json()
    except ValueError:
        return None

def login(post_func, domain:str, username:str, password:str) -> Tuple[Optional[str], r.Response]:
    """
    Returns a auth token and `requests.Response` as a tuple. When a request has not been successful, returns `None`
    as the token.
    - `domain` should not contain a trailing slash
    """

    def token_or_none(maybe_json:Mapping[str, str]) -> Optional[str]:
        return maybe_json.get('token', None) if maybe_json is not None else None

    response = post_func(domain + RESOURCES['login'], json={'username': username, 'password': password},
                         headers=HEADERS)
    return ((token_or_none(json_or_none(response)), response) if response.status_code == 200 else (None, response))

def scan_document(post_func, domain:str, token:str, document:Mapping[str, str]) -> Tuple[Optional[dict], r.Response]:
    """
    Returns a scanned document and `requests.Response` as a tuple. When a request has not been successful, return `None`
    as the document.
    - `domain` should not contain a trailing slash
    - `document` should be a dictionary
    """
    response = post_func(domain + RESOURCES['document'], json=document, headers={'Authorization': token, **HEADERS})
    return ((json_or_none(response), response) if response.status_code == 201 else (None, response))

def login_and_scan(post_func, domain:str, username:str, password:str, document:Mapping[str, str],
                   token:str=None) -> Tuple[Optional[dict], r.Response]:
    """
    Returns a scanned document and `requests.Response` as a tuple. When a request has not been successful, return `None`
    as the document. First it tries to use a given auth `token`. When the `token` is not valid, tries to log in via
    given `username` and `password`.
    - `domain` should not contain a trailing slash
    - `document` should be a dictionary
    """
    if not token:
        token, login_response = login(post_func, domain, username, password)

        if not token:
            return (None, login_response)

    document, document_response = scan_document(post_func, domain, token, document)
    if document_response.status_code == 401:
        token, login_response = login(post_func, domain, username, password)

    if not token:
        return (None, login_response)

    return scan_document(post_func, domain, token, document)


text:
/* TEMPLATE GENERATED TESTCASE FILE
Filename: CWE478_Missing_Default_Case_in_Switch__basic_12.c
Label Definition File: CWE478_Missing_Default_Case_in_Switch__basic.label.xml
Template File: point-flaw-12.tmpl.c
*/
/*
 * @description
 * CWE: 478 Missing Default Case in Switch
 * Sinks:
 *    GoodSink: Use default case in switch statement
 *    BadSink : No default case in a switch statement
 * Flow Variant: 12 Control flow: if(globalReturnsTrueOrFalse())
 *
 * */

#include "std_testcase.h"

#ifndef OMITBAD

void CWE478_Missing_Default_Case_in_Switch__basic_12_bad()
{
    if(globalReturnsTrueOrFalse())
    {
        {
            const char *charString = "shouldn\'t see this value";
            int x;
            x = (rand() % 3);
            switch (x)
            {
            case 0:
                charString = "0";
                break;
            case 1:
                charString = "1";
                break;
                /* FLAW: x could be 2, and there is no 'default' case for that */
            }
            printLine(charString);
        }
    }
    else
    {
        {
            const char *charString = "shouldn\'t see this value";
            int x;
            x = (rand() % 3);
            switch (x)
            {
            case 0:
                charString = "0";
                break;
            case 1:
                charString = "1";
                break;
                /* FIX: provide a 'default' case for other values */
            default:
                return;
            }
            printLine(charString);
        }
    }
}

#endif /* OMITBAD */

#ifndef OMITGOOD

/* good1() uses the GoodSink on both sides of the "if" statement */
static void good1()
{
    if(globalReturnsTrueOrFalse())
    {
        {
            const char *charString = "shouldn\'t see this value";
            int x;
            x = (rand() % 3);
            switch (x)
            {
            case 0:
                charString = "0";
                break;
            case 1:
                charString = "1";
                break;
                /* FIX: provide a 'default' case for other values */
            default:
                return;
            }
            printLine(charString);
        }
    }
    else
    {
        {
            const char *charString = "shouldn\'t see this value";
            int x;
            x = (rand() % 3);
            switch (x)
            {
            case 0:
                charString = "0";
                break;
            case 1:
                charString = "1";
                break;
                /* FIX: provide a 'default' case for other values */
            default:
                return;
            }
            printLine(charString);
        }
    }
}

void CWE478_Missing_Default_Case_in_Switch__basic_12_good()
{
    good1();
}

#endif /* OMITGOOD */

/* Below is the main(). It is only used when building this testcase on
   its own for testing or for building a binary to use in testing binary
   analysis tools. It is not used when compiling all the testcases as one
   application, which is how source code analysis tools are tested. */

#ifdef INCLUDEMAIN

int main(int argc, char * argv[])
{
    /* seed randomness */
    srand( (unsigned)time(NULL) );
#ifndef OMITGOOD
    printLine("Calling good()...");
    CWE478_Missing_Default_Case_in_Switch__basic_12_good();
    printLine("Finished good()");
#endif /* OMITGOOD */
#ifndef OMITBAD
    printLine("Calling bad()...");
    CWE478_Missing_Default_Case_in_Switch__basic_12_bad();
    printLine("Finished bad()");
#endif /* OMITBAD */
    return 0;
}

#endif


text:
module Boxcutter::LoadBalancer
  class Service
    include Boxcutter::Logging

    attr_reader :api

    def initialize(api, attrs, logger = $stdout)
      @api = api
      @attrs = attrs
      @logger = logger
    end

    def to_s
      "#<Service id:'#{id}' name:'#{name}' port:'#{port}' service_type:'#{service_type}'>"
    end

    def name
      @attrs["name"]
    end

    def port
      @attrs["port"]
    end

    def id
      @attrs["id"]
    end

    def service_type
      @attrs["service_type"]
    end

    def http?
      service_type == 'http'
    end

    def https?
      service_type == 'https'
    end

    def backends
      resp = api.backends(id)

      if resp.success?
        resp.parsed.map {|attrs| Backend.new(api, attrs, logger)}
      else
        []
      end
    end

    def each_backend_named(backend_name, &block)
      results = backends.select {|backend| backend.name == backend_name}
      if !results.empty?
        results.each(&block)
      else
        log "Could not find '#{backend_name}' on '#{self}'"
      end
    end
  end
end


text:
package com.fimtra.tcpchannel;

import java.io.IOException;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Queue;

import com.fimtra.channel.ChannelUtils;
import com.fimtra.channel.IReceiver;
import com.fimtra.channel.TransportTechnologyEnum;
import com.fimtra.util.Log;
import com.fimtra.util.ObjectUtils;

/**
 * Utility methods for a {@link TcpChannel}
 *

 * Socket options for the runtime can be set using system properties in the form
 * tcpchannel.{SocketOptions_constant} e.g.
 *
 *

 * -Dtcpchannel.TCP_NODELAY=true -Dtcpchannel.SO_RCVBUF=65535
 *

 *
 * @author Ramon Servadei
 */
public abstract class TcpChannelUtils
{
    static final Map<String, String> SOCKET_OPTIONS = new HashMap<String, String>();
    static
    {
        Map.Entry<Object, Object> entry = null;
        Object key = null;
        Object value = null;
        for (Iterator<Map.Entry<Object, Object>> it = System.getProperties().entrySet().iterator(); it.hasNext();)
        {
            try
            {
                entry = it.next();
                key = entry.getKey();
                value = entry.getValue();
                if (key.toString().startsWith("tcpchannel."))
                {
                    SOCKET_OPTIONS.put(key.toString().substring("tcpchannel.".length()), value.toString());
                }
            }
            catch (Exception e)
            {
                Log.log(TcpChannelUtils.class, "Could not read socket option " + ObjectUtils.safeToString(key)
                    + " from system properties", e);
            }
        }
        Log.log(TcpChannelUtils.class, "Socket options: ", ObjectUtils.safeToString(SOCKET_OPTIONS));
    }

    /**
     * Thrown when a TCP channel buffer cannot hold the incoming message. In these situations, the
     * {@link TcpChannel} can be constructed with a bigger receive buffer.
     *
     * @see TcpChannel#TcpChannel(String, int, IReceiver, int)
     * @author Ramon Servadei
     */
    public static class BufferOverflowException extends RuntimeException
    {
        private static final long serialVersionUID = 1L;

        public BufferOverflowException(String message)
        {
            super(message);
        }
    }

    /**
     * Get the next free TCP server port in the passed in ranges for the given host.
     *
     * @param hostName
     *            the hostname to use to find the next free default TCP server
     * @return a free TCP server port that can have a TCP server socket bound to it, -1 if there is
     *         not a free port
     */
    public synchronized static int getNextFreeTcpServerPort(String hostName, int startPortRangeInclusive,
        int endPortRangeExclusive)
    {
        if (ChannelUtils.TRANSPORT == TransportTechnologyEnum.SOLACE)
        {
            return System.identityHashCode(Runtime.getRuntime()) + (int) System.nanoTime();
        }

        String hostAddress = TcpChannelUtils.LOCALHOST_IP;
        try
        {
            hostAddress = InetAddress.getByName(hostName).getHostAddress();
        }
        catch (UnknownHostException e)
        {
        }
        for (int i = startPortRangeInclusive; i < endPortRangeExclusive; i++)
        {
            try
            {
                Log.log(TcpChannelUtils.class, "Trying ", hostAddress, ":", Integer.toString(i));
                final ServerSocket serverSocket = new ServerSocket();
                serverSocket.bind(new InetSocketAddress(hostAddress, i));
                serverSocket.close();
                // now ensure the server socket is closed before saying we can use it
                try
                {
                    int j = 0;
                    while (j++ < 10)
                    {
                        new Socket(hostAddress, i).close();
                        Thread.sleep(100);
                    }
                }
                catch (Exception e)
                {
                }
                Log.log(TcpChannelUtils.class, "Using ", hostAddress, ":", Integer.toString(i));
                return i;
            }
            catch (IOException e)
            {
                Log.log(TcpChannelUtils.class, e.getMessage());
            }
        }
        throw new RuntimeException("No free TCP port available betwen " + startPortRangeInclusive + " and "
            + endPortRangeExclusive);
    }

    /**
     * Handles the connection result for a call to
     * {@link TcpChannelUtils#connectSocketChannelInNonBlockingMode(SocketChannel, String, int, SelectorProcessor, IConnectionResultProcessor)}
     */
    static interface IConnectionResultProcessor
    {
        void onConnectionSuccess();

        void onConnectionFailed(Exception e);
    }

    /** Handles all socket read operations for all {@link TcpChannel} instances */
    final static SelectorProcessor READER = new SelectorProcessor("tcp-channel-reader", SelectionKey.OP_READ);

    /** Handles all socket write operations for all {@link TcpChannel} instances */
    final static SelectorProcessor WRITER = new SelectorProcessor("tcp-channel-writer", SelectionKey.OP_WRITE);

    /** Handles all socket accept operations for all {@link TcpServer} instances */
    final static SelectorProcessor ACCEPT_PROCESSOR = new SelectorProcessor("tcp-channel-accept",
        SelectionKey.OP_ACCEPT);

    /**
     * The public facing IP for the local host
     */
    public final static String LOCALHOST_IP;
    static
    {
        String hostAddress = null;
        try
        {
            hostAddress = InetAddress.getLocalHost().getHostAddress();
        }
        catch (UnknownHostException e)
        {
            throw new RuntimeException("Could not get host address", e);
        }
        LOCALHOST_IP = hostAddress;
    }

    private TcpChannelUtils()
    {
        super();
    }

    /**
     * Given a ByteBuffer of data, this method decodes it into a List of byte[] objects. Data
     * between {@link TcpChannel} objects is encoded in the following format (ABNF notation):
     *
     *

     *  stream = 1*frame
     *  frame = length data
     *
     *  length = 4OCTET ; big-endian integer indicating the length of the data
     *  data = 1*OCTET ; the data
     *

     *
     * @param frames
     *            the queue to place the decoded frames resolved from the buffer. Any incomplete
     *            frames are left in the buffer.
     * @param buffer
     *            a bytebuffer holding any number of frames or partial frames
     * @throws BufferOverflowException
     *             if the buffer size cannot hold a complete frame
     */
    static void decode(Queue<byte[]> frames, ByteBuffer buffer) throws BufferOverflowException
    {
        buffer.flip();
        // the TCP message could be a concatenation of multiple ones
        // the format is: <4 bytes len><data><4 bytes len><data>
        int len;
        byte[] message;
        int position;
        while (buffer.position() < buffer.limit())
        {
            try
            {
                len = buffer.getInt();
            }
            catch (BufferUnderflowException e)
            {
                break;
            }

            position = buffer.position();
            if (position + len > buffer.limit())
            {
                if (position == 4)
                {
                    if (position + len > buffer.capacity())
                    {
                        // the buffer is full and the length indicates that there is more data than
                        // the buffer can hold. We cannot extract the data from the buffer so we
                        // need indicate the buffer is full.
                        final String overflowMessage =
                            "Need to read " + len + " but buffer has no more space from current position: "
                                + buffer.toString() + ", read=" + asString(frames) + ", buffer="
                                + new String(buffer.array());
                        buffer.clear();
                        throw new BufferOverflowException(overflowMessage);
                    }
                }
                // we're waiting for more data to read
                // set the position back
                buffer.position(buffer.position() - 4);
                break;
            }
            else
            {
                message = new byte[len];
                System.arraycopy(buffer.array(), position, message, 0, len);
                buffer.position(position + len);
                frames.add(message);
            }
        }
        buffer.compact();
    }

    private static String asString(Queue<byte[]> values)
    {
        StringBuilder sb = new StringBuilder();
        sb.append("[");
        boolean first = true;
        for (byte[] bs : values)
        {
            if (first)
            {
                first = false;
            }
            else
            {
                sb.append(", ");
            }
            sb.append(bs.length).append(" bytes");
        }
        sb.append("]");
        return sb.toString();
    }

    /**
     * Given a ByteBuffer of data containing frames delimited by a termination byte, this method
     * decodes it into a List of byte[] objects, one for each frame. This method assumes data is
     * encoded in the following format (ABNF notation):
     *
     *

     * stream = 1*frame
     *
     * frame = data terminator
     * data = 1*OCTET ; the data
     *
     * terminator = OCTET OCTET ; an ASCII control code, possibly 0x3 for ETX (end of text)
     *

     *
     * @param frames
     *            the queue to place the decoded frames resolved from the buffer. Any incomplete
     *            frames are left in the buffer.
     * @param buffer
     *            a bytebuffer holding any number of frames or partial frames
     * @param terminator
     *            the byte sequence for the end of a frame
     * @throws BufferOverflowException
     *             if the buffer size cannot hold a complete frame
     */
    static void decodeUsingTerminator(Queue<byte[]> frames, ByteBuffer buffer, byte[] terminator)
        throws BufferOverflowException
    {
        buffer.flip();
        final byte[] bufferArray = buffer.array();
        int[] terminatorIndex = new int[2];
        int terminatorIndexPtr = 0;
        final int limit = buffer.limit();
        int i = 0;
        for (i = 0; i < limit; i++)
        {
            if (bufferArray[i] == terminator[0] && (i + 1 < limit) && bufferArray[i + 1] == terminator[1])
            {
                // mark the terminator index, expand the terminatorIndex array if we're at its limit
                terminatorIndex[terminatorIndexPtr++] = i;
                if (terminatorIndexPtr == terminatorIndex.length)
                {
                    // (always +2 as a heuristic for reducing constant resizing)
                    terminatorIndex = Arrays.copyOf(terminatorIndex, terminatorIndex.length + 2);
                }
            }
        }

        if (terminatorIndexPtr > 0)
        {
            int len;
            int lastTerminatorIndex = 0;
            byte[] frame;
            for (i = 0; i < terminatorIndexPtr; i++)
            {
                len = terminatorIndex[i] - lastTerminatorIndex;
                frame = new byte[len];
                System.arraycopy(bufferArray, lastTerminatorIndex, frame, 0, len);
                frames.add(frame);
                lastTerminatorIndex = terminatorIndex[i] + terminator.length;
            }
            buffer.position(lastTerminatorIndex);
        }
        else
        {
            // no terminator found, is the buffer totally full?
            if (buffer.limit() == buffer.capacity())
            {
                final String overflowMessage =
                    "No frame terminator found and buffer is at its limit: " + buffer.toString();
                buffer.clear();
                throw new BufferOverflowException(overflowMessage);
            }
        }
        buffer.compact();
    }

    /**
     * Create a {@link SocketChannel}, connect in blocking mode to the given TCP server host and
     * port and then set the socket channel to non-blocking mode.
     *
     * @param host
     *            the host name of the target TCP server socket to connect to
     * @param port
     *            the port of the target TCP server socket to connect to
     * @return a connected socket channel in non-blocking mode
     * @throws ConnectException
     *             if the socket could not be created or connected
     */
    static SocketChannel createAndConnectNonBlockingSocketChannel(String host, int port) throws ConnectException
    {
        final SocketChannel socketChannel;
        try
        {
            socketChannel = SocketChannel.open();
        }
        catch (IOException e)
        {
            final String message = "Could not create socket channel for " + host + ":" + port;
            Log.log(TcpChannelUtils.class, message, e);
            throw new ConnectException(message);
        }
        try
        {
            socketChannel.configureBlocking(true);
            socketChannel.connect(new InetSocketAddress(host, port));
            socketChannel.configureBlocking(false);
            return socketChannel;
        }
        catch (ConnectException e)
        {
            closeChannel(socketChannel);
            final String message =
                "Could not connect socket channel to " + host + ":" + port + " (" + e.toString() + ")";
            throw new ConnectException(message);

        }
        catch (Exception e)
        {
            final String message =
                "Could not connect socket channel to " + host + ":" + port + " (" + e.toString() + ")";
            Log.log(TcpChannelUtils.class, message, e);
            closeChannel(socketChannel);
            throw new ConnectException(message);
        }
    }

    /**
     * Close the channel
     */
    static void closeChannel(final SocketChannel channel)
    {
        Log.log(TcpChannelUtils.class, "Closing ", ObjectUtils.safeToString(channel));
        try
        {
            channel.socket().close();
            channel.close();
        }
        catch (IOException e)
        {
            Log.log(TcpChannelUtils.class, "Could not close " + ObjectUtils.safeToString(channel), e);
        }
    }

    /**
     * Sets TCP socket options on the channel that are found in system properties.
     *

     * e.g. -Dtcpchannel.TCP_NODELAY=true -Dtcpchannel.SO_RCVBUF=65535
     *
     * @param socketChannel
     *            the socket to configure options for
     */
    static void setOptions(SocketChannel socketChannel)
    {
        Map.Entry<String, String> entry = null;
        String key = null;
        String value = null;
        for (Iterator<Map.Entry<String, String>> it = SOCKET_OPTIONS.entrySet().iterator(); it.hasNext();)
        {
            entry = it.next();
            key = entry.getKey();
            value = entry.getValue();
            try
            {
                if (key.equals("TCP_NODELAY"))
                {
                    socketChannel.socket().setTcpNoDelay(Boolean.valueOf(value).booleanValue());
                    continue;
                }
                if (key.equals("SO_REUSEADDR"))
                {
                    socketChannel.socket().setReuseAddress(Boolean.valueOf(value).booleanValue());
                    continue;
                }
                if (key.equals("IP_TOS"))
                {
                    socketChannel.socket().setTrafficClass(Integer.valueOf(value).intValue());
                    continue;
                }
                if (key.equals("SO_LINGER"))
                {
                    socketChannel.socket().setSoLinger(true, Integer.valueOf(value).intValue());
                    continue;
                }
                if (key.equals("SO_TIMEOUT"))
                {
                    socketChannel.socket().setSoTimeout(Integer.valueOf(value).intValue());
                    continue;
                }
                if (key.equals("SO_SNDBUF"))
                {
                    socketChannel.socket().setSendBufferSize(Integer.valueOf(value).intValue());
                    continue;
                }
                if (key.equals("SO_RCVBUF"))
                {
                    socketChannel.socket().setReceiveBufferSize(Integer.valueOf(value).intValue());
                    continue;
                }
                if (key.equals("SO_KEEPALIVE"))
                {
                    socketChannel.socket().setKeepAlive(Boolean.valueOf(value).booleanValue());
                    continue;
                }
                if (key.equals("SO_OOBINLINE"))
                {
                    socketChannel.socket().setOOBInline(Boolean.valueOf(value).booleanValue());
                    continue;
                }
                Log.log(TcpChannelUtils.class, "Unhandled socket option: ", key);
            }
            catch (Exception e)
            {
                Log.log(TcpChannelUtils.class, "Could not set socket option " + ObjectUtils.safeToString(key) + " on "
                    + ObjectUtils.safeToString(socketChannel), e);
            }
        }
    }
}


text:
<?xml version="1.0" encoding="UTF-8"?>
<problems>
  <problem>
    <file>OctalRecognition.java</file>
    <line>3</line>
    <problem_class>Octal integer</problem_class>
    <description>Octal integer 0123 #loc</description>
  </problem>

  <problem>
    <file>OctalRecognition.java</file>
    <line>4</line>
    <problem_class>Octal integer</problem_class>
    <description>Octal integer 0_123 #loc</description>
  </problem>
</problems>


text:
<!DOCTYPE html>
<html>
<head>
    <title>test access page</title>
    <script src="browser-messaging-commons.js"></script>
    <script src="hmac-sha256.js"></script>
    <script src="AccessClient.js"></script>
    <script>
        var client;

        var accessClickHandler = function() {
            // set the password as entered by the user
            client.setUserAccessKey( 'fire-fighter3' );

            // start the websocket process
            client.authenticate();
        };

        var startAccessClient = function() {
            // simulate finding a user with an active session
            var opts = {};
            opts.user  = {
                id:'33e2c605f0334ce7a455ec8f6a60e063',
                session:'2a7f543a-5a4e-44db-a150-b653852ed0c0',
                privateChannel:'/44C4UUX'
            };

            // create the access client instance with user object
            client = AccessClient.createInstance( opts );

            // make available for debugging
            window.client = client;
        };
    </script>
</head>
<body onload="startAccessClient();">

<!-- create text input box and go button -->

</body>
</html>


text:
var fs = require('fs');
var fsExtra = require('fs-extra');
var _ =  require('lodash');
var request = require('request');
var unzip = require('unzip');
var mv = require('mv');
var shelljs = require('shelljs/shell');

require('string-prototype');

var env = require('../env');

function getFileContent(tplFp, tplParams) {
  var tplContent = Utils.readFileSync(tplFp);

  if (!tplParams) {
    return tplContent;
  }
  return _.template(tplContent)(tplParams);
}

var Utils = {
  getTemplatePath: function(fp) {
    return `${__dirname}/../templates/${fp}.tpl`;
  },

  getResourcePath: function(fp) {
    return `${__dirname}/../resources/${fp}`;
  },

  writeFileSync: function(targetFp, tplFp, tplParams) {
    var fileContent = getFileContent(tplFp, tplParams);
    fsExtra.outputFileSync(targetFp, fileContent);
  },

  readFileSync: function(fp, charset=null) {
    var content = fs.readFileSync(fp, charset);
    return content;
  },

  copyFileSync: function(srcFp, targetFp) {
    shelljs.cp('-R', srcFp, targetFp);
  },

  isFileExists: function(fp) {
    try {
      fs.lstatSync(fp);
    } catch (err) {
      if (err.code === 'ENOENT') {
        return false;
      } else {
        throw err;
      }
    }

    return true;
  },

  mkdirSync: function(dir) {
    try {
      fs.statSync(dir);
    } catch(err) {
      fs.mkdirSync(dir);
    }
  },

  moveDir: function(srcDir, targetDir, cb) {
    mv(srcDir, targetDir, { mkdirp: true }, function(err) {
      if (err) {
        cb(err, null);
        return;
      }
      cb(null, true);
    });
  },

  downloadFile: function(fileUrl, targetFp, cb) {
    request(
      {
        url: fileUrl,
        encoding: null
      },
      function(err, resp, body) {
        if (err) {
          cb(err, null);
          return;
        }

        if (resp && resp.statusCode == 200) {
          try {
            fs.writeFileSync(targetFp, body);
            cb(null, {fp: targetFp});
          } catch(err) {
            cb(err, null);
            return;
          }
        } else {
          cb({
            name: 'Error',
            message: 'Invalid URL'
          }, null);
        }
      }
    );
  },

  unzipFile: function(srcFp, targetDir, cb) {
    var stream = fs.createReadStream(srcFp)
      .pipe(unzip.Extract({ path: targetDir }));

    stream.on('error', function (err) {
      cb(err, null);
      return;
    });

    stream.on('finish', function () {
      cb(null, true)
    });
  },

  unlinkSync: function(fp) {
    if (typeof fp === 'object') {
      fp.forEach(function(file) {
        fs.unlinkSync(file);
      });
    } else {
      fs.unlinkSync(fp);
    }
  },

  forceUnlink: function(fp) {
    if (typeof fp !== 'object') {
      return;
    }
    shelljs.rm('-rf', fp);
  },

  getWorkingDir: function() {
    return shelljs.pwd();
  }
};

module.exports = Utils;


text:
import numpy as np
import pandas as pd

"""
A simple set of helper functions to make life a little easier :)

"""

def is_numpy(x):
    """
    As it states is the object x a numpy array
    """
    return isinstance(x, np.ndarray)

def is_pandas(x):
    """
        As it states is the object x a pandas array
    """
    return isinstance(x, pd.DataFrame)

def export_fold(train, folds, filename='./input/xfolds.csv'):
    """

    NEED TO DO MORE CHECKS TO MAKE SURE THE SPLITS ARE AS EXPECTED

    A simple helper function to export the folds created from sklearn.model_selection
    Its main purpose is to enable the export of the fold ids so stacking with different models, and languages
    and ensure that no leaking occurs.
    :param train: A pandas DF with an index ID
    :param folds: An sklearn.model_selection fold generator (KFold, StratifiedKFold, etc)
    :param filename: The path and file name to export the folds
    :return: Nothing, writes file to disk.
    """
    train['fold'] = 0
    # Capture the index
    i = 0
    for _, xtest in folds.split(train.index):
        print "Fold", i
        train['fold'].ix[xtest] = i
        i += 1
    print train.groupby('fold').count()
    # Export in case we want to use R
    train.to_csv(filename, header=True, index=None)


text:
require 'open3'

class CollectorExceptionHandlingTest < Minitest::Test
  include MultiverseHelpers

  setup_and_teardown_agent do
    NewRelic::Agent.agent.stubs(:sleep)
  end

  def after_setup
    # there's a call to sleep in handle_force_restart that we want to skip
    $collector.reset
  end

  RESTART_PAYLOAD       = { 'error_type' => 'NewRelic::Agent::ForceRestartException'    }
  DISCONNECT_PAYLOAD    = { 'error_type' => 'NewRelic::Agent::ForceDisconnectException' }
  RUNTIME_ERROR_PAYLOAD = { 'error_type' => 'RuntimeError'                              }

  def test_should_reconnect_on_force_restart_exception
    # We stub these exceptions because we want the EventLoop to exit with a
    # ForceRestartException the first time through, then (after reconnecting)
    # we force the disconnect so that this test will end cleanly.

    $collector.stub_exception('metric_data'       , RESTART_PAYLOAD   ).once
    $collector.stub_exception('get_agent_commands', DISCONNECT_PAYLOAD).once

    with_config(:data_report_period => 0) do
      NewRelic::Agent.agent.deferred_work!({})
    end

    assert_equal(2, $collector.calls_for('connect').size)

    # We expect one metric_data post that the collector responds to with the
    # ForceRestartException, and then another that gets through successfully.
    metric_data_calls = $collector.calls_for('metric_data')
    assert_equal(2, metric_data_calls.size)
    refute_equal(metric_data_calls[0].run_id, metric_data_calls[1].run_id)
  end

  def test_should_stop_reporting_after_force_disconnect
    $collector.stub_exception('metric_data', DISCONNECT_PAYLOAD).once

    with_config(:data_report_period => 0) do
      NewRelic::Agent.agent.deferred_work!({})
    end

    assert_equal(1, $collector.calls_for('connect').size)
    assert_equal(1, $collector.calls_for('metric_data').size)
  end

  def test_should_stop_reporting_after_force_disconnect_on_connect
    $collector.stub_exception('connect', DISCONNECT_PAYLOAD).once

    with_config(:data_report_period => 0) do
      NewRelic::Agent.agent.deferred_work!({})
    end

    assert_equal(1, $collector.calls_for('connect').size)
  end

  def test_should_reconnect_on_connect_exception
    $collector.stub_exception('connect', RUNTIME_ERROR_PAYLOAD).once
    $collector.stub_exception('metric_data', DISCONNECT_PAYLOAD).once

    with_config(:data_report_period => 0) do
      NewRelic::Agent.agent.deferred_work!({})
    end

    assert_equal(2, $collector.calls_for('connect').size)
  end

  def test_should_reconnect_on_get_redirect_host_exception
    $collector.stub_exception('get_redirect_host', RUNTIME_ERROR_PAYLOAD).once
    $collector.stub_exception('metric_data', DISCONNECT_PAYLOAD).once

    with_config(:data_report_period => 0) do
      NewRelic::Agent.agent.deferred_work!({})
    end

    assert_equal(2, $collector.calls_for('get_redirect_host').size)
    assert_equal(1, $collector.calls_for('connect').size)
  end
end


text:
Hat-Trie
========

[![Build Status](https://travis-ci.org/dcjones/hat-trie.svg)](https://travis-ci.org/dcjones/hat-trie)

This a ANSI C99 implementation of the HAT-trie data structure of Askitis and
Sinha, an extremely efficient (space and time) modern variant of tries.

The version implemented here maps arrays of bytes to words (i.e., unsigned
longs), which can be used to store counts, pointers, etc, or not used at all if
you simply want to maintain a set of unique strings.

For details see,

  1. Askitis, N., & Sinha, R. (2007). HAT-trie: a cache-conscious trie-based data
     structure for strings. Proceedings of the thirtieth Australasian conference on
     Computer science-Volume 62 (pp. 97–105). Australian Computer Society, Inc.

  2. Askitis, N., & Zobel, J. (2005). Cache-conscious collision resolution in
     string hash tables. String Processing and Information Retrieval (pp.
     91–102). Springer.

Installation
------------

    git clone git@github.com:dcjones/hat-trie.git
    cd hat-trie
    autoreconf -i
    ./configure
    make install

To use the library, include `hat-trie.h` and link using `-lhat-trie`.

Tests
-----

Build and run the tests:

    make check

Other Language Bindings
-----------------------
 * Ruby - https://github.com/luikore/triez
 * Python - https://github.com/kmike/hat-trie