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

#ifndef CHROME_BROWSER_EXTENSIONS_SYSTEM_DISPLAY_DISPLAY_INFO_PROVIDER_UTILS_H_
#define CHROME_BROWSER_EXTENSIONS_SYSTEM_DISPLAY_DISPLAY_INFO_PROVIDER_UTILS_H_

#include "chromeos/crosapi/mojom/cros_display_config.mojom-forward.h"
#include "extensions/browser/api/system_display/display_info_provider.h"
#include "extensions/common/api/system_display.h"
#include "ui/gfx/geometry/insets.h"

namespace extensions {

// Callback function for CrosDisplayConfigController crosapi interface.
// Converts input display layout |info| from crosapi to extension api type.
// Passes converted array into a |callback|.
void OnGetDisplayLayoutResult(
    base::OnceCallback<void(DisplayInfoProvider::DisplayLayoutList)> callback,
    crosapi::mojom::DisplayLayoutInfoPtr info);

// Converts display id string to number.
// Returns invalid id in case of error.
int64_t GetDisplayId(const std::string& display_id_str);

// Returns a display object for if display id is found.
// Return empty display object otherwise.
display::Display GetDisplayForId(const std::string& display_id_str);

// Converts display layout |position| from extension api to crosapi type.
crosapi::mojom::DisplayLayoutPosition GetDisplayLayoutPosition(
    api::system_display::LayoutPosition position);

// Converts system display |insets| to gfx type.
gfx::Insets GetInsets(const api::system_display::Insets& insets);

// Converts int |rotation_value| to crosapi type.
crosapi::mojom::DisplayRotationOptions GetMojomDisplayRotationOptions(
    int rotation_value);

// Converts crosapi |rotation_options| to int.
int GetRotationFromMojomDisplayRotationInfo(
    crosapi::mojom::DisplayRotationOptions rotation_options);

// Validates the DisplayProperties input. Does not perform any tests with
// DisplayManager dependencies. Returns an error string on failure or nullopt
// on success.
absl::optional<std::string> ValidateDisplayPropertiesInput(
    const std::string& display_id_str,
    const api::system_display::DisplayProperties& info);

// Converts display unit crosapi |mojo_info| to system display type.
api::system_display::DisplayUnitInfo GetDisplayUnitInfoFromMojo(
    const crosapi::mojom::DisplayUnitInfo& mojo_info);

// Converts system display calibration |pair| to crosapi type.
crosapi::mojom::TouchCalibrationPairPtr GetTouchCalibrationPair(
    const api::system_display::TouchCalibrationPair& pair);

void SetDisplayUnitInfoLayoutProperties(
    const crosapi::mojom::DisplayLayoutInfo& layout,
    api::system_display::DisplayUnitInfo* display);

}  // namespace extensions

#endif  // CHROME_BROWSER_EXTENSIONS_SYSTEM_DISPLAY_DISPLAY_INFO_PROVIDER_UTILS_H_

repository: chromium/chromium

repo_name: chromium/chromium

branch_name: refs/heads/main

extension: h


text:
// Copyright 2018 The MACE Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef MACE_OPS_OPENCL_IMAGE_LPNORM_H_
#define MACE_OPS_OPENCL_IMAGE_LPNORM_H_

#include "mace/core/ops/op_context.h"
#include "mace/runtimes/opencl/core/opencl_executor.h"
#include "mace/runtimes/opencl/core/opencl_helper.h"
#include "mace/runtimes/opencl/opencl_runtime.h"
#include "mace/core/tensor.h"
#include "mace/ops/opencl/lpnorm.h"

namespace mace {
namespace ops {
namespace opencl {
namespace image {

class LpNormKernel : public OpenCLLpNormKernel {
 public:
  explicit LpNormKernel(const int p, const int axis);
  ~LpNormKernel() = default;

  MaceStatus Compute(
      OpContext *context, const Tensor *input, Tensor *output) override;

 private:
  int p_;
  int axis_;

  cl::Kernel kernel_;
  uint32_t kwg_size_;
};

}  // namespace image
}  // namespace opencl
}  // namespace ops
}  // namespace mace

#endif  // MACE_OPS_OPENCL_IMAGE_LPNORM_H_

repository: gasgallo/mace

license_type: permissive

repo_name: gasgallo/mace

branch_name: refs/heads/master


text:
package artifactory

import (
	"flag"
	"net/http"
	"os"
	"time"

	"github.com/gorilla/mux"
	"github.com/npetzall/badger-go/lib/badge"
	"github.com/npetzall/badger-go/lib/metric"
	"github.com/npetzall/badger-go/lib/requests"
	logging "github.com/op/go-logging"
)

const PathPrefix = "/artifactory"

const defArtifactoryURL = "http://localhost:8081"

var artifactoryURLFlag = flag.String("artifactoryUrl", defArtifactoryURL, "configure url for artifactory")

var log = logging.MustGetLogger("artifactory")

var artifactoryURL = defArtifactoryURL

func Configure(r *mux.Router) {
	if os.Getenv("ARTIFACTORY_URL") != "" {
		artifactoryURL = os.Getenv("ARTIFACTORY_URL")
	}
	if defArtifactoryURL != *artifactoryURLFlag {
		artifactoryURL = *artifactoryURLFlag
	}
	log.Infof("Using baseUrl: %s", artifactoryURL)
	r.PathPrefix("/latestVersion").Methods("GET").Handler(http.HandlerFunc(getLatestVersion))
}

func getLatestVersion(w http.ResponseWriter, r *http.Request) {
	defer metric.Timed(time.Now(), "getLatestVersion", log)
	req, rerr := http.NewRequest("GET", artifactoryURL+"/api/search/latestVersion?"+r.URL.RawQuery, nil)
	if rerr != nil {
		log.Error(rerr)
		badge.DownstreamError(w)
		return
	}
	v, e := requests.Request(req, requests.Text())
	if e != nil {
		log.Error(e)
		badge.DownstreamError(w)
		return
	}
	berr, data := badge.CreateBadge("Latest Version", v, "green")
	if berr != nil {
		log.Error(berr)
		badge.DownstreamError(w)
		return
	} else {
		w.Header().Add("Content-Type", "image/svg+xml")
		w.Write(data)
		return
	}
}

license: MIT

repository: npetzall/badger-go

repo_name: npetzall/badger-go

extension: go


text:
# Exports:
#  LIBNEO_FOUND
#  LIBNEO_INCLUDE_DIR
#  LIBNEO_LIBRARY
# Hints:
#  LIBNEO_LIBRARY_DIR

find_path(LIBNEO_INCLUDE_DIR
          neo/neo.h neo/neo.hpp)

find_library(LIBNEO_LIBRARY
             NAMES neo
             HINTS "${LIBNEO_LIBRARY_DIR}")

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Neo DEFAULT_MSG LIBNEO_LIBRARY LIBNEO_INCLUDE_DIR)
mark_as_advanced(LIBNEO_INCLUDE_DIR LIBNEO_LIBRARY)

repository: micvision/neo-sdk

repo_name: micvision/neo-sdk

gha_license_id: MIT

extension: cmake


text:
#ifndef YARL_OPTIMIZATION_OPTIMIZERS_GD_HPP
#define YARL_OPTIMIZATION_OPTIMIZERS_GD_HPP

#include <cmath>

#include "yarl/utils/utils.hpp"

namespace yarl {

#define EGDC "GDOpt is not configured!"
#define EGDF "Failed to execute GDOpt.f() [%s]"

/**
 * Gradient Desent Optimization
 */
class GDOpt {
public:
  bool configured;

  int max_iter;
  VecX eta;
  VecX x;
  std::function<double(VecX x)> f;

  GDOpt() : configured(false), max_iter(1000), eta(), x(), f(nullptr) {}

  int configure(int max_iter,
                const VecX &eta,
                const VecX &x,
                std::function<double(VecX x)> f);
  int calcGradient(VecX &df);
  int optimize();
};

}  // namespace yarl
#endif

extension: hpp


text:
#include "Godzilla.h"
#include "CallStack.h"
#include "OutflowBC.h"

REGISTER_OBJECT(OutflowBC);

Parameters
OutflowBC::parameters()
{
    Parameters params = NaturalRiemannBC::parameters();
    return params;
}

OutflowBC::OutflowBC(const Parameters & params) : NaturalRiemannBC(params), components({ 0 })
{
    _F_;
}

const std::vector<PetscInt> &
OutflowBC::get_components() const
{
    _F_;
    return this->components;
}

void
OutflowBC::evaluate(PetscReal time,
                    const PetscReal * c,
                    const PetscReal * n,
                    const PetscScalar * xI,
                    PetscScalar * xG)
{
    _F_;
    xG[0] = xI[0];
}

repository: andrsd/godzilla

repo_name: andrsd/godzilla

extension: cpp


text:
/*
Copyright 2019 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

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

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

package containerd

import (
	"os"
	"os/exec"

	"k8s.io/kubeadm/kinder/pkg/build/bits"
)

// PreLoadInitImages preload images required by kubeadm-init into the containerd runtime installed that exists inside a kind(er) node
func PreLoadInitImages(bc *bits.BuildContext) error {
	// NB. this code is an extract from "sigs.k8s.io/kind/pkg/build/node"

	return bc.RunInContainer(
		"bash", "-c",
		`containerd & find /kind/images -name *.tar -print0 | xargs -r -0 -n 1 -P $(nproc) ctr --namespace=k8s.io images import --no-unpack && kill %1 && rm -rf /kind/images/*`,
	)
}

// Commit a kind(er) node image that uses the containerd runtime internally
func Commit(containerID, targetImage string) error {
	// NB. this code is an extract from "sigs.k8s.io/kind/pkg/build/node"

	// Save the image changes to a new image
	cmd := exec.Command("docker", "commit",
		/*
			The snapshot storage must be a volume to avoid overlay on overlay

			NOTE: we do this last because changing a volume with a docker image
			must occur before defining it.

			See: https://docs.docker.com/engine/reference/builder/#volume
		*/
		"--change", `VOLUME [ "/var/lib/containerd" ]`,
		// we need to put this back after changing it when running the image
		"--change", `ENTRYPOINT [ "/usr/local/bin/entrypoint", "/sbin/init" ]`,
		containerID, targetImage)

	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	return cmd.Run()
}

repository: prasadkatti/kubeadm

repo_name: prasadkatti/kubeadm


text:
package ring

import (
	"bytes"
	"math"
	"testing"
)

func TestNewBuilder(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	b.AddNode(true, 1, nil, nil, "")
	pa := b.PointsAllowed()
	if pa != 1 {
		t.Fatalf("NewBuilder's PointsAllowed was %d not 1", pa)
	}
	b.SetPointsAllowed(10)
	pa = b.PointsAllowed()
	if pa != 10 {
		t.Fatalf("NewBuilder's PointsAllowed was %d not 10", pa)
	}
	rc := b.Ring().ReplicaCount()
	if rc != 3 {
		t.Fatalf("NewBuilder's ReplicaCount was %d not 3", rc)
	}
	u16 := b.Ring().PartitionBitCount()
	if u16 != 1 {
		t.Fatalf("NewBuilder's PartitionBitCount was %d not 1", u16)
	}
	n := b.Ring().Nodes()
	if len(n) != 1 {
		t.Fatalf("NewBuilder's Nodes count was %d not 1", len(n))
	}
}

func TestBuilderPersistence(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	b.AddNode(true, 1, []string{"server1", "zone1"}, []string{"1.2.3.4:56789"}, "Meta One")
	b.AddNode(true, 1, []string{"server2", "zone1"}, []string{"1.2.3.5:56789", "1.2.3.5:9876"}, "Meta Four")
	b.AddNode(false, 0, []string{"server3", "zone1"}, []string{"1.2.3.6:56789"}, "Meta Three")
	b.Ring()
	buf := bytes.NewBuffer(make([]byte, 0, 65536))
	err := b.Persist(buf)
	if err != nil {
		t.Fatal(err)
	}
	b2, err := LoadBuilder(bytes.NewBuffer(buf.Bytes()))
	if err != nil {
		t.Fatal(err)
	}
	if b2.version != b.version {
		t.Fatalf("%v != %v", b2.version, b.version)
	}
	if len(b2.nodes) != len(b.nodes) {
		t.Fatalf("%v != %v", len(b2.nodes), len(b.nodes))
	}
	for i := 0; i < len(b2.nodes); i++ {
		if b2.nodes[i].id != b.nodes[i].id {
			t.Fatalf("%v != %v", b2.nodes[i].id, b.nodes[i].id)
		}
		if b2.nodes[i].capacity != b.nodes[i].capacity {
			t.Fatalf("%v != %v", b2.nodes[i].capacity, b.nodes[i].capacity)
		}
		if len(b2.nodes[i].tierIndexes) != len(b.nodes[i].tierIndexes) {
			t.Fatalf("%v != %v", len(b2.nodes[i].tierIndexes), len(b.nodes[i].tierIndexes))
		}
		for j := 0; j < len(b2.nodes[i].tierIndexes); j++ {
			if b2.nodes[i].tierIndexes[j] != b.nodes[i].tierIndexes[j] {
				t.Fatalf("%v != %v", b2.nodes[i].tierIndexes[j], b.nodes[i].tierIndexes[j])
			}
		}
		if len(b2.nodes[i].addresses) != len(b.nodes[i].addresses) {
			t.Fatalf("%v != %v", len(b2.nodes[i].addresses), len(b.nodes[i].addresses))
		}
		for j := 0; j < len(b2.nodes[i].addresses); j++ {
			if b2.nodes[i].addresses[j] != b.nodes[i].addresses[j] {
				t.Fatalf("%v != %v", b2.nodes[i].addresses[j], b.nodes[i].addresses[j])
			}
		}
		if b2.nodes[i].meta != b.nodes[i].meta {
			t.Fatalf("%v != %v", b2.nodes[i].meta, b.nodes[i].meta)
		}
	}
	if b2.partitionBitCount != b.partitionBitCount {
		t.Fatalf("%v != %v", b2.partitionBitCount, b.partitionBitCount)
	}
	if len(b2.replicaToPartitionToNodeIndex) != len(b.replicaToPartitionToNodeIndex) {
		t.Fatalf("%v != %v", len(b2.replicaToPartitionToNodeIndex), len(b.replicaToPartitionToNodeIndex))
	}
	for i := 0; i < len(b2.replicaToPartitionToNodeIndex); i++ {
		if len(b2.replicaToPartitionToNodeIndex[i]) != len(b.replicaToPartitionToNodeIndex[i]) {
			t.Fatalf("%v != %v", len(b2.replicaToPartitionToNodeIndex[i]), len(b.replicaToPartitionToNodeIndex[i]))
		}
		for j := 0; j < len(b2.replicaToPartitionToNodeIndex[i]); j++ {
			if b2.replicaToPartitionToNodeIndex[i][j] != b.replicaToPartitionToNodeIndex[i][j] {
				t.Fatalf("%v != %v", b2.replicaToPartitionToNodeIndex[i][j], b.replicaToPartitionToNodeIndex[i][j])
			}
		}
	}
	if len(b2.replicaToPartitionToLastMove) != len(b.replicaToPartitionToLastMove) {
		t.Fatalf("%v != %v", len(b2.replicaToPartitionToLastMove), len(b.replicaToPartitionToLastMove))
	}
	for i := 0; i < len(b2.replicaToPartitionToLastMove); i++ {
		if len(b2.replicaToPartitionToLastMove[i]) != len(b.replicaToPartitionToLastMove[i]) {
			t.Fatalf("%v != %v", len(b2.replicaToPartitionToLastMove[i]), len(b.replicaToPartitionToLastMove[i]))
		}
		for j := 0; j < len(b2.replicaToPartitionToLastMove[i]); j++ {
			if b2.replicaToPartitionToLastMove[i][j] != b.replicaToPartitionToLastMove[i][j] {
				t.Fatalf("%v != %v", b2.replicaToPartitionToLastMove[i][j], b.replicaToPartitionToLastMove[i][j])
			}
		}
	}
	if b2.pointsAllowed != b.pointsAllowed {
		t.Fatalf("%v != %v", b2.pointsAllowed, b.pointsAllowed)
	}
	if b2.maxPartitionBitCount != b.maxPartitionBitCount {
		t.Fatalf("%v != %v", b2.maxPartitionBitCount, b.maxPartitionBitCount)
	}
	if b2.moveWait != b.moveWait {
		t.Fatalf("%v != %v", b2.moveWait, b.moveWait)
	}
}

func TestBuilderLoadGarbage(t *testing.T) {
	b, err := LoadBuilder(bytes.NewBuffer([]byte{
		0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
		0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
	}))
	if err == nil {
		t.Fatal("")
	}
	if b != nil {
		t.Fatal("")
	}
}

func TestBuilderAddRemoveNodes(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	nA := b.AddNode(true, 1, nil, nil, "")
	nB := b.AddNode(true, 1, nil, nil, "")
	r := b.Ring()
	n := r.Nodes()
	if len(n) != 2 {
		t.Fatalf("Ring had %d nodes instead of 2", len(n))
	}
	b.RemoveNode(nA.ID())
	r = b.Ring()
	n = r.Nodes()
	if len(n) != 1 {
		t.Fatalf("Ring had %d nodes instead of 1", len(n))
	}
	pc := uint32(1) << r.PartitionBitCount()
	for p := uint32(0); p < pc; p++ {
		n = r.ResponsibleNodes(p)
		if len(n) != 3 {
			t.Fatalf("Supposed to get 3 replicas, got %d", len(n))
		}
		if n[0].ID() != nB.ID() ||
			n[1].ID() != nB.ID() ||
			n[2].ID() != nB.ID() {
			t.Fatalf("Supposed only have node id:2 and got %#v %#v %#v", n[0], n[1], n[2])
		}
	}
}

func TestBuilderNodeLookup(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	nA := b.AddNode(true, 1, nil, nil, "")
	nB := b.AddNode(true, 1, nil, nil, "")
	n := b.Node(nA.ID())
	if n.ID() != nA.ID() {
		t.Fatalf("Node lookup should've given id:1 but instead gave %#v", n)
	}
	n = b.Node(nB.ID())
	if n.ID() != nB.ID() {
		t.Fatalf("Node lookup should've given id:2 but instead gave %#v", n)
	}
	n = b.Node(84)
	if n != nil {
		t.Fatalf("Node lookup should've given nil but instead gave %#v", n)
	}
}

func TestBuilderRing(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	nA := b.AddNode(true, 1, nil, nil, "")
	b.AddNode(true, 1, nil, nil, "")
	r := b.Ring()
	n := r.LocalNode()
	if n != nil {
		t.Fatalf("Ring() should've returned an unbound ring; instead LocalNode gave %#v", n)
	}
	r.SetLocalNode(nA.ID())
	n = r.LocalNode()
	if n == nil || n.ID() != nA.ID() {
		t.Fatalf("SetLocalNode(nA.ID()) should've bound the ring to %#v; instead LocalNode gave %#v", nA, n)
	}
	pbc := r.PartitionBitCount()
	if pbc != 1 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 1", pbc)
	}
	// Make sure a new Ring call doesn't alter the previous Ring.
	b.AddNode(true, 3, nil, nil, "")
	r2 := b.Ring()
	r2.SetLocalNode(nA.ID())
	pbc = r2.PartitionBitCount()
	if pbc == 1 {
		t.Fatalf("Ring2's PartitionBitCount should not have been 1")
	}
	pbc = r.PartitionBitCount()
	if pbc != 1 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 1", pbc)
	}
	ns := r2.Nodes()
	if len(ns) != 3 {
		t.Fatalf("Ring2 should've had 3 nodes; instead had %d", len(ns))
	}
	ns = r.Nodes()
	if len(ns) != 2 {
		t.Fatalf("Ring should've had 2 nodes; instead had %d", len(ns))
	}
}

func TestBuilderResizeIfNeeded(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	b.AddNode(true, 1, nil, nil, "")
	b.AddNode(true, 1, nil, nil, "")
	r := b.Ring()
	pbc := r.PartitionBitCount()
	if pbc != 1 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 1", pbc)
	}
	nC := b.AddNode(false, 3, nil, nil, "")
	r = b.Ring()
	pbc = r.PartitionBitCount()
	if pbc != 1 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 1", pbc)
	}
	nC.SetActive(true)
	r = b.Ring()
	pbc = r.PartitionBitCount()
	if pbc != 4 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 4", pbc)
	}
	// Test that shrinking does not happen (at least for now).
	b.RemoveNode(nC.ID())
	r = b.Ring()
	pbc = r.PartitionBitCount()
	if pbc != 4 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 4", pbc)
	}
	// Test partition count cap.
	pbc = b.MaxPartitionBitCount()
	if pbc != 23 {
		t.Fatalf("Expected the default max partition bit count to be 23; it was %d", pbc)
	}
	b.SetMaxPartitionBitCount(6)
	pbc = b.MaxPartitionBitCount()
	if pbc != 6 {
		t.Fatalf("Expected the max partition bit count to be saved as 6; instead it was %d", pbc)
	}
	for i := 4; i < 14; i++ {
		b.AddNode(true, uint32(i), nil, nil, "")
	}
	r = b.Ring()
	pbc = r.PartitionBitCount()
	if pbc != 6 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 6", pbc)
	}
	// Just exercises the "already at max" short-circuit.
	b.AddNode(true, 14, nil, nil, "")
	r = b.Ring()
	pbc = r.PartitionBitCount()
	if pbc != 6 {
		t.Fatalf("Ring's PartitionBitCount was %d and should've been 6", pbc)
	}
}

func TestBuilderMinimizeTiers(t *testing.T) {
	b := NewBuilder()
	n := b.AddNode(true, 1, []string{"one"}, nil, "")
	b.AddNode(true, 1, []string{"two"}, nil, "")
	b.minimizeTiers()
	if len(b.tiers) != 1 {
		t.Fatal("")
	}
	if len(b.tiers[0]) != 3 {
		t.Fatal("")
	}
	if b.tiers[0][0] != "" {
		t.Fatal("")
	}
	if b.tiers[0][1] != "one" {
		t.Fatal("")
	}
	if b.tiers[0][2] != "two" {
		t.Fatal("")
	}
	b.RemoveNode(n.ID())
	b.minimizeTiers()
	if len(b.tiers) != 1 {
		t.Fatal("")
	}
	if len(b.tiers[0]) != 2 {
		t.Fatal("")
	}
	if b.tiers[0][0] != "" {
		t.Fatal("")
	}
	if b.tiers[0][1] != "two" {
		t.Fatal("")
	}
}

func TestBuilderLowerReplicaCount(t *testing.T) {
	b := NewBuilder()
	b.SetReplicaCount(3)
	b.AddNode(true, 1, nil, nil, "")
	b.AddNode(true, 1, nil, nil, "")
	b.AddNode(true, 1, nil, nil, "")
	b.Ring()
	// ring ends up:
	// 0 2
	// 1 1
	// 2 0
	if len(b.replicaToPartitionToNodeIndex) != 3 {
		t.Fatal(len(b.replicaToPartitionToNodeIndex))
	}
	if len(b.replicaToPartitionToNodeIndex[0]) != 2 {
		t.Fatal(len(b.replicaToPartitionToNodeIndex[0]))
	}
	if b.replicaToPartitionToNodeIndex[0][0] != 0 {
		t.Fatal(b.replicaToPartitionToNodeIndex[0][0])
	}
	if b.replicaToPartitionToNodeIndex[0][1] != 2 {
		t.Fatal(b.replicaToPartitionToNodeIndex[0][1])
	}
	if b.replicaToPartitionToNodeIndex[1][0] != 1 {
		t.Fatal(b.replicaToPartitionToNodeIndex[1][0])
	}
	if b.replicaToPartitionToNodeIndex[1][1] != 1 {
		t.Fatal(b.replicaToPartitionToNodeIndex[1][1])
	}
	if b.replicaToPartitionToNodeIndex[2][0] != 2 {
		t.Fatal(b.replicaToPartitionToNodeIndex[2][0])
	}
	if b.replicaToPartitionToNodeIndex[2][1] != 0 {
		t.Fatal(b.replicaToPartitionToNodeIndex[2][1])
	}
	// dropping the replica count should just drop the last replicas so:
	// 0 2
	// 1 1
	b.SetReplicaCount(2)
	if len(b.replicaToPartitionToNodeIndex) != 2 {
		t.Fatal(len(b.replicaToPartitionToNodeIndex))
	}
	if len(b.replicaToPartitionToNodeIndex[0]) != 2 {
		t.Fatal(len(b.replicaToPartitionToNodeIndex[0]))
	}
	if b.replicaToPartitionToNodeIndex[0][0] != 0 {
		t.Fatal(b.replicaToPartitionToNodeIndex[0][0])
	}
	if b.replicaToPartitionToNodeIndex[0][1] != 2 {
		t.Fatal(b.replicaToPartitionToNodeIndex[0][1])
	}
	if b.replicaToPartitionToNodeIndex[1][0] != 1 {
		t.Fatal(b.replicaToPartitionToNodeIndex[1][0])
	}
	if b.replicaToPartitionToNodeIndex[1][1] != 1 {
		t.Fatal(b.replicaToPartitionToNodeIndex[1][1])
	}
	// Just to show that now we have 2 replicas but 3 nodes and that the
	// partition count has to jump up to try to keep good balance.
	b.PretendElapsed(math.MaxUint16)
	b.Ring()
	b.SetReplicaCount(2)
	if len(b.replicaToPartitionToNodeIndex) != 2 {
		t.Fatal(len(b.replicaToPartitionToNodeIndex))
	}
	if len(b.replicaToPartitionToNodeIndex[0]) <= 2 {
		t.Fatal(len(b.replicaToPartitionToNodeIndex[0]))
	}
}

repository: wreese/valuestore

repo_name: wreese/valuestore


text:
package repository_test

import (
	"bytes"
	"context"
	"fmt"
	"image"
	"image/png"
	"os"
	"testing"

	"github.com/google/uuid"
	"github.com/stretchr/testify/assert"

	"github.com/payboxth/go-slip/repository"
)

var (
	bucketName     string = "paybox_slip"
	credentialFile string = "/secret/paybox_slip_storage_key.json"
	testFile       string = "test_slip.png"
	folderName     string = "test"
)

func init() {
	homeDir, err := os.UserHomeDir()
	if err != nil {
		fmt.Printf("User Home Dir is: %v \nError:, %v", homeDir, err)
	}
	credentialFile = homeDir + credentialFile
}

func TestNewGCSClient(t *testing.T) {
	_, err := repository.NewGCS(bucketName, credentialFile)
	if err != nil {
		t.Errorf("Repository cannot create Storage Client: %v", err)
	}
	t.Log("Create new GCS Client: OK")
}

// ตอนรันเทส ต้องเอา paybox_slip.json ไปใส่ด้วยนะ อยู่ใน slip/repository/secret
// ตอนนี้ยังเอาไว้ทำ integration test
// TODO แต่ถ้าปล่อย package นี้เป็น lib opensource จริงคงต้องแยกทำ mock ไว้ เทสกันด้วย
func TestStoreFile(t *testing.T) {
	generateName := uuid.New().String()
	objectName := fmt.Sprintf("%s/%s", folderName, generateName)
	t.Logf("object = %v", objectName)

	s, err := repository.NewGCS(bucketName, credentialFile)
	if err != nil {
		t.Errorf("Repository cannot create Storage Client: %v", err)
	}

	ctx := context.Background()

	url, err := s.StoreFile(ctx, testFile, objectName)
	if err != nil {
		t.Fatalf("Error on s.StoreFile: %v", err)
	}
	assert.NotNil(t, url, "url should not nil")
	expected := "https://storage.googleapis.com/paybox_slip/test/"
	assert.Containsf(t, url, expected, "Return URL does not contain ecpected = %v", url)
	assert.NotZerof(t, url, "URL is not empty as: %v", url)
	t.Logf("Success storage save file and return fileName = %v URL = %v", testFile, url)

	// Teardown by delete saved file
	err = s.RemoveFile(ctx, objectName)
	if err != nil {
		t.Errorf("cannot teardown by delete saved file: %v", err)
	}
	t.Logf("Teardown by delete file: %v", objectName)
}

func TestStoreByte(t *testing.T) {
	s, err := repository.NewGCS(bucketName, credentialFile)
	if err != nil {
		t.Errorf("Repository cannot create Storage Client: %v", err)
	}

	ctx := context.Background()
	inputFile, err := os.Open(testFile)
	if err != nil {
		panic(err)
	}
	m, format, err := image.Decode(inputFile)
	if err != nil {
		t.Errorf("image.Decode error = %v", err)
	}
	if format != "png" {
		t.Errorf("file format is not png but: %v", format)
	}
	t.Logf("file format is: %v", format)
	buf := new(bytes.Buffer)
	err = png.Encode(buf, m)
	if err != nil {
		t.Errorf("Error while png.Encode() = %v", err)
	}
	b := buf.Bytes()
	generateName := uuid.New().String()
	objectName := fmt.Sprintf("%s/%s.%s", folderName, generateName, "png")
	t.Logf("objectName = %v", objectName)

	url, err := s.StoreByte(ctx, b, objectName)
	if err != nil {
		assert.NotNilf(t, err, "Error on s.StoreByte(): %v", err)
	}
	t.Logf("Success storage save byte and return URL = %v", url)

	// Teardown by delete saved file
	err = s.RemoveFile(ctx, objectName)
	if err != nil {
		t.Errorf("cannot teardown by delete saved file: %v", err)
	}
	t.Logf("Teardown by delete file: %v", objectName)
}

repository: payboxth/goslip

repo_name: payboxth/goslip


text:
package lc_go_spec

import (
	. "github.com/bmatsuo/go-spec/spec"
	"testing"
)

func TestSimpleStuff(t *testing.T) {
	s := NewSpecTest(t)

	s.Describe("Value", func() {
		s.It("can be a native Go type", func() {
			s.Spec(1, Should, Equal, 1)
			s.Spec("abc", Should, Not, Equal, 1+3i)
			s.Spec(false, Should, Not, Equal, true)
		})
		s.It("can't fail!!!", func() {
			s.Spec(1, Should, Equal, 2)
			s.Spec("abc", Should, Equal, 1+3i)
			s.Spec(false, Should, Equal, true)
		})
	})
}

repository: mindreframer/golang-testing-stuff

repo_name: mindreframer/golang-testing-stuff


text:
@article{RN1259,
 author = {Pirozzi, Domenico and Fiorentino, Nunzio and Impagliazzo, Adriana and Sannino, Filomena and Yousuf, Abu and Zuccaro, Gaetano and Fagnano, Massimo},
 doi = {https://doi.org/10.1016/j.renene.2014.12.046},
 issn = {0960-1481},
 journal = {Renewable Energy},
 pages = {456-462},
 title = {Lipid production from Arundo donax grown under different agronomical conditions},
 type = {Journal Article},
 url = {http://www.sciencedirect.com/science/article/pii/S0960148114008738},
 volume = {77},
 year = {2015}
}

repository: drnazmul/ayusuf

repo_name: drnazmul/ayusuf

extension: bib


text:
package commands

import (
	"errors"
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/ajdnik/kube-cli/executable"
	"github.com/ajdnik/kube-cli/filesystem"
	"github.com/ajdnik/kube-cli/hash"
	"github.com/ajdnik/kube-cli/tar"
	"github.com/ajdnik/kube-cli/ui"
	"github.com/ajdnik/kube-cli/web"
	humanize "github.com/dustin/go-humanize"
	"github.com/spf13/cobra"
)

// UpdateCommand executes CLI update workflow, which downloads
// latest CLI tool, verifies the download and replaces the old
// binary.
var UpdateCommand = &cobra.Command{
	Use:   "update",
	Short: "Update the command line tool",
	Long: `Update the command line tool by pulling the latest
version from the web. Make sure you have an active web connection.`,
	RunE: func(cmd *cobra.Command, args []string) error {
		spin := ui.ShowSpinner(1, "Retrieving latest version info...")
		// Get CLI binary info
		info, err := executable.GetInfo()
		if err != nil {
			ui.SpinnerFail(1, "There was a problem retrieving the latest version info.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command.")
			return err
		}
		// Retrieve latest version info
		shaName := info.Name + "_" + info.OS + "_" + info.Arch + ".sha512"
		tarName := info.Name + "_" + info.OS + "_" + info.Arch + ".tar.gz"
		release, err := web.GetLatestRelease(shaName, tarName)
		if err != nil {
			ui.SpinnerFail(1, "There was a problem retrieving the latest version info.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command. Make sure you have an active internet connection.")
			return err
		}
		ui.SpinnerSuccess(1, fmt.Sprintf("Retrieved latest version is %v.", release.Version), spin)
		if info.Version == release.Version {
			ui.SuccessMessage("The CLI tool is already updated to the latest version.")
			return nil
		}
		spin = ui.ShowSpinner(2, "Downloading CLI archive...")
		// Create temp file for downloaded archive
		tarTemp, err := filesystem.CreateTemp()
		if err != nil {
			ui.SpinnerFail(2, "There was a problem downloading CLI archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command as an administrator.")
			return err
		}
		// Download latest CLI archive
		sz, err := web.DownloadFile(tarTemp, release.TarURL)
		if err != nil {
			ui.SpinnerFail(2, "There was a problem downloading CLI archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command as an administrator.")
			return err
		}
		defer os.Remove(tarTemp)
		ui.SpinnerSuccess(2, fmt.Sprintf("Downloaded CLI archive %s.", humanize.Bytes(uint64(sz))), spin)
		// Download SHA512 sum file
		spin = ui.ShowSpinner(3, "Verifying downloaded archive...")
		// Create temp file for downloaded hash file
		shaTemp, err := filesystem.CreateTemp()
		if err != nil {
			ui.SpinnerFail(3, "There was a problem verifying the downloaded archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command as an administrator.")
			return err
		}
		// Download archive hash
		sz, err = web.DownloadFile(shaTemp, release.ShaURL)
		if err != nil {
			ui.SpinnerFail(3, "There was a problem verifying the downloaded archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command as an administrator.")
			return err
		}
		defer os.Remove(shaTemp)
		// Extract downloaded hash from file
		dSum, err := extractSum(shaTemp)
		if err != nil {
			ui.SpinnerFail(3, "There was a problem verifying the downloaded archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command.")
			return err
		}
		// Compute hash from downloaded archive
		cSum, err := hash.Sum(tarTemp)
		if err != nil {
			ui.SpinnerFail(3, "There was a problem verifying the downloaded archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command.")
			return err
		}
		// Verify hash sums match
		if dSum != cSum {
			ui.SpinnerFail(3, "There was a problem verifying the downloaded archive.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command. The downloaded archive was corrupt.")
			return errors.New("update failed, SHA512 sum missmatch")
		}
		ui.SpinnerSuccess(3, "Verified downloaded archive.", spin)
		spin = ui.ShowSpinner(4, "Updating CLI binaries...")
		// Unarchive binary
		err = tar.Unarchive(tarTemp, filepath.Dir(info.Path))
		if err != nil {
			ui.SpinnerFail(4, "There was a problem updating CLI binaries.", spin)
			ui.FailMessage("Please, retry 'kube-cli update' command as an administrator.")
			return err
		}
		ui.SpinnerSuccess(4, fmt.Sprintf("Updated CLI binaries from %v to %v.", info.Version, release.Version), spin)
		return nil
	},
}

// Extract hash sum from a .sha512 file.
func extractSum(path string) (string, error) {
	var sum string
	b, err := ioutil.ReadFile(path)
	if err != nil {
		return sum, err
	}
	flds := strings.Fields(string(b))
	if len(flds) != 2 {
		return sum, errors.New("sum extraction failed, invalid format")
	}
	return flds[0], nil
}

repository: ajdnik/kube-cli

repo_name: ajdnik/kube-cli


text:
DESCRIPTION = "Task to install headers and libraries related to addons into the SDK"
LICENSE = "MIT"
PR = "r8"

inherit packagegroup

PACKAGE_ARCH = "${MACHINE_ARCH}"

UTILS = "libdrm-dev"

UTILS_append_omap3 = " canutils-dev"
UTILS_append_ti33x = " canutils-dev"
UTILS_append_ti43x = " canutils-dev"
UTILS_append_dra7xx = " canutils-dev"

EXTRA_LIBS = ""
EXTRA_LIBS_append_omap-a15 = " cmem-dev"
EXTRA_LIBS_append_dra7xx = " libulm-dev \
                             libulm-staticdev \
                             gdbserver-c6x-dev \
"

RDEPENDS_${PN} = "\
    ${UTILS} \
    ${EXTRA_LIBS} \
"

repository: varigit/meta-arago-var

repo_name: varigit/meta-arago-var

extension: bb


text:
package xinge

import (
	"bytes"
	"encoding/json"
	"errors"
	"net/http"
)

// CommonRspEnv 信鸽推送接口通用基础返回值的environment字段
type CommonRspEnv string

const (
	// EnvProd 生产环境
	EnvProd CommonRspEnv = "product"
	// EnvDev 测试环境
	EnvDev CommonRspEnv = "dev"
)

// XingeURL 信鸽API地址
var XingeURL = "https://openapi.xg.qq.com/v3/push/app"

// PushURL 修改信鸽的请求URL
func PushURL(url string) {
	XingeURL = url
}

//PushMsgOption Option函数的原型
type PushMsgOption func(*PushMsg) error

// CommonRsp 信鸽推送接口的通用基础返回值
type CommonRsp struct {
	// TODO: doc this
	Seq int64 `json:"seq"`
	// 推送id
	PushID string `json:"push_id"`
	// 错误码
	RetCode int `json:"ret_code"`
	// 用户指定推送环境，仅支持iOS
	Environment CommonRspEnv `json:"environment"`
	// 结果描述
	ErrMsg string `json:"err_msg,omitempty"`
	// 请求正确且有额外数据时，则结果在这个字段中
	Result map[string]string `json:"result,omitempty"`
}

// AudienceType 推送目标
type AudienceType string

const (
	// AudienceTypeAll 全量推送
	AudienceTypeAll AudienceType = "all"
	// AudienceTypeTag 标签推送
	AudienceTypeTag AudienceType = "tag"
	// AudienceTypeToken  单设备推送
	AudienceTypeToken AudienceType = "token"
	// AudienceTypeTokenList  设备列表推送
	AudienceTypeTokenList AudienceType = "token_list"
	// AudienceTypeAccount 单账号推送
	AudienceTypeAccount AudienceType = "account"
	// AudienceTypeAccountList 账号列表推送
	AudienceTypeAccountList AudienceType = "account_list"
)

// Platform push API platform参数
type Platform string

const (
	//PlatformAndroid Android推送平台标识
	PlatformAndroid Platform = "android"
	// PlatformiOS 苹果推送平台标识
	PlatformiOS Platform = "ios"
)

// MessageType push API message_type参数
type MessageType string

const (
	// MessageTypeOfNotify 消息类型为通知栏消息
	MessageTypeOfNotify MessageType = "notify"
	// MessageTypeOfMsg 消息类型为透传消息(android)/静默消息(iOS)
	MessageTypeOfMsg MessageType = "message"
)

// PushMsg 推送的消息体
type PushMsg struct {
	//AudienceType 受众类型，见AudienceType类型
	AudienceType `json:"audience_type"`
	//Platform 推送平台，见Platform类型
	Platform `json:"platform"`
	//Message 消息内容
	Message `json:"message"`
	//MessageType 消息类型，见MessageType类型
	MessageType `json:"message_type"`

	//TagList 当AudienceType == AdTag时必填
	TagList *TagList `json:"tag_list,omitempty"`
	/*TokenList 当AudienceType == AdToken 或 AdTokenList 时必填的参数，
	 当AdToken时即使传了多个token，也只有第一个会被推送
	 当AdTokenList时，最多支持1000个token，同时push_id第一次请求时必须填0
	 系统会返回一个push_id = 123(例)，后续推送如果push_id填写123(例)
	则会使用跟123相同的文案推送*/
	TokenList []string `json:"token_list,omitempty"`
	//AccountList 当AudienceType == AudienceTypeAccount 或 AdAccountList 时必填的参数，
	// 当AdAccount时即使传了多个token，也只有第一个会被推送
	// 当AdAccountList时，最多支持1000个token，同时push_id第一次请求时必须填0
	// 系统会返回一个push_id = 123(例)，后续推送如果push_id填写123(例)
	//AccountList 则会使用跟123相同的文案推送
	AccountList []string `json:"account_list,omitempty"`

	//ExpireTime 	消息离线存储时间（单位为秒）
	// 最长存储时间3天，若设置为0，则默认值（3天）
	// 建议取值区间[600, 86400x3]
	//ExpireTime 第三方通道离线保存消息不同厂商标准不同
	ExpireTime int `json:"expire_time,omitempty"`

	//SendTime 	指定推送时间
	// 格式为yyyy-MM-DD HH:MM:SS
	// 若小于服务器当前时间，则会立即推送
	//SendTime 仅全量推送和标签推送支持此字段
	SendTime string `json:"send_time,omitempty"`

	//MultiPkg 	多包名推送
	//MultiPkg 当app存在多个不同渠道包（例如应用宝、豌豆荚等），推送时如果是希望手机上安装任何一个渠道的app都能收到消息那么该值需要设置为true
	MultiPkg bool `json:"multi_pkg,omitempty"`

	//LoopTimes 	循环任务重复次数
	// 仅支持全推、标签推
	//LoopTimes 建议取值[1, 15]
	LoopTimes int `json:"loop_times,omitempty"`

	//Environment 	用户指定推送环境，仅限iOS平台推送使用
	// product： 推送生产环境
	//Environment dev： 推送开发环境
	Environment CommonRspEnv `json:"environment,omitempty"`

	//StatTag 	统计标签，用于聚合统计
	// 使用场景(示例)：
	// 现在有一个活动id：active_picture_123,需要给10000个设备通过单推接口（或者列表推送等推送形式）下发消息，同时设置该字段为active_picture_123
	//StatTag 推送完成之后可以使用v3统计查询接口，根据该标签active_picture_123 查询这10000个设备的实发、抵达、展示、点击数据
	StatTag string `json:"stat_tag,omitempty"`

	//Seq 	接口调用时，在应答包中信鸽会回射该字段，可用于异步请求
	//Seq 使用场景：异步服务中可以通过该字段找到server端返回的对应应答包
	Seq int64 `json:"seq,omitempty"`

	//AccountType 单账号推送时可选
	// 	 账号类型，参考后面账号说明。
	//AccountType  必须与账号绑定时设定的账号类型一致
	AccountType int `json:"account_type,omitempty"`

	//PushID
	// 账号列表推送、设备列表推送时必需
	// 账号列表推送和设备列表推送时，第一次推送该值填0，系统会创建对应的推送任务，
	// 并且返回对应的pushid：123，后续推送push_id 填123(同一个文案）
	// 表示使用与123 id 对应的文案进行推送。(注：文案的有效时间由前面的expire_time 字段决定）
	PushID string `json:"push_id,omitempty"`
	//nextIndex 如果推送的account,token大于1000,需要轮询推送,此标志用来作为遍历的游标
	nextIndex int
}

//IPushMsg PushMsg实现的接口
type IPushMsg interface {
	RenderOptions(opts ...PushMsgOption) error
	clone(options ...PushMsgOption) IPushMsg
	nextRequest() IPushMsg
	toHttpRequest(author IAuth) (request *http.Request, err error)
	equalsPlatform(platform Platform) bool
}

//RenderOptions 使用Option来动态修改PushMsg的内容
func (rst *PushMsg) RenderOptions(opts ...PushMsgOption) error {
	for _, opt := range opts {
		err := opt(rst)
		return err
	}
	return nil
}
func (rst *PushMsg) clone(options ...PushMsgOption) IPushMsg {
	request := PushMsg{
		AudienceType: rst.AudienceType,
		Platform:     rst.Platform,
		Message:      rst.Message,
		MessageType:  rst.MessageType,
		TagList:      rst.TagList,
		TokenList:    rst.TokenList,
		AccountList:  rst.AccountList,
		ExpireTime:   rst.ExpireTime,
		SendTime:     rst.SendTime,
		MultiPkg:     rst.MultiPkg,
		LoopTimes:    rst.LoopTimes,
		Environment:  rst.Environment,
		StatTag:      rst.StatTag,
		Seq:          rst.Seq,
		AccountType:  rst.AccountType,
		PushID:       rst.PushID}
	err := request.RenderOptions(options...)
	if err != nil {
		return nil
	}
	return &request
}
func (rst *PushMsg) nextRequest() IPushMsg {
	var request IPushMsg
	if rst.AudienceType == AudienceTypeAccountList {
		request = rst.clone(sliceAccountList)
	} else if rst.AudienceType == AudienceTypeTokenList {
		request = rst.clone(sliceTokenList)
	} else if rst.nextIndex == 0 {
		rst.nextIndex = -1
		request = rst
	} else {
		request = nil
	}
	return request
}

//equalsPlatform 比较Platform是否一致
func (rst *PushMsg) equalsPlatform(platform Platform) bool {
	return rst.Platform == platform
}

func (rst PushMsg) toHttpRequest(author IAuth) (request *http.Request, err error) {

	bodyBytes, err := json.Marshal(rst)
	if err != nil {
		return nil, err
	}
	request, err = http.NewRequest("POST", XingeURL, bytes.NewReader(bodyBytes))
	if err != nil {
		return nil, err
	}
	author.authRequest(request)
	return
}
func sliceAccountList(rst *PushMsg) error {
	lens := len(rst.AccountList)
	if lens > rst.nextIndex { //大于1000需要二次提交,如果此处长度大于nextIndex,则说明还有(account或者token没有推送)
		end := rst.nextIndex + 1000
		if end > lens {
			end = lens - rst.nextIndex
		}
		rst.AccountList = rst.AccountList[rst.nextIndex:end]
		return nil
	}
	return errors.New("index out of range!")
}
func sliceTokenList(rst *PushMsg) error {
	lens := len(rst.TokenList)
	if lens > rst.nextIndex { //大于1000需要二次提交,如果此处长度大于nextIndex,则说明还有(account或者token没有推送)
		end := rst.nextIndex + 1000
		if end > lens {
			end = lens - rst.nextIndex
		}
		rst.TokenList = rst.TokenList[rst.nextIndex:end]
		rst.nextIndex = end
		return nil
	}
	return errors.New("index out of range!")
}

// TagOperation 标签推送参数的逻辑操作符
type TagOperation string

const (
	// TagOperationAnd 推送tag1且tag2
	TagOperationAnd TagOperation = "AND"
	// TagOperationOr 推送tag1或tag2
	TagOperationOr TagOperation = "OR"
)

// TagList 标签推送参数
type TagList struct {
	//Tags 标签
	Tags []string `json:"tags"`
	//TagOperation 标签逻辑操作符
	TagOperation `json:"op"`
}

// Message 消息体
type Message struct {
	Title      string   `json:"title,omitempty"`
	Content    string   `json:"content,omitempty"`
	AcceptTime []string `json:"accept_time,omitempty"`
	//Android推送的参数
	Android *AndroidParams `json:"android,omitempty"`
	//IOS推送的参数
	IOS *IOSParams `json:"ios,omitempty"`
}

// AndroidParams 安卓push参数
type AndroidParams struct {
	NID           int                    `json:"n_id,omitempty"`
	BuilderID     int                    `json:"builder_id,omitempty"`
	Ring          int                    `json:"ring,omitempty"`
	RingRaw       string                 `json:"ring_raw,omitempty"`
	Vibrate       int                    `json:"vibrate,omitempty"`
	Lights        int                    `json:"lights,omitempty"`
	Cleanable     int                    `json:"clearable,omitempty"`
	IconType      int                    `json:"icon_type,omitempty"`
	IconRes       string                 `json:"icon_res,omitempty"`
	StyleID       int                    `json:"style_id,omitempty"`
	SmallIcon     int                    `json:"small_icon,omitempty"`
	Action        map[string]interface{} `json:"action,omitempty"`
	CustomContent map[string]string      `json:"custom_content,omitempty"`
}

// IOSParams iOS push参数
type IOSParams struct {
	Aps    *Aps              `json:"aps,omitempty"`
	Custom map[string]string `json:"custom,omitempty"`
}

// Aps 通知栏iOS消息的aps字段，详情请参照苹果文档 https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
type Aps struct {
	Alert            `json:"alert,omitempty"`
	Badge            int    `json:"badge,omitempty"`
	Category         string `json:"category,omitempty"`
	ContentAvailable int    `json:"content-available,omitempty"`
	Sound            string `json:"sound,omitempty"`
	ThreadId         string `json:"thread_id,omitempty"`
}

//Alert 自定义Alert的数据类型
type Alert map[string]interface{}

/*
Alert包含以下几个子属性：
title			String
body			String
title-loc-key	String or null
title-loc-args	Array of strings or null
action-loc-key	String or null
loc-key			String
loc-args		Array of strings
launch-image	String

其中，title,body应该是必须的。详细请参考：
https://developer.apple.com/library/archive/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html#//apple_ref/doc/uid/TP40008194-CH17-SW1
*/

//Set 设置Key-Value
func (alert Alert) Set(key string, value interface{}) {
	alert[key] = value
}

//SetTitle 设置标题
func (alert Alert) SetTitle(title string) {
	alert.Set("title", title)
}

//SetBody 设置提示详情
func (alert Alert) SetBody(content string) {
	alert.Set("body", content)
}

//SetTitleLocKey 设置相关属性
func (alert Alert) SetTitleLocKey(data string) {
	alert.Set("title-loc-key", data)
}

//SetTitleLocArgs  设置相关属性
func (alert Alert) SetTitleLocArgs(data []string) {
	alert.Set("title-loc-args", data)
}

//SetActionLocKey  设置相关属性
func (alert Alert) SetActionLocKey(data string) {
	alert.Set("action-loc-key", data)
}

//SetLocKey  设置相关属性
func (alert Alert) SetLocKey(data string) {
	alert.Set("loc-key", data)
}

//SetLocArgs  设置相关属性
func (alert Alert) SetLocArgs(data []string) {
	alert.Set("loc-args", data)
}

//SetLaunchImage  设置相关属性
func (alert Alert) SetLaunchImage(data string) {
	alert.Set("launch-image", data)
}

//DefaultAndroidParams 默认的Android推送参数
func DefaultAndroidParams() *AndroidParams {
	return &AndroidParams{NID: 0, BuilderID: 0, Ring: 1, Vibrate: 1, Lights: 0}
}

//DefaultIOSParams 默认的iOS推送参数
func DefaultIOSParams(title string, content string) *IOSParams {
	return &IOSParams{Aps: &Aps{Alert: DefaultApsAlert(title, content)}}
}

//DefaultApsAlert 创建默认的Alert
func DefaultApsAlert(title string, body string) Alert {
	alert := Alert{"title": title, "body": body}
	return alert
}

//DefaultAps 创建默认的Aps
func DefaultAps(title string, content string) *Aps {
	return &Aps{Alert: DefaultApsAlert(title, content)}
}

repository: kmlixh/xinge

repo_name: kmlixh/xinge


text:
# Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

INCLUDE(ExternalProject)

SET(LEVELDB_SOURCES_DIR ${THIRD_PARTY_PATH}/leveldb)
SET(LEVELDB_INSTALL_DIR ${THIRD_PARTY_PATH}/install/leveldb)
SET(LEVELDB_INCLUDE_DIR "${LEVELDB_INSTALL_DIR}/include" CACHE PATH "leveldb include directory." FORCE)
SET(LEVELDB_LIBRARIES "${LEVELDB_INSTALL_DIR}/lib/libleveldb.a" CACHE FILEPATH "leveldb library." FORCE)
INCLUDE_DIRECTORIES(${LEVELDB_INCLUDE_DIR})

## level must be compile with CXXFLAGS=-fPIC for brpc
## but it's pretty hard to pass it to BUILD_COMMAND
## so we sed file to patch Makefile
ExternalProject_Add(
        extern_leveldb
        ${EXTERNAL_PROJECT_LOG_ARGS}
        PREFIX ${LEVELDB_SOURCES_DIR}
        GIT_REPOSITORY "https://github.com/google/leveldb"
        GIT_TAG "v1.18"
        CONFIGURE_COMMAND sed -i "s/CXXFLAGS += -fPIC//" ${LEVELDB_SOURCES_DIR}/src/extern_leveldb/Makefile COMMAND sed -i "/CXXFLAGS +=/aCXXFLAGS += -fPIC" ${LEVELDB_SOURCES_DIR}/src/extern_leveldb/Makefile
        BUILD_IN_SOURCE 1
        BUILD_COMMAND $(MAKE) -j ${NUM_OF_PROCESSOR} libleveldb.a
        INSTALL_COMMAND mkdir -p ${LEVELDB_INSTALL_DIR}/lib/ COMMAND cp ${LEVELDB_SOURCES_DIR}/src/extern_leveldb/libleveldb.a ${LEVELDB_LIBRARIES} COMMAND cp -r ${LEVELDB_SOURCES_DIR}/src/extern_leveldb/include ${LEVELDB_INSTALL_DIR}/
)

ADD_LIBRARY(leveldb STATIC IMPORTED GLOBAL)
SET_PROPERTY(TARGET leveldb PROPERTY IMPORTED_LOCATION ${LEVELDB_LIBRARIES})
ADD_DEPENDENCIES(leveldb extern_leveldb)

repository: duzhanyuan/BaikalDB

repo_name: duzhanyuan/BaikalDB


text:
require linux.inc

DESCRIPTION = "Linux kernel for EfikaMX platform"
KERNEL_IMAGETYPE = "uImage"

COMPATIBLE_MACHINE = "efikamx"

python do_fetch () {
        import os,shutil
        bb.build.exec_func("base_do_fetch", d)
        if os.access(bb.data.expand("${DL_DIR}/73", d), os.R_OK):
                shutil.copy(bb.data.expand("${DL_DIR}/73", d), bb.data.expand("${DL_DIR}/${PN}-${PV}.tar.lzma", d))

        if os.access(bb.data.expand("${DL_DIR}/83", d), os.R_OK):
                shutil.copy(bb.data.expand("${DL_DIR}/83", d), bb.data.expand("${DL_DIR}/patch-${PV}.tar.lzma", d))
}

python do_unpack () {
       bb.build.exec_func('efikamx_kernel_do_unpack', d)
}

efikamx_kernel_do_unpack () {
       mkdir -p ${S}
       tar xf ${DL_DIR}/${PN}-${PV}.tar.lzma --strip-components=1 -C ${S}
       tar xf ${DL_DIR}/patch-${PV}.tar.lzma -C ${S}
}

python do_patch () {
       bb.build.exec_func('efikamx_kernel_do_patch', d)
}

efikamx_kernel_do_patch (){
       ( cd ${S}/patches; ls *.patch >series;cp *.config ${S}/../defconfig)
       ( cd ${S}; quilt push -a)
}

SRC_URI += "http://www.powerdeveloper.org/asset/by-id/73;name=kernel \
            http://www.powerdeveloper.org/asset/by-id/83;name=patch \
           "

SRC_URI[kernel.md5sum] = "c70ce0549cf85de79d5b28db7b552868"
SRC_URI[kernel.sha256sum] = "658395072352cd38477d80c105f00d824c73bf437f0843579d1e0bf41066617f"

SRC_URI[patch.md5sum] = "6c04416bce74dd413f3e82cd17378419"
SRC_URI[patch.sha256sum] = "5c15cffa02089d286fc07ba734b1f33a81d1f7ceaf70e596bd8b82ddd5e73ec2"

S = "${WORKDIR}/${PN}-${PV}"

repository: libo/openembedded

repo_name: libo/openembedded

branch_name: HEAD


text:
package conf

import (
	"fmt"
	"io"

	"github.com/burntsushi/toml"
	"github.com/pkg/errors"
)

// Rewrites represents rewrites config.
type Rewrites struct {
	Rewrite []Rewrite
}

// Rewrite in configuration for a single rewrite rule.
type Rewrite struct {
	From string
	To   string
	Copy bool
}

// ReadRewrites reads rules from the reader. Errors when parsing fails.
func ReadRewrites(r io.Reader) (Rewrites, error) {
	var rewrites Rewrites

	_, err := toml.DecodeReader(r, &rewrites)
	if err != nil {
		return rewrites, errors.Wrap(err, "error parsing rewrites")
	}
	if len(rewrites.Rewrite) == 0 {
		return rewrites, fmt.Errorf("no rules specified in the rules file")
	}
	for idx, rewrite := range rewrites.Rewrite {
		if len(rewrite.From) == 0 {
			return rewrites, fmt.Errorf("rewrite %d is missing 'From' section", idx)
		}
		if len(rewrite.To) == 0 {
			return rewrites, fmt.Errorf("rule %d is missing 'clusters' section", idx)
		}
	}
	return rewrites, nil
}

repository: bookingcom/nanotube

repo_name: bookingcom/nanotube

gha_language: Go


text:
#pragma once
#include <vertex/node.h>
#include <algorithm>
#include <cassert>
#include <map>
#include <memory>
#include <queue>
#include <set>
#include <type_traits>

namespace vertex {

/** ManagedContainer is a specialised AssociativeArray with reference
 * counting to ensure unreferenced Node objects are deleted from storage.
 * Container is a map which stores Node objects by Key
 * EdgeMap is a multimap which stores Node parents as pairs of Edge objects
 * Container MUST NOT invalidate iterators on insertion or deletion
 */
template <typename Container, typename EdgeMap>
class managed_container {
 public:
  static_assert(std::is_same<typename Container::key_type,
                             typename EdgeMap::key_type>::value,
                "Container::key_type != EdgeMap::key_type");
  static_assert(std::is_same<typename EdgeMap::key_type,
                             typename EdgeMap::value_type::second_type>::value,
                "EdgeMap::key_type != EdgeMap::value_type::second_type");

  using key_type = typename Container::key_type;
  using mapped_type = typename Container::mapped_type;
  using value_type = typename Container::value_type;
  using size_type = typename Container::size_type;
  using difference_type = typename Container::difference_type;
  using key_compare = typename Container::key_compare;
  using reference = value_type&;
  using const_reference = const value_type&;
  using pointer = typename Container::pointer;
  using const_pointer = typename Container::const_pointer;
  using iterator = typename Container::iterator;
  using const_iterator = typename Container::const_iterator;
  using reverse_iterator = typename Container::reverse_iterator;
  using const_reverse_iterator = typename Container::const_reverse_iterator;

  explicit managed_container(Container vertices = Container(),
                             EdgeMap edges = EdgeMap());

  iterator begin() const;
  const_iterator cbegin() const;
  iterator end() const;
  const_iterator cend() const;

  /** Insert a vertex, creating edges from all its children
   * Every child must exist */
  std::pair<iterator, bool> insert(const value_type& value);

  /** Find the vertex stored under the given key */
  iterator find(const key_type& key);

  /** Remove vertex at the specified position.
   *  The reference count of all child vertices will be decremented */
  iterator erase(iterator pos);

  /** Get reference count for vertex with given key */
  size_type count(const key_type& key);

  /** Erase the whole forest */
  void clear();

 private:
  using edge_type = typename EdgeMap::value_type;
  using edge_iterator = typename EdgeMap::iterator;

  /** Get an iterator to the edge matching the argument */
  edge_iterator find(const edge_type& edge);

  /** Remove the given vertex edge */
  edge_iterator erase(const edge_type& edge);

  /** Remove the vertex edge at the given position.
   * The given position must be valid. */
  edge_iterator erase(edge_iterator pos);

  Container vertices_;
  EdgeMap edges_;
};

template <typename V, typename E>
managed_container<V, E>::managed_container(V vertices, E edges)
    : vertices_(std::move(vertices)), edges_(std::move(edges)) {}

template <typename V, typename E>
std::pair<typename managed_container<V, E>::iterator, bool>
managed_container<V, E>::insert(
    const typename managed_container<V, E>::value_type& value) {
  auto result = vertices_.insert(value); /** store the vertex */
  for (const auto& link :
       value.second
           .children()) {  // add a edge from vertex to each of its children
    edges_.insert(std::make_pair(link.key(), value.first));
  }
  return result;
}

template <typename V, typename E>
typename managed_container<V, E>::iterator managed_container<V, E>::find(
    const typename managed_container<V, E>::key_type& key) {
  return vertices_.find(key);
}

template <typename V, typename E>
typename managed_container<V, E>::iterator managed_container<V, E>::erase(
    typename managed_container<V, E>::iterator pos) {
  auto result = pos;
  const auto& edge = pos->first;
  const auto& vertex = pos->second;
  assert(vertex.self_edge() == edge);

  if (edges_.count(edge) == 0) {  // Erase vertex iff no references to it
    for (const auto& child :
         vertex.edges_) {  // remove edges from vertex to its children
      auto child_edge = edge_type(child, edge);
      erase(child_edge);
    }
    result = vertices_.erase(pos);
  }
  return result;
}

/** TODO rewrite using traversal */
template <typename V, typename E>
typename managed_container<V, E>::edge_iterator managed_container<V, E>::erase(
    typename managed_container<V, E>::edge_iterator pos) {
  auto result = pos;
  std::queue<edge_iterator> to_visit;
  to_visit.push(pos);
  while (!to_visit.empty()) {
    pos = to_visit.front();
    to_visit.pop();
    assert(pos != edges_.end());
    auto child = pos->first;
    result = edges_.erase(pos);
    if (0 == edges_.count(
                 child)) {  // reference count of child vertex is zero, so erase
      auto vertex = vertices_.find(child);
      if (vertices_.end() != vertex) {
        for (const auto& grandchild : vertex->second.edges_) {
          auto it = find(edge_type(grandchild, vertex->first));
          to_visit.push(it);
        }
        vertices_.erase(vertex);
      }
    }
  }
  return result;
}

template <typename V, typename E>
typename managed_container<V, E>::edge_iterator managed_container<V, E>::find(
    const typename managed_container<V, E>::edge_type& edge) {
  auto result = edges_.cend();
  auto range = edges_.equal_range(edge.first);
  auto it = std::find(range.first, range.second, edge);
  if (it != range.second) {
    result = it;
  }
  return result;
}

template <typename V, typename E>
typename managed_container<V, E>::edge_iterator managed_container<V, E>::erase(
    const typename managed_container<V, E>::edge_type& edge) {
  auto result = find(edge);
  if (result != edges_.end()) {
    result = erase(result);
  }
  return result;
}

template <typename V, typename E>
typename managed_container<V, E>::size_type managed_container<V, E>::count(
    const typename managed_container::key_type& key) {
  return edges_.count(key);
}

template <typename V, typename E>
void managed_container<V, E>::clear() {
  edges_.clear();
  vertices_.clear();
}

}  // namespace vertex

repository: lockblox/vertex

repo_name: lockblox/vertex


text:
@inproceedings{Vinyals2015PointerN,
  title={Pointer Networks},
  author={Oriol Vinyals and Meire Fortunato and Navdeep Jaitly},
  booktitle={NIPS},
  year={2015}
}

repository: rbt-c/DL-NLP-Readings

path: /Bibtex/Pointer Networks.bib

repo_name: rbt-c/DL-NLP-Readings

filename: Pointer Networks.bib


text:
package templates

// Dockerfile ...
var Dockerfile = `# Code generated by {{ .Generator }} {{ .Version }} ({{ .Build }}); DO NOT EDIT

FROM golang:1.15.2-alpine3.12 AS compile
WORKDIR /go/src/app
COPY . .
ENV CGO_ENABLED 0
RUN go build -ldflags "-s -w -X=main.build={{ .GitBuild }} -X=main.name={{ .Name }} -X=main.version={{ .GitVersion }}" -o /go/src/app/app main.go

FROM scratch
COPY --from=compile /go/src/app/app /app
COPY --from=compile /go/src/app/web/static /web/static
CMD ["/app"]
`

repository: andygeiss/create-go-app

repo_name: andygeiss/create-go-app


text:
// Copyright 2013, 2014 MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// A thread-safe fixed-capacity queue.  Deletes last item on overflow

package queue

type Queue struct {
	items           chan interface{}
	onForcedDequeue func(interface{})
}

func New(capacity int, onForcedDequeue func(interface{})) *Queue {
	return &Queue{
		make(chan interface{}, capacity),
		onForcedDequeue,
	}
}

func (q *Queue) Cap() int {
	return cap(q.items)
}

func (q *Queue) Dequeue() (interface{}, error) {
	select {
	case item := <-q.items:
		return item, nil
	default:
		return nil, UnderflowError{}
	}
}

func (q *Queue) Enqueue(item interface{}) {
	for {
		select {
		case q.items <- item:
			return
		default:
			// q.items must be full
			// force a dequeue in order to make room
			select {
			case item := <-q.items:
				if q.onForcedDequeue != nil {
					q.onForcedDequeue(item)
				}
			default:
				// wow, we went from full to empty very fast.  let's start over
			}
		}
	}
}

func (q *Queue) IsEmpty() bool {
	return q.Len() == 0
}

func (q *Queue) IsFull() bool {
	return q.Len() == q.Cap()
}

func (q *Queue) Len() int {
	return len(q.items)
}

type UnderflowError struct {
}

func (UnderflowError) Error() string {
	return "Queue underflowed"
}

repository: mongodb/slogger

repo_name: mongodb/slogger


text:
package dbstore

import (
	"errors"
	"fmt"
	"reflect"
	"strings"
	"time"

	// . "github.com/ztrade/ztrade/pkg/define"
	// . "github.com/ztrade/ztrade/pkg/event"

	// . "github.com/SuperGod/trademodel"
	"github.com/go-xorm/xorm"
	log "github.com/sirupsen/logrus"
)

// TimeData data with time info
type TimeData interface {
	GetStart() int64
	Time() time.Time
	GetTable() string
	SetTable(string)
}

type DataCreator interface {
	Sing() TimeData
	Slice() interface{}
	GetSlice(interface{}) []interface{}
}

// TimeTbl tbl with time info
type TimeTbl struct {
	db       *DBStore
	exchange string
	symbol   string
	binSize  string
	table    string
	creator  DataCreator
	closeCh  chan bool
}

// NewTimeTbl create new time table
func NewTimeTbl(db *DBStore, creator DataCreator, exchange, symbol, binSize, extName string) (t *TimeTbl) {
	t = new(TimeTbl)
	t.db = db
	t.creator = creator
	t.exchange = exchange
	t.symbol = symbol
	t.binSize = binSize

	t.table = fmt.Sprintf("%s_%s_%s", exchange, symbol, binSize)
	if extName != "" {
		t.table += "_" + extName
	}
	if creator.Sing == nil || creator.Slice == nil {
		log.Error("TimeTbl data creator can't be nil")
		t = nil
	}
	return
}

func (t *TimeTbl) SetCloseCh(closeCh chan bool) {
	t.closeCh = closeCh
}

func (t *TimeTbl) getTable() (sess *xorm.Session) {
	data := t.creator.Sing()
	sess = t.db.GetTableSession(t.table, data)
	return
}

func (t *TimeTbl) GetSymbol() string {
	return t.symbol
}

func (t *TimeTbl) GetTable() string {
	return t.table
}

func (t *TimeTbl) GetDatas(since, end time.Time, limit int) (datas []interface{}, err error) {
	return t.getDatasWithParam(since, end, limit, 0)
}

func (t *TimeTbl) getDatasWithParam(since, end time.Time, limit, offset int) (datas []interface{}, err error) {
	ret := t.creator.Slice()
	sess := t.getTable()
	defer sess.Close()
	err = sess.Asc("start").Where("start>=? and start<?", since.Unix(), end.Unix()).Limit(limit, offset).Find(ret)
	if err != nil {
		return
	}
	datas = t.creator.GetSlice(ret)
	return
}

func (t *TimeTbl) DataRecent(recent int32, bSize string) (klines []interface{}, err error) {
	if bSize != t.binSize {
		err = fmt.Errorf("kline tbl %s binsize error: %s", t.table, bSize)
		return
	}
	ret := t.creator.Slice()
	sess := t.getTable()
	defer sess.Close()
	err = sess.Desc("start").Limit(int(recent), 0).Find(ret)
	if err != nil {
		return
	}
	datas := t.creator.GetSlice(ret)
	klines = make([]interface{}, len(datas))
	for k, v := range datas {
		klines[len(klines)-k-1] = v
	}
	return
}

func (t *TimeTbl) DataChan(start, end time.Time, bSize string) (klines chan []interface{}, err error) {
	if bSize != t.binSize {
		err = fmt.Errorf("kline tbl %s binsize error: %s", t.table, bSize)
		return
	}
	klines = make(chan []interface{}, 10240)
	go func() {
		nOffset := 0
		once := 500
		var err1 error
		var data []interface{}
		for {
			data, err1 = t.getDatasWithParam(start, end, once, nOffset)
			if err1 != nil {
				break
			}
			if len(data) == 0 {
				break
			}
			nOffset += len(data)
			klines <- data
			if len(data) < once {
				break
			}
		}
		if err1 != nil {
			log.Error("TimeTbl DataChan getDatas failed:", err1.Error())
		}
		close(klines)
	}()
	return
}

func (tbl *TimeTbl) IsEmpty() (isEmpty bool) {
	isEmpty = true
	sess := tbl.getTable()
	defer sess.Close()
	n, err := sess.Count()
	if err != nil {
		log.Errorf("table:%s count failed:%s", tbl.table, err.Error())
		return
	}
	if n > 0 {
		isEmpty = false
	}
	return
}

func (tbl *TimeTbl) GetNewest() (t time.Time) {
	sess := tbl.getTable()
	defer sess.Close()
	data := tbl.creator.Sing()
	_, err := sess.Desc("start").Limit(1, 0).Get(data)
	if err != nil {
		log.Errorf("TimeTbl get newest %s failed:%s", tbl.table, err.Error())
		return
	}
	t = data.Time()
	return
}

func (tbl *TimeTbl) GetOldest() (t time.Time) {
	sess := tbl.getTable()
	defer sess.Close()
	data := tbl.creator.Sing()
	_, err := sess.Asc("start").Limit(1, 0).Get(data)
	if err != nil {
		log.Errorf("TimeTbl get newest %s failed:%s", tbl.table, err.Error())
		return
	}
	t = data.Time()
	return
}

// Exists check if data's time exists
func (t *TimeTbl) Exists(data interface{}) (bRet bool, err error) {
	sess := t.getTable()
	if sess == nil {
		err = errors.New("no such table")
		return
	}
	defer sess.Close()
	v, ok := data.(TimeData)
	if !ok {
		err = fmt.Errorf("UpdateData type error:%s", reflect.TypeOf(v))
		return
	}
	bRet, err = sess.Table(t.table).Where("start=?", v.GetStart()).Exist()
	return
}

func (t *TimeTbl) AddOrUpdateData(data interface{}) (err error) {
	err = t.UpdateData(data)
	if err != nil {
		err = t.WriteData(data)
	}
	return
}

// UpdateData update datas
func (t *TimeTbl) UpdateData(data interface{}) (err error) {
	sess := t.getTable()
	if sess == nil {
		err = errors.New("no such table")
		return
	}
	defer sess.Close()
	var v TimeData
	v, ok := data.(TimeData)
	if !ok {
		err = fmt.Errorf("UpdateData type error:%s", reflect.TypeOf(v))
		return
	}
	v.SetTable(t.table)
	n, err := sess.Table(t.table).Where("start=?", v.GetStart()).UseBool().Update(data)
	if err != nil {
		log.Errorf("TimeTbl update data %s error:%s", v, err.Error())
		return
	}
	if n == 0 {
		err = fmt.Errorf("no such data")
	}
	return
}

// WriteData write data
func (t *TimeTbl) WriteData(data interface{}) (err error) {
	sess := t.getTable()
	if sess == nil {
		err = errors.New("no such table")
		return
	}
	defer sess.Close()

	var bRet bool
	var v TimeData

	v, ok := data.(TimeData)
	if !ok {
		err = fmt.Errorf("WriteData type error:%s", reflect.TypeOf(v))
		return
	}
	v.SetTable(t.table)
	bRet, err = sess.Table(t.table).Where("start=?", v.GetStart()).Exist()
	if err != nil {
		log.Errorf("TimeTbl check exist %s error:%s", v, err.Error())
		return
	}
	if bRet {
		log.Debugf("insert %s exist", v)
		return
	}
	_, err = sess.Insert(data)
	if err != nil {
		log.Errorf("TimeTbl insert %s error:%s", v, err.Error())
	}
	return
}

// WriteDatas write datas
func (t *TimeTbl) WriteDatas(datas []interface{}) (err error) {
	sess := t.getTable()
	if sess == nil {
		err = errors.New("no such table")
		return
	}
	defer sess.Close()
	err = sess.Begin()
	if err != nil {
		return
	}

	var v TimeData
	for _, data := range datas {
		v = data.(TimeData)
		v.SetTable(t.table)
		_, err = sess.Insert(data)
		if err != nil {
			if strings.Contains(err.Error(), "Duplicate entry") {
				log.Debugf("TimeTbl insert %s error:%#v", v, err)
			} else {
				log.Errorf("TimeTbl insert %s error:%#v", v, err)
			}
		}
		err = nil
	}
	sess.Commit()
	return
}

func (tbl *TimeTbl) Count() (n int64, err error) {
	sess := tbl.getTable()
	defer sess.Close()
	n, err = sess.Count()
	return
}


text:
// Copyright 2023 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package scheduler

import (
	"context"
	"fmt"
	"sync"

	"github.com/pingcap/errors"
	"github.com/pingcap/tidb/disttask/framework/proto"
	"github.com/pingcap/tidb/util/logutil"
	"go.uber.org/zap"
)

// InternalSchedulerImpl is the implementation of InternalScheduler.
type InternalSchedulerImpl struct {
	ctx       context.Context
	cancel    context.CancelFunc
	id        string
	taskID    int64
	taskTable TaskTable
	pool      Pool
	wg        sync.WaitGroup
	logCtx    context.Context

	mu struct {
		sync.RWMutex
		err error
		// runtimeCancel is used to cancel the Run/Rollback when error occurs.
		runtimeCancel context.CancelFunc
	}
}

// NewInternalScheduler creates a new InternalScheduler.
func NewInternalScheduler(ctx context.Context, id string, taskID int64, taskTable TaskTable, pool Pool) InternalScheduler {
	logPrefix := fmt.Sprintf("id: %s, task_id: %d", id, taskID)
	schedulerImpl := &InternalSchedulerImpl{
		id:        id,
		taskID:    taskID,
		taskTable: taskTable,
		pool:      pool,
		logCtx:    logutil.WithKeyValue(context.Background(), "scheduler", logPrefix),
	}
	schedulerImpl.ctx, schedulerImpl.cancel = context.WithCancel(ctx)

	return schedulerImpl
}

// Start starts the scheduler.
func (*InternalSchedulerImpl) Start() {
	//	s.wg.Add(1)
	//	go func() {
	//		defer s.wg.Done()
	//		s.heartbeat()
	//	}()
}

// Stop stops the scheduler.
func (s *InternalSchedulerImpl) Stop() {
	s.cancel()
	s.wg.Wait()
}

//	func (s *InternalSchedulerImpl) heartbeat() {
//		ticker := time.NewTicker(proto.HeartbeatInterval)
//		for {
//			select {
//			case <-s.ctx.Done():
//				return
//			case <-ticker.C:
//				if err := s.subtaskTable.UpdateHeartbeat(s.id, s.taskID, time.Now()); err != nil {
//					s.onError(err)
//					return
//				}
//			}
//		}
//	}

// Run runs the scheduler task.
func (s *InternalSchedulerImpl) Run(ctx context.Context, task *proto.Task) error {
	runCtx, runCancel := context.WithCancel(ctx)
	defer runCancel()
	s.registerCancelFunc(runCancel)

	s.resetError()
	logutil.Logger(s.logCtx).Info("scheduler run a step", zap.Any("step", task.Step), zap.Any("concurrency", task.Concurrency))
	scheduler, err := createScheduler(task)
	if err != nil {
		s.onError(err)
		return s.getError()
	}

	if err := scheduler.InitSubtaskExecEnv(runCtx); err != nil {
		s.onError(err)
		return s.getError()
	}
	defer func() {
		err := scheduler.CleanupSubtaskExecEnv(runCtx)
		if err != nil {
			logutil.Logger(s.logCtx).Error("cleanup subtask exec env failed", zap.Error(err))
		}
	}()

	minimalTaskCh := make(chan func(), task.Concurrency)
	defer close(minimalTaskCh)
	var minimalTaskWg sync.WaitGroup

	err = s.pool.RunWithConcurrency(minimalTaskCh, uint32(task.Concurrency))
	if err != nil {
		s.onError(err)
		return s.getError()
	}

	for {
		subtask, err := s.taskTable.GetSubtaskInStates(s.id, task.ID, proto.TaskStatePending)
		if err != nil {
			s.onError(err)
			break
		}
		if subtask == nil {
			logutil.Logger(s.logCtx).Info("scheduler finished subtasks", zap.Any("step", task.Step))
			break
		}
		s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateRunning, "")
		if err := s.getError(); err != nil {
			break
		}

		var minimalTasks []proto.MinimalTask
		minimalTasks, err = scheduler.SplitSubtask(runCtx, subtask.Meta)
		if err != nil {
			s.onError(err)
			if errors.Cause(err) == context.Canceled {
				s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateCanceled, "")
			} else {
				s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateFailed, s.getError().Error())
			}
			break
		}
		logutil.Logger(s.logCtx).Info("split subTask", zap.Any("cnt", len(minimalTasks)), zap.Any("subtask_id", subtask.ID))
		for _, minimalTask := range minimalTasks {
			minimalTaskWg.Add(1)
			j := minimalTask
			minimalTaskCh <- func() {
				s.runMinimalTask(runCtx, j, task.Type, task.Step)
				minimalTaskWg.Done()
			}
		}
		minimalTaskWg.Wait()
		if err := s.getError(); err == nil {
			if err := scheduler.OnSubtaskFinished(runCtx, subtask.Meta); err != nil {
				s.onError(err)
			}
		}
		if err := s.getError(); err != nil {
			if errors.Cause(err) == context.Canceled {
				s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateCanceled, "")
			} else {
				s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateFailed, s.getError().Error())
			}
			break
		}
		s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateSucceed, "")
	}

	return s.getError()
}

func (s *InternalSchedulerImpl) runMinimalTask(minimalTaskCtx context.Context, minimalTask proto.MinimalTask, tp string, step int64) {
	logutil.Logger(s.logCtx).Info("scheduler run a minimalTask", zap.Any("step", step), zap.Any("minimal_task", minimalTask))
	select {
	case <-minimalTaskCtx.Done():
		s.onError(minimalTaskCtx.Err())
		return
	default:
	}
	if s.getError() != nil {
		return
	}

	executor, err := createSubtaskExecutor(minimalTask, tp, step)
	if err != nil {
		s.onError(err)
		return
	}

	if err = executor.Run(minimalTaskCtx); err != nil {
		s.onError(err)
	}
}

// Rollback rollbacks the scheduler task.
func (s *InternalSchedulerImpl) Rollback(ctx context.Context, task *proto.Task) error {
	rollbackCtx, rollbackCancel := context.WithCancel(ctx)
	defer rollbackCancel()
	s.registerCancelFunc(rollbackCancel)

	s.resetError()
	logutil.Logger(s.logCtx).Info("scheduler rollback a step", zap.Any("step", task.Step))
	scheduler, err := createScheduler(task)
	if err != nil {
		s.onError(err)
		return s.getError()
	}
	subtask, err := s.taskTable.GetSubtaskInStates(s.id, task.ID, proto.TaskStateRevertPending)
	if err != nil {
		s.onError(err)
		return s.getError()
	}
	if subtask == nil {
		logutil.BgLogger().Warn("scheduler rollback a step, but no subtask in revert_pending state", zap.Any("step", task.Step))
		return nil
	}
	s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateReverting, "")
	if err := s.getError(); err != nil {
		return err
	}

	err = scheduler.Rollback(rollbackCtx)
	if err != nil {
		s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateRevertFailed, "")
		s.onError(err)
	} else {
		s.updateSubtaskStateAndError(subtask.ID, proto.TaskStateReverted, "")
	}
	return s.getError()
}

func createScheduler(task *proto.Task) (Scheduler, error) {
	constructor, ok := schedulerConstructors[task.Type]
	if !ok {
		return nil, errors.Errorf("constructor of scheduler for type %s not found", task.Type)
	}
	return constructor(task.Meta, task.Step)
}

func createSubtaskExecutor(minimalTask proto.MinimalTask, tp string, step int64) (SubtaskExecutor, error) {
	constructor, ok := subtaskExecutorConstructors[tp]
	if !ok {
		return nil, errors.Errorf("constructor of subtask executor for type %s not found", tp)
	}
	return constructor(minimalTask, step)
}

func (s *InternalSchedulerImpl) registerCancelFunc(cancel context.CancelFunc) {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.mu.runtimeCancel = cancel
}

func (s *InternalSchedulerImpl) onError(err error) {
	if err == nil {
		return
	}

	s.mu.Lock()
	defer s.mu.Unlock()

	if s.mu.err == nil {
		s.mu.err = err
		logutil.Logger(s.logCtx).Error("scheduler error", zap.Error(err))
	}

	if s.mu.runtimeCancel != nil {
		s.mu.runtimeCancel()
	}
}

func (s *InternalSchedulerImpl) getError() error {
	s.mu.RLock()
	defer s.mu.RUnlock()
	return s.mu.err
}

func (s *InternalSchedulerImpl) resetError() {
	s.mu.Lock()
	defer s.mu.Unlock()
	s.mu.err = nil
}

func (s *InternalSchedulerImpl) updateSubtaskStateAndError(id int64, state string, subTaskErr string) {
	err := s.taskTable.UpdateSubtaskStateAndError(id, state, subTaskErr)
	if err != nil {
		s.onError(err)
	}
}

repository: sarvex/tidb

repo_name: sarvex/tidb


text:
file(REMOVE_RECURSE
  "CMakeFiles/Profiler.dir/Profiler.cpp.o"
  "CMakeFiles/Profiler.dir/Timing.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/AIMasterUpdates.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/MainLoop.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/NetLayer.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/NetMessages.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/ObjectAIUpdates.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/ObjectEventHandlers.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/Pathing.cpp.o"
  "CMakeFiles/Profiler.dir/Targets/Scripts.cpp.o"
  "../../../Binaries/NWNX_Profiler.pdb"
  "../../../Binaries/NWNX_Profiler.so"
)

# Per-language clean rules from dependency scanning.
foreach(lang CXX)
  include(CMakeFiles/Profiler.dir/cmake_clean_${lang}.cmake OPTIONAL)
endforeach()