To avoid duplicating fields and making the code easier to follow.
Remove all of `FSNode` previous fields in favor on a single `pb.Data` structure
that is not exported. Accessor methods are added only for the necessary internal
fields. This takes up more memory, `pb.Data` is always created inside `FSNode`
and it stays there instead of just being created and destroyed during the
(un)marshal operations.
The removed fields `Data`, `blocksizes` and `Type` had a direct counterpart in
the embedded `pb.Data` structure, in contrast (only) the `subtotal` field
doesn't have one, it was used as a temporary accumulator to track the
`Filesize`, which is now being kept updated on every modification (to ensure the
entire `FSNode` is always at a valid state), so `subtotal` could just be removed
without the addition of any other field (this temporary accumulator was
obscuring how `Filesize` was computed).
To keep `Filesize` up to date a method was added (`UpdateFilesize()`) to adjust
its value in the two places where the file size could be modified, when changing
its data (in `SetData()`, accessor method added) and when adding or removing
child nodes (in `AddBlockSize()` and `RemoveBlockSize()`).
A constructor method was added (`NewFSNode()`) to initialize the required
fields, like `Type` which is explicitly set, this deprecates the previous
methodology of just calling `new(FSNode)` and relying in the default value of
`pb.Data_DataType` (`Data_Raw`) to avoid an explicit assignment. Also,
`Filesize` is initialized to avoid being left with a `nil` value before
marshaling empty nodes, which would result in a different hash from previous
versions, to be backwards compatible. Previous versions of `GetBytes()` always
set the `Filesize` value, even though it is reflected as an `optional` field in
the `.proto` file (this may be an inaccurate field rule).
Without the duplicated fields the functions `GetBytes()` and `FSNodeFromBytes()`
are now reduced to simple `Marshal()` and `Unmarshal()` operations respectively.
License: MIT
Signed-off-by:
Lucas Molas <schomatis@gmail.com>
| 名称 |
最后提交
|
最后更新 |
|---|---|---|
| .gx | 正在载入提交数据... | |
| Godeps | 正在载入提交数据... | |
| assets | 正在载入提交数据... | |
| bin | 正在载入提交数据... | |
| blocks/blockstoreutil | 正在载入提交数据... | |
| blockservice | 正在载入提交数据... | |
| ci | 正在载入提交数据... | |
| cmd | 正在载入提交数据... | |
| commands | 正在载入提交数据... | |
| core | 正在载入提交数据... | |
| coverage | 正在载入提交数据... | |
| docs | 正在载入提交数据... | |
| exchange | 正在载入提交数据... | |
| filestore | 正在载入提交数据... | |
| fuse | 正在载入提交数据... | |
| importer | 正在载入提交数据... | |
| keystore | 正在载入提交数据... | |
| merkledag | 正在载入提交数据... | |
| mfs | 正在载入提交数据... | |
| misc | 正在载入提交数据... | |
| mk | 正在载入提交数据... | |
| namesys | 正在载入提交数据... | |
| p2p | 正在载入提交数据... | |
| path | 正在载入提交数据... | |
| pin | 正在载入提交数据... | |
| plugin | 正在载入提交数据... | |
| repo | 正在载入提交数据... | |
| tar | 正在载入提交数据... | |
| test | 正在载入提交数据... | |
| thirdparty | 正在载入提交数据... | |
| unixfs | 正在载入提交数据... | |
| .codeclimate.yml | 正在载入提交数据... | |
| .dockerignore | 正在载入提交数据... | |
| .gitattributes | 正在载入提交数据... | |
| .gitignore | 正在载入提交数据... | |
| .travis.yml | 正在载入提交数据... | |
| CHANGELOG.md | 正在载入提交数据... | |
| CODEOWNERS | 正在载入提交数据... | |
| Dockerfile | 正在载入提交数据... | |
| Dockerfile.fast | 正在载入提交数据... | |
| ISSUE_TEMPLATE.md | 正在载入提交数据... | |
| LICENSE | 正在载入提交数据... | |
| Makefile | 正在载入提交数据... | |
| README.md | 正在载入提交数据... | |
| Rules.mk | 正在载入提交数据... | |
| appveyor.yml | 正在载入提交数据... | |
| circle.yml | 正在载入提交数据... | |
| codecov.yml | 正在载入提交数据... | |
| contribute.md | 正在载入提交数据... | |
| dev.md | 正在载入提交数据... | |
| doc.go | 正在载入提交数据... | |
| package.json | 正在载入提交数据... |