You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2016/05/06 00:39:56 UTC
[07/11] incubator-mnemonic git commit: MNEMONIC-40 - Clean up
assembled source package for release MNEMONIC-41 - Create a script for
preparing a release of a single package with signing and hashing MNEMONIC-42
- Normalize the names of project submodules
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java b/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java
deleted file mode 100644
index 0a36b02..0000000
--- a/core/src/main/java/org/apache/mnemonic/EntityFactoryProxy.java
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * proxy the restoring of generic non-volatile object fields
- *
- */
-
-public interface EntityFactoryProxy {
-
- /**
- * restore a durable object from persistent allocator using a handler of
- * non-volatile object
- *
- * @param allocator
- * specify a persistent allocator instance
- *
- * @param factoryproxys
- * specify an array of factory proxies for its restored non-volatile
- * object
- *
- * @param gfields
- * specify an array of generic types of its generic fields
- * corresponding to factoryproxys
- *
- * @param phandler
- * specify a non-volatile handler to restore
- *
- * @param autoreclaim
- * specify auto-reclaim for this restored non-volatile object
- *
- * @return the restored non-volatile object from this factory proxy
- *
- */
- <A extends CommonDurableAllocator<A>> Durable restore(A allocator, EntityFactoryProxy[] factoryproxys,
- GenericField.GType[] gfields, long phandler, boolean autoreclaim);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/EvictFilter.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/EvictFilter.java b/core/src/main/java/org/apache/mnemonic/EvictFilter.java
deleted file mode 100644
index 1d48a18..0000000
--- a/core/src/main/java/org/apache/mnemonic/EvictFilter.java
+++ /dev/null
@@ -1,41 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * A listener callback to validate its accepted key-value pair for evacuation.
- *
- */
-public interface EvictFilter<KeyT, ValueT> {
- /**
- * A call-back validator when an entry will be evicted.
- *
- * @param pool
- * the pool which an entry has been evicted from
- *
- * @param k
- * the entry's key
- *
- * @param v
- * the entry's value
- *
- * @return <tt>true</tt> if the entry is allowed to be dropped from its cache
- * pool.
- */
- boolean validate(CachePool<KeyT, ValueT> pool, KeyT k, ValueT v);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/GenericField.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/GenericField.java b/core/src/main/java/org/apache/mnemonic/GenericField.java
deleted file mode 100644
index b9d1df6..0000000
--- a/core/src/main/java/org/apache/mnemonic/GenericField.java
+++ /dev/null
@@ -1,305 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * this class defines generic field for non-volatile entity
- *
- */
-
-import sun.misc.Unsafe;
-
-@SuppressWarnings("restriction")
-public class GenericField<A extends CommonDurableAllocator<A>, E> implements Durable {
-
- /**
- * defines the types of generic field
- *
- */
- public enum GType {
- BOOLEAN, CHARACTER, BYTE, SHORT, INTEGER, LONG, FLOAT, DOUBLE, STRING, DURABLE
- };
-
- private Unsafe m_unsafe;
- private long m_fpos;
- private GType m_dgftype = null;
- private Durable m_field = null;
- private MemBufferHolder<A> m_strfield = null;
- private A m_allocator;
- private boolean m_autoreclaim;
- private EntityFactoryProxy m_defproxy = null;
- private EntityFactoryProxy[] m_efproxies;
- private GType[] m_gftypes;
-
- /**
- * Constructor: initialize this generic field
- *
- * @param defproxy
- * specify its entity factory proxy
- *
- * @param dgftype
- * specify its type
- *
- * @param efproxies
- * specify an array of containing entity factory proxies
- *
- * @param gftypes
- * specify an array of containing types corresponding to dfproxies
- *
- * @param allocator
- * specify the allocator this field sit on
- *
- * @param unsafe
- * specify the unsafe instance
- *
- * @param autoreclaim
- * specify true if ask for auto-reclaim for this field
- *
- * @param fpos
- * specify the field position
- *
- */
- public GenericField(EntityFactoryProxy defproxy, GType dgftype, EntityFactoryProxy[] efproxies, GType[] gftypes,
- A allocator, Unsafe unsafe, boolean autoreclaim, Long fpos) {
- m_unsafe = unsafe;
- m_fpos = fpos;
- m_allocator = allocator;
- m_autoreclaim = autoreclaim;
- m_efproxies = efproxies;
- m_gftypes = gftypes;
- m_defproxy = defproxy;
- m_dgftype = dgftype;
- }
-
- /**
- * set a value to this field
- *
- * @param e
- * specify a value to set
- *
- * @param destroy
- * specify true if want to destroy the original value
- */
- public void set(E e, boolean destroy) {
- boolean isnull = null == e;
- switch (m_dgftype) {
- case BYTE:
- m_unsafe.putByte(m_fpos, isnull ? (byte) 0 : (Byte) e);
- break;
- case BOOLEAN:
- m_unsafe.putByte(m_fpos, isnull ? (byte) 0 : ((Boolean) e ? (byte) 1 : (byte) 0));
- break;
- case CHARACTER:
- m_unsafe.putChar(m_fpos, isnull ? (char) 0 : (Character) e);
- break;
- case SHORT:
- m_unsafe.putShort(m_fpos, isnull ? (short) 0 : (Short) e);
- break;
- case INTEGER:
- m_unsafe.putInt(m_fpos, isnull ? (int) 0 : (Integer) e);
- break;
- case LONG:
- m_unsafe.putLong(m_fpos, isnull ? (long) 0 : (Long) e);
- break;
- case FLOAT:
- m_unsafe.putFloat(m_fpos, isnull ? (float) 0 : (Float) e);
- break;
- case DOUBLE:
- m_unsafe.putDouble(m_fpos, isnull ? (double) 0 : (Double) e);
- break;
- case STRING:
- String str = (String) e;
- if (destroy && null != get()) {
- m_strfield.destroy();
- m_strfield = null;
- m_unsafe.putAddress(m_fpos, 0L);
- }
- if (null == str) {
- m_unsafe.putAddress(m_fpos, 0L);
- } else {
- m_strfield = m_allocator.createBuffer(str.length() * 2, m_autoreclaim);
- if (null == m_strfield) {
- throw new OutOfHybridMemory("Create Durable String Error!");
- }
- m_strfield.get().asCharBuffer().put(str);
- m_unsafe.putAddress(m_fpos, m_allocator.getBufferHandler(m_strfield));
- }
- break;
- case DURABLE:
- if (destroy && null != get()) {
- m_field.destroy();
- m_field = null;
- m_unsafe.putAddress(m_fpos, 0L);
- }
- m_field = (Durable) e;
- m_unsafe.putAddress(m_fpos, null == m_field ? 0L : m_field.getHandler());
- break;
- }
-
- }
-
- /**
- * get the value of this field
- *
- * @return the field value
- */
- @SuppressWarnings("unchecked")
- public E get() {
- E ret = null;
- switch (m_dgftype) {
- case BYTE:
- ret = (E) Byte.valueOf(m_unsafe.getByte(m_fpos));
- break;
- case BOOLEAN:
- ret = (E) Boolean.valueOf(1 == m_unsafe.getByte(m_fpos));
- break;
- case CHARACTER:
- ret = (E) Character.valueOf(m_unsafe.getChar(m_fpos));
- break;
- case SHORT:
- ret = (E) Short.valueOf(m_unsafe.getShort(m_fpos));
- break;
- case INTEGER:
- ret = (E) Integer.valueOf(m_unsafe.getInt(m_fpos));
- break;
- case LONG:
- ret = (E) Long.valueOf(m_unsafe.getLong(m_fpos));
- break;
- case FLOAT:
- ret = (E) Float.valueOf(m_unsafe.getFloat(m_fpos));
- break;
- case DOUBLE:
- ret = (E) Double.valueOf(m_unsafe.getDouble(m_fpos));
- break;
- case STRING:
- if (null == m_strfield) {
- long phandler = m_unsafe.getAddress(m_fpos);
- if (0L != phandler) {
- m_strfield = m_allocator.retrieveBuffer(phandler, m_autoreclaim);
- if (null == m_strfield) {
- throw new RetrieveDurableEntityError("Retrieve String Buffer Failure.");
- }
- }
- }
- ret = (E) (null == m_strfield ? null : m_strfield.get().asCharBuffer().toString());
- break;
- case DURABLE:
- if (null == m_field) {
- long phandler = m_unsafe.getAddress(m_fpos);
- if (0L != phandler) {
- if (null == m_defproxy) {
- throw new RetrieveDurableEntityError("Proxy not specified for Non-Volatile Generic entity.");
- }
- m_field = m_defproxy.restore(m_allocator, m_efproxies, m_gftypes, phandler, m_autoreclaim);
- }
- }
- ret = (E) m_field;
- break;
- }
- return ret;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void initializeAfterCreate() {
- throw new UnsupportedOperationException("GenericField.initializeAfterCreate()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void initializeAfterRestore() {
- throw new UnsupportedOperationException("GenericField.initializeAfterRestore()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void cancelAutoReclaim() {
- if (null != m_field) {
- m_field.cancelAutoReclaim();
- }
- if (null != m_strfield) {
- m_strfield.cancelAutoReclaim();
- }
- m_autoreclaim = false;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void registerAutoReclaim() {
- if (null != m_field) {
- m_field.registerAutoReclaim();
- }
- if (null != m_strfield) {
- m_strfield.registerAutoReclaim();
- }
- m_autoreclaim = true;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long getHandler() {
- throw new UnsupportedOperationException("GenericField.getHandler()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean autoReclaim() {
- return m_autoreclaim;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void destroy() throws RetrieveDurableEntityError {
- if (null != m_field) {
- m_field.destroy();
- }
- if (null != m_strfield) {
- m_strfield.destroy();
- }
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void setupGenericInfo(EntityFactoryProxy[] efproxies, GType[] gftypes) {
- throw new UnsupportedOperationException("GenericField.setupGenericInfo()");
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public long[][] getNativeFieldInfo() {
- throw new UnsupportedOperationException("getNativeFieldInfo() on Generic Field.");
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java b/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java
deleted file mode 100644
index c14ecc3..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemBufferHolder.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.nio.ByteBuffer;
-
-/**
- * holder for a ByteBuffer instance.
- *
- */
-public class MemBufferHolder<A extends CommonAllocator<A>> extends MemHolder<A, ByteBuffer, MemBufferHolder<A>> {
-
- /**
- * Constructor: initialize with a bytebuffer.
- *
- * @param ar
- * specify an Allocator for this holder
- *
- * @param mres
- * specify a chunk to be holden
- *
- */
- public MemBufferHolder(A ar, ByteBuffer mres) {
- super(mres, ar);
- }
-
- /**
- * get the size of its held bytebuffer
- *
- * @return the size
- */
- @Override
- public long getSize() {
- return m_mres.capacity();
- }
-
- /**
- * resize its held buffer
- *
- * @param size
- * specify the new size for its held buffer
- */
- @Override
- public MemBufferHolder<A> resize(long size) {
- return m_allocator.resizeBuffer(this, size);
- }
-
- /**
- * register its held buffer for auto-reclaim
- *
- */
- @Override
- public void registerAutoReclaim() {
- m_allocator.registerBufferAutoReclaim(this);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java b/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java
deleted file mode 100644
index 02f1a5c..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemBufferHolderCachePool.java
+++ /dev/null
@@ -1,242 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.util.ArrayList;
-import java.util.HashSet;
-import java.util.Set;
-import java.util.Map;
-import java.util.concurrent.atomic.AtomicLong;
-
-/**
- *
- * This class inherited from abstract CachePool class that implemented all
- * inherited abstract methods. it specialized to cache MemBufferHolder objects
- * that is backed by native memory pool.
- *
- */
-public class MemBufferHolderCachePool<KeyT> extends CachePool<KeyT, MemBufferHolder<?>> {
-
- private static final long serialVersionUID = 684275993324558070L;
-
- private AtomicLong currentMemory = new AtomicLong(0L);
-
- private long maxStoreCapacity = 0L;
-
- /**
- * Constructs an empty cache pool with specified capacity.
- *
- * @param maxCapacity
- * the capacity of this cache pool
- *
- */
- public MemBufferHolderCachePool(long maxCapacity) {
- super(512, 0.75f);
- maxStoreCapacity = maxCapacity;
- }
-
- /**
- * @see CachePool#freeCapacity()
- */
- @Override
- public long freeCapacity() {
- return maxStoreCapacity - currentMemory.get();
- }
-
- /**
- * @see CachePool#usedCapacity()
- */
- @Override
- public long usedCapacity() {
- return currentMemory.get();
- }
-
- /**
- * @see Map#remove(Object)
- */
- @Override
- public synchronized MemBufferHolder<?> remove(Object k) {
- MemBufferHolder<?> ret = super.remove(k);
- if (null != ret) {
- currentMemory.getAndAdd(-ret.getSize());
- }
- return ret;
- }
-
- /**
- * Associates the specified value with the specified key in this map (optional
- * operation). If the map previously contained a mapping for the key, the old
- * value is replaced by the specified value. (A map m is said to contain a
- * mapping for a key k if and only if m.containsKey(k) would return true.)
- *
- * @param k
- * key with which the specified value is to be associated
- *
- * @param v
- * MemBufferHolder value to be associated with the specified key
- *
- * @return the previous value associated with key, or null if there was no
- * mapping for key. (A null return can also indicate that the map
- * previously associated null with key, if the implementation supports
- * null values.)
- */
- @Override
- public MemBufferHolder<?> put(KeyT k, MemBufferHolder<?> v) {
- return put(k, v, null, null);
- }
-
- /**
- * @see CachePool#put(Object, Object, DropEvent, EvictFilter)
- */
- @Override
- public MemBufferHolder<?> put(KeyT k, MemBufferHolder<?> v, DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
- MemBufferHolder<?> ret = null;
- long sz = v.getSize();
- if (containsKey(k)) {
- sz -= get(k).getSize();
- }
- if (sz <= maxStoreCapacity && ensureFreeSpace(sz, fsop, dfilter)) {
- currentMemory.addAndGet(sz);
- ret = super.put(k, v);
- } else {
- throw new ContainerOverflowException("Out of capacity of MemBufferHolderCachePool.");
- }
- return ret;
- }
-
- /**
- * @see Map#putAll(Map)
- */
- @Override
- public void putAll(Map<? extends KeyT, ? extends MemBufferHolder<?>> m) {
- putAll(m, null, null);
- }
-
- /**
- * @see CachePool#putAll(Map, DropEvent, EvictFilter)
- */
- @Override
- public void putAll(Map<? extends KeyT, ? extends MemBufferHolder<?>> m, DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
-
- long reqsz = 0;
- for (KeyT k : m.keySet()) {
- reqsz += m.get(k).getSize();
- }
-
- if (reqsz <= maxStoreCapacity && ensureFreeSpace(reqsz, fsop, dfilter)) {
- currentMemory.addAndGet(reqsz);
- super.putAll(m);
- } else {
- throw new ContainerOverflowException("Out of capacity of MemBufferHolderCachePool.");
- }
- }
-
- /**
- * @see CachePool#hotKeySet(int)
- */
- @Override
- public Set<KeyT> hotKeySet(int n) {
- Set<KeyT> ret = new HashSet<KeyT>();
- ArrayList<KeyT> keys = new ArrayList<KeyT>(keySet());
- int endindex = keys.size() > n ? keys.size() - n : 0;
- for (int i = keys.size(); i > endindex; i--) {
- ret.add(keys.get(i - 1));
- }
- return ret;
- }
-
- /**
- * @see CachePool#ensureFreeSpace(long)
- */
- @Override
- public boolean ensureFreeSpace(long freesz) {
- return ensureFreeSpace(freesz, null, null);
- }
-
- /**
- * @see CachePool#removeFirstEntry(DropEvent, EvictFilter)
- */
- @Override
- public boolean removeFirstEntry(DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
- boolean ret = false;
- boolean delible = true;
- for (Map.Entry<KeyT, MemBufferHolder<?>> entry : entrySet()) {
- if (null != dfilter) {
- delible = dfilter.validate(this, entry.getKey(), entry.getValue());
- }
- if (delible) {
- KeyT k = entry.getKey();
- MemBufferHolder<?> v = remove(k);
- if (null != fsop) {
- fsop.drop(this, k, v);
- }
- ret = true;
- break;
- }
- }
- return ret;
- }
-
- /**
- * @see CachePool#ensureFreeSpace(long, DropEvent, EvictFilter)
- */
- @Override
- public boolean ensureFreeSpace(long freesz, DropEvent<KeyT, MemBufferHolder<?>> fsop,
- EvictFilter<KeyT, MemBufferHolder<?>> dfilter) {
- boolean ret = false;
-
- if (freesz <= freeCapacity()) {
- return true;
- }
-
- if (freesz > maxStoreCapacity) {
- return false;
- }
-
- long selectedMemory = 0L;
- Set<KeyT> selected = new HashSet<KeyT>();
-
- boolean delible = true;
- for (Map.Entry<KeyT, MemBufferHolder<?>> entry : entrySet()) {
- if (null != dfilter) {
- delible = dfilter.validate(this, entry.getKey(), entry.getValue());
- }
- if (delible) {
- selectedMemory += entry.getValue().getSize();
- selected.add(entry.getKey());
- if (freesz <= freeCapacity() + selectedMemory) {
- ret = true;
- break;
- }
- }
- }
- if (ret) {
- for (KeyT k : selected) {
- MemBufferHolder<?> mbh = remove(k);
- if (null != fsop) {
- fsop.drop(this, k, mbh);
- }
- }
- }
- return ret;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java b/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java
deleted file mode 100644
index 308539f..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemChunkHolder.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * holder for a memory chunk.
- *
- */
-public class MemChunkHolder<A extends CommonAllocator<A>> extends MemHolder<A, Long, MemChunkHolder<A>> {
-
- protected long m_size;
-
- /**
- * Constructor: initialize with a memory chunk.
- *
- * @param ar
- * specify an Allocator for this holder
- *
- * @param mres
- * specify a chunk to be holden
- *
- * @param size
- * specify the size of this memory chunk
- */
- public MemChunkHolder(A ar, Long mres, long size) {
- super(mres, ar);
- m_size = size;
- }
-
- /**
- * get the size of its held memory chunk
- *
- * @return the size
- */
- @Override
- public long getSize() {
- return m_size;
- }
-
- /**
- * resize its held chunk
- *
- * @param size
- * specify the new size for its held chunk
- */
- @Override
- public MemChunkHolder<A> resize(long size) {
- return m_allocator.resizeChunk(this, size);
- }
-
- /**
- * register its held chunk for auto-reclaim
- *
- */
- @Override
- public void registerAutoReclaim() {
- m_allocator.registerChunkAutoReclaim(this);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemClustering.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemClustering.java b/core/src/main/java/org/apache/mnemonic/MemClustering.java
deleted file mode 100644
index a643c48..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemClustering.java
+++ /dev/null
@@ -1,313 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.EnumMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * clustering different kind of memory-like media and combine them as a larger
- * memory pool for allocation. it will notify user when the underlying memory
- * storage has been switched or downgraded for the request of allocation
- *
- */
-public class MemClustering {
-
- /**
- * an interface of event for performance level change.
- *
- */
- public interface PerformanceLevelChange {
-
- /**
- * callback if performance level changed
- *
- * @param prevlvl
- * the perf. level before change
- *
- * @param tgtlvl
- * the perf. level after change
- *
- */
- void changed(PerformanceLevel prevlvl, PerformanceLevel tgtlvl);
- }
-
- /**
- * an interface of event for CommonAllocator change.
- *
- */
- public interface AllocatorChange {
-
- /**
- * callback if allocator changed
- *
- * @param lvl
- * the perf. level after changed
- *
- * @param prevallocator
- * the allocator before change
- *
- * @param tgtallocator
- * the allocator after change
- */
- void changed(PerformanceLevel lvl, CommonAllocator<?> prevallocator, CommonAllocator<?> tgtallocator);
- }
-
- /**
- * an interface to assist the creation of memory holder.
- *
- * @param <H>
- * the holder type of memory resource
- *
- * @param bma
- * specify an allocator for this holder
- *
- * @param size
- * specify the size of this memory resource
- *
- * @return the holder created
- */
- private interface MemCreate<H extends MemHolder<? extends CommonAllocator<?>, ?, ?>> {
- H create(CommonAllocator<?> bma, long size);
- }
-
- private MemCreate<MemChunkHolder<?>> m_memchunkcreate = new MemCreate<MemChunkHolder<?>>() {
- @Override
- public MemChunkHolder<?> create(CommonAllocator<?> bma, long size) {
- return bma.createChunk(size);
- }
- };
-
- private MemCreate<MemBufferHolder<?>> m_membuffercreate = new MemCreate<MemBufferHolder<?>>() {
- @Override
- public MemBufferHolder<?> create(CommonAllocator<?> bma, long size) {
- return bma.createBuffer(size);
- }
- };
-
- /**
- * performance level categories.
- *
- */
- public enum PerformanceLevel {
- FASTEST, FAST, NORMAL, SLOW, SLOWEST
- }
-
- /**
- * configuration for each allocator node.
- *
- */
- public static class NodeConfig<A extends CommonAllocator<A>> {
- private A m_allocator;
- private PerformanceLevel m_level;
-
- /**
- * Constructor: initialize this instance with specified allocator and perf.
- * level.
- *
- * @param a
- * specify an allocator for this node
- *
- * @param l
- * specify a performance level
- */
- public NodeConfig(A a, PerformanceLevel l) {
- m_allocator = a;
- m_level = l;
- }
-
- /**
- * retrieve the allocator of this node.
- *
- * @return allocator of this node
- */
- public A getAllocator() {
- return m_allocator;
- }
-
- /**
- * retrieve the performance level of this node.
- *
- * @return level of this node
- */
- public PerformanceLevel getPerformanceLevel() {
- return m_level;
- }
- }
-
- private PerformanceLevelChange m_bwlvlchange = null;
- private AllocatorChange m_allocatorChange = null;
- private PerformanceLevel m_prevbwlevel = null;
- private CommonAllocator<?> m_prevallocator = null;
-
- private Map<PerformanceLevel, List<CommonAllocator<?>>> m_info;
-
- /**
- * Constructor: initialize a memory clustering instance.
- *
- * @param ncs
- * specify a set of node with specified configuration respectively
- */
- public MemClustering(NodeConfig<? extends CommonAllocator<?>>[] ncs) {
- m_info = new EnumMap<PerformanceLevel, List<CommonAllocator<?>>>(PerformanceLevel.class);
- for (PerformanceLevel lvl : PerformanceLevel.values()) {
- m_info.put(lvl, new ArrayList<CommonAllocator<?>>());
- }
-
- for (NodeConfig<? extends CommonAllocator<?>> nc : ncs) {
- m_info.get(nc.getPerformanceLevel()).add(nc.getAllocator());
- }
-
- }
-
- /**
- * set a callback of event for performance level change.
- *
- * @param bwlvlchange
- * specify a callback object for perf. level change
- */
- public void setPerformanceLevelChange(PerformanceLevelChange bwlvlchange) {
- m_bwlvlchange = bwlvlchange;
- }
-
- /**
- * set a callback of event for allocator change.
- *
- * @param allocatorChange
- * specify a callback object for allocator change
- */
- public <A extends CommonAllocator<A>> void setAllocatorChange(AllocatorChange allocatorChange) {
- m_allocatorChange = allocatorChange;
- }
-
- /**
- * a factory to create memory resource.
- *
- * @param <H>
- * the type of holder
- *
- * @param creator
- * specify a creator to delegate concrete holder creation
- *
- * @param startlevel
- * specify a perf. level that is the start level for memory resource
- * allocation
- *
- * @param size
- * specify a size to request memory resource
- *
- * @return a new created holder held an allocated memory resource
- */
- protected <H extends MemHolder<? extends CommonAllocator<?>, ?, ?>> H create(MemCreate<H> creator,
- PerformanceLevel startlevel, long size) {
- H ret = null;
- boolean eligible = false;
- for (PerformanceLevel lvl : m_info.keySet()) {
- if (!eligible && startlevel == lvl) {
- eligible = true;
- }
- if (eligible) {
- int distance = 0;
- List<CommonAllocator<?>> bmas = m_info.get(lvl);
- for (CommonAllocator<?> bma : bmas) {
- ret = creator.create(bma, size);
- if (null == ret) {
- distance++;
- } else {
- if (null != m_bwlvlchange && m_prevbwlevel != lvl) {
- m_bwlvlchange.changed(m_prevbwlevel, lvl);
- m_prevbwlevel = lvl;
- }
- if (null != m_allocatorChange && m_prevallocator != bma) {
- m_allocatorChange.changed(lvl, m_prevallocator, bma);
- m_prevallocator = bma;
- }
- break;
- }
- }
- Collections.rotate(bmas, distance);
- }
- if (null != ret) {
- break;
- }
- }
- return ret;
- }
-
- /**
- * create a chunk from this clustering.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @return a holder with a created chunk
- *
- */
- // @Override
- public MemChunkHolder<?> createChunk(long size) {
- return createChunk(PerformanceLevel.FASTEST, size);
- }
-
- /**
- * create a chunk from this clustering
- *
- * @param startlevel
- * specify the perf. level from which to search qualified node.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @return a holder with a created chunk
- */
- public MemChunkHolder<?> createChunk(PerformanceLevel startlevel, long size) {
- return create(m_memchunkcreate, startlevel, size);
- }
-
- /**
- * create a buffer from this clustering.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @return a holder with a created buffer
- *
- */
- // @Override
- public MemBufferHolder<?> createBuffer(long size) {
- return createBuffer(PerformanceLevel.FASTEST, size);
- }
-
- /**
- * create a buffer from this clustering
- *
- * @param startlevel
- * specify the perf. level from which to search qualified node.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @return a holder with a created buffer
- */
- public MemBufferHolder<?> createBuffer(PerformanceLevel startlevel, long size) {
- return create(m_membuffercreate, startlevel, size);
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemFile.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemFile.java b/core/src/main/java/org/apache/mnemonic/MemFile.java
deleted file mode 100644
index c4a1c06..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemFile.java
+++ /dev/null
@@ -1,67 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.io.File;
-
-/**
- * a memory file that manages its data on native memory storage. Note: this
- * class depends on PMFS, we suggest that NVM library to support this feature in
- * native layer. In addition, the permission of /mnt/pmfs will be set properly.
- *
- *
- */
-public class MemFile extends File {
-
- private static final long serialVersionUID = 6579668848729471173L;
- private String uri, id;
-
- /**
- * initialize the memory file.
- *
- * @param uri
- * specify the location of memory file
- *
- * @param id
- * specify the id of memory file
- */
- public MemFile(String uri, String id) {
- super(uri, id);
- this.uri = uri;
- this.id = id;
- }
-
- /**
- * retrieve the uri of this memory file.
- *
- * @return the uri of memory file
- */
- public String getUri() {
- return this.uri;
- }
-
- /**
- * retrieve the id of this memory file.
- *
- * @return the id of memory file
- */
- public String getId() {
- return this.id;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemHolder.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemHolder.java b/core/src/main/java/org/apache/mnemonic/MemHolder.java
deleted file mode 100644
index 75e4c7e..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemHolder.java
+++ /dev/null
@@ -1,75 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import org.flowcomputing.commons.resgc.ResHolder;
-
-/**
- * hold a memory kind of resource.
- *
- */
-public abstract class MemHolder<A extends CommonAllocator<A>, T, H extends MemHolder<A, T, H>> extends ResHolder<T, H> {
-
- protected A m_allocator;
-
- /**
- * Constructor: initialize with resource.
- *
- * @param mres
- * specify a resource to be holden
- *
- * @param ar
- * specify an Allocator for this holder
- *
- */
- public MemHolder(T mres, A ar) {
- super(mres);
- m_allocator = ar;
- }
-
- /**
- * get its allocator
- *
- * @return the allocator
- */
- public A getAllocator() {
- return m_allocator;
- }
-
- /**
- * resize its held resource
- *
- * @param size
- * specify the new size for its held resource
- */
- public abstract MemHolder<A, T, H> resize(long size);
-
- /**
- * get the size of its held memory resource.
- *
- * @return the size
- */
- public abstract long getSize();
-
- /**
- * register its held resource for auto-reclaim
- *
- */
- public abstract void registerAutoReclaim();
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/MemoryDurableEntity.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/MemoryDurableEntity.java b/core/src/main/java/org/apache/mnemonic/MemoryDurableEntity.java
deleted file mode 100644
index a2c56bf..0000000
--- a/core/src/main/java/org/apache/mnemonic/MemoryDurableEntity.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- *
- *
- */
-
-public interface MemoryDurableEntity<ALLOC_PMem3C93D24F59 extends CommonDurableAllocator<ALLOC_PMem3C93D24F59>> {
-
- void initializeDurableEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys,
- GenericField.GType[] gfields, boolean autoreclaim);
-
- void createDurableEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys,
- GenericField.GType[] gfields, boolean autoreclaim) throws OutOfHybridMemory;
-
- void restoreDurableEntity(ALLOC_PMem3C93D24F59 allocator, EntityFactoryProxy[] efproxys,
- GenericField.GType[] gfields, long phandler, boolean autoreclaim) throws RetrieveDurableEntityError;
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/NVMAddressTranslator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/NVMAddressTranslator.java b/core/src/main/java/org/apache/mnemonic/NVMAddressTranslator.java
deleted file mode 100644
index 0c4ff32..0000000
--- a/core/src/main/java/org/apache/mnemonic/NVMAddressTranslator.java
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * translate persistent memory address for allocator
- *
- */
-public interface NVMAddressTranslator {
-
- /**
- * calculate the portable address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the portable address
- */
- long getPortableAddress(long addr);
-
- /**
- * calculate the effective address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the effective address
- */
- long getEffectiveAddress(long addr);
-
- /**
- * get the base address
- *
- * @return the base address
- */
- long getBaseAddress();
-
- /**
- * set the base address for calculation
- *
- * @param addr
- * the base address
- *
- */
- long setBaseAddress(long addr);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/NonVolatileMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/NonVolatileMemAllocator.java b/core/src/main/java/org/apache/mnemonic/NonVolatileMemAllocator.java
deleted file mode 100644
index 6c07c31..0000000
--- a/core/src/main/java/org/apache/mnemonic/NonVolatileMemAllocator.java
+++ /dev/null
@@ -1,474 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.nio.ByteBuffer;
-
-import org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
-import org.flowcomputing.commons.resgc.ResCollector;
-import org.flowcomputing.commons.resgc.ResReclaim;
-
-/**
- * manage a big native persistent memory pool through libpmalloc.so provided by
- * pmalloc project.
- *
- *
- */
-public class NonVolatileMemAllocator extends CommonDurableAllocator<NonVolatileMemAllocator>
- implements NVMAddressTranslator {
-
- private boolean m_activegc = true;
- private long m_gctimeout = 100;
- private long m_nid = -1;
- private long b_addr = 0;
- private NonVolatileMemoryAllocatorService m_nvmasvc = null;
-
- /**
- * Constructor, it initializes and allocate a memory pool from specified uri
- * location with specified capacity and an allocator service instance.
- * usually, the uri points to a mounted non-volatile memory device or a
- * location of file system.
- *
- * @param nvmasvc
- * the non-volatile memory allocation service instance
- *
- * @param capacity
- * the capacity of memory pool
- *
- * @param uri
- * the location of memory pool will be created
- *
- * @param isnew
- * a place holder, always specify it as true
- */
- public NonVolatileMemAllocator(NonVolatileMemoryAllocatorService nvmasvc, long capacity, String uri, boolean isnew) {
- assert null != nvmasvc : "NonVolatileMemoryAllocatorService object is null";
- if (capacity <= 0) {
- throw new IllegalArgumentException("BigDataPMemAllocator cannot be initialized with capacity <= 0.");
- }
-
- m_nvmasvc = nvmasvc;
-
- m_nid = m_nvmasvc.init(capacity, uri, isnew);
- b_addr = m_nvmasvc.getBaseAddress(m_nid);
-
- /**
- * create a resource collector to release specified chunk that backed by
- * underlying big memory pool.
- */
- m_chunkcollector = new ResCollector<MemChunkHolder<NonVolatileMemAllocator>, Long>(new ResReclaim<Long>() {
- @Override
- public void reclaim(Long mres) {
- // System.out.println(String.format("Reclaim: %X", mres));
- boolean cb_reclaimed = false;
- if (null != m_chunkreclaimer) {
- cb_reclaimed = m_chunkreclaimer.reclaim(mres, null);
- }
- if (!cb_reclaimed) {
- m_nvmasvc.free(m_nid, mres);
- mres = null;
- }
- }
- });
-
- /**
- * create a resource collector to release specified bytebuffer that backed
- * by underlying big memory pool.
- */
- m_bufcollector = new ResCollector<MemBufferHolder<NonVolatileMemAllocator>, ByteBuffer>(
- new ResReclaim<ByteBuffer>() {
- @Override
- public void reclaim(ByteBuffer mres) {
- boolean cb_reclaimed = false;
- if (null != m_bufferreclaimer) {
- cb_reclaimed = m_bufferreclaimer.reclaim(mres, Long.valueOf(mres.capacity()));
- }
- if (!cb_reclaimed) {
- m_nvmasvc.destroyByteBuffer(m_nid, mres);
- mres = null;
- }
- }
- });
- }
-
- /**
- * enable active garbage collection. the GC will be forced to collect garbages
- * when there is no more space for current allocation request.
- *
- * @param timeout
- * the timeout is used to yield for GC performing
- */
- @Override
- public NonVolatileMemAllocator enableActiveGC(long timeout) {
- m_activegc = true;
- m_gctimeout = timeout;
- return this;
- }
-
- /**
- * disable active garbage collection.
- *
- */
- @Override
- public NonVolatileMemAllocator disableActiveGC() {
- m_activegc = false;
- return this;
- }
-
- /**
- * Release the memory pool and close it.
- *
- */
- @Override
- public void close() {
- super.close();
- m_nvmasvc.close(m_nid);
- }
-
- /**
- * force to synchronize uncommitted data to backed memory pool (this is a
- * placeholder).
- *
- */
- @Override
- public void sync() {
- }
-
- /**
- * re-size a specified chunk on its backed memory pool.
- *
- * @param mholder
- * the holder of memory chunk. it can be null.
- *
- * @param size
- * specify a new size of memory chunk
- *
- * @return the resized memory chunk handler
- */
- @Override
- public MemChunkHolder<NonVolatileMemAllocator> resizeChunk(MemChunkHolder<NonVolatileMemAllocator> mholder,
- long size) {
- MemChunkHolder<NonVolatileMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- Long addr = m_nvmasvc.reallocate(m_nid, mholder.get(), size, true);
- if (0 == addr && m_activegc) {
- m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
- addr = m_nvmasvc.reallocate(m_nid, mholder.get(), size, true);
- }
- if (0 != addr) {
- mholder.clear();
- mholder.destroy();
- ret = new MemChunkHolder<NonVolatileMemAllocator>(this, addr, size);
- if (ac) {
- m_chunkcollector.register(ret);
- }
- }
- }
- return ret;
- }
-
- /**
- * resize a specified buffer on its backed memory pool.
- *
- * @param mholder
- * the holder of memory buffer. it can be null.
- *
- * @param size
- * specify a new size of memory chunk
- *
- * @return the resized memory buffer handler
- *
- */
- @Override
- public MemBufferHolder<NonVolatileMemAllocator> resizeBuffer(MemBufferHolder<NonVolatileMemAllocator> mholder,
- long size) {
- MemBufferHolder<NonVolatileMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- int bufpos = mholder.get().position();
- int buflimit = mholder.get().limit();
- ByteBuffer buf = m_nvmasvc.resizeByteBuffer(m_nid, mholder.get(), size);
- if (null == buf && m_activegc) {
- m_bufcollector.waitReclaimCoolDown(m_gctimeout);
- buf = m_nvmasvc.resizeByteBuffer(m_nid, mholder.get(), size);
- }
- if (null != buf) {
- mholder.clear();
- mholder.destroy();
- buf.position(bufpos <= size ? bufpos : 0);
- buf.limit(buflimit <= size ? buflimit : (int) size);
- ret = new MemBufferHolder<NonVolatileMemAllocator>(this, buf);
- if (ac) {
- m_bufcollector.register(ret);
- }
- }
- }
- return ret;
- }
-
- /**
- * create a memory chunk that is managed by its holder.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @param autoreclaim
- * specify whether or not to reclaim this chunk automatically
- *
- * @return a holder contains a memory chunk
- */
- @Override
- public MemChunkHolder<NonVolatileMemAllocator> createChunk(long size, boolean autoreclaim) {
- MemChunkHolder<NonVolatileMemAllocator> ret = null;
- Long addr = m_nvmasvc.allocate(m_nid, size, true);
- if ((null == addr || 0 == addr) && m_activegc) {
- m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
- addr = m_nvmasvc.allocate(m_nid, size, true);
- }
- if (null != addr && 0 != addr) {
- ret = new MemChunkHolder<NonVolatileMemAllocator>(this, addr, size);
- ret.setCollector(m_chunkcollector);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * create a memory buffer that is managed by its holder.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @param autoreclaim
- * specify whether or not to reclaim this buffer automatically
- *
- * @return a holder contains a memory buffer
- */
- @Override
- public MemBufferHolder<NonVolatileMemAllocator> createBuffer(long size, boolean autoreclaim) {
- MemBufferHolder<NonVolatileMemAllocator> ret = null;
- ByteBuffer bb = m_nvmasvc.createByteBuffer(m_nid, size);
- if (null == bb && m_activegc) {
- m_bufcollector.waitReclaimCoolDown(m_gctimeout);
- bb = m_nvmasvc.createByteBuffer(m_nid, size);
- }
- if (null != bb) {
- ret = new MemBufferHolder<NonVolatileMemAllocator>(this, bb);
- ret.setCollector(m_bufcollector);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * retrieve a memory buffer from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory buffer to retrieve
- *
- * @param autoreclaim
- * specify whether this retrieved memory buffer can be reclaimed
- * automatically or not
- *
- * @return a holder contains the retrieved memory buffer
- */
- @Override
- public MemBufferHolder<NonVolatileMemAllocator> retrieveBuffer(long phandler, boolean autoreclaim) {
- MemBufferHolder<NonVolatileMemAllocator> ret = null;
- ByteBuffer bb = m_nvmasvc.retrieveByteBuffer(m_nid, getEffectiveAddress(phandler));
- if (null != bb) {
- ret = new MemBufferHolder<NonVolatileMemAllocator>(this, bb);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * retrieve a memory chunk from its backed memory allocator.
- *
- * @param phandler
- * specify the handler of memory chunk to retrieve
- *
- * @param autoreclaim
- * specify whether this retrieved memory chunk can be reclaimed
- * automatically or not
- *
- * @return a holder contains the retrieved memory chunk
- */
- @Override
- public MemChunkHolder<NonVolatileMemAllocator> retrieveChunk(long phandler, boolean autoreclaim) {
- MemChunkHolder<NonVolatileMemAllocator> ret = null;
- long eaddr = getEffectiveAddress(phandler);
- long sz = m_nvmasvc.retrieveSize(m_nid, eaddr);
- if (sz > 0L) {
- ret = new MemChunkHolder<NonVolatileMemAllocator>(this, eaddr, sz);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- }
- return ret;
- }
-
- /**
- * get the handler from a memory buffer holder.
- *
- * @param mbuf
- * specify the memory buffer holder
- *
- * @return a handler that could be used to retrieve its memory buffer
- */
- @Override
- public long getBufferHandler(MemBufferHolder<NonVolatileMemAllocator> mbuf) {
- return getPortableAddress(m_nvmasvc.getByteBufferHandler(m_nid, mbuf.get()));
- }
-
- /**
- * get the handler from a memory chunk holder.
- *
- * @param mchunk
- * specify the memory chunk holder
- *
- * @return a handler that could be used to retrieve its memory chunk
- */
- @Override
- public long getChunkHandler(MemChunkHolder<NonVolatileMemAllocator> mchunk) {
- return getPortableAddress(mchunk.get());
- }
-
- /**
- * determine whether this allocator supports to store non-volatile handler or
- * not. (it is a placeholder)
- *
- * @return true if there is
- */
- @Override
- public boolean hasDurableHandlerStore() {
- return true;
- }
-
- /**
- * start a application level transaction on this allocator. (it is a place
- * holder)
- *
- */
- @Override
- public void beginTransaction() {
- throw new UnsupportedOperationException("Transaction Unsupported.");
- }
-
- /**
- * end a application level transaction on this allocator. (it is a place
- * holder)
- *
- */
- @Override
- public void endTransaction() {
- throw new UnsupportedOperationException("Transaction Unsupported.");
- }
-
- /**
- * set a handler on key.
- *
- * @param key
- * the key to set its value
- *
- * @param handler
- * the handler
- */
- public void setHandler(long key, long handler) {
- m_nvmasvc.setHandler(m_nid, key, handler);
- }
-
- /**
- * get a handler value.
- *
- * @param key
- * the key to set its value
- *
- * @return the value of handler
- */
- public long getHandler(long key) {
- return m_nvmasvc.getHandler(m_nid, key);
- }
-
- /**
- * return the capacity of non-volatile handler store.
- *
- * @return the capacity of handler store
- *
- */
- public long handlerCapacity() {
- return m_nvmasvc.handlerCapacity(m_nid);
- }
-
- /**
- * calculate the portable address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the portable address
- */
- @Override
- public long getPortableAddress(long addr) {
- return addr - b_addr;
- }
-
- /**
- * calculate the effective address
- *
- * @param addr
- * the address to be calculated
- *
- * @return the effective address
- */
- @Override
- public long getEffectiveAddress(long addr) {
- return addr + b_addr;
- }
-
- /**
- * get the base address
- *
- * @return the base address
- */
- @Override
- public long getBaseAddress() {
- return b_addr;
- }
-
- /**
- * set the base address for calculation
- *
- * @param addr
- * the base address
- *
- */
- @Override
- public long setBaseAddress(long addr) {
- return b_addr = addr;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/OutOfHybridMemory.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/OutOfHybridMemory.java b/core/src/main/java/org/apache/mnemonic/OutOfHybridMemory.java
deleted file mode 100644
index d9c99ca..0000000
--- a/core/src/main/java/org/apache/mnemonic/OutOfHybridMemory.java
+++ /dev/null
@@ -1,32 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * this is an exception that should be throw once out of persistent memory
- *
- */
-
-public class OutOfHybridMemory extends RuntimeException {
-
- private static final long serialVersionUID = -6315943783592441148L;
-
- public OutOfHybridMemory(String s) {
- super(s);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/Reclaim.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Reclaim.java b/core/src/main/java/org/apache/mnemonic/Reclaim.java
deleted file mode 100644
index 84b85ac..0000000
--- a/core/src/main/java/org/apache/mnemonic/Reclaim.java
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- * an interface to reclaim its memory resource.
- *
- */
-public interface Reclaim<MRES> {
-
- /**
- * reclaim specified resources.
- *
- * @param mres
- * a resource to be reclaimed
- *
- * @param size
- * the size of resource, it will be set as null if unknown
- *
- * @return <tt>true</tt> if this resource has been reclaimed by this callback.
- * <tt>false</tt> if this resource needs to be claimed by default
- * reclaimer
- */
- boolean reclaim(MRES mres, Long size);
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/RetrieveDurableEntityError.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/RetrieveDurableEntityError.java b/core/src/main/java/org/apache/mnemonic/RetrieveDurableEntityError.java
deleted file mode 100644
index 4ff5a3e..0000000
--- a/core/src/main/java/org/apache/mnemonic/RetrieveDurableEntityError.java
+++ /dev/null
@@ -1,35 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-/**
- *
- *
- */
-
-public class RetrieveDurableEntityError extends RuntimeException {
-
- /**
- *
- */
- private static final long serialVersionUID = 3985459401986971131L;
-
- public RetrieveDurableEntityError(String s) {
- super(s);
- }
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java b/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
deleted file mode 100644
index 1d68937..0000000
--- a/core/src/main/java/org/apache/mnemonic/SysMemAllocator.java
+++ /dev/null
@@ -1,303 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.lang.reflect.Field;
-import java.nio.ByteBuffer;
-import java.util.Map;
-import java.util.concurrent.ConcurrentHashMap;
-import java.util.concurrent.atomic.AtomicLong;
-
-import org.flowcomputing.commons.resgc.ResCollector;
-import org.flowcomputing.commons.resgc.ResReclaim;
-
-import sun.misc.Unsafe;
-import sun.misc.Cleaner;
-
-/**
- * manage a system memory pool as a internal volatile allocator
- *
- */
-@SuppressWarnings("restriction")
-public class SysMemAllocator extends CommonAllocator<SysMemAllocator> {
-
- private boolean m_activegc = true;
- private long m_gctimeout = 100;
- private Unsafe m_unsafe = null;
- private AtomicLong currentMemory = new AtomicLong(0L);
- private long maxStoreCapacity = 0L;
- private Map<Long, Long> m_chunksize = new ConcurrentHashMap<Long, Long>();
-
- /**
- * Constructor, it initialize and allocate a memory pool from Java off-heap
- * with specified capacity.
- *
- * @param capacity
- * specify the capacity of a system memory pool
- *
- * @param isnew
- * a place holder, always specify it as true
- *
- * @throws Exception
- * fail to retrieve Unsafe object
- *
- */
- public SysMemAllocator(long capacity, boolean isnew) throws Exception {
- m_unsafe = Utils.getUnsafe();
- maxStoreCapacity = capacity;
- /**
- * create a resource collector to release specified bytebuffer that backed
- * by Java off-heap.
- */
- m_bufcollector = new ResCollector<MemBufferHolder<SysMemAllocator>, ByteBuffer>(new ResReclaim<ByteBuffer>() {
- @Override
- public synchronized void reclaim(ByteBuffer mres) {
- Long sz = Long.valueOf(mres.capacity());
- boolean cb_reclaimed = false;
- if (null != m_bufferreclaimer) {
- cb_reclaimed = m_bufferreclaimer.reclaim(mres, sz);
- }
- if (!cb_reclaimed) {
- try {
- Field cleanerField;
- cleanerField = mres.getClass().getDeclaredField("cleaner");
- cleanerField.setAccessible(true);
- Cleaner cleaner = (Cleaner) cleanerField.get(mres);
- cleaner.clean();
- } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
- e.printStackTrace();
- }
- mres = null;
- }
- currentMemory.addAndGet(-sz);
- }
- });
-
- /**
- * create a resource collector to release specified chunk that backed by
- * Java off-heap.
- */
- m_chunkcollector = new ResCollector<MemChunkHolder<SysMemAllocator>, Long>(new ResReclaim<Long>() {
- @Override
- public synchronized void reclaim(Long mres) {
- // System.out.println(String.format("Reclaim: %X ...", mres));
- Long sz = m_chunksize.remove(mres);
- boolean cb_reclaimed = false;
- if (null != m_chunkreclaimer) {
- cb_reclaimed = m_chunkreclaimer.reclaim(mres, sz);
- }
- if (!cb_reclaimed) {
- m_unsafe.freeMemory(mres);
- mres = null;
- }
- if (null != sz) {
- currentMemory.addAndGet(-sz);
- }
- }
- });
- }
-
- /**
- * enable active garbage collection. the GC will be forced to collect garbages
- * when there is no more space for current allocation request.
- *
- * @param timeout
- * the timeout is used to yield for GC performing
- *
- * @return this allocator
- */
- @Override
- public SysMemAllocator enableActiveGC(long timeout) {
- m_activegc = true;
- m_gctimeout = timeout;
- return this;
- }
-
- /**
- * disable active garbage collection.
- *
- * @return this allocator
- */
- @Override
- public SysMemAllocator disableActiveGC() {
- m_activegc = false;
- return this;
- }
-
- /**
- * release the memory pool and close it.
- *
- */
- @Override
- public void close() {
- super.close();
- }
-
- /**
- * force to synchronize uncommitted data to backed memory pool (this is a
- * placeholder).
- *
- */
- @Override
- public void sync() {
- }
-
- /**
- * re-size a specified chunk on its backed memory pool.
- *
- * @param mholder
- * the holder of memory chunk. it can be null.
- *
- * @param size
- * specify a new size of memory chunk
- *
- * @return the resized memory chunk handler
- */
- @Override
- public MemChunkHolder<SysMemAllocator> resizeChunk(MemChunkHolder<SysMemAllocator> mholder, long size) {
- MemChunkHolder<SysMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- Long addr = m_unsafe.reallocateMemory(mholder.get(), size);
- if (0 != addr) {
- mholder.clear();
- mholder.destroy();
- ret = new MemChunkHolder<SysMemAllocator>(this, addr, size);
- if (ac) {
- m_chunkcollector.register(ret);
- }
- }
- }
- }
- return ret;
- }
-
- /**
- * resize a specified buffer on its backed memory pool.
- *
- * @param mholder
- * the holder of memory buffer. it can be null.
- *
- * @param size
- * specify a new size of memory chunk
- *
- * @return the resized memory buffer handler
- *
- */
- @Override
- public MemBufferHolder<SysMemAllocator> resizeBuffer(MemBufferHolder<SysMemAllocator> mholder, long size) {
- MemBufferHolder<SysMemAllocator> ret = null;
- boolean ac = null != mholder.getRefId();
- if (size > 0) {
- int bufpos = mholder.get().position();
- int buflimit = mholder.get().limit();
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_bufcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- ByteBuffer buf = Utils.resizeByteBuffer(mholder.get(), size);
- if (null != buf) {
- mholder.clear();
- mholder.destroy();
- buf.position(bufpos <= size ? bufpos : 0);
- buf.limit(buflimit <= size ? buflimit : (int) size);
- ret = new MemBufferHolder<SysMemAllocator>(this, buf);
- if (ac) {
- m_bufcollector.register(ret);
- }
- }
- }
- }
- return ret;
- }
-
- /**
- * create a memory chunk that is managed by its holder.
- *
- * @param size
- * specify the size of memory chunk
- *
- * @param autoreclaim
- * specify whether or not to reclaim this chunk automatically
- *
- * @return a holder contains a memory chunk
- */
- @Override
- public MemChunkHolder<SysMemAllocator> createChunk(long size, boolean autoreclaim) {
- MemChunkHolder<SysMemAllocator> ret = null;
- Long addr = null;
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_chunkcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- addr = m_unsafe.allocateMemory(size);
- }
- if (null != addr && 0 != addr) {
- ret = new MemChunkHolder<SysMemAllocator>(this, addr, size);
- ret.setCollector(m_chunkcollector);
- if (autoreclaim) {
- m_chunkcollector.register(ret);
- }
- m_chunksize.put(addr, size);
- currentMemory.getAndAdd(size);
- }
- return ret;
- }
-
- /**
- * create a memory buffer that is managed by its holder.
- *
- * @param size
- * specify the size of memory buffer
- *
- * @return a holder contains a memory buffer
- */
- @Override
- public MemBufferHolder<SysMemAllocator> createBuffer(long size, boolean autoreclaim) {
- MemBufferHolder<SysMemAllocator> ret = null;
- ByteBuffer bb = null;
- if (currentMemory.get() + size > maxStoreCapacity) {
- if (m_activegc) {
- m_bufcollector.waitReclaimCoolDown(m_gctimeout);
- }
- }
- if (currentMemory.get() + size <= maxStoreCapacity) {
- bb = ByteBuffer.allocateDirect((int) size);
- }
- if (null != bb) {
- ret = new MemBufferHolder<SysMemAllocator>(this, bb);
- ret.setCollector(m_bufcollector);
- if (autoreclaim) {
- m_bufcollector.register(ret);
- }
- currentMemory.getAndAdd(size);
- }
- return ret;
- }
-
-}
http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/30836536/core/src/main/java/org/apache/mnemonic/Utils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/mnemonic/Utils.java b/core/src/main/java/org/apache/mnemonic/Utils.java
deleted file mode 100644
index 49794db..0000000
--- a/core/src/main/java/org/apache/mnemonic/Utils.java
+++ /dev/null
@@ -1,399 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.mnemonic;
-
-import java.lang.reflect.Field;
-import java.nio.ByteBuffer;
-import java.text.SimpleDateFormat;
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.Random;
-import java.util.UUID;
-
-import sun.misc.Unsafe;
-import java.util.Iterator;
-import java.util.List;
-import java.util.ServiceLoader;
-import org.apache.mnemonic.service.allocatorservice.VolatileMemoryAllocatorService;
-import org.apache.mnemonic.service.allocatorservice.NonVolatileMemoryAllocatorService;
-
-/**
- * <p>
- * Utilities for project.
- * </p>
- *
- */
-@SuppressWarnings("restriction")
-public class Utils {
- public static final String ANSI_RESET = "\u001B[0m";
- public static final String ANSI_BLACK = "\u001B[30m";
- public static final String ANSI_RED = "\u001B[31m";
- public static final String ANSI_GREEN = "\u001B[32m";
- public static final String ANSI_YELLOW = "\u001B[33m";
- public static final String ANSI_BLUE = "\u001B[34m";
- public static final String ANSI_PURPLE = "\u001B[35m";
- public static final String ANSI_CYAN = "\u001B[36m";
- public static final String ANSI_WHITE = "\u001B[37m";
-
- private static Unsafe m_unsafe = null;
-
- private static ServiceLoader<VolatileMemoryAllocatorService> m_vmasvcloader = null;
- private static ServiceLoader<NonVolatileMemoryAllocatorService> m_nvmasvcloader = null;
-
- /**
- * retrieve a volatile memory allocator service
- *
- * @param id
- * specify a name of allocator to retrieve
- *
- * @return the volatile memory allocator service instance
- */
- public static VolatileMemoryAllocatorService getVolatileMemoryAllocatorService(String id) {
- return getVolatileMemoryAllocatorService(id, true);
- }
-
- /**
- * retrieve a volatile memory allocator service
- *
- * @param id
- * specify a name of allocator to retrieve
- *
- * @param allownvmsvc
- * specify whether allow to treat non-volatile memory allocator as
- * volatile one during searching
- *
- * @return the volatile memory allocator service instance
- */
- public static VolatileMemoryAllocatorService getVolatileMemoryAllocatorService(String id, boolean allownvmsvc) {
- VolatileMemoryAllocatorService ret = null;
- if (null == m_vmasvcloader) {
- m_vmasvcloader = ServiceLoader.load(VolatileMemoryAllocatorService.class);
- }
- Iterator<VolatileMemoryAllocatorService> svcit = m_vmasvcloader.iterator();
- VolatileMemoryAllocatorService svc = null;
- while (null == ret && svcit.hasNext()) {
- svc = svcit.next();
- if (svc.getServiceId().equals(id)) {
- ret = svc;
- }
- }
- if (null == ret && allownvmsvc) {
- ret = getNonVolatileMemoryAllocatorService(id);
- }
- assert null != ret : "VolatileMemoryAllocatorService \'" + id + "\' not found!";
- return ret;
- }
-
- /**
- * retrieve a non-volatile memory allocator service
- *
- * @param id
- * specify a name of allocator to retrieve
- *
- * @return the non-volatile memory allocator service instance
- */
- public static NonVolatileMemoryAllocatorService getNonVolatileMemoryAllocatorService(String id) {
- NonVolatileMemoryAllocatorService ret = null;
- if (null == m_nvmasvcloader) {
- m_nvmasvcloader = ServiceLoader.load(NonVolatileMemoryAllocatorService.class);
- }
- Iterator<NonVolatileMemoryAllocatorService> svcit = m_nvmasvcloader.iterator();
- NonVolatileMemoryAllocatorService svc = null;
- while (null == ret && svcit.hasNext()) {
- svc = svcit.next();
- if (svc.getServiceId().equals(id)) {
- ret = svc;
- }
- }
- assert null != ret : "NonVolatileMemoryAllocatorService \'" + id + "\' not found!";
- return ret;
- }
-
- /**
- * Generates a unique name that contains current timestamp.
- *
- * @param format
- * the template that is used to generate unique name.
- *
- * @return unique path name.
- */
- public static String genUniquePathname(String format) {
- String ret = null;
- if (null != format && !format.isEmpty()) {
- ret = String.format(format, (new SimpleDateFormat("ddMMyy-hhmmss.SSS").format(new Date())));
- }
- return ret;
- }
-
- /**
- * retrieve the usage of memory.
- *
- * @return the size of memory has been occupied
- */
- public static long getMemoryUse(long timeout) {
- putOutTheGarbage(timeout);
- long totalMemory = Runtime.getRuntime().totalMemory();
- putOutTheGarbage(timeout);
- long freeMemory = Runtime.getRuntime().freeMemory();
- return (totalMemory - freeMemory);
- }
-
- /**
- * run garbage collections.
- */
- private static void putOutTheGarbage(long timeout) {
- collectGarbage(timeout);
- collectGarbage(timeout);
- }
-
- /**
- * run a garbage collection.
- */
- public static void collectGarbage(long timeout) {
- try {
- System.gc();
- Thread.sleep(timeout);
- System.runFinalization();
- Thread.sleep(timeout);
- } catch (InterruptedException ex) {
- ex.printStackTrace();
- }
- }
-
- /**
- * Retrieve an Unsafe object.
- *
- * @throws Exception
- * Error when get Unsafe object from runtime
- *
- * @return an unsafe object
- */
- public static Unsafe getUnsafe() throws Exception {
- if (null == m_unsafe) {
- Field field = sun.misc.Unsafe.class.getDeclaredField("theUnsafe");
- field.setAccessible(true);
- m_unsafe = (sun.misc.Unsafe) field.get(null);
- }
- return m_unsafe;
- }
-
- /**
- * resize a bytebuffer with a new instance
- *
- * @param buf
- * specify a buf to resize
- *
- * @param size
- * specify the size for resizing
- *
- * @return the resized bytebuffer instance
- */
- public static ByteBuffer resizeByteBuffer(ByteBuffer buf, long size) {
- ByteBuffer ret = ByteBuffer.allocateDirect((int) size);
- if (ret != null) {
- if (null != buf) {
- ret.put(buf);
- ret.flip();
- }
- }
- return ret;
- }
-
- /**
- * create a new instance of Random using default fixed seed
- *
- * @return the instance of Random
- */
- public static Random createRandom() {
- return createRandom(0L);
- }
-
- /**
- * create a new instance of Random
- *
- * @param rgenseed
- * specify a random seed
- *
- * @return the instance of Random
- */
- public static Random createRandom(long rgenseed) {
- Random ret = new Random();
- if (0L == rgenseed) {
- rgenseed = System.currentTimeMillis();
- System.out.println("Random number generator seed is " + rgenseed);
- } else {
- System.out.println("Fixed Random number generator seed is " + rgenseed);
- }
- ret.setSeed(rgenseed);
- return ret;
- }
-
- /**
- * generate a random string with fixed length
- *
- * @return the random string
- */
- public static String genRandomString() {
- return genRandomString(6);
- }
-
- /**
- * generate a random string
- *
- * @param len
- * specify the length of this random string
- *
- * @return the random string
- */
- public static String genRandomString(int len) {
- return UUID.randomUUID().toString().replaceAll("-", "").toUpperCase().substring(0, len);
- }
-
- /**
- * assert the equality of two generic objects using compareTo() operator
- *
- * @param <T>
- * the type of comparable objects
- *
- * @param actual
- * specify a object to be compared
- *
- * @param expected
- * specify a object to be expected
- *
- * @return true if equal according to compareTo()
- */
- public static <T extends Comparable<T>> boolean assertComparison(T actual, T expected) {
- boolean ret = false;
- if ((expected == null) && (actual == null)) {
- ret = true;
- } else if (expected != null) {
- ret = expected.compareTo(actual) == 0;
- }
- return ret;
- }
-
- /**
- * convert a long array to a initializer literal string.
- *
- * @param larr
- * specify a long array to be converted
- *
- * @return a literal string represent the initializer
- */
- public static String toInitLiteral(long[] larr) {
- return Arrays.toString(larr).replaceAll("\\[", "{").replaceAll("\\]", "}");
- }
-
- /**
- * convert a list of long array to a initializer literal string.
- *
- * @param llarr
- * specify a list of long array to be converted
- *
- * @return a literal string represent the initializer
- */
- public static String toInitLiteral(List<long[]> llarr) {
- List<String> slist = new ArrayList<String>();
- for (long[] larr : llarr) {
- slist.add(toInitLiteral(larr));
- }
- return "{" + String.join(",", slist) + "}";
- }
-
- /**
- * retrieve a set of native field info from a list of object field info
- * according to the field id info. it forms a value info stack for native code
- * to use as one standardized parameter
- *
- * @param objstack
- * a stack of object info retrieved from
- * Durable.getNativeFieldInfo(), order matters
- *
- * @param fidinfostack
- * a stack of field id in the form of (next_fid, next_level_fid)
- * order follows objstack the last next_level_fid specifies the
- * value's fid. the last item of next_fid could be null if there is
- * no next node if it is null that means the last item is a object
- * instead of node
- *
- * @return the stack of native field info
- *
- */
- public static List<long[]> getNativeParamForm(List<long[][]> objstack, long[][] fidinfostack) {
- List<long[]> ret = new ArrayList<long[]>();
- if (null == objstack || null == fidinfostack || fidinfostack.length != objstack.size()) {
- throw new IllegalArgumentException("Not the same depth");
- }
- for (int idx = 0; idx < fidinfostack.length; ++idx) {
- ret.add(genNativeStackItem(objstack.get(idx), fidinfostack[idx], idx == fidinfostack.length - 1));
- }
- return ret;
- }
-
- /**
- * generate an item of native stack.
- *
- * @param oinfo
- * a object field info
- *
- * @param fidinfo
- * a pair of field id info
- *
- * @param allowfidnull
- * allow the first field id is null
- *
- * @return the native item
- */
- public static long[] genNativeStackItem(long[][] oinfo, long[] fidinfo, boolean allowfidnull) {
- long[] ret = new long[4];
- long fid;
- boolean found;
- if (fidinfo.length != 2) {
- throw new IllegalArgumentException("the length of field id array is not exactly 2");
- }
- for (int idx = 0; idx < fidinfo.length; ++idx) {
- ret[idx * 2] = 0;
- ret[idx * 2 + 1] = 0;
- fid = fidinfo[idx];
- if (fid <= 0) {
- if (allowfidnull && 0 == idx) {
- continue;
- } else {
- throw new IllegalArgumentException("the field id is not greater than 0");
- }
- }
- found = false;
- for (long[] finfo : oinfo) {
- if (finfo.length != 3) {
- throw new IllegalArgumentException("the length of field array is not exactly 3");
- }
- if (fid == finfo[0]) {
- ret[idx * 2] = finfo[1];
- ret[idx * 2 + 1] = finfo[2];
- found = true;
- }
- }
- if (!found) {
- throw new IllegalArgumentException("field id not found");
- }
- }
- return ret;
- }
-}