| 56 | } |
| 57 | |
| 58 | func (tree RevTree) MarshalJSON() ([]byte, error) { |
| 59 | n := len(tree) |
| 60 | rep := revTreeList{ |
| 61 | Revs: make([]string, n), |
| 62 | Parents: make([]int, n), |
| 63 | } |
| 64 | revIndexes := map[string]int{"": -1} |
| 65 | |
| 66 | i := 0 |
| 67 | for _, info := range tree { |
| 68 | revIndexes[info.ID] = i |
| 69 | rep.Revs[i] = info.ID |
| 70 | if info.Body != nil || info.BodyKey != "" { |
| 71 | // Marshal either the BodyKey or the Body, depending on whether a BodyKey is specified |
| 72 | if info.BodyKey == "" { |
| 73 | if rep.BodyMap == nil { |
| 74 | rep.BodyMap = make(map[string]string, 1) |
| 75 | } |
| 76 | rep.BodyMap[strconv.FormatInt(int64(i), 10)] = string(info.Body) |
| 77 | } else { |
| 78 | if rep.BodyKeyMap == nil { |
| 79 | rep.BodyKeyMap = make(map[string]string) |
| 80 | } |
| 81 | rep.BodyKeyMap[strconv.FormatInt(int64(i), 10)] = info.BodyKey |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | // non-winning leaf revisions |
| 86 | if len(info.Channels) > 0 { |
| 87 | if rep.ChannelsMap == nil { |
| 88 | rep.ChannelsMap = make(map[string]base.Set, 1) |
| 89 | } |
| 90 | rep.ChannelsMap[strconv.FormatInt(int64(i), 10)] = info.Channels |
| 91 | } |
| 92 | |
| 93 | if info.Deleted { |
| 94 | if rep.Deleted == nil { |
| 95 | rep.Deleted = make([]int, 0, 1) |
| 96 | } |
| 97 | rep.Deleted = append(rep.Deleted, i) |
| 98 | } |
| 99 | if info.HasAttachments { |
| 100 | if rep.HasAttachments == nil { |
| 101 | rep.HasAttachments = make([]int, 0, 1) |
| 102 | } |
| 103 | rep.HasAttachments = append(rep.HasAttachments, i) |
| 104 | } |
| 105 | i++ |
| 106 | } |
| 107 | |
| 108 | for i, revid := range rep.Revs { |
| 109 | parentRevId := tree[revid].Parent |
| 110 | parentRevIndex, ok := revIndexes[parentRevId] |
| 111 | if ok { |
| 112 | rep.Parents[i] = parentRevIndex |
| 113 | } else { |
| 114 | // If the parent revision does not exist in the revtree due to being a dangling parent, then |
| 115 | // consider this a root node and set the parent index to -1 |