Skip to content

Commit

Permalink
Appease gometalinter and improve code format. Fixes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
albrow committed Jan 12, 2018
1 parent 8575d24 commit 69279b7
Show file tree
Hide file tree
Showing 24 changed files with 551 additions and 510 deletions.
40 changes: 20 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ Table of Contents
<!-- toc -->

- [Development Status](#development-status)
- [When is Zoom a Good Fit?](#when-is-zoom-a-good-fit-)
- [When is Zoom a Good Fit?](#when-is-zoom-a-good-fit)
- [Installation](#installation)
- [Initialization](#initialization)
- [Models](#models)
* [What is a Model?](#what-is-a-model-)
* [What is a Model?](#what-is-a-model)
* [Customizing Field Names](#customizing-field-names)
* [Creating Collections](#creating-collections)
* [Saving Models](#saving-models)
Expand All @@ -43,7 +43,7 @@ Table of Contents
* [Persistence](#persistence)
* [Atomicity](#atomicity)
* [Concurrent Updates and Optimistic Locking](#concurrent-updates-and-optimistic-locking)
- [Testing & Benchmarking](#testing---benchmarking)
- [Testing & Benchmarking](#testing--benchmarking)
* [Running the Tests](#running-the-tests)
* [Running the Benchmarks](#running-the-benchmarks)
- [Contributing](#contributing)
Expand Down Expand Up @@ -188,17 +188,17 @@ Models in Zoom are just structs which implement the `zoom.Model` interface:

``` go
type Model interface {
ModelId() string
SetModelId(string)
ModelID() string
SetModelID(string)
}
```

To clarify, all you have to do to implement the `Model` interface is add a getter and setter
for a unique id property.

If you want, you can embed `zoom.RandomId` to give your model all the
required methods. A struct with `zoom.RandomId` embedded will generate a pseudo-random id for itself
the first time the `ModelId` method is called iff it does not already have an id. The pseudo-randomly
If you want, you can embed `zoom.RandomID` to give your model all the
required methods. A struct with `zoom.RandomID` embedded will generate a pseudo-random id for itself
the first time the `ModelID` method is called iff it does not already have an id. The pseudo-randomly
generated id consists of the current UTC unix time with second precision, an incremented atomic
counter, a unique machine identifier, and an additional random string of characters. With ids generated
this way collisions are extremely unlikely.
Expand All @@ -213,7 +213,7 @@ A struct definition serves as a sort of schema for your model. Here's an example
type Person struct {
Name string
Age int
zoom.RandomId
zoom.RandomID
}
```

Expand All @@ -236,7 +236,7 @@ following struct definition:
type Person struct {
Name string `redis:"name"`
Age int `redis:"age"`
zoom.RandomId
zoom.RandomID
}
```

Expand Down Expand Up @@ -523,7 +523,7 @@ of all the available modifiers:
You can run a query with one of the following query finishers:

- [`Run`](http://godoc.org/github.com/albrow/zoom/#Query.Run)
- [`Ids`](http://godoc.org/github.com/albrow/zoom/#Query.Ids)
- [`IDs`](http://godoc.org/github.com/albrow/zoom/#Query.IDs)
- [`Count`](http://godoc.org/github.com/albrow/zoom/#Query.Count)
- [`RunOne`](http://godoc.org/github.com/albrow/zoom/#Query.RunOne)

Expand Down Expand Up @@ -613,10 +613,10 @@ To understand why optimistic locking is useful, consider the following code:

``` go
// likePost increments the number of likes for a post with the given id.
func likePost(postId string) error {
// Find the Post with the given postId
func likePost(postID string) error {
// Find the Post with the given postID
post := &Post{}
if err := Posts.Find(postId, post); err != nil {
if err := Posts.Find(postID, post); err != nil {
return err
}
// Increment the number of likes
Expand All @@ -641,16 +641,16 @@ You can use optimistic locking to avoid this problem. Here's the revised code:

```go
// likePost increments the number of likes for a post with the given id.
func likePost(postId string) error {
func likePost(postID string) error {
// Start a new transaction and watch the post key for changes. It's important
// to call Watch or WatchKey *before* finding the model.
tx := pool.NewTransaction()
if err := tx.WatchKey(Posts.ModelKey(postId)); err != nil {
if err := tx.WatchKey(Posts.ModelKey(postID)); err != nil {
return err
}
// Find the Post with the given postId
// Find the Post with the given postID
post := &Post{}
if err := Posts.Find(postId, post); err != nil {
if err := Posts.Find(postID, post); err != nil {
return err
}
// Increment the number of likes
Expand All @@ -676,9 +676,9 @@ particular problem might be best solved by the `HINCRBY` command.
```go
// likePost atomically increments the number of likes for a post with the given
// id and then returns the new number of likes.
func likePost(postId string) (int, error) {
func likePost(postID string) (int, error) {
// Get the key which is used to store the post in Redis
postKey := Posts.ModelKey(postId, post)
postKey := Posts.ModelKey(postID, post)
// Start a new transaction
tx := pool.NewTransaction()
// Add a command to increment the number of Likes. The HINCRBY command returns
Expand Down
32 changes: 19 additions & 13 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func BenchmarkConnection(b *testing.B) {
b.ResetTimer()
for i := 0; i < b.N; i++ {
conn := testPool.NewConn()
conn.Close()
_ = conn.Close()
}
}

Expand All @@ -35,7 +35,9 @@ func BenchmarkSet(b *testing.B) {
// BenchmarkGet sends the GET command after first sending SET
func BenchmarkGet(b *testing.B) {
conn := testPool.NewConn()
defer conn.Close()
defer func() {
_ = conn.Close()
}()
_, err := conn.Do("SET", "foo", "bar")
if err != nil {
b.Fatal(err)
Expand All @@ -51,10 +53,10 @@ func benchmarkCommand(b *testing.B, cmd string, args ...interface{}) {
for i := 0; i < b.N; i++ {
conn := testPool.NewConn()
if _, err := conn.Do(cmd, args...); err != nil {
conn.Close()
_ = conn.Close()
b.Fatal(err)
}
conn.Close()
_ = conn.Close()
}
}

Expand All @@ -68,7 +70,9 @@ func BenchmarkSave(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
testModels.Save(model)
if err := testModels.Save(model); err != nil {
b.Fatal(err)
}
}
}

Expand Down Expand Up @@ -103,14 +107,16 @@ func BenchmarkFind(b *testing.B) {
if err != nil {
b.Fatal(err)
}
ids := modelIds(Models(models))
ids := modelIDs(Models(models))
b.ResetTimer()

for i := 0; i < b.N; i++ {
b.StopTimer()
id := selectUnique(1, ids)[0]
b.StartTimer()
testModels.Find(id, &testModel{})
if err := testModels.Find(id, &testModel{}); err != nil {
b.Fatal(err)
}
}
}

Expand All @@ -124,15 +130,15 @@ func BenchmarkFind100(b *testing.B) {
if err != nil {
b.Fatal(err)
}
ids := modelIds(Models(models))
ids := modelIDs(Models(models))
b.ResetTimer()

// run the actual test
for i := 0; i < b.N; i++ {
b.StopTimer()
selectedIds := selectUnique(100, ids)
selectedIDs := selectUnique(100, ids)
t := testPool.NewTransaction()
for _, id := range selectedIds {
for _, id := range selectedIDs {
t.Find(testModels, id, &testModel{})
}
b.StartTimer()
Expand Down Expand Up @@ -193,7 +199,7 @@ func BenchmarkDelete(b *testing.B) {
b.Fatal(err)
}
b.StartTimer()
if _, err := testModels.Delete(models[0].ModelId()); err != nil {
if _, err := testModels.Delete(models[0].ModelID()); err != nil {
b.Fatal(err)
}
}
Expand All @@ -217,7 +223,7 @@ func BenchmarkDelete100(b *testing.B) {
t := testPool.NewTransaction()
for _, model := range models {
deleted := false
t.Delete(testModels, model.ModelId(), &deleted)
t.Delete(testModels, model.ModelID(), &deleted)
}
if err := t.Exec(); err != nil {
b.Fatal(err)
Expand Down Expand Up @@ -540,7 +546,7 @@ func selectUnique(num int, ids []string) []string {
}
}
results := make([]string, 0)
for key, _ := range selected {
for key := range selected {
results = append(results, key)
}
return results
Expand Down
26 changes: 13 additions & 13 deletions collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ func newUnindexedCollectionError(methodName string) error {
// Save writes a model (a struct which satisfies the Model interface) to the
// redis database. Save returns an error if the type of model does not match the
// registered Collection. To make a struct satisfy the Model interface, you can
// embed zoom.RandomId, which will generate pseudo-random ids for each model.
// embed zoom.RandomID, which will generate pseudo-random ids for each model.
func (c *Collection) Save(model Model) error {
t := c.pool.NewTransaction()
t.Save(c, model)
Expand All @@ -250,7 +250,7 @@ func (c *Collection) Save(model Model) error {
// redis database inside an existing transaction. save will set the err property
// of the transaction if the type of model does not match the registered
// Collection, which will cause exec to fail immediately and return the error.
// To make a struct satisfy the Model interface, you can embed zoom.RandomId,
// To make a struct satisfy the Model interface, you can embed zoom.RandomID,
// which will generate pseudo-random ids for each model. Any errors encountered
// will be added to the transaction and returned as an error when the
// transaction is executed.
Expand Down Expand Up @@ -287,7 +287,7 @@ func (t *Transaction) Save(c *Collection, model Model) {
}
// Add the model id to the set of all models for this collection
if c.index {
t.Command("SADD", redis.Args{c.IndexKey(), model.ModelId()}, nil)
t.Command("SADD", redis.Args{c.IndexKey(), model.ModelID()}, nil)
}
}

Expand Down Expand Up @@ -330,7 +330,7 @@ func (t *Transaction) saveNumericIndex(mr *modelRef, fs *fieldSpec) {
if err != nil {
t.setError(err)
}
t.Command("ZADD", redis.Args{indexKey, score, mr.model.ModelId()}, nil)
t.Command("ZADD", redis.Args{indexKey, score, mr.model.ModelID()}, nil)
}

// saveBooleanIndex adds commands to the transaction for saving a boolean
Expand All @@ -345,22 +345,22 @@ func (t *Transaction) saveBooleanIndex(mr *modelRef, fs *fieldSpec) {
if err != nil {
t.setError(err)
}
t.Command("ZADD", redis.Args{indexKey, score, mr.model.ModelId()}, nil)
t.Command("ZADD", redis.Args{indexKey, score, mr.model.ModelID()}, nil)
}

// saveStringIndex adds commands to the transaction for saving a string
// index on the given field. This includes removing the old index (if any).
func (t *Transaction) saveStringIndex(mr *modelRef, fs *fieldSpec) {
// Remove the old index (if any)
t.deleteStringIndex(mr.spec.name, mr.model.ModelId(), fs.redisName)
t.deleteStringIndex(mr.spec.name, mr.model.ModelID(), fs.redisName)
fieldValue := mr.fieldValue(fs.name)
for fieldValue.Kind() == reflect.Ptr {
if fieldValue.IsNil() {
return
}
fieldValue = fieldValue.Elem()
}
member := fieldValue.String() + nullString + mr.model.ModelId()
member := fieldValue.String() + nullString + mr.model.ModelID()
indexKey, err := mr.spec.fieldIndexKey(fs.name)
if err != nil {
t.setError(err)
Expand Down Expand Up @@ -432,7 +432,7 @@ func (t *Transaction) SaveFields(c *Collection, fieldNames []string, model Model
}
// Add the model id to the set of all models for this collection
if c.index {
t.Command("SADD", redis.Args{c.IndexKey(), model.ModelId()}, nil)
t.Command("SADD", redis.Args{c.IndexKey(), model.ModelID()}, nil)
}
}

Expand Down Expand Up @@ -466,7 +466,7 @@ func (t *Transaction) Find(c *Collection, id string, model Model) {
t.setError(fmt.Errorf("zoom: Error in Find or Transaction.Find: %s", err.Error()))
return
}
model.SetModelId(id)
model.SetModelID(id)
mr := &modelRef{
collection: c,
model: model,
Expand Down Expand Up @@ -505,7 +505,7 @@ func (t *Transaction) FindFields(c *Collection, id string, fieldNames []string,
return
}
// Set the model id and create a modelRef
model.SetModelId(id)
model.SetModelID(id)
mr := &modelRef{
collection: c,
spec: c.spec,
Expand Down Expand Up @@ -688,12 +688,12 @@ func (t *Transaction) deleteFieldIndexes(c *Collection, id string) {

// deleteNumericOrBooleanIndex removes the model from a numeric or boolean index for the given
// field. I.e. it removes the model id from a sorted set.
func (t *Transaction) deleteNumericOrBooleanIndex(fs *fieldSpec, ms *modelSpec, modelId string) {
func (t *Transaction) deleteNumericOrBooleanIndex(fs *fieldSpec, ms *modelSpec, modelID string) {
indexKey, err := ms.fieldIndexKey(fs.name)
if err != nil {
t.setError(err)
}
t.Command("ZREM", redis.Args{indexKey, modelId}, nil)
t.Command("ZREM", redis.Args{indexKey, modelID}, nil)
}

// DeleteAll deletes all the models of the given type in a single transaction. See
Expand Down Expand Up @@ -729,7 +729,7 @@ func (t *Transaction) DeleteAll(c *Collection, count *int) {
} else {
handler = NewScanIntHandler(count)
}
t.DeleteModelsBySetIds(c.IndexKey(), c.Name(), handler)
t.DeleteModelsBySetIDs(c.IndexKey(), c.Name(), handler)
}

// checkModelType returns an error iff model is not of the registered type that
Expand Down
Loading

0 comments on commit 69279b7

Please sign in to comment.