go.mercari.io/datastore@v1.8.2/caches.go (about)

     1  package datastore
     2  
     3  import (
     4  	"bytes"
     5  	"context"
     6  	"fmt"
     7  	"strconv"
     8  	"strings"
     9  )
    10  
    11  // Middleware hooks to the Datastore's RPC and It can modify arguments and return values.
    12  // see https://godoc.org/go.mercari.io/datastore/dsmiddleware
    13  type Middleware interface {
    14  	// AllocateIDs intercepts AllocateIDs operation.
    15  	AllocateIDs(info *MiddlewareInfo, keys []Key) ([]Key, error)
    16  	// PutMultiWithoutTx intercepts PutMulti without Transaction operation.
    17  	PutMultiWithoutTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) ([]Key, error)
    18  	// PutMultiWithTx intercepts PutMulti with Transaction operation.
    19  	PutMultiWithTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) ([]PendingKey, error)
    20  	// GetMultiWithoutTx intercepts GetMulti without Transaction operation.
    21  	GetMultiWithoutTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) error
    22  	// GetMultiWithTx intercepts GetMulti with Transaction operation.
    23  	GetMultiWithTx(info *MiddlewareInfo, keys []Key, psList []PropertyList) error
    24  	// DeleteMultiWithoutTx intercepts DeleteMulti without Transaction operation.
    25  	DeleteMultiWithoutTx(info *MiddlewareInfo, keys []Key) error
    26  	// DeleteMultiWithTx intercepts DeleteMulti with Transaction operation.
    27  	DeleteMultiWithTx(info *MiddlewareInfo, keys []Key) error
    28  	// PostCommit will kicked after Transaction commit.
    29  	PostCommit(info *MiddlewareInfo, tx Transaction, commit Commit) error
    30  	// PostRollback will kicked after Transaction rollback.
    31  	PostRollback(info *MiddlewareInfo, tx Transaction) error
    32  	// Run intercepts Run query operation.
    33  	Run(info *MiddlewareInfo, q Query, qDump *QueryDump) Iterator
    34  	// GetAll intercepts GetAll operation.
    35  	GetAll(info *MiddlewareInfo, q Query, qDump *QueryDump, psList *[]PropertyList) ([]Key, error)
    36  	// Next intercepts Next operation.
    37  	Next(info *MiddlewareInfo, q Query, qDump *QueryDump, iter Iterator, ps *PropertyList) (Key, error)
    38  	// Count intercepts Count operation.
    39  	Count(info *MiddlewareInfo, q Query, qDump *QueryDump) (int, error)
    40  }
    41  
    42  // MiddlewareInfo provides RPC's processing state.
    43  type MiddlewareInfo struct {
    44  	Context     context.Context
    45  	Client      Client
    46  	Transaction Transaction
    47  	Next        Middleware
    48  }
    49  
    50  // QueryDump provides information of executed query.
    51  type QueryDump struct {
    52  	Kind                string
    53  	Ancestor            Key
    54  	EventualConsistency bool
    55  	Namespace           string
    56  	Transaction         Transaction
    57  	Filter              []*QueryFilterCondition
    58  	Order               []string
    59  	Project             []string
    60  	DistinctOn          []string
    61  	Distinct            bool
    62  	KeysOnly            bool
    63  	Limit               int
    64  	Offset              int
    65  	Start               Cursor
    66  	End                 Cursor
    67  }
    68  
    69  func (dump *QueryDump) String() string {
    70  	// generate keys that are unique for queries
    71  	// TODO ProjectID...?
    72  
    73  	b := bytes.NewBufferString("v1:") // encoding format version
    74  	b.WriteString(dump.Kind)
    75  
    76  	if dump.Ancestor != nil {
    77  		b.WriteString("&a=")
    78  		b.WriteString(dump.Ancestor.String())
    79  	}
    80  
    81  	if dump.EventualConsistency {
    82  		b.WriteString("&e=t")
    83  	}
    84  
    85  	if dump.Namespace != "" {
    86  		b.WriteString("&n=")
    87  		b.WriteString(dump.Namespace)
    88  	}
    89  
    90  	if dump.Transaction != nil {
    91  		b.WriteString("&t=t")
    92  	}
    93  
    94  	if l := len(dump.Filter); l != 0 {
    95  		b.WriteString("&f=")
    96  		for idx, f := range dump.Filter {
    97  			b.WriteString(f.Filter)
    98  			b.WriteString(fmt.Sprintf("%+v", f.Value))
    99  			if (idx + 1) != l {
   100  				b.WriteString("|")
   101  			}
   102  		}
   103  	}
   104  	if l := len(dump.Order); l != 0 {
   105  		b.WriteString("&or=")
   106  		b.WriteString(strings.Join(dump.Order, "|"))
   107  	}
   108  	if l := len(dump.Project); l != 0 {
   109  		b.WriteString("&p=")
   110  		b.WriteString(strings.Join(dump.Project, "|"))
   111  	}
   112  	if dump.Distinct {
   113  		b.WriteString("&d=t")
   114  	}
   115  	if dump.KeysOnly {
   116  		b.WriteString("&k=t")
   117  	}
   118  	if dump.Limit != 0 {
   119  		b.WriteString("&l=")
   120  		b.WriteString(strconv.Itoa(dump.Limit))
   121  	}
   122  	if dump.Offset != 0 {
   123  		b.WriteString("&o=")
   124  		b.WriteString(strconv.Itoa(dump.Offset))
   125  	}
   126  	if dump.Start != nil {
   127  		b.WriteString("&s=")
   128  		b.WriteString(dump.Start.String())
   129  	}
   130  	if dump.End != nil {
   131  		b.WriteString("&e=")
   132  		b.WriteString(dump.End.String())
   133  	}
   134  
   135  	return b.String()
   136  }
   137  
   138  // QueryFilterCondition provides information of filter of query.
   139  type QueryFilterCondition struct {
   140  	Filter string
   141  	Value  interface{}
   142  }