|
![]() ![]()
|
Berkeley DB can store any kind of data, that is, it is entirely 8-bit clean. How you use this depends, to some extent, on the application language you are using. In the C/C++ languages, there are a couple of different ways to store structures and objects.
(버클리디비는 어떠한 종류의 데이타도 저장할수 있다.C/C++에서 구조체와 오브젝트를 저장하기위한 여러 방법이 있다.)
First, you can do some form of run-length encoding and copy your structure into another piece of memory before storing it:
(첫번째방법: 여러분은 어떤형태의 run-length엔코딩을 하고 저장하기 전에 구조체를 메모리에 복사할 수 있다.즉 바이트배열에 구조체필드의 값을 복사한다.)
struct {
char *data1;
u_int32_t data2;
...
} info;
size_t len;
u_int8_t *p, data_buffer[1024];
p = &data_buffer[0];
len = strlen(info.data1);
memcpy(p, &len, sizeof(len));
p += sizeof(len);
memcpy(p, info.data1, len);
p += len;
memcpy(p, &info.data2, sizeof(info.data2));
p += sizeof(info.data2);
...
and so on, until all the fields of the structure have been loaded into the byte array. If you want more examples, see the Berkeley DB logging routines (for example, btree/btree_auto.c:__bam_split_log()). This technique is generally known as "marshalling". If you use this technique, you must then un-marshall the data when you read it back:
(만약 예제를 원하면 Berkeley DB logging routines을 참조하라.(예를들어 btree/btree_auto.c:__bam_split_log()).이 기술은 일반적으로 마셜링으로 알려져 있다.마셜링을 사용했다면 데이타를 읽어들일때는 언마셜링해야 한다.)
struct {
char *data1;
u_int32_t data2;
...
} info;
size_t len;
u_int8_t *p;
p = &data_buffer[0];
memcpy(&len, p, sizeof(len));
p += sizeof(len);
info.data1 = malloc(len);
memcpy(info.data1, p, len);
p += len;
memcpy(&info.data2, p, sizeof(info.data2));
p += sizeof(info.data2);
...
and so on.
The second way to solve this problem only works if you have just one variable length field in the structure. In that case, you can declare the structure as follows:
(두번째방법: 가변적 길이를 가진 필드를 구조체가 가지고 있다면 다음처럼 구조체를 선언할 수 있다.)
struct { int a, b, c; u_int8_t buf[1]; } info;
Then, let's say you have a string you want to store in this structure. When you allocate the structure, you allocate it as:
(그런후 이 구조체에 문자열을 저장하기를 원한다고 해보자.구조체를 다음처럼 할당한다.)
malloc(sizeof(struct info) + strlen(string));
Since the allocated memory is contiguous, you can the initialize the structure as:
(할당된 메모리는 연속적이기 때문에 다음처럼 구조체를 초기화 할 수 있다.)
info.a = 1; info.b = 2; info.c = 3; memcpy(&info.buf[0], string, strlen(string) + 1);
and give it to Berkeley DB to store, with a length of:
(그리고 다음과 같은 길이로 버클리 디비에 저장한다.)
sizeof(struct info) + strlen(string);
In this case, the structure can be copied out of the database and used without any additional work.
(이경우 구조체는 디비로 부터 복사될 수 있고 추가작없이 사용가능하다.)
![]() ![]()
|
Copyright (c) 1996-2003 Sleepycat Software, Inc. - All rights reserved.