lbry-rocksdb/tutorial/index.html
2019-04-29 21:26:12 +08:00

181 lines
No EOL
12 KiB
HTML

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Basic Usage of python-rocksdb &#8212; python-rocksdb 0.6.7 documentation</title>
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="../_static/language_data.js"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Changelog" href="../changelog.html" />
<link rel="prev" title="Installing" href="../installation.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="basic-usage-of-python-rocksdb">
<h1>Basic Usage of python-rocksdb<a class="headerlink" href="#basic-usage-of-python-rocksdb" title="Permalink to this headline"></a></h1>
<div class="section" id="wrtie-batch">
<h2>Wrtie Batch<a class="headerlink" href="#wrtie-batch" title="Permalink to this headline"></a></h2>
<p>It is also possible to gather modifications and
apply them in a single operation</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">update</span> <span class="o">=</span> <span class="n">pyrocksdb</span><span class="o">.</span><span class="n">WriteBatch</span><span class="p">()</span>
<span class="c1"># collect the operations</span>
<span class="n">update</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;key1&#39;</span><span class="p">,</span> <span class="s1">&#39;value1&#39;</span><span class="p">)</span>
<span class="n">update</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="s1">&#39;key2&#39;</span><span class="p">,</span> <span class="s1">&#39;value2&#39;</span><span class="p">)</span>
<span class="n">update</span><span class="o">.</span><span class="n">delete</span><span class="p">(</span><span class="s1">&#39;key1&#39;</span><span class="p">)</span>
<span class="n">opts</span> <span class="o">=</span> <span class="n">pyrocksdb</span><span class="o">.</span><span class="n">WriteOptions</span><span class="p">()</span>
<span class="c1"># Apply these operatios in a single operation</span>
<span class="n">s</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="n">update</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">s</span><span class="o">.</span><span class="n">ok</span><span class="p">()</span>
<span class="n">opts</span> <span class="o">=</span> <span class="n">pyrocksdb</span><span class="o">.</span><span class="n">ReadOptions</span><span class="p">()</span>
<span class="n">blob</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="s1">&#39;key1&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">blob</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">is_not_found</span><span class="p">()</span>
<span class="n">blob</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="s1">&#39;key2&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">blob</span><span class="o">.</span><span class="n">status</span><span class="o">.</span><span class="n">ok</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">blob</span><span class="o">.</span><span class="n">data</span> <span class="o">==</span> <span class="s1">&#39;value2&#39;</span>
<span class="n">db</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="iteration">
<h2>Iteration<a class="headerlink" href="#iteration" title="Permalink to this headline"></a></h2>
<p>Iterators behave slightly different than expected. Per default they are not
valid. So you have to call one of its seek methods first</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">opts</span> <span class="o">=</span> <span class="n">pyrocksdb</span><span class="o">.</span><span class="n">WriteOptions</span><span class="p">()</span>
<span class="n">s</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;key1&#39;</span><span class="p">:</span> <span class="s1">&#39;value1&#39;</span><span class="p">,</span> <span class="s1">&#39;key2&#39;</span><span class="p">:</span> <span class="s1">&#39;value2&#39;</span><span class="p">,</span> <span class="s1">&#39;key3&#39;</span><span class="p">:</span> <span class="s1">&#39;value3&#39;</span><span class="p">}</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="n">db</span><span class="o">.</span><span class="n">put</span><span class="p">(</span><span class="n">opts</span><span class="p">,</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span>
<span class="n">opts</span> <span class="o">=</span> <span class="n">pyrocksdb</span><span class="o">.</span><span class="n">ReadOptions</span><span class="p">()</span>
<span class="n">it</span> <span class="o">=</span> <span class="n">db</span><span class="o">.</span><span class="n">iterator</span><span class="p">(</span><span class="n">opts</span><span class="p">)</span>
<span class="c1"># call this first</span>
<span class="n">it</span><span class="o">.</span><span class="n">seek_to_first</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">status</span><span class="p">()</span><span class="o">.</span><span class="n">ok</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
<span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">s</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">key</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">==</span> <span class="n">k</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">value</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">==</span> <span class="n">v</span>
<span class="n">it</span><span class="o">.</span><span class="n">next</span><span class="p">()</span>
<span class="k">assert</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
<span class="n">it</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="s1">&#39;key1&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">key</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;key1&#39;</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">value</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;value1&#39;</span>
<span class="n">it</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="s1">&#39;key2&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">key</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;key2&#39;</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">value</span><span class="p">()</span><span class="o">.</span><span class="n">data</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;value2&#39;</span>
<span class="n">it</span><span class="o">.</span><span class="n">seek</span><span class="p">(</span><span class="s1">&#39;key4&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
<span class="n">it</span><span class="o">.</span><span class="n">seek_for_prev</span><span class="p">(</span><span class="s1">&#39;key0&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="ow">not</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
<span class="n">it</span><span class="o">.</span><span class="n">seek_for_prev</span><span class="p">(</span><span class="s1">&#39;key4&#39;</span><span class="p">)</span>
<span class="k">assert</span> <span class="n">it</span><span class="o">.</span><span class="n">valid</span><span class="p">()</span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">python-rocksdb</a></h1>
<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../installation.html">Instructions how to install</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Tutorial</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#wrtie-batch">Wrtie Batch</a></li>
<li class="toctree-l2"><a class="reference internal" href="#iteration">Iteration</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../changelog.html">Changelog</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li>Previous: <a href="../installation.html" title="previous chapter">Installing</a></li>
<li>Next: <a href="../changelog.html" title="next chapter">Changelog</a></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" />
<input type="submit" value="Go" />
</form>
</div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
&copy;2014, sh.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 2.0.1</a>
&amp; <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
|
<a href="../_sources/tutorial/index.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>