<commit_before>
package org.exist.xmldb;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;

import javax.xml.transform.OutputKeys;

import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

public class RemoteResourceSet implements ResourceSet {

    protected RemoteCollection collection;
    protected int handle = -1;
    protected List resources;
    protected Properties outputProperties;
    
    private static Logger LOG = Logger.getLogger(RemoteResourceSet.class.getName());

    public RemoteResourceSet( RemoteCollection col, Properties properties, Object[] resources, int handle ) {
        this.handle = handle;
        this.resources = new ArrayList(resources.length);
        for (int i = 0; i < resources.length; i++) {
            this.resources.add(resources[i]);
        }
        this.collection = col;
        this.outputProperties = properties;
    }

    public void addResource( Resource resource ) {
        resources.add( resource );
    }

    public void clear() throws XMLDBException {
        List params = new ArrayList(1);
    	params.add(new Integer(handle));
        try {
            collection.getClient().execute("releaseQueryResult", params);
        } catch (XmlRpcException e) {
            System.err.println("Failed to release query result on server: " + e.getMessage());
        }
        resources.clear();
    }

    public ResourceIterator getIterator() throws XMLDBException {
        return new NewResourceIterator();
    }

    public ResourceIterator getIterator( long start ) throws XMLDBException {
        return new NewResourceIterator( start );
    }


    public Resource getMembersAsResource() throws XMLDBException {
        List params = new ArrayList(2);
    	params.add(new Integer(handle));
    	params.add(outputProperties);
    	try {
			byte[] data = (byte[]) collection.getClient().execute("retrieveAll", params);
			String content;
			try {
				content = new String(data, outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8"));
			} catch (UnsupportedEncodingException ue) {
				LOG.warn(ue);
				content = new String(data);
			}
			RemoteXMLResource res = new RemoteXMLResource( collection, handle, 0, 
	            	XmldbURI.EMPTY_URI, null );
	        res.setContent( content );
	        res.setProperties(outputProperties);
	        return res;
		} catch (XmlRpcException xre) {
			throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, xre.getMessage(), xre);
		}
    }

    public Resource getResource( long pos ) throws XMLDBException {
        if ( pos >= resources.size() )
            return null;
        // node or value?
        if ( resources.get( (int) pos ) instanceof Object[] ) {
            // node
            Object[] v = (Object[]) resources.get( (int) pos );
            String doc = (String) v[0];
            String s_id = (String) v[1];
            XmldbURI docUri;
            try {
            	docUri = XmldbURI.xmldbUriFor(doc);
            } catch (URISyntaxException e) {
            	throw new XMLDBException(ErrorCodes.INVALID_URI,e.getMessage(),e);
            }
			RemoteCollection parent = 
				new RemoteCollection(collection.getClient(), null, docUri.removeLastSegment());
			parent.properties = outputProperties;
            RemoteXMLResource res =
                new RemoteXMLResource( parent, handle,
                	(int)pos, docUri, s_id );
            res.setProperties(outputProperties);
            return res;
        } else if ( resources.get( (int) pos ) instanceof Resource )
            return (Resource) resources.get( (int) pos );
        else {
            // value
            RemoteXMLResource res = new RemoteXMLResource( collection, handle, (int)pos, 
            	XmldbURI.create(Long.toString( pos )), null );
            res.setContent( resources.get( (int) pos ) );
            res.setProperties(outputProperties);
            return res;
        }
    }

    public long getSize() throws XMLDBException {
        return resources == null ? 0 : (long) resources.size();
    }

    public void removeResource( long pos ) throws XMLDBException {
        resources.get( (int) pos );
    }

    class NewResourceIterator implements ResourceIterator {

        long pos = 0;

        public NewResourceIterator() { }

        public NewResourceIterator( long start ) {
            pos = start;
        }

        public boolean hasMoreResources() throws XMLDBException {
            return resources == null ? false : pos < resources.size();
        }

        public Resource nextResource() throws XMLDBException {
            return getResource( pos++ );
        }
    }
}
<commit_msg>[bugfix] call clear() from RemoteResourceSet.finalize(). clear() ensures that cached result sets are released on the server. Calling this from finalize doesn't give us any guarantee, but it at least helps to take some load from the db. If a lot of short-lived queries are issued from a single client, chances are good that the resource sets will be garbage collected rather quickly.<commit_after>
package org.exist.xmldb;

import java.io.UnsupportedEncodingException;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;

import javax.xml.transform.OutputKeys;

import org.apache.log4j.Logger;
import org.apache.xmlrpc.XmlRpcException;
import org.xmldb.api.base.ErrorCodes;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.base.XMLDBException;

public class RemoteResourceSet implements ResourceSet {

    protected RemoteCollection collection;
    protected int handle = -1;
    protected List resources;
    protected Properties outputProperties;
    
    private static Logger LOG = Logger.getLogger(RemoteResourceSet.class.getName());

    public RemoteResourceSet( RemoteCollection col, Properties properties, Object[] resources, int handle ) {
        this.handle = handle;
        this.resources = new ArrayList(resources.length);
        for (int i = 0; i < resources.length; i++) {
            this.resources.add(resources[i]);
        }
        this.collection = col;
        this.outputProperties = properties;
    }

    public void addResource( Resource resource ) {
        resources.add( resource );
    }

    public void clear() throws XMLDBException {
        List params = new ArrayList(1);
    	params.add(new Integer(handle));
        try {
            collection.getClient().execute("releaseQueryResult", params);
        } catch (XmlRpcException e) {
            System.err.println("Failed to release query result on server: " + e.getMessage());
        }
        resources.clear();
    }

    public ResourceIterator getIterator() throws XMLDBException {
        return new NewResourceIterator();
    }

    public ResourceIterator getIterator( long start ) throws XMLDBException {
        return new NewResourceIterator( start );
    }


    public Resource getMembersAsResource() throws XMLDBException {
        List params = new ArrayList(2);
    	params.add(new Integer(handle));
    	params.add(outputProperties);
    	try {
			byte[] data = (byte[]) collection.getClient().execute("retrieveAll", params);
			String content;
			try {
				content = new String(data, outputProperties.getProperty(OutputKeys.ENCODING, "UTF-8"));
			} catch (UnsupportedEncodingException ue) {
				LOG.warn(ue);
				content = new String(data);
			}
			RemoteXMLResource res = new RemoteXMLResource( collection, handle, 0, 
	            	XmldbURI.EMPTY_URI, null );
	        res.setContent( content );
	        res.setProperties(outputProperties);
	        return res;
		} catch (XmlRpcException xre) {
			throw new XMLDBException(ErrorCodes.INVALID_RESOURCE, xre.getMessage(), xre);
		}
    }

    public Resource getResource( long pos ) throws XMLDBException {
        if ( pos >= resources.size() )
            return null;
        // node or value?
        if ( resources.get( (int) pos ) instanceof Object[] ) {
            // node
            Object[] v = (Object[]) resources.get( (int) pos );
            String doc = (String) v[0];
            String s_id = (String) v[1];
            XmldbURI docUri;
            try {
            	docUri = XmldbURI.xmldbUriFor(doc);
            } catch (URISyntaxException e) {
            	throw new XMLDBException(ErrorCodes.INVALID_URI,e.getMessage(),e);
            }
			RemoteCollection parent = 
				new RemoteCollection(collection.getClient(), null, docUri.removeLastSegment());
			parent.properties = outputProperties;
            RemoteXMLResource res =
                new RemoteXMLResource( parent, handle,
                	(int)pos, docUri, s_id );
            res.setProperties(outputProperties);
            return res;
        } else if ( resources.get( (int) pos ) instanceof Resource )
            return (Resource) resources.get( (int) pos );
        else {
            // value
            RemoteXMLResource res = new RemoteXMLResource( collection, handle, (int)pos, 
            	XmldbURI.create(Long.toString( pos )), null );
            res.setContent( resources.get( (int) pos ) );
            res.setProperties(outputProperties);
            return res;
        }
    }

    public long getSize() throws XMLDBException {
        return resources == null ? 0 : (long) resources.size();
    }

    public void removeResource( long pos ) throws XMLDBException {
        resources.get( (int) pos );
    }

    protected void finalize() throws Throwable {
        try {
            clear();
        } finally {
            super.finalize();
        }
    }

    class NewResourceIterator implements ResourceIterator {

        long pos = 0;

        public NewResourceIterator() { }

        public NewResourceIterator( long start ) {
            pos = start;
        }

        public boolean hasMoreResources() throws XMLDBException {
            return resources == null ? false : pos < resources.size();
        }

        public Resource nextResource() throws XMLDBException {
            return getResource( pos++ );
        }
    }
}

{'fraction_non_alphanumeric': 0.09796388782174414, 'fraction_numerical': 0.0021129466000768342, 'mean_word_length': 2.5406324379462766, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 18}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4543776', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2747, 'n_words': 748}