session.go 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068
  1. // Copyright (c) 2012 The gocql Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package gocql
  5. import (
  6. "bytes"
  7. "encoding/binary"
  8. "errors"
  9. "fmt"
  10. "io"
  11. "strings"
  12. "sync"
  13. "time"
  14. "unicode"
  15. "github.com/golang/groupcache/lru"
  16. )
  17. // Session is the interface used by users to interact with the database.
  18. //
  19. // It's safe for concurrent use by multiple goroutines and a typical usage
  20. // scenario is to have one global session object to interact with the
  21. // whole Cassandra cluster.
  22. //
  23. // This type extends the Node interface by adding a convinient query builder
  24. // and automatically sets a default consinstency level on all operations
  25. // that do not have a consistency level set.
  26. type Session struct {
  27. Pool ConnectionPool
  28. cons Consistency
  29. pageSize int
  30. prefetch float64
  31. routingKeyInfoCache routingKeyInfoLRU
  32. schemaDescriber *schemaDescriber
  33. trace Tracer
  34. hostSource *ringDescriber
  35. mu sync.RWMutex
  36. cfg ClusterConfig
  37. closeMu sync.RWMutex
  38. isClosed bool
  39. }
  40. // NewSession wraps an existing Node.
  41. func NewSession(cfg ClusterConfig) (*Session, error) {
  42. //Check that hosts in the ClusterConfig is not empty
  43. if len(cfg.Hosts) < 1 {
  44. return nil, ErrNoHosts
  45. }
  46. maxStreams := 128
  47. if cfg.ProtoVersion > protoVersion2 {
  48. maxStreams = 32768
  49. }
  50. if cfg.NumStreams <= 0 || cfg.NumStreams > maxStreams {
  51. cfg.NumStreams = maxStreams
  52. }
  53. pool, err := cfg.ConnPoolType(&cfg)
  54. if err != nil {
  55. return nil, err
  56. }
  57. //Adjust the size of the prepared statements cache to match the latest configuration
  58. stmtsLRU.Lock()
  59. initStmtsLRU(cfg.MaxPreparedStmts)
  60. stmtsLRU.Unlock()
  61. s := &Session{
  62. Pool: pool,
  63. cons: cfg.Consistency,
  64. prefetch: 0.25,
  65. cfg: cfg,
  66. }
  67. //See if there are any connections in the pool
  68. if pool.Size() > 0 {
  69. s.routingKeyInfoCache.lru = lru.New(cfg.MaxRoutingKeyInfo)
  70. s.SetConsistency(cfg.Consistency)
  71. s.SetPageSize(cfg.PageSize)
  72. if cfg.DiscoverHosts {
  73. s.hostSource = &ringDescriber{
  74. session: s,
  75. dcFilter: cfg.Discovery.DcFilter,
  76. rackFilter: cfg.Discovery.RackFilter,
  77. closeChan: make(chan bool),
  78. }
  79. go s.hostSource.run(cfg.Discovery.Sleep)
  80. }
  81. return s, nil
  82. }
  83. s.Close()
  84. return nil, ErrNoConnectionsStarted
  85. }
  86. // SetConsistency sets the default consistency level for this session. This
  87. // setting can also be changed on a per-query basis and the default value
  88. // is Quorum.
  89. func (s *Session) SetConsistency(cons Consistency) {
  90. s.mu.Lock()
  91. s.cons = cons
  92. s.mu.Unlock()
  93. }
  94. // SetPageSize sets the default page size for this session. A value <= 0 will
  95. // disable paging. This setting can also be changed on a per-query basis.
  96. func (s *Session) SetPageSize(n int) {
  97. s.mu.Lock()
  98. s.pageSize = n
  99. s.mu.Unlock()
  100. }
  101. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  102. // there are only p*pageSize rows remaining, the next page will be requested
  103. // automatically. This value can also be changed on a per-query basis and
  104. // the default value is 0.25.
  105. func (s *Session) SetPrefetch(p float64) {
  106. s.mu.Lock()
  107. s.prefetch = p
  108. s.mu.Unlock()
  109. }
  110. // SetTrace sets the default tracer for this session. This setting can also
  111. // be changed on a per-query basis.
  112. func (s *Session) SetTrace(trace Tracer) {
  113. s.mu.Lock()
  114. s.trace = trace
  115. s.mu.Unlock()
  116. }
  117. // Query generates a new query object for interacting with the database.
  118. // Further details of the query may be tweaked using the resulting query
  119. // value before the query is executed. Query is automatically prepared
  120. // if it has not previously been executed.
  121. func (s *Session) Query(stmt string, values ...interface{}) *Query {
  122. s.mu.RLock()
  123. qry := &Query{stmt: stmt, values: values, cons: s.cons,
  124. session: s, pageSize: s.pageSize, trace: s.trace,
  125. prefetch: s.prefetch, rt: s.cfg.RetryPolicy, serialCons: s.cfg.SerialConsistency,
  126. defaultTimestamp: s.cfg.DefaultTimestamp,
  127. }
  128. s.mu.RUnlock()
  129. return qry
  130. }
  131. type QueryInfo struct {
  132. Id []byte
  133. Args []ColumnInfo
  134. Rval []ColumnInfo
  135. }
  136. // Bind generates a new query object based on the query statement passed in.
  137. // The query is automatically prepared if it has not previously been executed.
  138. // The binding callback allows the application to define which query argument
  139. // values will be marshalled as part of the query execution.
  140. // During execution, the meta data of the prepared query will be routed to the
  141. // binding callback, which is responsible for producing the query argument values.
  142. func (s *Session) Bind(stmt string, b func(q *QueryInfo) ([]interface{}, error)) *Query {
  143. s.mu.RLock()
  144. qry := &Query{stmt: stmt, binding: b, cons: s.cons,
  145. session: s, pageSize: s.pageSize, trace: s.trace,
  146. prefetch: s.prefetch, rt: s.cfg.RetryPolicy}
  147. s.mu.RUnlock()
  148. return qry
  149. }
  150. // Close closes all connections. The session is unusable after this
  151. // operation.
  152. func (s *Session) Close() {
  153. s.closeMu.Lock()
  154. defer s.closeMu.Unlock()
  155. if s.isClosed {
  156. return
  157. }
  158. s.isClosed = true
  159. s.Pool.Close()
  160. if s.hostSource != nil {
  161. close(s.hostSource.closeChan)
  162. }
  163. }
  164. func (s *Session) Closed() bool {
  165. s.closeMu.RLock()
  166. closed := s.isClosed
  167. s.closeMu.RUnlock()
  168. return closed
  169. }
  170. func (s *Session) executeQuery(qry *Query) *Iter {
  171. // fail fast
  172. if s.Closed() {
  173. return &Iter{err: ErrSessionClosed}
  174. }
  175. var iter *Iter
  176. qry.attempts = 0
  177. qry.totalLatency = 0
  178. for {
  179. conn := s.Pool.Pick(qry)
  180. //Assign the error unavailable to the iterator
  181. if conn == nil {
  182. iter = &Iter{err: ErrNoConnections}
  183. break
  184. }
  185. t := time.Now()
  186. iter = conn.executeQuery(qry)
  187. qry.totalLatency += time.Now().Sub(t).Nanoseconds()
  188. qry.attempts++
  189. //Exit for loop if the query was successful
  190. if iter.err == nil {
  191. break
  192. }
  193. if qry.rt == nil || !qry.rt.Attempt(qry) {
  194. break
  195. }
  196. }
  197. return iter
  198. }
  199. // KeyspaceMetadata returns the schema metadata for the keyspace specified.
  200. func (s *Session) KeyspaceMetadata(keyspace string) (*KeyspaceMetadata, error) {
  201. // fail fast
  202. if s.Closed() {
  203. return nil, ErrSessionClosed
  204. }
  205. if keyspace == "" {
  206. return nil, ErrNoKeyspace
  207. }
  208. s.mu.Lock()
  209. // lazy-init schemaDescriber
  210. if s.schemaDescriber == nil {
  211. s.schemaDescriber = newSchemaDescriber(s)
  212. }
  213. s.mu.Unlock()
  214. return s.schemaDescriber.getSchema(keyspace)
  215. }
  216. // returns routing key indexes and type info
  217. func (s *Session) routingKeyInfo(stmt string) (*routingKeyInfo, error) {
  218. s.routingKeyInfoCache.mu.Lock()
  219. cacheKey := s.cfg.Keyspace + stmt
  220. entry, cached := s.routingKeyInfoCache.lru.Get(cacheKey)
  221. if cached {
  222. // done accessing the cache
  223. s.routingKeyInfoCache.mu.Unlock()
  224. // the entry is an inflight struct similiar to that used by
  225. // Conn to prepare statements
  226. inflight := entry.(*inflightCachedEntry)
  227. // wait for any inflight work
  228. inflight.wg.Wait()
  229. if inflight.err != nil {
  230. return nil, inflight.err
  231. }
  232. key, _ := inflight.value.(*routingKeyInfo)
  233. return key, nil
  234. }
  235. // create a new inflight entry while the data is created
  236. inflight := new(inflightCachedEntry)
  237. inflight.wg.Add(1)
  238. defer inflight.wg.Done()
  239. s.routingKeyInfoCache.lru.Add(cacheKey, inflight)
  240. s.routingKeyInfoCache.mu.Unlock()
  241. var (
  242. prepared *resultPreparedFrame
  243. partitionKey []*ColumnMetadata
  244. )
  245. // get the query info for the statement
  246. conn := s.Pool.Pick(nil)
  247. if conn == nil {
  248. // no connections
  249. inflight.err = ErrNoConnections
  250. // don't cache this error
  251. s.routingKeyInfoCache.Remove(cacheKey)
  252. return nil, inflight.err
  253. }
  254. prepared, inflight.err = conn.prepareStatement(stmt, nil)
  255. if inflight.err != nil {
  256. // don't cache this error
  257. s.routingKeyInfoCache.Remove(cacheKey)
  258. return nil, inflight.err
  259. }
  260. if len(prepared.reqMeta.columns) == 0 {
  261. // no arguments, no routing key, and no error
  262. return nil, nil
  263. }
  264. // get the table metadata
  265. table := prepared.reqMeta.columns[0].Table
  266. var keyspaceMetadata *KeyspaceMetadata
  267. keyspaceMetadata, inflight.err = s.KeyspaceMetadata(s.cfg.Keyspace)
  268. if inflight.err != nil {
  269. // don't cache this error
  270. s.routingKeyInfoCache.Remove(cacheKey)
  271. return nil, inflight.err
  272. }
  273. tableMetadata, found := keyspaceMetadata.Tables[table]
  274. if !found {
  275. // unlikely that the statement could be prepared and the metadata for
  276. // the table couldn't be found, but this may indicate either a bug
  277. // in the metadata code, or that the table was just dropped.
  278. inflight.err = ErrNoMetadata
  279. // don't cache this error
  280. s.routingKeyInfoCache.Remove(cacheKey)
  281. return nil, inflight.err
  282. }
  283. partitionKey = tableMetadata.PartitionKey
  284. size := len(partitionKey)
  285. routingKeyInfo := &routingKeyInfo{
  286. indexes: make([]int, size),
  287. types: make([]TypeInfo, size),
  288. }
  289. for keyIndex, keyColumn := range partitionKey {
  290. // set an indicator for checking if the mapping is missing
  291. routingKeyInfo.indexes[keyIndex] = -1
  292. // find the column in the query info
  293. for argIndex, boundColumn := range prepared.reqMeta.columns {
  294. if keyColumn.Name == boundColumn.Name {
  295. // there may be many such bound columns, pick the first
  296. routingKeyInfo.indexes[keyIndex] = argIndex
  297. routingKeyInfo.types[keyIndex] = boundColumn.TypeInfo
  298. break
  299. }
  300. }
  301. if routingKeyInfo.indexes[keyIndex] == -1 {
  302. // missing a routing key column mapping
  303. // no routing key, and no error
  304. return nil, nil
  305. }
  306. }
  307. // cache this result
  308. inflight.value = routingKeyInfo
  309. return routingKeyInfo, nil
  310. }
  311. func (s *Session) executeBatch(batch *Batch) (*Iter, error) {
  312. // fail fast
  313. if s.Closed() {
  314. return nil, ErrSessionClosed
  315. }
  316. // Prevent the execution of the batch if greater than the limit
  317. // Currently batches have a limit of 65536 queries.
  318. // https://datastax-oss.atlassian.net/browse/JAVA-229
  319. if batch.Size() > BatchSizeMaximum {
  320. return nil, ErrTooManyStmts
  321. }
  322. var err error
  323. var iter *Iter
  324. batch.attempts = 0
  325. batch.totalLatency = 0
  326. for {
  327. conn := s.Pool.Pick(nil)
  328. //Assign the error unavailable and break loop
  329. if conn == nil {
  330. err = ErrNoConnections
  331. break
  332. }
  333. t := time.Now()
  334. iter, err = conn.executeBatch(batch)
  335. batch.totalLatency += time.Now().Sub(t).Nanoseconds()
  336. batch.attempts++
  337. //Exit loop if operation executed correctly
  338. if err == nil {
  339. return iter, err
  340. }
  341. if batch.rt == nil || !batch.rt.Attempt(batch) {
  342. break
  343. }
  344. }
  345. return nil, err
  346. }
  347. // ExecuteBatch executes a batch operation and returns nil if successful
  348. // otherwise an error is returned describing the failure.
  349. func (s *Session) ExecuteBatch(batch *Batch) error {
  350. _, err := s.executeBatch(batch)
  351. return err
  352. }
  353. // ExecuteBatchCAS executes a batch operation and returns nil if successful and
  354. // an iterator (to scan aditional rows if more than one conditional statement)
  355. // was sent, otherwise an error is returned describing the failure.
  356. // Further scans on the interator must also remember to include
  357. // the applied boolean as the first argument to *Iter.Scan
  358. func (s *Session) ExecuteBatchCAS(batch *Batch, dest ...interface{}) (applied bool, iter *Iter, err error) {
  359. if iter, err := s.executeBatch(batch); err == nil {
  360. if err := iter.checkErrAndNotFound(); err != nil {
  361. return false, nil, err
  362. }
  363. if len(iter.Columns()) > 1 {
  364. dest = append([]interface{}{&applied}, dest...)
  365. iter.Scan(dest...)
  366. } else {
  367. iter.Scan(&applied)
  368. }
  369. return applied, iter, nil
  370. } else {
  371. return false, nil, err
  372. }
  373. }
  374. // MapExecuteBatchCAS executes a batch operation much like ExecuteBatchCAS,
  375. // however it accepts a map rather than a list of arguments for the initial
  376. // scan.
  377. func (s *Session) MapExecuteBatchCAS(batch *Batch, dest map[string]interface{}) (applied bool, iter *Iter, err error) {
  378. if iter, err := s.executeBatch(batch); err == nil {
  379. if err := iter.checkErrAndNotFound(); err != nil {
  380. return false, nil, err
  381. }
  382. iter.MapScan(dest)
  383. applied = dest["[applied]"].(bool)
  384. delete(dest, "[applied]")
  385. // we usually close here, but instead of closing, just returin an error
  386. // if MapScan failed. Although Close just returns err, using Close
  387. // here might be confusing as we are not actually closing the iter
  388. return applied, iter, iter.err
  389. } else {
  390. return false, nil, err
  391. }
  392. }
  393. // Query represents a CQL statement that can be executed.
  394. type Query struct {
  395. stmt string
  396. values []interface{}
  397. cons Consistency
  398. pageSize int
  399. routingKey []byte
  400. pageState []byte
  401. prefetch float64
  402. trace Tracer
  403. session *Session
  404. rt RetryPolicy
  405. binding func(q *QueryInfo) ([]interface{}, error)
  406. attempts int
  407. totalLatency int64
  408. serialCons SerialConsistency
  409. defaultTimestamp bool
  410. }
  411. //Attempts returns the number of times the query was executed.
  412. func (q *Query) Attempts() int {
  413. return q.attempts
  414. }
  415. //Latency returns the average amount of nanoseconds per attempt of the query.
  416. func (q *Query) Latency() int64 {
  417. if q.attempts > 0 {
  418. return q.totalLatency / int64(q.attempts)
  419. }
  420. return 0
  421. }
  422. // Consistency sets the consistency level for this query. If no consistency
  423. // level have been set, the default consistency level of the cluster
  424. // is used.
  425. func (q *Query) Consistency(c Consistency) *Query {
  426. q.cons = c
  427. return q
  428. }
  429. // GetConsistency returns the currently configured consistency level for
  430. // the query.
  431. func (q *Query) GetConsistency() Consistency {
  432. return q.cons
  433. }
  434. // Trace enables tracing of this query. Look at the documentation of the
  435. // Tracer interface to learn more about tracing.
  436. func (q *Query) Trace(trace Tracer) *Query {
  437. q.trace = trace
  438. return q
  439. }
  440. // PageSize will tell the iterator to fetch the result in pages of size n.
  441. // This is useful for iterating over large result sets, but setting the
  442. // page size to low might decrease the performance. This feature is only
  443. // available in Cassandra 2 and onwards.
  444. func (q *Query) PageSize(n int) *Query {
  445. q.pageSize = n
  446. return q
  447. }
  448. // DefaultTimestamp will enable the with default timestamp flag on the query.
  449. // If enable, this will replace the server side assigned
  450. // timestamp as default timestamp. Note that a timestamp in the query itself
  451. // will still override this timestamp. This is entirely optional.
  452. //
  453. // Only available on protocol >= 3
  454. func (q *Query) DefaultTimestamp(enable bool) *Query {
  455. q.defaultTimestamp = enable
  456. return q
  457. }
  458. // RoutingKey sets the routing key to use when a token aware connection
  459. // pool is used to optimize the routing of this query.
  460. func (q *Query) RoutingKey(routingKey []byte) *Query {
  461. q.routingKey = routingKey
  462. return q
  463. }
  464. // GetRoutingKey gets the routing key to use for routing this query. If
  465. // a routing key has not been explicitly set, then the routing key will
  466. // be constructed if possible using the keyspace's schema and the query
  467. // info for this query statement. If the routing key cannot be determined
  468. // then nil will be returned with no error. On any error condition,
  469. // an error description will be returned.
  470. func (q *Query) GetRoutingKey() ([]byte, error) {
  471. if q.routingKey != nil {
  472. return q.routingKey, nil
  473. }
  474. // try to determine the routing key
  475. routingKeyInfo, err := q.session.routingKeyInfo(q.stmt)
  476. if err != nil {
  477. return nil, err
  478. }
  479. if routingKeyInfo == nil {
  480. return nil, nil
  481. }
  482. if len(routingKeyInfo.indexes) == 1 {
  483. // single column routing key
  484. routingKey, err := Marshal(
  485. routingKeyInfo.types[0],
  486. q.values[routingKeyInfo.indexes[0]],
  487. )
  488. if err != nil {
  489. return nil, err
  490. }
  491. return routingKey, nil
  492. }
  493. // composite routing key
  494. buf := &bytes.Buffer{}
  495. for i := range routingKeyInfo.indexes {
  496. encoded, err := Marshal(
  497. routingKeyInfo.types[i],
  498. q.values[routingKeyInfo.indexes[i]],
  499. )
  500. if err != nil {
  501. return nil, err
  502. }
  503. binary.Write(buf, binary.BigEndian, int16(len(encoded)))
  504. buf.Write(encoded)
  505. buf.WriteByte(0x00)
  506. }
  507. routingKey := buf.Bytes()
  508. return routingKey, nil
  509. }
  510. func (q *Query) shouldPrepare() bool {
  511. stmt := strings.TrimLeftFunc(strings.TrimRightFunc(q.stmt, func(r rune) bool {
  512. return unicode.IsSpace(r) || r == ';'
  513. }), unicode.IsSpace)
  514. var stmtType string
  515. if n := strings.IndexFunc(stmt, unicode.IsSpace); n >= 0 {
  516. stmtType = strings.ToLower(stmt[:n])
  517. }
  518. if stmtType == "begin" {
  519. if n := strings.LastIndexFunc(stmt, unicode.IsSpace); n >= 0 {
  520. stmtType = strings.ToLower(stmt[n+1:])
  521. }
  522. }
  523. switch stmtType {
  524. case "select", "insert", "update", "delete", "batch":
  525. return true
  526. }
  527. return false
  528. }
  529. // SetPrefetch sets the default threshold for pre-fetching new pages. If
  530. // there are only p*pageSize rows remaining, the next page will be requested
  531. // automatically.
  532. func (q *Query) Prefetch(p float64) *Query {
  533. q.prefetch = p
  534. return q
  535. }
  536. // RetryPolicy sets the policy to use when retrying the query.
  537. func (q *Query) RetryPolicy(r RetryPolicy) *Query {
  538. q.rt = r
  539. return q
  540. }
  541. // Bind sets query arguments of query. This can also be used to rebind new query arguments
  542. // to an existing query instance.
  543. func (q *Query) Bind(v ...interface{}) *Query {
  544. q.values = v
  545. return q
  546. }
  547. // SerialConsistency sets the consistencyc level for the
  548. // serial phase of conditional updates. That consitency can only be
  549. // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
  550. // SERIAL. This option will be ignored for anything else that a
  551. // conditional update/insert.
  552. func (q *Query) SerialConsistency(cons SerialConsistency) *Query {
  553. q.serialCons = cons
  554. return q
  555. }
  556. // Exec executes the query without returning any rows.
  557. func (q *Query) Exec() error {
  558. iter := q.Iter()
  559. return iter.err
  560. }
  561. // Iter executes the query and returns an iterator capable of iterating
  562. // over all results.
  563. func (q *Query) Iter() *Iter {
  564. if strings.Index(strings.ToLower(q.stmt), "use") == 0 {
  565. return &Iter{err: ErrUseStmt}
  566. }
  567. return q.session.executeQuery(q)
  568. }
  569. // MapScan executes the query, copies the columns of the first selected
  570. // row into the map pointed at by m and discards the rest. If no rows
  571. // were selected, ErrNotFound is returned.
  572. func (q *Query) MapScan(m map[string]interface{}) error {
  573. iter := q.Iter()
  574. if err := iter.checkErrAndNotFound(); err != nil {
  575. return err
  576. }
  577. iter.MapScan(m)
  578. return iter.Close()
  579. }
  580. // Scan executes the query, copies the columns of the first selected
  581. // row into the values pointed at by dest and discards the rest. If no rows
  582. // were selected, ErrNotFound is returned.
  583. func (q *Query) Scan(dest ...interface{}) error {
  584. iter := q.Iter()
  585. if err := iter.checkErrAndNotFound(); err != nil {
  586. return err
  587. }
  588. iter.Scan(dest...)
  589. return iter.Close()
  590. }
  591. // ScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  592. // statement containing an IF clause). If the transaction fails because
  593. // the existing values did not match, the previous values will be stored
  594. // in dest.
  595. func (q *Query) ScanCAS(dest ...interface{}) (applied bool, err error) {
  596. iter := q.Iter()
  597. if err := iter.checkErrAndNotFound(); err != nil {
  598. return false, err
  599. }
  600. if len(iter.Columns()) > 1 {
  601. dest = append([]interface{}{&applied}, dest...)
  602. iter.Scan(dest...)
  603. } else {
  604. iter.Scan(&applied)
  605. }
  606. return applied, iter.Close()
  607. }
  608. // MapScanCAS executes a lightweight transaction (i.e. an UPDATE or INSERT
  609. // statement containing an IF clause). If the transaction fails because
  610. // the existing values did not match, the previous values will be stored
  611. // in dest map.
  612. //
  613. // As for INSERT .. IF NOT EXISTS, previous values will be returned as if
  614. // SELECT * FROM. So using ScanCAS with INSERT is inherently prone to
  615. // column mismatching. MapScanCAS is added to capture them safely.
  616. func (q *Query) MapScanCAS(dest map[string]interface{}) (applied bool, err error) {
  617. iter := q.Iter()
  618. if err := iter.checkErrAndNotFound(); err != nil {
  619. return false, err
  620. }
  621. iter.MapScan(dest)
  622. applied = dest["[applied]"].(bool)
  623. delete(dest, "[applied]")
  624. return applied, iter.Close()
  625. }
  626. // Iter represents an iterator that can be used to iterate over all rows that
  627. // were returned by a query. The iterator might send additional queries to the
  628. // database during the iteration if paging was enabled.
  629. type Iter struct {
  630. err error
  631. pos int
  632. rows [][][]byte
  633. meta resultMetadata
  634. next *nextIter
  635. }
  636. // Columns returns the name and type of the selected columns.
  637. func (iter *Iter) Columns() []ColumnInfo {
  638. return iter.meta.columns
  639. }
  640. // Scan consumes the next row of the iterator and copies the columns of the
  641. // current row into the values pointed at by dest. Use nil as a dest value
  642. // to skip the corresponding column. Scan might send additional queries
  643. // to the database to retrieve the next set of rows if paging was enabled.
  644. //
  645. // Scan returns true if the row was successfully unmarshaled or false if the
  646. // end of the result set was reached or if an error occurred. Close should
  647. // be called afterwards to retrieve any potential errors.
  648. func (iter *Iter) Scan(dest ...interface{}) bool {
  649. if iter.err != nil {
  650. return false
  651. }
  652. if iter.pos >= len(iter.rows) {
  653. if iter.next != nil {
  654. *iter = *iter.next.fetch()
  655. return iter.Scan(dest...)
  656. }
  657. return false
  658. }
  659. if iter.next != nil && iter.pos == iter.next.pos {
  660. go iter.next.fetch()
  661. }
  662. // currently only support scanning into an expand tuple, such that its the same
  663. // as scanning in more values from a single column
  664. if len(dest) != iter.meta.actualColCount {
  665. iter.err = errors.New("count mismatch")
  666. return false
  667. }
  668. // i is the current position in dest, could posible replace it and just use
  669. // slices of dest
  670. i := 0
  671. for c, col := range iter.meta.columns {
  672. if dest[i] == nil {
  673. i++
  674. continue
  675. }
  676. switch col.TypeInfo.Type() {
  677. case TypeTuple:
  678. // this will panic, actually a bug, please report
  679. tuple := col.TypeInfo.(TupleTypeInfo)
  680. count := len(tuple.Elems)
  681. // here we pass in a slice of the struct which has the number number of
  682. // values as elements in the tuple
  683. iter.err = Unmarshal(col.TypeInfo, iter.rows[iter.pos][c], dest[i:i+count])
  684. i += count
  685. default:
  686. iter.err = Unmarshal(col.TypeInfo, iter.rows[iter.pos][c], dest[i])
  687. i++
  688. }
  689. if iter.err != nil {
  690. return false
  691. }
  692. }
  693. iter.pos++
  694. return true
  695. }
  696. // Close closes the iterator and returns any errors that happened during
  697. // the query or the iteration.
  698. func (iter *Iter) Close() error {
  699. return iter.err
  700. }
  701. // checkErrAndNotFound handle error and NotFound in one method.
  702. func (iter *Iter) checkErrAndNotFound() error {
  703. if iter.err != nil {
  704. return iter.err
  705. } else if len(iter.rows) == 0 {
  706. return ErrNotFound
  707. }
  708. return nil
  709. }
  710. type nextIter struct {
  711. qry Query
  712. pos int
  713. once sync.Once
  714. next *Iter
  715. }
  716. func (n *nextIter) fetch() *Iter {
  717. n.once.Do(func() {
  718. n.next = n.qry.session.executeQuery(&n.qry)
  719. })
  720. return n.next
  721. }
  722. type Batch struct {
  723. Type BatchType
  724. Entries []BatchEntry
  725. Cons Consistency
  726. rt RetryPolicy
  727. attempts int
  728. totalLatency int64
  729. serialCons SerialConsistency
  730. defaultTimestamp bool
  731. }
  732. // NewBatch creates a new batch operation without defaults from the cluster
  733. func NewBatch(typ BatchType) *Batch {
  734. return &Batch{Type: typ}
  735. }
  736. // NewBatch creates a new batch operation using defaults defined in the cluster
  737. func (s *Session) NewBatch(typ BatchType) *Batch {
  738. s.mu.RLock()
  739. batch := &Batch{Type: typ, rt: s.cfg.RetryPolicy, serialCons: s.cfg.SerialConsistency,
  740. Cons: s.cons, defaultTimestamp: s.cfg.DefaultTimestamp}
  741. s.mu.RUnlock()
  742. return batch
  743. }
  744. // Attempts returns the number of attempts made to execute the batch.
  745. func (b *Batch) Attempts() int {
  746. return b.attempts
  747. }
  748. //Latency returns the average number of nanoseconds to execute a single attempt of the batch.
  749. func (b *Batch) Latency() int64 {
  750. if b.attempts > 0 {
  751. return b.totalLatency / int64(b.attempts)
  752. }
  753. return 0
  754. }
  755. // GetConsistency returns the currently configured consistency level for the batch
  756. // operation.
  757. func (b *Batch) GetConsistency() Consistency {
  758. return b.Cons
  759. }
  760. // Query adds the query to the batch operation
  761. func (b *Batch) Query(stmt string, args ...interface{}) {
  762. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, Args: args})
  763. }
  764. // Bind adds the query to the batch operation and correlates it with a binding callback
  765. // that will be invoked when the batch is executed. The binding callback allows the application
  766. // to define which query argument values will be marshalled as part of the batch execution.
  767. func (b *Batch) Bind(stmt string, bind func(q *QueryInfo) ([]interface{}, error)) {
  768. b.Entries = append(b.Entries, BatchEntry{Stmt: stmt, binding: bind})
  769. }
  770. // RetryPolicy sets the retry policy to use when executing the batch operation
  771. func (b *Batch) RetryPolicy(r RetryPolicy) *Batch {
  772. b.rt = r
  773. return b
  774. }
  775. // Size returns the number of batch statements to be executed by the batch operation.
  776. func (b *Batch) Size() int {
  777. return len(b.Entries)
  778. }
  779. // SerialConsistency sets the consistencyc level for the
  780. // serial phase of conditional updates. That consitency can only be
  781. // either SERIAL or LOCAL_SERIAL and if not present, it defaults to
  782. // SERIAL. This option will be ignored for anything else that a
  783. // conditional update/insert.
  784. //
  785. // Only available for protocol 3 and above
  786. func (b *Batch) SerialConsistency(cons SerialConsistency) *Batch {
  787. b.serialCons = cons
  788. return b
  789. }
  790. // DefaultTimestamp will enable the with default timestamp flag on the query.
  791. // If enable, this will replace the server side assigned
  792. // timestamp as default timestamp. Note that a timestamp in the query itself
  793. // will still override this timestamp. This is entirely optional.
  794. //
  795. // Only available on protocol >= 3
  796. func (b *Batch) DefaultTimestamp(enable bool) *Batch {
  797. b.defaultTimestamp = enable
  798. return b
  799. }
  800. type BatchType byte
  801. const (
  802. LoggedBatch BatchType = 0
  803. UnloggedBatch = 1
  804. CounterBatch = 2
  805. )
  806. type BatchEntry struct {
  807. Stmt string
  808. Args []interface{}
  809. binding func(q *QueryInfo) ([]interface{}, error)
  810. }
  811. type ColumnInfo struct {
  812. Keyspace string
  813. Table string
  814. Name string
  815. TypeInfo TypeInfo
  816. }
  817. func (c ColumnInfo) String() string {
  818. return fmt.Sprintf("[column keyspace=%s table=%s name=%s type=%v]", c.Keyspace, c.Table, c.Name, c.TypeInfo)
  819. }
  820. // routing key indexes LRU cache
  821. type routingKeyInfoLRU struct {
  822. lru *lru.Cache
  823. mu sync.Mutex
  824. }
  825. type routingKeyInfo struct {
  826. indexes []int
  827. types []TypeInfo
  828. }
  829. func (r *routingKeyInfoLRU) Remove(key string) {
  830. r.mu.Lock()
  831. r.lru.Remove(key)
  832. r.mu.Unlock()
  833. }
  834. //Max adjusts the maximum size of the cache and cleans up the oldest records if
  835. //the new max is lower than the previous value. Not concurrency safe.
  836. func (r *routingKeyInfoLRU) Max(max int) {
  837. r.mu.Lock()
  838. for r.lru.Len() > max {
  839. r.lru.RemoveOldest()
  840. }
  841. r.lru.MaxEntries = max
  842. r.mu.Unlock()
  843. }
  844. type inflightCachedEntry struct {
  845. wg sync.WaitGroup
  846. err error
  847. value interface{}
  848. }
  849. // Tracer is the interface implemented by query tracers. Tracers have the
  850. // ability to obtain a detailed event log of all events that happened during
  851. // the execution of a query from Cassandra. Gathering this information might
  852. // be essential for debugging and optimizing queries, but this feature should
  853. // not be used on production systems with very high load.
  854. type Tracer interface {
  855. Trace(traceId []byte)
  856. }
  857. type traceWriter struct {
  858. session *Session
  859. w io.Writer
  860. mu sync.Mutex
  861. }
  862. // NewTraceWriter returns a simple Tracer implementation that outputs
  863. // the event log in a textual format.
  864. func NewTraceWriter(session *Session, w io.Writer) Tracer {
  865. return &traceWriter{session: session, w: w}
  866. }
  867. func (t *traceWriter) Trace(traceId []byte) {
  868. var (
  869. coordinator string
  870. duration int
  871. )
  872. t.session.Query(`SELECT coordinator, duration
  873. FROM system_traces.sessions
  874. WHERE session_id = ?`, traceId).
  875. Consistency(One).Scan(&coordinator, &duration)
  876. iter := t.session.Query(`SELECT event_id, activity, source, source_elapsed
  877. FROM system_traces.events
  878. WHERE session_id = ?`, traceId).
  879. Consistency(One).Iter()
  880. var (
  881. timestamp time.Time
  882. activity string
  883. source string
  884. elapsed int
  885. )
  886. t.mu.Lock()
  887. defer t.mu.Unlock()
  888. fmt.Fprintf(t.w, "Tracing session %016x (coordinator: %s, duration: %v):\n",
  889. traceId, coordinator, time.Duration(duration)*time.Microsecond)
  890. for iter.Scan(&timestamp, &activity, &source, &elapsed) {
  891. fmt.Fprintf(t.w, "%s: %s (source: %s, elapsed: %d)\n",
  892. timestamp.Format("2006/01/02 15:04:05.999999"), activity, source, elapsed)
  893. }
  894. if err := iter.Close(); err != nil {
  895. fmt.Fprintln(t.w, "Error:", err)
  896. }
  897. }
  898. type Error struct {
  899. Code int
  900. Message string
  901. }
  902. func (e Error) Error() string {
  903. return e.Message
  904. }
  905. var (
  906. ErrNotFound = errors.New("not found")
  907. ErrUnavailable = errors.New("unavailable")
  908. ErrUnsupported = errors.New("feature not supported")
  909. ErrTooManyStmts = errors.New("too many statements")
  910. ErrUseStmt = errors.New("use statements aren't supported. Please see https://github.com/gocql/gocql for explaination.")
  911. ErrSessionClosed = errors.New("session has been closed")
  912. ErrNoConnections = errors.New("no connections available")
  913. ErrNoKeyspace = errors.New("no keyspace provided")
  914. ErrNoMetadata = errors.New("no metadata available")
  915. )
  916. type ErrProtocol struct{ error }
  917. func NewErrProtocol(format string, args ...interface{}) error {
  918. return ErrProtocol{fmt.Errorf(format, args...)}
  919. }
  920. // BatchSizeMaximum is the maximum number of statements a batch operation can have.
  921. // This limit is set by cassandra and could change in the future.
  922. const BatchSizeMaximum = 65535