| 46 | } |
| 47 | |
| 48 | func (node *Node) SetFileFromSubset(subsetIndices file.FormFile) (err error) { |
| 49 | // load parent node |
| 50 | var n *Node |
| 51 | n, err = Load(node.Subset.Parent.Id) |
| 52 | if err != nil { |
| 53 | return err |
| 54 | } |
| 55 | |
| 56 | if _, indexExists := n.Indexes[node.Subset.Parent.IndexName]; !indexExists { |
| 57 | return errors.New("Index '" + node.Subset.Parent.IndexName + "' does not exist for parent node.") |
| 58 | } |
| 59 | |
| 60 | parentIndexFile := n.IndexPath() + "/" + node.Subset.Parent.IndexName + ".idx" |
| 61 | if _, statErr := os.Stat(parentIndexFile); statErr != nil { |
| 62 | return errors.New("Could not stat index file for parent node where parent node = '" + node.Subset.Parent.Id + "' and index = '" + node.Subset.Parent.IndexName + "'.") |
| 63 | } |
| 64 | |
| 65 | // we default to "array" index format for backwards compatibility |
| 66 | indexFormat := "array" |
| 67 | if n.Indexes[node.Subset.Parent.IndexName].Format == "array" || n.Indexes[node.Subset.Parent.IndexName].Format == "matrix" { |
| 68 | indexFormat = n.Indexes[node.Subset.Parent.IndexName].Format |
| 69 | } |
| 70 | |
| 71 | if fi, statErr := os.Stat(subsetIndices.Path); statErr != nil { |
| 72 | return errors.New("Could not stat uploaded subset_indices file.") |
| 73 | } else { |
| 74 | if fi.Size() == 0 { |
| 75 | return errors.New("Uploaded subset_indices file is size zero. This is prohibited.") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | f, _ := os.Open(subsetIndices.Path) |
| 80 | defer f.Close() |
| 81 | idxer := index.NewSubsetIndexer(f) |
| 82 | coIndexPath := node.Path() + "/" + node.Id + ".subset.idx" |
| 83 | oIndexPath := node.Path() + "/idx/" + node.Subset.Parent.IndexName + ".idx" |
| 84 | |
| 85 | coCount, oCount, oSize, err := index.CreateSubsetNodeIndexes(&idxer, coIndexPath, oIndexPath, parentIndexFile, indexFormat, n.Indexes[node.Subset.Parent.IndexName].TotalUnits) |
| 86 | if err != nil { |
| 87 | return |
| 88 | } |
| 89 | |
| 90 | // this info refers to the compressed index for the subset node's data file |
| 91 | node.Subset.Index.Path = coIndexPath |
| 92 | node.Subset.Index.TotalUnits = coCount |
| 93 | node.Subset.Index.AvgUnitSize = oSize / coCount |
| 94 | node.Subset.Index.Format = "array" |
| 95 | node.File.Size = oSize |
| 96 | node.File.CreatedOn = time.Now() |
| 97 | |
| 98 | // this info is for the subset index that's been created in the index folder |
| 99 | node.Indexes[node.Subset.Parent.IndexName] = &IdxInfo{ |
| 100 | Type: "subset", |
| 101 | TotalUnits: oCount, |
| 102 | AvgUnitSize: oSize / oCount, |
| 103 | Format: indexFormat, |
| 104 | CreatedOn: time.Now(), |
| 105 | } |