File Queue. Need some help. Thank You.

S

shapper

Hello,

I am trying to create a File Queue class with 5 methods: Add, Remove,
Update, Get and Commit.

Basically Add, Remove and Update do exactly that do the list.
Get finds if there is a file with that file name in the list and
returns its content (Byte[])
Commit calls a FileService to take the actions for all files in the
list.

I am holding the files as follows:
private IDictionary<String, KeyValuePair<String, Byte[]>> _files;

Which means:
private IDictionary<Action, KeyValuePair<Filename, Content>> _files;

Action can be Create, Delete and Update and based on this value a
different method on the service is used.
1. But I think the keys need to be unique so Action will not be.
2. I am also having some problem with Get method.

Anyway, could someone help me out with this?
Maybe I am creating this class in a wrong way?

public class FileQueue {

private String _path;
private IDictionary<String, KeyValuePair<String, Byte[]>> _files;

public FileQueue(String path) {

if (String.IsNullOrEmpty(path))
throw new ArgumentNullException("path", "Path cannot be
null");
_path = path;

} // FileQueue

public void Add(Byte[] content, String filename) {
_files.Add("Create", new KeyValuePair<String, Byte[]>(filename,
content));
} // Add

public void Get(String filename) {
if (_files.Values.Contains ??????
} // Get

public void Remove(String filename) {
_files.Add("Delete", new KeyValuePair<String, Byte[]>(filename,
null));
} // Remove

public void Update(Byte[] content, String filename) {
_files.Add("Update", new KeyValuePair<String, Byte[]>(filename,
content));
} // Update

} // FileQueue

Thank You,
Miguel
 
F

Family Tree Mike

Hello,

I am trying to create a File Queue class with 5 methods: Add, Remove,
Update, Get and Commit.

Basically Add, Remove and Update do exactly that do the list.
Get finds if there is a file with that file name in the list and
returns its content (Byte[])
Commit calls a FileService to take the actions for all files in the
list.

I am holding the files as follows:
private IDictionary<String, KeyValuePair<String, Byte[]>> _files;

Which means:
private IDictionary<Action, KeyValuePair<Filename, Content>> _files;

Action can be Create, Delete and Update and based on this value a
different method on the service is used.
1. But I think the keys need to be unique so Action will not be.
2. I am also having some problem with Get method.

Anyway, could someone help me out with this?
Maybe I am creating this class in a wrong way?

public class FileQueue {

private String _path;
private IDictionary<String, KeyValuePair<String, Byte[]>> _files;

public FileQueue(String path) {

if (String.IsNullOrEmpty(path))
throw new ArgumentNullException("path", "Path cannot be
null");
_path = path;

} // FileQueue

public void Add(Byte[] content, String filename) {
_files.Add("Create", new KeyValuePair<String, Byte[]>(filename,
content));
} // Add

public void Get(String filename) {
if (_files.Values.Contains ??????
} // Get

public void Remove(String filename) {
_files.Add("Delete", new KeyValuePair<String, Byte[]>(filename,
null));
} // Remove

public void Update(Byte[] content, String filename) {
_files.Add("Update", new KeyValuePair<String, Byte[]>(filename,
content));
} // Update

} // FileQueue

Thank You,
Miguel

A dictionary would not be good for that, in my opinion, for reasons more
than your unique dictionary key issue. You called this class a "Queue",
so why not extend the Queue<T> type?

I'm thinking something like this to hold the command/file data:

class FileCommandInfo
{
public string Command { get; set; }
public string Filename { get; set; }
public byte[] Data { get; set; }

public FileCommandInfo() { }
public FileCommandInfo(string command, string fn)
{
Filename = fn;
Command = command;
if (File.Exists(Filename))
Data = File.ReadAllBytes(Filename);
}
}

Then this to hold the queue:
class FileCommandQueue : Queue<FileCommandInfo>
{
// Replaces Miguel's Get function (a name I hate for a method).
public byte[] Search(string fn)
{
foreach (FileCommandInfo mfci in this)
if (mfci.Filename == fn) return mfci.Data;
}
}
 
S

shapper

I am trying to create a File Queue class with 5 methods: Add, Remove,
Update, Get and Commit.
Basically Add, Remove and Update do exactly that do the list.
Get finds if there is a file with that file name in the list and
returns its content (Byte[])
Commit calls a FileService to take the actions for all files in the
list.
I am holding the files as follows:
private IDictionary<String, KeyValuePair<String, Byte[]>>  _files;
Which means:
private IDictionary<Action, KeyValuePair<Filename, Content>>  _files;
Action can be Create, Delete and Update and based on this value a
different method on the service is used.
1. But I think the keys need to be unique so Action will not be.
2. I am also having some problem with Get method.
Anyway, could someone help me out with this?
Maybe I am creating this class in a wrong way?
   public class FileQueue {
     private String _path;
     private IDictionary<String, KeyValuePair<String, Byte[]>>  _files;
     public FileQueue(String path) {
       if (String.IsNullOrEmpty(path))
         throw new ArgumentNullException("path", "Path cannotbe
null");
       _path = path;
     } // FileQueue
     public void Add(Byte[] content, String filename) {
       _files.Add("Create", new KeyValuePair<String, Byte[]>(filename,
content));
     } // Add
     public void Get(String filename) {
       if (_files.Values.Contains ??????
     } // Get
     public void Remove(String filename) {
       _files.Add("Delete", new KeyValuePair<String, Byte[]>(filename,
null));
     } // Remove
     public void Update(Byte[] content, String filename) {
       _files.Add("Update", new KeyValuePair<String, Byte[]>(filename,
content));
     } // Update
   } // FileQueue
Thank You,
Miguel

A dictionary would not be good for that, in my opinion, for reasons more
than your unique dictionary key issue.  You called this class a "Queue",
so why not extend the Queue<T> type?

I'm thinking something like this to hold the command/file data:

class FileCommandInfo
{
   public string Command { get; set; }
   public string Filename { get; set; }
   public byte[] Data { get; set; }

   public FileCommandInfo() { }
   public FileCommandInfo(string command, string fn)
   {
     Filename = fn;
     Command = command;
     if (File.Exists(Filename))
       Data = File.ReadAllBytes(Filename);
   }

}

Then this to hold the queue:
class FileCommandQueue : Queue<FileCommandInfo>
{
   // Replaces Miguel's Get function (a name I hate for a method).
   public byte[] Search(string fn)
   {
     foreach (FileCommandInfo mfci in this)
       if (mfci.Filename == fn) return mfci.Data;
   }

}

I came up with the following. Not sure if it is the best approach but
any suggestion is welcome:

public class FileService {

private String _path;

public Queue<FileEntry> Queue { get; set; }

public FileService(String path) {

if (String.IsNullOrEmpty(path))
throw new ArgumentNullException("path", "Path cannot be
null");
_path = path;

} // FileService

public void Commit() {

foreach (FileEntry f in Queue) {

switch (f.Action) {
case FileAction.Create:
Create(f.Content, f.Filename);
break;
case FileAction.Delete:
Delete(f.Filename);
break;
case FileAction.Update:
Update(f.Content, f.Filename);
break;
}

}

Queue.Clear();

} // Commit

public void Create(Byte[] content, String filename) {

FileStream stream = new FileStream(String.Format("{0}/{1}",
_path, filename), FileMode.Create, FileAccess.ReadWrite);
BinaryWriter writer = new BinaryWriter(stream);
writer.Write(content);
writer.Close();

} // Create

public void Delete(String filename) {

File.Delete(String.Format("{0}/{1}", _path, filename));

} // Delete

public Byte[] Get(String filename) {

if (File.Exists(String.Format("{0}/{1}", _path, filename)))
return File.ReadAllBytes(String.Format("{0}/{1}", _path,
filename));
else
return null;

} // Get

public void Update(Byte[] content, String filename) {

Create(content, filename);

} // Update

} // FileService

Why not use Get?
I usually see Search as looking for something and returning True or
False if it was found or not ...
But that is just me.

Thanks,
Miguel
 
F

Family Tree Mike

public void Commit() {

foreach (FileEntry f in Queue) {

switch (f.Action) {
case FileAction.Create:
Create(f.Content, f.Filename);
break;
case FileAction.Delete:
Delete(f.Filename);
break;
case FileAction.Update:
Update(f.Content, f.Filename);
break;
}

}

Queue.Clear();

} // Commit

I would probably make that:

public void Commit()
{
while (Queue.Count > 0)
{
FileEntry f = Queue.Dequeue();
switch (f.Action)
{
// your code...
}
}
// no need for Queue.Clear().
}

Why not use Get?
I usually see Search as looking for something and returning True or
False if it was found or not ...
But that is just me.

Just my way of thinking, but "Get" implies a property to me, not a
method that Finds(). Come to think of it, that is what Microsoft uses,
Find(item), FindFirst(item), etc. Maybe we both can follow their lead.
 
S

shapper

On 2/3/2010 5:00 PM, shapper wrote:



<snip />









I would probably make that:

public void Commit()
{
   while (Queue.Count > 0)
   {
     FileEntry f = Queue.Dequeue();
     switch (f.Action)
     {
       // your code...
     }
   }
   // no need for Queue.Clear().

}

<snip />




Just my way of thinking, but "Get" implies a property to me, not a
method that Finds().  Come to think of it, that is what Microsoft uses,
Find(item), FindFirst(item), etc.  Maybe we both can follow their lead.

Thank you Mike.
I followed your suggestion about the loop.

And I also change Get to Find. It makes sense.
I often see Find to be used ... But get to ... So I never knew which
one to use.
Find seems logic because of the get/property relation.
And for me Find seems more logic then Search ... But that is just me.

Thank You,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top