session.go 25 KB

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