Search:

Search all manuals
Search this manual
Manual
Couchbase Client Library: .NET (C#) 1.1
Community Wiki and Resources
Wiki: .NET Client Library
Download Client Library
.NET Client Library
SDK Forum
Additional Resources
Community Wiki
Community Forums
Couchbase SDKs
Parent Section
7 Update Operations
Chapter Sections
Chapters

7.6. Prepend Methods

The Prepend() methods allow you to add information to an existing key/value pair in the database. You can use this to add information to a string or other data before the existing data.

The Prepend() methods prepend raw serialized data on to the end of the existing data in the key. If you have previously stored a serialized object into Couchbase and then use Prepend, the content of the serialized object will not be extended. For example, adding an List of integers into the database, and then using Prepend() to add another integer will result in the key referring to a serialized version of the list, immediately preceded by a serialized version of the integer. It will not contain an updated list with the new integer prepended to it. De-serialization of objects that have had data prepended may result in data corruption.

API Callobject.Prepend(key, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsObject ( Binary object )
Arguments 
string key Document ID used to identify the value
object value Value to be stored

The Prepend() method prepends information to the end of an existing key/value pair.

The sample below demonstrates how to create a csv string by prepending new values.

client.Store(StoreMode.Set, "beers", "Abbey Ale");
Func<string, byte[]> stringToBytes = (s) => Encoding.Default.GetBytes(s);
client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Three Philosophers,")));
client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Witte,")));

You can check if the Prepend operation succeeded by using the checking the return value.

var result = client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Hennepin,")));
if (result) {
	Console.WriteLine("Prepend succeeded");
} else {
	Console.WriteLine("Prepend failed");
}
API Callobject.Prepend(key, casunique, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsObject ( Binary object )
Arguments 
string key Document ID used to identify the value
ulong casunique Unique value used to verify a key/value combination
object value Value to be stored

Prepend() may also be used with a CAS value. With this overload, the return value is a CasResult, where success is determined by examining the CasResult's Result property.

var casv = client.GetWithCas("beers");
var casResult = client.Prepend("beers", casv.Cas, new ArraySegment<byte>(stringToBytes("Adoration,")));

if (casResult.Result) {
    Console.WriteLine("Prepend succeeded");
} else {
    Console.WriteLine("Prepend failed");
}
API Callobject.ExecutePrepend(key, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsIConcatOperationResult ( Concat operation result )
Arguments 
string key Document ID used to identify the value
object value Value to be stored

The ExecutePrepend() behaves similar to Prepend(), but instead of a Boolean it returns an instance of an IConcatOperationResult.

var concatResult = client.Store(StoreMode.Set, "beers", "Abbey Ale");
Func<string, byte[]> stringToBytes = (s) => Encoding.Default.GetBytes(s);
client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Three Philosophers,")));
client.Prepend("beers", new ArraySegment<byte>(stringToBytes("Witte,")));

if (! concatResult.Success) {
	logger.Error(concatResult.Message, concatResult.Exception);
}
API Callobject.ExecutePrepend(key, casunique, value)
Asynchronousno
Description Prepend a value to an existing key
ReturnsIConcatOperationResult ( Concat operation result )
Arguments 
string key Document ID used to identify the value
ulong casunique Unique value used to verify a key/value combination
object value Value to be stored

ExecutePrepend() may also be used with a CAS value. With this overload, the return value is an instance of an IConcatOperationResult.

var getResult = client.ExecuteGet("beers");
var concatResult = client.Prepend("beers", getResult.Cas, new ArraySegment<byte>(stringToBytes("Adoration,")));

if (concatResult.Success) {
    Console.WriteLine("Prepend succeeded");
} else {
    Console.WriteLine("Prepend failed");
}