inmempersistencemanager.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 598 行 · 第 1/2 页
JAVA
598 行
/** * store BLOB data in local file system in a sub directory * of the workspace home directory */ LocalFileSystem blobFS = new LocalFileSystem(); blobFS.setRoot(new File(context.getHomeDir(), "blobs")); blobFS.init(); this.blobFS = blobFS; blobStore = new FileSystemBLOBStore(blobFS); if (persistent) { // deserialize contents of state and refs stores loadContents(); } initialized = true; } /** * {@inheritDoc} */ public synchronized void close() throws Exception { if (!initialized) { throw new IllegalStateException("not initialized"); } try { if (persistent) { // serialize contents of state and refs stores storeContents(); } else { // clear out blob store try { String[] folders = blobFS.listFolders("/"); for (int i = 0; i < folders.length; i++) { blobFS.deleteFolder(folders[i]); } String[] files = blobFS.listFiles("/"); for (int i = 0; i < files.length; i++) { blobFS.deleteFile(files[i]); } } catch (Exception e) { // ignore } } // close BLOB file system blobFS.close(); blobFS = null; blobStore = null; stateStore.clear(); stateStore = null; refsStore.clear(); refsStore = null; } finally { initialized = false; } } /** * {@inheritDoc} */ public synchronized NodeState load(NodeId id) throws NoSuchItemStateException, ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } byte[] data = (byte[]) stateStore.get(id); if (data == null) { throw new NoSuchItemStateException(id.toString()); } ByteArrayInputStream in = new ByteArrayInputStream(data); try { NodeState state = createNew(id); Serializer.deserialize(state, in); return state; } catch (Exception e) { String msg = "failed to read node state: " + id; log.debug(msg); throw new ItemStateException(msg, e); } } /** * {@inheritDoc} */ public synchronized PropertyState load(PropertyId id) throws NoSuchItemStateException, ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } byte[] data = (byte[]) stateStore.get(id); if (data == null) { throw new NoSuchItemStateException(id.toString()); } ByteArrayInputStream in = new ByteArrayInputStream(data); try { PropertyState state = createNew(id); Serializer.deserialize(state, in, blobStore); return state; } catch (Exception e) { String msg = "failed to read property state: " + id; log.debug(msg); throw new ItemStateException(msg, e); } } /** * {@inheritDoc} */ protected void store(NodeState state) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } try { ByteArrayOutputStream out = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE); // serialize node state Serializer.serialize(state, out); // store in serialized format in map for better memory efficiency stateStore.put(state.getNodeId(), out.toByteArray()); // there's no need to close a ByteArrayOutputStream //out.close(); } catch (Exception e) { String msg = "failed to write node state: " + state.getNodeId(); log.debug(msg); throw new ItemStateException(msg, e); } } /** * {@inheritDoc} */ protected void store(PropertyState state) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } try { ByteArrayOutputStream out = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE); // serialize property state Serializer.serialize(state, out, blobStore); // store in serialized format in map for better memory efficiency stateStore.put(state.getPropertyId(), out.toByteArray()); // there's no need to close a ByteArrayOutputStream //out.close(); } catch (Exception e) { String msg = "failed to store property state: " + state.getPropertyId(); log.debug(msg); throw new ItemStateException(msg, e); } } /** * {@inheritDoc} */ protected void destroy(NodeState state) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } // remove node state stateStore.remove(state.getNodeId()); } /** * {@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) { if (val.getType() == PropertyType.BINARY) { BLOBFileValue blobVal = (BLOBFileValue) val.internalValue(); // delete blob file and prune empty parent folders blobVal.delete(true); } } } } // remove property state stateStore.remove(state.getPropertyId()); } /** * {@inheritDoc} */ public synchronized NodeReferences load(NodeReferencesId id) throws NoSuchItemStateException, ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } byte[] data = (byte[]) refsStore.get(id); if (data == null) { throw new NoSuchItemStateException(id.toString()); } ByteArrayInputStream in = new ByteArrayInputStream(data); try { NodeReferences refs = new NodeReferences(id); Serializer.deserialize(refs, in); return refs; } catch (Exception e) { String msg = "failed to load references: " + id; log.debug(msg); throw new ItemStateException(msg, e); } } /** * {@inheritDoc} */ protected void store(NodeReferences refs) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } try { ByteArrayOutputStream out = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE); // serialize references Serializer.serialize(refs, out); // store in serialized format in map for better memory efficiency refsStore.put(refs.getId(), out.toByteArray()); // there's no need to close a ByteArrayOutputStream //out.close(); } catch (Exception e) { String msg = "failed to store references: " + refs.getId(); log.debug(msg); throw new ItemStateException(msg, e); } } /** * {@inheritDoc} */ protected void destroy(NodeReferences refs) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } // remove node references refsStore.remove(refs.getId()); } /** * {@inheritDoc} */ public boolean exists(PropertyId id) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } return stateStore.containsKey(id); } /** * {@inheritDoc} */ public boolean exists(NodeId id) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } return stateStore.containsKey(id); } /** * {@inheritDoc} */ public boolean exists(NodeReferencesId id) throws ItemStateException { if (!initialized) { throw new IllegalStateException("not initialized"); } return refsStore.containsKey(id); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?