Newer
Older
ubixfs-2 / btree_key.cpp
#include "btree_key.h"
#include "btree_types.h"
#include <string.h>

int 
compareKeyNull(void *, void *) {
  return 0;
} // compareKeyNull

int 
compareKeyString(void *, void *) { 
  return 0;
} // compareKeyString

int 
compareKeyPChar(void * key1, void * key2) {
  const char * charKey1 = static_cast<const char *>(key1);
  const char * charKey2 = static_cast<const char *>(key2);
  return strcmp(charKey1, charKey2);
} // compareKeyPChar

int 
compareKeySingle(void * key1, void * key2) {
  if ((key1 == NULL) || (key2 == NULL)) return 0;

  const float * floatKey1 = static_cast<const float *>(key1);
  const float * floatKey2 = static_cast<const float *>(key2);

  if (*floatKey1 < *floatKey2) return -1;
  else if (*floatKey1 > *floatKey2) return 1;

  return 0;
} // compareKeySingle

int 
compareKeyDouble(void * key1, void * key2) {
  if ((key1 == NULL) || (key2 == NULL)) return 0;

  const double * doubleKey1 = static_cast<const double *>(key1);
  const double * doubleKey2 = static_cast<const double *>(key2);

  if (*doubleKey1 < *doubleKey2) return -1;
  else if (*doubleKey1 > *doubleKey2) return 1;
  
  return 0;
} // compareKeyDouble

int 
compareKeyInt32(void * key1, void * key2) {
  if ((key1 == NULL) || (key2 == NULL)) return 0;

  const int32 * int32Key1 = static_cast<const int32 *>(key1);
  const int32 * int32Key2 = static_cast<const int32 *>(key2);

  if (*int32Key1 < *int32Key2) return -1;
  else if (*int32Key1 > *int32Key2) return 1;
 
  return 0;
} // compareKeyInt32

int 
compareKeyInt64(void * key1, void * key2) {
  if ((key1 == NULL) || (key2 == NULL)) return 0;

  const int64 * int64Key1 = static_cast<const int64 *>(key1);
  const int64 * int64Key2 = static_cast<const int64 *>(key2);

  if (*int64Key1 < *int64Key2) return -1;
  else if (*int64Key1 > *int64Key2) return 1;

  return 0;
} // compareKeyInt64

/*
 *  CopyKey functions
 */

void 
copyKeyNull(void *, void *) {
  return;
} // copyKeyNull

void 
copyKeyString(void * srcKey, void * destkey) {

} // copyKeyString

void 
copyKeyPChar(void * srcKey, void * destKey) {
  const char * charSrcKey = static_cast<const char *>(srcKey);
  char * charDestKey = static_cast<char *>(destKey);
  strcpy(charDestKey, charSrcKey);
} // copyKeyPChar

void 
copyKeySingle(void * srcKey, void * destKey) {
  if ((srcKey == NULL) || (destKey == NULL)) return;
  *(float *)destKey = *(float *)srcKey;
} // copyKeySingle

void 
copyKeyDouble(void * srcKey, void * destKey) {
  if ((srcKey == NULL) || (destKey == NULL)) return;
  *(double *)destKey = *(double *)srcKey;
} // copyKeyDouble

void 
copyKeyInt32(void * srcKey, void * destKey) {
  if ((srcKey == NULL) || (destKey == NULL)) return;
  *(int32 *)destKey = *(int32 *)srcKey;
} // copyKeyInt32

void 
copyKeyInt64(void * srcKey, void * destKey) {
  if ((srcKey == NULL) || (destKey == NULL)) return;
  *(int64 *)destKey = *(int64 *)srcKey;
} // copyKeyInt64

/* 
 *  KeySize functions
 */

int 
keySizeNull(void *) {
  return 0;
} // keySizeNull

int 
keySizeString(void * key) {
  const char * charKey = static_cast<const char *>(key);
  if (charKey == NULL) return 0;
  return ((int)charKey[0]+1);
} // keySizeString

int 
keySizePChar(void * key) {
  const char * charKey = static_cast<const char *>(key);
  if (charKey == NULL) return 0;
  return (strlen(charKey)+1);
} // keySizePChar

int 
keySizeSingle(void *) {
  return sizeof(float);
} // keySizeSingle

int 
keySizeDouble(void *) {
  return sizeof(double);
}

int 
keySizeInt32(void *) {
  return sizeof(int32);
}

int 
keySizeInt64(void *) {
  return sizeof(int64); 
}