| 39 | } |
| 40 | |
| 41 | func (i *chunkRecord) Create(file string) (count int64, format string, err error) { |
| 42 | if i.t != "subset" { |
| 43 | tmpFilePath := fmt.Sprintf("%s/temp/%d%d.idx", conf.PATH_DATA, rand.Int(), rand.Int()) |
| 44 | |
| 45 | var f *os.File |
| 46 | f, err = os.Create(tmpFilePath) |
| 47 | if err != nil { |
| 48 | return |
| 49 | } |
| 50 | defer f.Close() |
| 51 | |
| 52 | format = "array" |
| 53 | eof := false |
| 54 | curr := int64(0) |
| 55 | count = 0 |
| 56 | buffer_pos := 0 // used to track the location in our byte array |
| 57 | |
| 58 | // Writing index file in 16MB chunks |
| 59 | var b [16777216]byte |
| 60 | for { |
| 61 | n, er := i.r.SeekChunk(curr, true) |
| 62 | if er != nil { |
| 63 | if er != io.EOF { |
| 64 | err = er |
| 65 | return |
| 66 | } |
| 67 | eof = true |
| 68 | } |
| 69 | |
| 70 | // Calculating position in byte array |
| 71 | x := (buffer_pos * 16) |
| 72 | if x == 16777216 { |
| 73 | f.Write(b[:]) |
| 74 | buffer_pos = 0 |
| 75 | x = 0 |
| 76 | } |
| 77 | y := x + 8 |
| 78 | z := x + 16 |
| 79 | |
| 80 | binary.LittleEndian.PutUint64(b[x:y], uint64(curr)) |
| 81 | if eof { |
| 82 | binary.LittleEndian.PutUint64(b[y:z], uint64(i.size-curr)) |
| 83 | } else { |
| 84 | binary.LittleEndian.PutUint64(b[y:z], uint64(n)) |
| 85 | } |
| 86 | curr += int64(n) |
| 87 | count += 1 |
| 88 | buffer_pos += 1 |
| 89 | |
| 90 | if eof { |
| 91 | break |
| 92 | } |
| 93 | } |
| 94 | if buffer_pos != 0 { |
| 95 | f.Write(b[:buffer_pos*16]) |
| 96 | } |
| 97 | err = os.Rename(tmpFilePath, file) |
| 98 | |