diff options
author | Dani Megert | 2006-01-09 17:13:54 +0000 |
---|---|---|
committer | Dani Megert | 2006-01-09 17:13:54 +0000 |
commit | c3d5b4156855ee62647a2fb9aca604d871385e09 (patch) | |
tree | 15a3bb82c5d1546739a7589b14ff345a6e79f71e /org.eclipse.text | |
parent | 6376c3ccf7636b6f82a3c37db091a2e23ddc5c02 (diff) | |
download | eclipse.platform.text-c3d5b4156855ee62647a2fb9aca604d871385e09.tar.gz eclipse.platform.text-c3d5b4156855ee62647a2fb9aca604d871385e09.tar.xz eclipse.platform.text-c3d5b4156855ee62647a2fb9aca604d871385e09.zip |
Released Anton's patch as a partial fix for bug 75086: [implementation] Request for a memory efficient way of loading large documents (in the mega bytes range)
Diffstat (limited to 'org.eclipse.text')
5 files changed, 162 insertions, 4 deletions
diff --git a/org.eclipse.text/component.xml b/org.eclipse.text/component.xml index 33c210892ec..630ddca77cd 100644 --- a/org.eclipse.text/component.xml +++ b/org.eclipse.text/component.xml @@ -9,6 +9,7 @@ <package name="org.eclipse.jface.text"> <type name="Assert" instantiate="false"/> <type name="ConfigurableLineTracker" subclass="false"/> + <type name="CopyOnWriteTextStore" subclass="false"/> <type name="DefaultLineTracker" subclass="false"/> <type name="GapTextStore" subclass="false"/> <type name="TextUtilities" subclass="false" instantiate="false"/> diff --git a/org.eclipse.text/src/org/eclipse/jface/text/CopyOnWriteTextStore.java b/org.eclipse.text/src/org/eclipse/jface/text/CopyOnWriteTextStore.java new file mode 100644 index 00000000000..3600d1e5088 --- /dev/null +++ b/org.eclipse.text/src/org/eclipse/jface/text/CopyOnWriteTextStore.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Anton Leherbauer (anton.leherbauer@windriver.com) - initial API and implementation + *******************************************************************************/ + +package org.eclipse.jface.text; + +/** + * Copy-on-write <code>ITextStore</code> wrapper. + * <p> + * This implementation uses an unmodifiable text store for the initial content. + * Upon first modification attempt, the unmodifiable store is replaced with + * a modifiable instance which must be supplied in the constructor.</p> + * <p> + * This class is not intended to be subclassed. + * </p> + * + * @since 3.2 + */ +public class CopyOnWriteTextStore implements ITextStore { + + /** + * An unmodifiable String based text store. It is not possible to modify the content + * other than using {@link #set}. Trying to {@link #replace} a text range will + * throw an <code>UnsupportedOperationException</code>. + */ + private static class StringTextStore implements ITextStore { + + /** Represents the content of this text store. */ + private String fText= ""; //$NON-NLS-1$ + + /** + * Create an empty text store. + */ + private StringTextStore() { + super(); + } + + /** + * Create a text store with initial content. + * @param text the initial content + */ + private StringTextStore(String text) { + super(); + set(text); + } + + /* + * @see org.eclipse.jface.text.ITextStore#get(int) + */ + public char get(int offset) { + return fText.charAt(offset); + } + + /* + * @see org.eclipse.jface.text.ITextStore#get(int, int) + */ + public String get(int offset, int length) { + return fText.substring(offset, offset + length); + } + + /* + * @see org.eclipse.jface.text.ITextStore#getLength() + */ + public int getLength() { + return fText.length(); + } + + /* + * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String) + */ + public void replace(int offset, int length, String text) { + // modification not supported + throw new UnsupportedOperationException(); + } + + /* + * @see org.eclipse.jface.text.ITextStore#set(java.lang.String) + */ + public void set(String text) { + fText= text != null ? text : ""; //$NON-NLS-1$ + } + + } + + /** The underlying "real" text store */ + protected ITextStore fTextStore= new StringTextStore(); + + /** A modifiable <code>ITextStore</code> instance */ + private final ITextStore fModifiableTextStore; + + /** + * Creates an empty text store. The given text store will be used upon first + * modification attempt. + * + * @param modifiableTextStore + * a modifiable <code>ITextStore</code> instance, may not be + * <code>null</code> + */ + public CopyOnWriteTextStore(ITextStore modifiableTextStore) { + Assert.isNotNull(modifiableTextStore); + fTextStore= new StringTextStore(); + fModifiableTextStore= modifiableTextStore; + } + + /* + * @see org.eclipse.jface.text.ITextStore#get(int) + */ + public char get(int offset) { + return fTextStore.get(offset); + } + + /* + * @see org.eclipse.jface.text.ITextStore#get(int, int) + */ + public String get(int offset, int length) { + return fTextStore.get(offset, length); + } + + /* + * @see org.eclipse.jface.text.ITextStore#getLength() + */ + public int getLength() { + return fTextStore.getLength(); + } + + /* + * @see org.eclipse.jface.text.ITextStore#replace(int, int, java.lang.String) + */ + public void replace(int offset, int length, String text) { + if (fTextStore != fModifiableTextStore) { + String content= fTextStore.get(0, fTextStore.getLength()); + fTextStore= fModifiableTextStore; + fTextStore.set(content); + } + fTextStore.replace(offset, length, text); + } + + /* + * @see org.eclipse.jface.text.ITextStore#set(java.lang.String) + */ + public void set(String text) { + fTextStore= new StringTextStore(text); + fModifiableTextStore.set(""); //$NON-NLS-1$ + } + +} diff --git a/org.eclipse.text/src/org/eclipse/jface/text/Document.java b/org.eclipse.text/src/org/eclipse/jface/text/Document.java index 5ceb5af3516..af87ad62a51 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/Document.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/Document.java @@ -14,7 +14,8 @@ package org.eclipse.jface.text; /** * Default document implementation. Uses a - * {@link org.eclipse.jface.text.GapTextStore} as default text store and a + * {@link org.eclipse.jface.text.GapTextStore} wrapped inside a + * {@link org.eclipse.jface.text.CopyOnWriteTextStore} as default text store and a * {@link org.eclipse.jface.text.SequentialRewriteTextStore} when in sequential * rewrite mode. * <p> @@ -26,6 +27,7 @@ package org.eclipse.jface.text; * * @see org.eclipse.jface.text.GapTextStore * @see org.eclipse.jface.text.SequentialRewriteTextStore + * @see org.eclipse.jface.text.CopyOnWriteTextStore */ public class Document extends AbstractDocument { @@ -35,7 +37,7 @@ public class Document extends AbstractDocument { */ public Document() { super(); - setTextStore(new GapTextStore(50, 300)); + setTextStore(new CopyOnWriteTextStore(new GapTextStore(50, 300))); setLineTracker(new DefaultLineTracker()); completeInitialization(); } @@ -47,7 +49,7 @@ public class Document extends AbstractDocument { */ public Document(String initialContent) { super(); - setTextStore(new GapTextStore(50, 300)); + setTextStore(new CopyOnWriteTextStore(new GapTextStore(50, 300))); setLineTracker(new DefaultLineTracker()); getStore().set(initialContent); getTracker().set(initialContent); diff --git a/org.eclipse.text/src/org/eclipse/jface/text/GapTextStore.java b/org.eclipse.text/src/org/eclipse/jface/text/GapTextStore.java index 2ddc3a7ca07..67178a483ea 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/GapTextStore.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/GapTextStore.java @@ -20,6 +20,7 @@ package org.eclipse.jface.text; * <p> * This class is not intended to be subclassed. * </p> + * @see CopyOnWriteTextStore for a copy-on-write text store wrapper */ public class GapTextStore implements ITextStore { diff --git a/org.eclipse.text/src/org/eclipse/jface/text/ITextStore.java b/org.eclipse.text/src/org/eclipse/jface/text/ITextStore.java index 403e02663ec..85c470a77c1 100644 --- a/org.eclipse.text/src/org/eclipse/jface/text/ITextStore.java +++ b/org.eclipse.text/src/org/eclipse/jface/text/ITextStore.java @@ -17,7 +17,8 @@ package org.eclipse.jface.text; * Provides access to the stored text and allows to manipulate it.</p> * <p> * Clients may - * implement this interface or use {@link org.eclipse.jface.text.GapTextStore} and + * implement this interface or use {@link org.eclipse.jface.text.GapTextStore}, + * {@link org.eclipse.jface.text.CopyOnWriteTextStore} and * {@link org.eclipse.jface.text.SequentialRewriteTextStore}.</p> */ public interface ITextStore { |