2002-04-29 13:22:26 +02:00
|
|
|
/*
|
|
|
|
* PROGRAM: JRD Sort
|
|
|
|
* MODULE: sort_mem.h
|
|
|
|
* DESCRIPTION: Sort Space Management
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Interbase Public
|
|
|
|
* License Version 1.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.Inprise.com/IPL.html
|
|
|
|
*
|
|
|
|
* Software distributed under the License is distributed on an
|
|
|
|
* "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express
|
|
|
|
* or implied. See the License for the specific language governing
|
|
|
|
* rights and limitations under the License.
|
|
|
|
*
|
|
|
|
* The Original Code was created by Inprise Corporation
|
|
|
|
* and its predecessors. Portions created by Inprise Corporation are
|
|
|
|
* Copyright (C) Inprise Corporation.
|
|
|
|
*
|
|
|
|
* All Rights Reserved.
|
|
|
|
* Contributor(s): ______________________________________.
|
|
|
|
*
|
|
|
|
* 2002.04.28 Dmitry Yemanov - Created new implementation of temporary
|
|
|
|
* sort space that allows virtual memory to
|
|
|
|
* be used as much as possible (or as much
|
|
|
|
* as server configured for).
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef SORT_MEM_H
|
|
|
|
#define SORT_MEM_H
|
|
|
|
|
|
|
|
#include "../jrd/sort.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Virtual scratch file
|
|
|
|
*/
|
|
|
|
|
|
|
|
class SortMem {
|
|
|
|
private:
|
|
|
|
|
|
|
|
// Generic storage block
|
|
|
|
class Block {
|
|
|
|
|
|
|
|
friend class SortMem;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Block *prev;
|
|
|
|
Block *next;
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t size;
|
2002-04-29 13:22:26 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Read bytes from the block into the given buffer
|
2002-10-26 10:21:16 +02:00
|
|
|
virtual size_t read(STATUS*, size_t, char*, size_t) = 0;
|
2002-04-29 13:22:26 +02:00
|
|
|
// Write bytes from the given buffer into the block
|
2002-10-26 10:21:16 +02:00
|
|
|
virtual size_t write(STATUS*, size_t, char*, size_t) = 0;
|
2002-04-29 13:22:26 +02:00
|
|
|
|
2002-10-26 10:21:16 +02:00
|
|
|
Block(Block*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
virtual ~Block() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Virtual memory block
|
|
|
|
class MemoryBlock : public Block {
|
|
|
|
private:
|
|
|
|
// Base address of the allocated virtual memory
|
|
|
|
char* address;
|
|
|
|
|
|
|
|
public:
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t read(STATUS*, size_t, char*, size_t);
|
|
|
|
size_t write(STATUS*, size_t, char*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
|
2002-10-26 10:21:16 +02:00
|
|
|
MemoryBlock(Block*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
~MemoryBlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
// File block
|
|
|
|
class FileBlock : public Block {
|
|
|
|
private:
|
|
|
|
// Sort file block
|
|
|
|
struct sfb *file;
|
|
|
|
// File offset
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t offset;
|
2002-04-29 13:22:26 +02:00
|
|
|
|
|
|
|
public:
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t read(STATUS*, size_t, char*, size_t);
|
|
|
|
size_t write(STATUS*, size_t, char*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
|
2002-10-26 10:21:16 +02:00
|
|
|
FileBlock(Block*, size_t, struct sfb*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
~FileBlock();
|
|
|
|
};
|
|
|
|
|
|
|
|
// Initialization flag
|
|
|
|
static bool is_initialized;
|
|
|
|
|
|
|
|
// Virtual memory allocation values
|
2002-10-26 10:21:16 +02:00
|
|
|
static size_t mem_block_size;
|
|
|
|
static size_t mem_upper_limit;
|
2002-04-29 13:22:26 +02:00
|
|
|
|
|
|
|
// Total amount of allocated virtual memory
|
2002-10-26 10:21:16 +02:00
|
|
|
static size_t mem_total_size;
|
2002-04-29 13:22:26 +02:00
|
|
|
|
|
|
|
struct sfb *internal;
|
|
|
|
|
|
|
|
// Virtual scratch file size
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t logical_size;
|
2002-04-29 13:22:26 +02:00
|
|
|
// Amount of storage space allocated for this scratch file
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t physical_size;
|
2002-04-29 13:22:26 +02:00
|
|
|
// File size on disk
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t file_size;
|
2002-04-29 13:22:26 +02:00
|
|
|
|
|
|
|
// First block in chain
|
|
|
|
Block *head;
|
|
|
|
// Last block in chain
|
|
|
|
Block *tail;
|
|
|
|
|
|
|
|
// Allocate one more block, if necessary
|
2002-10-26 10:21:16 +02:00
|
|
|
void allocate(size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
// Convert logical position to the physical one - pair [block, offset]
|
2002-10-26 10:21:16 +02:00
|
|
|
Block* seek(size_t&);
|
2002-04-29 13:22:26 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
// Read bytes from the scratch file
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t read(STATUS*, size_t, char*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
// Write bytes into the scratch file
|
2002-10-26 10:21:16 +02:00
|
|
|
size_t write(STATUS*, size_t, char*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
|
2002-10-26 10:21:16 +02:00
|
|
|
SortMem(struct sfb*, size_t);
|
2002-04-29 13:22:26 +02:00
|
|
|
~SortMem();
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SORT_MEM_H
|