// ExportCSV encodes the fragment designated by the index,field,shard as
// CSV of the form <row>,<col>
ExportCSV encodes the fragment designated by the index,field,shard as
CSV of the form ,
// ExportCSV encodes the fragment designated by the index field shard as // CSV of the form <row > <col >
func (api *API) ExportCSV(ctx context.Context, indexName string, fieldName string, shard uint64, w io.Writer) error {
	span, _ := tracing.StartSpanFromContext(ctx, "API.ExportCSV")
	defer span.Finish()
	if err := api.validate(apiExportCSV); err != nil {
		return errors.Wrap(err, "validating api method")
	}
	if !api.cluster.ownsShard(api.Node().ID, indexName, shard) {
		api.server.logger.Printf("node %s does not own shard %d of index %s", api.Node().ID, shard, indexName)
		return ErrClusterDoesNotOwnShard
	}
	index := api.holder.Index(indexName)
	if index == nil {
		return newNotFoundError(ErrIndexNotFound)
	}
	field := index.Field(fieldName)
	if field == nil {
		return newNotFoundError(ErrFieldNotFound)
	}
	f := api.holder.fragment(indexName, fieldName, viewStandard, shard)
	if f == nil {
		return ErrFragmentNotFound
	}
	cw := csv.NewWriter(w)
	var n int
	fn := func(rowID, columnID uint64) error {
		var rowStr string
		var colStr string
		var err error
		if field.keys() {
			if rowStr, err = api.holder.translateFile.TranslateRowToString(index.Name(), field.Name(), rowID); err != nil {
				return errors.Wrap(err, "translating row")
			}
		} else {
			rowStr = strconv.FormatUint(rowID, 10)
		}
		if index.Keys() {
			if colStr, err = api.holder.translateFile.TranslateColumnToString(index.Name(), columnID); err != nil {
				return errors.Wrap(err, "translating column")
			}
		} else {
			colStr = strconv.FormatUint(columnID, 10)
		}
		n++
		return cw.Write([]string{rowStr, colStr})
	}
	if err := f.forEachBit(fn); err != nil {
		return errors.Wrap(err, "writing CSV")
	}
	cw.Flush()
	span.LogKV("n", n)
	return nil
}
func ( api * API ) ExportCSV ( ctx context . Context , indexName string , fieldName string , shard uint64 , w io . Writer ) error { span , _ := tracing . StartSpanFromContext ( ctx , " " ) defer span . Finish ( ) if err := api . validate ( apiExportCSV ) ; err != nil { return errors . Wrap ( err , " " ) } if ! api . cluster . ownsShard ( api . Node ( ) . ID , indexName , shard ) { api . server . logger . Printf ( " " , api . Node ( ) . ID , shard , indexName ) return ErrClusterDoesNotOwnShard } index := api . holder . Index ( indexName ) if index == nil { return newNotFoundError ( ErrIndexNotFound ) } field := index . Field ( fieldName ) if field == nil { return newNotFoundError ( ErrFieldNotFound ) } f := api . holder . fragment ( indexName , fieldName , viewStandard , shard ) if f == nil { return ErrFragmentNotFound } cw := csv . NewWriter ( w ) var n int fn := func ( rowID , columnID uint64 ) error { var rowStr string var colStr string var err error if field . keys ( ) { if rowStr , err = api . holder . translateFile . TranslateRowToString ( index . Name ( ) , field . Name ( ) , rowID ) ; err != nil { return errors . Wrap ( err , " " ) } } else { rowStr = strconv . FormatUint ( rowID , 10 ) } if index . Keys ( ) { if colStr , err = api . holder . translateFile . TranslateColumnToString ( index . Name ( ) , columnID ) ; err != nil { return errors . Wrap ( err , " " ) } } else { colStr = strconv . FormatUint ( columnID , 10 ) } n ++ return cw . Write ( [ ] string { rowStr , colStr } ) } if err := f . forEachBit ( fn ) ; err != nil { return errors . Wrap ( err , " " ) } cw . Flush ( ) span . LogKV ( " " , n ) return nil }
ExportCSV encodes the fragment designated by the index,field,shard as
CSV of the form ,
ExportCSV encodes the fragment designated by the index field shard as CSV of the form
// Validate that this handler owns the shard. // Find index. // Find field from the index. // Find the fragment. // Wrap writer with a CSV writer. // Define the function to write each bit as a string, // translating to keys where necessary. // Iterate over each column. // Ensure data is flushed.