Java tutorial
/* * Copyright (C) 2014 SETRONICA - setronica.com * This source code is available under the terms of the GNU Lesser General Public License * as published by The Open Source Initiative (OSI), either version 3 of the License, * or (at your option) any later version. * * UCS 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. */ package com.setronica.ucs.storage; import com.mongodb.*; import java.io.*; import java.util.ArrayList; import java.util.List; public class MongoListStorage<E> implements ListStorage<E>, PersistentStorage { protected List<E> data; private String mongoDbName; private String mongoCollectionName; protected DBCollection collection; protected MongoClient mongoClient; public MongoListStorage(MongoClient mongoClient, String mongoDbName, String mongoCollectionName) { this.mongoClient = mongoClient; this.mongoDbName = mongoDbName; this.mongoCollectionName = mongoCollectionName; } @Override public E get(int index) { return data.get(index); } @Override public boolean add(E element) { if (data.add(element)) { try { save(data.size() - 1, element); } catch (IOException e) { throw new RuntimeException(e); } return true; } return false; } @Override public void refresh(int index) { E element = data.get(index); if (element == null) return; try { save(index, element); } catch (IOException e) { throw new RuntimeException(e); } } protected void save(int index, E element) throws IOException { BasicDBObject object = new BasicDBObject("_id", index); object.put("element", writeObject(element)); collection.insert(object); } @Override public void remove(int index) { data.set(index, null); collection.remove(new BasicDBObject("_id", index)); } @Override public int size() { return data.size(); } @Override public void open() throws IOException { DB db = mongoClient.getDB(mongoDbName); collection = db.getCollection(mongoCollectionName); if (collection == null) { collection = db.createCollection(mongoCollectionName, null); } collection.setWriteConcern(WriteConcern.ACKNOWLEDGED); DBCursor cursor = collection.find(); data = new ArrayList<E>(cursor.size()); for (DBObject entry : cursor) { Integer index = (Integer) entry.get("_id"); while (index >= data.size()) { data.add(null); } data.set(index, (E) readObject((byte[]) entry.get("element"))); } } //todo compact? @Override public void close() throws IOException { } private byte[] writeObject(Object element) throws IOException { ByteArrayOutputStream os = new ByteArrayOutputStream(); ObjectOutput out = new ObjectOutputStream(os); out.writeObject(element); byte[] val = os.toByteArray(); out.close(); os.close(); return val; } private Object readObject(byte[] data) throws IOException { ByteArrayInputStream bis = new ByteArrayInputStream(data); ObjectInput ois = new ObjectInputStream(bis); try { return ois.readObject(); } catch (ClassNotFoundException e) { throw new IOException(e); } finally { ois.close(); bis.close(); } } }