001: // Space4J(TM) - Object Persistence in RAM
002: // Copyright (C) 2003 Sergio Oliveira Junior
003: // This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License version 2.1 as published by the Free Software Foundation. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
004:
005: package org.space4j.demos.phonebook;
006:
007: import org.space4j.demos.phonebook.commands.*;
008: import org.space4j.passivation.*;
009: import org.space4j.implementation.*;
010: import org.space4j.commands.*;
011: import org.space4j.indexing.*;
012: import org.space4j.*;
013:
014: import java.util.*;
015: import java.io.*;
016: import java.net.*;
017:
018: /**
019: * This is a PhoneBook application that illustrates the framework for indexing.<br>
020: * java org.space4j.demos.phonebook.PhoneBookIndexing<br>
021: */
022: public class PhoneBookIndexing {
023:
024: protected Space4J space4j = null;
025: protected Space space = null;
026: private Index indexByName;
027: private IndexManager im = null;
028:
029: public PhoneBookIndexing() throws LoggerException,
030: CommandException, UnknownHostException, IOException,
031: ClassNotFoundException {
032: // Initializes the system, passing the dir where the logs and snapshots are stored.
033: // NOTE: The system will load everything in memory here. Snapshot and last commands.
034: space4j = new SimpleSpace4J("PhoneBookIndexing");
035:
036: // Start the system...
037: space4j.start();
038:
039: // Grab the space where all data resides...
040: space = space4j.getSpace();
041:
042: // If this is the first time, create our main hashmap...
043: if (space.getObject("phonenumbers") == null) {
044: space4j.executeCommand(new ObjCreateCmd("phonenumbers",
045: new HashMap()));
046: }
047:
048: // Create a sequence for our objects...
049: // This is just an Integer in memory !!!
050: if (space.getObject("phonenumbers_seq") == null) {
051: space4j.executeCommand(new ObjCreateCmd("phonenumbers_seq",
052: new Integer(0)));
053: }
054:
055: // Let's create a index for our data structure....
056: // phones by name:
057: im = space4j.getIndexManager();
058: indexByName = new Index("indx_phonenumbers_name",
059: PhoneBookRecord.class, new String[] { "name" });
060: if (!im.checkIndex(indexByName)) {
061: boolean ok = im.createIndex(indexByName, "phonenumbers",
062: new HashMap(), space4j);
063: if (ok)
064: System.out
065: .println(indexByName.toString() + " created!");
066: else
067: System.out.println(indexByName.toString()
068: + " could not be created!");
069: }
070:
071: }
072:
073: public void executeSnapshot() throws LoggerException {
074: space4j.executeSnapshot();
075: }
076:
077: public synchronized void addNumber(String name, String number)
078: throws CommandException, LoggerException {
079: int seq = space4j.executeCommand(new IncrementSeqCmd(
080: "phonenumbers_seq"));
081: space4j.executeCommand(new CreatePhoneCmd("phonenumbers", name,
082: number, seq));
083: }
084:
085: // now we are indexing...
086: private PhoneBookRecord getByName(String name) {
087: Map phonesByName = (Map) im.getIndexedMap(indexByName);
088: Key key = new Key(name);
089: PhoneBookRecord phone = (PhoneBookRecord) phonesByName.get(key);
090: return phone;
091: }
092:
093: public String getNumber(String name) {
094: PhoneBookRecord phone = getByName(name);
095: if (phone == null)
096: return null;
097: return phone.getNumber();
098: }
099:
100: public boolean delNumber(String name) throws CommandException,
101: LoggerException {
102: PhoneBookRecord phone = getByName(name);
103: if (phone == null)
104: return false;
105: int x = space4j.executeCommand(new RemovePhoneCmd(
106: "phonenumbers", phone.getId()));
107: if (x > 0)
108: return true;
109: return false;
110: }
111:
112: public ArrayList getNames() {
113: Iterator iter = space.getSafeIterator("phonenumbers");
114: if (iter == null)
115: return null;
116: ArrayList list = new ArrayList();
117: while (iter.hasNext()) {
118: PhoneBookRecord phone = (PhoneBookRecord) iter.next();
119: list.add(phone.getName());
120: }
121: return list;
122: }
123:
124: // This has nothing to do with Space4J.
125: // Just a simple logic for a minimal PhoneBook application.
126: // ToDo: Make it a Swing application. (Anyone?)
127: public static void main(String[] args) throws Exception {
128: PhoneBookIndexing book = new PhoneBookIndexing();
129: BufferedReader input = new BufferedReader(
130: new InputStreamReader(System.in));
131: System.out
132: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
133: while (true) {
134: String cmd = input.readLine();
135: System.out.println();
136: if (cmd.equalsIgnoreCase("l")) {
137: ArrayList list = book.getNames();
138: if (list == null || list.size() == 0)
139: System.out.println("No entries in phone book!\n");
140: else {
141: Iterator iter = list.iterator();
142: while (iter.hasNext()) {
143: String key = (String) iter.next();
144: String value = book.getNumber(key);
145: System.out.println(key + ": " + value);
146: }
147: System.out
148: .println("\nTotal: " + list.size() + "\n");
149: }
150: } else if (cmd.equalsIgnoreCase("a")) {
151: System.out.print("Name: ");
152: String name = input.readLine();
153: System.out.print("Tel: ");
154: String tel = input.readLine();
155: if (!name.trim().equals("") && !tel.trim().equals("")) {
156: book.addNumber(name, tel);
157: System.out.println(name + ": " + tel + " added!\n");
158: }
159: } else if (cmd.equalsIgnoreCase("f")) {
160: System.out.print("Name: ");
161: String name = input.readLine();
162: String tel = book.getNumber(name);
163: if (tel != null) {
164: System.out.println("Number: " + tel + "\n");
165: } else {
166: System.out.println("Nothing found!\n");
167: }
168: } else if (cmd.equalsIgnoreCase("r")) {
169: System.out.print("Name: ");
170: String name = input.readLine();
171: if (book.delNumber(name)) {
172: System.out.println("Item removed!\n");
173: } else {
174: System.out.println("Not found!\n");
175: }
176: } else if (cmd.equalsIgnoreCase("s")) {
177: book.executeSnapshot();
178: System.out.println("Snapshot taken!\n");
179: } else if (cmd.equalsIgnoreCase("q")) {
180: break;
181: }
182: System.out
183: .print("(L)ist | (A)dd | (F)ind | (R)emove | (S)napshot | (Q)uit => ");
184: }
185: }
186: }
|