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
+ }
0 commit comments