1 /*************************************************************************************************
2  * The update log API of Tokyo Tyrant
3  *                                                               Copyright (C) 2006-2010 FAL Labs
4  * This file is part of Tokyo Tyrant.
5  * Tokyo Tyrant is free software; you can redistribute it and/or modify it under the terms of
6  * the GNU Lesser General Public License as published by the Free Software Foundation; either
7  * version 2.1 of the License or any later version.  Tokyo Tyrant is distributed in the hope
8  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
10  * License for more details.
11  * You should have received a copy of the GNU Lesser General Public License along with Tokyo
12  * Tyrant; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
13  * Boston, MA 02111-1307 USA.
14  *************************************************************************************************/
15 
16 
17 #ifndef _TCULOG_H                        /* duplication check */
18 #define _TCULOG_H
19 
20 #if defined(__cplusplus)
21 #define __TCULOG_CLINKAGEBEGIN extern "C" {
22 #define __TCULOG_CLINKAGEEND }
23 #else
24 #define __TCULOG_CLINKAGEBEGIN
25 #define __TCULOG_CLINKAGEEND
26 #endif
27 __TCULOG_CLINKAGEBEGIN
28 
29 
30 #include <ttutil.h>
31 
32 
33 
34 /*************************************************************************************************
35  * API
36  *************************************************************************************************/
37 
38 
39 #define TCULSUFFIX     ".ulog"           /* suffix of update log files */
40 #define TCULMAGICNUM   0xc9              /* magic number of each command */
41 #define TCULMAGICNOP   0xca              /* magic number of NOP command */
42 #define TCULRMTXNUM    31                /* number of mutexes of records */
43 
44 typedef struct {                         /* type of structure for an update log */
45   pthread_mutex_t rmtxs[TCULRMTXNUM];    /* mutex for records */
46   pthread_rwlock_t rwlck;                /* mutex for operation */
47   pthread_cond_t cnd;                    /* condition variable */
48   pthread_mutex_t wmtx;                  /* mutex for waiting condition */
49   char *base;                            /* path of the base directory */
50   uint64_t limsiz;                       /* limit size */
51   int max;                               /* number of maximum ID */
52   int fd;                                /* current file descriptor */
53   uint64_t size;                         /* current size */
54   void *aiocbs;                          /* AIO tasks */
55   int aiocbi;                            /* index of AIO tasks */
56   uint64_t aioend;                       /* end offset of AIO tasks */
57 } TCULOG;
58 
59 typedef struct {                         /* type of structure for a log reader */
60   TCULOG *ulog;                          /* update log object */
61   uint64_t ts;                           /* beginning timestamp */
62   int num;                               /* number of current ID */
63   int fd;                                /* current file descriptor */
64   char *rbuf;                            /* record buffer */
65   int rsiz;                              /* size of the record buffer */
66 } TCULRD;
67 
68 typedef struct {                         /* type of structure for a replication */
69   int fd;                                /* file descriptor */
70   TTSOCK *sock;                          /* socket object */
71   char *rbuf;                            /* record buffer */
72   int rsiz;                              /* size of the record buffer */
73   uint16_t mid;                          /* master server ID number */
74 } TCREPL;
75 
76 
77 /* Create an update log object.
78    The return value is the new update log object. */
79 TCULOG *tculognew(void);
80 
81 
82 /* Delete an update log object.
83    `ulog' specifies the update log object. */
84 void tculogdel(TCULOG *ulog);
85 
86 
87 /* Set AIO control of an update log object.
88    `ulog' specifies the update log object.
89    If successful, the return value is true, else, it is false. */
90 bool tculogsetaio(TCULOG *ulog);
91 
92 
93 /* Open files of an update log object.
94    `ulog' specifies the update log object.
95    `base' specifies the path of the base directory.
96    `limsiz' specifies the limit size of each file.  If it is not more than 0, no limit is
97    specified.
98    If successful, the return value is true, else, it is false. */
99 bool tculogopen(TCULOG *ulog, const char *base, uint64_t limsiz);
100 
101 
102 /* Close files of an update log object.
103    `ulog' specifies the update log object.
104    If successful, the return value is true, else, it is false. */
105 bool tculogclose(TCULOG *ulog);
106 
107 
108 /* Get the mutex index of a record.
109    `ulog' specifies the update log object.
110    `kbuf' specifies the pointer to the region of the key.
111    `ksiz' specifies the size of the region of the key.
112    The return value is the mutex index of a record. */
113 int tculogrmtxidx(TCULOG *ulog, const char *kbuf, int ksiz);
114 
115 
116 /* Begin the critical section of an update log object.
117    `ulog' specifies the update log object.
118    `idx' specifies the index of the record lock.  -1 means to lock all.
119    If successful, the return value is true, else, it is false. */
120 bool tculogbegin(TCULOG *ulog, int idx);
121 
122 
123 /* End the critical section of an update log object.
124    `ulog' specifies the update log object.
125    `idx' specifies the index of the record lock.  -1 means to lock all.
126    If successful, the return value is true, else, it is false. */
127 bool tculogend(TCULOG *ulog, int idx);
128 
129 
130 /* Write a message into an update log object.
131    `ulog' specifies the update log object.
132    `ts' specifies the timestamp.  If it is 0, the current time is specified.
133    `sid' specifies the origin server ID of the message.
134    `mid' specifies the master server ID of the message.
135    `ptr' specifies the pointer to the region of the message.
136    `size' specifies the size of the region.
137    If successful, the return value is true, else, it is false. */
138 bool tculogwrite(TCULOG *ulog, uint64_t ts, uint32_t sid, uint32_t mid,
139                  const void *ptr, int size);
140 
141 
142 /* Create a log reader object.
143    `ulog' specifies the update log object.
144    `ts' specifies the beginning timestamp.
145    The return value is the new log reader object. */
146 TCULRD *tculrdnew(TCULOG *ulog, uint64_t ts);
147 
148 
149 /* Delete a log reader object.
150    `ulrd' specifies the log reader object. */
151 void tculrddel(TCULRD *ulrd);
152 
153 
154 /* Wait the next message is written.
155    `ulrd' specifies the log reader object. */
156 void tculrdwait(TCULRD *ulrd);
157 
158 
159 /* Read a message from a log reader object.
160    `ulrd' specifies the log reader object.
161    `sp' specifies the pointer to the variable into which the size of the region of the return
162    value is assigned.
163    `tsp' specifies the pointer to the variable into which the timestamp of the next message is
164    assigned.
165    `sidp' specifies the pointer to the variable into which the origin server ID of the next
166    message is assigned.
167    `midp' specifies the pointer to the variable into which the master server ID of the next
168    message is assigned.
169    If successful, the return value is the pointer to the region of the value of the next message.
170    `NULL' is returned if no record is to be read. */
171 const void *tculrdread(TCULRD *ulrd, int *sp, uint64_t *tsp, uint32_t *sidp, uint32_t *midp);
172 
173 
174 /* Store a record into an abstract database object.
175    `ulog' specifies the update log object.
176    `sid' specifies the origin server ID of the message.
177    `mid' specifies the master server ID of the message.
178    `adb' specifies the abstract database object.
179    `kbuf' specifies the pointer to the region of the key.
180    `ksiz' specifies the size of the region of the key.
181    `vbuf' specifies the pointer to the region of the value.
182    `vsiz' specifies the size of the region of the value.
183    If successful, the return value is true, else, it is false.
184    If a record with the same key exists in the database, it is overwritten. */
185 bool tculogadbput(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
186                   const void *kbuf, int ksiz, const void *vbuf, int vsiz);
187 
188 
189 /* Store a new record into an abstract database object.
190    `ulog' specifies the update log object.
191    `sid' specifies the origin server ID of the message.
192    `mid' specifies the master server ID of the message.
193    `adb' specifies the abstract database object.
194    `kbuf' specifies the pointer to the region of the key.
195    `ksiz' specifies the size of the region of the key.
196    `vbuf' specifies the pointer to the region of the value.
197    `vsiz' specifies the size of the region of the value.
198    If successful, the return value is true, else, it is false.
199    If a record with the same key exists in the database, this function has no effect. */
200 bool tculogadbputkeep(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
201                       const void *kbuf, int ksiz, const void *vbuf, int vsiz);
202 
203 
204 /* Concatenate a value at the end of the existing record in an abstract database object.
205    `ulog' specifies the update log object.
206    `sid' specifies the origin server ID of the message.
207    `mid' specifies the master server ID of the message.
208    `adb' specifies the abstract database object.
209    `kbuf' specifies the pointer to the region of the key.
210    `ksiz' specifies the size of the region of the key.
211    `vbuf' specifies the pointer to the region of the value.
212    `vsiz' specifies the size of the region of the value.
213    If successful, the return value is true, else, it is false.
214    If there is no corresponding record, a new record is created. */
215 bool tculogadbputcat(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
216                      const void *kbuf, int ksiz, const void *vbuf, int vsiz);
217 
218 
219 /* Concatenate a value at the end of the existing record and shift it to the left.
220    `ulog' specifies the update log object.
221    `sid' specifies the origin server ID of the message.
222    `mid' specifies the master server ID of the message.
223    `adb' specifies the abstract database object.
224    `kbuf' specifies the pointer to the region of the key.
225    `ksiz' specifies the size of the region of the key.
226    `vbuf' specifies the pointer to the region of the value.
227    `vsiz' specifies the size of the region of the value.
228    `width' specifies the width of the record.
229    If successful, the return value is true, else, it is false.
230    If there is no corresponding record, a new record is created. */
231 bool tculogadbputshl(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
232                      const void *kbuf, int ksiz, const void *vbuf, int vsiz, int width);
233 
234 
235 /* Remove a record of an abstract database object.
236    `ulog' specifies the update log object.
237    `sid' specifies the origin server ID of the message.
238    `mid' specifies the master server ID of the message.
239    `adb' specifies the abstract database object.
240    `kbuf' specifies the pointer to the region of the key.
241    `ksiz' specifies the size of the region of the key.
242    If successful, the return value is true, else, it is false. */
243 bool tculogadbout(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
244                   const void *kbuf, int ksiz);
245 
246 
247 /* Add an integer to a record in an abstract database object.
248    `ulog' specifies the update log object.
249    `sid' specifies the origin server ID of the message.
250    `mid' specifies the master server ID of the message.
251    `adb' specifies the abstract database object connected as a writer.
252    `kbuf' specifies the pointer to the region of the key.
253    `ksiz' specifies the size of the region of the key.
254    `num' specifies the additional value.
255    If successful, the return value is the summation value, else, it is `INT_MIN'.
256    If the corresponding record exists, the value is treated as an integer and is added to.  If no
257    record corresponds, a new record of the additional value is stored. */
258 int tculogadbaddint(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
259                     const void *kbuf, int ksiz, int num);
260 
261 
262 /* Add a real number to a record in an abstract database object.
263    `ulog' specifies the update log object.
264    `sid' specifies the origin server ID of the message.
265    `mid' specifies the master server ID of the message.
266    `adb' specifies the abstract database object connected as a writer.
267    `kbuf' specifies the pointer to the region of the key.
268    `ksiz' specifies the size of the region of the key.
269    `num' specifies the additional value.
270    If successful, the return value is the summation value, else, it is `NAN'.
271    If the corresponding record exists, the value is treated as a real number and is added to.  If
272    no record corresponds, a new record of the additional value is stored. */
273 double tculogadbadddouble(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
274                           const void *kbuf, int ksiz, double num);
275 
276 
277 /* Synchronize updated contents of an abstract database object with the file and the device.
278    `ulog' specifies the update log object.
279    `sid' specifies the origin server ID of the message.
280    `mid' specifies the master server ID of the message.
281    `adb' specifies the abstract database object.
282    If successful, the return value is true, else, it is false. */
283 bool tculogadbsync(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb);
284 
285 
286 /* Optimize the storage of an abstract database object.
287    `ulog' specifies the update log object.
288    `sid' specifies the origin server ID of the message.
289    `mid' specifies the master server ID of the message.
290    `adb' specifies the abstract database object.
291    `params' specifies the string of the tuning parameters, which works as with the tuning
292    of parameters the function `tcadbopen'.  If it is `NULL', it is not used.
293    If successful, the return value is true, else, it is false. */
294 bool tculogadboptimize(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb, const char *params);
295 
296 
297 /* Remove all records of an abstract database object.
298    `ulog' specifies the update log object.
299    `sid' specifies the origin server ID of the message.
300    `mid' specifies the master server ID of the message.
301    `adb' specifies the abstract database object.
302    If successful, the return value is true, else, it is false. */
303 bool tculogadbvanish(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb);
304 
305 
306 /* Call a versatile function for miscellaneous operations of an abstract database object.
307    `ulog' specifies the update log object.
308    `sid' specifies the origin server ID of the message.
309    `mid' specifies the master server ID of the message.
310    `adb' specifies the abstract database object.
311    `name' specifies the name of the function.
312    `args' specifies a list object containing arguments.
313    If successful, the return value is a list object of the result.  `NULL' is returned on failure.
314    All databases support "putlist", "outlist", and "getlist".  "putdup" is to store records.  It
315    receives keys and values one after the other, and returns an empty list.  "outlist" is to
316    remove records.  It receives keys, and returns an empty list.  "getlist" is to retrieve
317    records.  It receives keys, and returns values.  Because the object of the return value is
318    created with the function `tclistnew', it should be deleted with the function `tclistdel' when
319    it is no longer in use. */
320 TCLIST *tculogadbmisc(TCULOG *ulog, uint32_t sid, uint32_t mid, TCADB *adb,
321                       const char *name, const TCLIST *args);
322 
323 
324 /* Restore an abstract database object.
325    `adb' specifies the abstract database object.
326    `path' specifies the path of the update log directory.
327    `ts' specifies the beginning time stamp.
328    `con' specifies whether consistency checking is performed.
329    `ulog' specifies the update log object.
330    If successful, the return value is true, else, it is false. */
331 bool tculogadbrestore(TCADB *adb, const char *path, uint64_t ts, bool con, TCULOG *ulog);
332 
333 
334 /* Redo an update log message.
335    `adb' specifies the abstract database object.
336    `ptr' specifies the pointer to the region of the message.
337    `size' specifies the size of the region.
338    `ulog' specifies the update log object.
339    `sid' specifies the origin server ID of the message.
340    `mid' specifies the master server ID of the message.
341    `cp' specifies the pointer to the variable into which the result of consistency checking is
342    assigned.
343    If successful, the return value is true, else, it is false. */
344 bool tculogadbredo(TCADB *adb, const char *ptr, int size, TCULOG *ulog,
345                    uint32_t sid, uint32_t mid, bool *cp);
346 
347 
348 /* Create a replication object.
349    The return value is the new replicatoin object. */
350 TCREPL *tcreplnew(void);
351 
352 
353 /* Delete a replication object.
354    `repl' specifies the replication object. */
355 void tcrepldel(TCREPL *repl);
356 
357 
358 /* Open a replication object.
359    `repl' specifies the replication object.
360    `host' specifies the name or the address of the server.
361    `port' specifies the port number.
362    `sid' specifies the server ID of self messages.
363    If successful, the return value is true, else, it is false. */
364 bool tcreplopen(TCREPL *repl, const char *host, int port, uint64_t ts, uint32_t sid);
365 
366 
367 /* Close a remote database object.
368    `rdb' specifies the remote database object.
369    If successful, the return value is true, else, it is false. */
370 bool tcreplclose(TCREPL *repl);
371 
372 
373 /* Read a message from a replication object.
374    `repl' specifies the replication object.
375    `sp' specifies the pointer to the variable into which the size of the region of the return
376    value is assigned.
377    `tsp' specifies the pointer to the variable into which the timestamp of the next message is
378    assigned.
379    `sidp' specifies the pointer to the variable into which the origin server ID of the next
380    message is assigned.
381    If successful, the return value is the pointer to the region of the value of the next message.
382    `NULL' is returned if no record is to be read.  Empty string is returned when the no-operation
383    command has been received. */
384 const char *tcreplread(TCREPL *repl, int *sp, uint64_t *tsp, uint32_t *sidp);
385 
386 
387 
388 __TCULOG_CLINKAGEEND
389 #endif                                   /* duplication check */
390 
391 
392 /* END OF FILE */
393