Added bson encode and decode functions for KeyRange Arrays.

This commit is contained in:
shrutip 2014-01-21 23:46:00 -08:00
Родитель bb28b96967
Коммит 4d66eaf0e4
1 изменённых файлов: 34 добавлений и 0 удалений

Просмотреть файл

@ -164,6 +164,17 @@ func (kr *KeyRange) MarshalBson(buf *bytes2.ChunkedWriter) {
lenWriter.RecordLen()
}
func EncodeKeyRanges(buf *bytes2.ChunkedWriter, keyName string, keyranges []KeyRange) {
bson.EncodePrefix(buf, bson.Array, keyName)
lenWriter := bson.NewLenWriter(buf)
for i, kr := range keyranges {
bson.EncodePrefix(buf, bson.Object, bson.Itoa(i))
kr.MarshalBson(buf)
}
buf.WriteByte(0)
lenWriter.RecordLen()
}
func (kr *KeyRange) UnmarshalBson(buf *bytes.Buffer) {
bson.Next(buf, 4)
@ -182,6 +193,29 @@ func (kr *KeyRange) UnmarshalBson(buf *bytes.Buffer) {
}
}
func DecodeKeyRanges(buf *bytes.Buffer, kind byte) (keyranges []KeyRange) {
switch kind {
case bson.Array:
// valid
case bson.Null:
return nil
default:
panic(bson.NewBsonError("Unexpected data type %v for Keyranges", kind))
}
bson.Next(buf, 4)
keyranges = make([]KeyRange, 0, 10)
kind = bson.NextByte(buf)
var kr KeyRange
for i := 0; kind != bson.EOO; i++ {
bson.ExpectIndex(buf, i)
kr.UnmarshalBson(buf)
keyranges = append(keyranges, kr)
kind = bson.NextByte(buf)
}
return keyranges
}
// KeyRangesIntersect returns true if some Keyspace values exist in both ranges.
//
// See: http://stackoverflow.com/questions/4879315/what-is-a-tidy-algorithm-to-find-overlapping-intervals