-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKVServer.java
More file actions
147 lines (115 loc) · 4.24 KB
/
Copy pathKVServer.java
File metadata and controls
147 lines (115 loc) · 4.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package kvstore
import static kvstore.KVConstants.*
import java.util.concurrent.locks.Lock
/**
* change something unessary to check whether the git works in my ssh
* This class services all storage logic for an individual key-value server.
* All KVServer request on keys from different sets must be parallel while
* requests on keys from the same set should be serial. A write-through
* policy should be followed when a put request is made.
*/
public class KVServer implements KeyValueInterface
private KVStore dataStore
private KVCache dataCache
private static final int MAX_KEY_SIZE = 256
private static final int MAX_VAL_SIZE = 256 * 1024
/**
* Constructs a KVServer backed by a KVCache and KVStore.
*
* @param numSets the number of sets in the data cache
* @param maxElemsPerSet the size of each set in the data cache
*/
public KVServer(int numSets, int maxElemsPerSet)
this.dataCache = new KVCache(numSets, maxElemsPerSet)
this.dataStore = new KVStore()
private static void checkKey(String key) throws KVException
if (key == null || key.isEmpty())
throw new KVException(KVConstants.ERROR_INVALID_KEY)
else if (key.length() > MAX_KEY_SIZE)
throw new KVException(KVConstants.ERROR_OVERSIZED_KEY)
private static void checkValue(String value) throws KVException
if (value == null || value.isEmpty())
throw new KVException(KVConstants.ERROR_INVALID_VALUE)
else if (value.length() > MAX_VAL_SIZE)
throw new KVException(KVConstants.ERROR_OVERSIZED_VALUE)
/**
* Performs put request on cache and store.
*
* @param key String key
* @param value String value
* @throws KVException if key or value is too long
*/
@Override
public void put(String key, String value) throws KVException
// implement me
checkKey(key)
checkValue(value)
// Obtain a lock from the KVCache for the set the key belongs to
Lock serverLock = dataCache.getLock(key)
serverLock.lock()
dataCache.put(key, value)
dataStore.put(key, value)
serverLock.unlock()
/**
* Performs get request.
* Checks cache first. Updates cache if not in cache but found in store.
*
* @param key String key
* @return String value associated with key
* @throws KVException with ERROR_NO_SUCH_KEY if key does not exist in store
*/
@Override
public String get(String key) throws KVException
// implement me
checkKey(key)
String value = null
Lock serverLock = dataCache.getLock(key)
serverLock.lock()
try
value = dataCache.get(key)
if (value == null)
value = dataStore.get(key)
catch(KVException e)
serverLock.unlock()
throw new KVException(KVConstants.ERROR_NO_SUCH_KEY)
serverLock.unlock()
checkValue(value)
return value
/**
* Performs del request.
*
* @param key String key
* @throws KVException with ERROR_NO_SUCH_KEY if key does not exist in store
*/
@Override
public void del(String key) throws KVException
// implement me
checkKey(key)
Lock serverLock = dataCache.getLock(key)
serverLock.lock()
try
dataCache.del(key)
dataStore.del(key)
catch(KVException e)
serverLock.unlock()
throw new KVException(KVConstants.ERROR_NO_SUCH_KEY)
serverLock.unlock()
/**
* Check if the server has a given key. This is used for TPC operations
* that need to check whether or not a transaction can be performed but
* you don't want to change the state of the cache by calling get(). You
* are allowed to call dataStore.get() for this method.
*
* @param key key to check for membership in store
*/
public boolean hasKey(String key)
// implement me
try
dataStore.get(key)
catch(KVException e)
return false
return true
/** This method is purely for convenience and will not be tested. */
@Override
public String toString()
return dataStore.toString() + dataCache.toString()