You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

398 lines
12 KiB

  1. /*
  2. Copyright 2017 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package spanner
  14. import (
  15. "bytes"
  16. "fmt"
  17. "time"
  18. "cloud.google.com/go/civil"
  19. proto3 "github.com/golang/protobuf/ptypes/struct"
  20. sppb "google.golang.org/genproto/googleapis/spanner/v1"
  21. "google.golang.org/grpc/codes"
  22. )
  23. // A Key can be either a Cloud Spanner row's primary key or a secondary index key.
  24. // It is essentially an interface{} array, which represents a set of Cloud Spanner
  25. // columns. A Key can be used as:
  26. //
  27. // - A primary key which uniquely identifies a Cloud Spanner row.
  28. // - A secondary index key which maps to a set of Cloud Spanner rows indexed under it.
  29. // - An endpoint of primary key/secondary index ranges; see the KeyRange type.
  30. //
  31. // Rows that are identified by the Key type are outputs of read operation or targets of
  32. // delete operation in a mutation. Note that for Insert/Update/InsertOrUpdate/Update
  33. // mutation types, although they don't require a primary key explicitly, the column list
  34. // provided must contain enough columns that can comprise a primary key.
  35. //
  36. // Keys are easy to construct. For example, suppose you have a table with a
  37. // primary key of username and product ID. To make a key for this table:
  38. //
  39. // key := spanner.Key{"john", 16}
  40. //
  41. // See the description of Row and Mutation types for how Go types are
  42. // mapped to Cloud Spanner types. For convenience, Key type supports a wide range
  43. // of Go types:
  44. // - int, int8, int16, int32, int64, and NullInt64 are mapped to Cloud Spanner's INT64 type.
  45. // - uint8, uint16 and uint32 are also mapped to Cloud Spanner's INT64 type.
  46. // - float32, float64, NullFloat64 are mapped to Cloud Spanner's FLOAT64 type.
  47. // - bool and NullBool are mapped to Cloud Spanner's BOOL type.
  48. // - []byte is mapped to Cloud Spanner's BYTES type.
  49. // - string and NullString are mapped to Cloud Spanner's STRING type.
  50. // - time.Time and NullTime are mapped to Cloud Spanner's TIMESTAMP type.
  51. // - civil.Date and NullDate are mapped to Cloud Spanner's DATE type.
  52. type Key []interface{}
  53. // errInvdKeyPartType returns error for unsupported key part type.
  54. func errInvdKeyPartType(part interface{}) error {
  55. return spannerErrorf(codes.InvalidArgument, "key part has unsupported type %T", part)
  56. }
  57. // keyPartValue converts a part of the Key (which is a valid Cloud Spanner type)
  58. // into a proto3.Value. Used for encoding Key type into protobuf.
  59. func keyPartValue(part interface{}) (pb *proto3.Value, err error) {
  60. switch v := part.(type) {
  61. case int:
  62. pb, _, err = encodeValue(int64(v))
  63. case int8:
  64. pb, _, err = encodeValue(int64(v))
  65. case int16:
  66. pb, _, err = encodeValue(int64(v))
  67. case int32:
  68. pb, _, err = encodeValue(int64(v))
  69. case uint8:
  70. pb, _, err = encodeValue(int64(v))
  71. case uint16:
  72. pb, _, err = encodeValue(int64(v))
  73. case uint32:
  74. pb, _, err = encodeValue(int64(v))
  75. case float32:
  76. pb, _, err = encodeValue(float64(v))
  77. case int64, float64, NullInt64, NullFloat64, bool, NullBool, []byte, string, NullString, time.Time, civil.Date, NullTime, NullDate:
  78. pb, _, err = encodeValue(v)
  79. default:
  80. return nil, errInvdKeyPartType(v)
  81. }
  82. return pb, err
  83. }
  84. // proto converts a spanner.Key into a proto3.ListValue.
  85. func (key Key) proto() (*proto3.ListValue, error) {
  86. lv := &proto3.ListValue{}
  87. lv.Values = make([]*proto3.Value, 0, len(key))
  88. for _, part := range key {
  89. v, err := keyPartValue(part)
  90. if err != nil {
  91. return nil, err
  92. }
  93. lv.Values = append(lv.Values, v)
  94. }
  95. return lv, nil
  96. }
  97. // keySetProto lets a single Key act as a KeySet.
  98. func (key Key) keySetProto() (*sppb.KeySet, error) {
  99. kp, err := key.proto()
  100. if err != nil {
  101. return nil, err
  102. }
  103. return &sppb.KeySet{Keys: []*proto3.ListValue{kp}}, nil
  104. }
  105. // String implements fmt.Stringer for Key. For string, []byte and NullString, it
  106. // prints the uninterpreted bytes of their contents, leaving caller with the
  107. // opportunity to escape the output.
  108. func (key Key) String() string {
  109. b := &bytes.Buffer{}
  110. fmt.Fprint(b, "(")
  111. for i, part := range []interface{}(key) {
  112. if i != 0 {
  113. fmt.Fprint(b, ",")
  114. }
  115. switch v := part.(type) {
  116. case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, float32, float64, bool:
  117. // Use %v to print numeric types and bool.
  118. fmt.Fprintf(b, "%v", v)
  119. case string:
  120. fmt.Fprintf(b, "%q", v)
  121. case []byte:
  122. if v != nil {
  123. fmt.Fprintf(b, "%q", v)
  124. } else {
  125. fmt.Fprint(b, "<null>")
  126. }
  127. case NullInt64, NullFloat64, NullBool, NullString, NullTime, NullDate:
  128. // The above types implement fmt.Stringer.
  129. fmt.Fprintf(b, "%s", v)
  130. case civil.Date:
  131. fmt.Fprintf(b, "%q", v)
  132. case time.Time:
  133. fmt.Fprintf(b, "%q", v.Format(time.RFC3339Nano))
  134. default:
  135. fmt.Fprintf(b, "%v", v)
  136. }
  137. }
  138. fmt.Fprint(b, ")")
  139. return b.String()
  140. }
  141. // AsPrefix returns a KeyRange for all keys where k is the prefix.
  142. func (key Key) AsPrefix() KeyRange {
  143. return KeyRange{
  144. Start: key,
  145. End: key,
  146. Kind: ClosedClosed,
  147. }
  148. }
  149. // KeyRangeKind describes the kind of interval represented by a KeyRange:
  150. // whether it is open or closed on the left and right.
  151. type KeyRangeKind int
  152. const (
  153. // ClosedOpen is closed on the left and open on the right: the Start
  154. // key is included, the End key is excluded.
  155. ClosedOpen KeyRangeKind = iota
  156. // ClosedClosed is closed on the left and the right: both keys are included.
  157. ClosedClosed
  158. // OpenClosed is open on the left and closed on the right: the Start
  159. // key is excluded, the End key is included.
  160. OpenClosed
  161. // OpenOpen is open on the left and the right: neither key is included.
  162. OpenOpen
  163. )
  164. // A KeyRange represents a range of rows in a table or index.
  165. //
  166. // A range has a Start key and an End key. IncludeStart and IncludeEnd
  167. // indicate whether the Start and End keys are included in the range.
  168. //
  169. // For example, consider the following table definition:
  170. //
  171. // CREATE TABLE UserEvents (
  172. // UserName STRING(MAX),
  173. // EventDate STRING(10),
  174. // ) PRIMARY KEY(UserName, EventDate);
  175. //
  176. // The following keys name rows in this table:
  177. //
  178. // spanner.Key{"Bob", "2014-09-23"}
  179. // spanner.Key{"Alfred", "2015-06-12"}
  180. //
  181. // Since the UserEvents table's PRIMARY KEY clause names two columns, each
  182. // UserEvents key has two elements; the first is the UserName, and the second
  183. // is the EventDate.
  184. //
  185. // Key ranges with multiple components are interpreted lexicographically by
  186. // component using the table or index key's declared sort order. For example,
  187. // the following range returns all events for user "Bob" that occurred in the
  188. // year 2015:
  189. //
  190. // spanner.KeyRange{
  191. // Start: spanner.Key{"Bob", "2015-01-01"},
  192. // End: spanner.Key{"Bob", "2015-12-31"},
  193. // Kind: ClosedClosed,
  194. // }
  195. //
  196. // Start and end keys can omit trailing key components. This affects the
  197. // inclusion and exclusion of rows that exactly match the provided key
  198. // components: if IncludeStart is true, then rows that exactly match the
  199. // provided components of the Start key are included; if IncludeStart is false
  200. // then rows that exactly match are not included. IncludeEnd and End key
  201. // behave in the same fashion.
  202. //
  203. // For example, the following range includes all events for "Bob" that occurred
  204. // during and after the year 2000:
  205. //
  206. // spanner.KeyRange{
  207. // Start: spanner.Key{"Bob", "2000-01-01"},
  208. // End: spanner.Key{"Bob"},
  209. // Kind: ClosedClosed,
  210. // }
  211. //
  212. // The next example retrieves all events for "Bob":
  213. //
  214. // spanner.Key{"Bob"}.AsPrefix()
  215. //
  216. // To retrieve events before the year 2000:
  217. //
  218. // spanner.KeyRange{
  219. // Start: spanner.Key{"Bob"},
  220. // End: spanner.Key{"Bob", "2000-01-01"},
  221. // Kind: ClosedOpen,
  222. // }
  223. //
  224. // Although we specified a Kind for this KeyRange, we didn't need to, because
  225. // the default is ClosedOpen. In later examples we'll omit Kind if it is
  226. // ClosedOpen.
  227. //
  228. // The following range includes all rows in a table or under a
  229. // index:
  230. //
  231. // spanner.AllKeys()
  232. //
  233. // This range returns all users whose UserName begins with any
  234. // character from A to C:
  235. //
  236. // spanner.KeyRange{
  237. // Start: spanner.Key{"A"},
  238. // End: spanner.Key{"D"},
  239. // }
  240. //
  241. // This range returns all users whose UserName begins with B:
  242. //
  243. // spanner.KeyRange{
  244. // Start: spanner.Key{"B"},
  245. // End: spanner.Key{"C"},
  246. // }
  247. //
  248. // Key ranges honor column sort order. For example, suppose a table is defined
  249. // as follows:
  250. //
  251. // CREATE TABLE DescendingSortedTable {
  252. // Key INT64,
  253. // ...
  254. // ) PRIMARY KEY(Key DESC);
  255. //
  256. // The following range retrieves all rows with key values between 1 and 100
  257. // inclusive:
  258. //
  259. // spanner.KeyRange{
  260. // Start: spanner.Key{100},
  261. // End: spanner.Key{1},
  262. // Kind: ClosedClosed,
  263. // }
  264. //
  265. // Note that 100 is passed as the start, and 1 is passed as the end, because
  266. // Key is a descending column in the schema.
  267. type KeyRange struct {
  268. // Start specifies the left boundary of the key range; End specifies
  269. // the right boundary of the key range.
  270. Start, End Key
  271. // Kind describes whether the boundaries of the key range include
  272. // their keys.
  273. Kind KeyRangeKind
  274. }
  275. // String implements fmt.Stringer for KeyRange type.
  276. func (r KeyRange) String() string {
  277. var left, right string
  278. switch r.Kind {
  279. case ClosedClosed:
  280. left, right = "[", "]"
  281. case ClosedOpen:
  282. left, right = "[", ")"
  283. case OpenClosed:
  284. left, right = "(", "]"
  285. case OpenOpen:
  286. left, right = "(", ")"
  287. default:
  288. left, right = "?", "?"
  289. }
  290. return fmt.Sprintf("%s%s,%s%s", left, r.Start, r.End, right)
  291. }
  292. // proto converts KeyRange into sppb.KeyRange.
  293. func (r KeyRange) proto() (*sppb.KeyRange, error) {
  294. var err error
  295. var start, end *proto3.ListValue
  296. pb := &sppb.KeyRange{}
  297. if start, err = r.Start.proto(); err != nil {
  298. return nil, err
  299. }
  300. if end, err = r.End.proto(); err != nil {
  301. return nil, err
  302. }
  303. if r.Kind == ClosedClosed || r.Kind == ClosedOpen {
  304. pb.StartKeyType = &sppb.KeyRange_StartClosed{StartClosed: start}
  305. } else {
  306. pb.StartKeyType = &sppb.KeyRange_StartOpen{StartOpen: start}
  307. }
  308. if r.Kind == ClosedClosed || r.Kind == OpenClosed {
  309. pb.EndKeyType = &sppb.KeyRange_EndClosed{EndClosed: end}
  310. } else {
  311. pb.EndKeyType = &sppb.KeyRange_EndOpen{EndOpen: end}
  312. }
  313. return pb, nil
  314. }
  315. // keySetProto lets a KeyRange act as a KeySet.
  316. func (r KeyRange) keySetProto() (*sppb.KeySet, error) {
  317. rp, err := r.proto()
  318. if err != nil {
  319. return nil, err
  320. }
  321. return &sppb.KeySet{Ranges: []*sppb.KeyRange{rp}}, nil
  322. }
  323. // A KeySet defines a collection of Cloud Spanner keys and/or key ranges. All the
  324. // keys are expected to be in the same table or index. The keys need not be sorted in
  325. // any particular way.
  326. //
  327. // An individual Key can act as a KeySet, as can a KeyRange. Use the KeySets function
  328. // to create a KeySet consisting of multiple Keys and KeyRanges. To obtain an empty
  329. // KeySet, call KeySets with no arguments.
  330. //
  331. // If the same key is specified multiple times in the set (for example if two
  332. // ranges, two keys, or a key and a range overlap), the Cloud Spanner backend behaves
  333. // as if the key were only specified once.
  334. type KeySet interface {
  335. keySetProto() (*sppb.KeySet, error)
  336. }
  337. // AllKeys returns a KeySet that represents all Keys of a table or a index.
  338. func AllKeys() KeySet {
  339. return all{}
  340. }
  341. type all struct{}
  342. func (all) keySetProto() (*sppb.KeySet, error) {
  343. return &sppb.KeySet{All: true}, nil
  344. }
  345. // KeySets returns the union of the KeySets. If any of the KeySets is AllKeys, then
  346. // the resulting KeySet will be equivalent to AllKeys.
  347. func KeySets(keySets ...KeySet) KeySet {
  348. u := make(union, len(keySets))
  349. copy(u, keySets)
  350. return u
  351. }
  352. type union []KeySet
  353. func (u union) keySetProto() (*sppb.KeySet, error) {
  354. upb := &sppb.KeySet{}
  355. for _, ks := range u {
  356. pb, err := ks.keySetProto()
  357. if err != nil {
  358. return nil, err
  359. }
  360. if pb.All {
  361. return pb, nil
  362. }
  363. upb.Keys = append(upb.Keys, pb.Keys...)
  364. upb.Ranges = append(upb.Ranges, pb.Ranges...)
  365. }
  366. return upb, nil
  367. }