summaryrefslogtreecommitdiff
path: root/src/myriad.cpp
blob: d3914df56b15873d9f5c5a401db6964d06261df1 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2007-2010 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#include "bu/myriad.h"
#include "bu/stream.h"
#include "bu/myriadstream.h"
#include <stdio.h>

#include "bu/sio.h"
using Bu::sio;
using Bu::Fmt;

#define Myriad_MAGIC_CODE		((unsigned char *)"\xFF\xC3\x99\xBD")

namespace Bu
{
	subExceptionDef( MyriadException )
	template<typename t> t blkDiv( t total, t block ) {
		return (total/block)+((total%block==0)?(0):(1));
	}
}

Bu::Myriad::Myriad( Bu::Stream &sStore ) :
	sStore( sStore ),
	iBlockSize( 0 ),
	iBlocks( 0 ),
	iUsed( 0 )
{
}

Bu::Myriad::~Myriad()
{
	updateHeader();

	for( StreamArray::iterator i = aStreams.begin(); i; i++ )
	{
		delete *i;
	}
}

void Bu::Myriad::sync()
{
	updateHeader();

	// Later, also flush all caches.
}

void Bu::Myriad::initialize()
{
	sStore.setPosEnd( 0 );
	int iSize = sStore.tell();
	sStore.setPos( 0 );

	unsigned char buf[4];
	if( sStore.read( buf, 4 ) < 4 )
		throw MyriadException("Input stream appears to be empty.");
	if( memcmp( buf, Myriad_MAGIC_CODE, 4 ) )
	{
		throw MyriadException(
			"Stream does not appear to be a valid Myriad format.");
	}
	sStore.read( buf, 2 );
	if( buf[0] != 1 )
		throw MyriadException(
			"We can only handle version 1 for now.");
	if( buf[1] != 32 )
		throw MyriadException(
			"We can only handle 32-bit words at the moment.");
	sStore.read( &iBlockSize, 4 );
	int iStreams;
	sStore.read( &iStreams, 4 );

	iBlocks = iSize/iBlockSize;
	sio << "Myriad: iSize=" << iSize << ", iBlockSize=" << iBlockSize
		<< ", iBlocks=" << iBlocks << ", iStreams=" << iStreams << sio.nl;
	
	int iHeaderSize = 14 + 8 + 4;
	int iHeaderBlocks = blkDiv( iHeaderSize+4, iBlockSize );

	while( iHeaderSize > iHeaderBlocks*iBlockSize )
	{
		iHeaderSize = 14 + 8 + 4*iHeaderBlocks;
		iHeaderBlocks = blkDiv( iHeaderSize+4, iBlockSize );
	}

	sio << "Myriad: iHeaderSize=" << iHeaderSize
		<< ", iHeaderBlocks=" << iHeaderBlocks << sio.nl;
	
	bsBlockUsed.setSize( iBlocks, true );

	bool bCanSkip = false; // Can skip around, post initial header stream i/o
	for( int j = 0; j < iStreams; j++ )
	{
		int iHdrBlock = 0;
		int iCurBlock = 0;
		aStreams.append( new Stream() );
		Stream &s = *aStreams[j];
		sStore.read( &s.iId, 4 );
		sStore.read( &s.iSize, 4 );
		int iSBlocks = blkDiv(s.iSize, iBlockSize);
		sio << "Myriad: - Stream::iId=" << s.iId << ", Stream::iSize=" << s.iSize
			<< ", Stream::aBlocks=" << iSBlocks
			<< ", sStore.tell()=" << sStore.tell() << sio.nl;
		for( int k = 0; k < iSBlocks; k++ )
		{
			int iBId;
			sStore.read( &iBId, 4 );
			sio << "Myriad:   - iBId=" << iBId
				<< ", iStartPos=" << iBId*iBlockSize
				<< ", sStore.tell()=" << sStore.tell() << sio.nl;
			s.aBlocks.append( iBId );
			bsBlockUsed.setBit( iBId );
			if( (j == 0 && k == iHeaderBlocks-1) )
			{
				sio << "Myriad:   - End of prepartition, unlocking skipping."
					<< sio.nl;
				bCanSkip = true;
				iCurBlock = blkDiv( (int)sStore.tell(), iBlockSize );
			}
			if( bCanSkip && sStore.tell() >= iCurBlock*iBlockSize+iBlockSize )
			{
				iHdrBlock++;
				iCurBlock = aStreams[0]->aBlocks[iHdrBlock];
				sio << "Myriad: Ran out of data in block, finding next header "
					"block:  " << iHdrBlock << " = " << iCurBlock << " ("
					<< iCurBlock*iBlockSize << "b)" << sio.nl;
				sStore.setPos( iCurBlock*iBlockSize );
			}
		}
	}

	sio << bsBlockUsed.toString() << sio.nl;

	//printf("%d blocks, %db each, %db block offset\n",
	//	iBlocks, iBlockSize, iBlockStart );

}

void Bu::Myriad::initialize( int iBlockSize, int iPreAllocate )
{
	int iHeaderSize = 14 + 8 + 4;
	int iHeaderBlocks = blkDiv( iHeaderSize+4, iBlockSize );
	char cBuf = 1;
	int iBuf = 0;
	
	Stream *pStr = new Stream;
	pStr->iId = 0;

	while( iHeaderSize > iHeaderBlocks*iBlockSize )
	{
		iHeaderSize = 14 + 8 + 4*iHeaderBlocks;
		iHeaderBlocks = blkDiv( iHeaderSize+4, iBlockSize );
	}

	iPreAllocate += iHeaderBlocks;

	sio << "Myriad:  iHeaderSize=" << iHeaderSize << ", iBlockSize="
		<< iBlockSize << ", iHeaderBlocks=" << iHeaderBlocks << sio.nl;
	
	bsBlockUsed.setSize( iPreAllocate, true );

	char *pBlock = new char[iBlockSize];
	memset( pBlock, 0, iBlockSize );
	for( int j = 0; j < iPreAllocate; j++ )
	{
		sStore.write( pBlock, iBlockSize );
		pStr->aBlocks.append( j );
	}
	delete[] (char *)pBlock;

	sStore.setPos( 0 );

	// Magic number
	sStore.write( Myriad_MAGIC_CODE, 4 );

	// Version (0)
	sStore.write( &cBuf, 1 );

	// Bits per int
	cBuf = 32;
	sStore.write( &cBuf, 1 );

	// The size of each block
	sStore.write( &iBlockSize, 4 );

	iBuf = 1;
	// The number of streams
	sStore.write( &iBuf, 4 );
	
	// Stream header
	iBuf = 0;
	sStore.write( &iBuf, 4 );
	sStore.write( &iHeaderSize, 4 );
	for( iBuf = 0; iBuf < iHeaderBlocks; iBuf++ )
	{
		sStore.write( &iBuf, 4 );
	}

	this->iBlockSize = iBlockSize;
	this->iBlocks = iPreAllocate;
	
	pStr->iSize = sStore.tell();

	//hStreams.insert( 0, BlockArray( 0 ) );
}

void Bu::Myriad::updateHeader()
{
	if( !sStore.canWrite() )
		return;

	

	// TODO: Use the stream class to access this really smoothly, I hope :)
}

int Bu::Myriad::createStream( int iPreAllocate )
{
	Stream *pStr = new Stream();
	pStr->iId = aStreams.last()->iId+1;
	sio << "Myriad: New stream id=" << pStr->iId << ", iPreAllocate="
		<< iPreAllocate << sio.nl;
	pStr->iSize = 0;

	for( int j = 0; j < iPreAllocate; j++ )
	{
		int iFreeBlock = findEmptyBlock();
		sio << "Myriad: Adding block " << j << sio.nl;
		pStr->aBlocks.append( j );
		bsBlockUsed.setBit( j );
	}

	return 0;
}

int Bu::Myriad::findEmptyBlock()
{
	for( int j = 0; j < bsBlockUsed.getSize(); j++ )
	{
		if( bsBlockUsed.getBit( j ) == false )
			return j;
	}
	sio << "Myriad: findEmptyBlock(): No empty blocks, adding new one." << sio.nl;

	bsBlockUsed.setSize( bsBlockUsed.getSize()+1, false );
	sStore.setPos( iBlockSize*iBlocks );

	char *pBlock = new char[iBlockSize];
	memset( pBlock, 0, iBlockSize );
	sStore.write( pBlock, iBlockSize );
	delete pBlock;

	return iBlockSize++;
}

void Bu::Myriad::deleteStream( int /*iID*/ )
{
}

Bu::MyriadStream Bu::Myriad::openStream( int iID )
{
	return MyriadStream( *this, iID );
}

int Bu::Myriad::getBlockSize()
{
	return iBlockSize;
}

int Bu::Myriad::getNumBlocks()
{
	return iBlocks;
}

int Bu::Myriad::getNumUsedBlocks()
{
	return iUsed;
}