Created
July 27, 2016 06:34
-
-
Save slayercat/d2820d46bfbd5f34424dacad4175443a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "github.com/gocql/gocql" | |
import ( | |
"log" | |
"sync" | |
"time" | |
) | |
import ( | |
"fmt" | |
"strings" | |
) | |
type position struct { | |
Lat int | |
Lon int | |
Padding string | |
} | |
// NOTE: due to current implementation details it is not currently possible to use | |
// a pointer receiver type for the UDTMarshaler interface to handle UDT's | |
func (p position) MarshalUDT(name string, info gocql.TypeInfo) ([]byte, error) { | |
switch name { | |
case "lat": | |
return gocql.Marshal(info, p.Lat) | |
case "lon": | |
return gocql.Marshal(info, p.Lon) | |
case "padding": | |
return gocql.Marshal(info, p.Padding) | |
default: | |
return nil, fmt.Errorf("unknown column for position: %q", name) | |
} | |
} | |
func (p *position) UnmarshalUDT(name string, info gocql.TypeInfo, data []byte) error { | |
switch name { | |
case "lat": | |
return gocql.Unmarshal(info, data, &p.Lat) | |
case "lon": | |
return gocql.Unmarshal(info, data, &p.Lon) | |
case "padding": | |
return gocql.Unmarshal(info, data, &p.Padding) | |
default: | |
return fmt.Errorf("unknown column for position: %q", name) | |
} | |
} | |
func main() { | |
session := createSession() | |
defer session.Close() | |
err := createTable(session, `CREATE TYPE gocql_test.position( | |
lat int, | |
lon int, | |
padding text);`) | |
if err != nil { | |
panic(err) | |
} | |
err = createTable(session, `CREATE TABLE gocql_test.houses( | |
id int, | |
name text, | |
loc frozen<position>, | |
primary key(id) | |
);`) | |
if err != nil { | |
panic(err) | |
} | |
const ( | |
expLat = -1 | |
expLon = 2 | |
) | |
pad := strings.Repeat("X", 1000) | |
err = session.Query("INSERT INTO houses(id, name) VALUES(?, ?)", 1, "test" ).Exec() | |
if err != nil { | |
panic(err) | |
} | |
pos := &position{} | |
err = session.Query("SELECT loc FROM houses WHERE id = ?", 1).Scan(pos) | |
if err != nil { | |
panic(err) | |
} | |
} | |
func createTable(s *gocql.Session, table string) error { | |
// lets just be really sure | |
if err := s.Query(table).RetryPolicy(nil).Exec(); err != nil { | |
log.Printf("error creating table table=%q err=%v\n", table, err) | |
return err | |
} | |
return nil | |
} | |
func createCluster() *gocql.ClusterConfig { | |
cluster := gocql.NewCluster("10.255.194.234") | |
cluster.ProtoVersion = 3 | |
cluster.CQLVersion = "3.0.0" | |
cluster.Timeout = 5*time.Second | |
cluster.Consistency = gocql.Quorum | |
cluster.MaxWaitSchemaAgreement = 2 * time.Minute // travis might be slow | |
return cluster | |
} | |
func createKeyspace(cluster *gocql.ClusterConfig, keyspace string) { | |
c := *cluster | |
c.Keyspace = "system" | |
c.Timeout = 30 * time.Second | |
session, err := c.CreateSession() | |
if err != nil { | |
panic(err) | |
} | |
defer session.Close() | |
defer log.Println("closing keyspace session") | |
err = createTable(session, `DROP KEYSPACE IF EXISTS `+keyspace) | |
if err != nil { | |
panic(fmt.Sprintf("unable to drop keyspace: %v", err)) | |
} | |
err = createTable(session, fmt.Sprintf(`CREATE KEYSPACE %s | |
WITH replication = { | |
'class' : 'SimpleStrategy', | |
'replication_factor':1 | |
}`, keyspace)) | |
if err != nil { | |
panic(fmt.Sprintf("unable to create keyspace: %v", err)) | |
} | |
} | |
var initOnce sync.Once | |
func createSessionFromCluster(cluster *gocql.ClusterConfig) *gocql.Session { | |
// Drop and re-create the keyspace once. Different tests should use their own | |
// individual tables, but can assume that the table does not exist before. | |
initOnce.Do(func() { | |
createKeyspace(cluster, "gocql_test") | |
}) | |
cluster.Keyspace = "gocql_test" | |
session, err := cluster.CreateSession() | |
if err != nil { | |
print("createSession:", err) | |
} | |
return session | |
} | |
func createSession() *gocql.Session { | |
cluster := createCluster() | |
return createSessionFromCluster(cluster) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment