text:
'use strict';

const util = require('util');

/**
 * @classdesc
 * Represents a graph Element.
 * @param id
 * @param label
 * @abstract
 * @memberOf module:datastax/graph
 * @constructor
 */
function Element(id, label) {
  /**
   * Gets the element id.
   */
  this.id = id;
  /**
   * Gets the element label.
   * @type {String}
   */
  this.label = label;
}

/**
 * @classdesc
 * Represents a graph Vertex.
 * @param id
 * @param {String} label
 * @param {Object<string, Array>} properties
 * @extends {Element}
 * @memberOf module:datastax/graph
 * @constructor
 */
function Vertex(id, label, properties) {
  Element.call(this, id, label);
  /**
   * Gets the vertex properties.
   * @type {Object<string, Array>}
   */
  this.properties = properties;
}

util.inherits(Vertex, Element);

/**
 * @classdesc
 * Represents a graph Edge.
 * @param id
 * @param outV
 * @param {outVLabel} outVLabel
 * @param {String} label
 * @param inV
 * @param {String} inVLabel
 * @param {Object<string, Property>} properties
 * @extends {Element}
 * @memberOf module:datastax/graph
 * @constructor
 */
function Edge(id, outV, outVLabel, label, inV, inVLabel, properties) {
  Element.call(this, id, label);
  /**
   * Gets the id of outgoing vertex of the edge.
   */
  this.outV = outV;
  /**
   * Gets the label of the outgoing vertex.
   */
  this.outVLabel = outVLabel;
  /**
   * Gets the id of the incoming vertex of the edge.
   */
  this.inV = inV;

  /**
   * Gets the label of the incoming vertex.
   */
  this.inVLabel = inVLabel;
  /**
   * Gets the properties of the edge as an associative array.
   * @type {Object}
   */
  this.properties = {};
  (function adaptProperties(self) {
    if (properties) {
      const keys = Object.keys(properties);
      for (let i = 0; i < keys.length; i++) {
        const k = keys[i];
        self.properties[k] = properties[k].value;
      }
    }
  })(this);
}

util.inherits(Edge, Element);

/**
 * @classdesc
 * Represents a graph vertex property.
 * @param id
 * @param {String} label
 * @param value
 * @param {Object} properties
 * @extends {Element}
 * @memberOf module:datastax/graph
 * @constructor
 */
function VertexProperty(id, label, value, properties) {
  Element.call(this, id, label);
  this.value = value;
  this.key = this.label;
  this.properties = properties;
}

util.inherits(VertexProperty, Element);

/**
 * @classdesc
 * Represents a property.
 * @param key
 * @param value
 * @memberOf module:datastax/graph
 * @constructor
 */
function Property(key, value) {
  this.key = key;
  this.value = value;
}

/**
 * @classdesc
 * Represents a walk through a graph as defined by a traversal.
 * @param {Array} labels
 * @param {Array} objects
 * @memberOf module:datastax/graph
 * @constructor
 */
function Path(labels, objects) {
  this.labels = labels;
  this.objects = objects;
}

module.exports = {
  Edge,
  Element,
  Path,
  Property,
  Vertex,
  VertexProperty
};


text:
from flask_appbuilder import ModelView
from flask_appbuilder.fields import QuerySelectField
from flask_appbuilder.fieldwidgets import Select2Widget
from flask_appbuilder.models.sqla.interface import SQLAInterface

from . import appbuilder, db
from .models import Benefit, Department, Employee, EmployeeHistory, Function

def department_query():
    return db.session.query(Department)

class EmployeeHistoryView(ModelView):
    datamodel = SQLAInterface(EmployeeHistory)
    # base_permissions = ['can_add', 'can_show']
    list_columns = ["department", "begin_date", "end_date"]

class EmployeeView(ModelView):
    datamodel = SQLAInterface(Employee)

    list_columns = ["full_name", "department.name", "employee_number"]
    edit_form_extra_fields = {
        "department": QuerySelectField(
            "Department",
            query_func=department_query,
            widget=Select2Widget(extra_classes="readonly"),
        )
    }

    related_views = [EmployeeHistoryView]
    show_template = "appbuilder/general/model/show_cascade.html"

class FunctionView(ModelView):
    datamodel = SQLAInterface(Function)
    related_views = [EmployeeView]

class DepartmentView(ModelView):
    datamodel = SQLAInterface(Department)
    related_views = [EmployeeView]

class BenefitView(ModelView):
    datamodel = SQLAInterface(Benefit)
    add_columns = ["name"]
    edit_columns = ["name"]
    show_columns = ["name"]
    list_columns = ["name"]

db.create_all()

appbuilder.add_view_no_menu(EmployeeHistoryView, "EmployeeHistoryView")
appbuilder.add_view(
    EmployeeView, "Employees", icon="fa-folder-open-o", category="Company"
)
appbuilder.add_separator("Company")
appbuilder.add_view(
    DepartmentView, "Departments", icon="fa-folder-open-o", category="Company"
)
appbuilder.add_view(
    FunctionView, "Functions", icon="fa-folder-open-o", category="Company"
)
appbuilder.add_view(
    BenefitView, "Benefits", icon="fa-folder-open-o", category="Company"
)

redpajama_set_name: RedPajamaGithub


text:
const expect = require('chai').expect
const path = require('path')

// * enduro dependencies
const local_enduro = require('../../index').quick_init()
const flat_helpers = require(enduro.enduro_path + '/libs/flat_db/flat_helpers')
const test_utilities = require('../libs/test_utilities')

describe('Static assets copier', function () {

	before(function () {
		return test_utilities.before(local_enduro, 'asset_copier_testfolder')
			.then(() => {
				return enduro.actions.start()
			})
	})

	it('should have copied all images files', function () {
		expect(flat_helpers.file_exists_sync(path.join(enduro.project_path, enduro.config.build_folder, 'assets', 'img', 'imagesgohere.txt'))).to.be.ok
	})

	it('should have copied all fonts files', function () {
		expect(flat_helpers.file_exists_sync(path.join(enduro.project_path, enduro.config.build_folder, 'assets', 'fonts', 'fontsgohere.txt'))).to.be.ok
	})

	it('should have copied all javascript files', function () {
		expect(flat_helpers.file_exists_sync(path.join(enduro.project_path, enduro.config.build_folder, 'assets', 'js', 'main.js'))).to.be.ok
	})

	it('should have copied all admin_extensions files', function () {
		expect(flat_helpers.file_exists_sync(path.join(enduro.project_path, enduro.config.build_folder, 'assets', 'admin_extensions', 'sample_extension.js'))).to.be.ok
	})

	it('should have copied files to root', function () {
		expect(flat_helpers.file_exists_sync(path.join(enduro.project_path, enduro.config.build_folder, 'test.txt'))).to.be.ok
	})

	// navigate back to testfolder
	after(function () {
		return enduro.actions.stop_server()
			.then(() => {
				return test_utilities.after()
			})
	})
})


text:
package executor

import (
	"bytes"
	"context"
	"time"

	"github.com/opentracing/opentracing-go"
	"github.com/pingcap/failpoint"
	"github.com/pingcap/tidb/distsql"
	"github.com/pingcap/tidb/domain"
	"github.com/pingcap/tidb/domain/infosync"
	"github.com/pingcap/tidb/expression"
	"github.com/pingcap/tidb/infoschema"
	"github.com/pingcap/tidb/kv"
	"github.com/pingcap/tidb/parser/model"
	plannercore "github.com/pingcap/tidb/planner/core"
	"github.com/pingcap/tidb/sessionctx"
	"github.com/pingcap/tidb/sessiontxn"
	"github.com/pingcap/tidb/statistics"
	"github.com/pingcap/tidb/table"
	"github.com/pingcap/tidb/types"
	"github.com/pingcap/tidb/util/chunk"
	"github.com/pingcap/tidb/util/logutil"
	"github.com/pingcap/tidb/util/memory"
	"github.com/pingcap/tidb/util/ranger"
	"github.com/pingcap/tidb/util/stringutil"
	"github.com/pingcap/tipb/go-tipb"
	"golang.org/x/exp/slices"
)

// make sure `TableReaderExecutor` implements `Executor`.
var _ Executor = &TableReaderExecutor{}

// selectResultHook is used to hack distsql.SelectWithRuntimeStats safely for testing.
type selectResultHook struct {
	selectResultFunc func(ctx context.Context, sctx sessionctx.Context, kvReq *kv.Request,
		fieldTypes []*types.FieldType, fb *statistics.QueryFeedback, copPlanIDs []int) (distsql.SelectResult, error)
}

func (sr selectResultHook) SelectResult(ctx context.Context, sctx sessionctx.Context, kvReq *kv.Request,
	fieldTypes []*types.FieldType, fb *statistics.QueryFeedback, copPlanIDs []int, rootPlanID int) (distsql.SelectResult, error) {
	if sr.selectResultFunc == nil {
		return distsql.SelectWithRuntimeStats(ctx, sctx, kvReq, fieldTypes, fb, copPlanIDs, rootPlanID)
	}
	return sr.selectResultFunc(ctx, sctx, kvReq, fieldTypes, fb, copPlanIDs)
}

type kvRangeBuilder interface {
	buildKeyRange(ranges []*ranger.Range) ([]kv.KeyRange, error)
	buildKeyRangeSeparately(ranges []*ranger.Range) ([]int64, [][]kv.KeyRange, error)
}

// TableReaderExecutor sends DAG request and reads table data from kv layer.
type TableReaderExecutor struct {
	baseExecutor

	table table.Table

	// The source of key ranges varies from case to case.
	// It may be calculated from PhysicalPlan by executorBuilder, or calculated from argument by dataBuilder;
	// It may be calculated from ranger.Ranger, or calculated from handles.
	// The table ID may also change because of the partition table, and causes the key range to change.
	// So instead of keeping a `range` struct field, it's better to define a interface.
	kvRangeBuilder
	// TODO: remove this field, use the kvRangeBuilder interface.
	ranges []*ranger.Range

	// kvRanges are only use for union scan.
	kvRanges         []kv.KeyRange
	dagPB            *tipb.DAGRequest
	startTS          uint64
	txnScope         string
	readReplicaScope string
	isStaleness      bool
	// FIXME: in some cases the data size can be more accurate after get the handles count,
	// but we keep things simple as it needn't to be that accurate for now.
	netDataSize float64
	// columns are only required by union scan and virtual column.
	columns []*model.ColumnInfo

	// resultHandler handles the order of the result. Since (MAXInt64, MAXUint64] stores before [0, MaxInt64] physically
	// for unsigned int.
	resultHandler *tableResultHandler
	feedback      *statistics.QueryFeedback
	plans         []plannercore.PhysicalPlan
	tablePlan     plannercore.PhysicalPlan

	memTracker       *memory.Tracker
	selectResultHook // for testing

	keepOrder bool
	desc      bool
	paging    bool
	storeType kv.StoreType
	// corColInFilter tells whether there's correlated column in filter.
	corColInFilter bool
	// corColInAccess tells whether there's correlated column in access conditions.
	corColInAccess bool
	// virtualColumnIndex records all the indices of virtual columns and sort them in definition
	// to make sure we can compute the virtual column in right order.
	virtualColumnIndex []int
	// virtualColumnRetFieldTypes records the RetFieldTypes of virtual columns.
	virtualColumnRetFieldTypes []*types.FieldType
	// batchCop indicates whether use super batch coprocessor request, only works for TiFlash engine.
	batchCop bool

	// If dummy flag is set, this is not a real TableReader, it just provides the KV ranges for UnionScan.
	// Used by the temporary table, cached table.
	dummy bool
}

// Table implements the dataSourceExecutor interface.
func (e *TableReaderExecutor) Table() table.Table {
	return e.table
}

func (e *TableReaderExecutor) setDummy() {
	e.dummy = true
}

// Open initializes necessary variables for using this executor.
func (e *TableReaderExecutor) Open(ctx context.Context) error {
	if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
		span1 := span.Tracer().StartSpan("TableReaderExecutor.Open", opentracing.ChildOf(span.Context()))
		defer span1.Finish()
		ctx = opentracing.ContextWithSpan(ctx, span1)
	}
	failpoint.Inject("mockSleepInTableReaderNext", func(v failpoint.Value) {
		ms := v.(int)
		time.Sleep(time.Millisecond * time.Duration(ms))
	})

	e.memTracker = memory.NewTracker(e.id, -1)
	e.memTracker.AttachTo(e.ctx.GetSessionVars().StmtCtx.MemTracker)

	var err error
	if e.corColInFilter {
		if e.storeType == kv.TiFlash {
			execs, err := constructDistExecForTiFlash(e.ctx, e.tablePlan)
			if err != nil {
				return err
			}
			e.dagPB.RootExecutor = execs[0]
		} else {
			e.dagPB.Executors, err = constructDistExec(e.ctx, e.plans)
			if err != nil {
				return err
			}
		}
	}
	if e.runtimeStats != nil {
		collExec := true
		e.dagPB.CollectExecutionSummaries = &collExec
	}
	if e.corColInAccess {
		ts := e.plans[0].(*plannercore.PhysicalTableScan)
		e.ranges, err = ts.ResolveCorrelatedColumns()
		if err != nil {
			return err
		}
	}

	e.resultHandler = &tableResultHandler{}
	if e.feedback != nil && e.feedback.Hist != nil {
		// EncodeInt don't need *statement.Context.
		var ok bool
		e.ranges, ok = e.feedback.Hist.SplitRange(nil, e.ranges, false)
		if !ok {
			e.feedback.Invalidate()
		}
	}
	firstPartRanges, secondPartRanges := distsql.SplitRangesAcrossInt64Boundary(e.ranges, e.keepOrder, e.desc, e.table.Meta() != nil && e.table.Meta().IsCommonHandle)

	// Treat temporary table as dummy table, avoid sending distsql request to TiKV.
	// Calculate the kv ranges here, UnionScan rely on this kv ranges.
	// cached table and temporary table are similar
	if e.dummy {
		if e.desc && len(secondPartRanges) != 0 {
			// TiKV support reverse scan and the `resultHandler` process the range order.
			// While in UnionScan, it doesn't use reverse scan and reverse the final result rows manually.
			// So things are differ, we need to reverse the kv range here.
			// TODO: If we refactor UnionScan to use reverse scan, update the code here.
			// [9734095886065816708 9734095886065816709] | [1 3] [65535 9734095886065816707] => before the following change
			// [1 3] [65535 9734095886065816707] | [9734095886065816708 9734095886065816709] => ranges part reverse here
			// [1 3  65535 9734095886065816707 9734095886065816708 9734095886065816709] => scan (normal order) in UnionScan
			// [9734095886065816709 9734095886065816708 9734095886065816707 65535 3  1] => rows reverse in UnionScan
			firstPartRanges, secondPartRanges = secondPartRanges, firstPartRanges
		}
		kvReq, err := e.buildKVReq(ctx, firstPartRanges)
		if err != nil {
			return err
		}
		e.kvRanges = append(e.kvRanges, kvReq.KeyRanges...)
		if len(secondPartRanges) != 0 {
			kvReq, err = e.buildKVReq(ctx, secondPartRanges)
			if err != nil {
				return err
			}
			e.kvRanges = append(e.kvRanges, kvReq.KeyRanges...)
		}
		return nil
	}

	firstResult, err := e.buildResp(ctx, firstPartRanges)
	if err != nil {
		e.feedback.Invalidate()
		return err
	}
	if len(secondPartRanges) == 0 {
		e.resultHandler.open(nil, firstResult)
		return nil
	}
	var secondResult distsql.SelectResult
	secondResult, err = e.buildResp(ctx, secondPartRanges)
	if err != nil {
		e.feedback.Invalidate()
		return err
	}
	e.resultHandler.open(firstResult, secondResult)
	return nil
}

// Next fills data into the chunk passed by its caller.
// The task was actually done by tableReaderHandler.
func (e *TableReaderExecutor) Next(ctx context.Context, req *chunk.Chunk) error {
	if e.dummy {
		// Treat temporary table as dummy table, avoid sending distsql request to TiKV.
		req.Reset()
		return nil
	}

	logutil.Eventf(ctx, "table scan table: %s, range: %v", stringutil.MemoizeStr(func() string {
		var tableName string
		if meta := e.table.Meta(); meta != nil {
			tableName = meta.Name.L
		}
		return tableName
	}), e.ranges)
	if err := e.resultHandler.nextChunk(ctx, req); err != nil {
		e.feedback.Invalidate()
		return err
	}

	err := table.FillVirtualColumnValue(e.virtualColumnRetFieldTypes, e.virtualColumnIndex, e.schema.Columns, e.columns, e.ctx, req)
	if err != nil {
		return err
	}

	return nil
}

// Close implements the Executor Close interface.
func (e *TableReaderExecutor) Close() error {
	var err error
	if e.resultHandler != nil {
		err = e.resultHandler.Close()
	}
	e.kvRanges = e.kvRanges[:0]
	if e.dummy {
		return nil
	}
	e.ctx.StoreQueryFeedback(e.feedback)
	return err
}

// buildResp first builds request and sends it to tikv using distsql.Select. It uses SelectResult returned by the callee
// to fetch all results.
func (e *TableReaderExecutor) buildResp(ctx context.Context, ranges []*ranger.Range) (distsql.SelectResult, error) {
	if e.storeType == kv.TiFlash && e.kvRangeBuilder != nil {
		if !e.batchCop {
			// TiFlash cannot support to access multiple tables/partitions within one KVReq, so we have to build KVReq for each partition separately.
			kvReqs, err := e.buildKVReqSeparately(ctx, ranges)
			if err != nil {
				return nil, err
			}
			var results []distsql.SelectResult
			for _, kvReq := range kvReqs {
				result, err := e.SelectResult(ctx, e.ctx, kvReq, retTypes(e), e.feedback, getPhysicalPlanIDs(e.plans), e.id)
				if err != nil {
					return nil, err
				}
				results = append(results, result)
			}
			return distsql.NewSerialSelectResults(results), nil
		}
		// Use PartitionTable Scan
		kvReq, err := e.buildKVReqForPartitionTableScan(ctx, ranges)
		if err != nil {
			return nil, err
		}
		result, err := e.SelectResult(ctx, e.ctx, kvReq, retTypes(e), e.feedback, getPhysicalPlanIDs(e.plans), e.id)
		if err != nil {
			return nil, err
		}
		return result, nil
	}

	kvReq, err := e.buildKVReq(ctx, ranges)
	if err != nil {
		return nil, err
	}
	slices.SortFunc(kvReq.KeyRanges, func(i, j kv.KeyRange) bool {
		return bytes.Compare(i.StartKey, j.StartKey) < 0
	})
	e.kvRanges = append(e.kvRanges, kvReq.KeyRanges...)

	result, err := e.SelectResult(ctx, e.ctx, kvReq, retTypes(e), e.feedback, getPhysicalPlanIDs(e.plans), e.id)
	if err != nil {
		return nil, err
	}
	return result, nil
}

func (e *TableReaderExecutor) buildKVReqSeparately(ctx context.Context, ranges []*ranger.Range) ([]*kv.Request, error) {
	pids, kvRanges, err := e.kvRangeBuilder.buildKeyRangeSeparately(ranges)
	if err != nil {
		return nil, err
	}
	kvReqs := make([]*kv.Request, 0, len(kvRanges))
	for i, kvRange := range kvRanges {
		e.kvRanges = append(e.kvRanges, kvRange...)
		if err := updateExecutorTableID(ctx, e.dagPB.RootExecutor, true, []int64{pids[i]}); err != nil {
			return nil, err
		}
		var builder distsql.RequestBuilder
		reqBuilder := builder.SetKeyRanges(kvRange)
		kvReq, err := reqBuilder.
			SetDAGRequest(e.dagPB).
			SetStartTS(e.startTS).
			SetDesc(e.desc).
			SetKeepOrder(e.keepOrder).
			SetTxnScope(e.txnScope).
			SetReadReplicaScope(e.readReplicaScope).
			SetFromSessionVars(e.ctx.GetSessionVars()).
			SetFromInfoSchema(e.ctx.GetInfoSchema()).
			SetMemTracker(e.memTracker).
			SetStoreType(e.storeType).
			SetPaging(e.paging).
			SetAllowBatchCop(e.batchCop).
			SetClosestReplicaReadAdjuster(newClosestReadAdjuster(e.ctx, &reqBuilder.Request, e.netDataSize)).
			Build()
		if err != nil {
			return nil, err
		}
		kvReqs = append(kvReqs, kvReq)
	}
	return kvReqs, nil
}

func (e *TableReaderExecutor) buildKVReqForPartitionTableScan(ctx context.Context, ranges []*ranger.Range) (*kv.Request, error) {
	pids, kvRanges, err := e.kvRangeBuilder.buildKeyRangeSeparately(ranges)
	if err != nil {
		return nil, err
	}
	partitionIDAndRanges := make([]kv.PartitionIDAndRanges, 0, len(pids))
	for i, kvRange := range kvRanges {
		e.kvRanges = append(e.kvRanges, kvRange...)
		partitionIDAndRanges = append(partitionIDAndRanges, kv.PartitionIDAndRanges{
			ID:        pids[i],
			KeyRanges: kvRange,
		})
	}
	if err := updateExecutorTableID(ctx, e.dagPB.RootExecutor, true, pids); err != nil {
		return nil, err
	}
	var builder distsql.RequestBuilder
	reqBuilder := builder.SetPartitionIDAndRanges(partitionIDAndRanges)
	kvReq, err := reqBuilder.
		SetDAGRequest(e.dagPB).
		SetStartTS(e.startTS).
		SetDesc(e.desc).
		SetKeepOrder(e.keepOrder).
		SetTxnScope(e.txnScope).
		SetReadReplicaScope(e.readReplicaScope).
		SetFromSessionVars(e.ctx.GetSessionVars()).
		SetFromInfoSchema(e.ctx.GetInfoSchema()).
		SetMemTracker(e.memTracker).
		SetStoreType(e.storeType).
		SetPaging(e.paging).
		SetAllowBatchCop(e.batchCop).
		SetClosestReplicaReadAdjuster(newClosestReadAdjuster(e.ctx, &reqBuilder.Request, e.netDataSize)).
		Build()
	if err != nil {
		return nil, err
	}
	return kvReq, nil
}

func (e *TableReaderExecutor) buildKVReq(ctx context.Context, ranges []*ranger.Range) (*kv.Request, error) {
	var builder distsql.RequestBuilder
	var reqBuilder *distsql.RequestBuilder
	if e.kvRangeBuilder != nil {
		kvRange, err := e.kvRangeBuilder.buildKeyRange(ranges)
		if err != nil {
			return nil, err
		}
		reqBuilder = builder.SetKeyRanges(kvRange)
	} else {
		reqBuilder = builder.SetHandleRanges(e.ctx.GetSessionVars().StmtCtx, getPhysicalTableID(e.table), e.table.Meta() != nil && e.table.Meta().IsCommonHandle, ranges, e.feedback)
	}
	if e.table != nil && e.table.Type().IsClusterTable() {
		copDestination := infoschema.GetClusterTableCopDestination(e.table.Meta().Name.L)
		if copDestination == infoschema.DDLOwner {
			ownerManager := domain.GetDomain(e.ctx).DDL().OwnerManager()
			ddlOwnerID, err := ownerManager.GetOwnerID(ctx)
			if err != nil {
				return nil, err
			}
			serverInfo, err := infosync.GetServerInfoByID(ctx, ddlOwnerID)
			if err != nil {
				return nil, err
			}
			reqBuilder.SetTiDBServerID(serverInfo.ServerIDGetter())
		}
	}
	reqBuilder.
		SetDAGRequest(e.dagPB).
		SetStartTS(e.startTS).
		SetDesc(e.desc).
		SetKeepOrder(e.keepOrder).
		SetTxnScope(e.txnScope).
		SetReadReplicaScope(e.readReplicaScope).
		SetIsStaleness(e.isStaleness).
		SetFromSessionVars(e.ctx.GetSessionVars()).
		SetFromInfoSchema(sessiontxn.GetTxnManager(e.ctx).GetTxnInfoSchema()).
		SetMemTracker(e.memTracker).
		SetStoreType(e.storeType).
		SetAllowBatchCop(e.batchCop).
		SetClosestReplicaReadAdjuster(newClosestReadAdjuster(e.ctx, &reqBuilder.Request, e.netDataSize)).
		SetPaging(e.paging)
	return reqBuilder.Build()
}

func buildVirtualColumnIndex(schema *expression.Schema, columns []*model.ColumnInfo) []int {
	virtualColumnIndex := make([]int, 0, len(columns))
	for i, col := range schema.Columns {
		if col.VirtualExpr != nil {
			virtualColumnIndex = append(virtualColumnIndex, i)
		}
	}
	slices.SortFunc(virtualColumnIndex, func(i, j int) bool {
		return plannercore.FindColumnInfoByID(columns, schema.Columns[i].ID).Offset <
			plannercore.FindColumnInfoByID(columns, schema.Columns[j].ID).Offset
	})
	return virtualColumnIndex
}

// buildVirtualColumnInfo saves virtual column indices and sort them in definition order
func (e *TableReaderExecutor) buildVirtualColumnInfo() {
	e.virtualColumnIndex = buildVirtualColumnIndex(e.Schema(), e.columns)
	if len(e.virtualColumnIndex) > 0 {
		e.virtualColumnRetFieldTypes = make([]*types.FieldType, len(e.virtualColumnIndex))
		for i, idx := range e.virtualColumnIndex {
			e.virtualColumnRetFieldTypes[i] = e.schema.Columns[idx].RetType
		}
	}
}

type tableResultHandler struct {
	// If the pk is unsigned and we have KeepOrder=true and want ascending order,
	// `optionalResult` will handles the request whose range is in signed int range, and
	// `result` will handle the request whose range is exceed signed int range.
	// If we want descending order, `optionalResult` will handles the request whose range is exceed signed, and
	// the `result` will handle the request whose range is in signed.
	// Otherwise, we just set `optionalFinished` true and the `result` handles the whole ranges.
	optionalResult distsql.SelectResult
	result         distsql.SelectResult

	optionalFinished bool
}

func (tr *tableResultHandler) open(optionalResult, result distsql.SelectResult) {
	if optionalResult == nil {
		tr.optionalFinished = true
		tr.result = result
		return
	}
	tr.optionalResult = optionalResult
	tr.result = result
	tr.optionalFinished = false
}

func (tr *tableResultHandler) nextChunk(ctx context.Context, chk *chunk.Chunk) error {
	if !tr.optionalFinished {
		err := tr.optionalResult.Next(ctx, chk)
		if err != nil {
			return err
		}
		if chk.NumRows() > 0 {
			return nil
		}
		tr.optionalFinished = true
	}
	return tr.result.Next(ctx, chk)
}

func (tr *tableResultHandler) nextRaw(ctx context.Context) (data []byte, err error) {
	if !tr.optionalFinished {
		data, err = tr.optionalResult.NextRaw(ctx)
		if err != nil {
			return nil, err
		}
		if data != nil {
			return data, nil
		}
		tr.optionalFinished = true
	}
	data, err = tr.result.NextRaw(ctx)
	if err != nil {
		return nil, err
	}
	return data, nil
}

func (tr *tableResultHandler) Close() error {
	err := closeAll(tr.optionalResult, tr.result)
	tr.optionalResult, tr.result = nil, nil
	return err
}


text:
/*
 * 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 com.facebook.presto.operator.aggregation.state;

import com.facebook.airlift.stats.QuantileDigest;
import com.facebook.presto.spi.function.AccumulatorState;
import com.facebook.presto.spi.function.AccumulatorStateMetadata;

import java.util.List;

@AccumulatorStateMetadata(stateSerializerClass = DigestAndPercentileArrayStateSerializer.class, stateFactoryClass = DigestAndPercentileArrayStateFactory.class)
public interface DigestAndPercentileArrayState
        extends AccumulatorState
{
    QuantileDigest getDigest();

    void setDigest(QuantileDigest digest);

    void setPercentiles(List<Double> percentiles);

    List<Double> getPercentiles();

    void addMemoryUsage(int value);
}


text:
The form helper is primarily aimed at creating forms for adding and editing data.  Its workhorse method is `input()`, which checks the field type in the database schema and outputs a form element appropriate to the data type (e.g. a text input for VARCHAR, a textarea for TEXT, etc).

It also provides methods for creating inputs for associations, as shown below.

{% highlight php %}
<?php

// controllers/admin/events_controller.php

class AdminEventsController extends MvcAdminController {

  public function edit() {
    $this->set_speakers();
    $this->set_venues();
    $this->verify_id_param();
    $this->set_object();
    $this->create_or_save();
  }

  private function set_speakers() {
    $this->load_model('Speaker');
    $speakers = $this->Speaker->find(array('selects' => array('id', 'first_name', 'last_name')));
    $this->set('speakers', $speakers);
  }

  private function set_venues() {
    $this->load_model('Venue');
    $venues = $this->Venue->find(array('selects' => array('id', 'name')));
    $this->set('venues', $venues);
  }

}
?>

<?php

// views/admin/events/edit.php

$formatted_time = preg_replace('/:00$/', '', $object->time);

?>

<?php echo MvcInflector::titleize($this->action); ?> <?php echo MvcInflector::titleize($model->name); ?>

<?php echo $this->form->create($model->name); ?>
<?php echo $this->form->belongs_to_dropdown('Venue', $venues, array('style' => 'width: 200px;', 'empty' => true)); ?>
<?php echo $this->form->input('date', array('label' => 'Date (YYYY-MM-DD)')); ?>
<?php echo $this->form->input('time', array('label' => 'Time (24-hour clock)', 'value' => $formatted_time)); ?>
<?php echo $this->form->input('description'); ?>
<?php echo $this->form->has_many_dropdown('Speaker', $speakers, array('style' => 'width: 200px;', 'empty' => true)); ?>
<?php echo $this->form->input('is_public'); ?>
<?php echo $this->form->end('Update'); ?>
{% endhighlight %}

### Methods

#### create($model_name, $options=array())

#### end($label='Submit')

#### input($field_name, $options=array())

#### text_input($field_name, $options=array())

#### textarea_input($field_name, $options=array())

#### checkbox_input($field_name, $options=array())

#### hidden_input($field_name, $options=array())

#### select($field_name, $options=array())

#### select_tag($field_name, $options=array())

#### button($text, $options=array())

#### belongs_to_dropdown($model_name, $select_options, $options=array())

#### has_many_dropdown($model_name, $select_options, $options=array())


text:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Octokit
{
    ///
    /// A client for GitHub's User Administration API.
    ///
    /// <remarks>
    /// See the Administration API documentation for more details.
    /// </remarks>
    public class UserAdministrationClient : ApiClient, IUserAdministrationClient
    {
        ///
        /// Initializes a new instance of the <see cref="UserAdministrationClient"/> class.
        ///
        /// <param name="apiConnection">The client's connection</param>
        public UserAdministrationClient(IApiConnection apiConnection)
            : base(apiConnection)
        {
        }

        ///
        /// Promotes ordinary user to a site administrator.
        ///
        /// <remarks>
        /// https://developer.github.com/v3/users/administration/#promote-an-ordinary-user-to-a-site-administrator
        /// </remarks>
        /// <param name="login">The user to promote to administrator.</param>
        /// <returns></returns>
        public Task Promote(string login)
        {
            Ensure.ArgumentNotNullOrEmptyString(login, "login");
            var endpoint = ApiUrls.UserAdministration(login);
            return ApiConnection.Put(endpoint);
        }

        ///
        /// Demotes a site administrator to an ordinary user.
        ///
        /// <remarks>
        /// https://developer.github.com/v3/users/administration/#demote-a-site-administrator-to-an-ordinary-user
        /// </remarks>
        /// <param name="login">The user to demote from administrator.</param>
        /// <returns></returns>
        public Task Demote(string login)
        {
            Ensure.ArgumentNotNullOrEmptyString(login, "login");
            var endpoint = ApiUrls.UserAdministration(login);
            return ApiConnection.Delete(endpoint);
        }

        ///
        /// Suspends a user.
        ///
        /// <remarks>
        /// https://developer.github.com/v3/users/administration/#suspend-a-user
        /// </remarks>
        /// <param name="login">The user to suspend.</param>
        /// <returns></returns>
        public Task Suspend(string login)
        {
            Ensure.ArgumentNotNullOrEmptyString(login, "login");
            var endpoint = ApiUrls.UserSuspension(login);
            return ApiConnection.Put(endpoint);
        }

        ///
        /// Unsuspends a user.
        ///
        /// <remarks>
        /// https://developer.github.com/v3/users/administration/#unsuspend-a-user
        /// </remarks>
        /// <param name="login">The user to unsuspend.</param>
        /// <returns></returns>
        public Task Unsuspend(string login)
        {
            Ensure.ArgumentNotNullOrEmptyString(login, "login");
            var endpoint = ApiUrls.UserSuspension(login);
            return ApiConnection.Delete(endpoint);
        }
    }
}


text:
{% extends 'base.html' %}
{% block title %}{{ page_title(title) }}{% endblock %}
{% block content %}

        {% if settings.DEBUG %}
          Consider starting the search daemon!
        {% else %}
          {{ _('Search is temporarily unavailable.') }}
        {% endif %}

{% endblock %}


text:
#ifndef BITMASKV1_H
#define BITMASKV1_H
#include "DefinesV1.h"

NAMESPACE_LERC_START

/** BitMaskV1 - Convenient and fast access to binary mask bits
* includes RLE compression and decompression, in BitMaskV1.cpp
*
*/

class BitMaskV1
{
public:
  BitMaskV1(int nCols, int nRows) : m_pBits(nullptr), m_nRows(nRows), m_nCols(nCols)
  {
      m_pBits = new Byte[Size()];
      if (!m_pBits)
          m_nRows = m_nCols = 0;
      else
           m_pBits[Size() - 1] = 0; // Set potential pad bytes to zero
  }
  ~BitMaskV1()                                  { if (m_pBits) delete[] m_pBits; }

  Byte  IsValid(int k) const                  { return (m_pBits[k >> 3] & Bit(k)) != 0; }
  void  SetValid(int k) const                 { m_pBits[k >> 3] |= Bit(k); }
  void  SetInvalid(int k) const               { m_pBits[k >> 3] &= ~Bit(k); }
  int   Size() const                          { return (m_nCols * m_nRows - 1) / 8 + 1; }

  // max RLE compressed size is n + 4 + 2 * (n - 1) / 32767
  // Returns encoded size
  int RLEcompress(Byte *aRLE) const;
  // current encoded size
  int RLEsize() const;
  // Decompress a RLE bitmask, bitmask size should be already set
  // Returns false if input seems wrong
  bool RLEdecompress(const Byte* src, size_t nRemainingBytes) const;

private:
  Byte*  m_pBits;
  int   m_nRows, m_nCols;

  static Byte  Bit(int k)                      { return (1 << 7) >> (k & 7); }

  // Disable assignment op, default and copy constructor
  BitMaskV1();
  BitMaskV1(const BitMaskV1& copy);
  BitMaskV1& operator=(const BitMaskV1& m);
};

NAMESPACE_LERC_END
#endif


text:
layout: docs
title: Getting Started
permalink: /getting-started/
---

# Getting Started

## Create a Mix project

First of all, what is Mix? According to the [Introduction to Mix](http://elixir-lang.org/getting_started/mix/1.html) page on [elixir-lang.org](http://elixir-lang.org/),

> Mix is a build tool that provides tasks for creating, compiling, testing (and soon releasing) Elixir projects. Mix is inspired by the Leiningen build tool for Clojure and was written by one of its contributors.

That sounds super, doesn't it? At the end of the day, you will use Mix for a number of reasons in your project. The first way we will use Mix today is to have it create our initial project for us with `mix new --sup [project name]`.

```
$ mix new --sup your_project
* creating README.md
* creating .gitignore
* creating mix.exs
* creating config
* creating config/config.exs
* creating lib
* creating lib/your_project.ex
* creating test
* creating test/test_helper.exs
* creating test/your_project_test.exs

Your mix project was created successfully.
You can use mix to compile it, test it, and more:

    cd your_project
    mix test

Run "mix help" for more information.

$ cd your_project
$ ls -la
total 16
drwxr-xr-x  11 shane  staff   374 Feb 14 16:40 .
drwxr-xr-x  23 shane  staff   782 Feb 14 16:25 ..
-rw-r--r--   1 shane  staff    41 Feb 14 16:25 .gitignore
-rw-r--r--   1 shane  staff   428 Feb 14 16:25 README.md
drwxr-xr-x   2 shane  staff    23 Feb 14 16:25 config
drwxr-xr-x   4 shane  staff    28 Feb 14 16:25 lib
-rw-r--r--   1 shane  staff   705 Feb 14 16:31 mix.exs
drwxr-xr-x   4 shane  staff    56 Feb 14 16:25 test
```

## Add in Sugar

See that `mix.exs` file Mix generated for us? It contains project-level configurations, one of which is the project dependencies. We need to ensure Sugar is returned by the `deps/0` function, such as:

```elixir
def deps do
  [ {:sugar, github: "sugar-framework/sugar"} ]
end
```

If you're running Elixir v0.13.2 or higher, [hex](https://hex.pm/) is the preferred way of adding the dependency. For a general overview of this process, checkout the [hex usage page](https://hex.pm/docs/usage). Here's the tuple needed for hex:

```elixir
def deps do
  [ { :sugar, "~> 0.5" } ]
end
```

so open it up, add one of the tuples for Sugar, and save it.

You'll also need to add the Sugar application to the `application/0` function in that same file, like so:

```elixir
def application do
  [ applications: [:sugar],
    mod: {YourProject, []} ]
end
```

As of Elixir 1.4.0, the above step is unnecessary if you're using `extra_applications` instead of `applications` (the default for new Mix projects), since Mix will automatically find Sugar in your project's dependencies and do the necessary work to make sure Sugar starts when your application starts.

With all that done, we're going to use Mix to pull down a copy of Sugar and its dependencies and compile them, using `mix do deps.get, deps.compile`. This could also be accomplished in two commands, `mix deps.get` and `mix deps.compile`, but `mix do` allows us to chain commands, one after the other. This process can take a bit of time depending on the speed of your internet connection and computer.

```
$ mix do deps.get, deps.compile
* Getting sugar (git://github.com/sugar-framework/sugar.git)
Cloning into '/Volumes/Data/Code/elixir/your_project/deps/sugar'...
remote: Counting objects: 564, done.
remote: Compressing objects: 100% (255/255), done.
remote: Total 564 (delta 334), reused 464 (delta 277)
Receiving objects: 100% (564/564), 68.83 KiB | 0 bytes/s, done.
Resolving deltas: 100% (334/334), done.
Checking connectivity... done
* Getting mime (git://github.com/dynamo/mime.git)
Cloning into '/Volumes/Data/Code/elixir/your_project/deps/mime'...
remote: Reusing existing pack: 46, done.
remote: Total 46 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (46/46), 29.10 KiB | 0 bytes/s, done.
Resolving deltas: 100% (14/14), done.
Checking connectivity... done
* Getting cowboy (git://github.com/extend/cowboy.git)
Cloning into '/Volumes/Data/Code/elixir/your_project/deps/cowboy'...
remote: Reusing existing pack: 5810, done.
...
# The rest of the project dependencies are downloaded and compiled
...
* Compiling sugar
Compiled lib/mix/tasks/sugar/gen/router.ex
Compiled lib/mix/tasks/sugar/gen/config.ex
Compiled lib/mix/tasks/server.ex
Compiled lib/mix/tasks/sugar/gen/controller.ex
Compiled lib/sugar.ex
Compiled lib/mix/tasks/sugar/init.ex
Compiled lib/mix/tasks/sugar/gen/view.ex
Compiled lib/sugar/app.ex
Compiled lib/sugar/supervisor.ex
Compiled lib/sugar/controller.ex
Compiled lib/sugar/router.ex
Generated sugar.app
```

If everything goes to plan, no errors will be raised, and `Generated sugar.app` will be displayed, letting you know all of the dependencies have been compiled correctly.

With `mix` being the extensible build tool that it is, developers often add specialized `mix` tasks to their projects, adding functionality that makes using the project easier. Sugar is no exception. Let's see what's available.

```
$ mix help
mix                      # Run the default task (current: mix run)
mix archive              # Archive this project into a .ez file
mix clean                # Clean generated application files
mix cmd                  # Executes the given command
mix compile              # Compile source files
mix deps                 # List dependencies and their status
mix deps.clean           # Remove the given dependencies' files
mix deps.compile         # Compile dependencies
mix deps.get             # Get all out of date dependencies
mix deps.unlock          # Unlock the given dependencies
mix deps.update          # Update the given dependencies
mix dialyzer             # Runs dialyzer with default or project-defined flags.
mix dialyzer.plt         # Builds PLT with default erlang applications included.
mix do                   # Executes the tasks separated by comma
mix escriptize           # Generates an escript for the project
mix help                 # Print help information for tasks
mix local                # List local tasks
mix local.install        # Install a task or an archive locally
mix local.rebar          # Install rebar locally
mix local.uninstall      # Uninstall local tasks or archives
mix new                  # Create a new Elixir project
mix run                  # Run the given file or expression
mix server               # Runs Sugar and children
mix sugar.gen.controller # Creates Sugar controller files
mix sugar.gen.router     # Creates Sugar router files
mix sugar.gen.view       # Creates Sugar view files
mix sugar.init           # Creates Sugar support files
mix test                 # Run a project's tests
iex -S mix               # Start IEx and run the default task
```

All of the `mix` tasks available to your project will be listed along with a short description of what it does. Note that all of the Sugar tasks start with `sugar.*`.

Since `sugar.init` add in the necessary files for Sugar, let's run it.

```
$ mix sugar.init
* creating lib/your_project/router.ex
* creating lib/your_project/controllers
* creating lib/your_project/controllers/main.ex
* creating lib/your_project/models
* creating lib/your_project/views
* creating lib/your_project/views/main/index.html.eex
* creating priv
* creating priv/static
```

Awesome! Now we have our config module, a router, our first controller, and a view. The `sugar.init` task follows a few conventions regarding module names and file locations. For instance, `lib/your_project/controllers/main.ex` contains the `YourProject.Controllers.Main` module. Take a look around your project if you're unfamiliar with Elixir. Don't worry. We'll be here when you get back.

## Configure Your Router

A web application wouldn't serve much of a purpose if it couldn't map URLs to a particular function or set of expressions. It would essentially sit there, thinking it was doing its job when it was really doing anything but.

Enter the router. You define the routes for your web application, and the router makes sure the application does what it's supposed to when a user hits a page.

Let's see what Sugar put in our router.

```elixir
defmodule YourProject.Router do
  use Sugar.Router
  plug Sugar.Plugs.HotCodeReload

  if Sugar.Config.get(:sugar, :show_debugger, false) do
    plug Plug.Debugger, otp_app: :your_project
  end

  plug Plug.Static, at: "/static", from: :you_project

  # Uncomment the following line for session store
  # plug Plug.Session, store: :etc, key: "sid", secure: true, table: :session

  # Define your routes here
  get "/", YourProject.Controllers.Main, :index
end
```

Routes are defined with the form `method route [guard], controller, action`, so when `get "/", YourProject.Controllers.Main, :index` is used in the router, we are telling our application to call the `index` function (action) defined in the `YourProject.Controllers.Main` module (controller) whenever the `"/"` URL is accessed.

Along with defining routes, the router can also contain two other configurations: plugs and filters. Both of these allow for the modification of response on a much broader scale than what is possible with a single controller action.

To see the different ways routes can be defined or routers can be configured, checkout the documentation on [routing](/docs/routing/).

### Updating our `Mix.Config`

If you were to try to run your project at this time, it would probably fail. This is because the proper application environment vairables have not been set yet. Open up your `config/config.exs` file, and add these lines:

```elixir
config :sugar,
  router: YourProject.Router

config :sugar, YourProject.Router,
  https_only: false,
  http: [ port: 4000 ],
  https: false
```

This let's the Sugar internals know about the `YourProject.Router` module and sets the port number on which the HTTP server will listen.

Now we're ready to run `mix server`. You should see some output in your terminal window:

```
...
Generated index.html.eex
Generated show.html.eex
== Sugar running in http://127.0.0.1:4000 ==
```

Now you can access your application at `http://127.0.0.1:4000`.

## Add a Controller

One controller isn't enough to properly organize your application, you say? Good to hear! I suppose you'd like to know how to create a new controller, right? Well, look no further than your friend Mix.

```
$ mix sugar.gen.controller pages
* creating lib/your_project/controllers/pages.ex
```

Success! A new `YourProject.Controller.Pages` controller has been created for you.

```elixir
defmodule YourProject.Controllers.Pages do
  use Sugar.Controller

  def index(conn, []) do
    raw conn |> resp(200, "Hello world")
  end
end
```

Now it's your turn to take this controller and mould it to do your will. Check out the [controllers documentation](/docs/controllers/) for more information about controllers.


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

namespace System.Xml.XPath
{
    /// <summary>
    /// Reader that traverses the subtree rooted at the current position of the specified navigator.
    /// </summary>
    internal class XPathNavigatorReader : XmlReader, IXmlNamespaceResolver
    {
        enum State
        {
            Initial,
            Content,
            EndElement,
            Attribute,
            AttrVal,
            InReadBinary,
            EOF,
            Closed,
            Error,
        }

        private XPathNavigator _nav;
        private XPathNavigator _navToRead;
        private int _depth;
        private State _state;
        private XmlNodeType _nodeType;
        private int _attrCount;
        private bool _readEntireDocument;

        private ReadContentAsBinaryHelper _readBinaryHelper;
        private State _savedState;

        internal const string space = "space";

        internal static XmlNodeType[] convertFromXPathNodeType = {
            XmlNodeType.Document,               // XPathNodeType.Root
            XmlNodeType.Element,                // XPathNodeType.Element
            XmlNodeType.Attribute,              // XPathNodeType.Attribute
            XmlNodeType.Attribute,              // XPathNodeType.Namespace
            XmlNodeType.Text,                   // XPathNodeType.Text
            XmlNodeType.SignificantWhitespace,  // XPathNodeType.SignificantWhitespace
            XmlNodeType.Whitespace,             // XPathNodeType.Whitespace
            XmlNodeType.ProcessingInstruction,  // XPathNodeType.ProcessingInstruction
            XmlNodeType.Comment,                // XPathNodeType.Comment
            XmlNodeType.None                    // XPathNodeType.All
        };

        /// <summary>
        /// Translates an XPathNodeType value into the corresponding XmlNodeType value.
        /// XPathNodeType.Whitespace and XPathNodeType.SignificantWhitespace are mapped into XmlNodeType.Text.
        /// </summary>
        internal static XmlNodeType ToXmlNodeType(XPathNodeType typ)
        {
            return XPathNavigatorReader.convertFromXPathNodeType[(int)typ];
        }

        static public XPathNavigatorReader Create(XPathNavigator navToRead)
        {
            XPathNavigator nav = navToRead.Clone();
            return new XPathNavigatorReader(nav);
        }

        protected XPathNavigatorReader(XPathNavigator navToRead)
        {
            // Need clone that can be moved independently of original navigator
            _navToRead = navToRead;
            _nav = XmlEmptyNavigator.Singleton;
            _state = State.Initial;
            _depth = 0;
            _nodeType = XPathNavigatorReader.ToXmlNodeType(_nav.NodeType);
        }

        //-----------------------------------------------
        // IXmlNamespaceResolver -- pass through to Navigator
        //-----------------------------------------------
        public override XmlNameTable NameTable
        {
            get
            {
                return _navToRead.NameTable;
            }
        }

        IDictionary<string, string> IXmlNamespaceResolver.GetNamespacesInScope(XmlNamespaceScope scope)
        {
            return _nav.GetNamespacesInScope(scope);
        }

        string IXmlNamespaceResolver.LookupNamespace(string prefix)
        {
            return _nav.LookupNamespace(prefix);
        }

        string IXmlNamespaceResolver.LookupPrefix(string namespaceName)
        {
            return _nav.LookupPrefix(namespaceName);
        }

        //-----------------------------------------------
        // XmlReader -- pass through to Navigator
        //-----------------------------------------------

        public override XmlReaderSettings Settings
        {
            get
            {
                XmlReaderSettings rs = new XmlReaderSettings();
                rs.NameTable = this.NameTable;
                rs.ConformanceLevel = ConformanceLevel.Fragment;
                rs.CheckCharacters = false;
                return rs;
            }
        }

        public override System.Type ValueType
        {
            get { return _nav.ValueType; }
        }

        public override XmlNodeType NodeType
        {
            get { return _nodeType; }
        }

        public override string NamespaceURI
        {
            get
            {
                //NamespaceUri for namespace nodes is different in case of XPathNavigator and Reader
                if (_nav.NodeType == XPathNodeType.Namespace)
                    return this.NameTable.Add(XmlConst.ReservedNsXmlNs);
                //Special case attribute text node
                if (this.NodeType == XmlNodeType.Text)
                    return string.Empty;
                return _nav.NamespaceURI;
            }
        }

        public override string LocalName
        {
            get
            {
                //Default namespace in case of reader has a local name value of 'xmlns'
                if (_nav.NodeType == XPathNodeType.Namespace && _nav.LocalName.Length == 0)
                    return this.NameTable.Add("xmlns");
                //Special case attribute text node
                if (this.NodeType == XmlNodeType.Text)
                    return string.Empty;
                return _nav.LocalName;
            }
        }

        public override string Prefix
        {
            get
            {
                //Prefix for namespace nodes is different in case of XPathNavigator and Reader
                if (_nav.NodeType == XPathNodeType.Namespace && _nav.LocalName.Length != 0)
                    return this.NameTable.Add("xmlns");
                //Special case attribute text node
                if (this.NodeType == XmlNodeType.Text)
                    return string.Empty;
                return _nav.Prefix;
            }
        }

        public override string BaseURI
        {
            get
            {
                //reader returns BaseUri even before read method is called.
                if (_state == State.Initial)
                    return _navToRead.BaseURI;
                return _nav.BaseURI;
            }
        }

        public override bool IsEmptyElement
        {
            get
            {
                return _nav.IsEmptyElement;
            }
        }

        public override XmlSpace XmlSpace
        {
            get
            {
                XPathNavigator tempNav = _nav.Clone();
                do
                {
                    if (tempNav.MoveToAttribute(XPathNavigatorReader.space, XmlConst.ReservedNsXml))
                    {
                        switch (XmlConvertEx.TrimString(tempNav.Value))
                        {
                            case "default":
                                return XmlSpace.Default;
                            case "preserve":
                                return XmlSpace.Preserve;
                            default:
                                break;
                        }
                        tempNav.MoveToParent();
                    }
                }
                while (tempNav.MoveToParent());
                return XmlSpace.None;
            }
        }

        public override string XmlLang
        {
            get
            {
                return _nav.XmlLang;
            }
        }

        public override bool HasValue
        {
            get
            {
                if ((_nodeType != XmlNodeType.Element)
                    && (_nodeType != XmlNodeType.Document)
                    && (_nodeType != XmlNodeType.EndElement)
                    && (_nodeType != XmlNodeType.None))
                    return true;
                return false;
            }
        }

        public override string Value
        {
            get
            {
                if ((_nodeType != XmlNodeType.Element)
                    && (_nodeType != XmlNodeType.Document)
                    && (_nodeType != XmlNodeType.EndElement)
                    && (_nodeType != XmlNodeType.None))
                    return _nav.Value;
                return string.Empty;
            }
        }

        private XPathNavigator GetElemNav()
        {
            XPathNavigator tempNav;
            switch (_state)
            {
                case State.Content:
                    return _nav.Clone();
                case State.Attribute:
                case State.AttrVal:
                    tempNav = _nav.Clone();
                    if (tempNav.MoveToParent())
                        return tempNav;
                    break;
                case State.InReadBinary:
                    _state = _savedState;
                    XPathNavigator nav = GetElemNav();
                    _state = State.InReadBinary;
                    return nav;
            }
            return null;
        }

        private XPathNavigator GetElemNav(out int depth)
        {
            XPathNavigator nav = null;
            switch (_state)
            {
                case State.Content:
                    if (_nodeType == XmlNodeType.Element)
                        nav = _nav.Clone();
                    depth = _depth;
                    break;
                case State.Attribute:
                    nav = _nav.Clone();
                    nav.MoveToParent();
                    depth = _depth - 1;
                    break;
                case State.AttrVal:
                    nav = _nav.Clone();
                    nav.MoveToParent();
                    depth = _depth - 2;
                    break;
                case State.InReadBinary:
                    _state = _savedState;
                    nav = GetElemNav(out depth);
                    _state = State.InReadBinary;
                    break;
                default:
                    depth = _depth;
                    break;
            }
            return nav;
        }

        private void MoveToAttr(XPathNavigator nav, int depth)
        {
            _nav.MoveTo(nav);
            _depth = depth;
            _nodeType = XmlNodeType.Attribute;
            _state = State.Attribute;
        }

        public override int AttributeCount
        {
            get
            {
                if (_attrCount < 0)
                {
                    // attribute count works for element, regardless of where you are in start tag
                    XPathNavigator tempNav = GetElemNav();
                    int count = 0;
                    if (null != tempNav)
                    {
                        if (tempNav.MoveToFirstNamespace(XPathNamespaceScope.Local))
                        {
                            do
                            {
                                count++;
                            } while (tempNav.MoveToNextNamespace((XPathNamespaceScope.Local)));
                            tempNav.MoveToParent();
                        }
                        if (tempNav.MoveToFirstAttribute())
                        {
                            do
                            {
                                count++;
                            } while (tempNav.MoveToNextAttribute());
                        }
                    }
                    _attrCount = count;
                }
                return _attrCount;
            }
        }

        public override string GetAttribute(string name)
        {
            // reader allows calling GetAttribute, even when positioned inside attributes
            XPathNavigator nav = _nav;
            switch (nav.NodeType)
            {
                case XPathNodeType.Element:
                    break;
                case XPathNodeType.Attribute:
                    nav = nav.Clone();
                    if (!nav.MoveToParent())
                        return null;
                    break;
                default:
                    return null;
            }
            string prefix, localname;
            ValidateNames.SplitQName(name, out prefix, out localname);
            if (0 == prefix.Length)
            {
                if (localname == "xmlns")
                    return nav.GetNamespace(string.Empty);
                if ((object)nav == (object)_nav)
                    nav = nav.Clone();
                if (nav.MoveToAttribute(localname, string.Empty))
                    return nav.Value;
            }
            else
            {
                if (prefix == "xmlns")
                    return nav.GetNamespace(localname);
                if ((object)nav == (object)_nav)
                    nav = nav.Clone();
                if (nav.MoveToFirstAttribute())
                {
                    do
                    {
                        if (nav.LocalName == localname && nav.Prefix == prefix)
                            return nav.Value;
                    } while (nav.MoveToNextAttribute());
                }
            }
            return null;
        }

        public override string GetAttribute(string localName, string namespaceURI)
        {
            if (null == localName)
                throw new ArgumentNullException(nameof(localName));
            // reader allows calling GetAttribute, even when positioned inside attributes
            XPathNavigator nav = _nav;
            switch (nav.NodeType)
            {
                case XPathNodeType.Element:
                    break;
                case XPathNodeType.Attribute:
                    nav = nav.Clone();
                    if (!nav.MoveToParent())
                        return null;
                    break;
                default:
                    return null;
            }
            // are they really looking for a namespace-decl?
            if (namespaceURI == XmlConst.ReservedNsXmlNs)
            {
                if (localName == "xmlns")
                    localName = string.Empty;
                return nav.GetNamespace(localName);
            }
            if (null == namespaceURI)
                namespaceURI = string.Empty;
            // We need to clone the navigator and move the clone to the attribute to see whether the attribute exists,
            // because XPathNavigator.GetAttribute return string.Empty for both when the the attribute is not there or when
            // it has an empty value. XmlReader.GetAttribute must return null if the attribute does not exist.
            if ((object)nav == (object)_nav)
                nav = nav.Clone();
            if (nav.MoveToAttribute(localName, namespaceURI))
            {
                return nav.Value;
            }
            else
            {
                return null;
            }
        }

        private static string GetNamespaceByIndex(XPathNavigator nav, int index, out int count)
        {
            string thisValue = nav.Value;
            string value = null;
            if (nav.MoveToNextNamespace(XPathNamespaceScope.Local))
            {
                value = GetNamespaceByIndex(nav, index, out count);
            }
            else
            {
                count = 0;
            }
            if (count == index)
            {
                Debug.Assert(value == null);
                value = thisValue;
            }
            count++;
            return value;
        }

        public override string GetAttribute(int index)
        {
            if (index < 0)
                goto Error;
            XPathNavigator nav = GetElemNav();
            if (null == nav)
                goto Error;
            if (nav.MoveToFirstNamespace(XPathNamespaceScope.Local))
            {
                // namespaces are returned in reverse order,
                // but we want to return them in the correct order,
                // so first count the namespaces
                int nsCount;
                string value = GetNamespaceByIndex(nav, index, out nsCount);
                if (null != value)
                {
                    return value;
                }
                index -= nsCount;
                nav.MoveToParent();
            }
            if (nav.MoveToFirstAttribute())
            {
                do
                {
                    if (index == 0)
                        return nav.Value;
                    index--;
                } while (nav.MoveToNextAttribute());
            }
        // can't find it... error
        Error:
            throw new ArgumentOutOfRangeException(nameof(index));
        }

        public override bool MoveToAttribute(string localName, string namespaceName)
        {
            if (null == localName)
                throw new ArgumentNullException(nameof(localName));
            int depth = _depth;
            XPathNavigator nav = GetElemNav(out depth);
            if (null != nav)
            {
                if (namespaceName == XmlConst.ReservedNsXmlNs)
                {
                    if (localName == "xmlns")
                        localName = string.Empty;
                    if (nav.MoveToFirstNamespace(XPathNamespaceScope.Local))
                    {
                        do
                        {
                            if (nav.LocalName == localName)
                                goto FoundMatch;
                        } while (nav.MoveToNextNamespace(XPathNamespaceScope.Local));
                    }
                }
                else
                {
                    if (null == namespaceName)
                        namespaceName = string.Empty;
                    if (nav.MoveToAttribute(localName, namespaceName))
                        goto FoundMatch;
                }
            }
            return false;

        FoundMatch:
            if (_state == State.InReadBinary)
            {
                _readBinaryHelper.Finish();
                _state = _savedState;
            }
            MoveToAttr(nav, depth + 1);
            return true;
        }

        public override bool MoveToFirstAttribute()
        {
            int depth;
            XPathNavigator nav = GetElemNav(out depth);
            if (null != nav)
            {
                if (nav.MoveToFirstNamespace(XPathNamespaceScope.Local))
                {
                    // attributes are in reverse order
                    while (nav.MoveToNextNamespace(XPathNamespaceScope.Local))
                        ;
                    goto FoundMatch;
                }
                if (nav.MoveToFirstAttribute())
                {
                    goto FoundMatch;
                }
            }
            return false;
        FoundMatch:
            if (_state == State.InReadBinary)
            {
                _readBinaryHelper.Finish();
                _state = _savedState;
            }
            MoveToAttr(nav, depth + 1);
            return true;
        }

        public override bool MoveToNextAttribute()
        {
            switch (_state)
            {
                case State.Content:
                    return MoveToFirstAttribute();

                case State.Attribute:
                    {
                        if (XPathNodeType.Attribute == _nav.NodeType)
                            return _nav.MoveToNextAttribute();

                        // otherwise it is on a namespace... namespace are in reverse order
                        Debug.Assert(XPathNodeType.Namespace == _nav.NodeType);
                        XPathNavigator nav = _nav.Clone();
                        if (!nav.MoveToParent())
                            return false; // shouldn't happen
                        if (!nav.MoveToFirstNamespace(XPathNamespaceScope.Local))
                            return false; // shouldn't happen
                        if (nav.IsSamePosition(_nav))
                        {
                            // this was the last one... start walking attributes
                            nav.MoveToParent();
                            if (!nav.MoveToFirstAttribute())
                                return false;
                            // otherwise we are there
                            _nav.MoveTo(nav);
                            return true;
                        }
                        else
                        {
                            XPathNavigator prev = nav.Clone();
                            for (; ;)
                            {
                                if (!nav.MoveToNextNamespace(XPathNamespaceScope.Local))
                                {
                                    Debug.Fail("Couldn't find Namespace Node! Should not happen!");
                                    return false;
                                }
                                if (nav.IsSamePosition(_nav))
                                {
                                    _nav.MoveTo(prev);
                                    return true;
                                }
                                prev.MoveTo(nav);
                            }
                            // found previous namespace position
                        }
                    }
                case State.AttrVal:
                    _depth--;
                    _state = State.Attribute;
                    if (!MoveToNextAttribute())
                    {
                        _depth++;
                        _state = State.AttrVal;
                        return false;
                    }
                    _nodeType = XmlNodeType.Attribute;
                    return true;

                case State.InReadBinary:
                    _state = _savedState;
                    if (!MoveToNextAttribute())
                    {
                        _state = State.InReadBinary;
                        return false;
                    }
                    _readBinaryHelper.Finish();
                    return true;

                default:
                    return false;
            }
        }

        public override bool MoveToAttribute(string name)
        {
            int depth;
            XPathNavigator nav = GetElemNav(out depth);
            if (null == nav)
                return false;

            string prefix, localname;
            ValidateNames.SplitQName(name, out prefix, out localname);

            // watch for a namespace name
            bool IsXmlnsNoPrefix = false;
            if ((IsXmlnsNoPrefix = (0 == prefix.Length && localname == "xmlns"))
                || (prefix == "xmlns"))
            {
                if (IsXmlnsNoPrefix)
                    localname = string.Empty;
                if (nav.MoveToFirstNamespace(XPathNamespaceScope.Local))
                {
                    do
                    {
                        if (nav.LocalName == localname)
                            goto FoundMatch;
                    } while (nav.MoveToNextNamespace(XPathNamespaceScope.Local));
                }
            }
            else if (0 == prefix.Length)
            {
                // the empty prefix always means empty namespaceUri for attributes
                if (nav.MoveToAttribute(localname, string.Empty))
                    goto FoundMatch;
            }
            else
            {
                if (nav.MoveToFirstAttribute())
                {
                    do
                    {
                        if (nav.LocalName == localname && nav.Prefix == prefix)
                            goto FoundMatch;
                    } while (nav.MoveToNextAttribute());
                }
            }
            return false;

        FoundMatch:
            if (_state == State.InReadBinary)
            {
                _readBinaryHelper.Finish();
                _state = _savedState;
            }
            MoveToAttr(nav, depth + 1);
            return true;
        }

        public override bool MoveToElement()
        {
            switch (_state)
            {
                case State.Attribute:
                case State.AttrVal:
                    if (!_nav.MoveToParent())
                        return false;
                    _depth--;
                    if (_state == State.AttrVal)
                        _depth--;
                    _state = State.Content;
                    _nodeType = XmlNodeType.Element;
                    return true;
                case State.InReadBinary:
                    _state = _savedState;
                    if (!MoveToElement())
                    {
                        _state = State.InReadBinary;
                        return false;
                    }
                    _readBinaryHelper.Finish();
                    break;
            }
            return false;
        }

        public override bool EOF
        {
            get
            {
                return _state == State.EOF;
            }
        }

        public override ReadState ReadState
        {
            get
            {
                switch (_state)
                {
                    case State.Initial:
                        return ReadState.Initial;
                    case State.Content:
                    case State.EndElement:
                    case State.Attribute:
                    case State.AttrVal:
                    case State.InReadBinary:
                        return ReadState.Interactive;
                    case State.EOF:
                        return ReadState.EndOfFile;
                    case State.Closed:
                        return ReadState.Closed;
                    default:
                        return ReadState.Error;
                }
            }
        }

        public override void ResolveEntity()
        {
            throw new InvalidOperationException(SR.Xml_InvalidOperation);
        }

        public override bool ReadAttributeValue()
        {
            if (_state == State.InReadBinary)
            {
                _readBinaryHelper.Finish();
                _state = _savedState;
            }
            if (_state == State.Attribute)
            {
                _state = State.AttrVal;
                _nodeType = XmlNodeType.Text;
                _depth++;
                return true;
            }
            return false;
        }

        public override bool CanReadBinaryContent
        {
            get
            {
                return true;
            }
        }

        public override int ReadContentAsBase64(byte[] buffer, int index, int count)
        {
            if (ReadState != ReadState.Interactive)
            {
                return 0;
            }

            // init ReadContentAsBinaryHelper when called first time
            if (_state != State.InReadBinary)
            {
                _readBinaryHelper = ReadContentAsBinaryHelper.CreateOrReset(_readBinaryHelper, this);
                _savedState = _state;
            }

            // turn off InReadBinary state in order to have a normal Read() behavior when called from readBinaryHelper
            _state = _savedState;

            // call to the helper
            int readCount = _readBinaryHelper.ReadContentAsBase64(buffer, index, count);

            // turn on InReadBinary state again and return
            _savedState = _state;
            _state = State.InReadBinary;
            return readCount;
        }

        public override int ReadContentAsBinHex(byte[] buffer, int index, int count)
        {
            if (ReadState != ReadState.Interactive)
            {
                return 0;
            }

            // init ReadContentAsBinaryHelper when called first time
            if (_state != State.InReadBinary)
            {
                _readBinaryHelper = ReadContentAsBinaryHelper.CreateOrReset(_readBinaryHelper, this);
                _savedState = _state;
            }

            // turn off InReadBinary state in order to have a normal Read() behavior when called from readBinaryHelper
            _state = _savedState;

            // call to the helper
            int readCount = _readBinaryHelper.ReadContentAsBinHex(buffer, index, count);

            // turn on InReadBinary state again and return
            _savedState = _state;
            _state = State.InReadBinary;
            return readCount;
        }

        public override int ReadElementContentAsBase64(byte[] buffer, int index, int count)
        {
            if (ReadState != ReadState.Interactive)
            {
                return 0;
            }

            // init ReadContentAsBinaryHelper when called first time
            if (_state != State.InReadBinary)
            {
                _readBinaryHelper = ReadContentAsBinaryHelper.CreateOrReset(_readBinaryHelper, this);
                _savedState = _state;
            }

            // turn off InReadBinary state in order to have a normal Read() behavior when called from readBinaryHelper
            _state = _savedState;

            // call to the helper
            int readCount = _readBinaryHelper.ReadElementContentAsBase64(buffer, index, count);

            // turn on InReadBinary state again and return
            _savedState = _state;
            _state = State.InReadBinary;
            return readCount;
        }

        public override int ReadElementContentAsBinHex(byte[] buffer, int index, int count)
        {
            if (ReadState != ReadState.Interactive)
            {
                return 0;
            }

            // init ReadContentAsBinaryHelper when called first time
            if (_state != State.InReadBinary)
            {
                _readBinaryHelper = ReadContentAsBinaryHelper.CreateOrReset(_readBinaryHelper, this);
                _savedState = _state;
            }

            // turn off InReadBinary state in order to have a normal Read() behavior when called from readBinaryHelper
            _state = _savedState;

            // call to the helper
            int readCount = _readBinaryHelper.ReadElementContentAsBinHex(buffer, index, count);

            // turn on InReadBinary state again and return
            _savedState = _state;
            _state = State.InReadBinary;
            return readCount;
        }

        public override string LookupNamespace(string prefix)
        {
            return _nav.LookupNamespace(prefix);
        }

        /// <summary>
        /// Current depth in subtree.
        /// </summary>
        public override int Depth
        {
            get { return _depth; }
        }

        /// <summary>
        /// Move to the next reader state.  Return false if that is ReaderState.Closed.
        /// </summary>
        public override bool Read()
        {
            _attrCount = -1;
            switch (_state)
            {
                case State.Error:
                case State.Closed:
                case State.EOF:
                    return false;

                case State.Initial:
                    // Starting state depends on the navigator's item type
                    _nav = _navToRead;
                    _state = State.Content;
                    if (XPathNodeType.Root == _nav.NodeType)
                    {
                        if (!_nav.MoveToFirstChild())
                        {
                            SetEOF();
                            return false;
                        }
                        _readEntireDocument = true;
                    }
                    else if (XPathNodeType.Attribute == _nav.NodeType)
                    {
                        _state = State.Attribute;
                    }
                    _nodeType = ToXmlNodeType(_nav.NodeType);
                    break;

                case State.Content:
                    if (_nav.MoveToFirstChild())
                    {
                        _nodeType = ToXmlNodeType(_nav.NodeType);
                        _depth++;
                        _state = State.Content;
                    }
                    else if (_nodeType == XmlNodeType.Element
                        && !_nav.IsEmptyElement)
                    {
                        _nodeType = XmlNodeType.EndElement;
                        _state = State.EndElement;
                    }
                    else
                        goto case State.EndElement;
                    break;

                case State.EndElement:
                    if (0 == _depth && !_readEntireDocument)
                    {
                        SetEOF();
                        return false;
                    }
                    else if (_nav.MoveToNext())
                    {
                        _nodeType = ToXmlNodeType(_nav.NodeType);
                        _state = State.Content;
                    }
                    else if (_depth > 0 && _nav.MoveToParent())
                    {
                        Debug.Assert(_nav.NodeType == XPathNodeType.Element, _nav.NodeType.ToString() + " == XPathNodeType.Element");
                        _nodeType = XmlNodeType.EndElement;
                        _state = State.EndElement;
                        _depth--;
                    }
                    else
                    {
                        SetEOF();
                        return false;
                    }
                    break;

                case State.Attribute:
                case State.AttrVal:
                    if (!_nav.MoveToParent())
                    {
                        SetEOF();
                        return false;
                    }
                    _nodeType = ToXmlNodeType(_nav.NodeType);
                    _depth--;
                    if (_state == State.AttrVal)
                        _depth--;
                    goto case State.Content;
                case State.InReadBinary:
                    _state = _savedState;
                    _readBinaryHelper.Finish();
                    return Read();
            }
            return true;
        }

        /// <summary>
        /// set reader to EOF state
        /// </summary>
        private void SetEOF()
        {
            _nav = XmlEmptyNavigator.Singleton;
            _nodeType = XmlNodeType.None;
            _state = State.EOF;
            _depth = 0;
        }
    }

    /// <summary>
    /// The XmlEmptyNavigator exposes a document node with no children.
    /// Only one XmlEmptyNavigator exists per AppDomain (Singleton).  That's why the constructor is private.
    /// Use the Singleton property to get the EmptyNavigator.
    /// </summary>
    internal class XmlEmptyNavigator : XPathNavigator
    {
        private static volatile XmlEmptyNavigator s_singleton;

        private XmlEmptyNavigator()
        {
        }

        public static XmlEmptyNavigator Singleton
        {
            get
            {
                if (XmlEmptyNavigator.s_singleton == null)
                    XmlEmptyNavigator.s_singleton = new XmlEmptyNavigator();
                return XmlEmptyNavigator.s_singleton;
            }
        }

        //-----------------------------------------------
        // XmlReader
        //-----------------------------------------------

        public override XPathNodeType NodeType
        {
            get { return XPathNodeType.All; }
        }

        public override string NamespaceURI
        {
            get { return string.Empty; }
        }

        public override string LocalName
        {
            get { return string.Empty; }
        }

        public override string Name
        {
            get { return string.Empty; }
        }

        public override string Prefix
        {
            get { return string.Empty; }
        }

        public override string BaseURI
        {
            get { return string.Empty; }
        }

        public override string Value
        {
            get { return string.Empty; }
        }

        public override bool IsEmptyElement
        {
            get { return false; }
        }

        public override string XmlLang
        {
            get { return string.Empty; }
        }

        public override bool HasAttributes
        {
            get { return false; }
        }

        public override bool HasChildren
        {
            get { return false; }
        }

        //-----------------------------------------------
        // IXmlNamespaceResolver
        //-----------------------------------------------

        public override XmlNameTable NameTable
        {
            get { return new NameTable(); }
        }

        public override bool MoveToFirstChild()
        {
            return false;
        }

        public override void MoveToRoot()
        {
            //always on root
            return;
        }

        public override bool MoveToNext()
        {
            return false;
        }

        public override bool MoveToPrevious()
        {
            return false;
        }

        public override bool MoveToFirst()
        {
            return false;
        }

        public override bool MoveToFirstAttribute()
        {
            return false;
        }

        public override bool MoveToNextAttribute()
        {
            return false;
        }

        public override bool MoveToId(string id)
        {
            return false;
        }

        public override string GetAttribute(string localName, string namespaceName)
        {
            return null;
        }

        public override bool MoveToAttribute(string localName, string namespaceName)
        {
            return false;
        }

        public override string GetNamespace(string name)
        {
            return null;
        }

        public override bool MoveToNamespace(string prefix)
        {
            return false;
        }

        public override bool MoveToFirstNamespace(XPathNamespaceScope scope)
        {
            return false;
        }

        public override bool MoveToNextNamespace(XPathNamespaceScope scope)
        {
            return false;
        }

        public override bool MoveToParent()
        {
            return false;
        }

        public override bool MoveTo(XPathNavigator other)
        {
            // Only one instance of XmlEmptyNavigator exists on the system
            return (object)this == (object)other;
        }

        public override XmlNodeOrder ComparePosition(XPathNavigator other)
        {
            // Only one instance of XmlEmptyNavigator exists on the system
            return ((object)this == (object)other) ? XmlNodeOrder.Same : XmlNodeOrder.Unknown;
        }

        public override bool IsSamePosition(XPathNavigator other)
        {
            // Only one instance of XmlEmptyNavigator exists on the system
            return (object)this == (object)other;
        }

        //-----------------------------------------------
        // XPathNavigator2
        //-----------------------------------------------
        public override XPathNavigator Clone()
        {
            // Singleton, so clone just returns this
            return this;
        }
    }
}


text:
@interface RoundedButton : UIButton

@end


text:
/**
 * File: type_length_value.h
 * Description: Header file for data structure and macro definition of the TLV, the type length value encoding format of Provision Message
 *
 * Unaligned data will be used and for TLV, no data structure but some macros are used to extract information from TLV (due to var size of some fields)
 * A tlv_info_t structure is defined for decoded TLV header information
 *
 *To encoding TLV:
 *  i) declare array of tlv_info_t and filling type/version/size of each TLV
 *  ii) call get_tlv_msg_size to calculate size of encoded buffer
 *  iii) prepare the output buffer in tlv_msg_t (set both msg_buf and msg_size)
 *  iv) call tlv_msg_init to initialize tlv headers of all sub-tlv
 *  v) use function like cipher_text_tlv_get_encrypted_text(...) to get pointer to each payload component
 *  vi) copy corresponding data into each payload
 *To decoding TLV:
 *  i) initialize tlv_msg_t to input buffer (msg_buf and msg_size)
 *  ii) call read_tlv_info to get correpondent tlv_info_t array
 *  iii) use function like cipher_text_tlv_get_encrypted_text(...) to get pointer to each payload component
 *           or access size field of tlv_info_t directly to get size of payload
 *Usually, there're multiple level of TLVs.
 * So in encoding TLV, we need
 *  go through step i) and ii) from inner-most level TLV to out-most level of TLV to define
 *   tlv_info_t of all level of TLVs and get msg size for them
 *     Usually, the tlv_info_t of outer level is dependent on size of inner level
 *  After that, we could prepare the output buffer which is step iii) according to size of outmost level TLVs
 *   call tlv_msg_init to initialize TLV headers and get start address of payload (where inner TLV will share the memory)
 *      from outmost level to innermost level which is step iv) and v)
 * In decoding TLV, it is simpler, just decode from outmost-level to innermost-level and do decryption if required
 */

#ifndef _PVE_TLV_H
#define _PVE_TLV_H
#include "epid_types.h"
#include "sgx_urts.h"
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "oal/oal.h"
#include "tlv_common.h"
#include "se_sig_rl.h"
#include "se_wrapper.h"

#define FOUR_BYTES_SIZE_TYPE       128      /*mask used in type of TLV to indicate that 'size' field uses 4 bytes*/

#ifndef UINT16_MAX
#define UINT16_MAX                 0xFFFF
#endif
#ifndef UINT32_MAX
#define UINT32_MAX                 0xFFFFFFFFU
#endif

#define IS_FOUR_BYTES_SIZE_TYPE(x) (((x)&FOUR_BYTES_SIZE_TYPE)!=0)
#define GET_TLV_TYPE(x)            ((uint8_t)((x)&~FOUR_BYTES_SIZE_TYPE))

typedef enum _tlv_status_t {
    TLV_SUCCESS=0,
    TLV_OUT_OF_MEMORY_ERROR=1,
    TLV_INVALID_PARAMETER_ERROR,
    TLV_INVALID_MSG_ERROR,
    TLV_UNKNOWN_ERROR,
    TLV_MORE_TLVS,                 /*There're more TLVs in the encoded buffer than user's expectation*/
    TLV_INSUFFICIENT_MEMORY,       /*There'ld be more data in the TLV buffer according to the partially decoded data*/
    TLV_INVALID_FORMAT,            /*Invalid data format in the TLV buffer to be decoded*/
    TLV_UNSUPPORTED                /*the feature has not been supported such as version is later than supported version*/
}tlv_status_t;

/*usually, we could initialize header_size by UNKOWN_TLV_HEADER_SIZE
 *but sometimes, we could initialize it by LARGE_TLV_HEADER_SIZE if we want to always use 4 bytes for size even though it is small such as in EPIDSignature TLV*/
#define UNKNOWN_TLV_HEADER_SIZE    0
#define TLV_HEADER_SIZE_OFFSET     2
#define SMALL_TLV_HEADER_SIZE      4
#define LARGE_TLV_HEADER_SIZE      6
#define MAX_TLV_HEADER_SIZE        6      /*an upper bound for TLV header size*/
#define SHORT_TLV_MAX_SIZE         UINT16_MAX
/*It defines a TLV information
 *All those information is encoded inside a TLV but not in this structure*/
typedef struct _tlv_info_t{
    uint8_t  type;                 /*type of tlv must be between 0 and 127 before encoding*/
    uint8_t  version;
    uint16_t header_size;          /*header_size used to easy query begin and end address of TLV*/
    uint32_t size;                 /* 2 or 4 bytes size after encoding but always 4 bytes in this structure*/
    uint8_t  *payload;             /*pointer to payload of the TLV*/
}tlv_info_t;

typedef struct _tlv_msg_t{
    uint8_t *msg_buf;
    uint32_t msg_size;
}tlv_msg_t;

#define SIGRL_CERT_PREFIX_SIZE          (sizeof(se_sig_rl_t)-sizeof(SigRL))             /*size for version and type*/
#define SIGRL_CERT_HEADER_SIZE          (sizeof(se_sig_rl_t)-sizeof(SigRLEntry))
#define SIGRL_CERT_SIZE(sigrl_count)    (sizeof(se_sig_rl_t)+((sigrl_count)-1)*sizeof(SigRLEntry)+2*SE_ECDSA_SIGN_SIZE)
#define EPID_SIGNATURE_HEADER_SIZE      (sizeof(EPIDSignature)-sizeof(NRProof))
#define EPID_SIGNATURE_SIZE(sign_count) (sizeof(EPIDSignature)+((sign_count)-1)*sizeof(NRProof))

/*Function to decode a buffer which contains a list of TLV containers
 *msg.msg_buf: pointer to the start address of the buffer
 *msg.msg_size: number of bytes of the buffer
 *tlvs_count: input/output parameter, the input *tlvs_count gives size of array infos and offsets
 *              when the function return successful (TLV_SUCCESS) or there're too many TLVs (TLV_MORE_TLVS),
 *              the output of *tlvs_count returns the number of TLVs in the msg
 *infos:     output parameter, infos[i] gives the decoded TLV structure information of the i'th TLV in the msg
 *return:      TLV_SUCCESS on success or other value to indicate error
 */
tlv_status_t read_tlv_infos(const tlv_msg_t& msg, uint32_t *tlvs_count, tlv_info_t infos[]);

/*Function to return the header size in encoded TLV buffer*/
uint32_t get_tlv_header_size(const tlv_info_t *info);

/*Function to return the estimated upper bound of length of TLV in bytes given length in bytes of payload.
 * Currently, it returns the exact value for TLV encoding, it should not be used for TLV decoding
 *return 0 if there's any error
 */
inline static uint32_t get_tlv_total_size(size_t payload_size)
{
    if(payload_size>UINT16_MAX){/*6 bytes TLV header*/
        if(payload_size>UINT32_MAX-LARGE_TLV_HEADER_SIZE)/*overflow of uint32_t, return 0 to indicate error*/
            return 0;
        return static_cast<uint32_t>(payload_size+LARGE_TLV_HEADER_SIZE);
    }else{
        return static_cast<uint32_t>(payload_size+SMALL_TLV_HEADER_SIZE);/*4 bytes TLV header*/
    }
}

/*Function to return number in bytes of a msg with some encoded TLVs
 *tlvs_count gives number of TLVs in the msg and infos[i] gives info of the i'th TLV*/
uint32_t get_tlv_msg_size(uint32_t tlvs_count, const tlv_info_t infos[]);

/*Function to initialize a msg of some encoded TLVs
 *Before calling the function, all fields except payload of infos[.] has been initialized
 *After calling to the function, the TLV header of all TLVs will be fill in and payload of info[.] will be initialized
 *After calling to the function, we could use payload field
 *tlvs_count:    input parameter gives number of TLV in the msg
 *infos:        input parameter where infos[i] is the tlv structure information of the i'th TLV
 *tlv_msg.msg_buf:  the msg buffer to be initialized
 *tlv_msg.msg_size: input parameter gives the size of the msg buffer, it must be result of function get_tlv_msg_count(tlvs_count, infos);
 *The function return TLV_SUCCESS on success and other to indicate any error
 */
tlv_status_t tlv_msg_init(uint32_t tlvs_count, tlv_info_t infos[], const tlv_msg_t& tlv_msg);

/*Function used to initialize msg header only without checking payload size
 *The function will return size in bytes of the header to be filled
 *   the payload field will be set but payload size not checked
 *msg: The buffer to fill
 *info: the tlv_info of the TLV
 */
uint32_t tlv_msg_init_one_header(uint8_t msg[MAX_TLV_HEADER_SIZE], tlv_info_t& info);

/*Macro used to crack tlv structure, the pointer type of payload must be uint8_t *
 *The alignment of all types used inside payload should be 1 (which means no alignment)
 *cipher text TLV*/
#define CIPHER_TEXT_TLV_PAYLOAD_SIZE(text_size)         ((text_size)+1) /*size of payload*/
#define CIPHER_TEXT_TLV_SIZE(text_size)                 get_tlv_total_size(CIPHER_TEXT_TLV_PAYLOAD_SIZE(text_size)) /*size of TLV*/
uint8_t *cipher_text_tlv_get_key_id(const tlv_info_t& info);            /*given tlv_info_t, return pointer to key_id*/
tlv_msg_t cipher_text_tlv_get_encrypted_text(const tlv_info_t& info);   /*given tlv_info_t return tlv_msg for encrypted text so that we could restart decode*/
/*block cipher text TLV*/
#define BLOCK_CIPHER_TEXT_TLV_PAYLOAD_SIZE(text_size)   ((text_size)+IV_SIZE)     /*size of payload*/
#define BLOCK_CIPHER_TEXT_TLV_SIZE(text_size)           get_tlv_total_size(BLOCK_CIPHER_TEXT_TLV_PAYLOAD_SIZE(text_size)) /*size of TLV*/
struct _tlv_iv_t;/*an incomplete type for iv only*/
typedef struct _tlv_iv_t tlv_iv_t;
tlv_iv_t *block_cipher_tlv_get_iv(const tlv_info_t& info);
tlv_msg_t block_cipher_tlv_get_encrypted_text(const tlv_info_t& info);
#define BLOCK_CIPHER_TEXT_SIZE_FROM_PAYLOAD_SIZE(psize) ((psize)-IV_SIZE)

/*block cipher info TLV*/
#define BLOCK_CIPHER_INFO_TLV_PAYLOAD_SIZE()            SK_SIZE              /*size of payload*/
#define BLOCK_CIPHER_INFO_TLV_SIZE()                    get_tlv_total_size(BLOCK_CIPHER_INFO_TLV_PAYLOAD_SIZE())
struct _tlv_sk_t; /*an incomplete type for SK only*/
typedef struct _tlv_sk_t tlv_sk_t;
tlv_sk_t *block_cipher_info_tlv_get_sk(const tlv_info_t& info);

/*message authentication code TLV*/
#define MAC_TLV_PAYLOAD_SIZE(mac_size)                  (mac_size)
#define MAC_TLV_SIZE(mac_size)                          get_tlv_total_size(MAC_TLV_PAYLOAD_SIZE(mac_size))
struct _tlv_mac_t; /*an incomplete type for MAC only*/
typedef struct _tlv_mac_t tlv_mac_t;
tlv_mac_t *mac_tlv_get_mac(const tlv_info_t& info);
/*NONCE TLV*/
#define NONCE_TLV_PAYLOAD_SIZE(nonce_size)              (nonce_size)
#define NONCE_TLV_SIZE(nonce_size)                      get_tlv_total_size(NONCE_TLV_PAYLOAD_SIZE(nonce_size))
struct _tlv_nonce_t;
typedef struct _tlv_nonce_t tlv_nonce_t;
tlv_nonce_t *nonce_tlv_get_nonce(const tlv_info_t& info);

/*EPID GID TLV*/
#define EPID_GID_TLV_PAYLOAD_SIZE()                     (sizeof(GroupID))
#define EPID_GID_TLV_SIZE()                             get_tlv_total_size(EPID_GID_TLV_PAYLOAD_SIZE())
struct _tlv_gid_t; /*an incomplete type for GID only*/
typedef struct _tlv_gid_t tlv_gid_t;
tlv_sk_t *epid_gid_tlv_get_gid(const tlv_info_t& info);

/*EPID SigRL TLV*/
#define EPID_SIGRL_TLV_PAYLOAD_SIZE(sigrl_count)        (sizeof(se_sig_rl_t)+sizeof(SigRLEntry)*(sigrl_count)-sizeof(SigRLEntry)+ECDSA_SIGN_SIZE*2)
#define EPID_SIGRL_TLV_SIZE(sigrl_count)                (sigrl_count>0?get_tlv_total_size(EPID_SIGRL_TLV_PAYLOAD_SIZE(sigrl_count)):0)

/*EPID SigRL PSVN TLV*/
#define EPID_SIGRL_PSVN_TLV_PAYLOAD_SIZE()              (sizeof(psvn_t))
#define EPID_SIGRL_PSVN_TLV_SIZE()                      get_tlv_total_size(EPID_SIGRL_PSVN_TLV_PAYLOAD_SIZE())
psvn_t *epid_sigrl_psvn_tlv_get_psvn(const tlv_info_t& info);

/*EPID group certificate TLV*/
#define EPID_GROUP_CERT_TLV_PAYLOAD_SIZE()              (sizeof(signed_epid_group_cert_t))
#define EPID_GROUP_CERT_TLV_SIZE()                      get_tlv_total_size(EPID_GROUP_CERT_TLV_PAYLOAD_SIZE())

/*Device ID TLV*/
#define DEVICE_ID_TLV_PAYLOAD_SIZE()                    (sizeof(ppid_t)+sizeof(fmsp_t)+sizeof(psvn_t))
#define DEVICE_ID_TLV_SIZE()                            get_tlv_total_size(DEVICE_ID_TLV_PAYLOAD_SIZE())
ppid_t *device_id_tlv_get_ppid(const tlv_info_t& info);
fmsp_t *device_id_tlv_get_fmsp(const tlv_info_t& info);
psvn_t *device_id_tlv_get_psvn(const tlv_info_t& info);

/*PSID TLV*/
#define PSID_TLV_PAYLOAD_SIZE()                         (sizeof(psid_t))
#define PSID_TLV_SIZE()                                 get_tlv_total_size(PSID_TLV_PAYLOAD_SIZE())
psid_t *psid_tlv_get_psid(const tlv_info_t& info);

/*Join Proof TLV*/
#define EPID_JOIN_PROOF_TLV_PAYLOAD_SIZE()              (JOIN_PROOF_SIZE+BLIND_ESCROW_SIZE)
#define EPID_JOIN_PROOF_TLV_SIZE()                      get_tlv_total_size(EPID_JOIN_PROOF_TLV_PAYLOAD_SIZE())

/*EPID Signature TLV*/
#define EPID_SIGNATURE_TLV_PAYLOAD_SIZE(sign_count)     (sizeof(EPIDSignature)+sizeof(NRProof)*(sign_count)-sizeof(NRProof))
#define EPID_SIGNATURE_TLV_SIZE(sign_count)             (LARGE_TLV_HEADER_SIZE+EPID_SIGNATURE_TLV_PAYLOAD_SIZE(sign_count)) /*always use large header size for Signature TLV*/

/*EPID Membership Credential TLV*/
#define MEMBERSHIP_CREDENTIAL_TLV_PAYLOAD_SIZE()        (sizeof(membertship_credential_with_escrow_t))
#define MEMBERSHIP_CREDENTIAL_TLV_SIZE()                get_tlv_total_size(MEMBERSHIP_CREDENTIAL_TLV_PAYLOAD_SIZE())
PElemStr *membership_credential_tlv_get_x(const tlv_info_t& info);
G1ElemStr *membership_credential_tlv_get_A(const tlv_info_t& info);

/*FLAGS TLV*/
#define FLAGS_TLV_PAYLOAD_SIZE()    FLAGS_SIZE
#define FLAGS_TLV_SIZE()            get_tlv_total_size(FLAGS_TLV_PAYLOAD_SIZE())

#define ES_SELECTOR_TLV_PAYLOAD_SIZE()                  2
#define ES_SELECTOR_TLV_SIZE()                          get_tlv_total_size(ES_SELECTOR_TLV_PAYLOAD_SIZE())
uint8_t *es_selector_tlv_get_es_type(const tlv_info_t& info);
uint8_t *es_selector_tlv_get_selector_id(const tlv_info_t& info);

class TLVsMsg{
    uint32_t num_infos;
    tlv_info_t* infos;
    tlv_msg_t msg;
    CLASS_UNCOPYABLE(TLVsMsg)
protected:
    void clear(){
        if(msg.msg_buf!=NULL){free(msg.msg_buf);msg.msg_buf=NULL;msg.msg_size=0;}
        if(infos!=NULL){free(infos);infos=NULL;num_infos=0;}
    }
    tlv_status_t alloc_more_buffer(uint32_t buffer, tlv_msg_t& new_buf);
    tlv_status_t create_new_info(tlv_info_t *& new_info){
        if(num_infos==0){
            assert(infos==NULL);
            infos = (tlv_info_t *)malloc(sizeof(tlv_info_t));
            if(infos==NULL) return TLV_OUT_OF_MEMORY_ERROR;
            num_infos=1;
            new_info = infos;
        }else{
            tlv_info_t * p = (tlv_info_t *)malloc(sizeof(tlv_info_t)*(num_infos+1));
            if(p==NULL){
                return TLV_OUT_OF_MEMORY_ERROR;
            }
            if(0!=memcpy_s(p, sizeof(tlv_info_t)*(num_infos+1), infos, sizeof(tlv_info_t)*num_infos)){
                free(p);
                return TLV_UNKNOWN_ERROR;
            }
            free(infos);
            infos = p;
            new_info = infos + (num_infos);
            num_infos ++;
        }
        return TLV_SUCCESS;
    }
public:
    TLVsMsg(){
        num_infos = 0;
        infos = NULL;
        msg.msg_size = 0;
        msg.msg_buf = NULL;
    }
    ~TLVsMsg(){
        clear();
    }
    tlv_status_t init_from_buffer(const uint8_t *msg, uint32_t msg_size);
    tlv_status_t init_from_tlv_msg(const tlv_msg_t& tlv_msg);
    uint32_t get_tlv_count()const{return num_infos;}
    tlv_info_t& operator[](uint32_t x){
        assert(x<num_infos&&infos!=NULL);
        return infos[x];
    }
    const tlv_info_t& operator[](uint32_t x)const{
        assert(x<num_infos&&infos!=NULL);
        return infos[x];
    }
    uint32_t get_tlv_msg_size()const{return msg.msg_size;}
    const uint8_t *get_tlv_msg()const{return msg.msg_buf;}
    /*Functions to add different types of TLV into the TLVsMsg*/
    tlv_status_t add_cipher_text(const uint8_t *text, uint32_t len, uint8_t key_id);
    tlv_status_t add_block_cipher_text(const uint8_t iv[IV_SIZE],const uint8_t *text, uint32_t len);
    tlv_status_t add_block_cipher_info(const uint8_t sk[SK_SIZE]);
    tlv_status_t add_mac(const uint8_t mac[MAC_SIZE]);
    tlv_status_t add_nonce(const uint8_t *nonce, uint32_t nonce_size);
    tlv_status_t add_epid_gid(const GroupID& gid);
    tlv_status_t add_es_selector(uint8_t protocol, uint8_t selector_id);
    tlv_status_t add_quote(const uint8_t *quote_data, uint32_t quote_size);
    tlv_status_t add_quote_signature(const uint8_t *quote_signature, uint32_t sign_size);
    tlv_status_t add_x509_csr(const uint8_t *csr_data, uint32_t csr_size);
};

#endif


text:
fn function(x: isize) -> &'static [isize] {
    &[x] //~ ERROR cannot return reference to temporary value
}

fn main() {
    let x = function(1);
    let y = x[0];
}