Skip to content

Commit 09a468f

Browse files
committed
MNEMONIC-298:construct Java based volatile memory service class
1 parent 48b3767 commit 09a468f

File tree

4 files changed

+362
-0
lines changed

4 files changed

+362
-0
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?xml version="1.0"?>
2+
3+
<!--
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
-->
21+
22+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
23+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<modelVersion>4.0.0</modelVersion>
25+
<parent>
26+
<groupId>org.apache.mnemonic</groupId>
27+
<artifactId>mnemonic-memory-services</artifactId>
28+
<version>0.8.0-incubating-SNAPSHOT</version>
29+
</parent>
30+
31+
<artifactId>mnemonic-java-vmem-service</artifactId>
32+
<name>mnemonic-java-vmem-service</name>
33+
34+
<dependencies>
35+
<dependency>
36+
<groupId>org.testng</groupId>
37+
<artifactId>testng</artifactId>
38+
</dependency>
39+
</dependencies>
40+
41+
<build>
42+
<resources>
43+
<resource>
44+
<directory>src/main/resources</directory>
45+
</resource>
46+
</resources>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
</plugin>
52+
<plugin>
53+
<groupId>org.apache.maven.plugins</groupId>
54+
<artifactId>maven-scm-publish-plugin</artifactId>
55+
<configuration>
56+
<checkoutDirectory>${project.build.directory}/scmpublish</checkoutDirectory>
57+
<checkinComment>Publishing javadoc for ${project.artifactId}:${project.version}</checkinComment>
58+
<content>${project.reporting.outputDirectory}/apidocs</content>
59+
<skipDeletedFiles>true</skipDeletedFiles>
60+
<pubScmUrl>scm:git:https://github.com/bigdata-memory/mnenomic-project.git</pubScmUrl>
61+
<scmBranch>gh-pages</scmBranch> <!-- branch with static site -->
62+
</configuration>
63+
</plugin>
64+
<plugin>
65+
<groupId>org.apache.maven.plugins</groupId>
66+
<artifactId>maven-jar-plugin</artifactId>
67+
</plugin>
68+
<plugin>
69+
<groupId>org.apache.maven.plugins</groupId>
70+
<artifactId>maven-shade-plugin</artifactId>
71+
</plugin>
72+
</plugins>
73+
</build>
74+
75+
<profiles>
76+
<profile>
77+
<id>doc</id>
78+
<build>
79+
<plugins>
80+
<plugin>
81+
<groupId>org.apache.maven.plugins</groupId>
82+
<artifactId>maven-javadoc-plugin</artifactId>
83+
<configuration>
84+
<aggregate>true</aggregate>
85+
<show>public</show>
86+
<nohelp>true</nohelp>
87+
<header>Mnenomic-JavaVMemAllocator, ${project.version}</header>
88+
<footer>Mnenomic-JavaVMemAllocator, ${project.version}</footer>
89+
<doctitle>Mnenomic, ${project.version}</doctitle>
90+
<links>
91+
<link>http://static.springsource.org/spring/docs/3.0.x/javadoc-api/</link>
92+
<additionalparam>-Xdoclint:all -Xdoclint:-missing</additionalparam>
93+
</links>
94+
</configuration>
95+
<executions>
96+
<execution>
97+
<id>attach-javadocs</id>
98+
<goals>
99+
<goal>jar</goal>
100+
</goals>
101+
</execution>
102+
</executions>
103+
</plugin>
104+
</plugins>
105+
</build>
106+
</profile>
107+
<profile>
108+
<id>test</id>
109+
<build>
110+
<plugins>
111+
<plugin>
112+
<groupId>org.apache.maven.plugins</groupId>
113+
<artifactId>maven-surefire-plugin</artifactId>
114+
<configuration>
115+
<argLine>-Xmx2g -XX:MaxPermSize=1g</argLine>
116+
<suiteXmlFiles>
117+
<suiteXmlFile>src/test/resources/testng.xml</suiteXmlFile>
118+
</suiteXmlFiles>
119+
</configuration>
120+
</plugin>
121+
</plugins>
122+
</build>
123+
</profile>
124+
</profiles>
125+
126+
127+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.mnemonic.service.memoryservice.internal;
19+
20+
import org.apache.mnemonic.service.memoryservice.VolatileMemoryAllocatorService;
21+
import org.apache.mnemonic.ConfigurationException;
22+
import org.flowcomputing.commons.primitives.NativeLibraryLoader;
23+
24+
import java.nio.ByteBuffer;
25+
import java.util.Collections;
26+
import java.util.HashMap;
27+
import java.util.Map;
28+
29+
public class JavaVMemServiceImpl implements VolatileMemoryAllocatorService {
30+
31+
protected Map<Long, Long> m_info = Collections.synchronizedMap(new HashMap<Long, Long>());
32+
protected ArrayList<RandomAccessFile> mem_pool = new ArrayList<RandomAccessFile>();
33+
34+
@Override
35+
public String getServiceId() {
36+
return "javavmem";
37+
}
38+
39+
@Override
40+
public long init(long capacity, String uri, boolean isnew) {
41+
FileChannel channel = null;
42+
RandomAccessFile mappedFile = null;
43+
long cp = null;
44+
long ret = null;
45+
46+
47+
if (uri == null || uri.length == 0) {
48+
throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
49+
}
50+
if (capacity <= 0) {
51+
throw new ConfigurationException("Please supply the capacity");
52+
}
53+
54+
File file = new File(uri);
55+
56+
if (file.exists() && !file.isFile()) {
57+
throw new ConfigurationException(String.format("Please supply the file path: %s.", uri));
58+
}
59+
if (file.isFile() && file.length() <= 0) {
60+
throw new ConfigurationException("File length should be more than zero.");
61+
}
62+
63+
if (isnew) {
64+
if (file.exists()) {
65+
if (!file.delete()) {
66+
throw new ConfigurationException(String.format("Failed to delete the file: %s.", uri));
67+
}
68+
}
69+
mappedFile = new RandomAccessFile(file, "rw");
70+
mappedFile.setLength(capacity);
71+
} else {
72+
mappedFile = new RandomAccessFile(file, "rw");
73+
}
74+
75+
cp = file.length();
76+
mem_pool.add(mappedFile);
77+
ret = mem_pool.length - 1;
78+
m_info.put(ret, cp);
79+
80+
return ret;
81+
}
82+
83+
@Override
84+
public long adjustCapacity(long id, long reserve) {
85+
throw new UnsupportedOperationException("Unsupported to reduce capacity of this memory service");
86+
}
87+
88+
@Override
89+
public void close(long id) {
90+
if (mem_pool.get(id) != null) {
91+
mem_pool.get(id).close();
92+
mem_pool.get(id) = null;
93+
}
94+
}
95+
96+
@Override
97+
public void sync(long id, long addr, long length, boolean autodetect) {
98+
throw new UnsupportedOperationException("Unsupported to synchronization operation");
99+
}
100+
101+
@Override
102+
public long capacity(long id) {
103+
return m_info.get(id);
104+
}
105+
106+
@Override
107+
public long allocate(long id, long size, boolean initzero) {
108+
return 1L; //need detail
109+
}
110+
111+
@Override
112+
public long reallocate(long id, long addr, long size, boolean initzero) {
113+
return 1L; //need detail
114+
}
115+
116+
@Override
117+
public void free(long id, long addr) {
118+
///mem_pool.get(id) = null;//need change//allocateVS free
119+
}
120+
121+
@Override
122+
public ByteBuffer createByteBuffer(long id, long size) {
123+
ByteBuffer myByteBuffer = null;
124+
/*try {
125+
MapMode mapMode = readWrite ? MapMode.READ_WRITE : MapMode.READ_ONLY;
126+
FileChannel channel = mem_pool.get(id).getChannel();
127+
myByteBuffers = channel.map(mapMode, XXXXX, size);
128+
} catch (Exception e) {
129+
myBytebuffers = null;
130+
}*///need change
131+
132+
return myByteBuffer;
133+
}
134+
135+
@Override
136+
public ByteBuffer resizeByteBuffer(long id, ByteBuffer bytebuf, long size) {
137+
ByteBuffer myByteBuffer = null;
138+
return myByteBuffer; //need change
139+
}
140+
141+
@Override
142+
public void destroyByteBuffer(long id, ByteBuffer bytebuf) {
143+
//more detail
144+
}
145+
146+
@Override
147+
public ByteBuffer retrieveByteBuffer(long id, long handler) {
148+
ByteBuffer myByteBuffer = null;
149+
return myByteBuffer;//need change
150+
}
151+
152+
@Override
153+
public long retrieveSize(long id, long handler) {
154+
return 1L;//need change
155+
}
156+
157+
@Override
158+
public long getByteBufferHandler(long id, ByteBuffer buf) {
159+
return 1L;//need change
160+
}
161+
162+
@Override
163+
public void setHandler(long id, long key, long handler) {
164+
throw new UnsupportedOperationException("Unsupported to set handler");
165+
}
166+
167+
@Override
168+
public long getHandler(long id, long key) {
169+
throw new UnsupportedOperationException("Unsupported to get handler");
170+
}
171+
172+
@Override
173+
public long handlerCapacity(long id) {
174+
return 255;
175+
}
176+
177+
@Override
178+
public long getBaseAddress(long id) {
179+
return 1L;//need change
180+
}
181+
182+
@Override
183+
public void beginTransaction(boolean readOnly) {
184+
throw new UnsupportedOperationException("Not support transaction");
185+
}
186+
187+
@Override
188+
public void commitTransaction() {
189+
throw new UnsupportedOperationException("Not support transaction");
190+
}
191+
192+
@Override
193+
public void abortTransaction() {
194+
throw new UnsupportedOperationException("Not support transaction");
195+
}
196+
197+
@Override
198+
public boolean isInTransaction() {
199+
throw new UnsupportedOperationException("Not support transaction");
200+
}
201+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.apache.mnemonic.service.memoryservice.internal.JavaVMemServiceImpl
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to You under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
18+
# Root logger option
19+
log4j.rootLogger=INFO, file, stdout
20+
21+
#to stdout
22+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
23+
log4j.appender.stdout.Target=System.out
24+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
25+
log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n
26+
27+
#to log.log file
28+
log4j.appender.file=org.apache.log4j.RollingFileAppender
29+
log4j.appender.file.File=log.log
30+
log4j.appender.file.MaxFileSize=10MB
31+
log4j.appender.file.MaxBackupIndex=10
32+
log4j.appender.file.layout=org.apache.log4j.PatternLayout
33+
log4j.appender.file.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{1}: %m%n

0 commit comments

Comments
 (0)