-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcursor_log_lang_java.html
176 lines (176 loc) · 13.2 KB
/
cursor_log_lang_java.html
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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>WiredTiger: Log cursors in Java</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
$(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="wiredtiger.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectlogo"><a href="http://wiredtiger.com/"><img alt="Logo" src="LogoFinal-header.png" alt="WiredTiger" /></a></td>
<td style="padding-left: 0.5em;">
<div id="projectname">
 <span id="projectnumber">Version 2.8.0</span>
</div>
</td>
</tr>
</tbody>
</table>
</div>
<div class="banner">
<a href="https://github.com/wiredtiger/wiredtiger">Fork me on GitHub</a>
<a class="last" href="http://groups.google.com/group/wiredtiger-users">Join my user group</a>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.9.1 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="examples.html"><span>Examples</span></a></li>
<li><a href="community.html"><span>Community</span></a></li>
<li><a href="license.html"><span>License</span></a></li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('cursor_log_lang_java.html','');});
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Log cursors in Java </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>WiredTiger cursors provide access to data from a variety of sources, and one of these sources is the records in the transaction log files. Log files may not be present in every WiredTiger database, only databases that have been configured for logging using the <code>log</code> configuration for <code>wiredtiger.open</code>. In databases with log files, a log cursor provides access to the log records. Although log cursors are read-only, applications may store records in the log using Session.log_printf.</p>
<p>Each physical WiredTiger log record represents one or more operations in the database. When a log record represents more than a single operation in the database, all of the operations in a log record will be part of the same transaction, however, there is no corresponding guarantee that all operations in a transaction must appear in the same log record.</p>
<p>The following examples are taken from the complete example program ex_log.java.</p>
<p>To open a log cursor on the database:</p>
<div class="fragment"><div class="line"> cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(<span class="stringliteral">"log:"</span>, null, null);</div>
</div><!-- fragment --><p> A log cursor's key is a unique log record identifier, plus a int operation counter within that log record. When a log record maps one-to-one to a transaction (in other words, the returned log record has the only database operation the transaction made), the operation counter returned for the key will be zero.</p>
<p>The unique log record identifier maps to a WT_LSN data structure, which has two fields: WT_LSN::id, the log file identifier, and WT_LSN::offset, the offset of the log record in the log file.</p>
<p>Here is an example of getting the log cursor's key:</p>
<div class="fragment"><div class="line"> lsn.file = cursor.getKeyInt();</div>
<div class="line"> lsn.offset = cursor.getKeyLong();</div>
<div class="line"> opcount = cursor.getKeyInt();</div>
</div><!-- fragment --><p> The log cursor's value is comprised of six fields:</p>
<ul>
<li>a <code>long</code> transaction ID (set for commit records only, otherwise 0),</li>
<li>a <code>int</code> record type</li>
<li>a <code>int</code> operation type (set for commit records only, otherwise 0)</li>
<li>a <code>int</code> file id (if applicable, otherwise 0)</li>
<li>the operation key (commit records only, otherwise empty)</li>
<li>the operation value</li>
</ul>
<p>The transaction ID may not be unique across recovery, that is, closing and reopening the database may result in transaction IDs smaller than previously seen transaction IDs.</p>
<p>The record and operation types are taken from <a class="el" href="group__wt.html#log_types">log_types</a>; typically, the only record or operation type applications are concerned with is <a class="el" href="group__wt.html#ga8cf04a003979e7852209079d4093d783" title="message ">WT_LOGREC_MESSAGE</a>, which is a log record generated by the application.</p>
<p>The file ID may not be unique across recovery, that is, closing and reopening the database may result in file IDs changing. Additionally, there is currently no way to map file IDs to file names or higher-level objects.</p>
<p>Here is an example of getting the log cursor's value:</p>
<div class="fragment"><div class="line"> txnid = cursor.getValueLong();</div>
<div class="line"> rectype = cursor.getValueInt();</div>
<div class="line"> optype = cursor.getValueInt();</div>
<div class="line"> fileid = cursor.getValueInt();</div>
<div class="line"> logrec_key = cursor.getValueByteArray();</div>
<div class="line"> logrec_value = cursor.getValueByteArray();</div>
</div><!-- fragment --><p> For clarity, imagine a set of three log records:</p>
<ul>
<li>the first with a single operation,</li>
<li>the second with five operations,</li>
<li>the third with a single operation.</li>
</ul>
<p>The log cursor's Cursor.next call will return a total of seven records. The first time the log cursor will return a key with a unique log ID, a unique transaction ID, and an operation counter of 0. The next five returns from the log cursor will have a common log ID, a common transaction ID, and operation counters starting at 1 and ending at 5. The final return from the log cursor will again have a unique log ID, a unique transaction ID, and an operation counter of 0.</p>
<p>Here's a more complete example of walking the file file and displaying the results:</p>
<div class="fragment"><div class="line"> <span class="keyword">static</span> <span class="keywordtype">void</span></div>
<div class="line"> print_record(Lsn lsn, <span class="keywordtype">int</span> opcount,</div>
<div class="line"> <span class="keywordtype">int</span> rectype, <span class="keywordtype">int</span> optype, <span class="keywordtype">long</span> txnid, <span class="keywordtype">int</span> fileid,</div>
<div class="line"> byte[] key, byte[] value)</div>
<div class="line"> {</div>
<div class="line"> System.out.print(</div>
<div class="line"> <span class="stringliteral">"LSN ["</span> + lsn.file + <span class="stringliteral">"]["</span> + lsn.offset + <span class="stringliteral">"]."</span> + opcount +</div>
<div class="line"> <span class="stringliteral">": record type "</span> + rectype + <span class="stringliteral">" optype "</span> + optype +</div>
<div class="line"> <span class="stringliteral">" txnid "</span> + txnid + <span class="stringliteral">" fileid "</span> + fileid);</div>
<div class="line"> System.out.println(<span class="stringliteral">" key size "</span> + key.length +</div>
<div class="line"> <span class="stringliteral">" value size "</span> + value.length);</div>
<div class="line"> <span class="keywordflow">if</span> (rectype == wiredtiger.WT_LOGREC_MESSAGE)</div>
<div class="line"> System.out.println(<span class="stringliteral">"Application Record: "</span> + <span class="keyword">new</span> String(value));</div>
<div class="line"> }</div>
<div class="line"></div>
<div class="line"> <span class="comment">/*</span></div>
<div class="line"><span class="comment"> * simple_walk_log --</span></div>
<div class="line"><span class="comment"> * A simple walk of the log.</span></div>
<div class="line"><span class="comment"> */</span></div>
<div class="line"> <span class="keyword">static</span> <span class="keywordtype">int</span></div>
<div class="line"> simple_walk_log(Session session)</div>
<div class="line"> <span class="keywordflow">throws</span> WiredTigerException</div>
<div class="line"> {</div>
<div class="line"> Cursor cursor;</div>
<div class="line"> Lsn lsn = <span class="keyword">new</span> Lsn();</div>
<div class="line"> byte[] logrec_key, logrec_value;</div>
<div class="line"> <span class="keywordtype">long</span> txnid;</div>
<div class="line"> <span class="keywordtype">int</span> fileid, opcount, optype, rectype;</div>
<div class="line"> <span class="keywordtype">int</span> ret;</div>
<div class="line"></div>
<div class="line"> cursor = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#afb5b4a69c2c5cafe411b2b04fdc1c75d">open_cursor</a>(<span class="stringliteral">"log:"</span>, null, null);</div>
<div class="line"> <span class="keywordflow">while</span> ((ret = cursor.next()) == 0) {</div>
<div class="line"> lsn.file = cursor.getKeyInt();</div>
<div class="line"> lsn.offset = cursor.getKeyLong();</div>
<div class="line"> opcount = cursor.getKeyInt();</div>
<div class="line"> txnid = cursor.getValueLong();</div>
<div class="line"> rectype = cursor.getValueInt();</div>
<div class="line"> optype = cursor.getValueInt();</div>
<div class="line"> fileid = cursor.getValueInt();</div>
<div class="line"> logrec_key = cursor.getValueByteArray();</div>
<div class="line"> logrec_value = cursor.getValueByteArray();</div>
<div class="line"> print_record(lsn, opcount,</div>
<div class="line"> rectype, optype, txnid, fileid, logrec_key, logrec_value);</div>
<div class="line"> }</div>
<div class="line"> <span class="keywordflow">if</span> (ret == wiredtiger.WT_NOTFOUND)</div>
<div class="line"> ret = 0;</div>
<div class="line"> ret = cursor.close();</div>
<div class="line"> <span class="keywordflow">return</span> (ret);</div>
<div class="line"> }</div>
</div><!-- fragment --><p> The log cursor's key can be used to search for specific records in the log (assuming the record still exists and has not been archived), by setting the key and calling Cursor.search. However, it is not possible to search for a specific operation within a log record, and the key's operation counter is ignored when the key is set. The result of a search for a log record with more than one operation is always the first operation in the log record.</p>
<p>Here is an example of setting the log cursor's key:</p>
<div class="fragment"><div class="line"> cursor.putKeyInt(lsnsave.file);</div>
<div class="line"> cursor.putKeyLong(lsnsave.offset);</div>
</div><!-- fragment --><p> Log cursors are read-only, however applications can insert their own log records using Session.log_printf. Here is an example of adding an application record into the database log:</p>
<div class="fragment"><div class="line"> ret = session.<a class="code" href="struct_w_t___s_e_s_s_i_o_n.html#a504625d0b35da78f738d08530a409be9">log_printf</a>(<span class="stringliteral">"Wrote "</span> + record_count + <span class="stringliteral">" records"</span>);</div>
</div><!-- fragment --></div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="index.html">Reference Guide</a></li><li class="navelem"><a class="el" href="programming_lang_java.html">Writing WiredTiger applications in Java</a></li>
<li class="footer">Copyright (c) 2008-2016 MongoDB, Inc. All rights reserved. Contact <a href="mailto:[email protected]">[email protected]</a> for more information.</li>
</ul>
</div>
</body>
</html>