metadata.go 23 KB

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