// Map will write in any mutations needed to maintain this timeseries index
// based on the current item and the lastRead item
func (ts *TimeSeries) Map(writer gossie.Writer, item, lastRead interface{}) error {
	uid, t := ts.Marshaler(item)
	b, err := json.Marshal(item)
	if err != nil {
		log.Errorf("Error marshalling item %s", err) // TODO should probably bubble this up
		return err
	}
	rowPrefix := ""
	if ts.SecondaryIndexer != nil {
		rowPrefix = ts.SecondaryIndexer(item)
	}

	// C* ttl for ageing out columns
	var ttl int32
	if ts.Ttler != nil {
		ttl = ts.Ttler(item)
	}

	// always mutate against current marshaled value -- if not zero time
	if !t.IsZero() {
		tsRow := &gossie.Row{
			Key: ts.toRowKey(rowPrefix, t),
			Columns: []*gossie.Column{
				{
					Name:  idTimeToColumnName(uid, t),
					Value: b,
					Ttl:   ttl,
				},
			},
		}
		writer.Insert(ts.Cf, tsRow)
	}

	// old one that's different (and old one not zero)? add deletion...
	if lastRead != nil && !reflect.ValueOf(lastRead).IsNil() {
		lUid, lT := ts.Marshaler(lastRead)
		if ts.SecondaryIndexer != nil {
			lRowPrefix := ts.SecondaryIndexer(lastRead)
			if !lT.IsZero() && (!lT.Equal(t) || lRowPrefix != rowPrefix) {
				writer.DeleteColumns(ts.Cf, ts.toRowKey(lRowPrefix, lT), [][]byte{idTimeToColumnName(lUid, lT)})
			}
		}
	}

	// update index, if we have one defined
	if ts.IndexCf != "" {
		i := newIndex(ts, rowPrefix)
		i.write(writer, t.Truncate(ts.RowGranularity))
	}

	return nil
}
Exemple #2
0
// write will add a row into a mutation to update the index view
func (i *index) write(writer gossie.Writer, rowTime time.Time) {
	if rowTime.IsZero() {
		return
	}
	// truncate to our minimum granularity (24 hours - in order to constrain max cols per row to a few thousand likely)
	rowTime = i.truncate(rowTime)

	row := &gossie.Row{
		Key: i.rowKey(),
		Columns: []*gossie.Column{
			{
				Name:  i.colName(rowTime),
				Value: placeholderValue,
			},
		},
	}
	writer.Insert(i.ts.IndexCf, row)
	//log.Debugf("Writing timeseries INDEX mutation for row key=%v %v", string(i.rowKey()), row)
}