Browse Source

Merge pull request #1 from tux21b/master

Update repo
Phillip Couto 12 years ago
parent
commit
245a736126
7 changed files with 49 additions and 10 deletions
  1. 5 0
      AUTHORS
  2. 7 3
      cluster.go
  3. 1 1
      conn.go
  4. 3 0
      gocql_test/main.go
  5. 6 6
      marshal.go
  6. 21 0
      marshal_test.go
  7. 6 0
      session.go

+ 5 - 0
AUTHORS

@@ -5,3 +5,8 @@ Jonathan Rudenberg <jonathan@titanous.com>
 Thorsten von Eicken <tve@rightscale.com>
 Matt Robenolt <mattr@disqus.com>
 Phillip Couto <phillip.couto@stemstudios.com>
+Niklas Korz <korz.niklask@gmail.com>
+Nimi Wariboko Jr <nimi@channelmeter.com>
+Ghais Issa <ghais.issa@gmail.com>
+Sasha Klizhentas <klizhentas@gmail.com>
+Konstantin Cherkasov <k.cherkasoff@gmail.com>

+ 7 - 3
cluster.go

@@ -59,7 +59,7 @@ func (cfg *ClusterConfig) CreateSession() *Session {
 		connPool: make(map[string]*RoundRobin),
 		conns:    make(map[*Conn]struct{}),
 		quitWait: make(chan bool),
-		keyspace: cfg.Keyspace,		
+		keyspace: cfg.Keyspace,
 	}
 	impl.wgStart.Add(1)
 	for i := 0; i < len(impl.cfg.Hosts); i++ {
@@ -72,7 +72,9 @@ func (cfg *ClusterConfig) CreateSession() *Session {
 		}
 	}
 	impl.wgStart.Wait()
-	return NewSession(impl)
+	s := NewSession(impl)
+	s.SetConsistency(cfg.Consistency)
+	return s
 }
 
 type clusterImpl struct {
@@ -179,7 +181,9 @@ func (c *clusterImpl) HandleError(conn *Conn, err error, closed bool) {
 		return
 	}
 	c.removeConn(conn)
-	go c.connect(conn.Address()) // reconnect
+	if !c.quit {
+		go c.connect(conn.Address()) // reconnect
+	}
 }
 
 func (c *clusterImpl) HandleKeyspace(conn *Conn, keyspace string) {

+ 1 - 1
conn.go

@@ -342,7 +342,7 @@ func (c *Conn) Address() string {
 }
 
 func (c *Conn) UseKeyspace(keyspace string) error {
-	resp, err := c.exec(&queryFrame{Stmt: "USE " + keyspace, Cons: Any}, nil)
+	resp, err := c.exec(&queryFrame{Stmt: `USE "` + keyspace + `"`, Cons: Any}, nil)
 	if err != nil {
 		return err
 	}

+ 3 - 0
gocql_test/main.go

@@ -51,7 +51,10 @@ func initSchema() error {
 		}`).Exec(); err != nil {
 		return err
 	}
+	log.Println("Testing that the connections do not reconnect in an infinite loop.")
 	session.Close()
+	time.Sleep(15 * time.Second)
+	log.Println("If there were error messages that an address cannot be assigned then the test failed.")
 	cluster.Keyspace = "gocql_test"
 	session = cluster.CreateSession()
 

+ 6 - 6
marshal.go

@@ -37,7 +37,7 @@ func Marshal(info *TypeInfo, value interface{}) ([]byte, error) {
 		return marshalBool(info, value)
 	case TypeInt:
 		return marshalInt(info, value)
-	case TypeBigInt:
+	case TypeBigInt, TypeCounter:
 		return marshalBigInt(info, value)
 	case TypeFloat:
 		return marshalFloat(info, value)
@@ -785,7 +785,7 @@ func unmarshalList(info *TypeInfo, data []byte, value interface{}) error {
 		if len(data) < 2 {
 			return unmarshalErrorf("unmarshal list: unexpected eof")
 		}
-		n := int(data[0]<<8) | int(data[1])
+		n := int(data[0])<<8 | int(data[1])
 		data = data[2:]
 		if k == reflect.Array {
 			if rv.Len() != n {
@@ -800,7 +800,7 @@ func unmarshalList(info *TypeInfo, data []byte, value interface{}) error {
 			if len(data) < 2 {
 				return unmarshalErrorf("unmarshal list: unexpected eof")
 			}
-			m := int(data[0]<<8) | int(data[1])
+			m := int(data[0])<<8 | int(data[1])
 			data = data[2:]
 			if err := Unmarshal(info.Elem, data[:m], rv.Index(i).Addr().Interface()); err != nil {
 				return err
@@ -873,13 +873,13 @@ func unmarshalMap(info *TypeInfo, data []byte, value interface{}) error {
 	if len(data) < 2 {
 		return unmarshalErrorf("unmarshal map: unexpected eof")
 	}
-	n := int(data[0]<<8) | int(data[1])
+	n := int(data[1]) | int(data[0])<<8
 	data = data[2:]
 	for i := 0; i < n; i++ {
 		if len(data) < 2 {
 			return unmarshalErrorf("unmarshal list: unexpected eof")
 		}
-		m := int(data[0]<<8) | int(data[1])
+		m := int(data[1]) | int(data[0])<<8
 		data = data[2:]
 		key := reflect.New(t.Key())
 		if err := Unmarshal(info.Key, data[:m], key.Interface()); err != nil {
@@ -887,7 +887,7 @@ func unmarshalMap(info *TypeInfo, data []byte, value interface{}) error {
 		}
 		data = data[m:]
 
-		m = int(data[0]<<8) | int(data[1])
+		m = int(data[1]) | int(data[0])<<8
 		data = data[2:]
 		val := reflect.New(t.Elem())
 		if err := Unmarshal(info.Elem, data[:m], val.Interface()); err != nil {

+ 21 - 0
marshal_test.go

@@ -155,6 +155,27 @@ var marshalTests = []struct {
 		[]byte(nil),
 		map[string]int(nil),
 	},
+	{
+		&TypeInfo{Type: TypeList, Elem: &TypeInfo{Type: TypeVarchar}},
+		bytes.Join([][]byte{
+			[]byte("\x00\x01\xFF\xFF"),
+			bytes.Repeat([]byte("X"), 65535)}, []byte("")),
+		[]string{strings.Repeat("X", 65535)},
+	},
+	{
+		&TypeInfo{Type: TypeMap,
+			Key:  &TypeInfo{Type: TypeVarchar},
+			Elem: &TypeInfo{Type: TypeVarchar},
+		},
+		bytes.Join([][]byte{
+			[]byte("\x00\x01\xFF\xFF"),
+			bytes.Repeat([]byte("X"), 65535),
+			[]byte("\xFF\xFF"),
+			bytes.Repeat([]byte("Y"), 65535)}, []byte("")),
+		map[string]string{
+			strings.Repeat("X", 65535): strings.Repeat("Y", 65535),
+		},
+	},
 }
 
 func TestMarshal(t *testing.T) {

+ 6 - 0
session.go

@@ -161,6 +161,12 @@ func (q *Query) Iter() *Iter {
 // were selected, ErrNotFound is returned.
 func (q *Query) Scan(dest ...interface{}) error {
 	iter := q.Iter()
+	if iter.err != nil {
+		return iter.err
+	}
+	if len(iter.rows) == 0 {
+		return ErrNotFound
+	}
 	iter.Scan(dest...)
 	return iter.Close()
 }