Skip to content

Commit c4459e6

Browse files
authored
Create gfs-client.py
1 parent 3715268 commit c4459e6

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

gfs-client.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'''
2+
Definition of BaseGFSClient
3+
class BaseGFSClient:
4+
def readChunk(self, filename, chunkIndex):
5+
# Read a chunk from GFS
6+
def writeChunk(self, filename, chunkIndex, content):
7+
# Write a chunk to GFS
8+
'''
9+
10+
11+
class GFSClient(BaseGFSClient):
12+
"""
13+
@param: chunkSize: An integer
14+
"""
15+
def __init__(self, chunkSize):
16+
# do intialization if necessary
17+
self.chunkSize = chunkSize
18+
super(GFSClient,self).__init__()
19+
20+
"""
21+
@param: filename: a file name
22+
@return: conetent of the file given from GFS
23+
"""
24+
def read(self, filename):
25+
# write your code here
26+
if filename + '-0' not in self.chunks:
27+
return None
28+
data = ''
29+
i = 0
30+
while True:
31+
d = self.readChunk(filename, i)
32+
if not d:
33+
break
34+
i += 1
35+
data += d
36+
return data
37+
38+
"""
39+
@param: filename: a file name
40+
@param: content: a string
41+
@return: nothing
42+
"""
43+
def write(self, filename, content):
44+
# write your code here
45+
if filename + '-0' in self.chunks:
46+
i = 0
47+
while True:
48+
f = filename + '-' + str(i)
49+
if f in self.chunks:
50+
del self.chunks[f]
51+
i += 1
52+
else:
53+
break
54+
bytes = len(content)
55+
chunks = int(bytes / self.chunkSize)
56+
if (chunks * self.chunkSize) < bytes:
57+
chunks += 1
58+
for i in range(chunks):
59+
begin = i * self.chunkSize
60+
end = begin + self.chunkSize
61+
if end > bytes:
62+
end = bytes
63+
self.writeChunk(filename, i, content[begin:end])

0 commit comments

Comments
 (0)