Skip to content

Commit

Permalink
Better memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelvigee committed Nov 19, 2020
1 parent 9fa411e commit e40b45a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ func (r *Paginator) Cursor(encoded string, typ CursorType, limit int) (Cursor, e
}

func (r *Paginator) cursorString(v map[string]interface{}) string {
valuesArr := make([]interface{}, 0)
for _, column := range r.Columns {
valuesArr = append(valuesArr, v[column.Name])
valuesArr := make([]interface{}, len(r.Columns))
for i, column := range r.Columns {
valuesArr[i] = v[column.Name]
}

data, err := msgpack.Marshal(valuesArr)
Expand Down
2 changes: 1 addition & 1 deletion gorm_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func rowMap(rows *sql.Rows) (map[string]interface{}, error) {

columns := make([]interface{}, len(cols))
columnPointers := make([]interface{}, len(cols))
for i, _ := range columns {
for i := range columns {
columnPointers[i] = &columns[i]
}

Expand Down
13 changes: 7 additions & 6 deletions paginator.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func fork(tx *gorm.DB) *gorm.DB {
func (r *Paginator) generateCondition(typ CursorType, values map[string]interface{}, op Op) (string, []interface{}) {
s := "1=0"

args := make([]interface{}, 0)
args := make([]interface{}, 0, len(r.Columns)*2)

for i := len(r.Columns) - 1; i >= 0; i-- {
column := r.Columns[i]
Expand All @@ -75,12 +75,12 @@ func (r *Paginator) Paginate(c Cursor, tx *gorm.DB) (*Response, error) {
limit := c.Limit

otx := fork(tx)
columnNames := make([]string, 0)
for _, column := range r.Columns {
columnNames := make([]string, len(r.Columns))
for i, column := range r.Columns {
order := column.Order(c.Type)

otx = otx.Order(fmt.Sprintf("%v %v", column.Name, order))
columnNames = append(columnNames, column.Name)
columnNames[i] = column.Name
}

tx.Logger.Info(tx.Statement.Context, "columns: %v", columnNames)
Expand Down Expand Up @@ -153,8 +153,9 @@ func (r *Paginator) Paginate(c Cursor, tx *gorm.DB) (*Response, error) {
var sc, ec string
var cursors []string
if len(nvalues) > 0 {
for _, v := range nvalues {
cursors = append(cursors, r.cursorString(v))
cursors = make([]string, len(nvalues))
for i, v := range nvalues {
cursors[i] = r.cursorString(v)
}

mi := len(nvalues) - 1
Expand Down

0 comments on commit e40b45a

Please sign in to comment.