PyORAm
[iotcloud.git] / PyORAM / src / pyoram / storage / block_storage_sftp.py
1 __all__ = ('BlockStorageSFTP',)
2
3 import logging
4 from AliTimer import *
5 from pyoram.util.misc import chunkiter
6 from pyoram.storage.block_storage import \
7      BlockStorageTypeFactory
8 from pyoram.storage.block_storage_file import \
9     BlockStorageFile
10
11 import time
12
13 log = logging.getLogger("pyoram")
14
15 class BlockStorageSFTP(BlockStorageFile):
16     """
17     A block storage device for accessing file data through
18     an SSH portal using Secure File Transfer Protocol (SFTP).
19     """
20
21     def __init__(self,
22                  storage_name,
23                  sshclient=None,
24                  **kwds):
25         if sshclient is None:
26             raise ValueError(
27                 "Can not open sftp block storage device "
28                 "without an ssh client.")
29         super(BlockStorageSFTP, self).__init__(
30             storage_name,
31             _filesystem=sshclient.open_sftp(),
32             **kwds)
33         self._sshclient = sshclient
34         self._f.set_pipelined()
35         self._timer = Foo.Instance();
36
37
38
39     #
40     # Define BlockStorageInterface Methods
41     #
42
43     def clone_device(self):
44         f = BlockStorageSFTP(self.storage_name,
45                              sshclient=self._sshclient,
46                              threadpool_size=0,
47                              ignore_lock=True)
48         f._pool = self._pool
49         f._close_pool = False
50         return f
51
52     #@classmethod
53     #def compute_storage_size(...)
54
55     @classmethod
56     def setup(cls,
57               storage_name,
58               block_size,
59               block_count,
60               sshclient=None,
61               threadpool_size=None,
62               **kwds):
63         if sshclient is None:
64             raise ValueError(
65                 "Can not setup sftp block storage device "
66                 "without an ssh client.")
67
68         with BlockStorageFile.setup(storage_name,
69                                     block_size,
70                                     block_count,
71                                     _filesystem=sshclient.open_sftp(),
72                                     threadpool_size=threadpool_size,
73                                     **kwds) as f:
74             pass
75         f._filesystem.close()
76
77         return BlockStorageSFTP(storage_name,
78                                 sshclient=sshclient,
79                                 threadpool_size=threadpool_size)
80
81     #@property
82     #def header_data(...)
83
84     #@property
85     #def block_count(...)
86
87     #@property
88     #def block_size(...)
89
90     #@property
91     #def storage_name(...)
92
93     #def update_header_data(...)
94
95     def close(self):
96         print("sftp close 1")
97         super(BlockStorageSFTP, self).close()
98         print("sftp close 2")
99         self._filesystem.close()
100         print("sftp close 3")
101
102     def read_blocks(self, indices):
103         self._check_async()
104         args = []
105         for i in indices:
106             assert 0 <= i < self.block_count
107             self._bytes_received += self.block_size
108             args.append((self._header_offset + i * self.block_size,
109                          self.block_size))
110
111
112         sTime = time.time();
113         self._timer.startTimer();
114         a = self._f.readv(args)
115         self._timer.endTimer();
116
117
118         # print("Reading Blocks SFTP......" + str(time.time() - sTime));
119         return a
120
121     def yield_blocks(self, indices, chunksize=100):
122         for chunk in chunkiter(indices, n=chunksize):
123             assert all(0 <= i <= self.block_count for i in chunk)
124             self._bytes_received += self.block_size * len(chunk)
125             args = [(self._header_offset + i * self.block_size,
126                      self.block_size)
127                     for i in chunk]
128
129
130             self._timer.startTimer();
131             a = self._f.readv(args)
132             self._timer.endTimer();
133
134             # print("Yield SFTP......");
135
136             for block in a:
137                 yield block
138
139     #def read_block(...)
140
141     #def write_blocks(...)
142
143     #def write_block(...)
144
145     #@property
146     #def bytes_sent(...)
147
148     #@property
149     #def bytes_received(...)
150
151 BlockStorageTypeFactory.register_device("sftp", BlockStorageSFTP)