2 * The Apache Software License, Version 1.1
5 * Copyright (c) 1999 The Apache Software Foundation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
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.
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.
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.
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
47 * ====================================================================
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/>.
57 package net.jxta.impl.xindice.core.filer;
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;
67 import java.util.Collections;
68 import java.util.HashMap;
69 import java.util.Iterator;
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
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;
84 hashTable = Collections.synchronizedMap(new HashMap<Key, Record>());
85 posTable = Collections.synchronizedMap(new HashMap<Long, Key>());
88 public MemFiler(Map<Key, Record> hashTable, boolean readOnly) {
89 this.hashTable = hashTable;
90 this.readOnly = readOnly;
93 public MemFiler(Map<Key, Record> hashTable) {
94 this(hashTable, false);
97 public void setLocation(File root, String location) {}
99 public String getName() {
103 private void checkOpened() throws DBException {
105 throw new FilerException(FaultCodes.COL_COLLECTION_CLOSED, "Filer is closed");
109 private void checkReadOnly() throws DBException {
111 throw new FilerException(FaultCodes.COL_COLLECTION_READ_ONLY, "Filer is read-only");
115 public boolean create() {
120 public boolean open() {
125 public boolean isOpened() {
129 public boolean exists() {
133 public boolean drop() {
139 public boolean close() {
144 public void flush() {}
146 public Record readRecord(Key key) throws DBException {
147 if (key == null || key.getLength() == 0) {
151 return hashTable.get(key);
154 public Record readRecord(long pos) throws DBException {
159 Key key = posTable.get(pos);
161 return hashTable.get(key);
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 + "'");
169 throw new FilerException(FaultCodes.DBE_CANNOT_CREATE, "Invalid null value");
173 hashTable.put(key, new Record(key, value));
174 posTable.put(position, key);
175 long result = position;
181 public boolean deleteRecord(Key key) throws DBException {
182 if (key == null || key.getLength() == 0) {
187 return hashTable.remove(key) != null;
190 public long getRecordCount() throws DBException {
192 return hashTable.size();
195 public RecordSet getRecordSet() throws DBException {
197 return new MemRecordSet();
204 private class MemRecordSet implements RecordSet {
205 private Iterator<Record> it = hashTable.values().iterator();
207 public synchronized boolean hasMoreRecords() throws DBException {
211 public synchronized Record getNextRecord() throws DBException {
216 public synchronized Value getNextValue() throws DBException {
218 return (it.next()).getValue();
221 public synchronized Key getNextKey() {
222 return (it.next()).getKey();