-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.h
323 lines (265 loc) · 10.7 KB
/
server.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Copyright (c) 2008 MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
server.h
written by Elizabeth Reid
ereid@mit.edu
Version history:
This is version 1.03, released January 16, 2009
Main changes:
- Clarified behavior when getNext is called after get returns
KEY_NOT_FOUND
- Added a new structure, TxnState, that records information about the
current transaction. This structure is passed into all API calls.
- Required that transactions be able to span multiple indices.
- Changed the Payload in the Record structure so it is a fixed size array,
and clarified that the caller is responsible for allocating this structure
- Changed get/getNext so that they take in only a Record, with the key
field populated to indicate what record is to be retrieved.
- Clarified the return order of multiple records with duplicate keys when
get/getNext is called.
Older versions:
1.02, released December 16, 2008.
Further clarify key ordering, note that inserted payloads must be
copied.
1.01 Released December 14, 2008
Clarify issues with key ordering
1.0, Initial release, December 12, 2008.
*/
/* API that SIGMOD 2009 Contest participants must implement.
See http://db.csail.mit.edu/sigmod09contest/ for details
*/
#pragma once
#include <stdint.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
Implementation-specific data stored in this variable, used to identify a thread's state
*/
typedef struct IdxState IdxState;
/**
Implementation-specific data stored in this variable, used to identify a transaction across
Indices and store any information necessary for the implementation.
*/
typedef struct TxnState TxnState;
/**
Specifies the maximum size for a payload.
*/
#define MAX_PAYLOAD_LEN 100
/**
Specifies the maximum size for a varchar key.
*/
#define MAX_VARCHAR_LEN 128
/**
Status messages for outcomes of API calls.
*/
typedef enum ErrCode
{
SUCCESS,
DB_DNE,
DB_EXISTS,
DB_END,
KEY_NOTFOUND,
TXN_EXISTS,
TXN_DNE,
ENTRY_EXISTS,
ENTRY_DNE,
DEADLOCK,
FAILURE
} ErrCode;
/**
Three possible key types.
*/
typedef enum KeyType
{
SHORT,
INT,
VARCHAR
} KeyType;
/**
Stores the key value, whether it is a short, an int or a varchar.
@type defines what kind of key it is
*/
typedef struct
{
KeyType type;
union {
int32_t shortkey;
int64_t intkey;
char charkey[MAX_VARCHAR_LEN + 1];
}keyval;
} Key;
/**
The record information stored in an index
@value key: The lookup key for the record.
@value payload: The value stored under that key. It will be a
null-terminated C string of no more than 128 bytes.
*/
typedef struct
{
Key key;
char payload[MAX_PAYLOAD_LEN + 1];
} Record;
/**
Creates a new index data structure to be used by any thread.
@param type specifies what type of key the index will use
@param name a unique name to be used to identify this index in any process
@return ErrCode
SUCCESS if successfully created index.
DB_EXISTS if index with specified name already exists.
FAILURE if could not create index for some other reason.
*/
ErrCode create(KeyType type, char *name);
/**
Opens a specific index data structure to be used by this thread.
@param name the unique name specifying the index being opened
@param idxState returns the state handle for the index being opened
@return ErrCode
SUCCESS if successfully opened index.
DB_DNE if the name given does not have an associated DB that has been create()d.
FAILURE if DB exists but could not be opened for some other reason.
*/
ErrCode openIndex(const char *name, IdxState **idxState);
/**
Terminate use of current index by this thread.
@param idxState The state variable for the index being closed
@return ErrCode
SUCCESS if succesfully closed index.
DB_DNE is the DB never existed or was already closed by someone else.
FAILURE if could not close DB for some other reason.
**/
ErrCode closeIndex(IdxState *idxState);
/**
Signals the beginning of a transaction. Each thread can have only
one outstanding transaction running at a time.
@param txn Returns the transaction state for the new transaction.
@return ErrCode
SUCCESS if successfully began transaction.
TXN_EXISTS if there is already a transaction begun for this thread.
DEADLOCK if this transaction had to be aborted because of deadlock.
FAILURE if could not begin transaction for some other reason.
*/
ErrCode beginTransaction(TxnState **txn);
/**
Forces the current transaction to abort, rolling back all changes
made during the course of the transaction.
@param txn The state variable for the transaction being aborted.
@return ErrCode
SUCCESS if successfully aborted transaction.
TXN_DNE if there was no transaction to abort.
DEADLOCK if the abort failed because of deadlock.
FAILURE if could not abort transaction for some other reason.
*/
ErrCode abortTransaction(TxnState *txn);
/**
Signals the end of the current transaction, committing
all changes created in the transaction.
@param txn The state variable for the transaction being committed.
@return ErrCode
SUCCESS if successfully ended transaction.
TXN_DNE if there was no transaction currently open.
DEADLOCK if this transaction could not be closed because of deadlock.
FAILURE if could not end transaction for some other reason.
*/
ErrCode commitTransaction(TxnState *txn);
/**
Retrieve the first record associated with the given key value; if
more than one record exists with this key, return the first record
with this key. Contents of the retrieved record are copied into
the user supplied Record structure.
Records with the same key may be returned in any order, but it must
be that if there are n records with the same key k, a call to get
followed by n-1 calls to getNext will return all n records with key k.
If get returns KEY_NOTFOUND for a key k, the caller may invoke getNext
to find the first key after key k.
@param idxState The state variable for this thread
@param txn The transaction state to be used (or NULL if not in a transaction)
@param record Record containing the key being retrieved, into which the
payload is copied.
@return ErrCode
SUCCESS if successfully retrieved and returned unique record.
KEY_NOTFOUND if specified key value was not found in the DB.
DEADLOCK if this call could not complete because of deadlock.
FAILURE if could not retrieve unique record for some other reason.
*/
ErrCode get(IdxState *idxState, TxnState *txn, Record *record);
/**
Retrieve the record following the previous record retrieved by get or
getNext. If no such call has occurred since the current transaction
began, or if this is called from outside of a transaction, this
returns the first record in the index. Records are ordered in ascending
order by key. Records with the same key but different payloads
may be returned in any order.
If get returned KEY_NOT_FOUND for a key k, invoking getNext will
return the first key after k.
If the index is closed and reopened, or a new transaction has begun
since any previous call of get or getNext, getNext returns the first
record in the index.
@param idxState The state variable for the index whose next Record
is to be returned
@param txn The transaction state to be used (or NULL if not in a transaction)
@param record Record through which the next key/payload pair is returned
@return ErrCode
SUCCESS if successfully retrieved and returned the next record in the DB.
DB_END if reached the end of the DB.
DEADLOCK if this call could not complete because of deadlock.
FAILURE if could not retrieve next record for some other reason.
*/
ErrCode getNext(IdxState *idxState, TxnState *txn, Record *record);
/**
Insert a payload associated with the given key. An identical key can
be used multiple times, but only with unique payloads. If this is
called from outside of a transaction, it should commit immediately.
Records in an index are ordered in ascending order by key. Records
with the same key may be stored in any order.
The implementation is responsible for making a copy of payload
(e.g., it may not assume that the payload pointer continues
to be valid after this routine returns.)
@param idxState The state variable for this thread
@param txn The transaction state to be used (or NULL if not in a transaction)
@param k key value for insert
@param payload Pointer to the beginning of the payload string
@return ErrCode
SUCCESS if successfully inserted record into DB.
ENTRY_EXISTS if identical record already exists in DB.
DEADLOCK if this call could not complete because of deadlock.
FAILURE if could not insert entry for some other reason.
*/
ErrCode insertRecord(IdxState *idxState, TxnState *txn, Key *k, const char* payload);
/**
Remove the record associated with the given key from the index
structure. If a payload is specified in the Record, then the
key/payload pair specified is removed. Otherwise, the payload pointer
is a length 0 string and all records with the given key are removed from the
database. If this is called from outside of a transaction, it should
commit immediately.
@param idxState The state variable for this thread
@param txn The transaction state to be used (or NULL if not in a transaction)
@param record Record struct containing a Key and a char* payload
(or NULL pointer) describing what is to be deleted
@return ErrCode
SUCCESS if successfully deleted record from DB.
ENTRY_DNE if the specified key/payload pair could not be found in the DB.
KEY_NOTFOUND if the specified key could not be found in the DB, with only the key specified.
DEADLOCK if this call could not complete because of deadlock.
FAILURE if could not delete record for some other reason.
*/
ErrCode deleteRecord(IdxState *idxState, TxnState *txn, Record *record);
#ifdef __cplusplus
}
#endif