1//  Copyright (c) 2014 Couchbase, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// 		http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package document
16
17import (
18	"fmt"
19	"math"
20	"time"
21
22	"github.com/blevesearch/bleve/analysis"
23	"github.com/blevesearch/bleve/numeric"
24)
25
26const DefaultDateTimeIndexingOptions = StoreField | IndexField | DocValues
27const DefaultDateTimePrecisionStep uint = 4
28
29var MinTimeRepresentable = time.Unix(0, math.MinInt64)
30var MaxTimeRepresentable = time.Unix(0, math.MaxInt64)
31
32type DateTimeField struct {
33	name              string
34	arrayPositions    []uint64
35	options           IndexingOptions
36	value             numeric.PrefixCoded
37	numPlainTextBytes uint64
38}
39
40func (n *DateTimeField) Name() string {
41	return n.name
42}
43
44func (n *DateTimeField) ArrayPositions() []uint64 {
45	return n.arrayPositions
46}
47
48func (n *DateTimeField) Options() IndexingOptions {
49	return n.options
50}
51
52func (n *DateTimeField) Analyze() (int, analysis.TokenFrequencies) {
53	tokens := make(analysis.TokenStream, 0)
54	tokens = append(tokens, &analysis.Token{
55		Start:    0,
56		End:      len(n.value),
57		Term:     n.value,
58		Position: 1,
59		Type:     analysis.DateTime,
60	})
61
62	original, err := n.value.Int64()
63	if err == nil {
64
65		shift := DefaultDateTimePrecisionStep
66		for shift < 64 {
67			shiftEncoded, err := numeric.NewPrefixCodedInt64(original, shift)
68			if err != nil {
69				break
70			}
71			token := analysis.Token{
72				Start:    0,
73				End:      len(shiftEncoded),
74				Term:     shiftEncoded,
75				Position: 1,
76				Type:     analysis.DateTime,
77			}
78			tokens = append(tokens, &token)
79			shift += DefaultDateTimePrecisionStep
80		}
81	}
82
83	fieldLength := len(tokens)
84	tokenFreqs := analysis.TokenFrequency(tokens, n.arrayPositions, n.options.IncludeTermVectors())
85	return fieldLength, tokenFreqs
86}
87
88func (n *DateTimeField) Value() []byte {
89	return n.value
90}
91
92func (n *DateTimeField) DateTime() (time.Time, error) {
93	i64, err := n.value.Int64()
94	if err != nil {
95		return time.Time{}, err
96	}
97	return time.Unix(0, i64).UTC(), nil
98}
99
100func (n *DateTimeField) GoString() string {
101	return fmt.Sprintf("&document.DateField{Name:%s, Options: %s, Value: %s}", n.name, n.options, n.value)
102}
103
104func (n *DateTimeField) NumPlainTextBytes() uint64 {
105	return n.numPlainTextBytes
106}
107
108func NewDateTimeFieldFromBytes(name string, arrayPositions []uint64, value []byte) *DateTimeField {
109	return &DateTimeField{
110		name:              name,
111		arrayPositions:    arrayPositions,
112		value:             value,
113		options:           DefaultDateTimeIndexingOptions,
114		numPlainTextBytes: uint64(len(value)),
115	}
116}
117
118func NewDateTimeField(name string, arrayPositions []uint64, dt time.Time) (*DateTimeField, error) {
119	return NewDateTimeFieldWithIndexingOptions(name, arrayPositions, dt, DefaultDateTimeIndexingOptions)
120}
121
122func NewDateTimeFieldWithIndexingOptions(name string, arrayPositions []uint64, dt time.Time, options IndexingOptions) (*DateTimeField, error) {
123	if canRepresent(dt) {
124		dtInt64 := dt.UnixNano()
125		prefixCoded := numeric.MustNewPrefixCodedInt64(dtInt64, 0)
126		return &DateTimeField{
127			name:           name,
128			arrayPositions: arrayPositions,
129			value:          prefixCoded,
130			options:        options,
131			// not correct, just a place holder until we revisit how fields are
132			// represented and can fix this better
133			numPlainTextBytes: uint64(8),
134		}, nil
135	}
136	return nil, fmt.Errorf("cannot represent %s in this type", dt)
137}
138
139func canRepresent(dt time.Time) bool {
140	if dt.Before(MinTimeRepresentable) || dt.After(MaxTimeRepresentable) {
141		return false
142	}
143	return true
144}
145