use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.
the class InMemPersistenceManager method loadContents.
/**
* Reads the content of the hash maps from the file system
*
* @throws Exception if an error occurs
*/
public synchronized void loadContents() throws Exception {
// read item states
FileSystemResource fsRes = new FileSystemResource(wspFS, STATE_FILE_PATH);
if (!fsRes.exists()) {
return;
}
BufferedInputStream bis = new BufferedInputStream(fsRes.getInputStream());
DataInputStream in = new DataInputStream(bis);
try {
// number of entries
int n = in.readInt();
while (n-- > 0) {
// entry type
byte type = in.readByte();
ItemId id;
if (type == NODE_ENTRY) {
// entry type: node
// id
String s = in.readUTF();
id = NodeId.valueOf(s);
} else {
// entry type: property
// id
String s = in.readUTF();
id = PropertyId.valueOf(s);
}
// data length
int length = in.readInt();
byte[] data = new byte[length];
// data
in.readFully(data);
// store in map
stateStore.put(id, data);
}
} finally {
in.close();
}
// read references
fsRes = new FileSystemResource(wspFS, REFS_FILE_PATH);
bis = new BufferedInputStream(fsRes.getInputStream());
in = new DataInputStream(bis);
try {
// number of entries
int n = in.readInt();
while (n-- > 0) {
// target id
String s = in.readUTF();
NodeId id = NodeId.valueOf(s);
// data length
int length = in.readInt();
byte[] data = new byte[length];
// data
in.readFully(data);
// store in map
refsStore.put(id, data);
}
} finally {
in.close();
}
}
use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.
the class InMemPersistenceManager method storeContents.
/**
* Writes the content of the hash maps to the file system
*
* @throws Exception if an error occurs
*/
public synchronized void storeContents() throws Exception {
// write item states
FileSystemResource fsRes = new FileSystemResource(wspFS, STATE_FILE_PATH);
fsRes.makeParentDirs();
BufferedOutputStream bos = new BufferedOutputStream(fsRes.getOutputStream());
DataOutputStream out = new DataOutputStream(bos);
try {
// number of entries
out.writeInt(stateStore.size());
// entries
for (ItemId id : stateStore.keySet()) {
if (id.denotesNode()) {
// entry type
out.writeByte(NODE_ENTRY);
} else {
// entry type
out.writeByte(PROP_ENTRY);
}
// id
out.writeUTF(id.toString());
byte[] data = stateStore.get(id);
// data length
out.writeInt(data.length);
// data
out.write(data);
}
} finally {
out.close();
}
// write references
fsRes = new FileSystemResource(wspFS, REFS_FILE_PATH);
fsRes.makeParentDirs();
bos = new BufferedOutputStream(fsRes.getOutputStream());
out = new DataOutputStream(bos);
try {
// number of entries
out.writeInt(refsStore.size());
// entries
for (NodeId id : refsStore.keySet()) {
// target id
out.writeUTF(id.toString());
byte[] data = refsStore.get(id);
// data length
out.writeInt(data.length);
// data
out.write(data);
}
} finally {
out.close();
}
}
use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.
the class ObjectPersistenceManager method store.
/**
* {@inheritDoc}
*/
protected void store(NodeReferences refs) throws ItemStateException {
if (!initialized) {
throw new IllegalStateException("not initialized");
}
String refsFilePath = buildNodeReferencesFilePath(refs.getTargetId());
FileSystemResource refsFile = new FileSystemResource(itemStateFS, refsFilePath);
try {
refsFile.makeParentDirs();
OutputStream out = new BufferedOutputStream(refsFile.getOutputStream());
try {
Serializer.serialize(refs, out);
} finally {
out.close();
}
} catch (Exception e) {
String msg = "failed to store " + refs;
log.debug(msg);
throw new ItemStateException(msg, e);
}
}
use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.
the class ObjectPersistenceManager method destroy.
/**
* {@inheritDoc}
*/
protected void destroy(PropertyState state) throws ItemStateException {
if (!initialized) {
throw new IllegalStateException("not initialized");
}
// delete binary values (stored as files)
InternalValue[] values = state.getValues();
if (values != null) {
for (int i = 0; i < values.length; i++) {
InternalValue val = values[i];
if (val != null) {
val.deleteBinaryResource();
}
}
}
// delete property file
String propFilePath = buildPropFilePath(state.getPropertyId());
FileSystemResource propFile = new FileSystemResource(itemStateFS, propFilePath);
try {
if (propFile.exists()) {
// delete resource and prune empty parent folders
propFile.delete(true);
}
} catch (FileSystemException fse) {
String msg = "failed to delete property state: " + state.getParentId() + "/" + state.getName();
log.debug(msg);
throw new ItemStateException(msg, fse);
}
}
use of org.apache.jackrabbit.core.fs.FileSystemResource in project jackrabbit by apache.
the class Serializer method serialize.
/**
* Serializes the specified <code>PropertyState</code> object to the given
* binary <code>stream</code>. Binary values are stored in the specified
* <code>BLOBStore</code>.
*
* @param state <code>state</code> to serialize
* @param stream the stream where the <code>state</code> should be
* serialized to
* @param blobStore handler for BLOB data
* @throws Exception if an error occurs during the serialization
* @see #deserialize(PropertyState, InputStream,BLOBStore)
*/
public static void serialize(PropertyState state, OutputStream stream, BLOBStore blobStore) throws Exception {
DataOutputStream out = new DataOutputStream(stream);
// type
out.writeInt(state.getType());
// multiValued
out.writeBoolean(state.isMultiValued());
// definitionId
out.writeUTF("");
// modCount
out.writeShort(state.getModCount());
// values
InternalValue[] values = state.getValues();
// count
out.writeInt(values.length);
for (int i = 0; i < values.length; i++) {
InternalValue val = values[i];
if (state.getType() == PropertyType.BINARY) {
// special handling required for binary value:
// put binary value in BLOB store
InputStream in = val.getStream();
String blobId = blobStore.createId(state.getPropertyId(), i);
try {
blobStore.put(blobId, in, val.getLength());
} finally {
IOUtils.closeQuietly(in);
}
// store id of BLOB as property value
// value
out.writeUTF(blobId);
// in BLOB store and discard old value instance (e.g. temp file)
if (blobStore instanceof ResourceBasedBLOBStore) {
// optimization: if the BLOB store is resource-based
// retrieve the resource directly rather than having
// to read the BLOB from an input stream
FileSystemResource fsRes = ((ResourceBasedBLOBStore) blobStore).getResource(blobId);
values[i] = InternalValue.create(fsRes);
} else {
in = blobStore.get(blobId);
try {
values[i] = InternalValue.create(in);
} finally {
IOUtils.closeQuietly(in);
}
}
val.discard();
} else {
/**
* because writeUTF(String) has a size limit of 65k,
* Strings are serialized as <length><byte[]>
*/
//out.writeUTF(val.toString()); // value
byte[] bytes = val.toString().getBytes(ENCODING);
// lenght of byte[]
out.writeInt(bytes.length);
// byte[]
out.write(bytes);
}
}
}
Aggregations