text:
package dao

import (
	"context"
	"database/sql"
	"fmt"
	"strings"

	"go-common/app/service/main/member/model"
	xsql "go-common/library/database/sql"
	"go-common/library/log"
	"go-common/library/time"

	"github.com/pkg/errors"
)

const (
	_shard = 100
	// base
	_selBaseInfo = "SELECT mid,name,sex,face,sign,rank,birthday FROM user_base_%02d WHERE mid=? "
	_setSex      = "INSERT INTO user_base_%02d(mid,sex) VALUES (?,?) ON DUPLICATE KEY UPDATE sex=?"
	_setName     = "INSERT INTO user_base_%02d(mid,name) VALUES (?,?) ON DUPLICATE KEY UPDATE name=?"
	_setRank     = "INSERT INTO user_base_%02d(mid,rank) VALUES (?,?) ON DUPLICATE KEY UPDATE rank=?"
	_setSign     = "INSERT INTO user_base_%02d(mid,sign) VALUES (?,?) ON DUPLICATE KEY UPDATE sign=?"
	_setBirthday = "INSERT INTO user_base_%02d(mid,birthday) VALUES (?,?) ON DUPLICATE KEY UPDATE birthday=?"
	_setFace     = "INSERT INTO user_base_%02d(mid,face) VALUES (?,?) ON DUPLICATE KEY UPDATE face=?"
	_setBase     = `INSERT INTO user_base_%02d(mid,name,sex,face,sign,rank,birthday) VALUES (?,?,?,?,?,?,?)
		ON DUPLICATE KEY UPDATE name=?,sex=?,face=?,sign=?,rank=?,birthday=?`
	// _exp
	_setExp    = "INSERT INTO user_exp_%02d (mid,exp) VALUES(?,?) ON DUPLICATE KEY UPDATE exp=VALUES(exp)"
	_updateExp = "UPDATE user_exp_%02d SET exp=exp+? WHERE mid=?"
	_selExp    = "SELECT exp FROM user_exp_%02d where mid=?"
	//user_flag
	_selUserAttr    = "SELECT flag FROM user_flag where mid =? and flag & ?= ?"
	_updateUserAttr = "INSERT INTO user_flag(mid,flag) VALUES(?,?) ON DUPLICATE KEY UPDATE flag=flag|?"
	// user_official
	_selOfficials = "SELECT mid,role,title,description FROM user_official WHERE role>0"
	// moral
	_selMoral = "SELECT moral,added,deducted,last_recover_date from user_moral where mid=?"

	_initMoral              = `INSERT IGNORE INTO user_moral (mid,moral,added,deducted,last_recover_date) VALUES(?,?,?,?,?)`
	_updateMoral            = `update user_moral set moral=moral+?,added=added+?, deducted=deducted + ? where mid = ?`
	_updateMoralRecoverDate = `update user_moral set last_recover_date = ? where mid = ?`

	// official
	_setOfficialDoc = `INSERT INTO user_official_doc (mid,name,state,role,title,description,extra,submit_source,submit_time) VALUES (?,?,?,?,?,?,?,?,?)
	ON DUPLICATE KEY UPDATE name=VALUES(name), state=VALUES(state), role=VALUES(role), title=VALUES(title), description=VALUES(description), extra=VALUES(extra), submit_source=VALUES(submit_source), submit_time=VALUES(submit_time)`
	_selOfficialDoc = "SELECT mid,name,state,role,title,description,reject_reason,extra FROM user_official_doc WHERE mid=?"
	_selOfficial    = "SELECT role,title,description FROM user_official WHERE mid=? AND role>0"

	//official addit
	_setOfficialDocAddit = `INSERT INTO user_official_doc_addit (mid,property,vstring) VALUES (?,?,?) ON DUPLICATE KEY UPDATE vstring=VALUES(vstring)`

	// realname
	_selRealnameInfo           = `SELECT id,mid,channel,realname,country,card_type,card,card_md5,status,reason,ctime,mtime FROM realname_info WHERE mid = ? LIMIT 1`
	_selRealnameInfoByCard     = `SELECT id,mid,channel,realname,country,card_type,card,card_md5,status,reason,ctime,mtime FROM realname_info WHERE card_md5=? AND status in (0,1) LIMIT 1`
	_selRealnameInfoMidByCards = `SELECT mid,card_md5 FROM realname_info WHERE card_md5 IN (%s) AND status in (0,1)`
	_upsertRealnameInfo        = `INSERT INTO realname_info (mid,channel,realname,country,card_type,card,card_md5,status,reason) VALUES (?,?,?,?,?,?,?,?,?) ON DUPLICATE KEY UPDATE channel=?,realname=?,country=?,card_type=?,card=?,card_md5=?,status=?,reason=?`
	_updateRealnameInfo        = `UPDATE realname_info SET status=?,reason=? WHERE mid=?`
	_selRealnameApply          = "SELECT id,mid,realname,country,card_type,card_num,card_md5,hand_img,front_img,back_img,status,operator,operator_id,operator_time,remark,remark_status,ctime,mtime FROM realname_apply WHERE mid = ? ORDER BY ID DESC LIMIT 1"
	_insertRealnameApply       = `INSERT INTO realname_apply (mid,realname,country,card_type,card_num,card_md5,hand_img,front_img,back_img,status) VALUES (?,?,?,?,?,?,?,?,?,?)`
	_selRealnameApplyImg       = "SELECT id,img_data,ctime,mtime FROM realname_apply_img WHERE id = ? LIMIT 1"
	_insertRealnameApplyImg    = `INSERT INTO realname_apply_img (img_data) VALUES (?)`
	// alipay
	_selRealnameAlipayApply    = "SELECT id,mid,realname,card,img,status,reason,bizno,ctime,mtime FROM realname_alipay_apply WHERE mid = ? ORDER BY ID DESC LIMIT 1"
	_insertRealnameAlipayApply = `INSERT INTO realname_alipay_apply (mid,realname,card,img,status,reason,bizno) VALUES (?,?,?,?,?,?,?)`
	_updateRealnameAlipayApply = `UPDATE realname_alipay_apply SET status=?,reason=? WHERE id=?`

	// realname old
	_insertOldRealnameApply    = `INSERT INTO dede_identification_card_apply (mid,realname,type,card_data,card_for_search,front_img,front_img2,back_img,apply_time,status) VALUES (?,?,?,?,?,?,?,?,?,?)`
	_insertOldRealnameApplyImg = `INSERT INTO dede_identification_card_apply_img (img_data,add_time) VALUES (?,?)`
)

func hit(id int64) int64 {
	return id % _shard
}

// BeginTran begin transcation.
func (d *Dao) BeginTran(c context.Context) (tx *xsql.Tx, err error) {
	return d.db.Begin(c)
}

// BaseInfo base info of user.
func (d *Dao) BaseInfo(c context.Context, mid int64) (r *model.BaseInfo, err error) {
	r = &model.BaseInfo{}
	row := d.db.Master().QueryRow(c, fmt.Sprintf(_selBaseInfo, hit(mid)), mid)
	if err = row.Scan(&r.Mid, &r.Name, &r.Sex, &r.Face, &r.Sign, &r.Rank, &r.Birthday); err != nil {
		if err == xsql.ErrNoRows {
			r = nil
			err = nil
			return
		}
		err = errors.Wrapf(err, "dao base mid(%d)", mid)
	}
	r.RandFaceURL()
	return
}

// SetBase set base.
func (d *Dao) SetBase(c context.Context, base *model.BaseInfo) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setBase, hit(base.Mid)), base.Mid, base.Name, base.Sex, base.Face, base.Sign,
		base.Rank, base.Birthday, base.Name, base.Sex, base.Face, base.Sign, base.Rank, base.Birthday); err != nil {
		err = errors.Wrapf(err, "dao set base(%v)", base)
	}
	return
}

// SetSign set user sign.
func (d *Dao) SetSign(c context.Context, mid int64, sign string) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setSign, hit(mid)), mid, sign, sign); err != nil {
		err = errors.Wrapf(err, "dao set sign mid(%d)", mid)
	}
	return
}

// SetName set user name.
func (d *Dao) SetName(c context.Context, mid int64, name string) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setName, hit(mid)), mid, name, name); err != nil {
		err = errors.Wrapf(err, "dao set name mid(%d) name(%s)", mid, name)
	}
	return
}

// SetRank set user rank.
func (d *Dao) SetRank(c context.Context, mid, rank int64) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setRank, hit(mid)), mid, rank, rank); err != nil {
		err = errors.Wrapf(err, "dao set rank mid(%d) rank(%d)", mid, rank)
	}
	return
}

// SetSex set sex.
func (d *Dao) SetSex(c context.Context, mid, sex int64) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setSex, hit(mid)), mid, sex, sex); err != nil {
		err = errors.Wrapf(err, "dao set sex mid(%d) sex(%d)", mid, sex)
	}
	return
}

// SetBirthday set birthday.
func (d *Dao) SetBirthday(c context.Context, mid int64, birthday time.Time) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setBirthday, hit(mid)), mid, birthday, birthday); err != nil {
		err = errors.Wrapf(err, "dao set birthday mid(%d) birthday(%d)", mid, birthday)
	}
	return
}

// SetFace set face.
func (d *Dao) SetFace(c context.Context, mid int64, face string) (err error) {
	if _, err = d.db.Exec(c, fmt.Sprintf(_setFace, hit(mid)), mid, face, face); err != nil {
		err = errors.Wrapf(err, "dao set face mid(%d) face(%v)", mid, face)
	}
	return
}

// ExpDB get user exp from db.
func (d *Dao) ExpDB(c context.Context, mid int64) (count int64, err error) {
	row := d.db.QueryRow(c, fmt.Sprintf(_selExp, hit(mid)), mid)
	if err = row.Scan(&count); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			return
		}
		err = errors.Wrapf(err, "dao exp mid(%d)", mid)
	}
	return
}

// SetExp set user exp to count.
func (d *Dao) SetExp(c context.Context, mid, count int64) (affect int64, err error) {
	row, err := d.db.Exec(c, fmt.Sprintf(_setExp, hit(mid)), mid, count)
	if err != nil {
		err = errors.Wrapf(err, "dao set exp mid(%d) exp(%d)", mid, count)
		return
	}
	return row.RowsAffected()
}

// UpdateExp incr user exp by delta.
func (d *Dao) UpdateExp(c context.Context, mid, delta int64) (affect int64, err error) {
	row, err := d.db.Exec(c, fmt.Sprintf(_updateExp, hit(mid)), delta, mid)
	if err != nil {
		err = errors.Wrapf(err, "dao update exp mid(%d) exp(%d)", mid, delta)
		return
	}
	return row.RowsAffected()
}

// ------------- exp ---------------- //

// UserAttrDB get attr.
func (d *Dao) UserAttrDB(c context.Context, mid int64, attr uint) (hasAttr bool, err error) {
	var flag int8
	row := d.db.QueryRow(c, _selUserAttr, mid, attr, attr)
	if err = row.Scan(&flag); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			return
		}
		err = errors.Wrapf(err, "dao attr mid(%d) attr(%d)", mid, attr)
	}
	hasAttr = true
	return
}

// SetUserAttr update attr .
func (d *Dao) SetUserAttr(c context.Context, mid int64, attr uint) (err error) {
	if _, err = d.db.Exec(c, _updateUserAttr, mid, attr, attr); err != nil {
		err = errors.Wrapf(err, "dao attr mid(%d) attr(%d)", mid, attr)
		return
	}
	return
}

// Officials all officials info of user.
func (d *Dao) Officials(c context.Context) (om map[int64]*model.OfficialInfo, err error) {
	om = make(map[int64]*model.OfficialInfo)
	rows, err := d.db.Query(c, _selOfficials)
	if err != nil {
		err = errors.Wrap(err, "dao officials")
		return
	}
	defer rows.Close()
	for rows.Next() {
		var mid int64
		o := &model.OfficialInfo{}
		if err = rows.Scan(&mid, &o.Role, &o.Title, &o.Desc); err != nil {
			err = errors.Wrap(err, "dao officials scan")
			return
		}
		om[mid] = o
	}
	return
}

// Official is.
func (d *Dao) Official(c context.Context, mid int64) (*model.OfficialInfo, error) {
	row := d.db.QueryRow(c, _selOfficial, mid)
	o := &model.OfficialInfo{}
	if err := row.Scan(&o.Role, &o.Title, &o.Desc); err != nil {
		if err == sql.ErrNoRows {
			return nil, nil
		}
		return nil, errors.Wrap(err, "dao official scan")
	}
	return o, nil
}

// MoralDB get user moral from db.
func (d *Dao) MoralDB(c context.Context, mid int64) (moral *model.Moral, err error) {
	moral = &model.Moral{}
	row := d.db.QueryRow(c, _selMoral, mid)
	if err = row.Scan(&moral.Moral, &moral.Added, &moral.Deducted, &moral.LastRecoverDate); err != nil {
		if err == sql.ErrNoRows {
			moral = nil
			err = nil
			return
		}
		log.Error(" SelMoral row.Scan() error(%v) mid(%v)", err, mid)
		err = errors.Wrapf(err, "dao moral mid(%d)", mid)
		return
	}
	return
}

// TxMoralDB get user moral from db.
func (d *Dao) TxMoralDB(tx *xsql.Tx, mid int64) (moral *model.Moral, err error) {
	moral = &model.Moral{}
	row := tx.QueryRow(_selMoral, mid)
	if err = row.Scan(&moral.Moral, &moral.Added, &moral.Deducted, &moral.LastRecoverDate); err != nil {
		if err == sql.ErrNoRows {
			moral = nil
			err = nil
			return
		}
		log.Error(" SelMoral row.Scan() error(%v) mid(%v)", err, mid)
		err = errors.Wrapf(err, "dao moral mid(%d)", mid)
		return
	}
	return
}

// TxUpdateMoral set user moral.
func (d *Dao) TxUpdateMoral(tx *xsql.Tx, mid, moral, added, deducted int64) (err error) {
	if _, err = tx.Exec(_updateMoral, moral, added, deducted, mid); err != nil {
		err = errors.Wrapf(err, "TxUpdateMoral mid(%d) moralAdded(%v)", mid, moral)
		return
	}
	return
}

// TxUpdateMoralRecoverDate update moral recover date.
func (d *Dao) TxUpdateMoralRecoverDate(tx *xsql.Tx, mid int64, recoverDate time.Time) (err error) {
	if _, err = tx.Exec(_updateMoralRecoverDate, recoverDate, mid); err != nil {
		err = errors.Wrapf(err, "TxUpdateMoralRecoverDate mid(%d) recoverDate(%v)", mid, recoverDate)
		return
	}
	return
}

// TxInitMoral set user moral.
func (d *Dao) TxInitMoral(tx *xsql.Tx, mid, moral, added, deducted int64, lastRecoverDate time.Time) (err error) {
	if _, err = tx.Exec(_initMoral, mid, moral, added, deducted, lastRecoverDate); err != nil {
		err = errors.Wrapf(err, "TxInitMoral mid(%d) moral(%v)", mid, moral)
		return
	}
	return
}

// SetOfficialDoc add official doc.
func (d *Dao) SetOfficialDoc(c context.Context, od *model.OfficialDoc) (err error) {
	_, err = d.db.Exec(c, _setOfficialDoc, od.Mid, od.Name, model.OfficialStateWait, od.Role,
		od.Title, od.Desc, od.OfficialExtra.String(), od.SubmitSource, od.SubmitTime)
	if err != nil {
		err = errors.Wrapf(err, "dao add official doc")
		return
	}

	return
}

// SetOfficialDocAddit add official doc addit.
func (d *Dao) SetOfficialDocAddit(c context.Context, mid int64, property, vstring string) error {
	_, err := d.db.Exec(c, _setOfficialDocAddit, mid, property, vstring)
	if err != nil {
		err = errors.Wrapf(err, "dao add official doc addit")
		return err
	}
	return nil
}

// OfficialDoc get official doc.
func (d *Dao) OfficialDoc(c context.Context, mid int64) (*model.OfficialDoc, error) {
	od := new(model.OfficialDoc)
	row := d.db.QueryRow(c, _selOfficialDoc, mid)
	if err := row.Scan(&od.Mid, &od.Name, &od.State, &od.Role, &od.Title, &od.Desc, &od.RejectReason, &od.Extra); err != nil {
		err = errors.Wrapf(err, "official doc")
		return nil, err
	}
	od.ParseExtra()
	return od, nil
}

// Realname

// RealnameInfo is.
func (d *Dao) RealnameInfo(c context.Context, mid int64) (info *model.RealnameInfo, err error) {
	row := d.db.QueryRow(c, _selRealnameInfo, mid)
	info = &model.RealnameInfo{}
	if err = row.Scan(&info.ID, &info.MID, &info.Channel, &info.Realname, &info.Country, &info.CardType, &info.Card, &info.CardMD5, &info.Status, &info.Reason, &info.CTime, &info.MTime); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			info = nil
			return
		}
		err = errors.Wrapf(err, "dao RealnameInfo mid(%d)", mid)
		return
	}
	return
}

// RealnameInfoByCard is.
func (d *Dao) RealnameInfoByCard(c context.Context, cardMD5 string) (info *model.RealnameInfo, err error) {
	row := d.db.QueryRow(c, _selRealnameInfoByCard, cardMD5)
	info = &model.RealnameInfo{}
	if err = row.Scan(&info.ID, &info.MID, &info.Channel, &info.Realname, &info.Country, &info.CardType, &info.Card, &info.CardMD5, &info.Status, &info.Reason, &info.CTime, &info.MTime); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			info = nil
			return
		}
		err = errors.Wrapf(err, "dao RealnameInfo cardMD5(%s)", cardMD5)
		return
	}
	return
}

// UpsertTxRealnameInfo is.
func (d *Dao) UpsertTxRealnameInfo(c context.Context, tx *xsql.Tx, info *model.RealnameInfo) (err error) {
	if _, err = tx.Exec(_upsertRealnameInfo, info.MID, info.Channel, info.Realname, info.Country, info.CardType, info.Card, info.CardMD5, info.Status, info.Reason, info.Channel, info.Realname, info.Country, info.CardType, info.Card, info.CardMD5, info.Status, info.Reason); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// UpdateTxRealnameInfo is.
func (d *Dao) UpdateTxRealnameInfo(c context.Context, tx *xsql.Tx, mid int64, status model.RealnameApplyStatus, reason string) (err error) {
	if _, err = tx.Exec(_updateRealnameInfo, status, reason, mid); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// RealnameApply realname
func (d *Dao) RealnameApply(c context.Context, mid int64) (apply *model.RealnameApply, err error) {
	row := d.db.Master().QueryRow(c, _selRealnameApply, mid)
	apply = &model.RealnameApply{}
	if err = row.Scan(&apply.ID, &apply.MID, &apply.Realname, &apply.Country, &apply.CardType, &apply.CardNum, &apply.CardMD5, &apply.HandIMG, &apply.FrontIMG, &apply.BackIMG, &apply.Status, &apply.Operator, &apply.OperatorID, &apply.OperatorTime, &apply.Remark, &apply.RemarkStatus, &apply.CTime, &apply.MTime); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			apply = nil
			return
		}
		err = errors.Wrapf(err, "dao RealnameApply mid(%d)", mid)
		return
	}
	return
}

// InsertRealnameApply is
func (d *Dao) InsertRealnameApply(c context.Context, data *model.RealnameApply) (err error) {
	if _, err = d.db.Exec(c, _insertRealnameApply, data.MID, data.Realname, data.Country, data.CardType, data.CardNum, data.CardMD5, data.HandIMG, data.FrontIMG, data.BackIMG, data.Status); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// RealnameApplyIMG is
func (d *Dao) RealnameApplyIMG(c context.Context, id int) (img *model.RealnameApplyImage, err error) {
	row := d.db.Master().QueryRow(c, _selRealnameApplyImg, id)
	img = &model.RealnameApplyImage{}
	if err = row.Scan(&img.ID, &img.IMGData, &img.CTime, &img.MTime); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			img = nil
			return
		}
		err = errors.Wrapf(err, "dao RealnameApplyIMG mid(%d)", id)
		return
	}
	return
}

// InsertRealnameApplyImg is
func (d *Dao) InsertRealnameApplyImg(c context.Context, data *model.RealnameApplyImage) (id int64, err error) {
	var res sql.Result
	if res, err = d.db.Exec(c, _insertRealnameApplyImg, data.IMGData); err != nil {
		err = errors.WithStack(err)
		return
	}
	if id, err = res.LastInsertId(); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// InsertOldRealnameApply is
func (d *Dao) InsertOldRealnameApply(c context.Context, data *model.RealnameApply) (id int64, err error) {
	var (
		res      sql.Result
		cardType = int16(data.CardType)
	)
	if data.Country > 0 {
		cardType = data.Country
	}
	if res, err = d.accdb.Exec(c, _insertOldRealnameApply, data.MID, data.Realname, cardType, data.CardNum, data.CardMD5, data.FrontIMG, data.HandIMG, data.BackIMG, data.CTime.Unix(), data.Status); err != nil {
		err = errors.WithStack(err)
		return
	}
	if id, err = res.LastInsertId(); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// InsertOldRealnameApplyImg is
func (d *Dao) InsertOldRealnameApplyImg(c context.Context, data *model.RealnameApplyImage) (id int64, err error) {
	var res sql.Result
	if res, err = d.accdb.Exec(c, _insertOldRealnameApplyImg, data.IMGData, data.CTime); err != nil {
		err = errors.WithStack(err)
		return
	}
	if id, err = res.LastInsertId(); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// InsertTxRealnameAlipayApply .
func (d *Dao) InsertTxRealnameAlipayApply(c context.Context, tx *xsql.Tx, data *model.RealnameAlipayApply) (err error) {
	if _, err = tx.Exec(_insertRealnameAlipayApply, data.MID, data.Realname, data.Card, data.IMG, data.Status, data.Reason, data.Bizno); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// UpdateTxRealnameAlipayApply is.
func (d *Dao) UpdateTxRealnameAlipayApply(c context.Context, tx *xsql.Tx, id int64, status model.RealnameApplyStatus, reason string) (err error) {
	if _, err = tx.Exec(_updateRealnameAlipayApply, status, reason, id); err != nil {
		err = errors.WithStack(err)
		return
	}
	return
}

// RealnameAlipayApply .
func (d *Dao) RealnameAlipayApply(c context.Context, mid int64) (apply *model.RealnameAlipayApply, err error) {
	row := d.db.QueryRow(c, _selRealnameAlipayApply, mid)
	apply = &model.RealnameAlipayApply{}
	if err = row.Scan(&apply.ID, &apply.MID, &apply.Realname, &apply.Card, &apply.IMG, &apply.Status, &apply.Reason, &apply.Bizno, &apply.CTime, &apply.MTime); err != nil {
		if err == xsql.ErrNoRows {
			err = nil
			apply = nil
			return
		}
		err = errors.Wrapf(err, "dao RealnameAlipayApply mid(%d)", mid)
		return
	}
	return
}

func prepareStringArray(in []string) string {
	surrounded := make([]string, 0, len(in))
	for _, s := range in {
		surrounded = append(surrounded, fmt.Sprintf(`'%s'`, s))
	}
	return strings.Join(surrounded, ",")
}

// MidByRealnameCards is
func (d *Dao) MidByRealnameCards(ctx context.Context, cardMD5s []string) (map[string]int64, error) {
	rows, err := d.db.Query(ctx, fmt.Sprintf(_selRealnameInfoMidByCards, prepareStringArray(cardMD5s)))
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	result := make(map[string]int64, len(cardMD5s))
	for rows.Next() {
		mid, cardMD5 := int64(0), ""
		if err := rows.Scan(&mid, &cardMD5); err != nil {
			log.Warn("Failed to scan realname info: %+v", err)
			continue
		}
		result[cardMD5] = mid
	}
	return result, nil
}


text:
.. _hasura_platform:setup:

Hasura CLI: hasura platform:setup
---------------------------------

Setup a new Hasura project

Synopsis
~~~~~~~~

Setup all the clusters in a Hasura project for further use, also adds required remotes and hooks to git push

::

  hasura platform:setup [flags]

Alias: setup

Examples
~~~~~~~~

::

    # when a new project is cloned, execute to get credentials for cluster access,
    # add your ssh key to clusters and to create hooks and remotes
    $ hasura setup

Options
~~~~~~~

::

  -h, --help   help for platform:setup

Options inherited from parent commands
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

::

      --project string   hasura project directory where the commands should be executed. (default: current directory)

SEE ALSO
~~~~~~~~

* :ref:`hasura <hasura>` 	 - hasura controls the hasura project

*Auto generated by spf13/cobra*

redpajama_set_name: RedPajamaGithub


text:
package org.apache.hyracks.storage.am.rtree.api;

import org.apache.hyracks.api.dataflow.value.ISerializerDeserializer;

public interface IGenericPrimitiveSerializerDeserializer<T> extends
		ISerializerDeserializer<T> {
	public double getValue(byte[] bytes, int offset);
}


text:
local Draggable = {}

local previous_relative_mode

local dragging = false

function Draggable.move(dx, dy)
  if dragging then
    local start_x, start_y, display_index = love.window.getPosition()
    local display_w, display_h = love.window.getDesktopDimensions(display_index)
    local win_w, win_h = love.window.getMode()

    -- prevent window from moving > 80% out of current display
    local minimum_x = -0.8 * win_w
    local maximum_x = display_w - 0.2 * win_w

    local minimum_y = -0.8 * win_h
    local maximum_y = display_h - 0.2 * win_h

    local target_x = math.max(minimum_x, math.min(maximum_x, start_x + dx))
    local target_y = math.max(minimum_y, math.min(maximum_y, start_y + dy))

    love.window.setPosition(target_x, target_y, display_index)
  end
end

function Draggable.start()
  dragging = {x = love.mouse.getX(), y = love.mouse.getY()}

  previous_relative_mode = love.mouse.getRelativeMode()
  love.mouse.setRelativeMode(true)
end

function Draggable.stop()
  love.mouse.setRelativeMode(previous_relative_mode)

  if dragging then
    love.mouse.setPosition(dragging.x, dragging.y)
    dragging = false
  end
end

function Draggable.dragging()
  return dragging
end

return Draggable


text:
%   Unwrap single-channel phase images with FSL's Prelude
function data = fm_unwrap(data)

extension = data.extension;
n_channels = data.n_channels;
fnCurrentPhaseImages = data.fnCurrentPhaseImages;
fnCurrentMagImages = data.fnCurrentMagImages;
fnReconMag = data.fnReconMag;
n_echoes = data.n_echoes;
echoes_to_use = data.echoes_to_use;
unzip_command = data.unzip_command;
unwrap_method = data.unwrap_method;
writefile_dir = data.writefile_dir;
nii_pixdim = data.nii_pixdim;
prelude_thresholds = data.prelude_thresholds;
fsl_prefix = data.fsl_prefix;
fsl_suffix = data.fsl_suffix;
method = data.method;

switch method
    case {'conj-diff','phase-imaging'}
        n_phase_images_to_unwrap = 1;
        prelude_threshold = prelude_thresholds(1);
    case {'phase-match'}
        n_phase_images_to_unwrap = 2;
        prelude_threshold = prelude_thresholds(2);
    otherwise
        if data.n_echoes > 1
            n_phase_images_to_unwrap = 2;
        else
            n_phase_images_to_unwrap = 1;
        end
        prelude_threshold = prelude_thresholds(1);
end

if exist(data.fnBetMaskSubLarge) == 2
    fnBetMaskSubLarge = data.fnBetMaskSubLarge;
    mask_string = sprintf('-m %s', fnBetMaskSubLarge);
else
    mask_string = '';
end

%   Check if the data has been unwrapped already
unwrap = 'no';
for m = 1:n_channels
    for j=1:n_phase_images_to_unwrap
        fnCurrentPhaseImagesUnwrapped(m,j) = {strrep(char(fnCurrentPhaseImages(m,j)), extension, ['_unwrapped' extension])};
        if exist(char(fnCurrentPhaseImagesUnwrapped(m,j))) ~= 2 || strcmp(data.do_all_remaing_processing_stages, 'yes')
            unwrap = 'yes';
            data.do_all_remaing_processing_stages = 'yes';
        end
    end
end

switch unwrap
    case 'yes'
        disp(' * - unwrapping phase images');
        for m = 1:n_channels
            for j=1:n_phase_images_to_unwrap
                n=echoes_to_use(j);
                mag_file = char(fnCurrentMagImages(m,j));
                phase_file = char(fnCurrentPhaseImages(m,j));
                phase_file_unwrapped = char(fnCurrentPhaseImagesUnwrapped(m,j));
                %   Unwrap
                if n_channels ~= 1
                    disp(sprintf('Unwrapping phase images for channel %s, echo %s', int2str(m), int2str(n)));
                else
                    disp(sprintf('Unwrapping phase echo %s', int2str(n)));
                end
                switch unwrap_method
                    case 'prelude'
                       unwrap_command = sprintf('export LD_LIBRARY_PATH="/usr/lib/fsl:/usr/lib/fsl"; prelude -a %s -p %s -o %s -s -t %i',  unix_format(mag_file), unix_format(phase_file), unix_format(phase_file_unwrapped), prelude_threshold);
                  %      unwrap_command = sprintf('export LD_LIBRARY_PATH="/usr/lib/fsl:/usr/lib/fsl"; prelude -a %s -p %s -o %s -s %s',  unix_format(mag_file), unix_format(phase_file), unix_format(phase_file_unwrapped), mask_string);
                  switch method
                      case 'sep-channel'
                          [res, message] = unix(unwrap_command);
                          disp('');
                      otherwise
                        [res, message] = unix(unwrap_command);
                        if res ~= 0
                            error('Couldn''t unwrap: %s', message);
                        end
                        [res, message] = unix(unzip_command);
                        if res ~= 0
                            warning('Couldn''t unzip: %s', message);
                        end
                        %   centre again
                        centre_and_save_nii(load_nii(phase_file_unwrapped), phase_file_unwrapped, nii_pixdim);
                  end
                    case 'phun'
                        switch method
                            case 'sep-channel'
%                                unwrap_command = sprintf('phun -2 -n 1 --snr_min 1 --polar -o %s %s %s', unix_format(phase_file_unwrapped),  unix_format(mag_file), unix_format(phase_file));
                                unwrap_command = sprintf('phun -2 -n 1 --tq_min 0.02 --polar -o %s %s %s', unix_format(phase_file_unwrapped),  unix_format(mag_file), unix_format(phase_file));
                            otherwise
                                unwrap_command = sprintf('phun -2 -n 1 --tq_min 0.02 --polar -o %s %s %s', unix_format(phase_file_unwrapped),  unix_format(mag_file), unix_format(phase_file));
                        end
                        [res, message] = unix(unwrap_command);
                        if res ~= 0
                            error('Couldn''t unwrap: %s', message);
                        end
                    case 'snaphu'
                        unwrap_command = sprintf('sh /data/simon/scripts/snaphu_script.sh %s %s',  unix_format(mag_file), unix_format(phase_file));
                        [res, message] = unix(unwrap_command);
                        if res ~= 0
                            error('Couldn''t unwrap: %s', message);
                        end
                    otherwise
                        error('No valid unwrap option (unwrap_method) specified');
                end
            end
        end
    case 'no'
        disp(' - unwrapped data found');
end

data.fnCurrentPhaseImages = fnCurrentPhaseImagesUnwrapped;


text:
cordova.define('cordova/plugin_list', function(require, exports, module) {
module.exports = [
    {
        "file": "plugins/com.hutchind.cordova.plugins.launcher/www/Launcher.js",
        "id": "com.hutchind.cordova.plugins.launcher.Launcher",
        "clobbers": [
            "plugins.launcher"
        ]
    },
    {
        "file": "plugins/org.chromium.common/events.js",
        "id": "cordova-plugin-chrome-apps-common.events"
    },
    {
        "file": "plugins/org.chromium.common/helpers.js",
        "id": "cordova-plugin-chrome-apps-common.helpers"
    },
    {
        "file": "plugins/org.chromium.common/stubs.js",
        "id": "cordova-plugin-chrome-apps-common.stubs"
    },
    {
        "file": "plugins/cordova-plugin-chrome-apps-runtime/api/runtime.js",
        "id": "org.chromium.runtime.runtime"
    },
    {
        "file": "plugins/org.apache.cordova.device/www/device.js",
        "id": "org.apache.cordova.device.device",
        "clobbers": [
            "device"
        ]
    },
    {
        "file": "plugins/org.apache.cordova.splashscreen/www/splashscreen.js",
        "id": "org.apache.cordova.splashscreen.SplashScreen",
        "clobbers": [
            "navigator.splashscreen"
        ]
    },
    {
        "file": "plugins/org.chromium.i18n/i18n.js",
        "id": "org.chromium.i18n.I18n",
        "clobbers": [
            "chrome.i18n"
        ]
    },

    {
        "file": "plugins/cordova-plugin-chrome-apps-bootstrap/api/helpers/ChromeExtensionURLs.android.js",
        "id": "cordova-plugin-chrome-apps-bootstrap.helpers.ChromeExtensionURLs"
    }
];
module.exports.metadata =
// TOP OF METADATA
{
    "com.hutchind.cordova.plugins.launcher": "0.2.2",
    "org.chromium.runtime.runtime": "1.1.0",
    "org.apache.cordova.device": "0.2.13",
    "org.apache.cordova.splashscreen": "0.3.5",
    "org.chromium.i18n": "2.0.0",
    "cordova-plugin-chrome-apps-bootstrap": "2.0.8-dev"
}
// BOTTOM OF METADATA
});


text:
from django.contrib.auth.models import User
from models import Item, Bill, BillItem, BillItemExtra

class FormatError(Exception):
    pass

def undo_bill(billid, user):
    bill = Bill.objects.get(pk=billid)
    if not bill.is_committed():
        return 'Bill has already been deleted!'
    for billitem in bill.billitem_set.all():
        if billitem.item.quantity is not None:
            billitem.item.quantity += billitem.quantity
            billitem.item.save()
        for extra in billitem.billitemextra_set.all():
            if extra.item.quantity is not None:
                extra.item.quantity += extra.quantity
                extra.item.save()
    bill.deleted_by = user.username
    bill.save()
    return 'Bill #' + billid + ' deleted!'

def commit_bill(output, reqdata, user):
    errors = []
    to_commit_billitems = []
    to_commit_extras = []
    try:
        items = _get_items(reqdata)
        bill = Bill(customer_name=reqdata['customer_name'],
                    server=User.objects.get(pk=user.id).username,
                    customer_id=output['customer_id'], total=0)
        for r_billitem in reqdata['items']:
            # r_data = reqdata['items'][r_billitem]
            # print r_billitem['name']
            item = items[r_billitem['name']]
            billitem, ok = _create_item(r_billitem, BillItem, item)
            billitem_total = 0
            if ok:
                billitem_total += billitem.item_price
                # print billitem.item.name, billitem.item_price
                billitem.bill = bill
                billitem.category = billitem.item.category
                billitem.note = r_billitem['notes']
                to_commit_billitems.append(billitem)
            else:
                errors.append((billitem.item.name, billitem.item.quantity))
            for r_extra in r_billitem['extras']:
                r_data = r_billitem['extras'][r_extra]
                item = items[r_extra]
                extra, ok = _create_item(r_data, BillItemExtra, item)
                if ok:
                    billitem_total += extra.item_price * extra.quantity
                    # print extra.item.name, extra.item_price * extra.quantity
                    extra.billitem = billitem
                    to_commit_extras.append(extra)
                else:
                    errors.append((extra.item.name, extra.item.quantity))
            bill.total += billitem_total * billitem.quantity
            # print billitem.item.name, billitem.quantity
            # print 'Total', bill.total
    except KeyError as e:
        raise FormatError('Missing key: {}'.format(str(e)))
    if errors:
        output['total'] = 0
        output['customer_id'] = None
        output['errors'] = dict(errors)
        bill = None
    else:
        bill = _commit_bill_to_db(bill, to_commit_billitems, to_commit_extras,
                                  items)
        output['total'] = bill.total
        output['customer_id'] = bill.customer_id
        output['errors'] = {}
        output['date'] = bill.date
        output['billid'] = bill.id
    return output, bill

def _get_items(reqdata):
    items = {}
    for r_billitem in reqdata['items']:
        # print r_billitem
        items[r_billitem['name']] = None
        for r_extra in r_billitem['extras']:
            items[r_extra] = None
    db_items = Item.objects.select_for_update().filter(name__in=items.keys())
    for db_item in db_items:
        items[db_item.name] = db_item
    return items

def _create_item(r_data, BillElementClass, item):
    ok = True
    element = BillElementClass(item=item, item_price=item.price)
    if item.quantity is not None:
        remaining_quantity = item.quantity - r_data['qty']
        if remaining_quantity >= 0:
            item.quantity = remaining_quantity
        else:
            ok = False
    element.quantity = r_data['qty']
    return element, ok

def _commit_bill_to_db(bill, to_commit_billitems, to_commit_extras, items):
    for item in items.values():
        item.save()
    if bill.total < 0:
        bill.total = 0
    bill.save()
    for item in to_commit_billitems:
        item.bill = item.bill
        item.save()
    for item in to_commit_extras:
        item.billitem = item.billitem
        if item.billitem:
            item.save()
    return bill


text:
#include "third_party/blink/renderer/core/editing/iterators/simplified_backwards_text_iterator.h"

#include "third_party/blink/renderer/core/dom/first_letter_pseudo_element.h"
#include "third_party/blink/renderer/core/editing/editing_utilities.h"
#include "third_party/blink/renderer/core/editing/ephemeral_range.h"
#include "third_party/blink/renderer/core/editing/iterators/text_iterator.h"
#include "third_party/blink/renderer/core/editing/position.h"
#include "third_party/blink/renderer/core/editing/visible_units.h"
#include "third_party/blink/renderer/core/html/forms/html_form_control_element.h"
#include "third_party/blink/renderer/core/layout/layout_text_fragment.h"

namespace blink {

static int CollapsedSpaceLength(LayoutText* layout_text, int text_end) {
  const String& text = layout_text->GetText();
  int length = text.length();
  for (int i = text_end; i < length; ++i) {
    if (!layout_text->Style()->IsCollapsibleWhiteSpace(text[i]))
      return i - text_end;
  }

  return length - text_end;
}

static int MaxOffsetIncludingCollapsedSpaces(const Node* node) {
  int offset = CaretMaxOffset(node);
  if (auto* text = DynamicTo<LayoutText>(node->GetLayoutObject()))
    offset += CollapsedSpaceLength(text, offset) + text->TextStartOffset();
  return offset;
}

template <typename Strategy>
SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::
    SimplifiedBackwardsTextIteratorAlgorithm(
        const EphemeralRangeTemplate<Strategy>& range,
        const TextIteratorBehavior& behavior)
    : behavior_(behavior),
      text_state_(behavior),
      node_(nullptr),
      offset_(0),
      handled_node_(false),
      handled_children_(false),
      start_node_(nullptr),
      start_offset_(0),
      end_node_(nullptr),
      end_offset_(0),
      have_passed_start_node_(false),
      should_handle_first_letter_(false),
      should_stop_(false) {
  const Node* start_node = range.StartPosition().AnchorNode();
  if (!start_node)
    return;
  const Node* end_node = range.EndPosition().AnchorNode();
  int start_offset = range.StartPosition().ComputeEditingOffset();
  int end_offset = range.EndPosition().ComputeEditingOffset();

  Init(start_node, end_node, start_offset, end_offset);
}

template <typename Strategy>
void SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::Init(
    const Node* start_node,
    const Node* end_node,
    int start_offset,
    int end_offset) {
  if (!start_node->IsCharacterDataNode() && start_offset >= 0) {
    // |Strategy::childAt()| will return 0 if the offset is out of range. We
    // rely on this behavior instead of calling |countChildren()| to avoid
    // traversing the children twice.
    if (Node* child_at_offset = Strategy::ChildAt(*start_node, start_offset)) {
      start_node = child_at_offset;
      start_offset = 0;
    }
  }
  if (!end_node->IsCharacterDataNode() && end_offset > 0) {
    // |Strategy::childAt()| will return 0 if the offset is out of range. We
    // rely on this behavior instead of calling |countChildren()| to avoid
    // traversing the children twice.
    if (Node* child_at_offset = Strategy::ChildAt(*end_node, end_offset - 1)) {
      end_node = child_at_offset;
      end_offset = Position::LastOffsetInNode(*end_node);
    }
  }

  node_ = end_node;
  fully_clipped_stack_.SetUpFullyClippedStack(node_);
  offset_ = end_offset;
  handled_node_ = false;
  handled_children_ = !end_offset;

  start_node_ = start_node;
  start_offset_ = start_offset;
  end_node_ = end_node;
  end_offset_ = end_offset;

  have_passed_start_node_ = false;

  Advance();
}

template <typename Strategy>
void SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::Advance() {
  if (should_stop_)
    return;

  if (behavior_.StopsOnFormControls() &&
      HTMLFormControlElement::EnclosingFormControlElement(node_)) {
    should_stop_ = true;
    return;
  }

  text_state_.ResetRunInformation();

  while (node_ && !have_passed_start_node_) {
    // Don't handle node if we start iterating at [node, 0].
    if (!handled_node_ && !(node_ == end_node_ && !end_offset_)) {
      LayoutObject* layout_object = node_->GetLayoutObject();
      if (layout_object && layout_object->IsText() &&
          node_->getNodeType() == Node::kTextNode) {
        // FIXME: What about kCdataSectionNode?
        if (layout_object->Style()->Visibility() == EVisibility::kVisible &&
            offset_ > 0)
          handled_node_ = HandleTextNode();
      } else if (layout_object && (layout_object->IsLayoutEmbeddedContent() ||
                                   TextIterator::SupportsAltText(*node_))) {
        if (layout_object->Style()->Visibility() == EVisibility::kVisible &&
            offset_ > 0)
          handled_node_ = HandleReplacedElement();
      } else {
        handled_node_ = HandleNonTextNode();
      }
      if (text_state_.PositionNode())
        return;
    }

    if (!handled_children_ && Strategy::HasChildren(*node_)) {
      node_ = Strategy::LastChild(*node_);
      fully_clipped_stack_.PushFullyClippedState(node_);
    } else {
      // Exit empty containers as we pass over them or containers
      // where [container, 0] is where we started iterating.
      if (!handled_node_ && CanHaveChildrenForEditing(node_) &&
          Strategy::Parent(*node_) &&
          (!Strategy::LastChild(*node_) ||
           (node_ == end_node_ && !end_offset_))) {
        ExitNode();
        if (text_state_.PositionNode()) {
          handled_node_ = true;
          handled_children_ = true;
          return;
        }
      }

      // Exit all other containers.
      while (!Strategy::PreviousSibling(*node_)) {
        if (!AdvanceRespectingRange(
                ParentCrossingShadowBoundaries<Strategy>(*node_)))
          break;
        fully_clipped_stack_.Pop();
        ExitNode();
        if (text_state_.PositionNode()) {
          handled_node_ = true;
          handled_children_ = true;
          return;
        }
      }

      fully_clipped_stack_.Pop();
      if (AdvanceRespectingRange(Strategy::PreviousSibling(*node_)))
        fully_clipped_stack_.PushFullyClippedState(node_);
      else
        node_ = nullptr;
    }

    // For the purpose of word boundary detection,
    // we should iterate all visible text and trailing (collapsed) whitespaces.
    offset_ = node_ ? MaxOffsetIncludingCollapsedSpaces(node_) : 0;
    handled_node_ = false;
    handled_children_ = false;

    if (text_state_.PositionNode())
      return;
  }
}

template <typename Strategy>
bool SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::HandleTextNode() {
  int start_offset;
  int offset_in_node;
  LayoutText* layout_object = HandleFirstLetter(start_offset, offset_in_node);
  if (!layout_object)
    return true;

  String text = layout_object->GetText();

  if (behavior_.EmitsSpaceForNbsp())
    text.Replace(kNoBreakSpaceCharacter, kSpaceCharacter);

  if (!layout_object->HasInlineFragments() && text.length() > 0)
    return true;

  const int position_end_offset = offset_;
  offset_ = start_offset;
  const int position_start_offset = start_offset;
  DCHECK_LE(0, position_start_offset - offset_in_node);
  DCHECK_LE(position_start_offset - offset_in_node,
            static_cast<int>(text.length()));
  DCHECK_LE(1, position_end_offset - offset_in_node);
  DCHECK_LE(position_end_offset - offset_in_node,
            static_cast<int>(text.length()));
  DCHECK_LE(position_start_offset, position_end_offset);

  const int text_length = position_end_offset - position_start_offset;
  const int text_offset = position_start_offset - offset_in_node;
  CHECK_LE(static_cast<unsigned>(text_offset + text_length), text.length());
  text_state_.EmitText(To<Text>(*node_), position_start_offset,
                       position_end_offset, text, text_offset,
                       text_offset + text_length);
  return !should_handle_first_letter_;
}

template <typename Strategy>
LayoutText* SimplifiedBackwardsTextIteratorAlgorithm<
    Strategy>::HandleFirstLetter(int& start_offset, int& offset_in_node) {
  auto* layout_object = To<LayoutText>(node_->GetLayoutObject());
  start_offset = (node_ == start_node_) ? start_offset_ : 0;

  if (!layout_object->IsTextFragment()) {
    offset_in_node = 0;
    return layout_object;
  }

  auto* fragment = To<LayoutTextFragment>(layout_object);
  int offset_after_first_letter = fragment->Start();
  if (start_offset >= offset_after_first_letter) {
    // We'll stop in remaining part.
    DCHECK(!should_handle_first_letter_);
    offset_in_node = offset_after_first_letter;
    return layout_object;
  }

  if (!should_handle_first_letter_ && offset_after_first_letter < offset_) {
    // Enter into remaining part
    should_handle_first_letter_ = true;
    offset_in_node = offset_after_first_letter;
    start_offset = offset_after_first_letter;
    return layout_object;
  }

  // Enter into first-letter part
  should_handle_first_letter_ = false;
  offset_in_node = 0;

  DCHECK(fragment->IsRemainingTextLayoutObject());
  DCHECK(fragment->GetFirstLetterPseudoElement());

  LayoutObject* pseudo_element_layout_object =
      fragment->GetFirstLetterPseudoElement()->GetLayoutObject();
  DCHECK(pseudo_element_layout_object);
  DCHECK(pseudo_element_layout_object->SlowFirstChild());
  auto* first_letter_layout_object =
      To<LayoutText>(pseudo_element_layout_object->SlowFirstChild());

  const int end_offset =
      end_node_ == node_ && end_offset_ < offset_after_first_letter
          ? end_offset_
          : first_letter_layout_object->CaretMaxOffset();
  offset_ =
      end_offset + CollapsedSpaceLength(first_letter_layout_object, end_offset);

  return first_letter_layout_object;
}

template <typename Strategy>
bool SimplifiedBackwardsTextIteratorAlgorithm<
    Strategy>::HandleReplacedElement() {
  // We want replaced elements to behave like punctuation for boundary
  // finding, and to simply take up space for the selection preservation
  // code in moveParagraphs, so we use a comma.
  if (behavior_.EmitsPunctuationForReplacedElements())
    text_state_.EmitChar16AsNode(',', *node_);
  return true;
}

template <typename Strategy>
bool SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::HandleNonTextNode() {
  // We can use a linefeed in place of a tab because this simple iterator is
  // only used to find boundaries, not actual content. A linefeed breaks words,
  // sentences, and paragraphs.
  if (TextIterator::ShouldEmitNewlineForNode(*node_, false) ||
      TextIterator::ShouldEmitNewlineAfterNode(*node_) ||
      TextIterator::ShouldEmitTabBeforeNode(*node_)) {
    // TODO(editing-dev):The start of this emitted range is wrong. Ensuring
    // correctness would require |VisiblePositions| and so would be slow.
    // |previousBoundary expects this.
    text_state_.EmitChar16AfterNode('\n', *node_);
  }
  return true;
}

template <typename Strategy>
void SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::ExitNode() {
  if (TextIterator::ShouldEmitNewlineForNode(*node_, false) ||
      TextIterator::ShouldEmitNewlineBeforeNode(*node_) ||
      TextIterator::ShouldEmitTabBeforeNode(*node_)) {
    // TODO(editing-dev): When we want to use |EmitChar16BeforeNode()| when
    // test[1] and and test[2] failures are addressed.
    // [1] readonly-disabled-text-selection.html
    // [2] extend_selection_05_ltr_backward_word.html
    // TODO(editing-dev): The start of this emitted range is wrong. Ensuring
    // correctness would require |VisiblePositions| and so would be slow.
    // previousBoundary expects this.
    text_state_.EmitChar16BeforeChildren('\n', To<ContainerNode>(*node_));
  }
}

template <typename Strategy>
bool SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::AdvanceRespectingRange(
    const Node* next) {
  if (!next)
    return false;
  have_passed_start_node_ |= node_ == start_node_;
  if (have_passed_start_node_)
    return false;
  node_ = next;
  return true;
}

template <typename Strategy>
void SimplifiedBackwardsTextIteratorAlgorithm<
    Strategy>::EnsurePositionContainer() const {
  DCHECK(text_state_.PositionNode());
  if (text_state_.PositionContainerNode())
    return;
  const Node& node = *text_state_.PositionNode();
  const ContainerNode* parent = Strategy::Parent(node);
  DCHECK(parent);
  text_state_.UpdatePositionOffsets(*parent, Strategy::Index(node));
}

template <typename Strategy>
const Node* SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::StartContainer()
    const {
  if (!text_state_.PositionNode())
    return start_node_;
  EnsurePositionContainer();
  return text_state_.PositionContainerNode();
}

template <typename Strategy>
int SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::StartOffset() const {
  if (!text_state_.PositionNode())
    return start_offset_;
  EnsurePositionContainer();
  return text_state_.PositionStartOffset();
}

template <typename Strategy>
int SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::EndOffset() const {
  if (!text_state_.PositionNode())
    return start_offset_;
  EnsurePositionContainer();
  return text_state_.PositionEndOffset();
}

template <typename Strategy>
PositionTemplate<Strategy>
SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::StartPosition() const {
  return PositionTemplate<Strategy>::EditingPositionOf(StartContainer(),
                                                       StartOffset());
}

template <typename Strategy>
PositionTemplate<Strategy>
SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::EndPosition() const {
  return PositionTemplate<Strategy>::EditingPositionOf(StartContainer(),
                                                       EndOffset());
}

template <typename Strategy>
UChar SimplifiedBackwardsTextIteratorAlgorithm<Strategy>::CharacterAt(
    unsigned index) const {
  if (index >= text_state_.length())
    return 0;
  return text_state_.CharacterAt(text_state_.length() - index - 1);
}

template class CORE_TEMPLATE_EXPORT
    SimplifiedBackwardsTextIteratorAlgorithm<EditingStrategy>;
template class CORE_TEMPLATE_EXPORT
    SimplifiedBackwardsTextIteratorAlgorithm<EditingInFlatTreeStrategy>;

}  // namespace blink


text:
const util = require('util');
const glob = require('fast-glob');
const fse = require('fs-extra');
const lodashChunk = require('lodash/chunk');
const childProcess = require('child_process');

const execFileNode = util.promisify(childProcess.execFile);

function execFile(command, args) {
  return execFileNode(command, args, {
    cwd: process.cwd(),
    env: process.env,
    encoding: 'utf-8',
  });
}

const screenshotsBase = 'test/regressions/screenshots/chrome';
const screenshotsTmp = 'test/regressions/screenshots/argos';
const BATCH_SIZE = 200;

async function run() {
  const screenshots = await glob(`${screenshotsBase}/**/*`);
  const chunks = lodashChunk(screenshots, BATCH_SIZE);

  await Promise.all(
    chunks.map((chunk, chunkIndex) =>
      Promise.all(
        chunk.map((screenshot) => {
          return fse.move(
            screenshot,
            `${screenshotsTmp}/${chunkIndex}/${screenshot.replace(screenshotsBase, '')}`,
          );
        }),
      ),
    ),
  );

  for (let i = 0; i < chunks.length; i += 1) {
    // eslint-disable-next-line no-await-in-loop
    const argosResults = await execFile('argos', [
      'upload',
      `${screenshotsTmp}/${i}`,
      '--token',
      process.env.ARGOS_TOKEN,
      '--batchCount',
      chunks.length,
      '--external-build-id',
      process.env.CIRCLE_SHA1,
    ]);
    // eslint-disable-next-line no-console -- pipe stdout
    console.log(argosResults.stdout);
  }
}

run().catch((error) => {
  console.error(error);
  process.exit(1);
});


text:
<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateTaskLogsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('task_logs', function (Blueprint $table) {
            $table->increments('id');
            $table->unsignedInteger('task_id');
            $table->text('content');
            $table->timestamp('created_at');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('task_logs');
    }
}


text:
export * from './UserCenter/GetCurrentUserFavoriteBoards';
export * from './UserCenter/GetFavoritePosts';
export * from './UserCenter/GetRecentPosts';
export * from './UserCenter/GetUserFansInfo';
export * from './UserCenter/GetUserFollowingsInfo';
export * from './UserCenter/RefreshCurrentUserInfo';
export { sendWealth as sendWealthTo } from './UserCenter/SendWealth';


text:
namespace asp {

  /// Specialization of the generic LinescanModel for PeruSat satellites.
  class PeruSatCameraModel : public vw::camera::LinescanModel {

  public:
    //------------------------------------------------------------------
    // Constructors / Destructors
    //------------------------------------------------------------------
    PeruSatCameraModel(vw::camera::LagrangianInterpolation const& position,
                       vw::camera::LagrangianInterpolation const& velocity,
                       vw::camera::SLERPPoseInterpolation  const& pose,
                       vw::camera::LinearTimeInterpolation const& time,
                       vw::Vector2                         const& tan_psi_x,
                       vw::Vector2                         const& tan_psi_y,
                       vw::Quaternion<double>              const& instrument_biases,
                       vw::Vector2i                        const& image_size,
                       double min_time, double max_time,
                       bool   correct_velocity, bool   correct_atmosphere):
      vw::camera::LinescanModel(image_size, correct_velocity, correct_atmosphere),
      m_position_func(position), m_velocity_func(velocity),
      m_pose_func(pose), m_time_func(time),
      m_tan_psi_x(tan_psi_x), m_tan_psi_y(tan_psi_y),
      m_inverse_instrument_biases(inverse(instrument_biases)),
      m_min_time(min_time), m_max_time(max_time) {}

    virtual ~PeruSatCameraModel() {}
    virtual std::string type() const { return "LinescanPeruSat"; }

    // -- This set of functions implements virtual functions from LinescanModel.h --

    // Implement the functions from the LinescanModel class using functors
    virtual vw::Vector3 get_camera_center_at_time  (double time) const;
    virtual vw::Vector3 get_camera_velocity_at_time(double time) const;
    virtual vw::Quat    get_camera_pose_at_time    (double time) const;
    virtual double      get_time_at_line           (double line) const;

    /// As pixel_to_vector, but in the local camera frame.
    virtual vw::Vector3 get_local_pixel_vector(vw::Vector2 const& pix) const;

    // TODO: See if we can port these local changes to the parent class
    virtual vw::Vector2 point_to_pixel(vw::Vector3 const& point, double starty) const;
    virtual vw::Vector2 point_to_pixel(vw::Vector3 const& point) const {
      return point_to_pixel(point, -1); // Redirect to the function with no initial guess
    }

  private:

    vw::camera::LinearTimeInterpolation m_time_func;     ///< Yields time at a given line.
    vw::camera::LagrangianInterpolation m_position_func; ///< Yields position at time T
    vw::camera::LagrangianInterpolation m_velocity_func; ///< Yields velocity at time T
    vw::camera::SLERPPoseInterpolation  m_pose_func;     ///< Yields pose     at time T

    // These are used to find the look direction in camera coordinates at a given line
    vw::Vector2 m_tan_psi_x, m_tan_psi_y;
    vw::Quaternion<double> m_inverse_instrument_biases;

    /// These are the limits of when he have pose data available.
    double m_min_time, m_max_time;

    /// Throw an exception if the input time is outside the given bounds.
    /// - Pass the caller location in to get a nice error message.
    void check_time(double time, std::string const& location) const;

  }; // End class PeruSatCameraModel

  /// Load a PeruSat camera model from an XML file.
  /// - This function does not take care of Xerces XML init/de-init, the caller must
  ///   make sure this is done before/after this function is called!
  boost::shared_ptr<PeruSatCameraModel> load_perusat_camera_model_from_xml(std::string const& path);

} // end namespace asp

#endif//__STEREO_CAMERA_LINESCAN_PERUSAT_MODEL_H__


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

<widget xmlns="http://www.w3c.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0"
	version="1.0.0" id="org.alexmatos.conurapp">

	<name>Só de Cenoura</name>

	<description>
			Menu do restaurante Só de Cenoura
	</description>

	<author href="#" email="ams@ams.com">
		Alex Matos
	</author>

	<icon src="imagens/icon.png" />
	<!-- <gap:splash scr="imagens/splash.png" /> -->
	<plugin name="cordova-plugin-splashscreen" />
	<splash src="imagens/splash.png" />

	<preference name="orientation" value="portrait" />
	<preference name="fullscreen" value="true" />
	<preference name="permissions" value="none" />

	<gap:platform name="android" />
</widget>


text:
=head1 Deploying CSS

If you are using cssprepare as part of a build/deploy action, you should pay
special attention to two of the options C<-d> (C<--output-dir>) and C<-a>
(C<--use-all-shasum>).

A common performance gain for web sites is to concatenate and minify CSS (and
JavaScript too), which is what this tool does. A second gain is to then serve
that style sheet from a content delivery network (CDN) or sub-domain set up
to instruct the user agents to cache the content. In these cases, the filename
should be unique. Two common techniques are to add a version or a timestamp to
the filename.

This script uses a third option, fingerprinting. If you specify an output
directory with C<-d> (C<--output-dir>) when running cssprepare, the result
will be written to a file in that directory with a filename based upon the
SHA1 cryptographic fingerprint of the content. This means that running the
script multiple times against the same input will only ever produce one output
file, so you are not uselessly producing multiple stylesheets.

By default, the fingerprint is truncated after the first five characters, so
will look something like C<Jx1h3.css>. The chances of two different inputs
sharing the same filename are vanishingly small (even five characters allows
for over one billion variations). However, if you want, you can use all
twenty-seven characters of the fingerprint in the filename by adding the
option C<-a> (C<--use-all-shasum>).


text:
namespace MOE.Common.Migrations
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class AddApproachToPhasePedAgg : DbMigration
    {
        public override void Up()
        {
            DropPrimaryKey("dbo.PhasePedAggregations");
            AddColumn("dbo.PhasePedAggregations", "ApproachId", c => c.Int(nullable: false));
            AddPrimaryKey("dbo.PhasePedAggregations", new[] { "BinStartTime", "SignalId", "ApproachId" });
        }

        public override void Down()
        {
            DropPrimaryKey("dbo.PhasePedAggregations");
            DropColumn("dbo.PhasePedAggregations", "ApproachId");
            AddPrimaryKey("dbo.PhasePedAggregations", new[] { "BinStartTime", "SignalId", "PhaseNumber" });
        }
    }
}


text:
package com.appleframework.cache.redis.spring;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.redisson.RedissonClient;
import org.redisson.core.RMapCache;

public class RedisCache {

	private static Logger logger = Logger.getLogger(RedisCache.class);

	private final String name;
	private final int expire;
	private final RedissonClient redisson;

	public RMapCache<String, Object> getCacheMap() {
		return redisson.getMapCache(name);
	}

	public RedisCache(String name, int expire, RedissonClient redisson) {
		this.name = name;
		this.expire = expire;
		this.redisson = redisson;
	}

	public RedisCache(String name, RedissonClient redisson) {
		this.name = name;
		this.expire = 0;
		this.redisson = redisson;
	}

	public Object get(String key) {
		Object value = null;
		try {
			value = getCacheMap().get(key);
		} catch (Exception e) {
			logger.warn("获取 Cache 缓存错误", e);
		}
		return value;
	}

	public void put(String key, Object value) {
		if (value == null)
			return;
		try {
			if(expire == 0)
				getCacheMap().put(key, value);
			else
				getCacheMap().put(key, value, expire, TimeUnit.SECONDS);
		} catch (Exception e) {
			logger.warn("更新 Cache 缓存错误", e);
		}
	}

	public void clear() {
		try {
			getCacheMap().clear();
		} catch (Exception e) {
			logger.warn("删除 Cache 缓存错误", e);
		}
	}

	public void delete(String key) {
		try {
			getCacheMap().remove(key);
		} catch (Exception e) {
			logger.warn("删除 Cache 缓存错误", e);
		}
	}

	public int getExpire() {
		return expire;
	}

}


text:
#Region "Microsoft.VisualBasic::87fd1576a49ede7a0620995ae30b64e9, src\mzmath\TargetedMetabolomics\Contents\ContentTable.vb"

    ' Author:
    '
    '       xieguigang (gg.xie@bionovogene.com, BioNovoGene Co., LTD.)
    '
    ' Copyright (c) 2018 gg.xie@bionovogene.com, BioNovoGene Co., LTD.
    '
    '
    ' MIT License
    '
    '
    ' Permission is hereby granted, free of charge, to any person obtaining a copy
    ' of this software and associated documentation files (the "Software"), to deal
    ' in the Software without restriction, including without limitation the rights
    ' to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    ' copies of the Software, and to permit persons to whom the Software is
    ' furnished to do so, subject to the following conditions:
    '
    ' The above copyright notice and this permission notice shall be included in all
    ' copies or substantial portions of the Software.
    '
    ' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    ' IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    ' FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    ' AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    ' LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    ' OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    ' SOFTWARE.

    ' /********************************************************************************/

    ' Summaries:

    '     Class ContentTable
    '
    '         Constructor: (+1 Overloads) Sub New
    '         Function: GetIS, GetStandards, hasDefined, hasISDefined, StripMaxCommonNames
    '
    '
    ' /********************************************************************************/

#End Region

Imports System.Runtime.CompilerServices
Imports BioNovoGene.Analytical.MassSpectrometry.Math.LinearQuantitative
Imports Microsoft.VisualBasic.ComponentModel.DataSourceModel
Imports Microsoft.VisualBasic.Text

Namespace Content

    Public Class ContentTable

        ''' <summary>
        ''' [ion -> [sample, content]]
        ''' </summary>
        ReadOnly matrix As Dictionary(Of String, SampleContentLevels)
        ''' <summary>
        ''' [ion -> standards]
        ''' </summary>
        ReadOnly standards As Dictionary(Of String, Standards)
        ReadOnly [IS] As Dictionary(Of String, [IS])

        Default Public ReadOnly Property Content(sampleLevel As String, ion As String) As Double
            <MethodImpl(MethodImplOptions.AggressiveInlining)>
            Get
                Return matrix(ion)(sampleLevel)
            End Get
        End Property

        Sub New(matrix As Dictionary(Of String, SampleContentLevels), standards As Dictionary(Of String, Standards), [IS] As Dictionary(Of String, [IS]))
            Me.matrix = matrix
            Me.standards = standards
            Me.IS = [IS]
        End Sub

        ''' <summary>
        ''' check if the target <paramref name="ion"/> is exists or not?
        ''' </summary>
        ''' <param name="ion"></param>
        ''' <returns></returns>
        <MethodImpl(MethodImplOptions.AggressiveInlining)>
        Public Function hasDefined(ion As String) As Boolean
            Return standards.ContainsKey(ion)
        End Function

        ''' <summary>
        ''' check if the target <paramref name="is"/> is exists or not?
        ''' </summary>
        ''' <param name="[is]"></param>
        ''' <returns></returns>
        <MethodImpl(MethodImplOptions.AggressiveInlining)>
        Public Function hasISDefined([is] As String) As Boolean
            Return Me.IS.ContainsKey([is])
        End Function

        ''' <summary>
        ''' get IS definition model.
        ''' </summary>
        ''' <param name="ion"></param>
        ''' <returns></returns>
        <MethodImpl(MethodImplOptions.AggressiveInlining)>
        Public Function GetIS(ion As String) As [IS]
            If [IS] Is Nothing Then
                Return New [IS]
            Else
                Return [IS].TryGetValue(ion, [default]:=New [IS])
            End If
        End Function

        ''' <summary>
        ''' get target ion data model object
        ''' </summary>
        ''' <param name="ion"></param>
        ''' <returns></returns>
        <MethodImpl(MethodImplOptions.AggressiveInlining)>
        Public Function GetStandards(ion As String) As Standards
            Return standards(ion)
        End Function

        ''' <summary>
        ''' 这个函数主要是用于删除wiff文件转换为mzML文件之后留存的文件名前缀
        ''' </summary>
        ''' <param name="files">
        ''' a list of *.mzML file path that convert from wiff raw data file
        ''' </param>
        ''' <returns></returns>
        Public Shared Function StripMaxCommonNames(files As String()) As NamedValue(Of String)()
            Dim names As String() = files.Select(AddressOf BaseName).ToArray
            Dim minName As String = names.MinLengthString
            Dim index As Integer

            For i As Integer = 0 To minName.Length - 1
                index = i

                If names.Select(Function(str) str(index)).Distinct.Count > 1 Then
                    names = names _
                        .Select(Function(str)
                                    Return str.Substring(index).Trim(" "c, ASCII.TAB, "-"c, "_"c)
                                End Function) _
                        .ToArray

                    Exit For
                End If
            Next

            If names.All(Function(str) Char.IsDigit(str.First)) Then
                names = names _
                    .Select(Function(str) $"L{str}") _
                    .ToArray
            End If

            Return names _
                .Select(Function(nameStr, i)
                            Return New NamedValue(Of String) With {
                                .Name = nameStr,
                                .Value = files(i)
                            }
                        End Function) _
                .OrderBy(Function(file)
                             Return file.Name.Match("\d+").ParseInteger
                         End Function) _
                .ToArray
        End Function
    End Class
End Namespace


text:
export default function canToggleDomain(domain, servicesMap) {
  if (domain === 'lock') return true;
  if (domain === 'garage_door') return true;
  const serviceDomain = servicesMap.get(domain);
  return !!serviceDomain && serviceDomain.services.has('turn_on');
}


text:
var sparqlParserHelper = require('./sparqlparserhelper.js');
var expect = require('expect.js');

describe('Testing queries', function () {
  it('simple query', function () {

  var input = "SELECT * WHERE {?s <http://ddd.com> <http://ddd.com/rr>. \n ?x ?y ?z. \n ?x ?y ?z}";
  var expected = ['?s', '?x', '?y', '?z']
  var actual = sparqlParserHelper.getVariables(input);

  expect(actual).to.eql(expected);
  });
});


text:
package org.jeasy.jobs.user;

public class UserRepositoryTest {

    /*
     * The core module is database agnostic. Testing a repository here makes no sense.
     * Each database module contains a data source definition and database schema against which this repository is tested.
     * See tests in easy-jobs-tests module which are inherited in each database module.
     */
}


text:
#!/usr/bin/env python3

import os
import sys
from typing import List

from setuptools import setup

SETUP_DIR = os.path.dirname(__file__)
README = os.path.join(SETUP_DIR, "README.rst")

needs_pytest = {"pytest", "test", "ptr"}.intersection(sys.argv)
pytest_runner: List[str] = ["pytest < 8", "pytest-runner"] if needs_pytest else []

USE_MYPYC = False
# To compile with mypyc, a mypyc checkout must be present on the PYTHONPATH
if len(sys.argv) > 1 and sys.argv[1] == "--use-mypyc":
    sys.argv.pop(1)
    USE_MYPYC = True
if os.getenv("SCHEMA_SALAD_USE_MYPYC", None) == "1":
    USE_MYPYC = True

if USE_MYPYC and any(
    item in sys.argv
    for item in [
        "build",
        "bdist_wheel",
        "build_ext",
        "install",
        "install-lib",
        "bdist",
        "bdist_dumb",
        "bdist_rpm",
        "develop",
        "bdist_egg",
        "editable_wheel",
        "test",
    ]
):
    mypyc_targets = [
        # "schema_salad/codegen_base.py",  # interpreted classes cannot inherit from compiled
        # "schema_salad/codegen.py",
        "schema_salad/exceptions.py",
        "schema_salad/__init__.py",
        # "schema_salad/java_codegen.py",  # due to use of __name__
        "schema_salad/jsonld_context.py",
        "schema_salad/__main__.py",
        "schema_salad/main.py",
        "schema_salad/makedoc.py",
        # "schema_salad/python_codegen.py",  # due to use of __name__
        "schema_salad/ref_resolver.py",
        # "schema_salad/fetcher.py",  # to allow subclassing {Default,}Fetcher
        "schema_salad/schema.py",
        "schema_salad/sourceline.py",
        "schema_salad/utils.py",
        "schema_salad/validate.py",
        "schema_salad/avro/__init__.py",
        "schema_salad/avro/schema.py",
        # "schema_salad/tests/util.py",
        # "schema_salad/tests/test_print_oneline.py",
        # "schema_salad/tests/test_python_codegen.py",
        # "schema_salad/tests/test_java_codegen.py",
        # "schema_salad/tests/__init__.py",
        # "schema_salad/tests/test_cli_args.py",
        # "schema_salad/tests/test_fetch.py",
        # "schema_salad/tests/matcher.py",
        # "schema_salad/tests/test_cg.py",
        # "schema_salad/tests/test_examples.py",
        # "schema_salad/tests/test_errors.py",
        # "schema_salad/tests/test_real_cwl.py",
        # "schema_salad/tests/test_ref_resolver.py",
        # "schema_salad/tests/test_fp.py",
    ]

    from mypyc.build import mypycify

    opt_level = os.getenv("MYPYC_OPT_LEVEL", "3")
    ext_modules = mypycify(
        mypyc_targets, opt_level=opt_level, debug_level="0", verbose=True
    )
else:
    ext_modules = []

install_requires = [
    "requests >= 1.0",
    "ruamel.yaml >= 0.17.6, < 0.17.22;python_version>='3.7'",
    "ruamel.yaml >= 0.16.12, < 0.17.22",
    "rdflib >= 4.2.2, < 7.0.0",
    "rdflib-jsonld>=0.4.0, <= 0.6.1;python_version<='3.6'",
    "mistune >= 0.8.1, < 0.9",
    "CacheControl[filecache] >= 0.11.7, < 0.13",
]

extras_require = {
    "docs": [
        "sphinx >= 2.2",
        "sphinx-rtd-theme",
        "pytest < 8",
        "sphinx-autoapi",
        "sphinx-autodoc-typehints",
        "typed_ast;python_version<'3.8'",
        "sphinxcontrib-autoprogram",
    ],
    "pycodegen": ["black"],
}

setup(
    name="schema-salad",
    version="8.3",  # update the VERSION prefix in the Makefile as well 🙂
    description="Schema Annotations for Linked Avro Data (SALAD)",
    long_description=open(README).read(),
    long_description_content_type="text/x-rst",
    author="Common workflow language working group",
    author_email="common-workflow-language@googlegroups.com",
    url="https://github.com/common-workflow-language/schema_salad",
    download_url="https://github.com/common-workflow-language/schema_salad/releases",
    ext_modules=ext_modules,
    license="Apache 2.0",
    python_requires=">=3.6,<3.12",
    setup_requires=pytest_runner + ["setuptools_scm"],
    packages=["schema_salad", "schema_salad.tests", "schema_salad.avro"],
    package_data={
        "schema_salad": [
            "metaschema/*",
            "py.typed",
            "dotnet/*",
            "dotnet/*/*",
            "dotnet/*/*/*",
            "java/*",
            "java/*/*",
            "typescript/*",
            "typescript/*/*",
            "typescript/*/*/*",
            "typescript/.*",
        ],
        "schema_salad.tests": [
            "*.json",
            "*.yml",
            "docimp/*",
            "*.owl",
            "*.cwl",
            "*.txt",
            "foreign/*.cwl",
            "test_real_cwl/*",
            "test_real_cwl/*/*",
            "test_schema/*",
        ],
    },
    install_requires=install_requires,
    extras_require=extras_require,
    test_suite="tests",
    tests_require=["pytest<8"],
    entry_points={
        "console_scripts": [
            "schema-salad-tool=schema_salad.main:main",
            "schema-salad-doc=schema_salad.makedoc:main",
        ]
    },
    zip_safe=True,
    classifiers=[
        "Environment :: Console",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: Apache Software License",
        "Operating System :: POSIX",
        "Operating System :: MacOS :: MacOS X",
        "Development Status :: 5 - Production/Stable",
        "Programming Language :: Python :: 3.6",
        "Programming Language :: Python :: 3.7",
        "Programming Language :: Python :: 3.8",
        "Programming Language :: Python :: 3.9",
        "Programming Language :: Python :: 3.10",
        "Programming Language :: Python :: 3.11",
        "Typing :: Typed",
    ],
    use_scm_version=True,
)


text:
rm .git/hooks/pre-commit
ln -s $PWD/scripts/githooks/pre-commit .git/hooks/pre-commit


text:
package org.apache.skywalking.oap.server.exporter.provider.grpc;

import io.grpc.ManagedChannel;
import io.grpc.stub.StreamObserver;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.oap.server.core.analysis.metrics.DoubleValueHolder;
import org.apache.skywalking.oap.server.core.analysis.metrics.IntValueHolder;
import org.apache.skywalking.oap.server.core.analysis.metrics.LongValueHolder;
import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo;
import org.apache.skywalking.oap.server.core.analysis.metrics.MultiIntValuesHolder;
import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata;
import org.apache.skywalking.oap.server.core.exporter.ExportData;
import org.apache.skywalking.oap.server.core.exporter.ExportEvent;
import org.apache.skywalking.oap.server.core.exporter.MetricValuesExportService;
import org.apache.skywalking.oap.server.exporter.grpc.EventType;
import org.apache.skywalking.oap.server.exporter.grpc.ExportMetricValue;
import org.apache.skywalking.oap.server.exporter.grpc.ExportResponse;
import org.apache.skywalking.oap.server.exporter.grpc.MetricExportServiceGrpc;
import org.apache.skywalking.oap.server.exporter.grpc.SubscriptionMetric;
import org.apache.skywalking.oap.server.exporter.grpc.SubscriptionReq;
import org.apache.skywalking.oap.server.exporter.grpc.SubscriptionsResp;
import org.apache.skywalking.oap.server.exporter.grpc.ValueType;
import org.apache.skywalking.oap.server.exporter.provider.ExporterSetting;
import org.apache.skywalking.oap.server.exporter.provider.MetricFormatter;
import org.apache.skywalking.oap.server.library.client.grpc.GRPCClient;
import org.apache.skywalking.oap.server.library.datacarrier.DataCarrier;
import org.apache.skywalking.oap.server.library.datacarrier.consumer.IConsumer;
import org.apache.skywalking.oap.server.library.util.GRPCStreamStatus;

@Slf4j
public class GRPCMetricsExporter extends MetricFormatter implements MetricValuesExportService, IConsumer<ExportData> {
    /**
     * The period of subscription list fetching is hardcoded as 30s.
     */
    private static final long FETCH_SUBSCRIPTION_PERIOD = 30_000;
    private final ExporterSetting setting;
    private MetricExportServiceGrpc.MetricExportServiceStub exportServiceFutureStub;
    private MetricExportServiceGrpc.MetricExportServiceBlockingStub blockingStub;
    private DataCarrier exportBuffer;
    private ReentrantLock fetchListLock;
    private volatile List<SubscriptionMetric> subscriptionList;
    private volatile long lastFetchTimestamp = 0;

    public GRPCMetricsExporter(ExporterSetting setting) {
        this.setting = setting;
    }

    @Override
    public void start() {
        GRPCClient client = new GRPCClient(setting.getGRPCTargetHost(), setting.getGRPCTargetPort());
        client.connect();
        ManagedChannel channel = client.getChannel();
        exportServiceFutureStub = MetricExportServiceGrpc.newStub(channel);
        blockingStub = MetricExportServiceGrpc.newBlockingStub(channel);
        exportBuffer = new DataCarrier<ExportData>(setting.getBufferChannelNum(), setting.getBufferChannelSize());
        exportBuffer.consume(this, 1, 200);
        subscriptionList = new ArrayList<>();
        fetchListLock = new ReentrantLock();
    }

    @Override
    public void export(ExportEvent event) {
        Metrics metrics = event.getMetrics();
        if (metrics instanceof WithMetadata) {
            MetricsMetaInfo meta = ((WithMetadata) metrics).getMeta();
            if (subscriptionList.size() == 0 && ExportEvent.EventType.INCREMENT.equals(event.getType())) {
                exportBuffer.produce(new ExportData(meta, metrics, event.getType()));
            } else {
                subscriptionList.forEach(subscriptionMetric -> {
                    if (subscriptionMetric.getMetricName().equals(meta.getMetricsName()) &&
                        eventTypeMatch(event.getType(), subscriptionMetric.getEventType())) {
                        exportBuffer.produce(new ExportData(meta, metrics, event.getType()));
                    }
                });
            }

            fetchSubscriptionList();
        }
    }

    @Override
    public boolean isEnabled() {
        return setting.isEnableGRPCMetrics();
    }

    /**
     * Read the subscription list.
     */
    public void fetchSubscriptionList() {
        final long currentTimeMillis = System.currentTimeMillis();
        if (currentTimeMillis - lastFetchTimestamp > FETCH_SUBSCRIPTION_PERIOD) {
            fetchListLock.lock();
            try {
                if (currentTimeMillis - lastFetchTimestamp > FETCH_SUBSCRIPTION_PERIOD) {
                    lastFetchTimestamp = currentTimeMillis;
                    SubscriptionsResp subscription = blockingStub.withDeadlineAfter(10, TimeUnit.SECONDS)
                                                                 .subscription(SubscriptionReq.newBuilder().build());
                    subscriptionList = subscription.getMetricsList();
                    log.debug("Get exporter subscription list, {}", subscriptionList);
                }
            } catch (Throwable e) {
                log.error("Getting exporter subscription list fails.", e);
            } finally {
                fetchListLock.unlock();
            }
        }
    }

    @Override
    public void init(final Properties properties) {

    }

    @Override
    public void consume(List<ExportData> data) {
        GRPCStreamStatus status = new GRPCStreamStatus();
        StreamObserver<ExportMetricValue> streamObserver =
            exportServiceFutureStub.withDeadlineAfter(10, TimeUnit.SECONDS)
                                   .export(
                                       new StreamObserver<ExportResponse>() {
                                           @Override
                                           public void onNext(
                                               ExportResponse response) {

                                           }

                                           @Override
                                           public void onError(
                                               Throwable throwable) {
                                               status.done();
                                           }

                                           @Override
                                           public void onCompleted() {
                                               status.done();
                                           }
                                       });
        AtomicInteger exportNum = new AtomicInteger();
        data.forEach(row -> {
            ExportMetricValue.Builder builder = ExportMetricValue.newBuilder();

            Metrics metrics = row.getMetrics();
            if (metrics instanceof LongValueHolder) {
                long value = ((LongValueHolder) metrics).getValue();
                builder.setLongValue(value);
                builder.setType(ValueType.LONG);
            } else if (metrics instanceof IntValueHolder) {
                long value = ((IntValueHolder) metrics).getValue();
                builder.setLongValue(value);
                builder.setType(ValueType.LONG);
            } else if (metrics instanceof DoubleValueHolder) {
                double value = ((DoubleValueHolder) metrics).getValue();
                builder.setDoubleValue(value);
                builder.setType(ValueType.DOUBLE);
            } else if (metrics instanceof MultiIntValuesHolder) {
                int[] values = ((MultiIntValuesHolder) metrics).getValues();
                for (int value : values) {
                    builder.addLongValues(value);
                }
                builder.setType(ValueType.MULTI_LONG);
            } else {
                return;
            }

            MetricsMetaInfo meta = row.getMeta();
            builder.setMetricName(meta.getMetricsName());
            builder.setEventType(
                ExportEvent.EventType.INCREMENT.equals(row.getEventType()) ? EventType.INCREMENT : EventType.TOTAL);
            String entityName = getEntityName(meta);
            if (entityName == null) {
                return;
            }
            builder.setEntityName(entityName);
            builder.setEntityId(meta.getId());

            builder.setTimeBucket(metrics.getTimeBucket());

            streamObserver.onNext(builder.build());
            exportNum.getAndIncrement();
        });

        streamObserver.onCompleted();

        long sleepTime = 0;
        long cycle = 100L;

        //For memory safe of oap, we must wait for the peer confirmation.
        while (!status.isDone()) {
            try {
                sleepTime += cycle;
                Thread.sleep(cycle);
            } catch (InterruptedException e) {
            }

            if (sleepTime > 2000L) {
                log.warn(
                    "Export {} metrics to {}:{}, wait {} milliseconds.", exportNum.get(), setting.getGRPCTargetHost(),
                    setting
                        .getGRPCTargetPort(), sleepTime
                );
                cycle = 2000L;
            }
        }

        log.debug(
            "Exported {} metrics to {}:{} in {} milliseconds.", exportNum.get(), setting.getGRPCTargetHost(), setting
                .getGRPCTargetPort(), sleepTime);

        fetchSubscriptionList();
    }

    @Override
    public void onError(List<ExportData> data, Throwable t) {
        log.error(t.getMessage(), t);
    }

    @Override
    public void onExit() {

    }

    private boolean eventTypeMatch(ExportEvent.EventType eventType,
                                   org.apache.skywalking.oap.server.exporter.grpc.EventType subscriptionType) {
        return (ExportEvent.EventType.INCREMENT.equals(eventType) && EventType.INCREMENT.equals(subscriptionType))
            || (ExportEvent.EventType.TOTAL.equals(eventType) && EventType.TOTAL.equals(subscriptionType));
    }
}


text:
package org.bson.codecs;

import org.bson.BsonReader;
import org.bson.BsonSymbol;
import org.bson.BsonWriter;

/**
 * A codec for BSON symbol type.
 *
 * @since 3.0
 */
public class BsonSymbolCodec implements Codec<BsonSymbol> {
    @Override
    public BsonSymbol decode(final BsonReader reader, final DecoderContext decoderContext) {
        return new BsonSymbol(reader.readSymbol());
    }

    @Override
    public void encode(final BsonWriter writer, final BsonSymbol value, final EncoderContext encoderContext) {
        writer.writeSymbol(value.getSymbol());
    }

    @Override
    public Class<BsonSymbol> getEncoderClass() {
        return BsonSymbol.class;
    }
}


text:
import json

with open('/opt/env.json') as infile:
    env = json.load(infile)

LISTEN_PORT = int(env['ES_PROXY_LISTEN_PORT'])

# Permissions is a list of matrices
# Each matrix is the defined by the possible calls and the possible methods
# A user may be granted a set of permissions either
# on the cluster or on a particular index or indices.
PERMISSIONS = {
    # Home is considered the elasticsearch root or "/"
    'home_read': {
        'calls': ['_home', ],
        'methods': ['GET', ],
        'script': False
    },
    # Global Admin permission, allows all calls and methods
    'admin': {
        'calls': '*',
        'methods': '*',
        'script': False
    },
    # Global Admin read permission, allows all calls,
    # but just GET and HEAD methods
    'admin_read': {
        'calls': '*',
        'methods': ['GET', 'HEAD', ],
        'script': False
    },
    # Kibana admin - Kibana needs some permissions to
    # get information about the server and indices
    'kibana_admin': {
        'calls': '_nodes,',
        'methods': ['GET', 'HEAD', ],
        'script': False
    },
    # The basic calls and methods needed to search an index, or indices
    'index_search': {
        'calls': ['_search'],
        'methods': ['GET', 'POST', ],
        'script': False
    },
    # The basic calls and methods needed to read or query an index, or indices
    'index_read': {
        'calls': ['_document', '_query', ],
        'methods': ['GET', 'HEAD', ],
        'script': False
    },
    # The basic calls and methods needed to write to and index, or indices
    'index_write': {
        'calls': ['_document', '_create', ],
        'methods': ['PUT', 'POST', ],
        'script': False
    },
}

ELASTICSEARCH = {
    'url': 'http://%s:%s' % (env['ES_PORT_9200_TCP_ADDR'], env['ES_PORT_9200_TCP_PORT']),
    'auth_mode': 'basic',  # uses curl_httpclient can be basic or digest
    'auth_username': '',
    'auth_password': '',
}

#permissions get granted to users on resources based on policies
POLICIES = [
    # All users have access to the kibana internal index
    # a resource could be indices/aliases/cluster
    {
        'indices': ['kibana-int', ],
        'users': ['*'],
        'permissions': ['index_write', 'index_read']
    },
    #Kibana needs to know about the cluster _nodes
    {
        'cluster': True,
        'users': ['*'],
        'permissions': ['kibana_admin']
    },
    # sample
    {
        'indices': ['sample_index', ],
        'users': ['sample_user'],
        'permissions': ['index_read']
    },

]

#Ignore any requests for these paths
IGNORE_PATHS = [
    '/favicon.ico'
]