Skip to content

Commit 3662be4

Browse files
consistency serial was added
1 parent 953e0df commit 3662be4

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

frame.go

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ const (
192192

193193
type Consistency uint16
194194

195+
type SerialConsistency = Consistency
196+
195197
const (
196198
Any Consistency = 0x00
197199
One Consistency = 0x01
@@ -202,6 +204,8 @@ const (
202204
LocalQuorum Consistency = 0x06
203205
EachQuorum Consistency = 0x07
204206
LocalOne Consistency = 0x0A
207+
Serial Consistency = 0x08
208+
LocalSerial Consistency = 0x09
205209
)
206210

207211
func (c Consistency) String() string {
@@ -224,6 +228,10 @@ func (c Consistency) String() string {
224228
return "EACH_QUORUM"
225229
case LocalOne:
226230
return "LOCAL_ONE"
231+
case Serial:
232+
return "SERIAL"
233+
case LocalSerial:
234+
return "LOCAL_SERIAL"
227235
default:
228236
return fmt.Sprintf("UNKNOWN_CONS_0x%x", uint16(c))
229237
}
@@ -253,13 +261,21 @@ func (c *Consistency) UnmarshalText(text []byte) error {
253261
*c = EachQuorum
254262
case "LOCAL_ONE":
255263
*c = LocalOne
264+
case "SERIAL":
265+
*c = Serial
266+
case "LOCAL_SERIAL":
267+
*c = LocalSerial
256268
default:
257269
return fmt.Errorf("invalid consistency %q", string(text))
258270
}
259271

260272
return nil
261273
}
262274

275+
func (c Consistency) IsSerial() bool {
276+
return c == Serial || c == LocalSerial
277+
278+
}
263279
func ParseConsistency(s string) Consistency {
264280
var c Consistency
265281
if err := c.UnmarshalText([]byte(strings.ToUpper(s))); err != nil {
@@ -286,41 +302,6 @@ func MustParseConsistency(s string) (Consistency, error) {
286302
return c, nil
287303
}
288304

289-
type SerialConsistency uint16
290-
291-
const (
292-
Serial SerialConsistency = 0x08
293-
LocalSerial SerialConsistency = 0x09
294-
)
295-
296-
func (s SerialConsistency) String() string {
297-
switch s {
298-
case Serial:
299-
return "SERIAL"
300-
case LocalSerial:
301-
return "LOCAL_SERIAL"
302-
default:
303-
return fmt.Sprintf("UNKNOWN_SERIAL_CONS_0x%x", uint16(s))
304-
}
305-
}
306-
307-
func (s SerialConsistency) MarshalText() (text []byte, err error) {
308-
return []byte(s.String()), nil
309-
}
310-
311-
func (s *SerialConsistency) UnmarshalText(text []byte) error {
312-
switch string(text) {
313-
case "SERIAL":
314-
*s = Serial
315-
case "LOCAL_SERIAL":
316-
*s = LocalSerial
317-
default:
318-
return fmt.Errorf("invalid consistency %q", string(text))
319-
}
320-
321-
return nil
322-
}
323-
324305
const (
325306
apacheCassandraTypePrefix = "org.apache.cassandra.db.marshal."
326307
)

session.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ func NewSession(cfg ClusterConfig) (*Session, error) {
144144
return nil, errors.New("Can't use both Authenticator and AuthProvider in cluster config.")
145145
}
146146

147+
if cfg.Consistency.IsSerial() {
148+
return nil, fmt.Errorf("the default consistency level is not allowed to be SERIAL or LOCAL_SERIAL. Recived value: %v", cfg.Consistency)
149+
}
150+
147151
// TODO: we should take a context in here at some point
148152
ctx, cancel := context.WithCancel(context.TODO())
149153

@@ -1265,6 +1269,9 @@ func (q *Query) Bind(v ...interface{}) *Query {
12651269
// SERIAL. This option will be ignored for anything else that a
12661270
// conditional update/insert.
12671271
func (q *Query) SerialConsistency(cons SerialConsistency) *Query {
1272+
if !cons.IsSerial() {
1273+
panic("Serial consistency can only be SERIAL or LOCAL_SERIAL got " + cons.String())
1274+
}
12681275
q.serialCons = cons
12691276
return q
12701277
}
@@ -1915,6 +1922,9 @@ func (b *Batch) Size() int {
19151922
//
19161923
// Only available for protocol 3 and above
19171924
func (b *Batch) SerialConsistency(cons SerialConsistency) *Batch {
1925+
if !cons.IsSerial() {
1926+
panic("Serial consistency can only be SERIAL or LOCAL_SERIAL got " + cons.String())
1927+
}
19181928
b.serialCons = cons
19191929
return b
19201930
}

0 commit comments

Comments
 (0)