-
Notifications
You must be signed in to change notification settings - Fork 818
/
memory.go
214 lines (186 loc) · 4.71 KB
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package xlsx
import (
"fmt"
)
type MemoryRow struct {
row *Row
maxCol int
cells []*Cell
}
func makeMemoryRow(sheet *Sheet) *MemoryRow {
mr := &MemoryRow{
row: new(Row),
maxCol: -1,
}
mr.row.Sheet = sheet
mr.row.cellStoreRow = mr
sheet.setCurrentRow(mr.row)
return mr
}
func (mr *MemoryRow) Updatable() {
// Do nothing
}
func (mr *MemoryRow) CellUpdatable(c *Cell) {
// Do nothing
}
func (mr *MemoryRow) AddCell() *Cell {
cell := newCell(mr.row, mr.maxCol+1)
mr.PushCell(cell)
return cell
}
func (mr *MemoryRow) PushCell(c *Cell) {
mr.growCellsSlice(c.num + 1)
mr.cells[c.num] = c
}
func (mr *MemoryRow) growCellsSlice(newSize int) {
if newSize > (mr.maxCol + 1) {
mr.maxCol = (newSize - 1)
}
capacity := cap(mr.cells)
if newSize > capacity {
newCap := 2 * capacity
if newSize > newCap {
newCap = newSize
}
capacity = newCap
}
newSlice := make([]*Cell, newSize, capacity)
copy(newSlice, mr.cells)
mr.cells = newSlice
}
func (mr *MemoryRow) GetCell(colIdx int) *Cell {
if colIdx >= len(mr.cells) {
cell := newCell(mr.row, colIdx)
mr.growCellsSlice(colIdx + 1)
mr.cells[colIdx] = cell
return cell
}
cell := mr.cells[colIdx]
if cell == nil {
cell = newCell(mr.row, colIdx)
mr.cells[colIdx] = cell
}
return cell
}
func (mr *MemoryRow) ForEachCell(cvf CellVisitorFunc, option ...CellVisitorOption) error {
flags := &cellVisitorFlags{}
for _, opt := range option {
opt(flags)
}
fn := func(ci int, c *Cell) error {
if c == nil {
if flags.skipEmptyCells {
return nil
}
c = mr.GetCell(ci)
}
if !c.Modified() && flags.skipEmptyCells {
return nil
}
c.Row = mr.row
return cvf(c)
}
for ci, cell := range mr.cells {
err := fn(ci, cell)
if err != nil {
return err
}
}
cellCount := len(mr.cells)
var c *Cell
if !flags.skipEmptyCells {
for ci := cellCount; ci < mr.row.Sheet.MaxCol; ci++ {
c = mr.GetCell(ci)
err := cvf(c)
if err != nil {
return err
}
}
}
return nil
}
// MaxCol returns the index of the rightmost cell in the row's column.
func (mr *MemoryRow) MaxCol() int {
return mr.maxCol
}
// CellCount returns the total number of cells in the row.
func (mr *MemoryRow) CellCount() int {
return mr.maxCol + 1
}
// MemoryCellStore is the default CellStore - it holds all rows and
// cells in system memory. This is fast, right up until you run out
// of memory ;-)
type MemoryCellStore struct {
rows map[string]*Row
}
// UseMemoryCellStore is a FileOption that makes all Sheet instances
// for a File use memory as their backing store. This is the default
// backing store. You can use this option when you are comfortable
// keeping the contents of each Sheet in memory. This is faster than
// using a disk backed store, but can easily use a large amount of
// memory and, if you exhaust the available system memory, it'll
// actualy be slower than using a disk backed store (e.g. DiskV).
func UseMemoryCellStore(f *File) {
f.cellStoreConstructor = NewMemoryCellStore
}
// NewMemoryCellStore returns a pointer to a newly allocated MemoryCellStore
func NewMemoryCellStore() (CellStore, error) {
cs := &MemoryCellStore{
rows: make(map[string]*Row),
}
return cs, nil
}
// Close is nullOp for the MemoryCellStore, but we have to comply with
// the interface.
func (mcs *MemoryCellStore) Close() error {
return nil
}
// ReadRow returns a Row identfied by the given key.
func (mcs *MemoryCellStore) ReadRow(key string, s *Sheet) (*Row, error) {
r, ok := mcs.rows[key]
if !ok {
return nil, NewRowNotFoundError(key, "No such row")
}
return r, nil
}
// WriteRow pushes the Row to the MemoryCellStore.
func (mcs *MemoryCellStore) WriteRow(r *Row) error {
if r != nil {
key := r.key()
mcs.rows[key] = r
}
return nil
}
// MoveRow moves the persisted Row's position in the sheet.
func (mcs *MemoryCellStore) MoveRow(r *Row, index int) error {
oldKey := r.key()
r.num = index
newKey := r.key()
if _, exists := mcs.rows[newKey]; exists {
return fmt.Errorf("target index for row (%d) would overwrite a row already exists", index)
}
mcs.rows[newKey] = r
delete(mcs.rows, oldKey)
return nil
}
// RemoveRow removes a row from the sheet, it doesn't specifically
// move any following rows, leaving this decision to the user.
func (mcs *MemoryCellStore) RemoveRow(key string) error {
r, ok := mcs.rows[key]
if ok {
r.Sheet.setCurrentRow(nil)
delete(mcs.rows, key)
}
return nil
}
// MakeRowWithLen returns an empty Row, with a preconfigured starting length.
func (mcs *MemoryCellStore) MakeRowWithLen(sheet *Sheet, len int) *Row {
mr := makeMemoryRow(sheet)
mr.maxCol = len - 1
mr.growCellsSlice(len)
return mr.row
}
// MakeRow returns an empty Row
func (mcs *MemoryCellStore) MakeRow(sheet *Sheet) *Row {
return makeMemoryRow(sheet).row
}