The basics
If your Value is a JAXB class annotated with "@XmlRootElement" you want to store as a file.
There are three ways to do this.
Type |
Key Type |
Filename |
Instance class |
Description |
Filename |
String |
The string key value. |
DefaultFilenameXmlFileValueManager<V> |
Load and save the file as XML using JAXB where the filename is the key string value.
The value V must have the annotation "@XmlRootElement".
|
Indexed |
K extends Serializable |
Random UUID |
DefaultIndexedXmlFileValueManager <K extends Serializable,V> |
Load and save the file as XML using JAXB where the filename is a random UUID,
the key to file map is stored in a seperate index file which is also persisted frequently.
The value V must have the annotation "@XmlRootElement".
The key (K) and value (V) must extend Serializable .
Think hard what a good "indexSaveInterval" is, and test it.
|
Keyed |
K extends Keyed<K> |
Random UUID |
DefaultKeyedXmlFileValueManager <K extends Serializable,V extends Keyed<K>> |
Load and save the file as XML using JAXB where the filename is a random UUID,
the key stored inside the value using the Keyed interface.
The value V must have the annotation "@XmlRootElement".
The key (K) must extend Serializable .
|
Construction examples
Given the values:
CachingDirectory cachingDirectory= new CachingDirectory(Files.createTempDirectory("xmlstorage").toFile());
LRUCachingMapConfiguration configuration = DefaultLRUCachingMapConfiguration.ONE_DAY();
And the Book.class defined at the bottom of this page.
Construction - DefaultFilenameXmlFileValueManager
//Manual
DefaultFilenameXmlFileValueManager<Book> valueManager = new DefaultFilenameXmlFileValueManager<Book>(cachingDirectory,Book.class,PersistencesRestoreMethod.DELETE_ALL);
DefaultFileLRUCachingMap<String,Book> cache = new DefaultFileLRUCachingMap<String, Book>(configuration,valueManager);
//Factory
cache = LRUCachingMapFactory.createFilenameXmlCache(configuration, cachingDirectory, Book.class, PersistencesRestoreMethod.NOTHING);
Construction - DefaultIndexedXmlFileValueManager
//Manual
DefaultIndexedXmlFileValueManager<String,Book> valueManager = new DefaultIndexedXmlFileValueManager<String,Book>(cachingDirectory,Book.class,PersistencesRestoreMethod.LOAD_ALL,10);
DefaultFileLRUCachingMap<String,Book> cache = new DefaultFileLRUCachingMap<String, Book>(configuration,valueManager);
//Factory
cache = LRUCachingMapFactory.createIndexedXmlCache(configuration, cachingDirectory,PersistencesRestoreMethod.DELETE_ALL,10);
Construction - DefaultKeyedXmlFileValueManager
//Manual
DefaultKeyedXmlFileValueManager<String,Book> valueManager = new DefaultKeyedXmlFileValueManager<String,Book>(cachingDirectory,Book.class,PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);
DefaultFileLRUCachingMap<String,Book> cache = new DefaultFileLRUCachingMap<String, Book>(configuration,valueManager);
//Factory
cache = LRUCachingMapFactory.createKeyedXmlCache(configuration, cachingDirectory,Book.class,PersistencesRestoreMethod.LOAD_WITHIN_EXPIRETIME);
Example class - Book
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Book implements Serializable,Keyed<String> {
private static final long serialVersionUID = 1411270976129358371L;
//
private String id;
private String title;
private Double price;
private String author;
private Date releaseDate;
private String description;
private String genre;
public Book() {
super();
}
@Override
public String getKey() {
return getId();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Date getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(Date releaseDate) {
this.releaseDate = releaseDate;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getGenre() {
return genre;
}
public void setGenre(String genre) {
this.genre = genre;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((author == null) ? 0 : author.hashCode());
result = prime * result
+ ((description == null) ? 0 : description.hashCode());
result = prime * result + ((genre == null) ? 0 : genre.hashCode());
result = prime * result + ((id == null) ? 0 : id.hashCode());
result = prime * result + ((price == null) ? 0 : price.hashCode());
result = prime * result
+ ((releaseDate == null) ? 0 : releaseDate.hashCode());
result = prime * result + ((title == null) ? 0 : title.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Book other = (Book) obj;
if (author == null) {
if (other.author != null)
return false;
} else if (!author.equals(other.author))
return false;
if (description == null) {
if (other.description != null)
return false;
} else if (!description.equals(other.description))
return false;
if (genre == null) {
if (other.genre != null)
return false;
} else if (!genre.equals(other.genre))
return false;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
if (price == null) {
if (other.price != null)
return false;
} else if (!price.equals(other.price))
return false;
if (releaseDate == null) {
if (other.releaseDate != null)
return false;
} else if (!releaseDate.equals(other.releaseDate))
return false;
if (title == null) {
if (other.title != null)
return false;
} else if (!title.equals(other.title))
return false;
return true;
}
}
|