diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-06 21:02:17 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2014-01-06 21:02:17 +0400 |
commit | e36b714d8eb0444459ea7051feb7b1c9af4154df (patch) | |
tree | c9527cac2f1a53943d4a98faf0afaceb7ac348f0 /src | |
parent | 8916f4938f27078743f3f62376759f16bcda2f20 (diff) |
Add documentation to storage
Diffstat (limited to 'src')
-rw-r--r-- | src/System/Torrent/Storage.hs | 24 |
1 files changed, 22 insertions, 2 deletions
diff --git a/src/System/Torrent/Storage.hs b/src/System/Torrent/Storage.hs index 8c220721..6dda07e1 100644 --- a/src/System/Torrent/Storage.hs +++ b/src/System/Torrent/Storage.hs | |||
@@ -60,6 +60,7 @@ import Data.Torrent.Piece | |||
60 | import System.Torrent.FileMap as FM | 60 | import System.Torrent.FileMap as FM |
61 | 61 | ||
62 | 62 | ||
63 | -- | Some storage operations may throw an exception if misused. | ||
63 | data StorageFailure | 64 | data StorageFailure |
64 | -- | Occurs on a write operation if the storage has been opened | 65 | -- | Occurs on a write operation if the storage has been opened |
65 | -- using 'ReadOnly' mode. | 66 | -- using 'ReadOnly' mode. |
@@ -75,30 +76,45 @@ data StorageFailure | |||
75 | 76 | ||
76 | instance Exception StorageFailure | 77 | instance Exception StorageFailure |
77 | 78 | ||
78 | -- TODO validation | 79 | -- | Pieces store. |
79 | data Storage = Storage | 80 | data Storage = Storage |
80 | { mode :: !Mode | 81 | { mode :: !Mode |
81 | , pieceLen :: {-# UNPACK #-} !PieceSize | 82 | , pieceLen :: {-# UNPACK #-} !PieceSize |
82 | , fileMap :: {-# UNPACK #-} !FileMap | 83 | , fileMap :: {-# UNPACK #-} !FileMap |
83 | } | 84 | } |
84 | 85 | ||
85 | -- ResourceT ? | 86 | -- | Map torrent files: |
87 | -- | ||
88 | -- * when torrent first created use 'ReadWriteEx' mode; | ||
89 | -- | ||
90 | -- * when seeding, validation 'ReadOnly' mode. | ||
91 | -- | ||
86 | open :: Mode -> PieceSize -> FileLayout FileSize -> IO Storage | 92 | open :: Mode -> PieceSize -> FileLayout FileSize -> IO Storage |
87 | open mode s l = Storage mode s <$> mmapFiles mode l | 93 | open mode s l = Storage mode s <$> mmapFiles mode l |
88 | 94 | ||
95 | -- | Unmaps all files forcefully. It is recommended but not required. | ||
89 | close :: Storage -> IO () | 96 | close :: Storage -> IO () |
90 | close Storage {..} = unmapFiles fileMap | 97 | close Storage {..} = unmapFiles fileMap |
91 | 98 | ||
99 | -- | Normally you need to use 'Control.Monad.Trans.Resource.allocate'. | ||
92 | withStorage :: Mode -> PieceSize -> FileLayout FileSize | 100 | withStorage :: Mode -> PieceSize -> FileLayout FileSize |
93 | -> (Storage -> IO ()) -> IO () | 101 | -> (Storage -> IO ()) -> IO () |
94 | withStorage m s l = bracket (open m s l) close | 102 | withStorage m s l = bracket (open m s l) close |
95 | 103 | ||
104 | -- TODO allocateStorage? | ||
105 | |||
106 | -- | Count of pieces in the storage. | ||
96 | totalPieces :: Storage -> PieceCount | 107 | totalPieces :: Storage -> PieceCount |
97 | totalPieces Storage {..} = FM.size fileMap `sizeInBase` pieceLen | 108 | totalPieces Storage {..} = FM.size fileMap `sizeInBase` pieceLen |
98 | 109 | ||
99 | isValidIx :: PieceIx -> Storage -> Bool | 110 | isValidIx :: PieceIx -> Storage -> Bool |
100 | isValidIx i s = 0 <= i && i < totalPieces s | 111 | isValidIx i s = 0 <= i && i < totalPieces s |
101 | 112 | ||
113 | -- | Put piece data at the piece index by overwriting existing | ||
114 | -- data. | ||
115 | -- | ||
116 | -- This operation may throw 'StorageFailure'. | ||
117 | -- | ||
102 | writePiece :: Piece BL.ByteString -> Storage -> IO () | 118 | writePiece :: Piece BL.ByteString -> Storage -> IO () |
103 | writePiece p @ Piece {..} s @ Storage {..} | 119 | writePiece p @ Piece {..} s @ Storage {..} |
104 | | mode == ReadOnly = throwIO StorageIsRO | 120 | | mode == ReadOnly = throwIO StorageIsRO |
@@ -120,6 +136,10 @@ writePiece p @ Piece {..} s @ Storage {..} | |||
120 | pcount = totalPieces s | 136 | pcount = totalPieces s |
121 | offset = fromIntegral pieceIndex * fromIntegral pieceLen | 137 | offset = fromIntegral pieceIndex * fromIntegral pieceLen |
122 | 138 | ||
139 | -- | Read specific piece from storage. | ||
140 | -- | ||
141 | -- This operation may throw 'StorageFailure'. | ||
142 | -- | ||
123 | readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) | 143 | readPiece :: PieceIx -> Storage -> IO (Piece BL.ByteString) |
124 | readPiece pix s @ Storage {..} | 144 | readPiece pix s @ Storage {..} |
125 | | not (isValidIx pix s) = throwIO (InvalidIndex pix) | 145 | | not (isValidIx pix s) = throwIO (InvalidIndex pix) |