blob: 08dcf1ad094df9a1740f060b51a6b25357d05afc [file] [log] [blame]
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta http-equiv="CACHE-CONTROL" content="NO-CACHE">
<meta charset="UTF-8">
<title>Code Usage</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta name="generator" content="Jekyll v2.4.0">
<link rel="stylesheet" href="/css/font-awesome.min.css">
<link rel="stylesheet" href="/css/screen.css">
<link rel="icon" type="image/x-icon" href="/favicon.ico">
<script src="/js/jquery-3.2.1.min.js"></script>
<!--[if lt IE 9]>
<script src="/js/html5shiv.min.js"></script>
<script src="/js/respond.min.js"></script>
<![endif]-->
<script src="/js/screen.js"></script>
<script type="text/javascript" src="/assets/javascripts/bundle.js" charset="utf-8"></script>
</head>
<body>
<header role="banner">
<nav class="mobile-nav show-on-mobiles">
<ul>
<li class="">
<a href="/">Home</a>
</li>
<li class="current">
<a href="/docs/">Doc<span class="show-on-mobiles">s</span><span class="hide-on-mobiles">umentation</span></a>
</li>
</li>
<li class="">
<a href="/downloads/">Downloads</a>
</li>
<li class="">
<a href="/talks/">Talks</a>
</li>
<li class="">
<a href="/news/">News</a>
</li>
<li class="">
<a href="/develop/">Develop</a>
</li>
<li class="">
<a href="/help/">Help</a>
</li>
</ul>
</nav>
<div class="grid">
<div class="unit one-third center-on-mobiles">
<h1>
<a href="/">
<span class="sr-only">Apache Mnemonic</span>
<img src="/img/mnemonic_logo_v2.png" width="289" alt="Mnemonic Logo" class="logo">
</a>
<a href="https://www.apache.org/events/current-event.html">
<img src="https://www.apache.org/events/current-event-234x60.png"/>
</a>
</h1>
</div>
<nav class="main-nav unit two-thirds hide-on-mobiles">
<ul>
<li class="">
<a href="/">Home</a>
</li>
<li class="current">
<a href="/docs/">Doc<span class="show-on-mobiles">s</span><span class="hide-on-mobiles">umentation</span></a>
</li>
</li>
<li class="">
<a href="/downloads/">Downloads</a>
</li>
<li class="">
<a href="/talks/">Talks</a>
</li>
<li class="">
<a href="/news/">News</a>
</li>
<li class="">
<a href="/develop/">Develop</a>
</li>
<li class="">
<a href="/help/">Help</a>
</li>
</ul>
</nav>
</div>
<!-- Return to Top -->
<a href="#top" id="return-to-top"><i class="fa fa-chevron-up"></i></a>
</header>
<section class="docs">
<div class="grid">
<div class="docs-nav-mobile unit whole show-on-mobiles">
<select onchange="if (this.value) window.location.href=this.value">
<option value="">Navigate the docs…</option>
<optgroup label="Overview">
<option value="/docs/arch.html">Architecture</option>
<option value="/docs/features.html">Features</option>
<option value="/docs/modes.html">Mnemonic Modes</option>
<option value="/docs/memsrvcmp.html">Durable Memory Service Comparison</option>
<option value="/docs/integration.html">Integration with other projects</option>
<option value="/docs/domusecases.html">DOM Use Cases</option>
<option value="/docs/dcmusecases.html">DCM Use Cases</option>
<option value="/docs/msusecases.html">MS Use Cases</option>
<option value="/docs/sparkusecases.html">Apache Spark Integration Use Cases</option>
<option value="/docs/hadoopusecases.html">Apache Hadoop Integration Use Cases</option>
<option value="/docs/gcdata.html">Java GC data</option>
<option value="/docs/security.html">Security</option>
</optgroup>
<optgroup label="Usage">
<option value="/docs/getstarted.html">Get Started</option>
<option value="/docs/devsetup.html">Development Setup</option>
<option value="/docs/submitchanges.html">Submit Changes</option>
<option value="/docs/build.html">Project Build</option>
<option value="/docs/runtest.html">Run tests</option>
<option value="/docs/docker.html">Docker usage</option>
</optgroup>
</select>
</div>
<div class="unit four-fifths">
<article>
<h1>Code Usage</h1>
<h3 id="how-to-use-it-">How to use it ?</h3>
<h4 id="define-a-non-volatile-class">Define a Non-Volatile class:</h4>
<p>```java
/**
* a durable class should be abstract, implement Durable interface and marked with @DurableEntity annotation
*/
@DurableEntity
public abstract class Person<e> implements Durable, Comparable&lt;Person<e>&gt; {
E element; // Generic Type</e></e></p>
<pre><code> /**
* callback for this durable object creation
*/
@Override
public void initializeAfterCreate() {
System.out.println("Initializing After Created");
}
/**
* callback for this durable object recovery
*/
@Override
public void initializeAfterRestore() {
System.out.println("Initializing After Restored");
}
/**
* setup generic info manually to avoid performance penalty
*/
@Override
public void setupGenericInfo(EntityFactoryProxy[] efproxies, GenericField.GType[] gftypes) {
}
@Test
public void testOutput() throws RetrieveDurableEntityError {
System.out.printf("Person %s, Age: %d ( %s ) \n", getName(), getAge(),
null == getMother()? "No Recorded Mother" : "Has Recorded Mother");
}
public int compareTo(Person&lt;E&gt; anotherPerson) {
int ret = 0;
if (0 == ret) ret = getAge().compareTo(anotherPerson.getAge());
if (0 == ret) ret = getName().compareTo(anotherPerson.getName());
return ret;
}
/**
* Getters and Setters for non-volatile fields marked with @DurableGetter and @DurableSetter
*/
@DurableGetter(Id = 1L)
abstract public Short getAge();
@DurableSetter
abstract public void setAge(Short age);
@DurableGetter(Id = 2L)
abstract public String getName() throws RetrieveDurableEntityError;
@DurableSetter
abstract public void setName(String name, boolean destroy) throws OutOfPersistentMemory, RetrieveDurableEntityError;
@DurableGetter(Id = 3L)
abstract public Person&lt;E&gt; getMother() throws RetrieveDurableEntityError;
@DurableSetter
abstract public void setMother(Person&lt;E&gt; mother, boolean destroy) throws RetrieveDurableEntityError;
@DurableGetter(Id = 4L)
abstract public Person&lt;E&gt; getFather() throws RetrieveDurableEntityError;
@DurableSetter
abstract public void setFather(Person&lt;E&gt; mother, boolean destroy) throws RetrieveDurableEntityError; }
</code></pre>
<p>```</p>
<h4 id="use-a-non-volatile-class">Use a non-volatile class:</h4>
<h5 id="setup-an-allocator-for-non-volatile-object-graphs">Setup an allocator for non-volatile object graphs.</h5>
<p>```java
// create an allocator instance
NonVolatileMemAllocator act = new NonVolatileMemAllocator(1024 * 1024 * 8, “./pobj_person.dat”, true);</p>
<pre><code> // fetch handler store capacity from this non-volatile storage managed by this allocator
KEYCAPACITY = act.handlerCapacity();
....
// close it after use
act.close(); ```
</code></pre>
<h5 id="generate-structured-non-volatile-objects">Generate structured non-volatile objects.</h5>
<p>```java
// create a new non-volatile person object from this specific allocator
person = PersonFactory.create(act);</p>
<pre><code> // set attributes
person.setAge((short)rand.nextInt(50));
person.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
// keep this person on non-volatile handler store
act.setHandler(keyidx, person.getHandler());
for (int deep = 0; deep &lt; rand.nextInt(100); ++deep) {
// create another person as mother
mother = PersonFactory.create(act);
mother.setAge((short)(50 + rand.nextInt(50)));
mother.setName(String.format("Name: [%s]", UUID.randomUUID().toString()), true);
// set the person's mother
person.setMother(mother, true);
person = mother;
}
</code></pre>
<p>```</p>
<h5 id="use-the-non-volatile-objects">Use the non-volatile objects</h5>
<p>```java
for (long i = 0; i &lt; KEYCAPACITY; ++i) {</p>
<pre><code> System.out.printf("----------Key %d--------------\n", i);
// iterate non-volatile handlers from handler store of this specific allocator
val = act.getHandler(i);
if (0L == val) {
break;
}
// restore person objects from this specific allocator
Person&lt;Integer&gt; person = PersonFactory.restore(act, val, true);
while (null != person) {
person.testOutput();
// iterate all mother's ancestors
person = person.getMother();
}
}
</code></pre>
<p>```</p>
<h5 id="perform-the-durable-native-computing-eg-printing-wo-packingunpacking-massive-object-graphs">Perform the durable native computing (e.g. printing) w/o packing/unpacking massive object graphs</h5>
<p><code>java
// fetch print service
GeneralComputingService gcsvr = Utils.getGeneralComputingService("print");
// instantiate a value info for a value matrix
ValueInfo vinfo = new ValueInfo();
// instantiate a object stack
List&lt;long[][]&gt; objstack = new ArrayList&lt;long[][]&gt;();
// fill up with all durable object info in order
objstack.add(firstnv.getNativeFieldInfo());
objstack.add(person.getNativeFieldInfo());
// configure the Id stack for each level of durable objects
long[][] fidinfostack = {{2L, 1L}, {0L, 1L}};
// configure the handler of a value matrix
vinfo.handler = handler;
// set translate table from handler's allocator
vinfo.transtable = m_act.getTranslateTable();
// specify the durable type of value
vinfo.dtype = DurableType.SHORT;
// generate frames for this value matri from both stacks
vinfo.frames = Utils.genNativeParamForm(objstack, fidinfostack);
// form an array of value infos
ValueInfo[] vinfos = {vinfo};
// perform the print operation
gcsvr.perform(vinfos);
</code></p>
</article>
</div>
<div class="unit one-fifth hide-on-mobiles">
<aside>
<h4>Overview</h4>
<ul>
<li class=""><a href="/docs/arch.html">Architecture</a></li>
<li class=""><a href="/docs/features.html">Features</a></li>
<li class=""><a href="/docs/modes.html">Mnemonic Modes</a></li>
<li class=""><a href="/docs/memsrvcmp.html">Durable Memory Service Comparison</a></li>
<li class=""><a href="/docs/integration.html">Integration with other projects</a></li>
<li class=""><a href="/docs/domusecases.html">DOM Use Cases</a></li>
<li class=""><a href="/docs/dcmusecases.html">DCM Use Cases</a></li>
<li class=""><a href="/docs/msusecases.html">MS Use Cases</a></li>
<li class=""><a href="/docs/sparkusecases.html">Apache Spark Integration Use Cases</a></li>
<li class=""><a href="/docs/hadoopusecases.html">Apache Hadoop Integration Use Cases</a></li>
<li class=""><a href="/docs/gcdata.html">Java GC data</a></li>
<li class=""><a href="/docs/security.html">Security</a></li>
</ul>
<h4>Usage</h4>
<ul>
<li class=""><a href="/docs/getstarted.html">Get Started</a></li>
<li class=""><a href="/docs/devsetup.html">Development Setup</a></li>
<li class=""><a href="/docs/submitchanges.html">Submit Changes</a></li>
<li class=""><a href="/docs/build.html">Project Build</a></li>
<li class=""><a href="/docs/runtest.html">Run tests</a></li>
<li class=""><a href="/docs/docker.html">Docker usage</a></li>
</ul>
</aside>
</div>
<div class="clear"></div>
</div>
</section>
<footer role="contentinfo">
<p><div align="center">Apache Mnemonic, Mnemonic, Apache, and the Mnemonic logo are trademarks of The Apache Software Foundation.</div>
<div align="center"> Copyright &copy;&nbsp;2021 <a href="https://www.apache.org/">The Apache Software Foundation</a></div></p>
</footer>
<script>
var anchorForId = function (id) {
var anchor = document.createElement("a");
anchor.className = "header-link";
anchor.href = "#" + id;
anchor.innerHTML = "<span class=\"sr-only\">Permalink</span><i class=\"fa fa-link\"></i>";
anchor.title = "Permalink";
return anchor;
};
var linkifyAnchors = function (level, containingElement) {
var headers = containingElement.getElementsByTagName("h" + level);
for (var h = 0; h < headers.length; h++) {
var header = headers[h];
if (typeof header.id !== "undefined" && header.id !== "") {
header.appendChild(anchorForId(header.id));
}
}
};
document.onreadystatechange = function () {
if (this.readyState === "complete") {
var contentBlock = document.getElementsByClassName("docs")[0] || document.getElementsByClassName("news")[0];
if (!contentBlock) {
return;
}
for (var level = 1; level <= 6; level++) {
linkifyAnchors(level, contentBlock);
}
}
};
</script>
</body>
</html>