package models
type Timeline struct {
modelImpl
MsgKeys []string
}
type TimelineType byte
const (
TimelineType_INBOX TimelineType = iota
TimelineType_SENT
)
func NewTimeline(id string) *Timeline {
t := &Timeline{}
t.id = id
return t
}
func (t *Timeline) AddMsg(msgKey string) {
t.MsgKeys = append(t.MsgKeys, msgKey)
}
func (t *Timeline) GetId() string {
return t.id
}
````
We'll be using the bucket `Users` to store our data. We won't be [using bucket types](/riak/kv/2.2.3/developing/usage/bucket-types) here, so we don't need to specify one.
To use these records to store data, we will first have to create a user
record. Then, when a user creates a message, we will append that message
to one or more timelines. If it's a private message, we'll append it to
the Recipient's `Inbox` timeline and to the User's own `Sent` timeline.
If it's a group message, we'll append it to the Group's timeline, as
well as to the User's `Sent` timeline.
#### Buckets and keys revisited
Now that we've worked out how we will differentiate data in the system,
let's figure out our bucket and key names.
The bucket names are straightforward. We can use `Users`, `Msgs`, and
`Timelines`. The key names, however, are a little trickier. In past
examples we've used sequential integers, but this presents a problem: we
would need a secondary service to hand out these IDs. This service could
easily be a future bottleneck in the system, so let's use a natural key.
Natural keys are a great fit for key/value systems because both humans
and computers can easily construct them when needed, and most of the
time they can be made unique enough for a KV store.
Bucket | Key Pattern | Example Key
:------|:------------|:-----------
`Users` | `<user_name>` | `joeuser`
`Msgs` | `<username>_<datetime>` | `joeuser_2014-03-06T02:05:13.223556Z`
`Timelines` | `<username>_<type>_<date>` | `joeuser_Sent_2014-03-06Z`<br /> `marketing_group_Inbox_2014-03-06Z` |
For the `Users` bucket, we can be certain that we will want each
username to be unique, so let's use the `username` as the key. For the
`Msgs` bucket, let's use a combination of the username and the posting
datetime in an [ISO 8601 Long](http://en.wikipedia.org/wiki/ISO_8601)
format. This combination gives us the pattern `<username>_<datetime>`,
which produces keys like `joeuser_2014-03-05T23:20:28Z`.
Now for `Timelines`, we need to differentiate between `Inbox` and `Sent`
timelines, so we can simply add that type into the key name. We will
also want to partition each collection object into some time period,
that way the object doesn't grow too large (see note below).
For `Timelines`, let's use the pattern `<username>_<type>_<date>` for
users and `<groupname>_Inbox_<date>` for groups, which will look like
`joeuser_Sent_2014-03-06Z` or `marketing_group_Inbox_2014-03-05Z`,
respectively.

Note

Riak performs best with objects under 1-2 MB. Objects larger than that can
hurt performance, especially if many siblings are being created. We will cover
siblings, sibling resolution, and sibling explosions in the next chapter.

While this set of repositories solves many of our problems, it is very
minimal and doesn’t cover all the edge cases. For instance, what happens
if two different people try to create a user with the same username?

Also, we can easily compute key names now, but how do we quickly look
up the last 10 messages a user sent? Many of these answers will be
application-dependent. If your application shows the last 10 messages in
reverse order, for example, you may want to store that set of data in
another collection object to make lookup faster. There are drawbacks to
every solution, but we recommend seeking out the key/value-based
solution first, as it will likely be the quickest.

So to recap, in this chapter we learned:

How to choose bucket names.

How to choose natural keys based on how we want to partition our data.