session.go 26 KB

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