Beispiel #1
0
func (w *MockWriter) InsertTtl(cf string, row *Row, ttl int) Writer {
	rows := w.pool.Rows(cf)

	t := thrift.Int64Ptr(now())
	for _, c := range row.Columns {
		if c.Timestamp == nil {
			c.Timestamp = t
		}
		if ttl > 0 {
			c.Ttl = thrift.Int32Ptr(int32(ttl))
		}
		if c.Ttl != nil {
			// reset to the actual time to expire
			c.Ttl = thrift.Int32Ptr(int32(now()/1e6) + *c.Ttl)
		}
	}

	i := sort.Search(len(rows), func(i int) bool { return bytes.Compare(rows[i].Key, row.Key) >= 0 })
	if i < len(rows) && bytes.Equal(rows[i].Key, row.Key) {
		// Row already exists, merge the columns
		e := rows[i]
		checkExpired(e)
		cols := e.Columns
		for _, c := range row.Columns {
			j := sort.Search(len(cols), func(j int) bool { return bytes.Compare(cols[j].Name, c.Name) >= 0 })
			if j < len(cols) && bytes.Equal(cols[j].Name, c.Name) {
				// Column already exists, pick the one with the greater timestamp
				ec := cols[j]
				et := *t
				if ec != nil {
					et = *ec.Timestamp
				}
				if *c.Timestamp >= et {
					ec.Value = c.Value
					ec.Ttl = c.Ttl
					ec.Timestamp = c.Timestamp
				}
			} else {
				// New column, insert sorted
				cols = append(cols, c)
				copy(cols[j+1:], cols[j:])
				cols[j] = c
			}
		}
		e.Columns = cols
	} else {
		// New row, insert sorted
		sort.Sort(Columns(row.Columns))
		rows = append(rows, row)
		copy(rows[i+1:], rows[i:])
		rows[i] = row

		w.pool.Data[cf] = rows
	}

	return w
}
Beispiel #2
0
func NewTimestampAnnotation(value string, t time.Time, d time.Duration) *zipkin.Annotation {
	if t.IsZero() {
		t = time.Now()
	}
	var duration *int32
	if d != 0 {
		duration = thrift.Int32Ptr(int32(d / time.Microsecond))
	}

	return &zipkin.Annotation{
		Timestamp: t.UnixNano() / 1e3,
		Value:     value,
		Duration:  duration,
	}
}
Beispiel #3
0
func (w *writer) InsertTtl(cf string, row *Row, ttl int) Writer {
	t := now()
	for _, col := range row.Columns {
		tm := w.addWriter(cf, row.Key)
		c := cassandra.NewColumn()
		c.Name = col.Name
		c.Value = col.Value
		if ttl > 0 {
			c.TTL = thrift.Int32Ptr(int32(ttl))
		}
		if col.Timestamp != nil {
			c.Timestamp = col.Timestamp
		} else {
			c.Timestamp = &t
		}
		cs := cassandra.NewColumnOrSuperColumn()
		cs.Column = c
		tm.ColumnOrSupercolumn = cs
	}
	return w
}