Skip to content

Commit

Permalink
Initial collation support in indexes.
Browse files Browse the repository at this point in the history
For now tested by hand due to new configdb option constraints introduced
in current 3.3. Test harness will need to change yet again to adapt to
latest supported options. :-(
  • Loading branch information
niemeyer committed Aug 1, 2016
1 parent 293ca5c commit 9a2573d
Showing 1 changed file with 53 additions and 1 deletion.
54 changes: 53 additions & 1 deletion session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1011,6 +1011,8 @@ type indexSpec struct {
DefaultLanguage string "default_language,omitempty"
LanguageOverride string "language_override,omitempty"
TextIndexVersion int "textIndexVersion,omitempty"

Collation *Collation "collation,omitempty"
}

type Index struct {
Expand Down Expand Up @@ -1049,6 +1051,54 @@ type Index struct {
// from the weighted sum of the frequency for each of the indexed fields in
// that document. The default field weight is 1.
Weights map[string]int

// Collation defines the collation to use for the index.
Collation *Collation
}

type Collation struct {

// Locale defines the collation locale.
Locale string `bson:"locale"`

// CaseLevel defines whether to turn case sensitivity on at strength 1 or 2.
CaseLevel bool `bson:"caseLevel,omitempty"`

// CaseFirst may be set to "upper" or "lower" to define whether
// to have uppercase or lowercase items first. Default is "off".
CaseFirst string `bson:"caseFirst,omitempty"`

// Strength defines the priority of comparison properties, as follows:
//
// 1 (primary) - Strongest level, denote difference between base characters
// 2 (secondary) - Accents in characters are considered secondary differences
// 3 (tertiary) - Upper and lower case differences in characters are
// distinguished at the tertiary level
// 4 (quaternary) - When punctuation is ignored at level 1-3, an additional
// level can be used to distinguish words with and without
// punctuation. Should only be used if ignoring punctuation
// is required or when processing Japanese text.
// 5 (identical) - When all other levels are equal, the identical level is
// used as a tiebreaker. The Unicode code point values of
// the NFD form of each string are compared at this level,
// just in case there is no difference at levels 1-4
//
// Strength defaults to 3.
Strength int `bson:"strength,omitempty"`

// NumericOrdering defines whether to order numbers based on numerical
// order and not collation order.
NumericOrdering bool `bson:"numericOrdering,omitempty"`

// Alternate controls whether spaces and punctuation are considered base characters.
// May be set to "non-ignorable" (spaces and punctuation considered base characters)
// or "shifted" (spaces and punctuation not considered base characters, and only
// distinguished at strength > 3). Defaults to "non-ignorable".
Alternate string `bson:"alternate,omitempty"`

// Backwards defines whether to have secondary differences considered in reverse order,
// as done in the French language.
Backwards bool `bson:"backwards,omitempty"`
}

// mgo.v3: Drop Minf and Maxf and transform Min and Max to floats.
Expand Down Expand Up @@ -1242,6 +1292,7 @@ func (c *Collection) EnsureIndex(index Index) error {
Weights: keyInfo.weights,
DefaultLanguage: index.DefaultLanguage,
LanguageOverride: index.LanguageOverride,
Collation: index.Collation,
}

if spec.Min == 0 && spec.Max == 0 {
Expand Down Expand Up @@ -1456,6 +1507,7 @@ func indexFromSpec(spec indexSpec) Index {
DefaultLanguage: spec.DefaultLanguage,
LanguageOverride: spec.LanguageOverride,
ExpireAfter: time.Duration(spec.ExpireAfter) * time.Second,
Collation: spec.Collation,
}
if float64(int(spec.Min)) == spec.Min && float64(int(spec.Max)) == spec.Max {
index.Min = int(spec.Min)
Expand Down Expand Up @@ -1584,7 +1636,7 @@ func (s *Session) Refresh() {
}

// SetMode changes the consistency mode for the session.
//
//
// The default mode is Strong.
//
// In the Strong consistency mode reads and writes will always be made to
Expand Down

0 comments on commit 9a2573d

Please sign in to comment.