<commit_before>/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.server.ldap.handlers.controls;

import java.util.HashSet;
import java.util.Set;

import org.apache.directory.server.core.filtering.EntryFilteringCursor;
import org.apache.directory.server.ldap.LdapSession;
import org.apache.directory.shared.asn1.ber.tlv.Value;
import org.apache.directory.shared.ldap.constants.SchemaConstants;
import org.apache.directory.shared.ldap.exception.LdapException;
import org.apache.directory.shared.ldap.message.internal.InternalSearchRequest;
import org.apache.directory.shared.ldap.schema.AttributeType;
import org.apache.directory.shared.ldap.schema.SchemaManager;
import org.apache.directory.shared.ldap.util.StringTools;

/**
 * The structure which stores the informations relative to the pagedSearch control.
 * They are associated to a cookie, stored into the session and associated to an 
 * instance of this class.
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev:  $
 */
public class PagedSearchContext
{
    /** The previous search request */
    private InternalSearchRequest previousSearchRequest;
    
    /** The current position in the cursor */
    private int currentPosition;
    
    /** The cookie key */
    private byte[] cookie;
    
    /** The integer value for the cookie */
    private int cookieValue;
    
    /** The associated cursor for the current search request */
    private EntryFilteringCursor cursor;
    
    /**
     * Creates a new instance of this class, storing the Searchrequest into it.
     */
    public PagedSearchContext( InternalSearchRequest searchRequest )
    {
        previousSearchRequest = searchRequest;
        currentPosition = 0;
        
        // We compute a key for this cookie. It combines the search request
        // and some time seed, in order to avoid possible collisions, as
        // a user may send more than one PagedSearch on the same session.
        cookieValue = (int)(System.nanoTime()*17) + searchRequest.getMessageId();
        
        cookie = Value.getBytes( cookieValue );
    }
    
    
    /**
     * Compute a new key for this cookie, based on the current searchRequest 
     * hashCode and the current position. This value will be stored into the
     * session, and will permit the retrieval of this instance.
     * 
     * @return The new cookie's key
     */
    public byte[] getCookie()
    {
        return cookie;
    }

    
    public int getCookieValue()
    {
        return cookieValue;
    }
    
    
    /**
     * Compute a new cookie, if the previous one already exists. This
     * is unlikely, as we are based on some time seed, but just in case, 
     * this method will generate a new one.
     * @return The new cookie
     */
    public byte[] getNewCookie()
    {
        cookieValue = cookieValue + (int)(System.nanoTime()*17);
        cookie = Value.getBytes( cookieValue );
        <commit_msg>o Used an AtomicInteger for the cookie o Compute the cookie to avoid collisions<commit_after>/*
 *   Licensed to the Apache Software Foundation (ASF) under one
 *   or more contributor license agreements.  See the NOTICE file
 *   distributed with this work for additional information
 *   regarding copyright ownership.  The ASF licenses this file
 *   to you under the Apache License, Version 2.0 (the
 *   "License"); you may not use this file except in compliance
 *   with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing,
 *   software distributed under the License is distributed on an
 *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *   KIND, either express or implied.  See the License for the
 *   specific language governing permissions and limitations
 *   under the License.
 *
 */
package org.apache.directory.server.ldap.handlers.controls;

import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.directory.server.core.filtering.EntryFilteringCursor;
import org.apache.directory.server.ldap.LdapSession;
import org.apache.directory.shared.asn1.ber.tlv.Value;
import org.apache.directory.shared.ldap.constants.SchemaConstants;
import org.apache.directory.shared.ldap.exception.LdapException;
import org.apache.directory.shared.ldap.message.internal.InternalSearchRequest;
import org.apache.directory.shared.ldap.schema.AttributeType;
import org.apache.directory.shared.ldap.schema.SchemaManager;
import org.apache.directory.shared.ldap.util.StringTools;

/**
 * The structure which stores the informations relative to the pagedSearch control.
 * They are associated to a cookie, stored into the session and associated to an 
 * instance of this class.
 * 
 * @author <a href="mailto:<EMAIL>">Apache Directory Project</a>
 * @version $Rev:  $
 */
public class PagedSearchContext
{
    /** The previous search request */
    private InternalSearchRequest previousSearchRequest;
    
    /** The current position in the cursor */
    private int currentPosition;
    
    /** The cookie key */
    private byte[] cookie;
    
    /** The integer value for the cookie */
    private AtomicInteger cookieValue;
    
    /** The associated cursor for the current search request */
    private EntryFilteringCursor cursor;
    
    /**
     * Creates a new instance of this class, storing the SearchRequest into it.
     */
    public PagedSearchContext( InternalSearchRequest searchRequest )
    {
        previousSearchRequest = searchRequest;
        currentPosition = 0;
        
        // We compute a key for this cookie. It combines the search request
        // and some time seed, in order to avoid possible collisions, as
        // a user may send more than one PagedSearch on the same session.
        cookieValue = new AtomicInteger( searchRequest.getMessageId() << 16 );
        
        cookie = Value.getBytes( cookieValue.get() );
    }
    
    
    /**
     * Compute a new key for this cookie, based on the current searchRequest 
     * hashCode and the current position. This value will be stored into the
     * session, and will permit the retrieval of this instance.
     * 
     * @return The new cookie's key
     */
    public byte[] getCookie()
    {
        return cookie;
    }

    
    public int getCookieValue()
    {
        return cookieValue.get();
    }
    
    
    /**
     * Compute a new cookie, if the previous one already exists. This
     * is unlikely, as we are based on some time seed, but just in case, 
     * this method will generate a new one.
     * @return The new cookie
     */
    public byte[] getNewCookie()
    {
        cookie = Value.getBytes( cookieValue.incrementAndGet() );
        
{'fraction_non_alphanumeric': 0.0789818945158751, 'fraction_numerical': 0.0023615848858567303, 'mean_word_length': 2.9436109674081736, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '7493235', 'n_tokens_mistral': 2006, 'n_tokens_neox': 1978, 'n_words': 758}