summaryrefslogtreecommitdiffstats
path: root/indexlib/leafdatavector.cpp
blob: 56a3986c048013a46a7e57c42450367a3dac45e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108

/* This file is part of indexlib.
 * Copyright (C) 2005 Luís Pedro Coelho <luis@luispedro.org>
 *
 * Indexlib is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License, version 2, as
 * published by the Free Software Foundation and available as file
 * GPL_V2 which is distributed along with indexlib.
 * 
 * Indexlib is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA
 * 
 * In addition, as a special exception, the copyright holders give
 * permission to link the code of this program with any edition of
 * the Qt library by Trolltech AS, Norway (or with modified versions
 * of Qt that use the same license as Qt), and distribute linked
 * combinations including the two.  You must obey the GNU General
 * Public License in all respects for all of the code used other than
 * Qt.  If you modify this file, you may extend this exception to
 * your version of the file, but you are not obligated to do so.  If
 * you do not wish to do so, delete this exception statement from
 * your version.
 */

#include "leafdatavector.h"
#include "mmap_manager.h"
#include "compressed.h"
#include "logfile.h"
#include "path.h"

#include "format.h"
#include <unistd.h>

#ifdef USE_ZLIB_COMPRESSION
typedef compressed_file leafdatavector_manager;
#else
typedef mmap_manager leafdatavector_manager;
#endif

leafdatavector::leafdatavector( std::string name ):
	leafs_( std::auto_ptr<memory_manager>( new leafdatavector_manager( path_concat( name, "leafs" ) ) ) ),
	table_( path_concat( name, "table" ) )
{
}

void leafdatavector::remove( std::string name ) {
	leafdatavector_manager::remove( path_concat( name, "leafs" ) );
	memvector<leaf_dataptr>::remove( path_concat( name, "table" ) );
}

void leafdatavector::add( unsigned idx, unsigned what ) {
	//logfile() << format( "leafdatavector::add( %s, %s )\n" ) % idx % what;
	table_.resize( idx + 1 );
	int32_t now = table_[ idx ];
	if ( !now ) {
		++what;
		table_[ idx ] = -int( what );
	} else if ( now < 0 ) {
		leafdataptr just = leafs_.allocate( leaf_data_pool_traits::min_size() );
		leafdata::construct( just.raw_pointer() );
		table_[ idx ] = just.cast_to_uint32();
		just->add_reference( -now - 1 );
		assert( just->can_add( what ) );
		just->add_reference( what );
	} else {
		leafdataptr just = leafdataptr::cast_from_uint32( now );
		if ( !just->can_add( what ) ) {
			just = leafs_.reallocate( just, just->next_byte_size() );
			just->grow();
			table_[ idx ] = just.cast_to_uint32();
		}
		just->add_reference( what );
	}
}

void leafdatavector::remove_references_to( unsigned ref ) {
	//logfile() << format( "%s( %s )\n" ) % __PRETTY_FUNCTION__ % ref;
	for ( unsigned idx = 0; idx != table_.size(); ++idx ) {
		int32_t now = table_[ idx ];
		if ( now == -int( ref ) ) table_[ idx ] = 0;
		else if ( now > 0 ) leafdataptr::cast_from_uint32( now )->remove_reference( ref );
	}
}

std::vector<unsigned> leafdatavector::get( unsigned idx ) const {
	if ( idx >= table_.size() ) return std::vector<unsigned>();
	int32_t now = table_[ idx ];
	if ( now < 0 ) {
		std::vector<unsigned> res;
		res.push_back( -now - 1 );
		return res;
	} else if ( now > 0 ) {
		logfile() << format( "%s( %s ) in %s\n" ) % __PRETTY_FUNCTION__ % idx % now;
		leafdataptr just = leafdataptr::cast_from_uint32( now );
		return std::vector<unsigned>( just->begin(), just->end() );
	} else {
		return std::vector<unsigned>();
	}
}