]> sjero.net Git - linphone/blob - p2pproxy/dependencies-src/jxse-src-2.5/impl/src/net/jxta/impl/xindice/core/filer/MemFiler.java
remove mediastreamer2 and add it as a submodule instead.
[linphone] / p2pproxy / dependencies-src / jxse-src-2.5 / impl / src / net / jxta / impl / xindice / core / filer / MemFiler.java
1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  *    if any, must include the following acknowledgment:
21  *       "This product includes software developed by the
22  *        Apache Software Foundation (http://www.apache.org/)."
23  *    Alternately, this acknowledgment may appear in the software itself,
24  *    if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Xindice" and "Apache Software Foundation" must
27  *    not be used to endorse or promote products derived from this
28  *    software without prior written permission. For written
29  *    permission, please contact apache@apache.org.
30  *
31  * 5. Products derived from this software may not be called "Apache",
32  *    nor may "Apache" appear in their name, without prior written
33  *    permission of the Apache Software Foundation.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of the Apache Software Foundation and was
51  * originally based on software copyright (c) 1999-2001, The dbXML
52  * Group, L.L.C., http://www.dbxmlgroup.com.  For more
53  * information on the Apache Software Foundation, please see
54  * <http://www.apache.org/>.
55  *
56  */
57 package net.jxta.impl.xindice.core.filer;
58
59 import net.jxta.impl.xindice.core.DBException;
60 import net.jxta.impl.xindice.core.FaultCodes;
61 import net.jxta.impl.xindice.core.data.Key;
62 import net.jxta.impl.xindice.core.data.Value;
63 import net.jxta.impl.xindice.core.data.Record;
64 import net.jxta.impl.xindice.core.data.RecordSet;
65
66 import java.io.File;
67 import java.util.Collections;
68 import java.util.HashMap;
69 import java.util.Iterator;
70 import java.util.Map;
71
72 /**
73  * MemFiler is an In-Memory Filer implementation for Xindice.  MemFiler can be
74  * used for temporary collections and caching.  It's basically a layering on
75  * top of HashMap.
76  */
77 public final class MemFiler implements Filer {
78     private Map<Key, Record> hashTable = null;
79     private Map<Long, Key> posTable = null;
80     private boolean opened = false;
81     private boolean readOnly = false;
82     private long position = 0;
83     public MemFiler() {
84         hashTable = Collections.synchronizedMap(new HashMap<Key, Record>());
85         posTable = Collections.synchronizedMap(new HashMap<Long, Key>());
86     }
87
88     public MemFiler(Map<Key, Record> hashTable, boolean readOnly) {
89         this.hashTable = hashTable;
90         this.readOnly = readOnly;
91     }
92
93     public MemFiler(Map<Key, Record> hashTable) {
94         this(hashTable, false);
95     }
96
97     public void setLocation(File root, String location) {}
98
99     public String getName() {
100         return "MemFiler";
101     }
102
103     private void checkOpened() throws DBException {
104         if (!opened) {
105             throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED, "Filer is closed");
106         }
107     }
108
109     private void checkReadOnly() throws DBException {
110         if (readOnly) {
111             throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY, "Filer is read-only");
112         }
113     }
114
115     public boolean create() {
116         hashTable.clear();
117         return true;
118     }
119
120     public boolean open() {
121         opened = true;
122         return opened;
123     }
124
125     public boolean isOpened() {
126         return opened;
127     }
128
129     public boolean exists() {
130         return true;
131     }
132
133     public boolean drop() {
134         hashTable.clear();
135         opened = false;
136         return !opened;
137     }
138
139     public boolean close() {
140         opened = false;
141         return !opened;
142     }
143
144     public void flush() {}
145
146     public Record readRecord(Key key) throws DBException {
147         if (key == null || key.getLength() == 0) {
148             return null;
149         }
150         checkOpened();
151         return hashTable.get(key);
152     }
153    
154     public Record readRecord(long pos) throws DBException {
155         if (pos < 0) {
156             return null;
157         }
158         checkOpened();
159         Key key = posTable.get(pos);
160
161         return hashTable.get(key);
162     }
163    
164     public long writeRecord(Key key, Value value) throws DBException {
165         if (key == null || key.getLength() == 0) {
166             throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid key: '" + key + "'");
167         }
168         if (value == null) {
169             throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid null value");
170         }
171         checkOpened();
172         checkReadOnly();
173         hashTable.put(key, new Record(key, value));
174         posTable.put(position, key);
175         long result = position;
176
177         position++;
178         return result;
179     }
180    
181     public boolean deleteRecord(Key key) throws DBException {
182         if (key == null || key.getLength() == 0) {
183             return false;
184         }
185         checkOpened();
186         checkReadOnly();
187         return hashTable.remove(key) != null;
188     }
189
190     public long getRecordCount() throws DBException {
191         checkOpened();
192         return hashTable.size();
193     }
194
195     public RecordSet getRecordSet() throws DBException {
196         checkOpened();
197         return new MemRecordSet();
198     }
199
200     /**
201      * MemRecordSet
202      */
203
204     private class MemRecordSet implements RecordSet {
205         private Iterator<Record> it = hashTable.values().iterator();
206
207         public synchronized boolean hasMoreRecords() throws DBException {
208             return it.hasNext();
209         }
210
211         public synchronized Record getNextRecord() throws DBException {
212             checkOpened();
213             return it.next();
214         }
215
216         public synchronized Value getNextValue() throws DBException {
217             checkOpened();
218             return (it.next()).getValue();
219         }
220
221         public synchronized Key getNextKey() {
222             return (it.next()).getKey();
223         }
224     }
225 }
226