Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how does a struct saved by the zoom #7

Closed
xiaoao opened this issue Apr 17, 2015 · 4 comments
Closed

how does a struct saved by the zoom #7

xiaoao opened this issue Apr 17, 2015 · 4 comments

Comments

@xiaoao
Copy link

xiaoao commented Apr 17, 2015

set, hash, or list? and how can I get it by redis-cli?

@albrow
Copy link
Owner

albrow commented Apr 17, 2015

Great question.

All the fields for the struct are stored in a hash with the key <modelName>:<id>. All primitive fields (int, bool, string, byte, rune, etc) are converted to strings. Everything else falls back to a binary encoding via the gob package.

When you call Save, the model's id is also added to a set of all ids for a given type. The key for the set is <modelName>:all.

Lastly if you add the zoom:"index" struct tag to any of the fields they will be saved in a sorted set with the key <modelName>:<fieldName>. These sorted set indexes are used for queries.

Here's a concrete example. Let's say you have a Person type:

type Person struct {
    Name string
    Age  int     `zoom:"index"`
    zoom.DefaultData
}

And you create and save a new person like so:

person := &Person{
    Name: "Bob",
    Age: 27,
}
if err := zoom.Save(person); err != nil {
    // handle err
}

When you call Save, a random id will be generated for the model. For the sake of simplicity let's say the id that was generated was "foo". Here are the redis commands that will be run:

# Add the fields to the main model hash
HMSET Person:foo Name Bob Age 27
# Add the model id to the set of all person ids
SADD Person:all foo
# Index the Age field in a sorted set
ZADD Person:Age 27 foo

Note that I left out string indexes because those are a bit more complicated. However, I'm happy to share if you are curious.

In order to get the fields for the model from redis, you need to know the id. In the above example, you could use HGETALL Person:foo. If you don't know the id, you can check the set of all ids. In the above example, this would be SMEMBERS Person:all.

Let me know if you have any other questions. Otherwise, I will add all this information to the documentation and then close this issue.

@albrow
Copy link
Owner

albrow commented Apr 17, 2015

By the way, in the rewrite branch there are methods to get all the keys that are used for storing a model in redis. I'm not sure when I will be able to merge it into master, but I'm working on the rewrite branch almost every day.

@xiaoao
Copy link
Author

xiaoao commented Apr 24, 2015

Thank you for your great answer. I 'm looking forward to the rewrite branch.

@albrow
Copy link
Owner

albrow commented May 4, 2015

@xiaoao I have written a wiki page which provides more information about how Zoom works under the hood. I may add more information to it later, but I believe it is sufficient. I'm going to close this issue for now. Please let me know if you feel there is any information missing from the wiki page.

@albrow albrow closed this as completed May 4, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants