metadata.go 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031
  1. // Copyright (c) 2015 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. "encoding/hex"
  7. "encoding/json"
  8. "fmt"
  9. "log"
  10. "strconv"
  11. "strings"
  12. "sync"
  13. )
  14. // schema metadata for a keyspace
  15. type KeyspaceMetadata struct {
  16. Name string
  17. DurableWrites bool
  18. StrategyClass string
  19. StrategyOptions map[string]interface{}
  20. Tables map[string]*TableMetadata
  21. }
  22. // schema metadata for a table (a.k.a. column family)
  23. type TableMetadata struct {
  24. Keyspace string
  25. Name string
  26. KeyValidator string
  27. Comparator string
  28. DefaultValidator string
  29. KeyAliases []string
  30. ColumnAliases []string
  31. ValueAlias string
  32. PartitionKey []*ColumnMetadata
  33. ClusteringColumns []*ColumnMetadata
  34. Columns map[string]*ColumnMetadata
  35. OrderedColumns []string
  36. }
  37. // schema metadata for a column
  38. type ColumnMetadata struct {
  39. Keyspace string
  40. Table string
  41. Name string
  42. ComponentIndex int
  43. Kind ColumnKind
  44. Validator string
  45. Type TypeInfo
  46. ClusteringOrder string
  47. Order ColumnOrder
  48. Index ColumnIndexMetadata
  49. }
  50. // the ordering of the column with regard to its comparator
  51. type ColumnOrder bool
  52. const (
  53. ASC ColumnOrder = false
  54. DESC = true
  55. )
  56. type ColumnIndexMetadata struct {
  57. Name string
  58. Type string
  59. Options map[string]interface{}
  60. }
  61. type ColumnKind int
  62. const (
  63. ColumnUnkownKind ColumnKind = iota
  64. ColumnPartitionKey
  65. ColumnClusteringKey
  66. ColumnRegular
  67. ColumnCompact
  68. )
  69. func (c ColumnKind) String() string {
  70. switch c {
  71. case ColumnPartitionKey:
  72. return "partition_key"
  73. case ColumnClusteringKey:
  74. return "clustering_key"
  75. case ColumnRegular:
  76. return "regular"
  77. case ColumnCompact:
  78. return "compact"
  79. default:
  80. return fmt.Sprintf("unkown_column_%d", c)
  81. }
  82. }
  83. func (c *ColumnKind) UnmarshalCQL(typ TypeInfo, p []byte) error {
  84. if typ.Type() != TypeVarchar {
  85. return unmarshalErrorf("unable to marshall %s into ColumnKind, expected Varchar", typ)
  86. }
  87. kind, err := columnKindFromSchema(string(p))
  88. if err != nil {
  89. return err
  90. }
  91. *c = kind
  92. return nil
  93. }
  94. func columnKindFromSchema(kind string) (ColumnKind, error) {
  95. switch kind {
  96. case "partition_key":
  97. return ColumnPartitionKey, nil
  98. case "clustering_key", "clustering":
  99. return ColumnClusteringKey, nil
  100. case "regular":
  101. return ColumnRegular, nil
  102. case "compact_value":
  103. return ColumnCompact, nil
  104. default:
  105. return -1, fmt.Errorf("unknown column kind: %q", kind)
  106. }
  107. }
  108. // default alias values
  109. const (
  110. DEFAULT_KEY_ALIAS = "key"
  111. DEFAULT_COLUMN_ALIAS = "column"
  112. DEFAULT_VALUE_ALIAS = "value"
  113. )
  114. // queries the cluster for schema information for a specific keyspace
  115. type schemaDescriber struct {
  116. session *Session
  117. mu sync.Mutex
  118. cache map[string]*KeyspaceMetadata
  119. }
  120. // creates a session bound schema describer which will query and cache
  121. // keyspace metadata
  122. func newSchemaDescriber(session *Session) *schemaDescriber {
  123. return &schemaDescriber{
  124. session: session,
  125. cache: map[string]*KeyspaceMetadata{},
  126. }
  127. }
  128. // returns the cached KeyspaceMetadata held by the describer for the named
  129. // keyspace.
  130. func (s *schemaDescriber) getSchema(keyspaceName string) (*KeyspaceMetadata, error) {
  131. s.mu.Lock()
  132. defer s.mu.Unlock()
  133. metadata, found := s.cache[keyspaceName]
  134. if !found {
  135. // refresh the cache for this keyspace
  136. err := s.refreshSchema(keyspaceName)
  137. if err != nil {
  138. return nil, err
  139. }
  140. metadata = s.cache[keyspaceName]
  141. }
  142. return metadata, nil
  143. }
  144. // clears the already cached keyspace metadata
  145. func (s *schemaDescriber) clearSchema(keyspaceName string) {
  146. s.mu.Lock()
  147. defer s.mu.Unlock()
  148. delete(s.cache, keyspaceName)
  149. }
  150. // forcibly updates the current KeyspaceMetadata held by the schema describer
  151. // for a given named keyspace.
  152. func (s *schemaDescriber) refreshSchema(keyspaceName string) error {
  153. var err error
  154. // query the system keyspace for schema data
  155. // TODO retrieve concurrently
  156. keyspace, err := getKeyspaceMetadata(s.session, keyspaceName)
  157. if err != nil {
  158. return err
  159. }
  160. tables, err := getTableMetadata(s.session, keyspaceName)
  161. if err != nil {
  162. return err
  163. }
  164. columns, err := getColumnMetadata(s.session, keyspaceName)
  165. if err != nil {
  166. return err
  167. }
  168. // organize the schema data
  169. compileMetadata(s.session.cfg.ProtoVersion, keyspace, tables, columns)
  170. // update the cache
  171. s.cache[keyspaceName] = keyspace
  172. return nil
  173. }
  174. // "compiles" derived information about keyspace, table, and column metadata
  175. // for a keyspace from the basic queried metadata objects returned by
  176. // getKeyspaceMetadata, getTableMetadata, and getColumnMetadata respectively;
  177. // Links the metadata objects together and derives the column composition of
  178. // the partition key and clustering key for a table.
  179. func compileMetadata(
  180. protoVersion int,
  181. keyspace *KeyspaceMetadata,
  182. tables []TableMetadata,
  183. columns []ColumnMetadata,
  184. ) {
  185. keyspace.Tables = make(map[string]*TableMetadata)
  186. for i := range tables {
  187. tables[i].Columns = make(map[string]*ColumnMetadata)
  188. keyspace.Tables[tables[i].Name] = &tables[i]
  189. }
  190. // add columns from the schema data
  191. for i := range columns {
  192. // decode the validator for TypeInfo and order
  193. if columns[i].ClusteringOrder != "" { // Cassandra 3.x+
  194. columns[i].Type = NativeType{typ: getCassandraType(columns[i].Validator)}
  195. columns[i].Order = ASC
  196. if columns[i].ClusteringOrder == "desc" {
  197. columns[i].Order = DESC
  198. }
  199. } else {
  200. validatorParsed := parseType(columns[i].Validator)
  201. columns[i].Type = validatorParsed.types[0]
  202. columns[i].Order = ASC
  203. if validatorParsed.reversed[0] {
  204. columns[i].Order = DESC
  205. }
  206. }
  207. table := keyspace.Tables[columns[i].Table]
  208. table.Columns[columns[i].Name] = &columns[i]
  209. table.OrderedColumns = append(table.OrderedColumns, columns[i].Name)
  210. }
  211. if protoVersion == 1 {
  212. compileV1Metadata(tables)
  213. } else {
  214. compileV2Metadata(tables)
  215. }
  216. }
  217. // Compiles derived information from TableMetadata which have had
  218. // ColumnMetadata added already. V1 protocol does not return as much
  219. // column metadata as V2+ (because V1 doesn't support the "type" column in the
  220. // system.schema_columns table) so determining PartitionKey and ClusterColumns
  221. // is more complex.
  222. func compileV1Metadata(tables []TableMetadata) {
  223. for i := range tables {
  224. table := &tables[i]
  225. // decode the key validator
  226. keyValidatorParsed := parseType(table.KeyValidator)
  227. // decode the comparator
  228. comparatorParsed := parseType(table.Comparator)
  229. // the partition key length is the same as the number of types in the
  230. // key validator
  231. table.PartitionKey = make([]*ColumnMetadata, len(keyValidatorParsed.types))
  232. // V1 protocol only returns "regular" columns from
  233. // system.schema_columns (there is no type field for columns)
  234. // so the alias information is used to
  235. // create the partition key and clustering columns
  236. // construct the partition key from the alias
  237. for i := range table.PartitionKey {
  238. var alias string
  239. if len(table.KeyAliases) > i {
  240. alias = table.KeyAliases[i]
  241. } else if i == 0 {
  242. alias = DEFAULT_KEY_ALIAS
  243. } else {
  244. alias = DEFAULT_KEY_ALIAS + strconv.Itoa(i+1)
  245. }
  246. column := &ColumnMetadata{
  247. Keyspace: table.Keyspace,
  248. Table: table.Name,
  249. Name: alias,
  250. Type: keyValidatorParsed.types[i],
  251. Kind: ColumnPartitionKey,
  252. ComponentIndex: i,
  253. }
  254. table.PartitionKey[i] = column
  255. table.Columns[alias] = column
  256. }
  257. // determine the number of clustering columns
  258. size := len(comparatorParsed.types)
  259. if comparatorParsed.isComposite {
  260. if len(comparatorParsed.collections) != 0 ||
  261. (len(table.ColumnAliases) == size-1 &&
  262. comparatorParsed.types[size-1].Type() == TypeVarchar) {
  263. size = size - 1
  264. }
  265. } else {
  266. if !(len(table.ColumnAliases) != 0 || len(table.Columns) == 0) {
  267. size = 0
  268. }
  269. }
  270. table.ClusteringColumns = make([]*ColumnMetadata, size)
  271. for i := range table.ClusteringColumns {
  272. var alias string
  273. if len(table.ColumnAliases) > i {
  274. alias = table.ColumnAliases[i]
  275. } else if i == 0 {
  276. alias = DEFAULT_COLUMN_ALIAS
  277. } else {
  278. alias = DEFAULT_COLUMN_ALIAS + strconv.Itoa(i+1)
  279. }
  280. order := ASC
  281. if comparatorParsed.reversed[i] {
  282. order = DESC
  283. }
  284. column := &ColumnMetadata{
  285. Keyspace: table.Keyspace,
  286. Table: table.Name,
  287. Name: alias,
  288. Type: comparatorParsed.types[i],
  289. Order: order,
  290. Kind: ColumnClusteringKey,
  291. ComponentIndex: i,
  292. }
  293. table.ClusteringColumns[i] = column
  294. table.Columns[alias] = column
  295. }
  296. if size != len(comparatorParsed.types)-1 {
  297. alias := DEFAULT_VALUE_ALIAS
  298. if len(table.ValueAlias) > 0 {
  299. alias = table.ValueAlias
  300. }
  301. // decode the default validator
  302. defaultValidatorParsed := parseType(table.DefaultValidator)
  303. column := &ColumnMetadata{
  304. Keyspace: table.Keyspace,
  305. Table: table.Name,
  306. Name: alias,
  307. Type: defaultValidatorParsed.types[0],
  308. Kind: ColumnRegular,
  309. }
  310. table.Columns[alias] = column
  311. }
  312. }
  313. }
  314. // The simpler compile case for V2+ protocol
  315. func compileV2Metadata(tables []TableMetadata) {
  316. for i := range tables {
  317. table := &tables[i]
  318. clusteringColumnCount := componentColumnCountOfType(table.Columns, ColumnClusteringKey)
  319. table.ClusteringColumns = make([]*ColumnMetadata, clusteringColumnCount)
  320. if table.KeyValidator != "" {
  321. keyValidatorParsed := parseType(table.KeyValidator)
  322. table.PartitionKey = make([]*ColumnMetadata, len(keyValidatorParsed.types))
  323. } else { // Cassandra 3.x+
  324. partitionKeyCount := componentColumnCountOfType(table.Columns, ColumnPartitionKey)
  325. table.PartitionKey = make([]*ColumnMetadata, partitionKeyCount)
  326. }
  327. for _, columnName := range table.OrderedColumns {
  328. column := table.Columns[columnName]
  329. if column.Kind == ColumnPartitionKey {
  330. table.PartitionKey[column.ComponentIndex] = column
  331. } else if column.Kind == ColumnClusteringKey {
  332. table.ClusteringColumns[column.ComponentIndex] = column
  333. }
  334. }
  335. }
  336. }
  337. // returns the count of coluns with the given "kind" value.
  338. func componentColumnCountOfType(columns map[string]*ColumnMetadata, kind ColumnKind) int {
  339. maxComponentIndex := -1
  340. for _, column := range columns {
  341. if column.Kind == kind && column.ComponentIndex > maxComponentIndex {
  342. maxComponentIndex = column.ComponentIndex
  343. }
  344. }
  345. return maxComponentIndex + 1
  346. }
  347. // query only for the keyspace metadata for the specified keyspace from system.schema_keyspace
  348. func getKeyspaceMetadata(session *Session, keyspaceName string) (*KeyspaceMetadata, error) {
  349. keyspace := &KeyspaceMetadata{Name: keyspaceName}
  350. if session.useSystemSchema { // Cassandra 3.x+
  351. const stmt = `
  352. SELECT durable_writes, replication
  353. FROM system_schema.keyspaces
  354. WHERE keyspace_name = ?`
  355. var replication map[string]string
  356. iter := session.control.query(stmt, keyspaceName)
  357. iter.Scan(&keyspace.DurableWrites, &replication)
  358. err := iter.Close()
  359. if err != nil {
  360. return nil, fmt.Errorf("Error querying keyspace schema: %v", err)
  361. }
  362. keyspace.StrategyClass = replication["class"]
  363. keyspace.StrategyOptions = make(map[string]interface{})
  364. for k, v := range replication {
  365. keyspace.StrategyOptions[k] = v
  366. }
  367. } else {
  368. const stmt = `
  369. SELECT durable_writes, strategy_class, strategy_options
  370. FROM system.schema_keyspaces
  371. WHERE keyspace_name = ?`
  372. var strategyOptionsJSON []byte
  373. iter := session.control.query(stmt, keyspaceName)
  374. iter.Scan(&keyspace.DurableWrites, &keyspace.StrategyClass, &strategyOptionsJSON)
  375. err := iter.Close()
  376. if err != nil {
  377. return nil, fmt.Errorf("Error querying keyspace schema: %v", err)
  378. }
  379. err = json.Unmarshal(strategyOptionsJSON, &keyspace.StrategyOptions)
  380. if err != nil {
  381. return nil, fmt.Errorf(
  382. "Invalid JSON value '%s' as strategy_options for in keyspace '%s': %v",
  383. strategyOptionsJSON, keyspace.Name, err,
  384. )
  385. }
  386. }
  387. return keyspace, nil
  388. }
  389. // query for only the table metadata in the specified keyspace from system.schema_columnfamilies
  390. func getTableMetadata(session *Session, keyspaceName string) ([]TableMetadata, error) {
  391. var (
  392. iter *Iter
  393. scan func(iter *Iter, table *TableMetadata) bool
  394. stmt string
  395. keyAliasesJSON []byte
  396. columnAliasesJSON []byte
  397. )
  398. if session.useSystemSchema { // Cassandra 3.x+
  399. stmt = `
  400. SELECT
  401. table_name
  402. FROM system_schema.tables
  403. WHERE keyspace_name = ?`
  404. switchIter := func() *Iter {
  405. iter.Close()
  406. stmt = `
  407. SELECT
  408. view_name
  409. FROM system_schema.views
  410. WHERE keyspace_name = ?`
  411. iter = session.control.query(stmt, keyspaceName)
  412. return iter
  413. }
  414. scan = func(iter *Iter, table *TableMetadata) bool {
  415. r := iter.Scan(
  416. &table.Name,
  417. )
  418. if !r {
  419. iter = switchIter()
  420. if iter != nil {
  421. switchIter = func() *Iter { return nil }
  422. r = iter.Scan(&table.Name)
  423. }
  424. }
  425. return r
  426. }
  427. } else if session.cfg.ProtoVersion < protoVersion4 {
  428. // we have key aliases
  429. // TODO: Do we need key_aliases?
  430. stmt = `
  431. SELECT
  432. columnfamily_name,
  433. key_validator,
  434. comparator,
  435. default_validator,
  436. key_aliases,
  437. column_aliases,
  438. value_alias
  439. FROM system.schema_columnfamilies
  440. WHERE keyspace_name = ?`
  441. scan = func(iter *Iter, table *TableMetadata) bool {
  442. return iter.Scan(
  443. &table.Name,
  444. &table.KeyValidator,
  445. &table.Comparator,
  446. &table.DefaultValidator,
  447. &keyAliasesJSON,
  448. &columnAliasesJSON,
  449. &table.ValueAlias,
  450. )
  451. }
  452. } else {
  453. stmt = `
  454. SELECT
  455. columnfamily_name,
  456. key_validator,
  457. comparator,
  458. default_validator
  459. FROM system.schema_columnfamilies
  460. WHERE keyspace_name = ?`
  461. scan = func(iter *Iter, table *TableMetadata) bool {
  462. return iter.Scan(
  463. &table.Name,
  464. &table.KeyValidator,
  465. &table.Comparator,
  466. &table.DefaultValidator,
  467. )
  468. }
  469. }
  470. iter = session.control.query(stmt, keyspaceName)
  471. tables := []TableMetadata{}
  472. table := TableMetadata{Keyspace: keyspaceName}
  473. for scan(iter, &table) {
  474. var err error
  475. // decode the key aliases
  476. if keyAliasesJSON != nil {
  477. table.KeyAliases = []string{}
  478. err = json.Unmarshal(keyAliasesJSON, &table.KeyAliases)
  479. if err != nil {
  480. iter.Close()
  481. return nil, fmt.Errorf(
  482. "Invalid JSON value '%s' as key_aliases for in table '%s': %v",
  483. keyAliasesJSON, table.Name, err,
  484. )
  485. }
  486. }
  487. // decode the column aliases
  488. if columnAliasesJSON != nil {
  489. table.ColumnAliases = []string{}
  490. err = json.Unmarshal(columnAliasesJSON, &table.ColumnAliases)
  491. if err != nil {
  492. iter.Close()
  493. return nil, fmt.Errorf(
  494. "Invalid JSON value '%s' as column_aliases for in table '%s': %v",
  495. columnAliasesJSON, table.Name, err,
  496. )
  497. }
  498. }
  499. tables = append(tables, table)
  500. table = TableMetadata{Keyspace: keyspaceName}
  501. }
  502. err := iter.Close()
  503. if err != nil && err != ErrNotFound {
  504. return nil, fmt.Errorf("Error querying table schema: %v", err)
  505. }
  506. return tables, nil
  507. }
  508. // query for only the column metadata in the specified keyspace from system.schema_columns
  509. func getColumnMetadata(
  510. session *Session,
  511. keyspaceName string,
  512. ) ([]ColumnMetadata, error) {
  513. // Deal with differences in protocol versions
  514. var stmt string
  515. var scan func(*Iter, *ColumnMetadata, *[]byte) bool
  516. if session.cfg.ProtoVersion == 1 {
  517. // V1 does not support the type column, and all returned rows are
  518. // of kind "regular".
  519. stmt = `
  520. SELECT
  521. columnfamily_name,
  522. column_name,
  523. component_index,
  524. validator,
  525. index_name,
  526. index_type,
  527. index_options
  528. FROM system.schema_columns
  529. WHERE keyspace_name = ?
  530. `
  531. scan = func(
  532. iter *Iter,
  533. column *ColumnMetadata,
  534. indexOptionsJSON *[]byte,
  535. ) bool {
  536. // all columns returned by V1 are regular
  537. column.Kind = ColumnRegular
  538. return iter.Scan(
  539. &column.Table,
  540. &column.Name,
  541. &column.ComponentIndex,
  542. &column.Validator,
  543. &column.Index.Name,
  544. &column.Index.Type,
  545. &indexOptionsJSON,
  546. )
  547. }
  548. } else if session.useSystemSchema { // Cassandra 3.x+
  549. stmt = `
  550. SELECT
  551. table_name,
  552. column_name,
  553. clustering_order,
  554. type,
  555. kind,
  556. position
  557. FROM system_schema.columns
  558. WHERE keyspace_name = ?
  559. `
  560. scan = func(
  561. iter *Iter,
  562. column *ColumnMetadata,
  563. indexOptionsJSON *[]byte,
  564. ) bool {
  565. return iter.Scan(
  566. &column.Table,
  567. &column.Name,
  568. &column.ClusteringOrder,
  569. &column.Validator,
  570. &column.Kind,
  571. &column.ComponentIndex,
  572. )
  573. }
  574. } else {
  575. // V2+ supports the type column
  576. stmt = `
  577. SELECT
  578. columnfamily_name,
  579. column_name,
  580. component_index,
  581. validator,
  582. index_name,
  583. index_type,
  584. index_options,
  585. type
  586. FROM system.schema_columns
  587. WHERE keyspace_name = ?
  588. `
  589. scan = func(
  590. iter *Iter,
  591. column *ColumnMetadata,
  592. indexOptionsJSON *[]byte,
  593. ) bool {
  594. return iter.Scan(
  595. &column.Table,
  596. &column.Name,
  597. &column.ComponentIndex,
  598. &column.Validator,
  599. &column.Index.Name,
  600. &column.Index.Type,
  601. &indexOptionsJSON,
  602. &column.Kind,
  603. )
  604. }
  605. }
  606. // get the columns metadata
  607. columns := []ColumnMetadata{}
  608. column := ColumnMetadata{Keyspace: keyspaceName}
  609. var indexOptionsJSON []byte
  610. iter := session.control.query(stmt, keyspaceName)
  611. for scan(iter, &column, &indexOptionsJSON) {
  612. var err error
  613. // decode the index options
  614. if indexOptionsJSON != nil {
  615. err = json.Unmarshal(indexOptionsJSON, &column.Index.Options)
  616. if err != nil {
  617. iter.Close()
  618. return nil, fmt.Errorf(
  619. "Invalid JSON value '%s' as index_options for column '%s' in table '%s': %v",
  620. indexOptionsJSON,
  621. column.Name,
  622. column.Table,
  623. err,
  624. )
  625. }
  626. }
  627. columns = append(columns, column)
  628. column = ColumnMetadata{Keyspace: keyspaceName}
  629. }
  630. err := iter.Close()
  631. if err != nil && err != ErrNotFound {
  632. return nil, fmt.Errorf("Error querying column schema: %v", err)
  633. }
  634. return columns, nil
  635. }
  636. // type definition parser state
  637. type typeParser struct {
  638. input string
  639. index int
  640. }
  641. // the type definition parser result
  642. type typeParserResult struct {
  643. isComposite bool
  644. types []TypeInfo
  645. reversed []bool
  646. collections map[string]TypeInfo
  647. }
  648. // Parse the type definition used for validator and comparator schema data
  649. func parseType(def string) typeParserResult {
  650. parser := &typeParser{input: def}
  651. return parser.parse()
  652. }
  653. const (
  654. REVERSED_TYPE = "org.apache.cassandra.db.marshal.ReversedType"
  655. COMPOSITE_TYPE = "org.apache.cassandra.db.marshal.CompositeType"
  656. COLLECTION_TYPE = "org.apache.cassandra.db.marshal.ColumnToCollectionType"
  657. LIST_TYPE = "org.apache.cassandra.db.marshal.ListType"
  658. SET_TYPE = "org.apache.cassandra.db.marshal.SetType"
  659. MAP_TYPE = "org.apache.cassandra.db.marshal.MapType"
  660. )
  661. // represents a class specification in the type def AST
  662. type typeParserClassNode struct {
  663. name string
  664. params []typeParserParamNode
  665. // this is the segment of the input string that defined this node
  666. input string
  667. }
  668. // represents a class parameter in the type def AST
  669. type typeParserParamNode struct {
  670. name *string
  671. class typeParserClassNode
  672. }
  673. func (t *typeParser) parse() typeParserResult {
  674. // parse the AST
  675. ast, ok := t.parseClassNode()
  676. if !ok {
  677. // treat this is a custom type
  678. return typeParserResult{
  679. isComposite: false,
  680. types: []TypeInfo{
  681. NativeType{
  682. typ: TypeCustom,
  683. custom: t.input,
  684. },
  685. },
  686. reversed: []bool{false},
  687. collections: nil,
  688. }
  689. }
  690. // interpret the AST
  691. if strings.HasPrefix(ast.name, COMPOSITE_TYPE) {
  692. count := len(ast.params)
  693. // look for a collections param
  694. last := ast.params[count-1]
  695. collections := map[string]TypeInfo{}
  696. if strings.HasPrefix(last.class.name, COLLECTION_TYPE) {
  697. count--
  698. for _, param := range last.class.params {
  699. // decode the name
  700. var name string
  701. decoded, err := hex.DecodeString(*param.name)
  702. if err != nil {
  703. log.Printf(
  704. "Error parsing type '%s', contains collection name '%s' with an invalid format: %v",
  705. t.input,
  706. *param.name,
  707. err,
  708. )
  709. // just use the provided name
  710. name = *param.name
  711. } else {
  712. name = string(decoded)
  713. }
  714. collections[name] = param.class.asTypeInfo()
  715. }
  716. }
  717. types := make([]TypeInfo, count)
  718. reversed := make([]bool, count)
  719. for i, param := range ast.params[:count] {
  720. class := param.class
  721. reversed[i] = strings.HasPrefix(class.name, REVERSED_TYPE)
  722. if reversed[i] {
  723. class = class.params[0].class
  724. }
  725. types[i] = class.asTypeInfo()
  726. }
  727. return typeParserResult{
  728. isComposite: true,
  729. types: types,
  730. reversed: reversed,
  731. collections: collections,
  732. }
  733. } else {
  734. // not composite, so one type
  735. class := *ast
  736. reversed := strings.HasPrefix(class.name, REVERSED_TYPE)
  737. if reversed {
  738. class = class.params[0].class
  739. }
  740. typeInfo := class.asTypeInfo()
  741. return typeParserResult{
  742. isComposite: false,
  743. types: []TypeInfo{typeInfo},
  744. reversed: []bool{reversed},
  745. }
  746. }
  747. }
  748. func (class *typeParserClassNode) asTypeInfo() TypeInfo {
  749. if strings.HasPrefix(class.name, LIST_TYPE) {
  750. elem := class.params[0].class.asTypeInfo()
  751. return CollectionType{
  752. NativeType: NativeType{
  753. typ: TypeList,
  754. },
  755. Elem: elem,
  756. }
  757. }
  758. if strings.HasPrefix(class.name, SET_TYPE) {
  759. elem := class.params[0].class.asTypeInfo()
  760. return CollectionType{
  761. NativeType: NativeType{
  762. typ: TypeSet,
  763. },
  764. Elem: elem,
  765. }
  766. }
  767. if strings.HasPrefix(class.name, MAP_TYPE) {
  768. key := class.params[0].class.asTypeInfo()
  769. elem := class.params[1].class.asTypeInfo()
  770. return CollectionType{
  771. NativeType: NativeType{
  772. typ: TypeMap,
  773. },
  774. Key: key,
  775. Elem: elem,
  776. }
  777. }
  778. // must be a simple type or custom type
  779. info := NativeType{typ: getApacheCassandraType(class.name)}
  780. if info.typ == TypeCustom {
  781. // add the entire class definition
  782. info.custom = class.input
  783. }
  784. return info
  785. }
  786. // CLASS := ID [ PARAMS ]
  787. func (t *typeParser) parseClassNode() (node *typeParserClassNode, ok bool) {
  788. t.skipWhitespace()
  789. startIndex := t.index
  790. name, ok := t.nextIdentifier()
  791. if !ok {
  792. return nil, false
  793. }
  794. params, ok := t.parseParamNodes()
  795. if !ok {
  796. return nil, false
  797. }
  798. endIndex := t.index
  799. node = &typeParserClassNode{
  800. name: name,
  801. params: params,
  802. input: t.input[startIndex:endIndex],
  803. }
  804. return node, true
  805. }
  806. // PARAMS := "(" PARAM { "," PARAM } ")"
  807. // PARAM := [ PARAM_NAME ":" ] CLASS
  808. // PARAM_NAME := ID
  809. func (t *typeParser) parseParamNodes() (params []typeParserParamNode, ok bool) {
  810. t.skipWhitespace()
  811. // the params are optional
  812. if t.index == len(t.input) || t.input[t.index] != '(' {
  813. return nil, true
  814. }
  815. params = []typeParserParamNode{}
  816. // consume the '('
  817. t.index++
  818. t.skipWhitespace()
  819. for t.input[t.index] != ')' {
  820. // look for a named param, but if no colon, then we want to backup
  821. backupIndex := t.index
  822. // name will be a hex encoded version of a utf-8 string
  823. name, ok := t.nextIdentifier()
  824. if !ok {
  825. return nil, false
  826. }
  827. hasName := true
  828. // TODO handle '=>' used for DynamicCompositeType
  829. t.skipWhitespace()
  830. if t.input[t.index] == ':' {
  831. // there is a name for this parameter
  832. // consume the ':'
  833. t.index++
  834. t.skipWhitespace()
  835. } else {
  836. // no name, backup
  837. hasName = false
  838. t.index = backupIndex
  839. }
  840. // parse the next full parameter
  841. classNode, ok := t.parseClassNode()
  842. if !ok {
  843. return nil, false
  844. }
  845. if hasName {
  846. params = append(
  847. params,
  848. typeParserParamNode{name: &name, class: *classNode},
  849. )
  850. } else {
  851. params = append(
  852. params,
  853. typeParserParamNode{class: *classNode},
  854. )
  855. }
  856. t.skipWhitespace()
  857. if t.input[t.index] == ',' {
  858. // consume the comma
  859. t.index++
  860. t.skipWhitespace()
  861. }
  862. }
  863. // consume the ')'
  864. t.index++
  865. return params, true
  866. }
  867. func (t *typeParser) skipWhitespace() {
  868. for t.index < len(t.input) && isWhitespaceChar(t.input[t.index]) {
  869. t.index++
  870. }
  871. }
  872. func isWhitespaceChar(c byte) bool {
  873. return c == ' ' || c == '\n' || c == '\t'
  874. }
  875. // ID := LETTER { LETTER }
  876. // LETTER := "0"..."9" | "a"..."z" | "A"..."Z" | "-" | "+" | "." | "_" | "&"
  877. func (t *typeParser) nextIdentifier() (id string, found bool) {
  878. startIndex := t.index
  879. for t.index < len(t.input) && isIdentifierChar(t.input[t.index]) {
  880. t.index++
  881. }
  882. if startIndex == t.index {
  883. return "", false
  884. }
  885. return t.input[startIndex:t.index], true
  886. }
  887. func isIdentifierChar(c byte) bool {
  888. return (c >= '0' && c <= '9') ||
  889. (c >= 'a' && c <= 'z') ||
  890. (c >= 'A' && c <= 'Z') ||
  891. c == '-' ||
  892. c == '+' ||
  893. c == '.' ||
  894. c == '_' ||
  895. c == '&'
  896. }